Embedding local system fonts in Flex 4

In a previous example, “Embedding fonts by name instead of location”, we saw how you could embed a font in Flex by specifying the font name instead of a location by using the local() function instead of the src() function when embedding a font.

The following example shows how you can load a system font in Flex 4 by specifying a custom Flex configuration file and specifying a local font path.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/01/21/embedding-local-system-fonts-in-flex-4/ -->
<s:Application name="Spark_fontFace_local_test"
               xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark">
 
    <!-- -load-config+=local-font-config.xml -->
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
 
        @font-face {
            src: local("Arial");
            fontFamily: "MyFont";
            unicodeRange: U+0020-00FF;
        }
 
        s|Label {
            fontFamily: "MyFont";
            fontSize: 44;
        }
    </fx:Style>
 
    <s:Label id="myLabel"
            text="Hello World"
            rotation="15"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

Next we need to create a custom Flex config file (in this case local-font-config.xml, and save this file in your project’s /src/ folder) and specify a local font path, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<flex-config>
    <compiler>
        <fonts>
            <local-font-paths>
                <path-element>C:/Temp/fonts</path-element>
                <!--<path-element>/System/Library/Fonts/</path-element>-->
            </local-font-paths>
        </fonts>
    </compiler>
</flex-config>

In the previous config file, you specify a folder where the Flex compiler should look for “local” fonts. In this case I have a C:\Temp\fonts\ folder which has a series of subdirectories containing .TTF and .OTF fonts.

Finally, you need to specify the custom config file in Flash Builder’s project properties dialog box (for more information, see “Using a custom flex-config.xml file in Flex Builder 3”).

  1. Select Project > Properties from Flex Builder’s main menu.
  2. Select “Flex Compiler” from the left hand menu.
  3. In the Additional compiler arguments text field add the following argument: -load-config+=local-font-config.xml

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

4 thoughts on “Embedding local system fonts in Flex 4

  1. How do i expose these embedded fonts to actionscript? I’d like to use the Font.registerFont() method with those fonts, but how/where can i do that?

Comments are closed.