as3 dynamically runtime font embedding

2.2k views Asked by At

I have a AS3 project in Flash CS4 in which I am dynamically loading in a font whose location is passed in via XML. It works when I write in the font name (here EASTERAR.TTF). However, if I replace it with a variable, I get an invalid metadata error. How can it be solved?

Works fine

[Embed(source='C:/fonts11/EASTERAR.TTF', fontFamily="xyz")]

var xyz:Class;

var arialEmbeddedFont:Font = new xyz();

var textFormat:TextFormat = new TextFormat();
textFormat.color = 0xFF0000;
textFormat.font = arialEmbeddedFont.fontName;
textFormat.size = 32;

var textField:TextField = new TextField();
textField.autoSize = TextFieldAutoSize.LEFT;
textField.wordWrap = true;
textField.defaultTextFormat = textFormat;
textField.embedFonts = true;
textField.text = "rohan";
textField.width = 500;
textField.x = 25;
textField.y = 25;
addChild(textField);

Shows meta-data error

var fontname:String = 'EASTERAR.TTF'
[Embed(source='C:/fonts11/'+fontname, fontFamily="xyz")]

Thanks.

1

There are 1 answers

2
NemoStein On BEST ANSWER

You can't use variables in Embed metatags (in fact, you can't use in any meta).
And don't think that you can embed a font in runtime too...

What you can do is embed a font in a swf and load this file dynamically, at runtime.

To exemplify:
Create a new FLA file, embed your font and export to ActionScript, then, export the file.
Now, create another FLA, use URLLoader to download the previous SWF and use getDefinitionByName (flash.utils.getDefinitionByName) to get access to the font inside the loaded SWF. Now you can change the font of your text to the new font.