Embedding and using external fonts.
Copy link to clipboard
Copied
I'm using Flash CS4. I'm trying to load and embed an external font. The main problem I'm having is that I need to import more than just latin 1. I need many special characters imported as well. Because of this, using Flash CS4's create new font from the library is not an option. I'm using Verdana, so I know the font has all the characters I need.
So what I need to do is create an external swf that holds the font. Then I need to import and use the font. Unfortunately, I've found way too many ways online that work but don't fit my needs.
Any help is appreciated.
Copy link to clipboard
Copied
What are you the needs that solutions you found do not fulfill?
Do you mean "load" at runtime when you say "import"?
Copy link to clipboard
Copied
Correct. I need to load the font at runtime. I also need the ability to specify which Unicodes to use. I guess my frustration with the matter is, I can't find complete solutions anywhere online. I keep finding bits and pieces of different ways of doing this.
This is what I need to do:
1. On runtime, load the external swf file that contains an embedded font.
2. Once the swf is loaded, apply the embedded font to a dynamic text box.
This is what I need to find out:
1. How do I embed a font into a swf that allows me to specify which unicodes to use.
2. I know how to import the swf on runtime.
3. Once the swf is imported, what do you do to start using the font? How and what do you reference from the imported swf to use the font?
Any HELP to clarify this is VERY appreciated.
Copy link to clipboard
Copied
1. Embedding
a. You can embed font in Flash IDE by creating Font as a library asset and then compile this swf. There are tons of help available on Internet.
b. If you use Flash CS5 or compilers that utilizes Flex SDK, you can create class like below and compile it into a swf:
package
{
import flash.display.Sprite;
public class FontLibrary extends Sprite
{
[Embed(systemFont="Verdana", fontName="_verdana", mimeType="application/x-font", advancedAntiAliasing="true", fontStyle="normal",unicodeRange="U+0020-U+007E")]
public static var Normal:Class;
[Embed(systemFont="Verdana", fontName="_verdana", mimeType="application/x-font", advancedAntiAliasing="true", fontStyle="normal", fontWeight="bold",unicodeRange="U+0020-U+007E")]
public static var Bold:Class;
[Embed(systemFont="Verdana", fontName="_verdana", mimeType="application/x-font", advancedAntiAliasing="true", fontStyle="italic", fontWeight="normal",unicodeRange="U+0020-U+007E")]
public static var Italic:Class;
[Embed(systemFont="Verdana", fontName="_verdana", mimeType="application/x-font", advancedAntiAliasing="true", fontStyle="italic", fontWeight="bold",unicodeRange="U+0020-U+007E")]
public static var ItalicBold:Class;
}
}
No matter what way you choose - you will have font available the external swf.
Try to load external swf into current domain. When you load the swf there is no much to do. Once swf is loaded - fonts should be available throughout application.
To use the font - just pass its name into TextFiled instance's TextFormat. WIth the example above:
myTextFormat.font = "_verdana";
Documentation for TextFormat:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/TextFormat.html
To see available fonts you can use the following:
var allFonts:Array = Font.enumerateFonts(false);
allFonts.sortOn("fontName", Array.CASEINSENSITIVE);
for (var i:int = 0; i < allFonts.length; i++)
{
trace(allFonts.fontName);
}
Copy link to clipboard
Copied
OK, this is what I've gathered from you and other sources. Is this how it's supposed to be?
///////////////////////////////////////////////////////////////Within font swf
package
{
import flash.display.Sprite;
public class FontLibrary extends Sprite
{
[Embed(systemFont="Verdana", fontName="_verdana", mimeType="application/x-font", advancedAntiAliasing="true", fontStyle="normal",unicodeRange="U+0020-U+007E")]
public static var Normal:Class;
}
}
///////////////////////////////////////////////////////////////Within Main
function loadFont()
{
fontPath = "fonts/Verdana.ttf";
var fontLoader:Loader = new Loader();
fontLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFontLoaded);
fontLoader.load(new URLRequest(fontPath));
};
function onFontLoaded(e:Event)
{
var FontLib:Class = e.target.applicationDomain.getDefinition("Verdana") as Class;
Font.registerFont(FontLib);
runText();
};
function runText()
{
var myTextFormat:TextFormat = new TextFormat;
myTextFormat.font = "_verdana";
var textF:TextField = new TextField;
textF.setTextFormat(myTextFormat);
addChild(textF);
};
Copy link to clipboard
Copied
First:
var FontLib:Class = e.target.applicationDomain.getDefinition("Verdana") as Class;
Should be something lie this:
var FontLib:Class = e.target.applicationDomain.getDefinition("Normal") as Class;
Also, you may not need to even do that if you load fonts into current domain:
fontLoader.load(new URLRequest(fontPath), ApplicationDomain.currentDomain);

