lab.joelgillman.com

Sketches, Code Bites and Toys




Why You Should Embed Your Fonts
2008-09-22

I think this should go without saying, but not everyone knows how to do this. First I randomly downloaded a font from dafont.com to make sure that I would be using a font that 99% of people didn’t have. If you check out the Flash below you can see that the top text field is using some crazy bitmap-esque font. The text field on the bottom is actually using the same font but since I created it on the stage by hand (rather than dynamically with my code) it is probably displaying Times New Roman. Or some other default serif typeface. The the source code for a commented explanation.

AS3 Code:

import flash.display.Sprite;

import flash.text.AntiAliasType;
import flash.text.Font;
import flash.text.FontStyle;
import flash.text.FontType;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.TextRenderer;


var format:TextFormat = new TextFormat();
var font:Font         = new bitmapRegular36(); //use the linkage name for the font in the library
var size:Number       = 36;


function init():void {
    format = makeTextFormat(size, font);
    makeTextField("This ridiculous font is dynamicly embedded.", format);
}

function makeTextFormat(__size:Number, __font:Font):TextFormat {
    var result:TextFormat = new TextFormat();
    result.font     = __font.fontName;
    result.size     = __size;
    result.kerning  = true; //not availible for non-embedded fonts
   
    return result;
 }

function makeTextField(_inputText:String, __format:TextFormat, __x:Number = 0, __y:Number = 0):void {
    var tf:TextField = new TextField();
    tf.x              = __x;
    tf.y              = __y;
    tf.width          = 450;
    tf.height         = 200;
    tf.type           = TextFieldType.INPUT;
    tf.wordWrap       = true;
    tf.multiline      = true;
    tf.selectable     = true;
    tf.embedFonts     = true;
    tf.antiAliasType  = AntiAliasType.ADVANCED; //not availible for non-embedded fonts
    tf.gridFitType    = GridFitType.PIXEL; //not availible for non-embedded fonts
    tf.text           = _inputText;
   
    //as a general rule, set the textFormat last
    tf.setTextFormat(__format);
   
    addChild(tf);
}

//start
init();

Download the source.

Original font by Sam Derrick.



Share

trackback URL for this post: http://lab.joelgillman.com/archives/73_why-you-should-embed-your-fonts/trackback

comments as feed

Post a Comment

Your email is never published nor shared.
Required fields are marked *
*
*



© 2010 jgillman -¦- Go WordPress! -¦- RSS 2.0 Feed