I am using Flash Text Engine's TextBlock & TextLine to display text. However, I found that some font make TextLine calculate height incorrectly. Here is my sample code:
package users
{
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.engine.ElementFormat;
import flash.text.engine.FontDescription;
import flash.text.engine.FontLookup;
import flash.text.engine.FontPosture;
import flash.text.engine.FontWeight;
import flash.text.engine.RenderingMode;
import flash.text.engine.TextBaseline;
import flash.text.engine.TextBlock;
import flash.text.engine.TextElement;
import flash.text.engine.TextLine;
public class TestTextSize extends Sprite
{
private var textBlock:TextBlock = null;
private var textElement:TextElement = null;
private var _textField:TextField;
private var _textFormat:TextFormat;
private var elementFormat:ElementFormat;
private var _textLine:TextLine;
[Embed(source="assets/fonts/SWANSE__.TTF", fontFamily="Swansea", mimeType="application/x-font", embedAsCFF="true")]
private const Swansea:Class;
public function TestTextSize()
{
super();
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
stage.scaleMode = StageScaleMode.NO_SCALE;
var fontDescription:FontDescription = new FontDescription("Swansea"
, FontWeight.NORMAL, FontPosture.NORMAL, FontLookup.EMBEDDED_CFF, RenderingMode.CFF);
elementFormat = new ElementFormat();
elementFormat.fontDescription = fontDescription;
elementFormat.fontSize = 114.5;
elementFormat.color = 0x990000;
textElement = new TextElement("This is some text", elementFormat);
textBlock = new TextBlock(textElement);
textBlock.baselineZero = TextBaseline.ASCENT;
_textLine = textBlock.createTextLine(null, 1000,0,true);
_textLine.y = 0;
addChild(_textLine);
trace(_textLine.height);
trace(_textLine.textHeight);
trace(_textLine.totalHeight);
trace(_textLine.totalAscent);
trace(_textLine.totalDescent);
var rect:Rectangle = _textLine.getBounds(this);
trace(rect.y, rect.height);
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0x004400);
sprite.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
sprite.graphics.endFill();
sprite.alpha = 0.3;
sprite.y = 0;
addChild(sprite);
}
}
}
The font I'm using is Swansea (http://www.fontspace.com/roger-white/swansea) standard normal font. From the code, I extract TextLine's height by using getBounds, and use that height to draw rectangle. The rectangle appears to has height shorter than the actual text, means that getBounds can't get correct actual text height.
The result from running the code looks like this:
The console give the following result:
57.6
57.59326171875
57.59326171875
0
57.59326171875
0 57.6
which means TextLine.textHeight, height, totalHeight, and result from getBounds give the same value, roughly 57.6 pixel.
So, my question is, how can I get the actual text height?
Please don't tell me not to use Swansea. So far, I have tried 5-6 different fonts and only Swansea give me this problem. But, how do I know that this problem will not happen again? At least, if there is a way to distinguish font that give this problem and font that won't, that would be highly appreciated.