Skip to main content
Frédéric C.
Inspiring
October 6, 2017
Question

Can't register a font loaded externally

  • October 6, 2017
  • 1 reply
  • 442 views

Hi,

My app is downloading SWF assets from the web, and stores them locally in the ApplicationStorage directory. Some of them contain embedded fonts. If I load the SWF from ApplicationStorage, I get the following error when I try to register the font class it contains:

Error #1508: The value specified for argument font is invalid.

If I place the same SWF in the local directory of my app, all works fine. I did some research and it seems to be related to some security issues. I tried to use a LoaderContext to change the security settings, but it doesn't work.

If I just use:

var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);

I get the error #1508 above.

If I use:

var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, SecurityDomain.currentDomain);

I get the following error:

Error #2142: Security sandbox violation: local SWF files cannot use the LoaderContext.securityDomain property.

I'm out of ideas. Here is the full code below. Can you help, please? Thank you very much!

package

{

    import flash.display.Loader;

    import flash.display.MovieClip;

    import flash.events.Event;

    import flash.events.IOErrorEvent;

    import flash.filesystem.File;

    import flash.net.URLRequest;

    import flash.system.ApplicationDomain;

    import flash.system.LoaderContext;

    import flash.system.SecurityDomain;

    import flash.text.Font;

    import flash.text.TextField;

    import flash.text.TextFieldAutoSize;

    import flash.text.TextFormat;

    public class Test extends MovieClip {

     

        public static const FONT_NAME:String = "Poiret One";

        public static const FONT_EXPORT_NAME:String = "PoiretOne";

        public static const FONT_FILE_NAME:String = File.applicationStorageDirectory.resolvePath("Assets/Fonts.swf").url;

         

        function Test():void {

            var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);

            loaderContext.allowLoadBytesCodeExecution = true;

            var loader:Loader = new Loader();

            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFontLoaded);

            loader.addEventListener(IOErrorEvent.IO_ERROR, onFontLoadingFailed);

            loader.addEventListener(IOErrorEvent.NETWORK_ERROR, onFontLoadingFailed);

            loader.addEventListener(IOErrorEvent.VERIFY_ERROR, onFontLoadingFailed);

            loader.addEventListener(IOErrorEvent.DISK_ERROR, onFontLoadingFailed);

            loader.load(new URLRequest(FONT_FILE_NAME), loaderContext);

         

        }

        private function onFontLoaded(e:Event):void {

            trace("Successfully loaded " + FONT_EXPORT_NAME + " (" + FONT_FILE_NAME + ")");

            if(e.target.applicationDomain.hasDefinition(FONT_EXPORT_NAME)) {

                var FontClass:Class = e.target.applicationDomain.getDefinition(FONT_EXPORT_NAME) as Class; 

                trace ((new FontClass).fontName);

                Font.registerFont(FontClass);

                addTextField();

            } else {

                trace("Missing " + FONT_EXPORT_NAME + "!");

            }

        }

        private function onFontLoadingFailed(e:IOErrorEvent):void {         

            trace("Missing " + FONT_FILE_NAME);

        }

        private function addTextField():void {

            trace("Adding text field ...");

         

            var format:TextFormat = new TextFormat();

            format.font = FONT_NAME;

            format.size = 45;

            format.color = 0x9f0000;

            format.bold = true;

         

            var field:TextField = new TextField();

            field.defaultTextFormat = format;

            field.text = "Embed font at runtime.";

            field.x = field.y = 35;

            field.embedFonts = true;

            field.autoSize = TextFieldAutoSize.LEFT;

         

            addChild(field);

         

        }

    }

}

This topic has been closed for replies.

1 reply

Frédéric C.
Inspiring
October 12, 2017

Do you have any idea please? I suppose there is a solution for this, right? Thank you!