package {
    import com.adobe.viewsource.ViewSource;

    import flash.display.DisplayObject;
    import flash.display.Graphics;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;
    import flash.text.Font;
    import flash.text.engine.FontLookup;
    import flash.text.engine.FontPosture;
    import flash.text.engine.JustificationStyle;
    import flash.text.engine.Kerning;

    import flashx.textLayout.container.ContainerController;
    import flashx.textLayout.conversion.TextConverter;
    import flashx.textLayout.edit.EditManager;
    import flashx.textLayout.elements.Configuration;
    import flashx.textLayout.elements.TextFlow;
    import flashx.textLayout.formats.BlockProgression;
    import flashx.textLayout.formats.Direction;
    import flashx.textLayout.formats.JustificationRule;
    import flashx.textLayout.formats.TextAlign;
    import flashx.textLayout.formats.TextLayoutFormat;
    import flashx.textLayout.formats.WhiteSpaceCollapse;
    import flashx.undo.UndoManager;

    [SWF ('#ffffff', width='420', height='420')]
    public class TLFSample07 extends Sprite {
        public function TLFSample07() {
            super();
            ViewSource.addMenuItem(this, "srcview/index.html");
            addEventListener(Event.ADDED_TO_STAGE, addToStageHandler, false, 0, true);
        }
        protected function addToStageHandler(event:Event):void {
            var context_ :LoaderContext;
            var loader_  :Loader;

            context_ = new LoaderContext();
            context_.applicationDomain = ApplicationDomain.currentDomain;

            loader_ = new Loader();
            loader_.contentLoaderInfo.addEventListener(Event.COMPLETE, contentLoaderInfoCompleteHandler);
            loader_.load(new URLRequest("asset/swf/TaigaFont.swf"), context_);

            removeEventListener(event.type, arguments.callee);
        }
        protected function contentLoaderInfoCompleteHandler(event:Event):void {

            const FONT_CLASS :Class  = ApplicationDomain.currentDomain.getDefinition("TaigaFont_FONT") as Class; // <基底クラス名> + "_" + <フォント埋め込みクラス名>
            const MARK_UP    :String =
                "<TextFlow xmlns='http://ns.adobe.com/textLayout/2008'>" +
                "<div><p>こんにちは。</p></div>" +
                "<p>私の名前は <span fontSize='25' color='#cc0000'>大雅</span> です。</p>" +
                "<p>私は日本人です。</p>" +
                "<p>お寿司が好きです。</p>" +
                "<p>お酒はもっと好きです。<br /></p>" +
                "<p><span fontSize='40' fontWeight='BOLD' color='#006600'>大事なことなのでもう一度言います。<br /></span></p>" +
                "<p>私の名前は <span fontSize='25' color='#0000cc'>大雅</span> です。</p>" +
                "<p>私は日本人です。</p>" +
                "<p>お寿司が好きです。</p>" +
                "<p>お酒はもっと好きです。</p>" +
                "</TextFlow>";

            Font.registerFont(FONT_CLASS); //外部読み込みフォントの登録

            var gap               :int;
            var controllerWidth_  :int;
            var controllerHeight_ :int;
            var width_            :int;
            var height_           :int;

            var targetA           :MySprite;
            var targetB           :MySprite;
            var targetC           :MySprite;

            var config            :Configuration;
            var g                 :Graphics;
            var textFlow          :TextFlow;
            var textLayoutFormat  :TextLayoutFormat;

            gap                                 = 10;

            controllerWidth_                    = 180;
            controllerHeight_                   = 180;

            width_                              = controllerWidth_  + gap + gap;
            height_                             = controllerHeight_ + gap + gap;

            targetA                             = addChild( new MySprite() ) as MySprite;
            targetB                             = addChild( new MySprite() ) as MySprite;
            targetC                             = addChild( new MySprite() ) as MySprite;

            textLayoutFormat                    = new TextLayoutFormat();

            textLayoutFormat.color              = 0x333333;
            textLayoutFormat.fontSize           = 18;
            textLayoutFormat.kerning            = Kerning.ON;
            textLayoutFormat.fontStyle          = FontPosture.NORMAL;
            textLayoutFormat.textAlign          = TextAlign.START;
            textLayoutFormat.direction          = Direction.LTR;
            textLayoutFormat.blockProgression   = BlockProgression.RL;
            textLayoutFormat.locale             = "ja";
            textLayoutFormat.fontFamily         = ( new FONT_CLASS() as Font ).fontName; //外部読み込みフォントめいの定義
            textLayoutFormat.whiteSpaceCollapse = WhiteSpaceCollapse.PRESERVE;
            textLayoutFormat.justificationRule  = JustificationRule.EAST_ASIAN;
            textLayoutFormat.justificationStyle = JustificationStyle.PUSH_IN_KINSOKU;
            textLayoutFormat.columnCount        = 2;

            config                              = new Configuration();
            config.textFlowInitialFormat        = textLayoutFormat;

            //マークアップのインポート
            //Build 465 で、TextFilter クラスは TextConverter クラスという名前に変わりました
            textFlow = TextConverter.importToFlow(MARK_UP, TextConverter.TEXT_LAYOUT_FORMAT, config);
            textFlow.fontLookup = FontLookup.EMBEDDED_CFF; // CFF フォーマットの埋め込みフォントを使う

            //編集かつアンドゥできるようにする
            textFlow.interactionManager = new EditManager( new UndoManager() );

            //コントローラの定義
            //複数登録すると…
            textFlow.flowComposer.addController( new ContainerController(targetA, controllerWidth_, controllerHeight_) );
            textFlow.flowComposer.addController( new ContainerController(targetB, controllerWidth_, controllerHeight_) );
            textFlow.flowComposer.addController( new ContainerController(targetC, controllerWidth_, controllerHeight_) );
            textFlow.flowComposer.updateAllControllers();

            //土台
            targetA.x = width_ * 2 + 10;
            targetA.y = 20;
            targetB.x = width_;
            targetB.y = 20;
            targetC.x = width_;
            targetC.y = height_ + 10 + 20;

            //縦書きにすると、textFlow の描画方向が -x 方向になるため、-x 方向に矩形を描画しています
            g = targetA.getGraphics();
            g.beginFill(0xdccccc);
            g.drawRect(gap, -gap, -width_, height_);
            g.endFill();

            g = targetB.getGraphics();
            g.beginFill(0xccdccc);
            g.drawRect(gap, -gap, -width_, height_);
            g.endFill();

            g = targetC.getGraphics();
            g.beginFill(0xccccdc);
            g.drawRect(gap, -gap, -width_, height_);
            g.endFill();

            (event.currentTarget as LoaderInfo).removeEventListener(event.type, arguments.callee);

        }
    }
}
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Rectangle;
class MySprite extends Sprite {
    protected var dummyShape:Shape;
    public function MySprite() {
        super();
        dummyShape = new Shape();
    }
    public override function addChild(child:DisplayObject):DisplayObject {
//        trace("MySprite.addChild()", this);
        return super.addChild(child);
    }
    public override function removeChild(child:DisplayObject):DisplayObject {
//        trace("MySprite.removeChild()", this);
        return super.removeChild(child);
    }
    public override function get graphics():Graphics {
//        trace("MySprite.graphics", this);
        return dummyShape.graphics;
//        return super.graphics;
    }
    public override function set scrollRect(value:Rectangle):void {
//        trace("scrollRect : ", value, this);
//        super.scrollRect = value;
    }
    public function getGraphics():Graphics {
        return super.graphics;
    }
}