Loading font from SWF

Made a post on Haxe forum: https://groups.google.com/forum/?hl=en#!topic/haxelang/6LjeWfyYHPw
But this actually may be an OpenFL issue, as im trying to compile an OpenFL project.

The problem is: I can’t register fonts from a SWF loaded externally.
Pure AS3 code for registering fonts looks as follows:

var fontVerdana:Class = event.target.applicationDomain.getDefinition("Verdana") as Class; 
Font.registerFont(fontVerdana);

Trying to repeat it with OpenFL:

var fontVerdana:Dynamic = Type.createEmptyInstance(cast event.target.applicationDomain.getDefinition("Verdana"));
Font.registerFont(fontVerdana);

Getting error #1034: Type coercion failed

Trying with safe cast:

var fontVerdana:Class<Dynamic> = cast (event.target.applicationDomain.getDefinition("Verdana"), Class<Dynamic>);
Font.registerFont(fontVerdana);

Getting error #1058: Migration issue

The first thing I would do to debug this:

trace(event.target.applicationDomain.getDefinition("Verdana"));

[class Verdana]


Now that I look at it again, I don’t see why you’re calling Type.createEmptyInstance(). That doesn’t match the AS3 code you posted.

The second thing you tried is better. The cast should work fine, and the problem is something to do with registerFont().

If i simply do

Font.registerFont(event.target.applicationDomain.getDefinition("Verdana"));

I will get error 1508: The Value Specified For Argument Font Is Invalid

Ok, so this is a Haxe/OpenFL problem?

It looks like a Flash Player problem. Possible solutions have been discussed here and here.

Also, please don’t delete useful information. If I hadn’t seen that post, I wouldn’t have been able to provide these links.

Thanks, I’ve restored it.

Depending on what you are loading, you could also consider loading the external SWFs into the same application domain as the host application, it might help

Thanks, i got it working.

It only works when targeting flash though.
If i choose Neko the app will crash after URLLoader.load().
And when targeting Windows it shows compilation error “Dynamic should be openfl._v2.text.Font” for the line where registerFont is called.

var fontVerdana:Class<Dynamic> = cast (event.target.applicationDomain.getDefinition("Verdana"), Class<Dynamic>);
Font.registerFont(fontVerdana);

OpenFL does not have an ActionScript bytecode interpreter. Unlike AIR and Flash Player (which run a Flash Player VM), OpenFL is compiling compatible API code to native (C++) or browser (JavaScript) code. You will not be able to load a SWF this way.

However, the good news is that you should be able to use web fonts in HTML5, which won’t require the whole loading process at all – just set your font name and move forward. In Windows, you should be able to load TTF fonts from disk. Can you bundle all the fonts you need with your project, or do you need to download it separately?

If its impossible with C, i’ll bundle them.

But i have a question. When targeting Windows, is font loading the only limitation when accessing assets from externally loaded SWF?
Or should external SWF loading be avoided at all for all platforms except flash? If so, how do i perform dynamic assets loading from web - do i simply load files directly one by one?

Sure, on a native level, you load images, fonts, meta-data (etc) using separate files, or the format of your choosing (perhaps use an archive, and decompress, etc).

There is the “swf” library, which can support runtime processing of a SWF asset, but it will not support any code (only assets) and it might not be perfect, but you could give this a try, and contribute if you find any issues (and if this is the path you like most).

var swf = new SWF (Assets.getBytes ("myFile.swf"));
var clip = swf.createMovieClip ("ExportedSymbolName");

It should have the ability to get BitmapData as well

EDIT: However, keep in mind this is only going to work well on native. Runtime handling of SWF assets is not supported by the SWF library for HTML5, and would be very inefficient, I feel.

Still using URLLoader, correct (even for Neko)?

Thanks, i’ll try it.

Native should support URLLoader, or we’ve been working on standardizing helper functions such as BitmapData.fromFile or BitmapData.fromBytes to do it in one go