Runtime Font Loading and Static TextField Conflicts

June 3rd, 2009 | by Matt |

So I have this dilemma. It's unbelievably frustrating. I want to believe a feasible workaround exists, but after hours of messing around, I couldn't come up with anything. Allow me to explain, maybe some of you have an idea of what to do.

I'm working on a Flash site that needs to handle various languages and lots of dynamic text. In order to facilitate this, I've decided to implement runtime font loading and embedding. Each font is exported into its own SWF file and a class is attached to it. For instance, I have ArcherBold.fla which has one font item in the library named ArcherBold that is set to export for ActionScript and the class fonts.ArcherBold (which extends flash.text.Font) is attached to it.

Within the application, I load the specified fonts that are defined in a config file. This config file specifies the the font name, and the class name, and the path to the SWF file. For example, a font definition for the site for Archer Bold looks like so:

<font>
  <fontName>Archer Bold</fontName>
  <fontClass>fonts.ArcherBold</fontClass>
  <fontPath>shared/fonts/archerbold.swf</fontPath>
</font>

The site then loads the SWF file, then registers the font class with the global Font.registerFont() method. With this in place, I can then populate TextFields with HTML formatted text and font tags to render the text with the desired font. For example:

<font face="Archer Bold" size="20">My Text</font>

All seems fine and dandy there. No problems whatsoever. But then an odd case came up. I started placing a few static TextFields in parts of the site that use the same font (Archer Bold) that is loaded at run time. Can you imagine what happens next? Surprise! The dynamic text fields no longer render using the run time loaded fonts. In fact, they don't render at all! 

I tried all sorts of useless things to alleviate the problem but to no avail. A coworker and I did did come up with something, but its a totally convoluted workaround. Basically, you have to have to have two copeis of the same font, but adjust the name. For instance, you would have to use a program to duplicate the font and give it another name, such as Archer Bold Static. Anyone placing static text in an FLA would have to use that font instead of the original font. That's just ridiculous if you ask me. The only other option I can see is to make all TextFields dynamically populated, but that would just be a pain in the ass. Especially if we start to include SWFs created by inexperienced developers or designers.

Mind you, I'm still on CS3 (I know, right?). Perhaps CS4 and Flash Player 10 can solve this problem. If anyone knows, I'd greatly appreciate some help.

Update - June 4th: So I installed the trial version of Flash CS4 and ran a test using Flash Player 10. Surprise! Same result. Arrrrgggghhh!

  1. 8 Responses to “Runtime Font Loading and Static TextField Conflicts”

  2. I believe you don’t have to make the textfields dynamically populated. If you can get away with it, why not just embed the Archer Bold font in the main swf? Instead of static text fields, just make them dynamic with the font embedded.

    Rich Rodecker on Jun 3, 2009 | Reply

  3. break all the text apart in the static text fields…? :D :D …sorry matt :P

    emma on Jun 3, 2009 | Reply

  4. @Rich Trust me, I’d love to do that. Run time font loading gives me much more flexibility when adding another region/language in the future. Not every language needs certain fonts, and even some languages only use system fonts to render the characters (such as Chinese). So embedding fonts in the main SWF is out of the question. I’d have to embed all sorts of fonts/characters to accommodate every language the site will be in.

    Matt on Jun 3, 2009 | Reply

  5. @emma I did think of that! But gosh, what a pain it would be. Blah!

    Matt on Jun 3, 2009 | Reply

  6. Try doing a

    var array:Array = Font.enumerateFonts(true);
    for each (var font:Font in array) { trace(font.fontName); }

    with the static font present in your fla and before the loaded font is registered, then also execute that code after and see how the trace changes. This may give you some insight into what is happening, namely I imagine that font is already registered when it is set to static in the fla and therefore when you try to register the loaded one they collide.

    One workaround would be to rename the font that is being loaded at runtime to Archer Bold Embed and use that as a unique reference whenever you want the font.

    Another idea that might be worth trying is to set the TextField to be dynamic rather than static and enter in your text as usual. The textfield should retain the text you entered, and possibly (untested) use the runtime loaded font effectively giving you the best of both worlds.

    Hope this helps!

    Elliot Chong on Jun 5, 2009 | Reply

  7. Thanks Elliot for the ideas. I’ve actually tried tracing out the enumerated fonts as you say and there are no clues as to whats happening there. Additionally, setting TextFields to dynamic causes the same conflict. I did, however, settle on creating a copy of the font with a hyphenated name using TransType. The hyphenated names are used to load at runtime and the regular versions are used for any text placed at author-time. Really annoying, but seems to be only way around this issue.

    Matt on Jun 5, 2009 | Reply

  8. Happened to me too, and we figured it out.
    This happens because when you assign the font to a static or dynamic text field, a class holding the font (holding only the characters you used in the text fields, or more if you embedded any in the dynamic ones) is generated and put into the application domain. You can not override classes with the same name already in the application domain, so when you load the font file, the font class in it is silently ignored (this applies to all classes you try to override).
    The only workaround I could come up with is breaking (ctrl+b) all the static text fields until they’re just shapes, and putting the font of the dynamic text fields to default system font, and then set the font to the loaded one dynamically on runtime after the font has been loaded.
    Just make sure that the font is not already in use anywhere or it will not be loaded.

    Ari on Jun 8, 2009 | Reply

  9. Hi,
    have the same problem, so frustrating.

    So what is the best solution ?

    Could you explain how you hyphenate the font name in TransType ?

    Thank you.

    Max on Jul 3, 2009 | Reply

Post a Comment