ActionScript® 3.0 Reference for the Adobe® Flash® Platform
Home  |  Show Packages and Classes List |  Packages  |  Classes  |  What's New  |  Index  |  Appendixes
flashx.textLayout.compose 

ISWFContext  - AS3

Packageflashx.textLayout.compose
Interfacepublic interface ISWFContext
Implementors TextContainerManager

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 10, AIR 1.5

The ISWFContext interface allows a SWF file to share its context with other SWF files that load it. An application that loads a SWF file of type ISWFContext can call methods in the context of the loaded SWF file. The main usage is for calling the FTE TextLine creation methods.

There are two reasons for an application to use this interface to control TextLine creation:

  • Reuse an embedded font: if an application wants to use a font embedded in a loaded SWF file, the application can access the font if a TextLine is created in the context of the loaded SWF file.
  • Reuse existing TextLine instances: reusing existing TextLine instances can result in faster recompose times. TLF reuses existing TextLine instances internally. TLF reuses a TextLine by calling TextBlock.recreateTextLine() instead of TextBlock.createTextLine() when TLF recognizes that a TextLine is extant.

Your application may have additional TextLine instances that can be reused. To manually reuse existing TextLine instances:

  1. trap calls to TextBlock.createTextLine(), then
  2. call TextBlock.recreateTextLine() with the extant TextLine instance instead of TextBlock.createTextLine().

Please note, however, that the TextBlock.recreateTextLine() is available only in Flash Player 10.1 and later.

View the examples

Related API Elements



Public Methods
 MethodDefined By
  
callInContext(fn:Function, thisArg:Object, argArray:Array, returns:Boolean = true):*
A way to call a method in a client controlled context.
ISWFContext
Method Detail

callInContext

()method
public function callInContext(fn:Function, thisArg:Object, argArray:Array, returns:Boolean = true):*

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 10, AIR 1.5

A way to call a method in a client controlled context.

Parameters

fn:Function — The function or method to call
 
thisArg:Object — The this pointer for the function
 
argArray:Array — The arguments for the function
 
returns:Boolean (default = true) — If true, the function returns a value

Returns
* — Whatever the function returns, if anything.

Related API Elements

EmbeddedFontLineCreator.as

The following example shows how to create an ISWFContext implementation that allows other SWF files to access an embedded font.

The EmbeddedFontLineCreator class implements ISWFContext and embeds a font. Other classes can load a SWF file based on EmbeddedFontLineCreator and access the embedded font.


package flashx.textLayout.compose.examples {
    import flash.display.Sprite;
    
    import flashx.textLayout.compose.ISWFContext;
        
    public class EmbeddedFontLineCreator extends Sprite implements ISWFContext
    {
        [Embed( source="C:\\Windows\\Fonts\\BirchStd.otf", fontFamily="embeddedBirchStd", cff="embedAsCFF",
                unicodeRange="U+0041-U+005A, U+0061-U+007A, U+003F")]
        public var embeddedBirchStdFont:Class;
        
        public function callInContext(fn:Function, thisArg:Object, argsArray:Array, returns:Boolean=true):*
        {
            if (returns)
                return fn.apply(thisArg, argsArray);
            fn.apply(thisArg, argsArray);
        }
    }
}

FontConsumer.as

The following example shows how to create a class that can access a font embedded in another SWF file. The FontConsumer class loads a SWF file that implements the ISWFContext interface. Specifically, this examples loads an instance of EmbeddedFontLineCreator, an example class that implements ISWFContext.

The FontConsumer class first loads the EmbeddedFontLineCreator.swf file and waits for the load function to succeed. Once the load succeeds, the event listener function createFlow() creates a text container and a text flow. The event listener then creates a flow composer and associates the loaded SWF file with the flow composer's swfContext property. This association allows a FontConsumer instance to call methods within the context of the loaded SWF file. With access to the context of EmbeddedFontLineCreator, the FontConsumer instance can use the font embedded in EmbeddedFontLineCreator.

package flashx.textLayout.compose.examples {
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.text.engine.FontLookup;
    
    import flashx.textLayout.compose.StandardFlowComposer;
    import flashx.textLayout.container.ContainerController;
    import flashx.textLayout.conversion.TextConverter;
    import flashx.textLayout.elements.Configuration;
    import flashx.textLayout.elements.TextFlow;
    import flashx.textLayout.formats.TextLayoutFormat;
    
    public class FontConsumer extends Sprite
    {
        private var fontSWF:Loader = new Loader();    
        
        public function FontConsumer()
        {
            var fontSWFURL:URLRequest = new URLRequest("EmbeddedFontLineCreator.swf");
            fontSWF.contentLoaderInfo.addEventListener( Event.COMPLETE, createFlow );
            fontSWF.load( fontSWFURL );    
        }
        
        private function createFlow( event:Event ):void
        {
            var container:Sprite = new Sprite();
            this.addChild( container );
            var controller:ContainerController = new ContainerController( container, 600, 700 );
            
            var format:TextLayoutFormat = new TextLayoutFormat();
            format.fontFamily = "embeddedBirchStd";
            format.fontLookup = FontLookup.EMBEDDED_CFF;
            
            var config:Configuration = new Configuration();
            config.textFlowInitialFormat = format;
            
            var flow:TextFlow = TextConverter.importToFlow( "Shall I compare thee to a summer's day?", TextConverter.PLAIN_TEXT_FORMAT, config );
            flow.flowComposer = new StandardFlowComposer();
            
            var embeddedFontLineCreator:Class = fontSWF.contentLoaderInfo.applicationDomain.getDefinition( "flashx.textLayout.compose.examples.EmbeddedFontLineCreator" ) as Class;
            flow.flowComposer.swfContext = new embeddedFontLineCreator();
            
            flow.flowComposer.addController( controller );
            flow.flowComposer.updateAllControllers();
        }
    }
}