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.
<?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>



{ 7 comments… read them below or add one }
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.
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
Yes, with pre-embedded fonts…no prob. What I mean is getting the users font list and rotating on those.
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
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
Nice one Peter, Is there any way of finding out whats the language of the font
like english, french, latin and we can display that in the datagrid…
What abot showing fonts located in a directory? Let’s say I have additional fonts that aren’t currently enabled. I typically use a font management tool to turn them on. But I’d like to create a tool viewing fonts I have to determine if I want to turn it on.