<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flexpert to the rescue &#187; Flash</title>
	<atom:link href="http://www.flexpert.be/tag/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.flexpert.be</link>
	<description>Blog site about all things Adobe</description>
	<lastBuildDate>Sat, 28 Aug 2010 17:07:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Embedding fonts in Flex, AIR and Actionscript projects</title>
		<link>http://www.flexpert.be/2009/06/embedding-fonts-in-flex-air-and-actionscript-projects/</link>
		<comments>http://www.flexpert.be/2009/06/embedding-fonts-in-flex-air-and-actionscript-projects/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 12:45:57 +0000</pubDate>
		<dc:creator>Steven Peeters</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Actionscript 3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[embed]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[font]]></category>

		<guid isPermaLink="false">http://www.flexpert.be/?p=49</guid>
		<description><![CDATA[Question: I’m trying to develop a Flex application that uses a Text component which will take an html rendered text. So far, no problem there. However, I would like to use more than 1 font (regular, bold, italic) in this text component and I’m not sure if the fonts are all on the user’s computer. [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: medium;"><strong>Question:</strong></span></p>
<p><em>I’m trying to develop a Flex application that uses a Text component which will take an html rendered text. So far, no problem there. However, I would like to use more than 1 font (regular, bold, italic) in this text component and I’m not sure if the fonts are all on the user’s computer. What’s the best way of tackling this problem?</em></p>
<p><span style="font-size: medium;"><strong>Solution:</strong></span></p>
<p>If you don’t know for sure the fonts are installed on the user’s computer (because it’s some exotic font or some typical OS dependant font, the best way of dealing with that would be to embed the font in your Flex application (or any Actionscript application). Now, how do you do this exactly when you want to embed multiple fonts for a single component? Well, I’ll show you.</p>
<p>There are actually 3 ways of embedding your fonts in an application:</p>
<ol>
<li>embed fonts in your Actionscript code</li>
<li>embed fonts directly in CSS</li>
<li>embed fonts in CSS using a SWF file that has (part of) the fonts embedded</li>
</ol>
<p><strong>1. Embed your fonts directly in Actionscript</strong></p>
<p>To embed a font in Actionscript, you’ll have to assign it to some static variable to be able to incorporate it in the compilation of your application. Just before that variable definition, you specify a <strong>metadata tag “Embed”</strong> (just like you would if you were to embed an image). The properties for this metadata tag are:</p>
<ul>
<li>source: the location of your font file</li>
<li>fontName: the name you want to use in the application for your font. I’ve chosen to use myFont to indicate it can be anything you like. But you could also choose the font name itself if you prefer it like that</li>
<li>fontStyle: italic if it is an italic font (can be in combination with fontWeight)</li>
<li>fontWeight: bold if it is a bold font (can be in combination with fontStyle)</li>
<li>mimeType: e.g. application/x-font-truetype if it’s a truetype font</li>
</ul>
<p>Repeat this step for all the different fonts you want to embed, but <strong>make sure that the fontName property is exactly the same</strong> for all of them. So, when for example you want to embed a Verdana font in regular, bold, italic and boldItalic, your code would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Embed<span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span>=<span style="color: #990000;">&quot;C:<span style="">\\</span>Windows<span style="">\\</span>Fonts<span style="">\\</span>Verdana.ttf&quot;</span>,
       <span style="color: #004993;">fontName</span>=<span style="color: #990000;">&quot;myFont&quot;</span>,
       mimeType=<span style="color: #990000;">&quot;application/x-font-truetype&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> static const VerdanaTTF<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
&nbsp;
<span style="color: #000000;">&#91;</span>Embed<span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span>=<span style="color: #990000;">&quot;C:<span style="">\\</span>Windows<span style="">\\</span>Fonts<span style="">\\</span>VerdanaBold.ttf&quot;</span>,
       fontWeight=<span style="color: #990000;">&quot;bold&quot;</span>,
       <span style="color: #004993;">fontName</span>=<span style="color: #990000;">&quot;myFont&quot;</span>,
       mimeType=<span style="color: #990000;">&quot;application/x-font-truetype&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> static const VerdanaBoldTTF<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
&nbsp;
<span style="color: #000000;">&#91;</span>Embed<span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span>=<span style="color: #990000;">&quot;C:<span style="">\\</span>Windows<span style="">\\</span>Fonts<span style="">\\</span>VerdanaItalic.ttf&quot;</span>,
       <span style="color: #004993;">fontStyle</span>=<span style="color: #990000;">&quot;italic&quot;</span>,
       <span style="color: #004993;">fontName</span>=<span style="color: #990000;">&quot;myFont&quot;</span>,
       mimeType=<span style="color: #990000;">&quot;application/x-font-truetype&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> static const VerdanaItalicTTF<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
&nbsp;
<span style="color: #000000;">&#91;</span>Embed<span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span>=<span style="color: #990000;">&quot;C:<span style="">\\</span>Windows<span style="">\\</span>Fonts<span style="">\\</span>VerdanaBoldItalic.ttf&quot;</span>,
       fontWeight=<span style="color: #990000;">&quot;bold&quot;</span>,
       <span style="color: #004993;">fontStyle</span>=<span style="color: #990000;">&quot;italic&quot;</span>,
       <span style="color: #004993;">fontName</span>=<span style="color: #990000;">&quot;myFont&quot;</span>,
       mimeType=<span style="color: #990000;">&quot;application/x-font-truetype&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> static const VerdanaBoldItalicTTF<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;</pre></div></div>

<p><strong>2. Embed your font directly in CSS</strong></p>
<p>Now, you can do exactly the same using only an external CSS file. The embedding part itself is a little bit different though. You would have to use the @font-face style for each of the separate font styles you want to incorporate in the resulting SWF file and provide it with the following attributes:</p>
<ul>
<li>src: the location of your font file.</li>
<li>fontFamily: the name you want to use in your application for your font. Again, I’ve chosen to use myFont to indicate it can be anything you like. But you could also choose the font name itself if you prefer it like that</li>
<li>fontStyle: italic if it is an italic font (can be combined with fontWeight)</li>
<li>fontWeight: bold if it is a bold font (can be combined with fontStyle)</li>
</ul>
<p>The src style attribute can either point to a file on the local file system or to a file somewhere on the web or another location. For embedding a font which resides <strong>on the local file system, you would specify local(“&lt;font name&gt;”)</strong> . The font name will remain the same for all the variations you want to embed, because it’s the style attributes that will make up the difference here. You’ll probably use this local specification most of the time, because you’ll probably want to embed a font that you have on your own computer. However, if you were to incorporate a font file which resides somewhere <strong>on a remote location, you would use url(“&lt;path to font file&gt;”)</strong>. This path can either be a relative or absolute path.</p>
<p>Of course you need to <strong>repeat the @font-face style for every font variation you want to embed</strong>. Again, make sure that the fontFamily property is exactly the same for all of them, because you will use that name in the fontFamily attribute of the style you are going to apply on the component in order to use the different font styles.</p>
<p>For the same Verdana font definitions, your code should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">@font<span style="color: #000000; font-weight: bold;">-</span>face <span style="color: #000000;">&#123;</span>
	src<span style="color: #000000; font-weight: bold;">:</span> local<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Verdana&quot;</span><span style="color: #000000;">&#41;</span>;
	fontFamily<span style="color: #000000; font-weight: bold;">:</span> myFont;
<span style="color: #000000;">&#125;</span>
&nbsp;
@font<span style="color: #000000; font-weight: bold;">-</span>face <span style="color: #000000;">&#123;</span>
	src<span style="color: #000000; font-weight: bold;">:</span> local<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Verdana&quot;</span><span style="color: #000000;">&#41;</span>;
	fontFamily<span style="color: #000000; font-weight: bold;">:</span> myFont;
	fontWeight<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">bold</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
@font<span style="color: #000000; font-weight: bold;">-</span>face <span style="color: #000000;">&#123;</span>
	src<span style="color: #000000; font-weight: bold;">:</span> local<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Verdana&quot;</span><span style="color: #000000;">&#41;</span>;
	fontFamily<span style="color: #000000; font-weight: bold;">:</span> myFont;
	<span style="color: #004993;">fontStyle</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">italic</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
@font<span style="color: #000000; font-weight: bold;">-</span>face <span style="color: #000000;">&#123;</span>
	src<span style="color: #000000; font-weight: bold;">:</span> local<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Verdana&quot;</span><span style="color: #000000;">&#41;</span>;
	fontFamily<span style="color: #000000; font-weight: bold;">:</span> myFont;
	fontWeight<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">bold</span>;
	<span style="color: #004993;">fontStyle</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">italic</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>3.	Use a SWF file with partially embedded fonts</strong></p>
<p>These first 2 methods work just fine. However, <strong>they do add several hundreds of kilobytes to your SWF file size</strong>. And this is only for 1 specific font, so imagine what it would do to a SWF file that compiles a couple of fonts into your application. This is due to the fact that a font is more than just alphabetical characters. Most of the time you don’t need all of those characters, but just the basic ones like digits, lowercase and uppercase letters and punctuation signs.</p>
<p>Fortunately, <strong>there is also another solution that allows you to limit the number of glyphs</strong> (this is what the individual font signs are called) to incorporate in your application. To be able to do this, we are going to have to take our development out of Flex Builder (or Flash builder of course) and bring it into Flash.</p>
<p>So, in Flash, you start by creating a <strong>new ActionScript 3 Flash file</strong>. On the stage, you <strong>place a text field</strong> and type some text. In the properties of that text field, you have to <strong>set it to Dynamic Text</strong>. In the Character subframe of the properties panel, you <strong>then select the font you want to embed as well as the style</strong> (regular, bold, italic or bold italic).</p>
<p><img class="aligncenter size-full wp-image-81" title="Properties panel" src="http://www.flexpert.be/wp-content/uploads/dynamic_text.jpg" alt="Properties panel" width="405" height="401" /></p>
<p>At the bottom of that same properties panel, you also see <strong>a button labeled “Character Embedding”</strong>. Just click on that one and you’ll get a dialog box which allows you to <strong>select the glyphs you want to embed</strong> in the Flash file. Select the uppercase, lowercase, numerals and punctuation glyphs and click ok to embed them in the SWF.</p>
<p><strong><img class="aligncenter size-full wp-image-87" title="Embed settings" src="http://www.flexpert.be/wp-content/uploads/embed.jpg" alt="Embed settings" width="304" height="399" /></strong></p>
<p>Now, perform the same procedure for the other font styles you want to embed, so you’ll eventually end up with 4 different dynamic text fields that all include a different style of your font.</p>
<p>The next step is then to <strong>compile your SWF file</strong>, so you can use it in your Flex or AIR application. To do that, you’ll first have to go to the Publish Settings, which you can find in the File menu. In the Format tab of the settings dialog, make sure that only Flash type is selected and then click on Publish. A SWF file will now be generated and that file will contain all the characters from the different font styles that you’ve just indicated.</p>
<p>Go back to Flex Builder (or Flash Builder of course) and <strong>add a fonts directory to your project</strong>. In this directory you place the SWF file that you have just generated. Now, to actually use the embedded fonts from the SWF file, <strong>we have to rely again on CSS</strong>. This is quite similar to the second method I’ve explained. However, <strong>this time the fontFamily property must be exactly the same as the actual font that you have included</strong>. So, in my example here, it has to be “Verdana”. You cannot choose the fontFamily name you want to use in your application freely anymore.</p>
<p>So, enter the different @font-face styles that match the embedded fonts from the SWF file in the same way as you would do when using the second method, bearing in mind that the font name is fixed now. Your code should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">@font<span style="color: #000000; font-weight: bold;">-</span>face <span style="color: #000000;">&#123;</span>
	src<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">url</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;../fonts/Verdana.swf&quot;</span><span style="color: #000000;">&#41;</span>;
	fontFamily<span style="color: #000000; font-weight: bold;">:</span> Verdana;
<span style="color: #000000;">&#125;</span>
&nbsp;
@font<span style="color: #000000; font-weight: bold;">-</span>face <span style="color: #000000;">&#123;</span>
	src<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">url</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;../fonts/Verdana.swf&quot;</span><span style="color: #000000;">&#41;</span>;
	fontFamily<span style="color: #000000; font-weight: bold;">:</span> Verdana;
	fontWeight<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">bold</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
@font<span style="color: #000000; font-weight: bold;">-</span>face <span style="color: #000000;">&#123;</span>
	src<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">url</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;../fonts/Verdana.swf&quot;</span><span style="color: #000000;">&#41;</span>;
	fontFamily<span style="color: #000000; font-weight: bold;">:</span> Verdana;
	<span style="color: #004993;">fontStyle</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">italic</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
@font<span style="color: #000000; font-weight: bold;">-</span>face <span style="color: #000000;">&#123;</span>
	src<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">url</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;../fonts/Verdana.swf&quot;</span><span style="color: #000000;">&#41;</span>;
	fontFamily<span style="color: #000000; font-weight: bold;">:</span> Verdana;
	fontWeight<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">bold</span>;
	<span style="color: #004993;">fontStyle</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">italic</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>When you take a closer look at the generated SWF file, <strong>you will certainly notice a big difference in file size between the first 2 methods and this last one</strong>. This is because now, not all glyphs are embedded in the SWF file.</p>
<p>All you have to do now, is just set the fontFamily for the necessary components to the name you’ve just used in your CSS or Actionscript style embedding of the fonts, make sure you use the htmlText property and you’re all set to go.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flexpert.be/2009/06/embedding-fonts-in-flex-air-and-actionscript-projects/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
