Using fonts inside a runtime shared library (RSL) loaded trough actionscript in Flash CS4 and CS5

This post describes how to use fonts which are stored in a runtime shared library (RSL) that is loaded trough actionscript. The post continues from the previous post I made about loading RSL via actionscript.

As a start I created two flash files very similar to the other article:

  1. The first file acts as the RSL file. I added Font definition to the library; using Arial Regular. I checked the Export for runtime sharing, used ArialRegular as class name and entered for the URL the name of an empty SWF.
  2. Into a second file I copied and pasted the Font definition from the first file (which automatically turned on Import for runtime sharing. I extended the timeline to 10 frames, created a new keyframe at frame 5 and placed at that frame a dynamic text field with some text using the font from the library. I also placed stop() at frame 5.
    At frame 2 I added the following code:
    this.stop();
    var loader: Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
    var context: LoaderContext = new LoaderContext();
    context.applicationDomain = ApplicationDomain.currentDomain; 
    loader.load(new URLRequest('test.export.picture.swf'), context);
     
    function onComplete(anEvent: Event): void
    {
    	trace('loaded');
    	play();
    }
     
    function onError(anEvent: IOErrorEvent): void
    {
    	trace('error');
    }

    This code will load the RSL and then continues the movie.

As shown in the other post, the Font definition will be present in the main movies context. However after testing the movie, I did not see any text.
It turns out having the Font definition in the same context is not enough, the definition has also to be registered trough the static method Font.registerFont().

So I changed the code at frame 2 to:

...
function onComplete(anEvent: Event): void
{
	trace('loaded');
	Font.registerFont(ApplicationDomain.currentDomain.getDefinition('ArialRegular') as Class);
	play();
}
...

Now the text was showing up when testing the movie.

Summary
To use Font Definition stored into a RSL, it needs to be registered (after the RSL has been loaded into the same domain) trough the method Font.Font.registerFont().
To obtain the definition use the method ApplicationDomain.currentDomain.getDefinition().