1 Reply Latest reply on Jun 11, 2010 9:16 AM by Flex harUI

    Embedding fonts in textFields

    Ben Danis Level 1

      I am trying to embed a font in my flex project that loads sprites within UI components

       

      I load a series of swf files.  The first ones being a class that registers fonts

      I have embedded each font in a separate class definition that extends FontAsset.

       

      Each font is embedded this way

      [Embed(    fontWeight="bold",
                  fontName="_TRAJAN PRO",
                  source="/../libs/fonts/TrajanPro-Bold.otf",
                  advancedAntiAliasing="true",
                  mimeType="application/x-font-truetype",
                  embedAsCFF="false")]
          public class TrajanBold extends FontAsset

       

      When its loaded I do Font.registerFont(loadedswf.trajanBoldClass) (trajanBold returning me the class TrajanBold)

       

      when i look at all the registered fonts, _TRAJAN PRO is in the list, therefore it should be fine

       

      Then, when i create a sprite in a UIComponent, i set the text format for all text fields to their embedded name (bold or regular, i have both embedded).

      The font simply won't display, everything gets displayed as Times New Roman still.  I'm running out of options.  I've tried embedding using fx:Style @font-face, but that doesn't work either.

       

              static public function initializeFonts():void {
                  _fontList = [];
                  _fontList = Font.enumerateFonts();
              }
              static public function setLocaleContainer(container:DisplayObjectContainer):void {
                  if (_fontList == null)
                      initializeFonts();
                  var num:uint = container.numChildren;
                  for (var i:uint = 0; i < num; i++) {
                      var o:DisplayObject = container.getChildAt(i);
                      if (o is TextField && (o as TextField).styleSheet == null) {
                          try {
                              var t:TextField = o as TextField
                              var f:TextFormat = t.defaultTextFormat;
                              var fontName:String = f.font;
                              var embedName:String = "_" + fontName.toUpperCase();

                               for each (var font:Font in _fontList) {
                                  if (font.fontName.toUpperCase() == embedName) {
                                      //var s:String = t.text;
                                      f.font = font.fontName;
                                     
                                      //t.embedFonts = true;
                                      t.setTextFormat(f);
                                      t.defaultTextFormat = f;
                                      //t.text = s;
                                      trace(t.name, embedName, "fontName: ", fontName, "new font", t.defaultTextFormat.font);
                                      break;
                                  }
                              }
                             
                             
                             
                          } catch (e:Error) {
                              trace("couldn't embed font",    e.message);
                          }
                      } else if (o is DisplayObjectContainer) {
                          setLocaleContainer(o as DisplayObjectContainer);
                      }
                  }
              }

       

      If i add the embed font = true line, then everything disappears.

       

      Any help would be greatly appreciated!

       

      I used to have the fonts embedded within the text field.  But i just added the dynamic embedding of fonts to reduce file size and repetition for all my screens.