May
15

Error Embedding Fonts Across ApplicationDomains Mike Baker

Actionscript, Flash

I ran into a frustrating problem the other day while working on some client work. While attempting to register a font with Flash at runtime I was getting the following error:

Error #1508: The value specified for argument font is invalid.

Let me describe the situation. With the sites I’ve been working on recently we deploy the sites to 40+ different languages. Since embedding the entire UTF-8 character set results in users downloading multiple megs per font we use dynamic font embedding to embed specific ranges. One added advantage is that we can substitute fonts at run time in situations where the font we are using doesn’t support a specific character set. Our designers enjoy that they can use their fancy fonts and aren’t restricted to the handful of fonts which support the Georgian character set. In addition to dynamically embedding the fonts I was loading content into separate Application Domains with each of those applications attempting to register fonts.

Normally a font would be registered by:

var fontClass:Class = getDefinitionByName(FONT_LINKAGE) as Class;
Font.registerFont(fontClass);

The error was throwing when the loaded content went to register fonts in its own Application Domain. I’ve been using runtime font embedding for some time now so the cause of the error above wasn’t immediately obvious. After a great deal of tweak and testing I’ve come to the conclusion that somewhere behind the scenes, Flash doesn’t like having fonts registered across multiple application domains when running locally. Oddly enough when deployed online everything works as expected.

My workaround, albeit a bit gross, is to pass the host application domain into the loaded content and use the host application domain’s Font class to register the font. The code to do this looks something like:

var fontClass:Class = getDefinitionByName(FONT_LINKAGE) as Class;
hostApplicatonDomain.getDefinition('flash.text.Font').registerFont(fontClass);

Where hostApplicationDomain is the main applications Application Domain which I’ve passed in before hand.

I assume you could pass font instances you want to register back to the host application in an event instead of the approach I used. I opted to pass the host ApplicationDomain into the loaded content since I’m not sure what else might break internally in Flash as we continue to integrate applications into the framework. If this is the only problem I may change this in the future so I don’t have to make sure developers are dropping any reference to the passed in ApplicationDomain.

Check out the example application I threw together which demonstrates this bug in Flash. Keep in mind this bug only comes up when compiling locally so you’ll only see the error if you download the swf and run it on your machine. I’ve provided a link to the source code below as well. I’d love to hear feedback on the workaround.

Source:

https://breaktrycatchrepo.googlecode.com/svn/trunk/developers/mikebaker/flash/loadedFontRegister/trunk

This entry was posted on Sunday, May 15th, 2011 at 1:21 pm and is filed under Actionscript, Flash. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.