Flash文本引擎 - TextLine以某种字体错误地计算高度

时间:2011-11-14 17:24:11

标签: actionscript-3 flash flex text flex4.5

我正在使用Flash Text Engine的TextBlock& TextLine显示文本。但是,我发现某些字体使TextLine错误地计算高度。这是我的示例代码:

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);
        }
    }
}

我使用的字体是斯旺西(http://www.fontspace.com/roger-white/swansea)标准普通字体。 从代码中,我使用getBounds提取TextLine的高度,并使用该高度绘制矩形。矩形的高度似乎比实际文本短,这意味着getBounds无法获得正确的实际文本高度。

运行代码的结果如下所示:

The result from running the above code

控制台提供以下结果:

57.6
57.59326171875
57.59326171875
0
57.59326171875
0 0 57.6

表示TextLine.textHeight,height,totalHeight和getBounds的结果给出相同的值,大约为57.6像素。
所以,我的问题是,我怎样才能得到实际的文字高度?
请不要告诉我不要使用斯旺西。到目前为止,我已经尝试了5-6种不同的字体,只有斯旺西给我这个问题。但是,我怎么知道这个问题不会再发生呢?至少,如果有一种方法可以区分出现此问题的字体和不会出现此字体的字体,那将非常感激。

1 个答案:

答案 0 :(得分:0)

我发现如果我注释掉这行

textBlock.baselineZero = TextBaseline.ASCENT;

然后totalHeighttotalAscent属性将返回83.81396484375

也许您也可以尝试使用字体编辑器重新打包字体。