Embedding fonts from a Flex application into a Flash SWF file

by Peter deHaan on December 20, 2008

in Embed, Flex, Font

The following examples show how you can embed a font from a Flex Library Project into a Flash application.

Note: For an example of embedding a font from Flash into Flex, see “Embedding fonts from a Flash SWF file into a Flex application” at FlexExamples.com.

Full code after the jump.

  1. In Flex Builder 3, create a new Flex Library Project by selecting File > New > Flex Library Project from the main menu. Name your project “Library_Font_test”.
  2. Create a new ActionScript Class in the Flex Library Project by selecting the project in the Flex Navigator and selecting File > New > ActionScript Class from the main menu.
  3. In the Name field, enter “FontsFontsFonts” and click the Finish button. Flex Builder will create a FontsFontsFonts.as document in the project’s /src/ folder.
  4. Enter the following code in the FontsFontsFonts.as file and save the document:
// ActionScript 3.0 (Flex Builder / Flex Library Project)
package {
    public class FontsFontsFonts {
        [Embed(source="C:/Windows/Fonts/arial.ttf", fontFamily="ArialEmbedded")]
        public const ArialEmbeddedFont:Class;
    }
}
  1. Close Flex Builder and open Flash CS4.
  2. Create a new ActionScript 3.0 file by selecting File > New from the main menu and then selecting “Flash File (ActionScript 3.0)” from the New Document dialog box’s General Tab. Click OK to create the new document.
  3. Save the new Flash document as main.fla in an empty folder on your hard drive.
  4. Enter the following code in frame 1 on the timeline of the Flash document:
// ActionScript 3.0 (Flash Authoring / Timeline)
var myFont:Font = new FontsFontsFonts_ArialEmbeddedFont();
 
var fmt:TextFormat = new TextFormat();
fmt.font = myFont.fontName;
fmt.size = 32;
 
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.defaultTextFormat = fmt;
tf.embedFonts = true;
tf.multiline = true;
tf.rotation = 15;
tf.text = "The quick brown fox jumps over the lazy dog." + "\n" + new Date().toTimeString();
tf.width = 400;
tf.wordWrap = true;
tf.x = 100;
tf.y = 10;
addChild(tf);
  1. Copy the generated Library_Font_test.swc file from the Flex Library Project’s /bin/ directory, and save it to the same directory as the main.fla file saved in step 7.
  2. Select File > Publish Settings from the main menu to display the Publish Settings dialog box.
  3. Select the Flash tab and click the Settings button next to the Script drop down menu to display the Advanced ActionScript 3.0 Settings dialog box.
  4. Select the Library path tab and click the Add New Path button (the plus sign).
  5. Select the new path row and click the Browse to SWC file button to display the Browse to SWC file dialog box. Locate the Library_Font_test.swc file you copied to the same directory as the .FLA file in step 9 and click the Open button.
  6. Click the OK buttons to dismiss the Advanced ActionScript 3.0 Settings dialog box and the Publish Settings dialog box.
  7. Select Control > Test Movie from the main menu to compile and run the .FLA file and view the generated .SWF file.

{ 9 comments… read them below or add one }

1 sk 04.21.09 at 10:22 am

Hello Peter

I can’t make this work. The following constructor:
new FontsFontsFonts_ArialEmbeddedFont();

keeps throwing an error: ‘Call to an undefined method’

Here’s my code:
——————-

in Flex Builder 3:
package bin
{
public class FArial
{
public function FArial()
{
[Embed(systemFont="Arial", fontFamily="ArialEmbedded")]
public const ArialEmbeddedFont:Class;
}
}
}

in Flash CS4:
// ActionScript 3.0 (Flash Authoring / Timeline)
var myFont:Font = new FArial_ArialEmbeddedFont();
[...]

- The library path is set to FArial.swc
- Actionscript 3
- Flash Player 9

Any insight or help would be much appreciated.

Thank you.

sk

2 Chris 06.05.09 at 5:53 am

Peter,
Thank you.
Will this approach work with time line text fields?
thanks,
Chris

3 pivo 07.24.09 at 12:14 am

SK, you might want to consider to publish for Flash player 10… ;-)

4 pivo 07.24.09 at 12:21 am

Sorry SK,seems to work for flash player 9 as well.

I see your package is located in the bin folder, why not just put it in the root? Works fine for me…

5 ian pretorius 08.05.09 at 9:05 am

Thakns for the example it works by me as well ;)

6 daniel 10.20.09 at 8:04 pm

thx it works great

7 Matt 01.24.10 at 9:05 pm

Thanks! This was a big help.

8 nusirat 02.28.10 at 10:57 am

Hello Peter
thanx alot for your example
but i face a problem when i try to use it with flash text layout component
this is my code

_textFlow=parent[myTextLayoutComp].textFlow;
var myFont:Font = new FontsFontsFonts_ArialEmbeddedFont();
//_textFlow.fontLookup=FontLookup.EMBEDDED_CFF;
//_textFlow.renderingMode=RenderingMode.CFF;
trace(“myFont.fontType “+myFont.fontType);
var textFlow:TextFlow = new TextFlow();
var charFormat:CharacterFormat = new CharacterFormat();
charFormat.color=textColorValue.color
charFormat.fontLookup=flash.text.engine.FontLookup.EMBEDDED_CFF;
charFormat.fontFamily=myFont.fontName
charFormat.kerning=flash.text.engine.Kerning.ON;

_textFlow.hostCharacterFormat=charFormat;
it dosent work with my textlayout

9 Peter deHaan 02.28.10 at 2:07 pm

@nusirat,

If you’re using the Text Layout Components, you may need to embed the font with embedAsCFF=true.

Peter

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: