Give user a choice to select font
Subject with loading font isn’t new but some its part weren’t still described. Or I missed them and had to walk through debugging nightmare.
So target application is text/image editors on flash that gives user option to be unique and change font style of his label. And the problem is the font as other graphic content has some weight. Including fonts to application affect on user traffic and experience, because user has to wait longer. And here usually comes idea don’t load all fonts with application and load them only when user require it.
Many projects don’t include fonts into application and load needed on demand. You can found great example at Picnik Editor . All displayed fonts weren’t loaded on at the beginning. Them are loading only when user select them. In that way application size reduced a lot. For example Arial Black font has complete size – 45KB. It is including basic latin lowercase, uppercase, numeric and punctuation characters. Also in 4 styles: regular, bold, italic, bold italic. But Arial Black is smallest font. Other ones which are more artistic can be more then 150KB. So solution is to preload fonts on demand.
Loading
Loading font into flash application at runtime is simple task. You can find ready to use solutions there:
Loading fonts example
Loading fonts util
To use font in application need to load it and register via Font.registerFont(); Then it will be ready to use in your text:
textfield.embedFonts = true;
textfield.setTextFormat(new TextFormat(“Font name”));
Second utility above is hardcore implementation. It gives ability to load fonts packed into swf versions 7, 8, 9.
If successfully tried some utility and got textfield with correct font that’s great. But it isn’t final.
Font preparation
Loading font is half of goal. Flash Player can’t load font as ttf. Flash will recognize font only if it packed in swf. You might need to create couple of fonts ready to load. For example you let user to choose 20 fonts. Then it’s need to get 20 swf files with these fonts. Or 80! if you are going to load each style separately (regular, bold, italic or bolditalic). That’s might be nightmare and chores. Especially if you don’t know exactly which symbols need to include.
Here swfmill might be useful. It’s rock util for swf generation. So generation of single swf file with one font style looks like:
<?xml version="1.0" encoding="iso-8859-1" ?> <movie width="1" height="1" framerate="12"> <background color="#ffffff"/> <frame> <library> <font import="font/arialbold.ttf" glyphs=" abcdefghijklmn opqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345 6789!#$%^*()-=_+,.?/ "&<>'"/> </library> </frame> </movie>
So you can facilitate goal and work with xml instead of fla files. Instead of 20 fla files you might have 20 xml files which are easy to modify (replace content). But here is few problems. This implementation requires that you have ttf file for each font style. For example for Arial: arialregular.ttf, arialbold.ttf, arialitalic.ttf, arialbolditalic.ttf. Some fonts might be packed at single file for example you may find arial.ttf with all styles. But swfmill can read only first font inside file. Another problem of swfmill that it is create swf file up to 8 version.
Finally jsfl can save you from troubles. Spending few hours to reading API documentation you can write script for swf generation in jsfl. Or certainly using my one. Script create blank flash document, put textfield on stage set it font and embedded symbols. Then compiles to many swf files with different predefined fonts. As result you got single file with ability easy to change embedded symbols type. Easy to add new font. Sure it’s need to have fonts already installed at you OS. I like automation.
Font thumbnails
Next feature is styled list of fonts. It’s better if user choose font by visual representation instead of plain text name.

As you might have noticed at picnik fonts labels are styled, means that font already applied to it. It can be done by creating separate swf with incuding only symbols used in font label. For example for label “Palatino Linotype”, it’s need to include regular style of chars “Paltino Lype”. It also can be generated with jsfl.
Problems
It is impossible to develop in flash without stumble over problems. One of them makes a real pain. If your application already has embedded font you can’t register loaded symbols. For example you have a static Arial text “Welcome”. You will fail to use other Arial symbols in application via loading font. So you must to embed all Arial font to fla or don’t use or either. Font symbols becomes embedded in next cases:
• when you put static text on stage. If you will put static text
• when you put dynamic or input text with including chars
You can easy check if you swf has embedded font. Just decompile it with SWFDecompiler. It hasn’t support of version 9 but it still display used assets in swf. And there you can find what fonts are embedded and glyphs count. But looking for embedded font in big fla isn’t simple. Here some jsfl script might be usefull again.
Hurry up to buy gifts!
Comments(1)