R – Large serifs in a font cause flash to measure size incorrectly

actionscript-3apache-flexflashfonts

I have a textarea where I measure the textWidth and textHeight to make sure the user cannot enter more text than can fit in the text area. I also extended the textArea with a textHeightNow and textWidthNow that measure the textField's dimensions since they update w/out requiring validation. Now this works great for 90% of the embedded fonts I'm using but any fonts that have giant serifs are not measured properly, for instance look at the 'f's in this text area:

alt text http://img.skitch.com/20091101-xhm5jguma1qgukg6fxrymrwr3u.jpg

You can see they get cut off on both sides because textWidth and textWidthNow both return an incorrect size not taking into account the massive serifs. The font size, coords and dimensions of the text area are all integers so thats not the problem, any other suggestions? I looked to see if textArea has a clipContent argument but it doesn't (that would have been nice).

Thanks

Best Answer

I've run into this problem before and as far as I can tell you have two options, none of them very nice:

  • Use the new flash.text.engine in Flash Player 10... its very hard to use but I think it will measure such fonts (and ligatures) properly.
  • Draw the textfield into a bitmapdata and use getColorBoundsRect to determine its real dimensions.

I ended up using the latter as changing the whole app to the new engine would have been much much harder. You will need to tweak margins and use a larger textfield (inside some container) to be able to draw it properly, but it should work.

Related Topic