package {
    import com.adobe.viewsource.ViewSource;
    
    import fl.events.ColorPickerEvent;
    import fl.events.ComponentEvent;
    
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.text.AntiAliasType;
    import flash.text.TextField;
    import flash.text.TextFormat;

    /**
     * Example application to show how embedded fonts will conflict with SWC's which include
     * the same font in either static or dynamic (embed) format.
     * 
     * @author Chris Black
     * 
     */
     
    [SWF(width='300', height='300', backgroundColor='#FFFFFF', frameRate='30')]
    public class ConflictingFonts extends Sprite
    {
        // Embed Georgia using the default names. Will conflict with the SWC!!
        [Embed(source="/assets/fonts/georgia.ttf", advancedAntiAliasing="true", fontFamily="Georgia", 
            fontName="Georgia", fontWeight="normal", mimeType="application/x-font" )]
        private var GeorgiaFont:Class;
        public static const GEORGIA:String = "Georgia";
        
        // Embed Georgia using a unique fontFamily and fontName.
        [Embed(source="/assets/fonts/georgia.ttf", advancedAntiAliasing="true", fontFamily="MyGeorgia", 
            fontName="MyGeorgia", fontWeight="normal", mimeType="application/x-font" )]
        private var MyGeorgiaFont:Class;
        public static const MY_GEORGIA:String = "MyGeorgia";
        
        // Used for storing a reference to the default Georgia font.
        private var georgiaFormat:TextFormat;
        
        // Used for storing a reference to the uniquely defined Georgia font.
        private var myGeorgiaFormat:TextFormat;
        
        // Stores the selected TextFormat.
        private var currentFormat:TextFormat;
        
        // TextField used for the invisible text example.
        private var georgiaText:TextField;
        
        /**
         * Creates a new ConflictingFonts instance. 
         * 
         */
        public function ConflictingFonts()
        {
            ViewSource.addMenuItem(this, "http://www.blackcj.com/blog/wp-content/swfs/ConflictingFonts/srcview/index.html");  // Used to allow ViewSource
        
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            // Component from a SWC which uses Georgia in a static text field
            var cc:ColorComponent = new ColorComponent();
            cc.colorPicker.addEventListener(ColorPickerEvent.CHANGE, selectColor, false, 0, true);
            cc.y = 30;
            this.addChild(cc);
            
            // Component from another SWC that uses Georgia in a static text field
            var sc:SelectionComponent = new SelectionComponent();
            sc.georgia.addEventListener(ComponentEvent.BUTTON_DOWN, selectGeorgia, false, 0, true);
            sc.georgia.selected = true;
            sc.myGeorgia.addEventListener(ComponentEvent.BUTTON_DOWN, selectMyGeorgia, false, 0, true);
            sc.y = cc.y + cc.height;
            this.addChild(sc);
            
            
            // Create the local TextFormat References
            georgiaFormat = new TextFormat(GEORGIA, 18);
            currentFormat = georgiaFormat;
            myGeorgiaFormat = new TextFormat(MY_GEORGIA, 18);
            
            // Create the local TextField (the text won't show up!)
            georgiaText = new TextField();
            georgiaText.x = 2;
            georgiaText.y = 2;
            georgiaText.embedFonts = true;
            georgiaText.antiAliasType = AntiAliasType.ADVANCED;
            georgiaText.width = 300;
            georgiaText.height = 22;
            georgiaText.defaultTextFormat = georgiaFormat;
            georgiaText.setTextFormat(georgiaFormat);
            georgiaText.text = "Invisible Text!";
            this.addChild(georgiaText);
            
        }
        
        /**
         * Used to change the color of the text. Why not have the component do something?
         *  
         * @param event
         * 
         */
        private function selectColor(event:ColorPickerEvent):void
        {
            georgiaFormat.color = event.color;
            myGeorgiaFormat.color = event.color;
            updateFormat();    
        }
        
        /**
         * Used to update the TextFormat of the TextField after a change has been made. 
         * 
         */
        private function updateFormat():void
        {
            georgiaText.defaultTextFormat = currentFormat;
            georgiaText.setTextFormat(currentFormat);                        
        }
        
        /**
         * Triggered when the Georgia radio button is clicked.
         * 
         * @param event
         * 
         */
        private function selectGeorgia(event:ComponentEvent):void
        {
            currentFormat = georgiaFormat;
            updateFormat();    
        }
        
        /**
         * Triggered when the MyGeorgia radio button is clicked.
         *  
         * @param event
         * 
         */
        private function selectMyGeorgia(event:ComponentEvent):void
        {
            currentFormat = myGeorgiaFormat;
            updateFormat();    
        }
        
        
    }
}