You can include fonts in your Flex applications. Although it is easier and more efficient to use the default device fonts, you can embed other fonts so that you can apply special effects to text-based controls, such as rotating and fading.
When you compile an application in Flex, the application stores the names of the fonts that you used to create the text. Adobe® Flash® Player uses the font names to locate identical or similar fonts on the user's system when the application runs. You can also embed fonts in the application so that the exact font is used, regardless of whether the client's system has that font.
You define the font that appears in each of your components by using the fontFamily style property. You can set this property in an external style sheet, an <fx:Style> block, or inline. This property can take a list of fonts, as the following example shows:
.myClass {
fontFamily: Open_Sans, PT_Serif;
color: Red;
fontSize: 22;
fontWeight: bold;
}
If the client's system does not have the first font in the list, Flash Player attempts to find the second, and so on, until it finds a font that matches. If no fonts match, Flash Player makes a best guess to determine which font the client uses.
Fonts are inheritable style properties. So, if you set a font style on a container, all controls inside that container inherit that style, as the following example shows:
<?xml version="1.0"?>
<!-- fonts/InheritableExample.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark"
width="600">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
s|VGroup {
fontFamily: PT_Serif;
fontSize: 13pt;
}
s|HGroup {
fontFamily: Times;
fontSize: 13pt;
}
</fx:Style>
<s:Panel title="Styles Inherited from VGroup Type Selector">
<s:VGroup>
<s:Button label="This Button uses PT_Serif"/>
<s:Label text="This Label control is in PT_Serif."/>
<s:RichText height="75" width="200">
<s:text>
The text in this RichText control uses the PT_Serif font
because it is inherited from the VGroup style.
</s:text>
</s:RichText>
</s:VGroup>
</s:Panel>
<s:Panel title="Styles Inherited from HGroup Type Selector">
<s:HGroup>
<s:Button label="This Button uses Times"/>
<s:Label text="This Label control is in Times."/>
<s:RichText height="75" width="200">
<s:text>
The text in this RichText control uses the Times font
because it is inherited from the HGroup style.
</s:text>
</s:RichText>
</s:HGroup>
</s:Panel>
</s:Application>
This example defines the HGroup and VGroup type selectors' fontSize and fontFamily properties. Flex applies these styles to all components in the container that support those properties; in these cases, the Button, Label, and RichText controls.
Controls that support FTE have limited font fallback. What this means is that when you try to use a special character that is not available in a font, Flash Player attempts to use a character from another font if possible.
You can specify any font for the fontFamily property. However, not all systems have all font faces, which can result in an unexpected appearance of text in your application. The safest course when specifying font faces is to include a device font as a default at the end of the font list. Device fonts do not export font outline information and are not embedded in the SWF file. Instead, Flash Player uses whatever font on the client's local computer most closely resembles the device font.
Flash Player supports three device fonts. The following table describes these fonts:
|
Font name |
Description |
|---|---|
_sans |
The _sans device font is a sans-serif typeface; for example, PT_Serif or Open_Sans. |
_serif |
The _serif device font is a serif typeface; for example, Times Roman. |
_typewriter |
The _typewriter device font is a monospace font; for example, Courier. |
The following example specifies the device font _sans to use if Flash Player cannot find either of the other fonts on the client machine:
<?xml version="1.0"?>
<!-- fonts/DeviceFont.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<!-- Use a vertical layout -->
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
.myClass {
fontFamily: Open_Sans, PT_Serif, "_sans";
color: Red;
fontSize: 12;
fontWeight: bold;
}
</fx:Style>
<s:Panel title="myClass Class Selector with Device Font">
<s:VGroup styleName="myClass">
<s:Button label="Click Me"/>
<s:Label text="This is a Label control."/>
<s:RichText width="200">
<s:text>
The text in the RichText control uses the myClass class selector.
</s:text>
</s:RichText>
</s:VGroup>
</s:Panel>
</s:Application>
Using device fonts does not affect the size of the SWF file because the fonts reside on the client. However, using device fonts can affect performance of the application because it requires that Flash Player interact with the local operating system. Also, if you use only device fonts, your selection is limited to three fonts.
Rather than rely on a client machine to have the fonts you specify, you can embed fonts in your application. This means that the font is always available to Flash Player when the application is running, and you do not have to consider the implications of a missing font.
Supported file types include TrueType fonts (*.ttf), OpenType fonts (*.otf), as well as TrueType Collections (*.ttc), Mac Data Fork Fonts (*.dfont), and Mac Resource Fork TrueType Suitcases (which do not have a file extension).
Benefits of embedded fonts
Embedded fonts have the following benefits:
Client environment does not need the font to be installed.
Embedded fonts can be rotated and faded.
Embedded fonts are anti-aliased, which means that their edges are smoothed for easier readability. This is especially apparent when the text size is large.
Embedded fonts provide smoother playback when zooming.
Text appears exactly as you expect when you use embedded fonts.
When you embed a font, you can use the advanced anti-aliasing information that provides clear, high-quality text rendering in SWF files. Using advanced anti-aliasing greatly improves the readability of text, particularly when it is rendered at smaller font sizes. For more information about advanced anti-aliasing, see Using advanced anti-aliasing with non-CFF based fonts.
Drawbacks of using embedded fonts
Using embedded fonts is not always the best solution, however. Embedded fonts have the following limitations and drawbacks:
Embed only TrueType or OpenType fonts and related "collection" formats. To embed other font types such as Type 1 PostScript fonts, embed that font in a SWF file that you create in Flash or with the fontswf utility, and then embed that SWF file in your application. For information about using the fontswf utility, see Using the fontswf utility.
Embedded fonts increase the file size of your application, because the document must contain font outlines for the text. This can result in longer download times for your users.
In some cases, the text that is rendered by embedded fonts is truncated when they are used in visual components. This can happen, for example, when you explicitly set the width of a control. In these cases, you might be required to change the padding properties of the component by using style properties or subclassing it. This only occurs with some fonts.
If you use MX controls in a Flex 4 application, you might have to add additional code to make the MX control use the embedded font. For more information, see Embedding fonts with MX components.
Embed fonts with CSS
You typically use Cascading Style Sheets (CSS) syntax for embedding fonts in applications. You use the @font-face "at-rule" declaration to specify the source of the embedded font and then define the name of the font by using the fontFamily property. You typically specify the @font-face declaration for each face of the font for the same family that you use (for example, plain, bold, and italic).
Embed fonts with ActionScript
You can also embed fonts in ActionScript by using the [Embed] metadata tag. As with the @font-face declaration, you must specify a separate [Embed] tag for each font face.
Finding fonts
If you attempt to embed a font that the Flex compiler cannot find, Flex throws an error and your application does not compile. As a result, be sure to include directories that contain your fonts in the source path, or use absolute paths to the font file.
To embed TrueType or OpenType fonts, you use the following syntax in your style sheet or <fx:Style> tag:
@font-face {
src: url("location");
fontFamily: alias;
[fontStyle: normal | italic | oblique] ;
[fontWeight: normal | bold | heavy] ;
[embedAsCFF:true | false] ;
[advancedAntiAliasing: true | false];
}
|
Property |
Description |
|---|---|
|
src |
Specifies the file path location of a font. In Flex 4 and later, you cannot embed a font by its local name only. If you specify a relative location, the location is relative to the file in which the @font-face rule appears. |
|
fontFamily |
Sets the alias for the font that you use to apply the font in style sheets. This property is required. If you embed a font with a family name that matches the family name of a system font, the Flex compiler gives you a warning. You can disable this warning by setting the show-shadows-system-font-warnings compiler option to false. |
|
fontStyle |
Set the style type face value for the font. This property is optional, unless you are embedding a face that requires it. The default value is normal. Valid values are normal, italic, and oblique. |
|
fontWeight |
Set the weight type face value for the font. This property is optional, unless you are embedding a face that requires it. The default value is normal. Valid values are normal, bold, and heavy. |
|
embedAsCFF |
Indicates whether to embed an FTE-enabled font for components. Flash Text Engine (FTE) is a library that provides text controls with a rich set of formatting options. For Flex 4 and later, the default value is true. If you set the compatibility-version compiler option to 3.0.0, then the default value is false. If you set the embedAsCFF property to true, then you can use the advanced formatting features of FTE such as bidirectional text, kerning, and ligatures. If you set the value of embedAsCFF to false, then the embedded font does not support FTE, and works only with the MX text components. For information on using FTE-based classes for text rendering in your MX text controls, see Embedding fonts with MX components. |
|
advancedAntiAliasing |
Determines whether to include the advanced anti-aliasing information when embedding the font. This property is optional and only used for legacy fonts. This property is ignored if you embed a font with the embedAsCFF property set to true. You cannot use this option when embedding fonts from a SWF file because this option requires access to the original, raw font file to pre-calculate anti-aliasing information at compile time. For more information on using advanced anti-aliasing, see Using advanced anti-aliasing with non-CFF based fonts. |
Example of @font-face rule
The following example embeds the MyriadWebPro.ttf font file:
@font-face {
src: url("../assets/MyriadWebPro.ttf");
fontFamily: myFontFamily;
embedAsCFF: true;
}
After you embed a font with an @font-face declaration, you can use the value of the fontFamily property, or alias, in a selector's property declarations. The following example uses myFontFamily, the value of the fontFamily property, as the font in the VGroup type selector:
<?xml version="1.0"?>
<!-- fonts/EmbeddedFontFace.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myFontFamily;
embedAsCFF: true;
}
s|VGroup {
fontFamily: myFontFamily;
fontSize: 15;
}
</fx:Style>
<s:Panel title="Embedded Font Applied With Type Selector">
<s:VGroup>
<!-- This MX button tries to use a system font. -->
<mx:Button label="Click Me"/>
<!-- This Spark button uses the font of the VGroup container. -->
<s:Button label="Click Me"/>
<s:Label text="This is a Label control."/>
<s:RichText width="250">
<s:text>
The text in this RichText control uses the
font set on the VGroup.
</s:text>
</s:RichText>
</s:VGroup>
</s:Panel>
<!-- This button uses the default font because it is not in the VGroup. -->
<s:Button label="Click Me"/>
</s:Application>
Considerations when using the MX Button control
When you run this example, you might notice that the label on the MX Button (in the "mx" namespace) disappears. This is because the default style of an MX Button control's label uses a bold typeface. However, the embedded font's typeface (Myriad Web Pro) does not contain a definition for the bold typeface.
The Spark Button (in the "s" namespace) control's label renders with the embedded font because it does not require a bold faced font.
Apply the MXFTEText.css theme if you mix MX and Spark components and use embedded fonts. In this case, you should embed both the plain and bold faces of the embedded font in separate @font-face rules. For more information, see Using FTE in MX controls. This theme sets embedAsCFF to false.
Add fontWeight:bold to the @font-face rule. This will render the MX Button label's text, but with a device font.
Embed a bold typeface so that the label of an MX Button control is rendered with the correct font. For information on embedding bold typefaces, see Using multiple typefaces.
Change the MX Button control's label typeface to be non-bold. You can do this by creating a custom skin for the MX Button control.
Apply embedded font inline
You can also apply the embedded font inline by specifying the alias as the value of the control's fontFamily property, as the following example shows:
<?xml version="1.0"?>
<!-- fonts/EmbeddedFontFaceInline.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myFontFamily;
embedAsCFF: true;
}
</fx:Style>
<s:Panel title="Embedded Font Applied Inline">
<s:VGroup fontFamily="myFontFamily">
<s:Button label="Click Me"/>
<s:Label text="This is a Label."/>
<s:RichText width="200" fontFamily="myFontFamily">
<s:text>The text in the RichText control is Myriad Web Pro.</s:text>
</s:RichText>
</s:VGroup>
</s:Panel>
</s:Application>
The src property in the @font-face declaration specifies the location of the font family. You use the src property to embed a TrueType or OpenType font by location by specifying a valid URI to the font. The URI can be relative (for example, ../fontfolder/akbar.ttf) or absolute (for example, c:/myfonts/akbar.ttf). The URI can also point to a SWF file that has embedded fonts within it, such as a SWF file created with the fontswf utility. For information about using the fontswf utility, see Using the fontswf utility.
You must specify the url of the src property in the @font-face declaration. All other properties are optional.
You can embed TrueType or OTF font files by location by using the [Embed] metadata tag in ActionScript. To embed a font by location, you use the source property in the [Embed] metadata tag.
The [Embed] metadata tag takes the same properties that you set as the @font-face rule. You separate them with commas. For the list of properties, see Embedded font syntax
The following examples embed fonts by location by using the [Embed] tag syntax:
<?xml version="1.0"?>
<!-- fonts/EmbeddedFontFaceActionScriptByLocation.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark"
width="700">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
.mystyle1 {
fontFamily:myMyriadFont;
fontSize: 32pt;
}
.mystyle2 {
fontFamily:myBoldMyriadFont;
fontSize: 32pt;
fontWeight: bold;
}
</fx:Style>
<fx:Script>
/*
* Embed a font by location.
*/
[Embed(source='../assets/MyriadWebPro.ttf',
fontName='myMyriadFont',
mimeType='application/x-font',
embedAsCFF='true'
)]
// You do not use this variable directly. It exists so that
// the compiler will link in the font.
private var font1:Class;
/*
* Embed a font with bold typeface by location.
*/
[Embed(source='../assets/MyriadWebPro-Bold.ttf',
fontWeight='bold',
fontName='myBoldMyriadFont',
mimeType='application/x-font',
embedAsCFF='true'
)]
private var font2:Class;
</fx:Script>
<s:Panel title="Embedded Fonts Using ActionScript">
<s:VGroup>
<s:RichText
width="100%"
height="75"
styleName="mystyle1"
text="This text uses the MyriadWebPro font."
/>
<s:RichText
width="100%"
height="75"
styleName="mystyle2"
text="This text uses the MyriadWebPro-Bold font."
/>
</s:VGroup>
</s:Panel>
</s:Application>
You use the value of the fontName property that you set in the [Embed] tag as the alias (fontFamily) in your style definition.
Note that specifying the mimeType is not necessary if your font uses a known file extension such as *.ttf.
To embed a font with a different typeface (such as bold or italic), you specify the fontWeight or fontStyle properties in the [Embed] statement and in the style definition. For more information on embedding different typefaces, see Using multiple typefaces.
You can specify a subset of the font's character range by specifying the unicodeRange parameter in the [Embed] metadata tag or the @font-face declaration. Embedding a range of characters rather than using the default of all characters can reduce the size of the embedded font and, therefore, reduce the final output size of your SWF file. For more information, see Setting character ranges.
When the @font-face src property points to a file that is a "container" of several fonts (such as a *.ttc or *.dfont file), you use the fontFamily property to select the precise font face you want out of the collection. The value of the fontFamily property should be the full font name of the exact font face that you want to target.
<?xml version="1.0"?>
<!-- fonts/TTCTest.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@font-face {
src:url("../assets/CAMBRIA.TTC");
fontFamily: "Cambria Math";
embedAsCFF: true;
}
@font-face {
src:url("../assets/CAMBRIA.TTC");
fontFamily: "Cambria";
embedAsCFF: true;
}
.useCambriaMath {
fontFamily: "Cambria Math";
fontSize:24;
}
.useCambria {
fontFamily: "Cambria";
fontSize:24;
}
</fx:Style>
<s:HGroup>
<s:Button label="Default Font" fontSize="24"/>
<s:Button label="Cambria Math" styleName="useCambriaMath"/>
<s:Button label="Cambria" styleName="useCambria"/>
</s:HGroup>
</s:Application>
You can also use this technique when using the [Embed] syntax.
Flex 3 components in the Halo theme use non-CFF fonts. The text in these components is rendered with the flash.text.TextField API (instead of the new Flash Text Engine (FTE).
When you embed non-CFF fonts (with the embedAsCFF property set to false), you can use advanced anti-aliasing to provide those fonts with additional information about the font. Non-CFF embedded fonts that use the advanced anti-aliasing information are typically clearer and appear sharper at smaller font sizes. CFF fonts have this information by default.
By default, non-CFF fonts that you embed in applications use the advanced anti-aliasing information. This default is set by the fonts.advanced-anti-aliasing compiler option in the flex-config.xml file (the default value is true). You can override this default value by setting the value in your style sheets or changing it in the configuration file. To disable advanced anti-aliasing in style sheets, you set the advancedAntiAliasing style property to false in your @font-face rule, as the following example shows:
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myFontFamily;
advancedAntiAliasing: false;
embedAsCFF: false;
}
Using advanced anti-aliasing can degrade the performance of your compiler. This is not a run-time concern, but can be noticeable if you compile your applications frequently or use the web-tier compiler. Using advanced anti-aliasing can also cause a slight delay when you load SWF files. You notice this delay especially if you are using several different character sets, so be aware of the number of fonts that you use. The presence of advanced anti-aliasing information may also cause an increase in the memory usage in Flash Player and Adobe® AIR™. Using four or five fonts, for example, can increase memory usage by approximately 4 MB.
When you embed non-CFF fonts that use advanced anti-aliasing in your applications, the fonts function exactly as other embedded fonts. They are anti-aliased, you can rotate them, and you can make them partially or wholly transparent.
Font definitions that use advanced anti-aliasing support several additional styles properties: fontAntiAliasType, fontGridFitType, fontSharpness, and fontThickness. These properties are all inheriting styles, but they are applied on the component being styled. They are not relevant to the actual font embedding process (and thus must not be specified in the @font-face rule).
Because the advanced anti-aliasing-related style properties are CSS styles, you can use them in the same way that you use standard style properties, such as fontFamily and fontSize. For example, a text-based component could use subpixel-fitted advanced anti-aliasing of New Century 14 at sharpness 50 and thickness -35, while all Button controls could use pixel-fitted advanced anti-aliasing of Tahoma 10 at sharpness 0 and thickness 0. These styles apply to all the text in a TextField control; you cannot apply them to some characters and not others.
The default values for the advanced anti-aliasing styles properties are defined in the defaults.css file. If you replace this file or use another style sheet that overrides these properties, Flash Player and AIR use the standard font renderer to render the fonts that use advanced anti-aliasing. If you embed fonts that use advanced anti-aliasing, you must set the fontAntiAliasType property to advanced, or you lose the benefits of the advanced anti-aliasing information.
The following table describes these properties:
|
Style property |
Description |
|---|---|
|
fontAntiAliasType |
Sets the antiAliasType property of internal TextField controls. The valid values are normal and advanced. The default value is advanced, which enables advanced anti-aliasing for the font. Set this property to normal to prevent the compiler from using advanced anti-aliasing. This style has no effect for system fonts or fonts embedded without the advanced anti-aliasing information. |
|
fontGridFitType |
Sets the gridFitType property of internal TextField controls. The valid values are none, pixel, and subpixel. The default value is pixel. For more information, see the TextField and GridFitType classes in the ActionScript 3.0 Reference for the Adobe Flash Platform . This property has the same effect as the gridFitType style property of the TextField control for system fonts, only it applies when you embed fonts with advanced anti-aliasing. Changing the value of this property has no effect unless the fontAntiAliasType property is set to advanced. |
|
fontSharpness |
Sets the sharpness property of internal TextField controls. The valid values are numbers from -400 to 400. The default value is 0. This property has the same effect as the fontSharpness style property on the TextField control for system fonts, only it applies when you embed fonts with advanced anti-aliasing. Changing the value of this property has no effect unless the fontAntiAliasType property is set to advanced. |
|
fontThickness |
Sets the thickness property of internal TextField controls. The valid values are numbers from -200 to 200. The default value is 0. This property has the same effect as the fontThickness style property on the TextField control for system fonts, only it applies when you embed fonts with advanced anti-aliasing. Changing the value of this property has no effect unless the fontAntiAliasType property is set to advanced. |
To use functionality similar to advanced anti-aliasing with CFF based fonts, you use the functionality of FTE that is built into Spark's text-based controls. For more information, see Formatting Data.
You can use the SystemManager class's isFontFaceEmbedded() method to determine whether the font is embedded or whether it has been registered globally with the register() method of the Font class. The isFontFaceEmbedded() method takes a single argument—the object that describes the font's TextFormat—and returns a Boolean value that indicates whether the font family you specify is embedded, as the following example shows:
<?xml version="1.0"?>
<!-- fonts/DetectingEmbeddedFonts.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="determineIfFontFaceIsEmbedded()">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@font-face {
src: url(../assets/MyriadWebPro.ttf);
fontFamily: myPlainFont;
embedAsCFF: true;
}
.myStyle1 {
fontFamily: myPlainFont;
fontSize:12pt
}
</fx:Style>
<fx:Script><![CDATA[
import mx.managers.SystemManager;
import mx.core.FlexGlobals;
import flash.text.TextFormat;
[Bindable]
private var b1:Boolean;
[Bindable]
private var b2:Boolean;
public function determineIfFontFaceIsEmbedded():void {
var tf1:TextFormat = new TextFormat();
tf1.font = "myPlainFont";
var tf2:TextFormat = new TextFormat();
tf2.font = "Open_Sans";
b1 = FlexGlobals.topLevelApplication.systemManager.
isFontFaceEmbedded(tf1);
b2 = FlexGlobals.topLevelApplication.systemManager.
isFontFaceEmbedded(tf2);
}
]]></fx:Script>
<s:Form>
<s:FormItem label="isFontFaceEmbedded (myPlainFont):">
<s:Label id="l1" text=" {b1}"/>
</s:FormItem>
<s:FormItem label="isFontFaceEmbedded (Open_Sans):">
<s:Label id="l2" text="{b2}"/>
</s:FormItem>
</s:Form>
</s:Application>
In this example, the font identified by the myPlainFont family name is embedded, but the Open_Sans font is not.
You can use the Font class's enumerateFonts() method to output information about device or embedded fonts. The following example lists embedded fonts:
<?xml version="1.0"?>
<!-- fonts/EnumerateFonts.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="listFonts()">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myFont;
embedAsCFF: true;
}
@font-face {
src:url("../assets/MyriadWebPro-Bold.ttf");
fontFamily: myFont;
fontWeight: bold;
embedAsCFF: true;
}
@font-face {
src:url("../assets/MyriadWebPro-Italic.ttf");
fontFamily: myFont;
fontStyle: italic;
embedAsCFF: true;
}
.myPlainStyle {
fontSize: 20;
fontFamily: myFont;
}
.myBoldStyle {
fontSize: 20;
fontFamily: myFont;
fontWeight: bold;
}
.myItalicStyle {
fontSize: 20;
fontFamily: myFont;
fontStyle: italic;
}
</fx:Style>
<fx:Script><![CDATA[
private function listFonts():void {
var fontArray:Array = Font.enumerateFonts(true);
ta1.text += "Fonts: \n";
for(var i:int = 0; i < fontArray.length; i++) {
var thisFont:Font = fontArray[i];
ta1.text += "FONT " + i + ":: name: " + thisFont.fontName + "; typeface: " +
thisFont.fontStyle + "; type: " + thisFont.fontType;
if (thisFont.fontType == "embeddedCFF"||thisFont.fontType == "embedded") {
ta1.text += "*";
}
ta1.text += "\n";
}
}
]]></fx:Script>
<s:VGroup>
<s:RichText text="Plain Label" styleName="myPlainStyle"/>
<s:RichText text="Bold Label" styleName="myBoldStyle"/>
<s:RichText text="Italic Label" styleName="myItalicStyle"/>
<s:TextArea id="ta1" height="200" width="400"/>
<s:RichText text="* Embedded" styleName="myItalicStyle"/>
</s:VGroup>
</s:Application>
The following list shows the first few lines of sample output. This list will vary depending on the client's system.
FONT 0:: name: myFont; typeface: regular; type: embeddedCFF* FONT 1:: name: myFont; typeface: bold; type: embeddedCFF* FONT 2:: name: myFont; typeface: italic; type: embeddedCFF* FONT 3:: name: Marlett; typeface: regular; type: device FONT 4:: name: Open_Sans; typeface: regular; type: device FONT 5:: name: Open_Sans CE; typeface: regular; type: device
The enumerateFonts() method takes a single Boolean argument: enumerateDeviceFonts. The default value of the enumerateDeviceFonts property is false, which means it returns an Array of embedded fonts by default.
If you set the enumerateDeviceFonts argument to true, the enumerateFonts() method returns an array of available device fonts on the client system, but only if the client's mms.cfg file sets the DisableDeviceFontEnumeration property to 0, the default value. If you set the DisableDeviceFontEnumeration property to 1, Flash Player cannot list device fonts on a client computer unless you explicitly configure the client to allow it. For more information about configuring the client with the mms.cfg file, see the Flash Player documentation.
The fontswf utility is a simple command line tool that converts a single font face from a font file into a SWF file. This SWF file can be used as the source of an embedded font in your applications. Supported font file types are *.ttf, *.otf, *.ttc, and *.dfont.
The fontswf utility is for users of the Flex SDK that prefer only open-source technology. Because the font managers are not open-source, this utility can be used in their place so that you can embed fonts in your applications.
The fontswf utility is in the sdk_root/bin directory of the Flex SDK. If you do not have the fontswf utility in your bin directory, you must get a more recent version of the SDK.
fontswf [options] font_input_file
c:\flex\bin> fontswf -4 -u U+0020-007F -bold -o c:/temp/myboldfont.swf c:/assets/fonts/myboldfont.swf
|
Option |
Description |
|---|---|
|
-a, -alias name |
Sets the font's alias. The default is the font's family name. |
|
-b, -bold |
Embeds the font's bold face. |
|
-i, -italic |
Embeds the font's italic face. |
|
-o, -output file_path |
Sets the output file path for the SWF file. |
|
-u, -unicode-range range |
Sets the included character range. The default value is "*", which includes all characters. For information on using character ranges, see Setting character ranges. |
|
-3 |
Generates a font SWF file for applications that use TextField-based text rendering. Use this option if you are creating a font SWF file for a Flex 3 application. |
|
-4 |
Generates a font SWF file for applications that support CFF (Flex 4) with Flash Player 10. This is the default option. |
Most fonts have four typeface styles: plain, bold, italic, and bold-italic. You can embed any number of these typeface styles for each font in your applications. If you embed only the bold typeface in your application, you cannot use the normal (or plain) typeface unless you also embed that typeface. For each typeface that you use, you must add a new @font-face declaration to your style sheet.
The following example embeds the bold, italic, and plain typefaces of the Myriad Web Pro font. After you define the font face, you define selectors for the font by using the same alias as the fontFamily. You define one for the bold, one for the italic, and one for the plain face. To apply the font styles, this example applies the class selectors to the Label controls inline:
<?xml version="1.0"?>
<!-- fonts/MultipleFaces.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<!-- Use a vertical layout -->
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myFont;
embedAsCFF: true;
}
@font-face {
/* Note the different filename for boldface. */
src:url("../assets/MyriadWebPro-Bold.ttf");
fontFamily: myFont; /* Notice that this is the same alias. */
fontWeight: bold;
embedAsCFF: true;
}
@font-face {
/* Note the different filename for italic face. */
src:url("../assets/MyriadWebPro-Italic.ttf");
fontFamily: myFont; /* Notice that this is the same alias. */
fontStyle: italic;
embedAsCFF: true;
}
.myPlainStyle {
fontSize: 32;
fontFamily: myFont;
}
.myBoldStyle {
fontSize: 32;
fontFamily: myFont;
fontWeight: bold;
}
.myItalicStyle {
fontSize: 32;
fontFamily: myFont;
fontStyle: italic;
}
</fx:Style>
<s:VGroup>
<s:Label text="Plain Text" styleName="myPlainStyle"/>
<s:Label text="Italic Text" styleName="myItalicStyle"/>
<s:Label text="Bold Text" styleName="myBoldStyle"/>
</s:VGroup>
</s:Application>
Optionally, you can apply the bold or italic type to controls inline, as the following example shows:
<?xml version="1.0"?>
<!-- fonts/MultipleFacesAppliedInline.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myFont;
embedAsCFF: true;
}
@font-face {
src:url("../assets/MyriadWebPro-Bold.ttf");
fontFamily: myFont;
fontWeight: bold;
embedAsCFF: true;
}
@font-face {
src:url("../assets/MyriadWebPro-Italic.ttf");
fontFamily: myFont;
fontStyle: italic;
embedAsCFF: true;
}
.myStyle1 {
fontSize: 32;
fontFamily: myFont;
}
</fx:Style>
<s:VGroup styleName="myStyle1">
<s:Label text="Plain Text"/>
<s:Label text="Italic Text" fontStyle="italic"/>
<s:Label text="Bold Text" fontWeight="bold"/>
</s:VGroup>
</s:Application>
If you use a bold-italic font, the font must have a separate typeface for that font. You specify both properties (fontWeight and fontStyle) in the @font-face and selector blocks, as the following example shows:
@font-face {
src:url("../assets/KNIZIA-BI.TTF");
fontStyle: italic;
fontWeight: bold;
fontFamily: myFont;
embedAsCFF: true;
}
.myBoldItalicStyle {
fontFamily:myFont;
fontWeight:bold;
fontStyle:italic;
fontSize: 32;
}
In the @font-face definition, you can specify whether the font is bold or italic by using the fontWeight and fontStyle properties. For a bold font, you can set fontWeight to bold or an integer greater than or equal to 700. You can specify the fontWeight as plain or normal for a nonboldface font. For an italic font, you can set fontStyle to italic or oblique. You can specify the fontStyle as plain or normal for a nonitalic face. If you do not specify a fontWeight or fontStyle, Flex assumes you embedded the plain or regular font face.
You can also add any other properties for the embedded font, such as fontSize, to the selector, as you would with any class or type selector.
Some fonts with multiple faces are packaged as TTC (TrueType Collection) files. In these cases, you can use the fontFamily alias to embed the specific face in the TTC file that you want. For more information, see Embedding fonts in container formats.
By default, Flex includes the entire font definition for each embedded font in the application, so you should limit the number of fonts that you use to reduce the size of the application. You can limit the size of the font definition by defining the character range of the font. For more information, see Setting character ranges.
By specifying a range (or subset) of characters that compose the face of an embedded font, you reduce the size of an embedded font. Each character in a font that you use must be described; removing some of these characters reduces the overall size of the description information that Flex must include for each embedded font.
You can set the range of glyphs in the flex-config.xml file or in the @font-face declaration. You specify individual characters or ranges of characters using the Unicode values for the characters, and you can set multiple ranges for each font declaration.
The syntax for setting a character range is as follows:
U+[beginning of range]-[end of range];
U+0041-005A
You can also use wild cards Unicode range syntax; for example:
U+00??
This is the same as U+0000-00FF.
U+0041
U+0041,U+0043-00FF,U+0045
If you use a character that is outside of the declared range, Flex displays a device font for that character (for FTE-based controls) or a blank character (for non-FTE-based controls). For more information on setting character ranges in applications built with Flex, see the CSS-2 Fonts specification at www.w3.org/TR/1998/REC-CSS2-19980512/fonts.html#descdef-unicode-range.
If you embed a font from a SWF file that you create with Flash or the fontswf utility, you should set the character range to include only those characters that you need to keep the size of the SWF file as small as possible. For information about using the fontswf utility, see Using the fontswf utility.
You can set the range of embedded characters in an application by using the unicodeRange property of the @font-face declaration. The following example embeds the Myriad Web Pro font and defines the range of characters for the font in the <fx:Style> tag:
<?xml version="1.0"?>
<!-- fonts/EmbeddedFontCharacterRange.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myFontFamily;
embedAsCFF: true;
unicodeRange:
U+0041-005A, /* Upper-Case [A..Z] */
U+0061-007A, /* Lower-Case a-z */
U+0030-0039, /* Numbers [0..9] */
U+002E-002E; /* Period [.] */
}
s|RichText {
fontFamily: myFontFamily;
fontSize: 32;
}
</fx:Style>
<s:Panel title="Embedded Font Character Range">
<s:RichText width="400" height="150">
<s:text>The Text Uses Only Some of Available Characters 0 1 2 3 4 5 6 7 8 9.</s:text>
</s:RichText>
</s:Panel>
</s:Application>
You can specify the language and character range for embedded fonts in the flex-config.xml file by using the <language-range> child tag. This lets you define the range once and use it across multiple @font-face blocks.
The following example creates named ranges in the flex‑config.xml file:
<fonts> <languages> <language-range> <lang>englishRange</lang> <range>U+0020-007E</range> </language-range> <language-range> <lang>lowerCaseEnglish</lang> <range>U+0061-007A</range> </language-range> <languages> </fonts>
In your style sheet, you point to the defined ranges by using the unicodeRange property of the @font-face declaration, as the following example shows:
@font-face {
fontFamily: myPlainFont;
src: url("../assets/MyriadWebPro.ttf");
unicodeRange: "englishRange";
embedAsCFF: true;
}
Flex includes a file that lists convenient mappings of the Flash UnicodeTable.xml character ranges for use in the Flex configuration file. For the Flex SDK, the file is located at flex_install_dir/frameworks/flash-unicode-table.xml.
The following example shows the predefined range Latin 1:
<language-range> <lang>Latin I</lang> <range>U+0020,U+00A1-00FF,U+2000-206F,U+20A0-20CF,U+2100-2183</range> </language-range>
To make ranges listed in the flash-unicode-table.xml file available in your applications, copy the ranges from this file and add them to the flex-config.xml files.
You can use the Font class to detect the available characters in an embedded font. You do this with the hasGlyphs() method.
The following example embeds the same font twice, each time restricting the font to different character ranges. The first font includes support only for the letters A and B. The second font family includes all 128 glyphs in the Basic Latin block.
<?xml version="1.0"?>
<!-- charts/CharacterRangeDetection.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="checkCharacterSupport();"
>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@font-face {
font-family: myABFont;
src:url("../assets/MyriadWebPro.ttf");
/*
* Limit range to the characters A and B.
*/
unicodeRange: U+0041-0042;
embedAsCFF: true;
}
@font-face {
font-family: myWideRangeFont;
src:url("../assets/MyriadWebPro.ttf");
/*
* Set range to the 128 characters in
* the Basic Latin block.
*/
unicodeRange: U+0041-007F;
embedAsCFF: true;
}
</fx:Style>
<fx:Script><![CDATA[
public function checkCharacterSupport():void {
var fontArray:Array = Font.enumerateFonts(false);
for(var i:int = 0; i < fontArray.length; i++) {
var thisFont:Font = fontArray[i];
if (thisFont.hasGlyphs("DHARMA")) {
ta1.text += "The font '" + thisFont.fontName +
"' supports these glyphs.\n";
} else {
ta1.text += "The font '" + thisFont.fontName +
"' does not support these glyphs.\n";
}
}
}
]]></fx:Script>
<s:VGroup>
<s:RichText>
<s:text>myABFont unicodeRange: U+0041-0042 (characters A and B)</s:text>
</s:RichText>
<s:RichText>
<s:text>myWideRangeFont unicodeRange: U+0041-007F (Basic Latin chars)</s:text>
</s:RichText>
<s:Label text="Glyphs: DHARMA"/>
<s:RichText id="ta1" height="150" width="300"/>
</s:VGroup>
</s:Application>
When using multi-byte fonts that have a large number of characters, such as those used in Asian languages, you should embed the smallest possible set of characters. If you embed a font's entire character set, the size of your application's SWF file can be very large. You can define sets of Unicode character ranges in the flex-config.xml file and then reference the name of that range in your style's @font-face declaration.
Flex provides predefined character ranges for common multi-byte scripts such as Thai, Kanji, Hangul, and Hebrew in the flash-unicode-table.xml file. This file is not processed by Flex, but is included to provide you with ready definitions for various character ranges. For example, the following character range for Thai is listed in the flash-unicode-table.xml file:
<language-range> <lang>Thai</lang> <range>U+0E01-0E5B</range> </language-range>
To use this language in your application, copy the character range to the flex-config.xml file or pass it on the command line by using the fonts.languages.language-range option. Add the full definition as a child tag to the <languages> tag, as the following example shows:
<flex-config> <compiler> <fonts> <languages> <language-range> <lang>thai</lang> <range>U+0E01-0E5B</range> </language-range> </languages> </fonts> </compiler> ... </flex-config>
You can change the value of the <lang> element to anything you want. When you embed the font by using CSS, you refer to the language by using this value in the unicodeRange property of the @font-face declaration, as the following example shows:
@font-face {
fontFamily:"Thai_font";
src: url("../assets/THAN.TTF"); /* Embed from file */
unicodeRange:"thai";
embedAsCFF: true;
}
The embedAsCFF (Compact Font Format) property indicates whether to embed a font that supports the advanced text layout features used by the Flash Text Engine (FTE). This is sometimes referred to as DefineFont4. If you set the value of the embedAsCFF property to true, then you can only use that font with controls that support FTE. If you set the value of the embedAsCFF property to false, then the embedded font does not support FTE and you can only use that font with controls that do not have FTE support.
The implications of this appear when you try to use MX controls with a font that was embedded with FTE support. In those cases, the text does not appear, as the following example shows.
<?xml version="1.0"?>
<!-- fonts/CFFTest.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myCFFFont;
embedAsCFF: true;
}
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myFontNoCFF;
embedAsCFF: false;
}
.myCFFStyle {
fontSize: 32;
fontFamily: myCFFFont;
}
.myStyleNoCFF {
fontSize: 32;
fontFamily: myFontNoCFF;
}
</fx:Style>
<s:Panel title="Using Correct Fonts">
<s:VGroup>
<s:Button label="Spark Button" styleName="myCFFStyle"/>
<mx:Button label="MX Button" styleName="myStyleNoCFF"/>
</s:VGroup>
</s:Panel>
<s:Panel title="Using Incorrect Fonts">
<s:VGroup>
<s:Button label="Spark Button" styleName="myCFFStyle"/>
<!-- The Button label will not show up because it's a MX control that
is attempting to use a Spark-compatible embedded font. -->
<mx:Button label="MX Label" styleName="myCFFStyle"/>
</s:VGroup>
</s:Panel>
</s:Application>
As this example illustrates, if you mix MX and Spark controls inside the same application, you might not be able to use the same embedded font.
Specify that the MX controls use FTE classes to render text, rather than their default text renderers.
Embed both the non-CFF version of the font in addition to the CFF version of the font. This is the less-desireable approach because it increases the size of your SWF file.
The following sections describe each of these solutions.
The controls that support FTE include all Spark components in the spark.components.* package. This includes the Spark text controls such as Label, RichText, and RichEditableText. This also includes Spark versions of the TextInput and TextArea controls. This does not include MX controls in the mx.controls.* package.
The reason that MX controls do not support FTE is that in previous versions of Flex, they used the UITextField subcomponent to render text. This subcomponent does not support FTE. Spark controls, on the other hand, use FTE-compatible classes to render text.
The Flex SDK provides the mx.core.UIFTETextField and mx.controls.MXFTETextInput classes that support FTE for MX text controls. You can use these classes in some MX controls so that those controls can use CFF versions of embedded fonts. As a result, those controls can use the same embedded fonts that you also use with the Spark controls. You do this by setting the textFieldClass and textInputClass styles to use these classes.
The easiest way to use the MXFTETextInput and UIFTETextField classes with your MX text controls is to apply the MXFTEText.css theme file to your application. This theme causes your MX controls to use the MXFTETextInput and UIFTETextField classes for text rendering. The MXFTEText.css theme file is a convenience theme that is set up to apply only FTE-supporting classes to MX controls.
DateField {
textInputClass: ClassReference("mx.controls.MXFTETextInput");
}
Label {
textFieldClass: ClassReference("mx.core.UIFTETextField");
}
On the command line, you specify the MXFTEText.css theme file by using the theme compiler option, as the following example shows:
mxmlc -theme+=frameworks/projects/spark/MXFTEText.css MyApp.mxml
Note that instead of setting theme=filename, this example uses theme+=filename. This is so that the MXFTEText.css file is used as a theme, in addition to the default themes, not as a replacement of the default themes.
<?xml version="1.0"?>
<!-- fonts/UseTLFTextTheme.mxml -->
<!-- Compile this example by setting theme=MXFTEText.css for a compiler argument. -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myCFFFont;
embedAsCFF: true;
}
.myCFFStyle {
fontSize: 32;
fontFamily: myCFFFont;
}
</fx:Style>
<s:Panel title="Spark and MX Buttons">
<s:VGroup>
<s:Button label="Spark Button" styleName="myCFFStyle"/>
<mx:Button label="MX Button" styleName="myCFFStyle"/>
</s:VGroup>
</s:Panel>
</s:Application>
defaultDataGridItemRenderer: ClassReference("mx.controls.dataGridClasses.FTEDataGridItemRenderer");
Some controls are not affected by the MXFTEText.css theme. This is because some MX controls have Spark equivalents, such as Button or ComboBox. As a result, you should use the Spark version of the control instead of the MX version where possible.
<?xml version="1.0" encoding="utf-8"?>
<!-- fonts/TextFieldClassExample.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:layout>
<s:VerticalLayout
paddingLeft="20" paddingRight="20"
paddingTop="20" paddingBottom="20" />
</s:layout>
<fx:Style>
@namespace mx "library://ns.adobe.com/flex/mx";
@namespace s "library://ns.adobe.com/flex/spark";
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: cffFont;
embedAsCFF: true;
}
mx|Label{
fontFamily: cffFont;
fontSize: 22;
textFieldClass:ClassReference("mx.core.UIFTETextField");
}
s|Label{
fontFamily: cffFont;
fontSize: 22;
}
</fx:Style>
<mx:Label text="Hello World 1234567890 [MX Label]" width="100%"/>
<s:Label text="Hello World 1234567890 [Spark Label]" width="100%"/>
</s:Application>
<mx:Label text="Hello World 1234567890 [Label]" width="100%" textFieldClass="mx.core.UIFTETextField"/>
Do not use the setStyle() method to specify the value of these style properties in ActionScript.
The UIFTETextField class does not support editing, scrolling, selection, linking, or rich text. As a result, you can only use this class on MX controls that do not use these features. The MXFTETextInput class can be edited and is selectable.
Accordion
Alert (the text is not selectable)
Button
ButtonBar
CheckBox
DateChooser
FileSystemComboBox
FileSystemHistoryButton
FormHeading
FormItem
FormHeading
FormItem
HSlider (the labels follow the same rules as the MX Label control)
LinkBar
LinkButton
Menu
MenuBar
Panel
PopUpButton
PopUpMenuButton
PrintDataGrid
ProgressBar
RadioButton
TabBar
TabNavigator
TitleWindow
ToggleButtonBar
ToolTip
VSlider (the labels follow the same rules as the MX Label control)
|
MX control |
Description |
|---|---|
|
List-based components (such as List, FileSystemList, HorizontalList, TileList, DataGrid, and Tree) |
Some list-based components have Spark equivalents (including List, HorizontalList, and TileList). You can use the UIFTETextField and MXFTETextInput classes in the other components if you do not use selection, editability, HTML links, or scrolling. Otherwise, you should embed a non-CFF version of the font to support these controls. |
|
Label and Text |
Use Spark equivalents such as Label, RichText, and RichEditableText. You can use UIFTETextField with the Label and Text controls if the text is not selectable or you do not use the htmlText property to specify the content of the controls. Otherwise, you should embed a non-CFF version of the font to support these controls. |
|
TextInput and TextArea |
Use the Spark equivalents. Otherwise, you should embed a non-CFF version of the font to support these controls. |
|
RichTextEditor |
There is no equivalent class. In this case, you should embed a non-CFF version of the font to support this control. |
|
ColorPicker |
There is no equivalent class. In this case, you should embed a non-CFF version of the font to support this control. However, the ColorPicker control only uses the font to display a color value, so in some cases, using an embedded font might not be necessary. |
|
ComboBox |
Use the Spark equivalent. If your ComboBox's text does not need to be editable, you can use the MXFTETextInput class. Otherwise, you should embed a non-CFF version of the font to support this control. |
|
DateField |
If you do not use editability, then you can use the MXFTETextInput class. Otherwise, you should embed a non-CFF version of the font to support this control. |
|
NumericStepper |
If you do not use editability, then you can use the MXFTETextInput class. Otherwise, you should embed a non-CFF version of the font to support this control. |
Has no Spark equivalent
Does not support using the UIFTETextField and MXFTETextInput classes for text rendering
Must use selection, scrolling, or HTML text
Is editable
If you compile an application with the compatibility-version compiler option set to 3.0.0, then the non-CFF version of the font is embedded automatically.
<?xml version="1.0"?>
<!-- fonts/EmbedBoth.mxml -->
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Style>
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myCFFFont;
embedAsCFF: true;
}
@font-face {
src:url("../assets/MyriadWebPro.ttf");
fontFamily: myFontNoCFF;
advancedAntiAliasing: true;
embedAsCFF: false;
}
.myCFFStyle {
fontSize: 32;
fontFamily: myCFFFont;
}
.myStyleNoCFF {
fontSize: 32;
fontFamily: myFontNoCFF;
}
</fx:Style>
<s:Panel title="Using Two Different Embedded Fonts">
<s:VGroup>
<s:Label text="Spark Label"
styleName="myCFFStyle"/>
<mx:Label text="MX Label"
styleName="myStyleNoCFF" selectable="true"/>
</s:VGroup>
</s:Panel>
</s:Application>
Note that when embedding a non-CFF font, you have the option of specifying the advancedAntiAliasing property. With CFF fonts, this property is ignored. The advanced anti-aliasing functionality is provided natively with FTE.
There are some techniques that you can use to successfully embed fonts into your applications.
The following table describes common compiler errors and their solutions:
|
Error |
Solution |
|---|---|
Unable to resolve 'swf_file_name' for transcoding |
Indicates that the font was not found by the compiler. Ensure that the path to the font is correct in the @font-face declaration or the [Embed] tag and that the path is accessible by the compiler. |
Font 'font_name' with style_description not found |
Indicates that the fontName property used in the [Embed] statement might not match the name of the font. For fonts in SWF files, ensure that the spelling and word spacing of the font name in the list of available fonts in Flash is the same as the fontName property in your [Embed] statement and the fontFamily property that you use in your style definitions. This error can also mean that the font's style was not properly embedded in Flash. Open the FLA file and ensure that there is a text area with the font and style described, that the text is dynamic, and that you selected a character range for that text. |
To determine if your fonts are embedded properly, you can use the isFontFaceEmbedded() method of the SystemManager, as described in Detecting embedded fonts.
If you are using fonts embedded for Spark controls with MX controls, you should refer to Embedding fonts with MX components.
To properly embed your fonts, try the following techniques:
If one type of control is not correctly displaying its text, ensure that you are embedding the appropriate typeface. For example, the MX Button control's text labels require the bold typeface. If you do not embed the bold typeface, the MX Button control does not display the label's text.
In your application, ensure that you set all properties for each font typeface in the @font-face declaration or [Embed] statement. To embed a bold typeface, you must set the fontWeight property to bold, as the following example shows:
@font-face {
src: url(../assets/MyriadWebProEmbed.ttf);
fontFamily: "Myriad Web Pro";
fontWeight: bold;
embedAsCFF: true;
}
You also must set the fontWeight style property in your style definition:
.myStyle2 {
fontFamily: "Myriad Web Pro";
fontWeight: bold;
fontSize: 12pt;
}
If you use the [Embed] statement, you must set the fontWeight property to bold as the following example shows:
[Embed(source="MyriadWebProEmbed.ttf", fontName="Myriad Web Pro",fontWeight="bold")]
For fonts that are embedded in SWF files that you import, open the FLA file in Flash and ensure that all of the typefaces were added properly. Select each text area and do the following:
Check that the font name is correct. Ensure that the spelling and word spacing of the font name in the list of available fonts in Flash is the same as the fontFamily property in the @font-face declaration or the fontName property in your [Embed] statement. This value must also match the fontFamily property that you use in your style definitions.
If you did not select an anti-aliasing option for the font in Flash 8 (for example, you chose Bitmap Text (no anti-alias)), you might need to change the value of the font name to a format that matches fontName_fontSizept_st (for example, "Wingdings_8pt_st"). In the CSS for that bitmap font, be sure to set fontAntiAliasType to normal.
To determine the exact font name exported by Flash (which you must match as the value of the fontFamily property in your application), open the SWF file in Flash and select Debug > Variables.
Check that the style is properly applied. For example, select the bold text area and check that the typeface really is bold.
Click the Embed button and ensure that the range of embedded characters includes the characters that you use in your application.
Check that each text area is set to Dynamic Text and not Static Text or Input Text. The type of text is indicated by the first drop-down box in the text's Properties tab.
Unless the SWF file was compiled with CFF, you must set the value of the embedAsCFF property to false for the imported font.
For fonts in SWF files, ensure that you are using the latest SWF file that contains your fonts and was generated in Flash. Regenerate the SWF file in Flash if necessary.
@font-face {
src: url(../assets/MyriadWebProEmbed.ttf);
fontFamily: "Myriad Web Pro";
fontWeight: bold;
embedAsCFF: false;
}
Flex includes several font managers to handle embedded fonts. The font managers take embedded font definitions and draw each character in Flash Player. This process is known as transcoding. The font managers are Batik, JRE, AFE (Adobe Font Engine), and CFF, represented by the BatikFontManager, JREFontManager, AFEFontManager, and CFFFontManager classes, respectively.
The CFF font manager supports both TrueType and OpenType fonts. It also supports URL and system fonts. Use this manager for all CFF fonts.
The AFE font manager supports both TrueType and OpenType fonts. It also adds support for all non-CFF font embedding in Flex 4. The AFE font manager is the only font manager that you can use to transcode TrueType or OpenType fonts for non-CFF fonts. The fonts can only be referenced by a path to the font file, not by an OS-specific font name. If you embed an OpenType font, the compiler will use the AFE font manager to transcode the font because the other font managers do not support OpenType fonts, unless that OpenType font is a system font, in which case, the compiler will throw an error. None of the font managers can transcode OpenType fonts that are embedded as system fonts.
The Batik font manager transcodes only TrueType fonts, but does not support TrueType Collections (*.ttc). It does not transcode system fonts. If you specify the font location when you embed the font, the compiler will use the Batik font manager. In general, the Batik font manager provides smoother rendering and more accurate line metrics (which affect multiline text and line-length calculations) than the JRE font manager.
The JRE font manager transcodes TrueType system fonts, but the quality of output is generally not as good as the Batik font manager. If you install the font on your system, the compiler will use the JRE font manager because the Batik font manager does not support system fonts.
The following table shows which fonts are supported by which font managers:
|
CFF |
Batik |
AFE |
JRE |
|
|---|---|---|---|---|
|
Font type |
TrueType, OpenType |
TrueType |
TrueType, OpenType |
TrueType |
|
Method of embedding |
URL, system |
URL |
URL |
System |
You determine which font managers the compiler can use in the flex-config.xml file. The default setting is to use all of them, as the following example shows:
<fonts> <managers> <manager-class>flash.fonts.JREFontManager</manager-class> <manager-class>flash.fonts.BatikFontManager</manager-class> <manager-class>flash.fonts.AFEFontManager</manager-class> <manager-class>flash.fonts.CFFFontManager</manager-class> </managers> </fonts>
The preference of <manager> elements is in reverse order. This means that by default the CFF font manager is the preferred font manager; the compiler checks to see if a font can be transcoded using it first. If not, then the compiler checks to see whether the font can be transcoded using the AFE font manager and then the Batik font manager. Finally, if the other font managers fail, the compiler checks to see whether the JRE font manager can transcode the font.
Navigation
Adobe, Adobe AIR, Adobe Flash, Adobe Flash Platform and Adobe Flash Player are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries and are used by permission from Adobe. No other license to the Adobe trademarks are granted.