Search code examples
actionscript-3actionscriptflash-builder

Embedding bold font as3


I am having trouble embedding a font as bold using FB4. I am using TextLayout fields and TextLayoutFormat to do this and my code is as follows:

package
{
import flash.display.Sprite;
import flash.text.AntiAliasType;
import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.engine.FontLookup;

import flashx.textLayout.container.ContainerController;
import flashx.textLayout.elements.ParagraphElement;
import flashx.textLayout.elements.SpanElement;
import flashx.textLayout.elements.TextFlow;
import flashx.textLayout.formats.TextLayoutFormat;

public class FontEmbedding extends Sprite
{
    [Embed(source='TAHOMA.ttf', fontName='Tahoma', embedAsCFF="true")]
    private var _fontTahoma:Class;
    private const TAHOMA:String = "Tahoma";

    [Embed(source='TAHOMA.ttf', fontWeight = FontWeight.BOLD, fontName='Tahoma Bold', embedAsCFF="true")]
    private var _fontTahomBold:Class;
    private const TAHOMA_BOLD:String = "Tahoma Bold";

    public function FontEmbedding()
    {           
        this.textLayout();
    }

    private function textLayout():void
    {
        var textLayoutFormat:TextLayoutFormat = new TextLayoutFormat();
        textLayoutFormat.fontLookup = FontLookup.EMBEDDED_CFF;
        textLayoutFormat.fontFamily = TAHOMA_BOLD;
        textLayoutFormat.fontSize = 32;

        var textFlow:TextFlow = new TextFlow();
        var paragraphElement:ParagraphElement = new ParagraphElement();
        textFlow.addChild(paragraphElement);

        var span:SpanElement = new SpanElement();
        span.text = "This is an example";
        paragraphElement.addChild(span);

        textFlow.format = textLayoutFormat;

        var containerController:ContainerController = new     ContainerController(this, 400, 100);
        textFlow.flowComposer.addController(containerController);
        textFlow.flowComposer.updateAllControllers();
    }
}
}

The font just displays as normal and the 'fontWeight' property is ignored. Any ideas would be gratefully received?


Solution

  • This code worked for me, with the line textLayoutFormat.fontWeight = FontWeight.BOLD; or not.

    package
    {
        import flash.display.Sprite;
        import flash.text.AntiAliasType;
        import flash.text.Font;
        import flash.text.TextField;
        import flash.text.TextFieldAutoSize;
        import flash.text.TextFormat;
        import flash.text.engine.FontLookup;
        import flash.text.engine.FontWeight;
    
        import flashx.textLayout.container.ContainerController;
        import flashx.textLayout.elements.ParagraphElement;
        import flashx.textLayout.elements.SpanElement;
        import flashx.textLayout.elements.TextFlow;
        import flashx.textLayout.formats.TextLayoutFormat;
    
        public class FontEmbedding extends Sprite
        {
            [Embed(source='ChaparralPro-Bold.otf', fontWeight = FontWeight.BOLD, fontName='ChaparralPro-Bold', embedAsCFF="true")]
            private var _fontGillSans:Class;
            private const FONT_CHAP:String = "ChaparralPro-Bold";
    
            public function FontEmbedding()
            {           
                this.textLayout();
            }
    
            private function textLayout():void
            {
                var textLayoutFormat:TextLayoutFormat = new TextLayoutFormat();
                textLayoutFormat.fontLookup = FontLookup.EMBEDDED_CFF;
                textLayoutFormat.fontFamily = FONT_CHAP;
                textLayoutFormat.fontSize = 32;
                textLayoutFormat.fontWeight = FontWeight.BOLD;
    
                var textFlow:TextFlow = new TextFlow();
                var paragraphElement:ParagraphElement = new ParagraphElement();
                textFlow.addChild(paragraphElement);
    
                var span:SpanElement = new SpanElement();
                span.text = "This is an example";
                paragraphElement.addChild(span);
    
                textFlow.format = textLayoutFormat;
    
                var containerController:ContainerController = new     ContainerController(this, 400, 100);
                textFlow.flowComposer.addController(containerController);
                textFlow.flowComposer.updateAllControllers();
            }
        }
    }