How to add dynamically fonts ? (for openfl)

Hi !
I was wondering, how to dynamically add fonts ?
I basically do

var font = openfl.text.Font.fromBytes(fontBytes);
        openfl.text.Font.registerFont(font);

but it doesn’t seem haxeui is using my new font. How to register the font with haxeui ?

And you want to add fonts at runtime i guess? There is code for it… haxeui-openfl/AssetsImpl.hx at master · haxeui/haxeui-openfl · GitHub

Do you have a full test app that isnt working?

Cheers,
Ian

When I look a the code , It seems that the font needs to be in the Assets or in Resource. But I don’t have a dynamic way to add my font to the Assets or Resource ( I think … ) .
The thing is I open an epub, in the epub there are fonts, that I unzip to use them.

Inspired by the code , I tried

var font = openfl.text.Font.fromBytes(fontBytes);
		var fontInfo = {
            data: font.fontName
        }
        openfl.text.Font.registerFont(font);
		haxe.ui.ToolkitAssets.instance._onFontLoaded(font.name, fontInfo); // With a private acesse, but it doesn't seem to work

in fact font loaded also mentions resourceID

private function _onFontLoaded(resourceId:String, font:FontInfo) {
        if (_fontCache == null) {
            _fontCache = new Map<String, FontInfo>();
        }
        _fontCache.set(resourceId, font);
        _fontCallbacks.invokeAndRemove(resourceId, font);
    }

So maybe in fact the problem, how do I add a resource id dynamically ?

Do these functions help at all:

You would need to change them around a little, but if they do work, it would be nice to add a function there “fontFromBytes” or something.

EDIT: what openfl target are you using?

I’m using Linux target :slight_smile:

I found a solution by modifying getFontFromHaxeResource, so that it looks in registered openfl fonts :slight_smile:

    @:access(openfl.text.Font.__fontByName)
    private override function getFontFromHaxeResource(resourceId:String, callback:String->FontInfo->Void) {
	if (openfl.text.Font.__fontByName.get(resourceId) != null) {
		var fontInfo = {
		    data: resourceId
		}
		callback(resourceId, fontInfo);
		return;
	}
        var bytes = Resource.getBytes(resourceId);
        if (bytes == null) {
            callback(resourceId, null);
            return;
        }
        
        #if (js && html5)
        
        loadWebFontFontResourceDynamically(resourceId, bytes, callback);
        
        #else
        
        var font = openfl.text.Font.fromBytes(bytes);
        openfl.text.Font.registerFont(font);
        var fontInfo = {
            data: font.fontName
        }
        callback(resourceId, fontInfo);
        
        #end
    }
    

can you paste in your full working code? Maybe we can add a “fontFromBytes” into the AssetsBase class… could be useful in the future. But before i refactor the code, id like to see the full code of what worked for you.

Cheers,
Ian

EDIT: oh, hang on, that is the working modified code, correct?

yes it’s the modified code :slight_smile:

So that I just need to do

            var font = openfl.text.Font.fromBytes(fontBytes);
            openfl.text.Font.registerFont(font);

and haxe ui takes care of it, once the font is registered.

So i think thats a good optimisation anyway… no point in loading it if its already loaded!

Ill also see about exposing some “fontFromBytes” call… ill write here when ive made the changes and tested locally… maybe you can test it in your app for me?

Cheers,
Ian

maybe you can test it in your app for me?
Of course ! :slight_smile:

1 Like

In fact my modification was useless :hot_face:. It works without it , ( you still need to register fonts with openfl though). The fonts ( and more precisely the description of the fonts) were the problem, they give a bad name.

right, so in the end you just needed to do:

        var font = openfl.text.Font.fromBytes(bytes);
        openfl.text.Font.registerFont(font);

?
Do you still think it would be useful to have a “fontFromBytes” call in HaxeUIAssets?

I actually think it would.
Maybe it’s far away, but you can already have gui theme for haxeui. Maybe you could use external ui themes ( with their own fonts), and easily enable to skin a program which is made with haxe ui

You can definantely already include fonts into a haxeui theme, i do it here: haxeui-theme-kenney/module.xml at master · haxeui/haxeui-theme-kenney · GitHub

EDIT: ill look at adding the fontFromBytes function, i can see its utility (like imageFromBytes)