Setting the ligature level on a Spark TextInput control in Flex Gumbo

by Peter deHaan on October 21, 2008

in FontLookup, LigatureLevel, TextInput (Spark), beta1, needsSWF

In earlier examples, “Embedding fonts in Flex Gumbo” and “Setting the typographic case on a Spark TextArea control in Flex Gumbo”, we’ve seen how to embed fonts in Flex Gumbo.

The following example shows how you can set the ligature level on a Spark TextInput control in Flex 4/Gumbo by setting the ligatureLevel style to one of the static constants in the flash.text.engine.LigatureLevel class.

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/21/setting-the-ligature-level-on-an-fxtextinput-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextInput_ligatureLevel_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <fx:Style>
        @font-face {
            src: url("assets/ACaslonPro-Regular.otf");
            fontFamily: "AdobeCaslonProEmbedded";
            cff: true;
            unicodeRange: U+0020-007B;
        }
    </fx:Style>

    <fx:Script>
        <![CDATA[
            import mx.events.ItemClickEvent;
            import flash.text.engine.LigatureLevel;

            private function toggleButtonBar_itemClick(evt:ItemClickEvent):void {
                textInput.setStyle("ligatureLevel", evt.item);
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <fx:Array id="arr">
            <fx:String>{LigatureLevel.MINIMUM}</fx:String>
            <fx:String>{LigatureLevel.COMMON}</fx:String>
            <fx:String>{LigatureLevel.UNCOMMON}</fx:String>
            <fx:String>{LigatureLevel.EXOTIC}</fx:String>
        </fx:Array>
    </fx:Declarations>

    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <mx:Form styleName="plain">
            <mx:FormItem label="ligatureLevel:">
                <mx:ToggleButtonBar id="toggleButtonBar"
                        dataProvider="{arr}"
                        itemClick="toggleButtonBar_itemClick(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <s:TextInput id="textInput"
            text="fi fj fl ft ffi ffj ffl ct st Th"
            fontFamily="AdobeCaslonProEmbedded"
            fontSize="48"
            fontLookup="embeddedCFF"
            horizontalCenter="0"
            verticalCenter="0" />

</s:Application>

View source is enabled in the following example.

You can also set the ligatureLevel style in an external .CSS file or <Style /> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/21/setting-the-ligature-level-on-an-fxtextinput-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextInput_ligatureLevel_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";

        @font-face {
            src: url("assets/ACaslonPro-Regular.otf");
            fontFamily: "AdobeCaslonProEmbedded";
            cff: true;
            unicodeRange: U+0020-007B;
        }

        s|TextInput {
            fontFamily: "AdobeCaslonProEmbedded";
            fontSize: 48;
            fontLookup: "embeddedCFF";
            ligatureLevel: "exotic";
        }
    </fx:Style>

    <s:TextInput id="fxTextInput"
            text="fi fj fl ft ffi ffj ffl ct st Th"
            horizontalCenter="0"
            verticalCenter="0" />

</s:Application>

Or, you can set the ligatureLevel style in MXML, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/21/setting-the-ligature-level-on-an-fxtextinput-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextInput_ligatureLevel_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">

    <fx:Style>
        @font-face {
            src: url("assets/ACaslonPro-Regular.otf");
            fontFamily: "AdobeCaslonProEmbedded";
            cff: true;
            unicodeRange: U+0020-007B;
        }
    </fx:Style>

    <s:TextInput id="fxTextInput"
            text="fi fj fl ft ffi ffj ffl ct st Th"
            fontFamily="AdobeCaslonProEmbedded"
            fontSize="48"
            fontLookup="embeddedCFF"
            ligatureLevel="exotic"
            horizontalCenter="0"
            verticalCenter="0"
            width="75%" />

</s:Application>

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/21/setting-the-ligature-level-on-an-fxtextinput-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextInput_ligatureLevel_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">

    <fx:Script>
        <![CDATA[
            import flash.text.engine.FontLookup;
            import flash.text.engine.LigatureLevel;

            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ToggleButtonBar;
            import mx.events.ItemClickEvent;

            import spark.components.TextInput;

            [Embed(source="assets/ACaslonPro-Regular.otf",
                        fontFamily="AdobeCaslonProEmbedded",
                        cff="true",
                        unicodeRange="U+0020-007B",
                        mimeType="application/x-font")]
            private const MyEmbeddedFont:Class;

            private var toggleButtonBar:ToggleButtonBar;
            private var textInput:TextInput;
            private var arr:Array;

            private function init():void {
                arr = [];
                arr.push(LigatureLevel.MINIMUM);
                arr.push(LigatureLevel.COMMON);
                arr.push(LigatureLevel.UNCOMMON);
                arr.push(LigatureLevel.EXOTIC);

                toggleButtonBar = new ToggleButtonBar();
                toggleButtonBar.dataProvider = arr;
                toggleButtonBar.addEventListener(ItemClickEvent.ITEM_CLICK, toggleButtonBar_itemClick);

                var formItem:FormItem = new FormItem();
                formItem.label = "ligatureLevel:";
                formItem.addChild(toggleButtonBar);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.percentWidth = 100;
                appControlBar.setStyle("cornerRadius", 0);
                appControlBar.addChild(form);
                addElementAt(appControlBar, 0);

                textInput = new TextInput();
                textInput.text = "fi fj fl ft ffi ffj ffl ct st Th";
                textInput.setStyle("fontFamily", "AdobeCaslonProEmbedded");
                textInput.setStyle("fontSize", 48);
                textInput.setStyle("fontLookup", FontLookup.EMBEDDED_CFF);
                textInput.horizontalCenter = 0;
                textInput.verticalCenter = 0;
                textInput.percentWidth = 90;
                addElement(textInput);
            }

            private function toggleButtonBar_itemClick(evt:ItemClickEvent):void {
                textInput.setStyle("ligatureLevel", evt.item);
            }
        ]]>
    </fx:Script>

</s:Application>

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 more information on the LigatureLevel class in Flash Player 10, see http://livedocs.adobe.com/flex/gumbo/langref/flash/text/engine/LigatureLevel.html.

{ 2 comments… read them below or add one }

1 mani October 24, 2008 at 9:11 pm

Wonderful artical, thank you very much, peterd~~

By the way, could you tell me how to arrange image and text dynamicly?
For example, I inputed some texts like “abcdefg”, then I want to insert an image between “c” and “d”.
I’m confused by this problem for a long time, please help me out^_^

I just want to do somting like https://buzzword.acrobat.com do, any suggestions?

Thanks for your time~~
mani.

Reply

2 Peter deHaan June 1, 2009 at 4:14 pm

Updated example to compile against Flex SDK 4.0.0.7411.

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

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="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: