swf load 를 이용하여 embeded font 사용하기
font 를 embed 하여 사용하면 swf 파일의 용량이 엄청나게 늘어난다.
따라서 초기 로딩시간이 길어지게 되고, 사용자에게 불필요한 기다림의 시간을 주게 된다.
따라서 swf load 를 이용하여 이러한 문제점을 해결하고자 아래와 같이 샘플을 만들어 보았다.
즉, 빈 fla 파일에 사용하고자 하는 font 만 embed 한 후 swf 로 퍼블리쉬 한다.
그 담에 필요에 따라 swf 파일을 로드하여 그 안에 있는 embeded font 를 사용하는 것이다.
fla 파일에 font 를 embed 하면 Font Class 로 type이 지정된다.
따라서, Font.registerFont( Class ) / Font.enumerateFonts( Boolean ) method 를 사용하여
간단히 구현할 수 있다.
아래 trackback 에 한상훈 실장님의 블로그를 보면, 서로다른 도메인 간에 사용할 수 있게
SecurityDomain 을 설정해 주는 방법에 대해서 알 수 있다.
[ 샘플 보기 ]
소스 보기
|
package
{
import flash.display.Loader;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFormat;
import kr.asduck.debug.echo;
/**
* Use embeded fonts by loading swf file
* ( font 가 embeded 된 swf 를 load 하여 swf file 내의 font를 사용하는 Sample )
* font embeded 시 용량이 커지는 문제를 swf file 을 load 하여 사용하는 방식으로 해결
* 단, 도메인이 서로 다를 경우 이 방법으로는 사용이 불가능하다. ( applicationDomain 부분에 대한 설정이 필요함 )
*
* @author duck
* 2008.04.29
*/
public class SampleEmbededFont extends Sprite
{
public var txt:TextField;
public var btnFontType1:SimpleButton, btnFontType2:SimpleButton;
private var arrFontType:Array;
private var arrFontList:Array;
private var swfLoader:Loader;
public function SampleEmbededFont()
{
// ** default setting ** //
this.arrFontType = [ "font_BrushScriptMT", "font_MalgunGD" ];
this.arrFontList = new Array();
this.txt.text = "This is sample of embeded font";
// ** swf file load ** //
var swfPath:String = "http://asduck.kr/step/step5/embededFonts.swf";
this.swfLoader = new Loader();
this.swfLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, swfLoadComplete );
this.swfLoader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, swfLoadFail );
this.swfLoader.load( new URLRequest( swfPath ) );
echo( "\n1. swf load" );
echo( "\t - swf load : loading..." );
}
private function swfLoadComplete( e:Event ):void
{
echo( "swf load : load complete!" );
this.btnFontType1.addEventListener( MouseEvent.CLICK, changeFontListener );
this.btnFontType2.addEventListener( MouseEvent.CLICK, changeFontListener );
// ** embeded fonts setting ** //
var i:int;
for( i=0; i<this.arrFontType.length; i++ )
{
var loadedFont:Class = this.swfLoader.contentLoaderInfo.applicationDomain.getDefinition( this.arrFontType[i] ) as Class;
Font.registerFont( loadedFont );
}
this.arrFontList = Font.enumerateFonts( false );
}
private function swfLoadFail( e:IOErrorEvent ):void
{
echo( "swf load : load fail by IOError!" );
}
/**
*
* Change font type listener
*
*/
private function changeFontListener( e:MouseEvent ):void
{
var tmpStr:String = e.target.name;
var index:int = int( tmpStr.substr( tmpStr.length - 1, tmpStr.length )) - 1;
// ** apply embeded font to the TextField ** //
var format:TextFormat = new TextFormat();
format.font = this.arrFontList[index].fontName;
echo( "\nfont_name : ", this.arrFontList[index].fontName );
this.txt.embedFonts = true;
this.txt.setTextFormat( format );
}
}
}
| |
Trackback URL : http://geniusduck.tistory.com/trackback/31