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.
Spark components by default use the new Text Layout
Framework (TLF) engine and require CFF embedded fonts. Renaun Erickson demonstrates (video) how
to embed fonts for both TLF and non-TLF based components.
Peter DeHaan’s FlexExamples blog includes several examples of embedding fonts
in Flex 4.
Cameron Yule explains how how embedding fonts in
Flex 4 uses Text Layout Framework (TLF).
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).
Note: Check your font licenses before embedding any
font files in your applications. Fonts might have licensing restrictions
that preclude them from being stored as vector information.
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.
Embedded font syntax
To embed TrueType or OpenType fonts, you use the following
syntax in your style sheet or <fx:Style> tag:
The following table describes the properties for the @font-face rule:
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.
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.
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>
The executing SWF file for the previous example is shown below:
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.
To ensure that the MX Button control’s label uses the proper
typeface, you can:
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 executing SWF file for the previous example is shown below:
Locating embedded fonts
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.
Embedding fonts in ActionScript
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>
The executing SWF file for the previous example is shown below:
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.
Embedding fonts in container formats
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.
The following example embeds and uses two different fonts from
the same TTC file:
You can also use this technique when using the [Embed] syntax.
Using advanced anti-aliasing with non-CFF based fonts
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:
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.
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.
Detecting embedded fonts
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:
The executing SWF file for the previous example is shown below:
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: Arial; typeface: regular; type: device
FONT 5:: name: Arial 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.
Using the fontswf utility
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 Mozilla Public License version of
the Flex SDK. This version includes 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. For Flash Builder, the fontswf utility is located
in the sdks/4.6.0/bin directory. If you do not have the fontswf
utility in your bin directory, you must get a more recent version of
the SDK.
You use fontswf by invoking it from the command line, as the
following example shows:
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.