joon
joon

Reputation: 864

Why can't I display embedded fonts in AS3?

I have gone through all topics on Embedding fonts in AS3 I could find,a nd tried all solutions. I'm probably missing something obvious, but I don't fully understand what I'm doing so please guide me in the right direction. Many of the answers involve Flash Builder or another tool but I use FlashDevelop. No idea whether that matters.

I have this line in my Main.as:

[Embed(source = "assets/SKA_75_marul_CE_extended.ttf", 
fontName = "SKA_75_marul_CE_extended", 
fontWeight = "bold", 
advancedAntiAliasing = "true", 
mimeType = "application/x-font")] 
public static var SKA_75_marul_CE_extended:String;

And this exists in the constructor of an extended Sprite called Pointer.as:

var format:TextFormat = new TextFormat();
format.font = "SKA_75_marul_CE_extended";
format.color = 0xFFCCCC;
format.size = 20;           

var label:TextField = new TextField();
label.defaultTextFormat = format;
label.text = "test";
label.embedFonts = true;
label.antiAliasType = AntiAliasType.ADVANCED;

//label.setTextFormat(format);    --> I tried this too, didn't work...
label.defaultTextFormat = format;
label.x += img.width + 50;
this.addChild(label);

The only way I've found to get it to display anything is if I turn off embedFonts. I've tried embedding C:/windows/fonts/arial.ttf without success.

It seems that embedding fonts is a dark art like no other and I must concede after 1 hour of struggling. Please send help.

UPDATE:

Here's the working code, turns out it was due to having the correct order of operations...:

[Embed(source="assets/SKA_75_marul_CE_extended.ttf", 
                fontName = "myFont", 
                mimeType = "application/x-font", 
                fontWeight="normal", 
                fontStyle="normal", 
                unicodeRange="U+0020-U+007E", 
                advancedAntiAliasing="true", 
                embedAsCFF="false")]
        private var myEmbeddedFont:Class;

            var tf:TextFormat = new TextFormat( "myFont", 20,0xffffff );

            var t:TextField     = new TextField;
            t.embedFonts        = true; // very important to set
            t.defaultTextFormat = tf;
            t.text              = text;
            t.x += img.width + 50;
            t.width = 700;
            this.addChild( t );

Upvotes: 8

Views: 5775

Answers (1)

Jonathan Dunlap
Jonathan Dunlap

Reputation: 2631

It's most DEFINITIVELY a "dark art" to get embedded fonts to work right. I would first check if "SKA_75_marul_CE_extended" is the actual name the font has in its metadata (I used Suitcase Fusion to extract the name). I've also seen TTF fonts that Flash simply refuses to embed (perhaps invalid metadata causes the embed system to fault). I would continue testing with a known working font until you find the actual problem in case it is a font file problem.

One thing I noticed is "public static var SKA_75_marul_CE_extended:String;"... shouldn't this be of type Class?

FlashDevelop font embed reference from someone who had issues: http://www.flashdevelop.org/community/viewtopic.php?p=28301

Upvotes: 5

Related Questions