Skip to main content
Participant
February 16, 2011
Question

Possible to use embedded font in TLFTextField, with TextFormat?

  • February 16, 2011
  • 1 reply
  • 4572 views

I want to be able to dynamically change the font in a TLFTextField to an embedded font (not a device font).  I'm using Flash CS5.

The only way I've found a TLFTextField will use an embedded font is if the on-stage TLFTextField has been set to use that font through the Flash UI.  The example code I've found online (Font.registerFont etc) all works fine for a TextField, but not TLFTextField.

Below is a very simple piece of code that I expected to work.  I have a font (Earwig Factory) embedded in my library (as DF4, linkage "Earwig"), and the isFontCompatible trace call prints "true", but still it is using the device font - if I remove the system font from my machine (or view the SWF on another machine) it falls back to Times New Roman.

Can anyone give a very simple example of a TLFTextField dynamically selecting an embedded font?  Eventually I would like to store my fonts in a separate SWC / SWF, but for now just working out of the Library would be a good start.

Thanks,
Grant

import flash.text.TextFormat;

import flash.text.AntiAliasType;

import flash.text.FontStyle;

import fl.text.TLFTextField;

import flash.text.engine.FontLookup;

var format:TextFormat = new TextFormat();

format.font = "Earwig Factory"

trace("Earwig Factory isFontCompatible: " + tf.isFontCompatible("Earwig Factory", FontStyle.REGULAR) );

tf.defaultTextFormat = format;

tf.embedFonts = true;

//tf.antiAliasType = AntiAliasType.ADVANCED;

tf.setTextFormat(format);

//tf.textFlow.fontLookup = FontLookup.EMBEDDED_CFF;

This topic has been closed for replies.

1 reply

Participating Frequently
February 17, 2011

Howdy Grant,

Sometimes the names of the fonts change when they are embedded in the SWF. It is recommended that you instantiate a version of your font and use the name that it returns to you to set the font name of the format. I have modified your code below.

Let me know if it is still not working for you.

Craig Simmons

Flash Professional Team

import flash.text.TextFormat;

import flash.text.AntiAliasType;

import flash.text.FontStyle;

import flash.text.Font;

import fl.text.TLFTextField;

import flash.text.engine.FontLookup;

var format:TextFormat = new TextFormat();

var fntEarwig:Font = new Earwig();

format.font = fntEarwig.fontName;

trace("Earwig Factory isFontCompatible: " + tf.isFontCompatible( fntEarwig.fontName, FontStyle.REGULAR) );

tf.defaultTextFormat = format;

tf.embedFonts = true;

tf.text = "Test";

//tf.antiAliasType = AntiAliasType.ADVANCED;

tf.setTextFormat(format);

//tf.textFlow.fontLookup = FontLookup.EMBEDDED_CFF;

grantcoxAuthor
Participant
February 18, 2011

Unfortunately that doesn't help (and in this case the fntEarwig.fontName traces the same regardless) - the font displays while I have the system font installed, but testing the SWF without the system font just shows Times New Roman.

The weird thing is, it traces "Earwig Factory isFontCompatible: true", even when the system font is not installed and it's displaying Times.  So however the "isFontCompatible" works, it is finding the embedded font.  But actually using it...

grantcoxAuthor
Participant
February 18, 2011

Ok, I have some success - I can get an on-stage TLFTextField to dynamically use an embedded font.  However, this is quite complex, and doesn't use a TextFormat.  And is this seriously the way Adobe intends us to do it - if so perhaps this should be documented better?

import fl.text.TLFTextField;

import flashx.textLayout.formats.TextLayoutFormat;

import flashx.textLayout.elements.TextFlow;

import flash.text.TextFormat;

import flash.text.AntiAliasType;

import flash.text.FontStyle;

import flash.text.Font;

import flash.text.engine.FontLookup;

import flashx.textLayout.edit.ISelectionManager;

import flashx.textLayout.edit.EditManager;

import flashx.textLayout.edit.SelectionState;

var updateTF:TLFTextField = stageTF;

var applyFont:Font = new Earwig();

updateTF.embedFonts = true;

var tlfFormat:TextLayoutFormat = new TextLayoutFormat();

tlfFormat.fontFamily = applyFont.fontName;

tlfFormat.fontLookup = FontLookup.EMBEDDED_CFF;

updateTF.textFlow.invalidateAllFormats();

updateTF.textFlow.hostFormat = tlfFormat;

var prevManager:ISelectionManager = updateTF.textFlow.interactionManager;

var editManager:EditManager = new EditManager();

var sel:SelectionState = new SelectionState(updateTF.textFlow, 0, updateTF.text.length);

updateTF.textFlow.interactionManager = editManager;

editManager.applyLeafFormat(tlfFormat, sel);

updateTF.textFlow.interactionManager = prevManager;

updateTF.textFlow.flowComposer.updateAllControllers();