フォントを埋め込む必要がある案件で、パブリッシュの度にフォント埋め込み処理を行っていては効率が悪いです。そこでどうにかしてフォントを外部から読むことができないかなと調べてみました。

■ フォント埋め込み用の swf を作成する

そのままなんですが、フォント埋め込み用の swf を作成します。新規作成し、Flash IDE のライブラリで右クリック、「新しいフォント」で埋め込みたいフォントを選択します。リンケージで 「ActionScript に書き出し」、「最初のフレームに書き出し」にチェックを入れます。そしてパブリッシュ。ここでできた swf をメインの swf に読み込むことで、フォントを外部読み込みとして扱うことができます。
この swf 名を「fontCollection.swf」とします。

■ メイン swf に fontCollection.swf を読み込む

以下に Progression で使う場合のやり方を記します。ついでにこないだの Singleton も絡めます。

・IndexScene ( import は省略します)
private var _fontCollection:CastLoader;
private var _singleton:Singleton;
public function IndexScene() {
	_singleton = Singleton.getInstance();
}
protected override function _onLoad():void {
	//font
	_fontCollection = new CastLoader();
	var _loaderContext:LoaderContext = new LoaderContext();
	_loaderContext.applicationDomain = ApplicationDomain.currentDomain;
	_fontCollection.load(new URLRequest("fontCollection.swf"), _loaderContext);
	_fontCollection.onCastLoadComplete = function():void {
		var _className = "リンケージで設定した時の名前";
		var _loadedFont:Font = new Font();
		var LoadedFontClass:Class = ApplicationDomain.currentDomain.getDefinition(_className) as Class;
		Font.registerFont(LoadedFontClass);
		_loadedFont = new LoadedFontClass();
				
		// 読み込んだフォントデータを Singleton へ
		_singleton.embedFontData = _loadedFont;
	}
}

・Singleton
package {
	import flash.text.Font;

	/**
	 * Singleton
	 */
	public class Singleton {

		/**
		 * own
		 */
		private static var _singleton:Singleton;
		
		/**
		 * font
		 */
		private var _embedFontData:Font;
		public function get embedFontData():Font { return _embedFontData; }
		public function set embedFontData(value:Font):void {
			_embedFontData = value;
		}

		/**
		 * constructor
		 */
		public function Singleton(observer:SingletonObserver) {
			if (observer == null) {
				throw new ArgumentError("public construction not allowed. use getInstance() method.");
			}
		}
		
		public static function getInstance():Singleton {
			if (_singleton == null) {
				_singleton = new Singleton(new SingletonObserver());
			}
			
			return _singleton;
		}
	}
}

class SingletonObserver {
	public function SingletonObserver() { }
}

・外部フォントを使いたいクラスの一部
var _embedFontData:Font = Singleton.getInstance().embedFontData;
var _tf:TextField = new TextField();
var _format:TextFormat = new TextFormat(_embedFontData.fontName);
_tf.embedFonts = true;
_tf.defaultTextFormat = _format;
_tf.text = "foobar";
addChild(_tf);


参考リンク
http://zapruder.main.jp/blog/?p=75