Cameron Yule

Embedded fonts and the Text Layout Framework

Adobe’s Text Layout Framework, in combination with the new text engine in Flash Player 10, is finally giving Flash developers the kind of typographic control we’ve needed for years. Feature highlights include bi—directional and vertical text, support for over 30 writing systems, flowing text across multiple columns and vastly improved character formatting including kerning, ligatures and typographic case.

While a few tutorials on it have been appearing in the last few days, notably Using the Text Layout Framework in Flex 3.2 and AIR 1.5, there’s still a lack of information on how to use embedded fonts which as we all know is necessary if you want to use non-standard fonts or tween the alpha property of your text.

Flex 4 SDK / Flash Professional CS4

From the Text Layout Framework release notes;

DefineFont4 is a new embedded font format used by the new text engine in Flash Player 10 providing improved typography, international support and reduced font size. The new text engine does not support any of the previous SWF embedded formats such as DefineFont3 generated by Flash CS3 Professional or Flex 3. You will have to use Flash CS4 Professional or Flex Gumbo to embed fonts using the DefineFont4 format.

This means that to use embedded fonts with the TLF, you have to use either the Flex 4 (“Gumbo”) SDK, which is currently still in Beta, or Flash Professional CS4.

Moving to Flex 4 SDK

If you’re currently using the Flex 3 SDK and want to move to Flex 4, there’s a handy tutorial on the Flex Examples blog, Using the beta Gumbo in Flex Builder 3.

A word of warning is that several of the nightly builds and the latest stable build refuse to run on OS X. I wasn’t alone with this problem, both Richard Leggett and Aral Balkan have been bitten too as you’ll see in the comments on Richard’s blog post, Upgrading Flex 3/AIR Projects to Gumbo/Flex 4.

For the time being, I’m using the Adobe MAX preview (build 4.0.0.4021) version.

Embedding fonts

Here’s an example of how to embed a font in AS3, the important new part to notice is the cff="true", which is a reference to the Compact Font Format required by the new text engine.

package com.cameronyule.fonts
{
	import flash.text.Font;
 
	public class AFont extends Font 
	{
		[Embed(source="/path/to/fonts/AFont.ttf", fontFamily="AFont", cff="true")]
		private var _a_font:String;
 
		public static const NAME:String = "AFont";
 
		public function AFont() 
		{
			super();
			_a_font;
		}
	}
}

Using your embedded fonts

There are two main ways to use the Text Layout Framework, TextFlows and TextLines. TextFlows are better for larger blocks of text while TextLines are good for single lines of text.

The most important part is actually the character formatting, so while this example uses TextLines you’ll be able to easily transfer it to TextFlows as well.

private function createTextLine(someText:String):void
{	
	var bounds:Rectangle = new Rectangle(10, 10, this.width, this.height);
 
	var paragraphFormat:ParagraphFormat = new ParagraphFormat();
	paragraphFormat.direction = Direction.LTR;
 
	var characterFormat:CharacterFormat = new CharacterFormat();
	characterFormat.fontSize = 18;
	characterFormat.fontFamily = AFont.NAME;
	characterFormat.fontLookup = flash.text.engine.FontLookup.EMBEDDED_CFF;
	characterFormat.renderingMode = flash.text.engine.RenderingMode.CFF;
 
	TextLineFactory.createTextLinesFromString(addTextLineToContainer, someText, bounds, characterFormat, paragraphFormat);
}
 
private function addTextLineToContainer(textLine:flash.text.engine.TextLine):void
{
	this.addChild(textLine);
}

And there you have it, embedded fonts using the new Text Layout Framework.

Published on February 26, 2009 in Flash, Programming
Tags: , , , , ,

Comments

Join the discussion by leaving a comment, or trackback from your own site. (Comment feed)

Anne Knoche
26/02/2009

It would be VERY helpful if you could write about how someone can mark up text and use subscript and superscript to get the same quality one does when using static text in Flash authoring. I had tried to use various techniques in the past, but always found the subscripts of inferior quality at point sizes of 10 and 12.

Haykel
26/02/2009

Hi Cameron,

thanks for this article. Just wanted to point out that I also written a small article about using embedded fonts with the new Gumbo text primitives. This can be interesting for developers that don’t want to use the TLF directly.

Here is the link:
http://www.allmas-tn.com/2008111710/gumbo-using-embedded-fonts-with-the-new-text-primitives.html

Trackback
27/02/2009

[...] my last post on using embedded fonts with the Text Layout Framework, Anne Knoche left a comment asking how to mark up input text to use subscript and [...]

Trackback
19/03/2009

[...] via Cameronyule [...]

Angel Romero
19/03/2009

Adobe’s Text Layout Framework looks very promising. Oh,so many neat features, so little time. It’s nice to see a post on this topic.

Daniel Wabyick
23/03/2009

Its possible to create a Flex 4 actionscript-only project to embed the fonts, and publish a SWF that gets loaded in by a Flex 3 (or AIR 1.5) application.

A little snippet from the Flex 4 AS project.

[Embed(source='fonts/Arial.ttf',fontName='Arial CFF',cff='true')]
private var ArialClass:Class;

[Embed(source='fonts/Arial Bold.ttf',fontName='Arial CFF',fontWeight='bold',cff='true')]
private var ArialBoldClass:Class;

public function FontEmbed()
{
Font.registerFont( ArialClass );
Font.registerFont( ArialBoldClass );
}

—-

The simply, you load this project in, and using ‘Arial CFF’ as the fontName.

Cameron
26/03/2009

Thanks for the tip Daniel, it’s always good to have more options.

Trackback
17/06/2009

[...] text. It’s worth point out these examples are using embedded fonts. I’ve posted an example of using embedded fonts with the text layout framework [...]

Neil
02/07/2009

Cameron,

I am creating a Hebrew font swf and loading it with the Loader class.

I am assuming that once the cff embed is true, you can’t use the font in a normal textfield or at least thats what happens.

Does this mean I now need to embed fonts just to use in the TLF?

Thanks for the info.

Cameron
13/07/2009

Hi Neil,

That’s correct, CFF embedded fonts aren’t available for instances of TextField, only the Text Layout Framework.

I noticed a while back on Richard Leggett’s blog that he mentioned there were plans to develop a new version of TextField which used the TLF internals, which would mean CFF fonts were supported. I’m unsure if anything came of this though.

Neil
13/07/2009

Hi Cam,

Thank you for the reply, and a great blog. I will keep an eye on the new version of TextField to see if this gets implemented. Would be a shame to have to maintain two types of font.

Trackback
14/09/2009

[...] via Cameronyule [...]

Casey
25/01/2010

If you are getting this compiler error:

transcoding parameter ‘cff’ is not supported by ‘flex2.compiler.media.FontTranscoder’

It’s because cff=”true” is now embedAsCFF=”true”

Leave a comment

Comments are moderated, so be nice!