Actionscript 3 Flash文本引擎-TextLine在某些字体中计算高度不正确
我使用Flash文本引擎的TextBlock和TextLine来显示文本。然而,我发现一些字体使文本行计算高度不正确。以下是我的示例代码:Actionscript 3 Flash文本引擎-TextLine在某些字体中计算高度不正确,actionscript-3,flash,apache-flex,text,flex4.5,Actionscript 3,Flash,Apache Flex,Text,Flex4.5,我使用Flash文本引擎的TextBlock和TextLine来显示文本。然而,我发现一些字体使文本行计算高度不正确。以下是我的示例代码: package users { import flash.display.Sprite; import flash.display.StageScaleMode; import flash.events.Event; import flash.geom.Rectangle; import flash.text.Text
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);
}
}
}
我使用的字体是斯旺西()标准普通字体。
从代码中,我使用getBounds提取TextLine的高度,并使用该高度绘制矩形。矩形的高度似乎比实际文本短,这意味着getBounds无法获得正确的实际文本高度
运行代码的结果如下所示:
控制台给出以下结果:
57.6
57.59326171875
57.59326171875
0
57.59326171875
0 57.6
这意味着TextLine.textHeight、height、totalHeight和getBounds的结果给出了相同的值,大约57.6像素。
所以,我的问题是,我怎样才能得到实际的文本高度?
请不要告诉我不要使用斯旺西。到目前为止,我已经尝试了5-6种不同的字体,只有斯旺西给我这个问题。但是,我怎么知道这个问题不会再次发生?至少,如果有一种方法可以区分出现此问题的字体和不会出现此问题的字体,我们将不胜感激。我发现如果我注释掉这一行
textBlock.baselineZero = TextBaseline.ASCENT;
然后totalHeight
和totalAscent
属性将返回83.81396484375
也许你也可以尝试用字体编辑器重新包装字体。我发现如果我把这行注释掉
textBlock.baselineZero = TextBaseline.ASCENT;
然后totalHeight
和totalAscent
属性将返回83.81396484375
也许你也可以尝试用字体编辑器重新打包字体