In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: フォント共有テスト(Dynamic Embedded Fonts)

Get Adobe Flash player
by buccchi 18 Apr 2011
    Embed
/**
 * Copyright buccchi ( http://wonderfl.net/user/buccchi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/rh02
 */

// forked from mousepancyo's フォント共有テスト(Dynamic Embedded Fonts)
package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.events.MouseEvent;
    import flash.text.TextFormat;
    import flash.system.Security;
    import com.bit101.components.*;

    public class FontEmbedTest extends Sprite {
        
        private var tx1:TextField = new TextField() 
        private var tx2:TextField = new TextField() 
        private var _Stencil:FontEmbed;
        private var _HiraginoKaku:FontEmbed;        
        private var _txtFmt:TextFormat = new TextFormat()
        
        private var _bar:ProgressBar;
        private var _label:Label;

        public function FontEmbedTest() {
            _label = new Label(this, 180, 203, "Now Loading");
            _bar = new ProgressBar(this, 180, 220);
            _bar.maximum = 100;
            _bar.addEventListener(Event.ENTER_FRAME, loading);
            
            tx1.x = 30
            tx1.y = 50
            tx1.rotation = -5
            tx2.x = 50
            tx2.y = 150
            tx2. rotation = 20
            tx1.defaultTextFormat = tx2.defaultTextFormat = _txtFmt
            
            addChild(tx1)
            addChild(tx2)
            
            //Load Stencil
            //_Stencil = new FontEmbed("http://www.digifie.jp/files/Stencil.swf", "Stencil");
           
            //Load Hiragino Kaku Gothic Pro W6
            //_HiraginoKaku = new FontEmbed("http://www.digifie.jp/files/HiraginoKakuW6.swf", "HirakakuW6");
            _HiraginoKaku = new FontEmbed("http://buccchi.jp/wonderfl/201104/font.swf", "KozukaGothicB");
            _HiraginoKaku.addEventListener(FontEmbed.FONT_LOADED, onFontLoaded);
        }
        
        private function loading(event:Event):void {
            _bar.value = _HiraginoKaku.progress;
            if(_bar.value >= 100){
                _bar.removeEventListener(Event.ENTER_FRAME, loading);
                removeChild(_bar)
                removeChild(_label)
            }
        }

        private function onFontLoaded(e:Event):void {
            //Embeted Hiragino Kaku Gothic Pro W6
            //_HiraginoKaku.embetedFont(tx2, "Hiragino Kaku Gothic Pro W6", 18, _txtFmt);
            _HiraginoKaku.embetedFont(tx2, "Kozuka Gothic Pro B", 18, _txtFmt);
            _HiraginoKaku.removeEventListener(FontEmbed.FONT_LOADED, onFontLoaded);
            
            //Test
            tx1.autoSize = tx2.autoSize = "left";
            //tx1.text = "Font : Stencil\nHello Wonderfl";
            tx2.text = "0,123,456,789";
        }
    }
}


//class FontEnbed
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.EventDispatcher;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.Font;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.system.Security;

class FontEmbed extends EventDispatcher {

    private var _fontClassName:String
    private var _loader:Loader = new Loader()
    public var progress:int
    
    public static const FONT_LOADED:String = "font_loaded";
    
    public function FontEmbed(fontPath:String, fontClassName:String) {
        swfLoad(fontPath);
        _fontClassName = fontClassName;
    }
    
    //Load FontSwf
    private function swfLoad(fontPath:String):void{
        var context:LoaderContext = new LoaderContext(); 
        context.checkPolicyFile = true;
        context.securityDomain = SecurityDomain.currentDomain; 
        context.applicationDomain = ApplicationDomain.currentDomain;
        var req:URLRequest = new URLRequest(fontPath);
        _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadComplete);
        _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgressListener);
        _loader.load(req, context);
    }
    
    //Progress
    private function onProgressListener(e:ProgressEvent):void {
        progress = e.bytesLoaded/e.bytesTotal*100;
    }
    
    //Load Complete
    private function swfLoadComplete(e:Event):void {
        _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,swfLoadComplete);
        _loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,onProgressListener);
        var fontClass:Class = _loader.contentLoaderInfo.applicationDomain.getDefinition(_fontClassName) as Class; 
        try{
            Font.registerFont(fontClass);
        }catch(e:Error){
            //
        }
        dispatchEvent(new Event(FontEmbed.FONT_LOADED));
    }
    
    //Embeted Font
    public function embetedFont(txt:TextField, fontname:String, size:Number, fmt:TextFormat):void {
        var tfmt:TextFormat = fmt
        tfmt.font = fontname;
        tfmt.size = size;
        txt.embedFonts = true;
        txt.defaultTextFormat = tfmt;
    }
}