Mario,
Here's how I do it, relevant code contained for the list archives and
those that also run into this problem. I changed the title of the
response so that it can be flagged as a solution to the problem (is
there a standard practice for solutions in this list?).
1. Create a separate Flex project for the Font Library. The end
result of this project (an Actionscript-only project) is to create
each font as an SWF file.
a. Create a Font class that all your fonts will extend. I do this so
that additional properties may be added to the fonts themselves that
you can use during your runtime application. Things like a
"displayName" so that you can logically display the name of the font
however you want in a ComboBox for examples.
package
{
import flash.display.Sprite;
import flash.text.Font;
public class MyFontBase extends Sprite
{
public var registeredFontName:String;
public var displayName:String;
public function MyFontBase():void { }
}
}
b. Create the classes with the actual embedded font. Note that
depending on what type of font you need, you will probably run into
compiler issues. Some system fonts just fail to compile. After you've
created all your AS files for the fonts, make sure to go to your Flex
project properties and set all of these files as runnable
applications. Each will get compiled to an SWF that you load in at
runtime in your other project.
package {
import flash.display.Sprite;
import flash.text.Font;
import MyFontBase;
public class ArialFontLib extends MyFontBase {
[Embed(source='/fonts/arial.ttf', fontName='ArialRegular',
mimeType='application/x-font')]
public static var ArialRegularFont:Class;
/** Register the font directly as soon as this class is initialized
*/
public function ArialFontLib():void {
registeredFontName = "ArialRegular";
displayName = "Arial";
Font.registerFont(ArialFontLib.ArialRegularFont);
}
}
}
c. Notice that the font classes register themselves against the Flex
Font class, instead of you doing it separately somewhere else down
the pipeline.
2. In your new Flex Project that will load these library files,
create a class that loads up these SWF files and broadcasts when each
of them has completed loading. Might want to add in a broadcast that
says when all of them are done loading as well. I can't share my code
for this because my FontManager class is pretty in depth and does a
bunch of other stuff. It's the standard loading of content though.
Here's the final dispatch so you can see how it ties in with the
function that applies the styles:
// Dispatch event that the font loaded
dispatchEvent(new FontManagerEvent(FontManagerEvent.FONT_LOADED,
false, false, Sprite(event.target.loader.content) ) );
a. Create a function in your new Flex project that listens for the
fonts that load and when each one completes. This function applies a
new CSSStyleDeclaration to that loaded font and makes it available.
Finally, make sure the StyleManager creates this new style declaration.
/** Apply the stylesheet globally for this loaded font */
private function onFontLoaded(event:FontManagerEvent) : void {
var fontName:String = MyFontBase
( event.loadedFont ).registeredFontName;
var style:CSSStyleDeclaration = new CSSStyleDeclaration();
style.setStyle('fontSize', 12);
style.setStyle('fontFamily', fontName);
StyleManager.setStyleDeclaration("." + fontName, style, true);
}
b. Now the font is registered in the StyleManager as a new style
declaration. You can use this on any of the Flex controls in your
application. You can also use this in custom components that are
created after you've loaded the fonts (like a custom UITextField
display). Note that you may need to add:
regenerateStyleCache(true);
within the constructor of your custom UIComponent so that the styles
that were loaded at runtime are going to propagate to your custom
component.
I did not include here the other implementation that I use with
FlashType (fonts exported from the Flash IDE). The process is very
similar but you need to take special care that the fontAntiAliasType
style declaration is set properly or you'll get runtime errors.
There are also ways of managing the various weights of the font
within a single class. You can look that up online and extrapolate
those implementations to the process I've detailed above.
good luck,
jon