Specifying custom named unicode ranges in Flex
We’ve looked at general font embedding and unicode ranges before in previous examples (see “Embedding and animating fonts in a Flex application” and “Specifying certain unicode-ranges for embedded fonts”), but this example will show you how you can embed a named range of unicode characters into an application by editing your flex-config.xml file and specifying unicode ranges.
Full code after the jump.
OK, so the first question you may have is: “Sounds great, but why would I want to embed a font in my Flex application?”. Reasonable question, and I’m glad you asked. Well, perhaps you want to rotate some text, or set the alpha on some text so it is semi-transparent. In order to do either of these things you need to embed a font face into your Flex application. But as you may expect, embedding fonts and additional assets make your SWF file sizes bigger, so you should be aware of a few small details before you go embedding a number of fonts in your applications.
Lets start off with a simple example. Consider the following code:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/24/specifying-custom-named-unicode-ranges-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white">
<mx:Style>
@font-face {
src: local("Base 02");
fontFamily: Base02Embedded;
}
</mx:Style>
<mx:Label text="The quick brown fox jumped over the lazy dog."
fontFamily="Base02Embedded"
fontSize="16"
rotation="15"
truncateToFit="false" />
</mx:Application>
This code defines a @font-face style in an <mx:Style /> block. Note that it only specifies a src attribute and fontFamily style. What the code is doing is embedding the whole available unicode range for the Base02 font.
If you export a release (non-debugging) version of the previous example, the SWF is 272,788 bytes (approximagely 266 KB). By comparison, if you didn’t embed a font into your application, the SWF size would only be 152,415 bytes (approximately 148 KB, or 55% of the embedded font example SWF file size).
So, what can we do if we want to embed a font, but not increase the size of our SWF file significantly? Correct, you can specify unicode ranges! Note that we have a few options:
1) The following unicode range specifies the whole range of uppercase, lowercase, symbols and punctuation:
unicode-range: U+0021-U+007B;
2) The following unicode range specifies all uppercase, lowercase characters as well as numbers:
unicode-range:
U+0030-U+0039, /* 0-9 */
U+0041-U+0051, /* Uppercase A-Z */
U+0052-U+007A; /* Lowercase a-z */
3) The following unicode range specifies only the letters used in the string being displayed:
unicode-range:
U+0054-U+0054, /* T */
U+0061-U+007A, /* a-z */
U+002E-U+002E; /* . (period) */
So now that we have a slightly better understanding of font embedding and unicode ranges, lets look at setting up our own custom ranges so we can specify unicode ranges by name instead of slightly cryptic unicode numbers.
First, locate your flex-config.xml XML document in your particular Flex SDK framework directory of choice. It should be located in your particular Flex SDK’s /frameworks/ directory. If you are using Flex Builder 3 standalone on Windows, that path would probably look something like the following:
C:\Program Files\Adobe\Flex Builder 3\sdks\3.0.0\frameworks\flex-config.xml
Open up the XML file in your XML editor of choice. If you consider yourself a somewhat unlucky person, you may want to make a backup of the file before editing it. Scroll down until you find a section under <fonts>, then, locate the child node <languages>. You can see that there is currently a single language-range specified, although it is currently commented out. By default, the XML we’re interested in should look like this:
<languages>
<language-range>
<lang>englishRange</lang>
<range>U+0020-U+007E</range>
</language-range>
</languages>
Note that it specifies a <language-range /> node with the custom name “englishRange” and a unicode range of U+0020 through U+007E. To use this range, simply uncomment the XML node, and save the file. Note, you may need to clean or recompile your project in Flex Builder. At this point, you can do something like the following in Flex Builder:
<mx:Style>
@font-face {
src: local("Base 02");
fontFamily: Base02Embedded;
unicode-range: englishRange; /* was: U+0020-U+007E */
}
</mx:Style>
As you can see, using a named custom unicode-range is a lot simpler, easier to remember, and a lot more reusable.
Similar to defining unicode ranges in your <mx:Script /> blocks, you can specify multiple ranges in your flex-config.xml file by separating the ranges with commas, as seen in the following snippet:
<language-range>
<lang>UpperLower</lang>
<range>U+0020, U+0041-U+005A, U+0061-U+007A</range>
</language-range>
This code embeds the space character (U+0020), uppercase characters (U+0041 to U+005A), and lowecase characters (U+0061 to U+007A). Then, to use the new range in your Flex applications, you can use the following snippet:
<mx:Style>
@font-face {
src: local("Base 02");
fontFamily: Base02Embedded;
unicode-range: UpperLower;
}
</mx:Style>
If you want to specify multiple custom unicode ranges, simply add more children to the <languages /> node in the flex-config.xml file, as seen in the following snippet:
<languages>
<language-range>
<lang>englishRange</lang>
<range>U+0020-U+0026, U+0027-U+007E</range>
</language-range>
<language-range>
<lang>Uppercase</lang>
<range>U+0020,U+0041-U+005A</range>
</language-range>
<language-range>
<lang>Lowercase</lang>
<range>U+0020,U+0061-U+007A</range>
</language-range>
<language-range>
<lang>Numerals</lang>
<range>U+0030-U+0039,U+002E</range>
</language-range>
<language-range>
<lang>Punctuation</lang>
<range>U+0020-U+002F,U+003A-U+0040,U+005B-U+0060,U+007B-U+007E</range>
</language-range>
<language-range>
<lang>Basic Latin</lang>
<range>U+0020-U+002F,U+0030-U+0039,U+003A-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E</range>
</language-range>
</languages>
You may have noticed that last language-range, “Basic Latin”, had a space in the name. In order to use the custom unicode range in your Flex application, simply wrap the name in double-quotes, as seen in the following snippet:
unicode-range: "Basic Latin";
There you have it, using named unicode ranges in your Flex applications. For a few samples of common unicode ranges, check out the flash-unicode-table.xml XML file in the same directory as your flex-config.xml file. If you are using Flex Builder 3 standalone on Windows, that path would probably look something like the following:
C:\Program Files\Adobe\Flex Builder 3\sdks\3.0.0\frameworks\flash-unicode-table.xml
One last word of advice. Since your flex-config.xml file is unique to each installed SDK, you may need to make the same changes to both your Flex 2.0.1 SDK builds and Flex 3 builds if you switch between SDKs regularly. Also, if you download nightly builds from the Flex 3 SDK nightly builds download page on the Adobe Labs site, you may need to back up your flex-config.xml file before overwriting the currently installed SDK directory.
Happy Flexing!
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
5 Responses to Specifying custom named unicode ranges in Flex
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Thanks for that!
I instantly wondered if it is possible to use multiples of the named ranges. Can’t seem to make that work. I tried: unicodeRange:Uppercase,Lowercase; (no go) unicodeRange:”Uppercase,Lowercase”; (no go), and some other variants.
Is it possible to do something like that, or do you have to define the whole set every time you need different subsets of fonts?
thanks for info.
When my application run on Windows XP, I can enter unicode text (Vietnamese). But when run on Ubuntu 8.0.4, when I’m typing unicode character it display wrong:
Expected: thể
Actual: thÃể.
Please help me out.
Thanks
i have the same problem with linux flash player only, default Ubuntu player (10,0,12,36) behaves little bit different then the latest flash player (10,0,15,3) from the adobe site with debugging enabled ().
Just to increase the list Latin-1 is: “U+0020-U+00FF” and Basic-Latin can be represented as: “U+0020-U+007E”
Thanks for sharing.