Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 2 of 2

Thread: swf assets, loading fonts in Actionscript using FlashDevelop (AS3 project)

  1. #1

    swf assets, loading fonts in Actionscript using FlashDevelop (AS3 project)

    I know this has been posted and discussed over and over again, but whatever solution i use (and there are generally two types), none work me. Even worse, all those examples were from as early as 2007 !

    Now to the problem. I use FlashDevelop 4.0.0 RC1 and Flex SDK 4.5.1 (downloaded during FD install).

    This is a font asset swf that holds my font to be loaded later in separate swf project in FD4. Example 1:

    Code:
    package
     {
     import flash.display.Sprite;
    
     public class ExtFontLib extends Sprite
      {
      [Embed(source="../lib/6809CHAR.TTF", fontName='6809 Chargen', mimeType="application/x-font")]
      public static var MyFont : Class;
      }
     }
    Other method is slightly different. Example 2

    Code:
    package
     {
     import flash.text.Font; 
    
     [Embed(systemFont='Bank Gothic', fontName='emBank Gothic', mimeType='application/x-font')] 
     public class BankGothicNormalFont extends Font {}
     }
    And now test app:

    Code:
    package
     {
     import flash.display.Loader;
     import flash.display.Sprite;
     import flash.events.Event;
     import flash.net.URLRequest;
     import flash.text.*;
     import mx.logging.*;
    
    
     public class TestFont extends Sprite
      {
    
      public function TestFont () : void 
       {
       if (stage) init ();
       else addEventListener (Event.ADDED_TO_STAGE, init);
       }
      private function init (e : Event = null) : void 
       {
       removeEventListener (Event.ADDED_TO_STAGE, init);
       BeginProgram (); // entry point
       }
      private function BeginProgram () : void
       {
       loadFont ("http://127.0.0.1/ExtFontLib.swf");
       }
      private function loadFont (url : String) : void
       {
       var loader : Loader = new Loader ();
       loader.contentLoaderInfo.addEventListener (Event.COMPLETE, completeHandler);
       loader.load (new URLRequest (url));
       }
      private function completeHandler (event : Event) : void
       {
       var LoadedFontLibrary : Class = event.target.applicationDomain.getDefinition  ("ExtFontLib") as Class;
       Font.registerFont (LoadedFontLibrary.MyFont);  //PROBLEM SPOT
       test ();
       }
      public function test () : void
       {
       var tf : TextField = new TextField ();
       tf.embedFonts = true;
       tf.antiAliasType = AntiAliasType.ADVANCED;
       tf.autoSize = TextFieldAutoSize.LEFT;
       tf.border = true;
       tf.borderColor = 0xff7777;
       tf.textColor = 0x000000;
       tf.x = 100;
       tf.y = 100;
       tf.text = "testing\n1234567890\nanotherline";
       tf.setTextFormat (new TextFormat ("6809 Chargen", 16, 0));
       tf.rotation = 15;
       stage.addChild (tf);
       }
      }
     }
    I used source from example 1 and tried loading it in test app and got error

    Code:
    [Fault] exception, information=ArgumentError: Error #1508: The value specified for argument font is invalid.
    It fails at line with "Font.registerFont (LoadedFontLibrary.MyFont)".

    If i try with example 2, i am loading with above line modified to "Font.registerFont (LoadedFontLibrary)" and get error

    Code:
    [Fault] exception, information=TypeError: Error #2023: Class ExtFontLib$ must inherit from Sprite to link to the root.
    So in both cases it fails. Any ideas ?

  2. #2

    Current resolution to asset loading problem

    After some googling around i've made first working example if anyone is (still) interested.

    There are two FD projects to be used for font resource swf and test application.

    Font SWF project source example project (note "MYFONT.TTF" file is added to project's library prior to compiling)

    "MyFontLibrary.as"
    Code:
    package
     {
      import flash.display.Sprite;
      import flash.text.Font;
      public class MyFontLibrary extends Sprite
       {
       // embedAsCFF is false, if i don't use it FD build fails with transcoding error, found on net it's a flash version 10's quirk 
       [Embed(source = "../lib/MYFONT.TTF", fontName = "MyFont", mimeType = "application/x-font", embedAsCFF = "false")]
        private var MyFont : Class;
        public function MyFontLibrary () : void
         {
           Font.registerFont (MyFont); // this SWF's constructor will run when SWF is loaded in test application
         }
       }
     }
    This is separate test application project.

    "loadFontDemo.as"
    Code:
    package
     {
     import flash.display.Loader;
     import flash.display.MovieClip;
     import flash.display.Sprite;
     import flash.events.Event;
     import flash.net.URLRequest;
     import flash.text.TextField;
     import flash.text.TextFieldAutoSize;
     import flash.text.AntiAliasType;
     import flash.text.Font;
     import flash.text.TextFormat;
    
     public class loadFontDemo extends Sprite
      {
      public function loadFontDemo () : void
       {
        if (stage) init ();
        else addEventListener (Event.ADDED_TO_STAGE, init);
       }
       private function init (e : Event = null) : void 
        {
        removeEventListener (Event.ADDED_TO_STAGE, init);
        BeginProgram (); // entry point
        }
       private var extloader : Loader;
       public function BeginProgram () : void
        {
        extloader = new Loader ();
        extloader.contentLoaderInfo.addEventListener (Event.COMPLETE, completeHandler);
        /* if i feed "http://127.0.0.1/MyFontLibrary.swf" to loader, i can't later access content */
        extloader.load (new URLRequest ("MyFontLibrary.swf"));
        }
       private function completeHandler (event : Event) : void
        {
        extloader.removeEventListener (Event.COMPLETE, completeHandler);
        test ();
        }
       public function test () : void
        {
        var tf : TextField = new TextField ();
        tf.embedFonts = true;
        tf.antiAliasType = AntiAliasType.ADVANCED;
        tf.autoSize = TextFieldAutoSize.LEFT;
        tf.border = true;
        tf.text = "blah blah blah\nblah blah blah\nblah blah blah ";
        tf.x = 50;
        tf.y = 50;
        tf.border = true;
        tf.borderColor = 0xff00ff;
        tf.textColor = 0x000000;
        tf.setTextFormat (new TextFormat ("MyFont", 26, 0)); //note the name MyFont used !
        tf.rotation = 15; //this ensures that font embedding and use from external SWF worked
        stage.addChild (tf);
        }
      }
     }
    Before i reached this solution i was unaware of adobe's security model which was messing up my attempts to load and ACCESS content in separate SWF from a small web server on same computer.

    If i set up URLRequest to download "MyFontLibrary.swf" without path all works fine, even if i set up web server on another machine (so it's not on localhost).

    However, if i explicitly define URL as "http://127.0.0.1/MyFontLibrary.swf", it WILL fail.

    I know this has something to do with security policy and needs to be defined in "crossdomain.xml" file and loaded from server, but i haven't done enough searching and testing to find a working solution.

    It stumped me that running SWFs off localhost is more hassle than from some remote machine.

    And a reason why i want to define exact URL to file ? Well, i had used a finished SWF banner that failed to load it's XML config unless it was located at site root, too messy for my taste. So i want to define all URLs exactly and (to repeat myself) while it works on remote machines, it fails if used on localhost web server where most of my testing is done.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012