简体   繁体   中英

What's the definition name for fonts loaded at runtime created with fontswf?

I ran into the 'fontswf' utility to embed TTF fonts into SWF and I am struggling to load them at runtime from my app. All the examples I find online refer to loading the font via [Embed] but I am actually loading them with a flash.display.Loader and from what I gather, once the Loader.contentLoaderInfo fires an Event.INIT I need to register the font, like so:

public function handleLoaderComplete( event:Event ):void {
  var FontClass:Class = event.target.applicationDomain.getDefinition( fontName );
  Font.registerFont( FontClass );
}

The problem is I don't know what to pass in as fontName. I am generating my SWF through:

$ fontswf -a belshaw -o belshaw.swf belshaw.ttf

But when I try to call getDefinition( 'belshaw' ), I get an error saying 'Variable belshaw is not defined'. Any suggestions on how to accomplish this?

The alternative is to generate my on SWF files through templating an .as file and compiling them, but I would rather use a built in tool like fontswf if it's already there.

Thanks

Ruy

I am on a project that had to allow users to dynamically upload their own fonts. We tried getting fontswf to work and ran into issue after issue and the terrible documentation did nothing to help. Eventually we just created a template and use a bash script to embed the .ttf file into a new .swf, the service returns the url to that .swf, then the main app loads that .swf and registers the font.

I know that's not exactly an answer to your question, but I do know that it works. ;)

You could use a decompiler psoftware to decompile your font SWF to see what fonts are embedded.

Or ...

You can loop through the fonts in the embedded file runtime:

import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;

var fontLoader:Loader = new Loader();
var fontLoaderInfo:LoaderInfo = fontLoader.contentLoaderInfo;

fontLoaderInfo.addEventListener(Event.COMPLETE, onFontLoaded);

fontLoader.load(new URLRequest("Verdana.swf"));

function onFontLoaded (e:Event):void {
   var info:LoaderInfo = e.currentTarget as LoaderInfo;
   var loader:Loader = info.content as Loader;
   var embeddedFonts:Array = Font.enumerateFonts(false);
   for(var i:Number = 0; i < embeddedFonts.length; i++){
      var item:Font = embeddedFonts[i];
      trace("[" + i + "] name:" + item.fontName + ", style: " + item.fontStyle + ", type: " + item.fontType);
   }
}

This is not tested, I found it here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM