Question

I am working on an application which needs to load fonts dynamically based upon the fonts used in a given document that the user opens. The fonts are used in a RichEditableTextControl so need to be CFF format.

If I add the code:

[Embed(source="/assets/fonts/AvenirLTStd Book.otf",
fontFamily="EmbedAvenir LT Std 45 Book",
mimeType="application/x-font",
embedAsCFF="true")]
public const embeddedFont:Class;

to the main SWF then the text displays correctly with the embedded font but moving the code to a separate file and adding a loader as per the information I found at the following link does not load the font - http://www.scottgmorgan.com/blog/index.php/2007/06/18/runtime-font-embedding-in-as3-there-is-no-need-to-embed-the-entire-fontset-anymore/

The loader code is:

private function loadFont(url:String):void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fontLoaded);
    loader.load(new URLRequest(url));
}

private function fontLoaded(event:Event):void {
    var FontLibrary:Class = event.target.applicationDomain.getDefinition("FontAvenirLTStd") as Class;
    Font.registerFont(FontLibrary.embeddedFont);
}

There is an error thrown at the Font.registerFont line to say that the parameter being passed cannot be null. I have checked in debug mode and the issue seems to be that the class exists but does not have any content. The FontLibrary class is instantiated but the only child entry in the debugger is _prototype so trying to access the embeddedFont property does return undefined.

At the moment the font SWF is in the assets folder of the main project so I don't believe there should be any security restrictions and, as I said, the SWF loading part appears to work.

One thing which is hampering my diagnostics is that I am not sure if the problem is the font SWF not being created correctly and having no content or if the main app is unable to load it. Any help on at least being able to narrow that down would be appreciated.

I would appreciate all the help I can get on this as I have been stuck at this problem for some time and it is a key part of the application.

Thanks in advance to everyone.

Was it helpful?

Solution

Just a quick note for anyone who ends up here from Google, the problem was that I had managed to lose the static keyword from the embeddedFont constant definition in the top block. It should have been public static const embeddedFont:Class;

Hope this helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top