3 Replies Latest reply on Jan 25, 2011 2:51 PM by phi2265

    Accessing a non TTF/OTF font embedded in Flash CS5 SWF via Flex

    phi2265 Level 1

      Hello,

       

      I'm trying to embed an older Mac font in a Flash Builder application. I need it to be loaded dymanically for size reasons. I am able to do this with TTF and OTF fonts, but I'm having trouble with other formats.

       

      I embedded the font in a CS5 FLA, but I can't seem to locate the font when I load the SWF into Flash Builder.

       

      My function is below. I have it enumerating through the variables in the loaded SWF, but the font I need is not coming up as one of them. I tried, in the FLA, to cast the embedded font to a Class in AS because, but when try to access that variable ("font") after it's loaded, I get null. I also tried setting the embedded font to a Font variable in AS, but I still get null on the other side. Does anyone have any suggestions? Alternate solutions are welcome also.. Thanks!!

       

              private function loadFonts():void{
                  var lc:LoaderContext = new LoaderContext();
                  lc.securityDomain = SecurityDomain.currentDomain;
                  lc.applicationDomain = ApplicationDomain.currentDomain;
                  var ldr:Loader = new Loader();
                  ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
                  ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                  ldr.load(new URLRequest('compatil_bold.swf'),lc);
                 
                  function completeHandler(e:Event):void{
                      fontLibrary = e.target.applicationDomain.getDefinition('Compatil') as Class;
                      var varList:XMLList = flash.utils.describeType(fontLibrary)..variable;
                      for (var i:int = 0; i < varList.length(); i++){
                          trace(varList[i].@name+': '+fontLibrary[varList[i].@name]);   
                      }
                      Font.registerFont(fontLibrary.font);
                      //Font.registerFont(fontLibrary.CompatilFactBold);
                      trace('font loaded');
                      fontsLoaded = true;
                  }
                 
                  function ioErrorHandler(e:ErrorEvent):void{
                      trace(e.text);
                  }
              }

       

      in FLA

       

      var font:Class = new CompatilFactBold() as Class;

        • 1. Re: Accessing a non TTF/OTF font embedded in Flash CS5 SWF via Flex
          Flex harUI Adobe Employee

          In the FLA, it looks like the font is called "CompatilFactBold", but you are

          using 'Compatil' in the getDefinition call.

           

          In Flex, you rarely need to access a Class that represents the font.  I'm

          not sure if the SWF is registering the font or not or what the fontFamily

          is, but that is probably more important to Flex.

           

          See: http://blogs.adobe.com/aharui/2010/03/flex_and_embedded_fonts.html

          • 2. Re: Accessing a non TTF/OTF font embedded in Flash CS5 SWF via Flex
            phi2265 Level 1

            You're correct - in the FLA I named the font class "CompatilFactBold." In the document properties (FLA) I named the class Compatil. So I was thinking CompatilFactBold should be a child of Compatil. Is that incorrect? I thought I could reference it as if I were referencing a font embedded in this manor:

            package{

            import flash.display.Sprite;

             

            public class FontLib1 extends Sprite{

            [Embed(source='fonts/ACaslonPro-Regular.otf', fontFamily='caslon', mimeType='application/x-font', embedAsCFF='false', unicodeRange='U+0030-U+003F,U+0041-U+0051,U+0052-U+007A,U+0021-U+002E')]

            public static var CaslonFont:Class;

            [Embed(source='fonts/ACaslonPro-Italic.otf', fontFamily='caslonItal', mimeType='application/x-font', embedAsCFF='false', unicodeRange='U+0030-U+003F,U+0041-U+0051,U+0052-U+007A,U+0021-U+002E')]

            public static var CaslonItalicFont:Class;

            }

            }

             

             

            I should also mention that my project in Flash Builder is AS only. I don't think I have access to all of the components mentioned in the blog post you pointed me to.

             

            Also, am I able to use the font in the context of the main SWF without calling Font.registerFont()?

             

            Message was edited by: phi2265

            • 3. Re: Accessing a non TTF/OTF font embedded in Flash CS5 SWF via Flex
              phi2265 Level 1

              I got it!

               

              Here is the embedded font in a CS5 FLA:

              Picture 33.png

               

              That's all you have to do in Flash.

               

              Here's the function I have in my Flash Builder AS application:

               

              private function loadFonts():void{

              var lc:LoaderContext = new LoaderContext();

              lc.securityDomain = SecurityDomain.currentDomain;

              lc.applicationDomain = ApplicationDomain.currentDomain;

              var ldr:Loader = new Loader();

              ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);

              ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

              ldr.load(new URLRequest('http://some.other.domain/compatil_bold.swf'),lc);

               

              function completeHandler(e:Event):void{

              fontLibrary = e.target.applicationDomain.getDefinition('CompatilFactBold') as Class;

              Font.registerFont(fontLibrary);

              trace('font loaded');

              fontsLoaded = true;

              }

               

              function ioErrorHandler(e:ErrorEvent):void{

              trace(e.text);

              }

              }

               

               

              Then you reference the font by its name when you want to use it. In this case: 'CompatilFact LT Bold.'

               

               

              update: question is answered.