Embedding fonts at runtime in Adobe AIR
I am going to do something that I haven't did before but got some hints about this, it can be possible and then started to do it. I need some help of you all. I am trying to register fonts which are saved within the external SWF , and wanted to embed them at the runtime into an Adobe AIR application. I am just working with a File object to load the SWF in this case, but am not completely sure about the procedure to register the fonts and what I have to do after that. Anyone has any suggestion or guidelines for this ?
Re: Embedding fonts at runtime in Adobe AIR
There are some little bit changes that you have to commit to do this. And some of the object are available through which you can do it. You need to use a Loader obejct to load the SWF. Then after reference the class name for the font in the specified SWF file, it can be implemented in this way :
var Fontttt:Font = new Font1(); // "Font1" class name specified for font in the SWF file.
var Formatttt:TextFormat = new TextFormat();
Formatttt.font = Fontttt.fontName;
Re: Embedding fonts at runtime in Adobe AIR
Here is the code which I have to perform this task . Commonly, I issued a button, a file open dialog pops up, and then I use a Loader to load the SWF. But I don't have any idea about the registration procedure of the font after that? Is what I am manipulating in fontLoaded() fine ?
Code:
import flash.utils.describeType;
import mx.controls.SWFLoader;
private var FtFile: FileReference = new FileReference();
private var Ft: File = new File();
private function initApp(): void
{
var embdFonts: Array = Font.enumerateFonts(false);
trace ("=== BEFORE ===");
for (var lpi: int = 0; lp < embdFonts.length; lp++)
{
var item: Font = embdFonts[lp];
trace("["+lp+"] name: "+item.fontName + ", style: "+ item.fontStyle+", type: "+item.fontType);
}
btnLoadFont.addEventListener(MouseEvent.CLICK, loadFont);
}
private function loadFont(e: MouseEvent): void
{
Ft.browseForOpen("Select Font");
Ft.addEventListener(Event.SELECT, fontSelect);
}
private function fontSelect(e: Event): void
{
trace("File: "+Ft.url);
var loader: Loader = new Loader();
var context: LoaderContext;
var loaderInfo: LoaderInfo = loader.contentLoaderInfo;
loaderInfo.addEventListener(Event.COMPLETE, fontLoaded);
context = new LoaderContext( true, new ApplicationDomain( ApplicationDomain.currentDomain ) );
loader.load(new URLRequest(Ft.url), context);
}
private function fontLoaded(e: Event): void
{
trace(e.target);
var FtLib:Class = ApplicationDomain((e.target as LoaderInfo).applicationDomain).getDefinition('_Verdana') as Class;
Font.registerFont(FtLib);
var embdFonts: Array = Font.enumerateFonts(false);
trace("=== AFTER ===");
for (var lp: int = 0; lp < embdFonts.length; lp++)
{
var item: Font = embdFonts[lp];
trace("["+lp+"] name: "+item.fontName + ", style: "+ item.fontStyle+", type: "+item.fontType);
}
}
Re: Embedding fonts at runtime in Adobe AIR
Actually! I got you now, Do you desired the user to open to choose a SWF file which has the font? This may cause the issue because the URL is not present in the application directory. If you just need to load a SWF file, entitled as EmbeddedFonts.swf in your application folder specified as which has embedded fonts, make the changes in your according to this !!!