How to access fonts embedded in a Flash/Animate SWF

I have fonts embedded in a swf that I want to access directly using the class names I’ve given them.

If the class name specified in the flash file is BoldFont and I have checked Export for Actionscript and Export for runtime sharing and exported the movie, why do I get a “Type not found : BoldFont” error in the line var font = new BoldFont();

I believe this may work on the Flash target, on HTML5 we require webfonts, and do not have a converter from the embedded vector font format in the SWF to TrueType or webfonts, similarly we require a TTF or OTF font on native platforms.

We handle static text using our vector API, but our TextField is designed for standard fonts at the moment, so we recommend that you use a font name in the SWF that matches the name of the real font, then we will set textFormat.font to that name at runtime, so that if you also include the same font TTF/OTF in your Assets directory, we can match the two up and show your font correctly

1 Like

I assume you mean something like this which I got to work:

var font:Font = new Font("Mryiad Pro");
var labelFormat:TextFormat=new TextFormat(font.fontName, 48, 0xffffff, true);

I assume the success of that is dependent on the user having that font on their system.

The Font class API says that the class is available for “Available on all platforms” and “is used to manage embedded fonts in SWF files. Embedded fonts are represented as a subclass of the Font class. The Font class is currently useful only to find out information about embedded fonts; you cannot alter a font by using this class. You cannot use the Font class to load external fonts, or to create an instance of a Font object by itself. Use the Font class as an abstract base class.”

What is meant by “manage embedded fonts in SWF files?”

How do you use “the Font class as an abstract base class.”



how i managed fonts in my last project :

1 - Created a Dynamic TexField in Flash SWF and embedded the fonts one by one to that text field.

and then those are sitting in library like :


2 - then i put font files [get them from windows fonts folder] in the assets folder:


when i compiled the project via CLI it showed me the web fonts are missing warnings however the font’s were generated and were in bin/html5/fonts so i copied the webfonts from bin fonts folder to obj fonts folder and warnings were gone but it happened randomly i didn’t get those warnings again. any ways the fonts are now accessible in the project.

3 - Create a text field with the format and put the actual name of the font [i didn’t try the “name” which i gave them in the Flash]

format = new TextFormat ("Poppins", 46, getRandomColor(), true);
txt = new TextField();
txt.width = 700;
txt.text = CURSSOR;
txt.defaultTextFormat = format;

and that’s how i accessed my fonts in the project .
[note that in HTML5 the webfonts are loaded those fonts are way smaller in size [bits] so they load quickly.]

i hope this helps you or any one else going from same process .

EDIT : after reading @singmajesty answer :smiley:

i didn’t even check the demos lol that’s way more simple then i did :smiley:

var font = Assets.getFont ("fonts/FreebooterUpdated.ttf");
var defaultFormat = new TextFormat (font.fontName, 60, 0x000000);

Take a look at some of our samples, such as “AddingText” or “PiratePig”

openfl create PiratePig
cd PiratePig
openfl test html5

There is a font in the asset directory. We A.) convert and embed the font on the Flash target, B.) convert and use the font as webfonts on HTML5, or C.) copy and use the font directly on native targets. This applies to most other fonts.

In Flash, you cannot new Font, I believe, but instead have to extend the Font class by embedding in Flash Professional, as you did earlier. This obviously is not a restriction in OpenFL’s other targets, though this specific aspect of Flash has been somewhat difficult to make entirely consistent. If only we could generate new classes at runtime :slight_smile: