Friday, February 11, 2011

SHARE - How to embed font from 2 files swf

on your action script

package{
public class Main extends Sprite {
private var fontFace:FontControl;

public function Main(){
envFontFace = new FontControl();
envFontFace.addEventListener(FontControl.COMPLETE, loadFont);
envFontFace.load("Arial.swf", "Aller.swf",
[{font:'Arial', id:'Arial'}, {font:'Aller', id:'Aller'}])
}
}
}


add this to FontControl.as

package{
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.EventDispatcher;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.text.Font;

public class FontControl extends EventDispatcher
{
public static const COMPLETE:String = "font_load_complete";
public static const ERROR:String = "font_load_error";
public static var FONTS:Array;

private var _loaderENV:Loader;
private var _loaderINV:Loader;
private var _domainENV:ApplicationDomain;
private var _domainINV:ApplicationDomain;

private var fileLoaded:int;

public function FontControl() {
}
public function load(envPath:String, invPath:String, fontArr:Array):void
{
FONTS = fontArr;
fileLoaded = 0;
_loaderENV = new Loader();
_loaderENV.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, _ioErrHdlr);
_loaderENV.contentLoaderInfo.addEventListener(Event.COMPLETE, _loadComplete);
_loaderENV.load(new URLRequest(envPath));

_loaderINV = new Loader();
_loaderINV.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, _ioErrHdlr);
_loaderINV.contentLoaderInfo.addEventListener(Event.COMPLETE, _loadComplete);
_loaderINV.load(new URLRequest(invPath));
}

private function _loadComplete(e:Event):void
{
fileLoaded = fileLoaded + 1 ;
if(fileLoaded == 2){
_domainENV = _loaderENV.contentLoaderInfo.applicationDomain;
_domainINV = _loaderINV.contentLoaderInfo.applicationDomain;
_registerFonts();
var embeddedFonts:Array = Font.enumerateFonts(false);
}
}

private function _registerFonts():void
{
for (var i:uint = 0; FONTS[i]; i++)
{
if(i==0){
if(_domainENV.hasDefinition(FONTS[i].id)){
Font.registerFont(_domainENV.getDefinition(FONTS[i].id) as Class);
}
if(_domainENV.hasDefinition(FONTS[i].id+'Bold')){
Font.registerFont(_domainENV.getDefinition(FONTS[i].id+'Bold') as Class);
}
if(_domainENV.hasDefinition(FONTS[i].id+'BoldItalic')){
Font.registerFont(_domainENV.getDefinition(FONTS[i].id+'BoldItalic') as Class);
}
if(_domainENV.hasDefinition(FONTS[i].id+'Italic')){
Font.registerFont(_domainENV.getDefinition(FONTS[i].id+'Italic') as Class);
}
}
if(i==1){
if(_domainINV.hasDefinition(FONTS[i].id)){
Font.registerFont(_domainINV.getDefinition(FONTS[i].id) as Class);
}
if(_domainINV.hasDefinition(FONTS[i].id+'Bold')){
Font.registerFont(_domainINV.getDefinition(FONTS[i].id+'Bold') as Class);
}
if(_domainINV.hasDefinition(FONTS[i].id+'BoldItalic')){
Font.registerFont(_domainINV.getDefinition(FONTS[i].id+'BoldItalic') as Class);
}
if(_domainINV.hasDefinition(FONTS[i].id+'Italic')){
Font.registerFont(_domainINV.getDefinition(FONTS[i].id+'Italic') as Class);
}
}
}
dispatchEvent(new Event(COMPLETE));
}

private function _ioErrHdlr(e:IOErrorEvent):void
{
trace(e);
dispatchEvent(new Event(ERROR));
}
}
}

No comments:

Post a Comment