4 Replies Latest reply on Mar 15, 2010 6:53 PM by Psyclist

    Embedding fonts at runtime in Adobe AIR

    Psyclist

      I'm trying to register fonts that are stored in an external SWF, and embed them at runtime into an Adobe AIR application. I'm currently using a File object to load the SWF, but am not sure how to register the fonts after that. Anyone has any ideas? Thanks!

        • 1. Re: Embedding fonts at runtime in Adobe AIR
          Jeff Swartz Level 3

          Use a Loader obejct to load the SWF. You can then reference the class name for the font in that SWF file:

           

          var myFont:Font = new Font1(); // "Font1" is the class name for the font in the SWF file.
          var myFormat:TextFormat = new TextFormat();
          myFormat.font = myFont.fontName;

          • 2. Re: Embedding fonts at runtime in Adobe AIR
            Psyclist Level 1

            This is the code I have. Basically I hit a button, a file open dialog comes up, and then I use a Loader to load the SWF. But how do I register the font after that? Is what I'm doing in fontLoaded() correct?

             

            Thanks!

            ZQ

             

             

            --- Code ---

             

            import flash.utils.describeType;
                        import mx.controls.SWFLoader;
                        private var myFontFile: FileReference = new FileReference();
                       
                        private var myFont: File = new File();
                       
                        private function initApp(): void
                        {
                            var embeddedFonts: Array = Font.enumerateFonts(false);
                            trace ("=== BEFORE ===");
                            for (var i: int = 0; i < embeddedFonts.length; i++)
                            {
                                var item: Font = embeddedFonts[i];
                                trace("["+i+"] name: "+item.fontName + ", style: "+ item.fontStyle+", type: "+item.fontType);
                            }   
                            btnLoadFont.addEventListener(MouseEvent.CLICK, loadFont);
                        }
                       
                        private function loadFont(e: MouseEvent): void
                        {
                            myFont.browseForOpen("Select Font");
                            myFont.addEventListener(Event.SELECT, fontSelect);
                      
                        }
                       
                        private function fontSelect(e: Event): void
                        {
                            trace("File: "+myFont.url);
                            var loader: Loader = new Loader();
                            var context: LoaderContext;
                           
                            var loaderInfo: LoaderInfo = loader.contentLoaderInfo;
                           
                            loaderInfo.addEventListener(Event.COMPLETE, fontLoaded);
                            context = new LoaderContext( true, new ApplicationDomain( ApplicationDomain.currentDomain ) );
                            loader.load(new URLRequest(myFont.url), context);
                        }
                       
                        private function fontLoaded(e: Event): void
                        {
                            trace(e.target);
                           
                            var FontLibrary:Class = ApplicationDomain((e.target as LoaderInfo).applicationDomain).getDefinition('_Verdana') as Class;
                          
                            Font.registerFont(FontLibrary);
                           
                     
                            var embeddedFonts: Array = Font.enumerateFonts(false);
                            trace("=== AFTER ===");
                            for (var i: int = 0; i < embeddedFonts.length; i++)
                            {
                                var item: Font = embeddedFonts[i];
                                trace("["+i+"] name: "+item.fontName + ", style: "+ item.fontStyle+", type: "+item.fontType);
                            }
                        }

            • 3. Re: Embedding fonts at runtime in Adobe AIR
              Jeff Swartz Level 3

              Do want the user to browse to select a SWF file that contains the font? This may cause problems because the URL is not in the application directory.

               

              If you just want to load a SWF file, named EmbeddedFonts.swf in your application directory, that contains embedded fonts, this works:

               

                          private function initApp(): void
                          {
                              var embeddedFonts: Array = Font.enumerateFonts(false);
                              trace ("=== BEFORE ===");
                              for (var i: int = 0; i < embeddedFonts.length; i++)
                              {
                                  var item: Font = embeddedFonts[i];
                                  trace("["+i+"] name: "+item.fontName + ", style: "+ item.fontStyle+", type: "+item.fontType);
                              }  

               

                              var loader: Loader = new Loader();
                              var context: LoaderContext;
                              var loaderInfo: LoaderInfo = loader.contentLoaderInfo;
                            
                              loaderInfo.addEventListener(Event.COMPLETE, fontLoaded);
                              loader.load(new URLRequest("app:/EmbeddedFonts.swf"), context);
                          }
                        
                          private function fontLoaded(e: Event): void
                          {
                              var FontLibrary:Class = ApplicationDomain((e.target as LoaderInfo).applicationDomain).getDefinition('_Verdana') as Class;
                              trace(FontLibrary);
                              Font.registerFont(FontLibrary);     
                      
                              var embeddedFonts: Array = Font.enumerateFonts(false);
                              trace("=== AFTER ===");
                              for (var i: int = 0; i < embeddedFonts.length; i++)
                              {
                                  var item: Font = embeddedFonts[i];
                                  trace("["+i+"] name: "+item.fontName + ", style: "+ item.fontStyle+", type: "+item.fontType);
                              }
                          }

              1 person found this helpful
              • 4. Re: Embedding fonts at runtime in Adobe AIR
                Psyclist Level 1

                So basically as long as the font SWF is in the application directory, it should load without any problems? I'll try it out! Thanks!