<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" viewSourceURL="srcview/index.html">
    
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import flash.text.TextField;
            import flash.text.TextFormat;
            import flash.geom.Rectangle;
            
            private var _generated_text_field:TextField;
            
            private function renderText():void{
                
                //CREATE SAMPLE TEXT FIELD, TAKE A SNAPSHOT OF IT
                _generated_text_field=createSampleTextField();
                temp_uic.addChild(_generated_text_field);
                var bounds:Rectangle=temp_uic.getBounds(_generated_text_field);
                var bmd:BitmapData=new BitmapData(bounds.width,bounds.height,true,0);
                bmd.draw(temp_uic);
                
                /*
                var preview:Bitmap=new Bitmap(bmd);
                preview.alpha=.2;
                temp_uic.addChild(preview);
                    */
                var i:uint=0;
                var lines:Array=new Array();
                var line_ys:Array=new Array();
                huge_text.text="";
                
                var output_text:TextField=createRenderTextField(bmd.width+100,bmd.height+100);
                
                //temp_uic.addChild(output_text);
                output_text.text="W";
                
                var mono_char_width:Number=output_text.getCharBoundaries(0).width;
                var mono_char_height:Number=output_text.getCharBoundaries(0).height;
                
                //CREATE ARRAYS OF STRINGS
                output_text.text=String(output_text.getCharBoundaries(0).y);
                while(output_text.getCharBoundaries(output_text.text.length-1).y < bmd.height){
                    lines[i]="";
                    line_ys[i]=output_text.getCharBoundaries(output_text.text.length-1).y;
                    output_text.appendText("\nW");
                    if(i==100)break;
                    i++;
                }
                
                //LOOP THROUGH CHARACTER POSITIONS IN SAMPLE TEXT FIELD, TEST POSITIONS AGAINST PIXELS IN SNAPSHOT
                var xindex:uint=0;
                var j:uint=0;
                while(xindex<bmd.width){
                    for(j=0;j<lines.length;j++){
                        if(bmd.getPixel32(xindex,line_ys[j])==0x00000000){
                            lines[j]+=" ";//empty space
                        }else{
                            lines[j]+=_generated_text_field.text.charAt(_generated_text_field.getCharIndexAtPoint(xindex,10));//the y is arbitrary...
                        }
                    }
                    
                    xindex+=mono_char_width;
                }
                
                huge_text.text=lines.join("\n");
                temp_uic.removeChild(_generated_text_field);                
            }
            
            private function createSampleTextField():TextField{
                var tf:TextField=new TextField();
                tf.autoSize="left";
                tf.text=text_to_create.text;
                tf.selectable=false;
                var tformat:TextFormat=new TextFormat();
                tformat.bold=true;
                tformat.size=font_size_slider.value;
                tformat.font=fonts_list.selectedLabel;
                tf.setTextFormat(tformat);
                return tf;
            }
            
            private function createRenderTextField(w:uint,h:uint):TextField{
                var tf:TextField=new TextField();
                tf.multiline=true;
                tf.width=w;
                tf.height=h;
                var tformat:TextFormat=new TextFormat();
                tformat.size=12;
                tformat.color=0xBBBBBB;
                tformat.font="Courier";
                tf.setTextFormat(tformat);
                return tf;
            }
            
            
            
        ]]>
    </mx:Script>
    
    <mx:Panel width="1000" height="500" horizontalCenter="0" label="the huge text generator"  y="50">
        <mx:HBox horizontalAlign="center" paddingTop="20">

            <mx:ComboBox id="fonts_list" dataProvider="{flash.text.Font.enumerateFonts(true)}" labelField="fontName" />

            <mx:Label text="font size:" fontSize="14" fontWeight="bold" />            
            <mx:HSlider id="font_size_slider" minimum="80" maximum="200" value="140"/>
            
            <mx:TextInput id="text_to_create" text="HUGE T3xT!?$ :)" width="200" fontSize="14" fontWeight="bold" />
            <mx:Button label="create HUGE text baby" click="renderText();" />

        </mx:HBox>
        <mx:Canvas width="100%" height="100%" >
            <mx:UIComponent id="temp_uic" />
            <mx:TextArea width="100%" height="100%" id="huge_text" fontSize="12" fontFamily="Courier" backgroundAlpha="0" />            
        </mx:Canvas>
    </mx:Panel>
    
</mx:Application>