I came up with this solution when I had to compile over 100 fonts into SWF files for a recent project. Doing this by hand would have been madness so I wrote a ruby script and a shell script to automate the process.
Step 1: Setup
The directory structure I used was as follows.
Step 2: Prep fonts
Ok told a tiny white lie when I said it was all automated. Since (to my knowledge) you can’t inspect a font file to determine if it’s normal, regular, bold, italic, or bold italic (the weights and styles Flex understands) you’ll have to set up a naming convention that the Ruby script can parse. You’ll be using the font name to create an ActionScript class so it should also be a legal name.
Here’s an example using the Arial family.
Step 3: The Ruby Script
# delete any previous as
Find.find( 'as' ) do | as |
if File.extname( as ) == ".as"
File.unlink as
end
end
# generate new as
Find.find( 'fonts' ) do | font |
if File.file?( font )
# extension name
ext = File.extname( font )
# is font
if ext == ".ttf" || ext == ".otf"
# file name
full_name = File.basename( font )
name = File.basename( font, ext )
# font weight and style
font_weight = ""
font_style = ""
if full_name.include? "_Bold" + ext
font_weight = "fontWeight='bold',"
end
if full_name.include? "_Italic" + ext
font_style = "fontStyle='italic',"
end
if full_name.include? "_BoldItalic" + ext
font_weight = "fontWeight='bold',"
font_style = "fontStyle='italic',"
end
# generate as
f = File.new("as/.as", "w+")
f.write "package
{
import flash.display.Sprite;
public class extends Sprite
{
[Embed(source='../fonts/', fontName='', unicodeRange='U+0000-U+00FF,U+2100-U+214F')]
public var Font:Class;
}
}"
# some helpful output
print ',"', "", '"'
end
end
end
In TextMate you can open this script and hit Command-R to run it, or run it via the command line ‘ruby convert.rb’. Once the script runs you’ll have generated ActionScript files in the ‘as’ directory ready to be compiled.
Step 4: The Shell Script
echo "<h2>Fonts Custom Compile</h2>";
echo "<code> Started @ `date "+%H:%M:%S"`</code><br />";
You’ll need to customize the script for your Flex SDK location, mine is at "/Applications/Adobe Flex Builder 3/sdks/3.3.0.4589/bin/mxmlc". The hidden gem in there is , about 90% of fonts will compile without it but the rest were blowing up until I added that argument.
Again with TextMate you can run the script with Command-R. Each font will take about five seconds to compile, my set of over 100 fonts took over ten minutes
That’s it! The SWFs are compiled and ready to be runtime loaded into a Flex app. Creating the ActionScript for 100+ fonts would have been a multi-day process but with a little scripting it took less than an hour.
One Comment
Just FYI – your actionsnip site is not working, there is an error when trying to view any of the snips.