Reputation: 509
In a mixed Flash IDE/Flex project, I have a TextField and I want to retrieve the Font class object that's associated with that TextField. The TextField lives in a SWF that was created in the CS4 IDE and is loaded into a Flex SWF.
Currently, I have code that works if the following criteria are met:
Code as follows:
fontClass = childSwf.loaderInfo.applicationDomain.getDefinition("CustomFont") as Class;
What I really want to do is not have to know the name of the exported font. Instead, I want to grab either the font's Class or the Class name dynamically from the TextField.
Even better would be the ability to get the Class for built-in fonts without requiring the export.
FWIW, the end goal is to grab any arbitrary TextField and check if it contains characters that its embedded font can't show using Font::hasGlyphs(). However, fonts in child SWFs aren't registered to show up in Font::enumerateFonts().
Upvotes: 1
Views: 2772
Reputation: 754
you can get the text field font name by using getTextFormat
function of it. Consider txt
is the textfield, then
var format:TextFormat = txt.getTextFormat();
trace(format.font);
After some research, I found this solution and it might solve the problem.
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getQualifiedClassName;
import flash.text.Font;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
var font:Font;
var txt:TextField;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("sub_fla.swf"));
function onLoaded(e:Event){
var cl:Class = e.currentTarget.applicationDomain.getDefinition("CustomFont") as Class;
Font.registerFont(cl);
trace(cl);
txt = new TextField();
this.addChild(txt);
txt.text = "Moorthy";
var format:TextFormat = new TextFormat();// = txt.getTextFormat();
font = new cl();
format.font = font.fontName;
txt.setTextFormat(format);
enumerateFonts();
}
function enumerateFonts(){
var embeddedFonts:Array = Font.enumerateFonts(false);
embeddedFonts.sortOn("fontName", Array.CASEINSENSITIVE);
trace("---->"+embeddedFonts.indexOf(txt.getTextFormat().font));
for(var i:int = 0;i<embeddedFonts.length;i++){
font = embeddedFonts[i];
trace("embeddedFonts["+i+"]:"+font.fontName+":"+font);
if(txt.getTextFormat().font == font.fontName){
trace("My font class is '"+getQualifiedClassName(font) +"'");
}
}
}
Don't forget to register the font to get it in the enumerateFonts
list. Otherwise it fetches the default font class not your custom class.
Or else, you can simply add a variable to the movieclip (in which the text field is placed) to hold the font class or font class name.
Eg: If holder
is the movieclip who contains the textfield, then use
holder.fontClass = cl
instead of
Font.registerFont
and you can simply retrieve the font class by
txt.parent.fontClass
If so there is no need of enumerateFonts
in this way.
Upvotes: 1