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().

Load and use runtime shared libraries trough actionscript in Flash CS4 and CS5

This post describes how to load a runtime shared library (RSL) via actionscript but still be able to use it as a normal RSL within CS4 and CS5. The idea is that instead of letting the flash player automatically load the RSL (whenever it is accessed for the first time), the RSL is loaded with a Loader object via actionscript. The allows the programmer to pick the correct time to load it. Also the loading progress feedback can be shown to the user.

First I describe the various experiments I did; at the end I give a summary of the method I used.

As a start I created two flash files:

  1. The first file is the RSL (called test.export.picture.fla). I placed a png image inside a MovieClip (to get an asset with a nice size in bytes). Then I checked the Export for runtime sharing in the MovieClip properties, which automatically also checks the export and export in frame 1 checkboxes. As URL I used the swf filename (test.export.picture.swf)
  2. In the second file (called test.import.picture.fla) I extended the main timeline to be 10 frames. At frame 5 I placed a key frame and via copy and paste I placed a copy of the MovieClip from the first fla file. This will automatically set the Import for runtime sharing checkbox and fills the URL with the value that was entered at the first file.

After building both flash files, everything was working as expected. Then I tested the 2nd file with the bandwidth profiler turned on (ctrl+B) and with the Simulate Download (pressing ctrl+Enter a second time while viewing the movie). This showed that at frame 5 the RSL was being loaded.

As second part of the experiment I added some actionscript code to the 2nd flash file. At the 2nd frame I added code that loaded the RSL swf file directly using a Loader object. The scriptcode also stopped the movie and continue playing it after the RSL file was completly loaded:

var loader: Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
loader.load(new URLRequest('test.export.picture.swf'));
 
function onComplete(anEvent: Event): void
{
	trace('loaded');
	play();
}
 
function onError(anEvent: IOErrorEvent): void
{
	trace('error');
}

Again I tested the movie with the bandwidth profiler and simulating the download. As expected at frame 2 the RSL file was loaded. But at frame 5 the same file was loaded again. This is not really what I had hoped for. Although there is probably almost no delay the second time the RSL file is loaded because of (memory) caching, one can never be sure. Also it is not clear if this means the RSL file uses 2x the memory.

Next step I changed the actionscript so that the RSL was loaded into the same context as the main SWF:

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);
...

After testing with the bandwidth profiler, nothing seemed to have changed. However I was pretty sure the MovieClip class defined in the RSL file was now available in the main movies context. So as last test I created an empty swf file (by starting a new as3 project without anything in it) and changed the import settings so the URL was pointing to this empty swf file. I tested the movie and it was working (yay!). At frame 5 the small swf got loaded, but since the MovieClip class definition was already available, the flash player still managed to create the MovieClip instance and show it on the main timeline.

Summary
To load and use RSL files via actionscript perform the following steps:

  1. Create an empty Flash file and compile it into a SWF file. This file will act as a dummy file, used by the player when it has to load a RSL file.
  2. Create a Flash file to be used as RSL and add assets normally. When enabling Export for runtime put the name of the dummy swf file at the URL. It seems the url entered here is only used to fill in the URL when creating the imported version of the asset (trough copy and paste).
  3. Copy and paste assets to other library and use them normally.
  4. Make sure the RSL file is loaded completely before using any of the assets from it. Load it within the context of the main movie using something like:
    var loader: Loader = new Loader();
    var context: LoaderContext = new LoaderContext();
    context.applicationDomain = ApplicationDomain.currentDomain; 
    loader.load(new URLRequest(...), context);
  5. If everything works ok, the asset should be used the flash player needing it to load the RSL itself.

Note: instead of using a dummy swf file, one can also use the filename of the SWF that will be initially loaded. It seems flash will not try to load it again. This is ofcourse only usable if a RSL is not used with multiple projects.

To use the above also with shared fonts, some extra work is required. This I will describe in my next post.

Embedding additional font outlines in a runtime shared library (RSL) with Flash CS4

Note that with CS5 this is no longer an issue; since you can specify exactly which font outline groups to embed with the Font Embedding dialog.

However with CS4 it is a different story.

At the end of this blog post I describe a solution how to include additional font outlines. First I describe the experiments I did, in case readers want to recreated those themselves.

As a test I created two flash files:

  • a flash file with a Font definition in the library with export for runtime sharing checked (based upon Arial – Regular)
  • a flash file with a Font definition in the library with import for runtime sharing checked and referencing the first file. I also added a dynamic text field containing some normal text and text using Cyrillic font (Eastern Europe).

The normal text showed up, but not the text with Cyrillic font. Looking at the generated size report it was also clear the Cyrillic outlines did not get exported.

So as next step I placed a dynamic text field in the main timeline of the first runtime sharing fla using the Font definition in the library. I selected the Cyrillic font via Character Embedding… outlines But nothing changed after creating the swf.

I tried various things, but in all cases the Font definition seemed to override the font embed options of the text field. At the end I found a solution (yay!), which I’ll describe next.

Solution
To embed additional (you can not override the default embedded outlines) for a Font definition in CS4 do the following:

  1. create a new Font definition in the library
  2. place a new text field on the main time-line (or use a movie clip, but in that case the movie clip must be exported as well)
  3. set type of text field to Dynamic Text
  4. as Family select the created font definition (make sure it is the font definition in the library and not the font used by the definition)
  5. click Character Embedding… and select the outlines you want to embed as well (no need to select the default ones)
  6. now for the trick to get it working: after this change the text type to Static Text
  7. build the swf file; the swf file should now also contain the additional font outlines