Using embedded fonts

Rather than rely on a client machine to have the fonts you specify, you can embed TrueType font families in your Flex application. This means that the font is always available to Flash Player when running the application, and you do not have to consider the implications of a missing font.

Embedded fonts have the following benefits:

Using embedded fonts is not always the best solution, however. Embedded fonts have the following limitations and drawbacks:

You typically use CSS syntax for embedding fonts in Flex applications. You use the @font-face "at-rule" declaration to specify the source of the embedded font, and then define the name of the font by using the fontFamily property. You must specify the @font-face declaration for each face of the font for the same family that you use. The source can be a local JRE font or one that is accessible by using a file path. You use this font family name in your MXML code to refer to the embedded font.

NOTE

 

Check your font licenses before embedding any font files in your Flex applications. Fonts might have licensing restrictions that preclude them from being stored as vector information.

If you attempt to embed a font that the Flex compiler cannot find, Flex throws an error and your application does not compile.

Subtopics

Embedded font syntax
Using FlashType hinting
Detecting embedded fonts

Embedded font syntax

To embed TrueType fonts, you use the following syntax in your style sheet or <mx:Style> tag:

@font-face {
    src:url("location") | local("name");
    fontFamily: alias;
    [fontStyle: italic | oblique | normal;]
    [fontWeight: bold | heavy | normal;]
    [flashType: true | false;]
}

The src property specifies how the compiler should look for the font, and the name of the font to look for. You can either load a font by its filename (by using src:url) or by its system font name (by using src:local). This property is required. For more information, see Locating embedded fonts.

The fontFamily property sets the alias for the font that you use to apply the font in style sheets. This property is required.

The fontStyle and fontWeight properties set the type face values for the font. These properties are optional. The default values are normal.

The flashType property determines whether to include the FlashType hinting information when embedding the font. This property is optional. The default value is true. You cannot use this option when embedding fonts from a SWF file (see Embedding fonts from SWF files). For more information on using FlashType hinting, see Using FlashType hinting.

The following example embeds the MyriadWebPro.ttf font file:

@font-face {
    src:url("../assets/MyriadWebPro.ttf");
    fontFamily: myFontFamily;
    flashType: true;
}

The following example embeds that same font, but by using its system font name:

@font-face {
    src:local("Myriad Web Pro");
    fontFamily: myFontFamily;
    flashType: true;
}

After you embed a font with an @font-face declaration, you can use the new fontFamily name, or alias, in a type or class selector. The following example uses the myFontFamily embedded fontFamily as a type selector for VBox controls:

<?xml version="1.0"?>
<!-- fonts/EmbeddedFontFace.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
  <mx:Style>
     @font-face {
        src:url("../assets/MyriadWebPro.ttf");
        fontFamily: myFontFamily;
        flashType: true;
     }
     
     VBox {
        fontFamily: myFontFamily;
     }     
  </mx:Style>

  <mx:Panel title="Embedded Font Applied With Type Selector">
     <mx:VBox>
        <mx:Button label="Click Me"/>
        <mx:Label text="Label"/> 
        <mx:TextArea width="400" height="75" text="The text 
            uses the myClass class selector."
        />
     </mx:VBox>
  </mx:Panel>
</mx:Application>

You can also apply the embedded font inline, as the following example shows:

<?xml version="1.0"?>
<!-- fonts/EmbeddedFontFaceInline.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
  <mx:Style>
     @font-face {
        src:url("../assets/MyriadWebPro.ttf");
        fontFamily: myFontFamily;
        flashType: true;
     }
  </mx:Style>

  <mx:Panel title="Embedded Font Applied Inline">
     <mx:VBox fontFamily="myFontFamily">
        <mx:Button label="Click Me"/>
        <mx:Label text="Label"/> 
        <mx:TextArea width="400" height="75" text="This text 
            is in Myriad Web Pro."
        />
     </mx:VBox>
  </mx:Panel>
</mx:Application>

When you run this example, you might notice that the Button control's label does not appear. This is because the label of a Button control uses a bold typeface. However, the embedded font's typeface (MyriadWebPro) does not contain a definition for the bold typeface. You must also embed a font's bold typeface so that the label of a Button control is rendered with the correct font. For information on embedding bold typefaces, see Using multiple typefaces.


Flex 2.01

Take a survey