Mapping TextFormat Ranges

 May, 19 - 2009   no comments   Uncategorized

There are several reasons you might want to know where one TextFormat ends and another begins. If your application supports any HTML authoring, you’ll likely need something like this, even if your HTML support is fairly basic. I’ve been working on a font loading management utility, so I needed something to detect which portions of the text use which font. Once I have this, I can use Font.hasGlyphs to determine whether I need to load a deeper subset of glyphs or can get away with a more minimal unicode range. (You can quit drooling. That code will be posted here when it’s ready.)

Doing this requires two classes. The first determines whether two TextFormat instances are equal.

  1. package {
  2. import flash.text.TextFormat;
  3. public class TextFormatUtil {
  4. public static var defaultPropertySet:Array /* of String property names */ = [‘font’, ‘size’, ‘color’, ‘underline’, ‘bold’, ‘italic’, ‘url’];
  5. public static var fontOnlyPropertySet:Array /* of String property names */ = [‘font’];
  6. public static function equals (f1:TextFormat, f2:TextFormat, propertySet:Array=null)
  7. {
  8.   if (propertySet == null)
  9.   {
  10.     propertySet = defaultPropertySet
  11.   }
  12.   var match:Boolean = true
  13.   for (var i:String in propertySet)
  14.   {
  15.     var prop = propertySet[i]
  16.     //trace (i + ” ” + prop + ” ” + f1[prop] + ” ” + f2[prop])
  17.     if (f1[prop] != f2[prop])
  18.     {
  19.       match = false
  20.       break
  21.     }
  22.   }
  23.   return match
  24. }
  25. }
  26. }

Easy enough. Now, you could just walk through each character in the TextField one at a time testing for TextFormat equality. But that’s really inefficient. Instead, I implemented a binary search.

  1. package {
  2. import flash.text.TextFormat;
  3. import flash.events.Event;
  4. import flash.text.TextField;
  5. public class TextFormatMapper {
  6. private var charsInTextFormatAtIndex : Array /* of int */;
  7. private var textField : TextField;
  8. public function TextFormatMapper() {
  9. }
  10. public function setTextField(tf:TextField)
  11. {
  12.   textField = tf
  13.   textField.addEventListener(Event.CHANGE, onTextFieldChanged)
  14. }
  15. protected function onTextFieldChanged (event:Event) {
  16.   mapTextFormat()
  17. }
  18. public function mapTextFormat()
  19. {
  20.   charsInTextFormatAtIndex = new Array()
  21.   var startIndex = 0
  22.   while (startIndex < textField.text.length)
  23.   {
  24.     var hi:int = textField.text.length
  25.     var lo:int = startIndex+1
  26.  
  27.     var format1:TextFormat = textField.getTextFormat(startIndex, startIndex+1)
  28.     while (lo < hi) {
  29.       var mid:int = Math.ceil (lo + ((hi lo) / 2))
  30.       var format2:TextFormat = textField.getTextFormat(startIndex + 1, mid)
  31.       if ( TextFormatUtil.equals (format1,format2, TextFormatUtil.fontOnlyPropertySet) )
  32.       {
  33.         //try a larger span if possible
  34.         lo = mid //+ 1
  35.       }
  36.       else
  37.       {
  38.         //try a smaller span if possible
  39.         hi = mid 1
  40.       }
  41.     }
  42.     var charsInTextFormat:int = hi startIndex
  43.     charsInTextFormatAtIndex.push ( charsInTextFormat )
  44.     startIndex = hi
  45.   }
  46. }
  47. public function getCharsInTextFormatAtIndex() : Array {
  48.   return charsInTextFormatAtIndex;
  49. }
  50. }
  51. }

To use it, you create a new TextFormatMapper and pass your TextField to the setTextField method. You can see I’m automatically calling the mapTextFormat method when the Event.CHANGE fires on the TextField. For testing, I hardcoded it to only test against the font property, but you can see how to easily change this to another property set on line 44. Once it’s finished, you can call getCharsInTextFormatAtIndex to get an array containing the length (in characters) of each distinct TextFormat (in this case, distinct font) encountered in the TextField.

This class will change a bit as I move forward with it, but I was really pleased to get this far.


Related articles


Leave a Reply

Your email address will not be published. Fields with * are mandatory.