Categories Displayed in Flash

Flash + Fonts: Dynamically Loaded Fonts + Embedding + CSS + HTML == Hell

I swear about every year I end up reexploring the dark internals to flash's murky font seas, lose a day to a weekend everytime.

Basically the embedded fonts + html is a fairly complicated beast, that in order to work has to be called in a very specific order to work.

Here's a new twist.  As I've covered in previous articles the ability to load fonts dynamically is very cool, but what of those textfields on stage used as placeholders? How do they join in the font fun?  THEY DON'T.   It doesn't appear possible to have them styled as CSS+HTML+Embedded, even if you set it to new text with CSS the text inside just dissapears.  However you can create a new TextField that does support those values at the exact same place, and swap them like:

  1.  
  2.                 //////////// Takes a clip on stage and replaces it with an 'identical' one ////////////////////////////////////////
  3.                 public static function  swapClip(txt : TextField) : TextField {
  4.                         trace("swapClip2..............." + txt.name);
  5.                         var par : DisplayObjectContainer = txt.parent;
  6.  
  7.                         var tf : TextField = new TextField();
  8.                         var ss : StyleSheet = new StyleSheet();
  9.                         var tff : TextFormat = txt.defaultTextFormat;
  10.                        
  11.                         var p : Object = new Object();
  12.                         p.fontSize = tff.size;
  13.                
  14.                         p.fontFamily = tff.font;
  15.                         //"Myriad Pro";
  16.                         p.color = tff.color;
  17.                         p.size = tff.size;
  18.                         ss.setStyle("p", p);
  19.                         //
  20.                         tff.font = tff.font;
  21.                         //"Myriad Pro";
  22.                         tf.defaultTextFormat = tff;
  23.                         // new TextFormat("Myriad Pro", 16, 0);  
  24.                         tf.styleSheet = ss;
  25.                        
  26.                        
  27.                         tf.width = txt.width;
  28.                         tf.height = txt.height;
  29.                         tf.x = txt.x;
  30.                         tf.y = txt.y;
  31.                         //      tf.border = true;
  32.  
  33.                         tf.embedFonts = true;
  34.                         tf.antiAliasType = AntiAliasType.ADVANCED;
  35.                         tf.selectable = true;
  36.                         tf.multiline = true;
  37.                         tf.wordWrap = true;
  38.                         tf.condenseWhite = false;
  39.    
  40.                         //      tf.rotation = 15;
  41.                         //      txt.visible = false;
  42.                         trace("swapClip htmlText " + txt.htmlText);
  43.                         var html : String = "<p>" + txt.text + "</p>";
  44.                         tf.htmlText = html;
  45.                         tf.name = txt.name + "";
  46.              
  47.                         //              par
  48.                         par.addChildAt(tf, par.getChildIndex(txt));
  49.                         par.removeChild(txt);
  50.                         return tf;
  51.                 }
  52.  

Weirdly you have to give it both TextFormat and styleSheet to get the similar formatting as the clip on stage, clearly Flash's internal renderer has significant internal state and both TextFormat or StyleSheet overlapp controlling different aspects of it.

1 responses to 'Flash + Fonts: Dynamically Loaded Fonts + Embedding + CSS + HTML == Hell'

Made on 28.10.08 @ 4:28 pm

[...] you haven’t you may want to check out loading Font Libraries dynamically. Here and [...]

Add a Comment: