package {
    import com.adobe.viewsource.ViewSource;

    import flash.display.DisplayObject;
    import flash.display.Graphics;
    import flash.display.Sprite;
    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 TLFSample06 extends Sprite {

        public function TLFSample06() {
            super();
            ViewSource.addMenuItem(this, "srcview/index.html");

            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='20' fontWeight='BOLD' color='#006600'>大事なことなのでもう一度言います。<br /></span></p>" +
                "<p>私の名前は <span fontSize='25' color='#0000cc'>大雅</span> です。</p>" +
                "<p>私は日本人です。</p>" +
                "<p>お寿司が好きです。</p>" +
                "<p>お酒はもっと好きです。</p>" +
                "</TextFlow>";

            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.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.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();

        }
    }
}
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Graphics;
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;
    }
}