Font loading and getBytes

Hi,

This is a pretty generic question that I ran into using haxeui-openfl html5 build.

If I have test.ttf in assets/fonts openfl/lime will load the file as a font resource and Haxeui can automatically use it.
Now I would like to access the actual bytes of that resource (because an external pdf library needs it). And I expect openfl.Assets.getBytes(“assets/fonts/test.ttf”) would give me that. But that fails.

Would you perhaps know why?

I can duplicate the font file, rename it test.wtf and load those bytes. And that works fine! But I’m actually using Arial Unicode MS which is 25MB, so I don’t what to add 50MB(ttf+wtf) to an HTML5 App.

I can also add to get the bytes, but that prevents the font from getting loaded automatically, which is not what I want.

Alternatively, it is possible to get bytes in a different way using a macro:
@:font(“assets/font/Arial-Unicode.ttf”)
private class ArialUnicodeMS extends openfl.text.Font {}

And then later:
var bytes:haxe.io.Bytes = haxe.Resource.getBytes(ArialUnicodeMS.resourceName);
var ba:flash.utils.ByteArray = openfl.utils.ByteArray.fromBytes(bytes);

But these are not the same bytes as those you get from Assets.getBytes. Seems to be the bytes of the instanced font object instead of the raw bytes.

So, in short, I’m looking for a way to get the raw bytes of a font object. You seem pretty knowledgeable about fonts :grin:, do you know how to get those pesky bytes while having the fonts loaded normally?

Thanks!

I guess you need to know how openfl is storing the fonts… i would have assumed it was in haxe.Resource, but i could be way off here. Have you tried iterating through the haxe.Resource names? Anything interesting there? Alternatively you could use haxeui module to store your fonts, haxeui will then expand them and turn them into openfl fonts, and they are stored as haxe.Resources, issue then it is a haxeui dependant solution, but that may not be a problem for you?

Not sure if that helps at all, but might be asking the openfl lot “where do embedded fonts end up?”

Cheers,
Ian

Thanks Ian, those seem like interesting options. Haxeui dependency is no problem.

I did however run into the fact that regardless of these issues, the PDFs that are generated also need the font, and also end becoming a lot bigger, duh. And 15 MB PDFs is not really acceptable. Unicode fonts with decent actual character support is still a pretty hard issue it seems.

Maybe fonts should be stripped based on actual glyphs used. Wouldn’t work for user input though…

1 Like