I’m working in a new project where we have to automate the integration of fonts loaded on a server by users and integrate them in a Flash / AIR project.
In the most cases, if you search online, you can find 2 main solutions:
1. first one is embedding fonts in a class with [Embed] metadata
2. second one is create a swf file and instantiate the class of Font embedded inside (this method require Flash Pro)
Searching well you can also find a solution with swfmill but there are some problems to integrate with AS3 projects.
Like you know, in my spare time, I’m playing with Haxe and Corona SDK and in this case Haxe help me to accomplish my task.
I found a great library called hxswfl made with Haxe that allow me to create a swf or swc file directly from an XML, there are many other usage of this library that are described on online repository of this project, but what I need this time is use it to create a library with embedded fonts inside with a class name assigned to each font.
So first of all, following the library XML schema, I create a lib with a list of fonts in this way:
<lib> <font file="Palatino-Bold.ttf" class="com.insideabit.PalatinoBold"/> <font file="Palatino-BoldItalic.ttf" class="com.insideabit.PalatinoBoldItalic"/> <font file="Palatino-Italic.ttf" class="com.insideabit.PalatinoItalic"/> <font file="Palatino-Roman.ttf" class="com.insideabit.PalatinoRoman"/> <font file="Verdana-Bold.ttf" class="com.insideabit.VerdanaBold"/> <font file="verdana.ttf" class="com.insideabit.Verdana"/> </lib>
Any node has 2 attribute, file attribute where I set the path to find my font on filesystem and the class where I set the class name of my font that I’ll use in my actionscript class later.
After download source of hxswfml library (and obviously before anything I’ve installed Haxe in my computer), I go with Terminal in bin/neko folder and I launched neko file (hxswfml.n) with those parameters:
neko hxswfml.n xml2lib librarySWF.xml lib.swf
I’m telling to my neko program that it has to convert my xml to a swf library, embedding inside all fonts described in the XML file with that class name.
Now we have our SWF file with fonts, we can create our Actionscript project and load with a loader object our swf file.
Then, when SWF file is loaded, we have to register our fonts inside the SWF library, you can accomplish this task like this:
for(var i:int = 0; i < _fontsArr.length; i++){ var FontLib:Class = _loader.contentLoaderInfo.applicationDomain.getDefinition(FONT_NAME) as Class; Font.registerFont(FontLib); }
Ok, now you can use any fonts that you have in your SWF without install them in your laptop and without create any AS3 class, this is a really easy way to work with external fonts in a dynamic way.
Obviously I prepare a sample to download if you have any questions feel free to leave me a message to this post.
It’s not finished yet, because I’ve a question for you: what happens if I use the same technique for different devices?
On web and desktop (AIR application) there are any problem because I can load any external SWF file, the same on Android and Playbook but on iOS?
With iOS you can a SWC file instead a SWF file so your SWC library will be embedded at compile time in your application for iOS and you can access to fonts more easily instantiate directly your class name.
For more information about this technique I suggest to read this article on Adobe DevNet.
GREAT! simply GREAT! 🙂
I’ll do some tests on how to make it working on iOS too, but this my friend seems to be a great way to get fonts anywhere with few lines of code.
wonderful! 🙂
You should be able to do this with the free Flex compiler (mxmlc) too. Works from the command line.
Of course but think to bring everything and make an automation online where user upload assets and with PHP version of hxswfml you create the SWF file without using neko, mxmlc and anything else!
In this case I used neko but you can do much more if you take a look to that library you don’t need to stop on integrate assets but potentially you can create SWF file with animations, movieclips and so on!
Ive never used Haxe but Im so interested in testing this out that I just have to try it… I have no idea where to put the source of hxswfml library in order to get it to work. is there a good tutorial where I can check that out?
First of all install Haxe (http://haxe.org/download) then download source lib of hxswfml and then follow my instructions 😀
after you download source of hxswfml library, where do you put it?
whatever you want, you only need to navigate to that folder via Terminal
Great ! Simple and effective
Man Thank you so MUCH..!!!! for the people that dont want deal with the neko enviroments path, just copy all the neko’s files into the haxe folder and woala.!