15
Oct
08

Embedding fonts in Flex Gumbo

The following example shows how you can embed a font in the Flex Gumbo beta and use the embedded font with the new TextView class using the latest version (build 4.0.0.3616) of the beta Flex Gumbo SDK.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3″.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/15/embedding-fonts-in-flex-gumbo/ -->
<FxApplication name="DefineFont4_cff_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        creationComplete="init();">
    <layout>
        <BasicLayout />
    </layout>

    <Style>
        @font-face {
            src: url("assets/ACaslonPro-Regular.otf");
            fontFamily: "ACaslonProRegularEmbedded";
            cff: true;
        }

        TextView {
            fontFamily: ACaslonProRegularEmbedded;
            fontLookup: embeddedCFF;
            fontSize: 34;
            horizontalCenter: 0;
            verticalCenter: 0;
        }
    </Style>

    <TextView id="textView"
            text="The quick brown fox jumped over the lazy dog. 01234567890"
            height="{textView.contentHeight}"
            width="400" />

</FxApplication>

View source is enabled in the following example.

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/15/embedding-fonts-in-flex-gumbo/ -->
<FxApplication name="DefineFont4_cff_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        creationComplete="init();">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            import flash.text.engine.FontLookup;
            import mx.binding.utils.BindingUtils;
            import mx.components.TextView;

            [Embed(source="assets/ACaslonPro-Regular.otf",
                    fontFamily="ACaslonProRegularEmbedded",
                    mimeType="application/x-font",
                    cff="true")]
            private const ACaslonProRegularEmbeddedFont:Class;

            private var textView:TextView;

            private function init():void {
                textView = new TextView();
                textView.text = "The quick brown fox jumped over the lazy dog. 01234567890";
                textView.width = 400;
                textView.setStyle("fontFamily", "ACaslonProRegularEmbedded");
                textView.setStyle("fontLookup", FontLookup.EMBEDDED_CFF);
                textView.setStyle("fontSize", 34);
                textView.setStyle("horizontalCenter", 0);
                textView.setStyle("verticalCenter", 0);
                addItem(textView);

                BindingUtils.bindProperty(textView, "height", textView, "contentHeight");
            }
        ]]>
    </Script>

</FxApplication>

This entry is based on a beta version of the Flex Gumbo 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 Gumbo SDK.

For examples of embedding fonts in earlier versions of Flex (Flex 3 and earlier), see the Font tag archive at http://blog.flexexamples.com/category/fonts/.


6 Responses to “Embedding fonts in Flex Gumbo”


  1. 1 Johannes Oct 28th, 2008 at 3:11 am

    Hey Peter,
    i’m working a while with the Flex SDK 4.

    And something seems to be wrong with the Matrix3D Class (flash.geom.Matrix3D). The FB doesn’t sees the Class, FDT does - yes both have the same SDK Path.

    And while trying your example, FB tells me, that he can not find Matrix3D. FB says that, when i use the Gumbo Namespace xmlns=”http://ns.adobe.com/mxml/2009″.

    Any ideas?
    Feel free to send me an E-Mail! ;-)

    g^2

  2. 2 Troy Blank Nov 17th, 2008 at 8:33 am

    HEY! I am currently using flash, and am desperately trying to embed CFF fonts, but all i get is embedded fonts and notice you have imported fonts using this:

    @font-face {
    src: url(”assets/ACaslonPro-Regular.otf”);
    fontFamily: “ACaslonProRegularEmbedded”;
    cff: true;
    }

    I can’t find any “cff” property in flash that is not read only that I can set to true… was curious if you knew a way of doing the same type of embed in flash with actionscript or even the ui.

    Thanks!

  3. 3 Kirill Mazin Nov 19th, 2008 at 9:12 pm

    I have the same problem. I can’t find anything in the documentation
    on how to embed cff fonts in Flash. Thanks!

  4. 4 peterd Nov 20th, 2008 at 7:02 am

    Troy Blank/Kirill Mazin,

    I’m not sure if Flash CS4 supports CFF fonts. Support for CFF fonts was added to the Flex compiler and I don’t know if Flash supports them yet.

    You could try downloading the beta Flex Gumbo SDK and using something like this in Flash CS4, http://actionscriptexamples.com/2008/10/26/using-the-flex-sdk-with-flash-cs4/ and then just use the [Embed] metadata to embed the CFF font using code from above.

    Peter

  5. 5 Troy Blank Nov 22nd, 2008 at 8:00 am

    sweet, thanks so much for the pointer and example, i will try this today. I still haven’t found a way just using the standard flash classes, but this looks like it should do the trick.

  6. 6 Troy Blank Nov 25th, 2008 at 12:00 pm

    thanks peterd, but it looks like adobe released something that does the same trick.

    just to let you guys know, in ordered to embed CFF fonts into flash cs4 you can use the adobe’s new text layout framework, i hope this saves as much time as i have lost trying to figure this out :)

    http://labs.adobe.com/technologies/textlayout/

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed