I ran through this one more time today to get this ironed out once and for all. Seems folks are still having issues identifying fonts and targeting them for use in sub movies from externally loaded libraries.
I’ve attached a .zip with two FlashDevelop projects which uses the FlexSDK to compile. You should really be using FlashDevelop if you’re on a PC — it’s bar none the best IDE for Flash out there. If you’re on a Mac, you can use FlashBuilder which does much of the same thing but is missing a few key features that I really love about FlashDevelop. It’s the only reason I still use a PC over a Mac.
Okay, here’s the run down on how I built out the pieces:
First there’s a project called ‘FontTester.as3proj’ that compiles a simple font library swf. There are two embeds in that file and in the constructor it registers those fonts for use by the static Font class. They should already be registered but what I’m doing is forcing it to redo the action in the parent swf when it loads this swf file.
Compiling this project generates the file ‘FONTLOADER.swf’ in the bin.
The second project (‘FontEmbeddingAS3′) is the shell app that will load the library swf and take all the fonts from that swf and make them available in the shell:
First thing I do is make sure everything is loaded and ready.
Then I load the library swf and wait for it to load.
Next is the great part; in the function fontsloaded I enumerate the fonts that are now registered in the system and reregister the fonts in the current application domain so that they’re available for the current domain to use.
This happens in the very terse line:
Font.registerFont(e.currentTarget.loader.content.loaderInfo.applicationDomain.getDefinition(f.fontName) as Class);
You’ll need to do this in a level that is accessible by the current application domain. If you find that the fonts are not showing up in a specific domain level you’ll need to repeat this process there.
For example, you’ve created a movieclip at some point and are attaching text fields to the movieclip. If your creation happens before your loading and attaching the fonts (always possible in an asyncronyous environment) fonts will fail to be available. Timing is everything so to ensure that the fonts are ready you really should put your font library loading as a first step before your application launches.
Here’s the source:
Find unicode ranges here. I’ve got part of the line commented out for the setting of unicode ranges for example.
For non latin fonts you’ll need to use a non latin font for embedding (arialuni.ttf for example). Luckily in Flash 10 this isn’t an issue any longer but it does come up occationally.
Here’s the FontEmbeddingAS3 source.