Embedding fonts Class in Flash CS3/AS3
by Carlos Pinho, July 30th, 2007

flash_cs3_63x63.gifHere it is a embedding font class by Maohao.

package
{
import flash.display.Sprite;
import flash.text.TextField;import flash.text.Font;
import flash.utils.getDefinitionByName;public class SimpleTextPaneTestDrive01 extends Sprite
{
private static var embeddedFont:Font = null;
public function SimpleTextPaneTestDrive01()
{
var embeddedFontClass:Class = getDefinitionByName(”Font1″) as Class;
Font.registerFont(embeddedFontClass);
var embeddedFontsArray:Array = Font.enumerateFonts(false);
embeddedFont = embeddedFontsArray[0];var fmt:TextFormat = new TextFormat();
//fmt.bold = true;
fmt.color =0xffffff;
fmt.size = 16;//Old day fashion: If you embed it in a textfield on stage
// fmt.font = “Helvetica”;
fmt.font = embeddedFont.fontName;
var tf:TextField = new TextField();
tf.text = “hello world!”;
tf.setTextFormat(fmt);

}

}
}

Brgds,
CP

No Responses to “Embedding fonts Class in Flash CS3/AS3”

  1. Ain Tohvri Says:

    Just for the record: font embedding in Flash CS3 IDE does not support unicode ranges. To read further please read an article at flash tekkie.

  2. Andre Says:

    Hey I just wanted to comment on the structure of your code. The industry has developed best practice methods for writing code. It will help your readers if you structure your code properly. See example below.

    package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.Font;
    import flash.utils.getDefinitionByName;

    public class SimpleTextPaneTestDrive01 extends Sprite {
    var embeddedFont:Font = null;

    function SimpleTextPaneTestDrive01() {
    var embeddedFontClass:Class = getDefinitionByName(“Font1″) as Class;
    var embeddedFontsArray:Array = Font.enumerateFonts(false);
    var fmt:TextFormat = new TextFormat();
    var tf:TextField = new TextField();

    Font.registerFont(embeddedFontClass);
    embeddedFont = embeddedFontsArray[0];

    fmt.color =0xffffff;
    fmt.size = 16;
    fmt.font = embeddedFont.fontName;

    tf.text = “hello world!”;
    tf.setTextFormat(fmt);
    }
    }
    }

  3. Sven Says:

    Argh, crap. This is the 40.001 script I’ve found on embedding font in AS3, and the shit still do not work.

    Please, be extremely specific of the context when you throw something like this into cyberspace. Try to eliminate ALL guessing.

  4. Jesse Couch Actionscript 3.0 Says:

    Sven << this worked perfect.

    Andre << your code has a smiley face in it, how is that standard?

    for anyone using flex 3 not flash cs3 do this to embed the font

    [Embed(source='../../../assets/fonts/urfont.otf',
    fontName='urfont',
    mimeType='application/x-font'
    )]
    private var fontclass:Class;

    var embeddedFontClass:Class = fontclass;

  5. Jaime Méndez Says:

    Follow this link, this sollution is more easy in all ways…

    http://www.zedia.net/2007/using-embedded-fonts-in-flash-cs3-using-actionscript-3/

  6. Nick Hutto Says:

    So What do i replace Font1 in the code with cause it throws this error

    ReferenceError: Error #1065: Variable Font1 is not defined.
    at global/flash.utils::getDefinitionByName()
    at Test()

  7. Pete Says:

    Sven, I had the same problems trying to understand it. I’ve finally got the answer:

    Open Library, on the right hand side click “New Font…” and select the font you want. Here’s the gotcha: the name is NOT the linkage.

    To add the linkage you have to add your font to the library, and THEN right-click and select “Linkage…”. Call it “Font1″ and the code above will work.

    I’ve spent hours trying to figure this out. Such a stupid UI misstep made by the CS3 team…

  8. ss Says:

    Sven, you rock!

Let leave a Comments for this post.