10
Sep
07

Finding out which fonts are installed on a user’s system

The following example shows how you can use the static Font.enumerateFonts() method in Flex to determine which fonts are installed on a user’s system. The enumerateFonts() method returns an array of Font objects (see the flash.text.Font for more information)

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/10/finding-out-which-fonts-are-installed-on-a-users-system/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init()">

    <mx:Script>
        <![CDATA[
            import flash.text.Font;

            private function init():void {
                arr = Font.enumerateFonts(true);
                arr.sortOn("fontName", Array.CASEINSENSITIVE);
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr" />
    <mx:String id="str">The quick brown fox jumped over the lazy dog.</mx:String>

    <mx:ApplicationControlBar dock="true">
        <mx:Label text="String:" />
        <mx:TextInput id="textInput" text="{str}" />

        <mx:Spacer width="100%" />

        <mx:Label text="Number of installed fonts: {arr.length}" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid" dataProvider="{arr}">
        <mx:columns>
            <mx:DataGridColumn dataField="fontName"
                    width="200"
                    itemRenderer="mx.controls.Label" />
            <mx:DataGridColumn dataField="fontStyle" />
            <mx:DataGridColumn dataField="fontType" />
        </mx:columns>
    </mx:DataGrid>

    <mx:Label id="lbl"
            text="{textInput.text}"
            width="{dataGrid.width}"
            height="32"
            fontFamily="{dataGrid.selectedItem.fontName}"
            fontSize="16" />

</mx:Application>

View source is enabled in the following example.

Note that in the previous example, we passed a value of true to the enumerateFonts() method. The enumerateFonts() method takes a single Boolean parameter, enumerateDeviceFonts (default value is false), which controls whether both device and embedded fonts are returned (true) or whether just embedded fonts are returned (false).

If you modified the previous example and embedded a font using the following code, you would see that an additional font “Base02″ appears in the data grid:

<mx:Style>
    @font-face{
        src: url("./fonts/base02.ttf");
        fontFamily: "Base02";
    }
</mx:Style>

Also, if you modified the <mx:Script /> tag and passed false to the enumerateFonts() method, only one item would be displayed in the data grid (fontName:”Base02″, fontStyle:”regular”, fontType:”embedded”):

<mx:Script>
    <![CDATA[
        import flash.text.Font;

        private function init():void {
            arr = Font.enumerateFonts(false);
            arr.sortOn("fontName", Array.CASEINSENSITIVE);
        }
    ]]>
</mx:Script>

5 Responses to “Finding out which fonts are installed on a user's system”


  1. 1 John C. Bland II Sep 10th, 2007 at 11:35 am

    Question/Issue: (I haven’t tested this in Flex but Flash CS3 doesn’t allow it (for understood reasons).) Can you embed those fonts and do rotations, etc on the textfield? CS3 would show the font properly but doing rotatings was a no-no.

  2. 2 peterd Sep 10th, 2007 at 11:44 am

    John C. Bland II,

    You should be able to rotate embedded font no problem as long as it is embedded properly. In fact, I believe I covered an example of embedding fonts with alpha fades and rotations in a Flash CS3 Quick Start I wrote a while back.

    For more information, see http://www.adobe.com/devnet/flash/quickstart/embedding_fonts/ (specifically the last example).

    I don’t know that I’ve specifically tried this in Flex (although I do recall posting something on rotating tooltips which probably applies).

    Peter

  3. 3 John C. Bland II Sep 10th, 2007 at 12:39 pm

    Yes, with pre-embedded fonts…no prob. What I mean is getting the users font list and rotating on those.

  4. 4 peterd Sep 10th, 2007 at 2:45 pm

    John,

    I’m not sure how/if that could be done (you’d have to ask on Flexcoders list). Since the embed is done at compile time in Flex and the font is “baked” into the SWF, I don’t know how you could do something similar at run time. But I haven’t really tried this so maybe I’m just overlooking something.

    Sorry,

    Peter

  5. 5 John C. Bland II Sep 10th, 2007 at 10:57 pm

    Yeah, no sweat because I informed the client of that and used embedded fonts and we’re months beyond this stage. When I saw the post I figured I’d ask to see if I was wrong because they were adamant about it being possible. :-D

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;".