Loading TTF fonts in runtime

Hiya!

I’m creating a graphical interface / client with OpenFL to plug onto a RakNet backend. The current system is such that every asset used in the client is loaded from the RakNet server. I’ve already got the system working with loading in bitmaps using BitmapData.fromBytes.

However, I also need to load in .ttf fonts. I think the font is properly loaded into a Font object since it gives the correct font name when loaded in with Font.fromBytes. I need to find a way to use that Font object in a TextField though and I can’t figure out how to do that. Most sources seem to say that it’s not possible to load fonts in runtime; even the API docs for the Font class say that you can’t load in external fonts using it.

So, is it possible to use a font loaded at runtime in a TextField and if so, how can I do that? Should I find a way to squeeze it in with the embedded assets accessed via Assets or is there another way to achieve this?

Many thanks in advance!

1 Like

After you load the font, does Font.registerFont help you?

Try and use the font name in your TextField too

Hey, sorry for the late reply.

I don’t know how I would pass the Font object into Font.registerFont as it requires a Class<Dynamic> as input. From what I can tell these are normally made by embedding the font in compile-time with @:font(path) and creating classes for all needed fonts, essentially hardcoding them in.

Is there a way to generate one of these classes in runtime? Or is there another way of registering the font?

Oh, gotcha. Yeah, it’s possible to extend Font with a custom class that has the correct path and name set, but perhaps we should find a way to make this more automatic