Embedding a font in a swf using as3

actionscript-3embedfonts

I have a project using flash (and AIR for android and eventuall iOS) for translating some phrases. Although most fonts are fine there are some that I have to load after the user has chosen the languages. Over a year ago I generated a couple of swf files (Bengali and Urdu) and put them on my web server. The flash application then loads them when required and everything is OK…
However as I am nearing implementing the project I thought I should generate the swf files for the other languages that are in the pipeline and for some reason I can't manage it! Needless to say I have misplaced (tidied) the original AS3 source for the two fonts I have done.

package   
{  
    import flash.display.Sprite;  
    import flash.events.Event;  
    public class ArabicF extends Sprite   
    {       
        [Embed(source = "trado.ttf",
        fontName = "ArabicX",
        fontFamily = "ArabicY",
        fontWeight = "normal",
        fontStyle = "normal",
        mimeType = "application/x-font",
        advancedAntiAliasing="true",
        embedAsCFF="true")]  
        public static const ArabicZ:Class;  
    }   
}  

This has expanded from a much simpler form as I added things to get it to work and I must have tried all permutations of true and false. I load the swf ok but then can't extract the class. With the two fonts I embedded before the swf class and the parameter all have the same name and they work fine (i.e.
Urdu.swf
then later:
var FontClass:Class = evt.target.applicationDomain.getDefinition("Urdu") as Class;
and then
Font.registerFont(FontClass.Urdu);
in the calling .as application with the above Arabic.swf I try

trace("1 Arabic "+evt.target.applicationDomain.hasDefinition("ArabicF"));  
trace("2 Arabic "+evt.target.applicationDomain.hasDefinition("Arabic"));  
trace("3 ArabicX "+evt.target.applicationDomain.hasDefinition("ArabicX"));  
trace("4 ArabicY "+evt.target.applicationDomain.hasDefinition("ArabicY"));  
trace("5 ArabicZ "+evt.target.applicationDomain.hasDefinition("ArabicZ"));  

but all return false

PS also tried generating the Arabic.swf using fontswf.bat which again seems to make a very similar swf file which is loaded but I can't extract the class from it

PPS I'm using flashdevelop and the font swf are set up as AS3 standard project compiler options.

Best Answer

I don't use Flash Develop, but perhaps I can offer some perspective from my implementation.


If I wanted to embed Arial, the embedding swf would "Export for ActionScript" the embedded font. In its document class, use registerFont() as below:

Font.registerFont(Arial);

Like every other swf, you'd use a Loader to import it into your runtime, and at that point, the font will be available to your global table. You can query the complete list of registered fonts using enumerateFonts().

var fontList:Array = Font.enumerateFonts();
for (var i:int = 0; i < fontList.length; i++) {
    trace(fontList[i].fontName);
}

Assuming we've got a TextField called "txt", you can then implement that font with setTextFormat() like so:

var format:TextFormat = new TextFormat();
format.font = "Arial";
txt.embedFonts = true;
txt.antiAliasType = AntiAliasType.ADVANCED;
txt.setTextFormat(format);
txt.defaultTextFormat = format;

A disclaimer from Adobe on the use of these last two settings:

"When you apply a TextFormat object to a text field using the TextField.defaultTextFormat property or the TextField.setTextFormat() method, only its defined properties are applied. Use the TextField.defaultTextFormat property to apply formatting BEFORE you add text to the TextField, and the setTextFormat() method to add formatting AFTER you add text to the TextField."

As I said before, my workflow doesn't utilize Flash Develop, but rather Flash IDE to embed the fonts into the swfs. Your results may vary, but be aware you must set embedFonts to true, or it won't respond to your embedded font. I've lost hair on that issue before.

Hope that helps. Cheers,

Related Topic