Runtime font sharing.

우선 공유하려는 폰트를 라이브러리에 등록하고, 아래 그림과 같이 설정한 다음 SWF을 퍼블리시 합니다.
First, register a font in the library, and set the linkage properties like the following image. Then, publish the SWF file.
まず、共有しようとするフォントをライブラリに登録し、下のイメージのように設定して、SWFファイルを出版します。

Sharing a font

이 SWF파일을 Loader 객체로 로드한 다음, 아래 코드를 사용하여 공유된 폰트를 등록, 사용할 수 있습니다.
Load the SWF via Loader object, then you can retrieve the shared font from the loaded SWF.
このSWFファイルをLoader客体を通じてロードした後で、次のコードを使って、共有されたフォントを登録し、使用できます。
var sharedFont:Class = font_loader.contentLoaderInfo.applicationDomain.getDefinition("Gothic") as Class;
Font.registerFont(sharedFont);

Using the shared font.

그러나 여기서 한 가지 문제가 있습니다. 같은 도메인의 경우 상관이 없으나, 서로 다른 도메인에서는 SWF가 로드되었다 하더라도, ApplicationDomain.getDefinition() 메소드를 통해 폰트를 가져올 수 없습니다. 즉, www.mydomain.com의 SWF에서 font.mydomain.com의 SWF파일에 정의된 폰트를 사용할 수 없다는 것입니다.

이 문제를 해결하기 위해서는 LoaderContext.securityDomain 속성을 사용하여, 다른 도메인의 SWF 사이에서 자원에 대한 접근을 허용할 필요가 있습니다. 다음의 코드는 LoaderContext를 사용하여 다른 도메인 사이에 폰트를 공유할 수 있도록 구성한 것입니다.

But there is a problem. If the two SWF files is on the same domain, no problem. If the SWFs are seperated in different domains, you can not access to the shared font with ApplicationDomain.getDefinition() method, though the SWF file is loaded in. So, an SWF file on www.mydomain.com can not use a font in an SWF on font.mydomain.com.

You can use LoaderContext property, to allow access to assets in a different domain’s SWF file. The following code shows how it works.

でも、一つ、問題があります。同じドメインなら問題ないが、二つのSWFファイルが違うドメインにあると、そのSWFをロードしても、ApplicationDomain.getDefinition()メソッドを使って共有フォントにアクセスすることが出来ないんです。

この問題を解決するために、LoaderContext.securityDomain属性を設定して、他のドメインにあるSWFファイルの資源にアクセスを許容しなければなりません。次のコードは、LoaderContextを使用して、違うドメインからフォントを共有しに作成したものです。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package{
 
	import	flash.display.*;
	import	flash.events.*;
	import	flash.net.*;
	import	flash.system.*;
	import	flash.text.*;
 
 
	public class ShareFont extends MovieClip{
 
		public const FONT_NAME:String = "맑은 고딕";
		public const FONT_SHARE_NAME:String = "Gothic";
		public const FONT_URL:String = "gothic.swf";
 
		public const PLAYER_TYPE:String = Capabilities.playerType;
		private var font_loader:Loader;
		private var context:LoaderContext;
 
 
		public function ShareFont(){
 
			// LoaderContext
			context = new LoaderContext();
			context.checkPolicyFile = true;
			context.securityDomain = SecurityDomain.currentDomain;
 
			// Loads an swf that contains the shared font.
			font_loader = new Loader();
			font_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fontCompleteListener);
			if(PLAYER_TYPE == "External"){
				font_loader.load(new URLRequest(FONT_URL));
			}else{
				font_loader.load(new URLRequest(FONT_URL), context);
			}
 
 
		}
 
 
		/**
		 * Font load complete listener.
		 */
		private function fontCompleteListener(e:Event):void{
 
			// Get Font
			var sharedFont:Class = font_loader.contentLoaderInfo.applicationDomain.getDefinition(FONT_SHARE_NAME) as Class;
			Font.registerFont(sharedFont);
 
			// TextFormat
			var fmt:TextFormat = new TextFormat();
			fmt.size = 16;
			fmt.color = 0x666666;
			fmt.font = FONT_NAME;
 
			// TextField
			var tf:TextField = new TextField();
			tf.embedFonts = true;
			tf.type = TextFieldType.INPUT;
			tf.multiline = true;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.text = "공유 폰트 사용하기";
			tf.setTextFormat(fmt);
 
			addChild(tf);
 
		}
 
	}
 
}

Related Links
http://geniusduck.tistory.com/entry/swf-load-%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%98%EC%97%AC-embeded-font-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0
http://blog.jidolstar.com/247
http://liverwort.tistory.com/227
http://www.adobe.com/devnet/flash/quickstart/embedding_fonts/

Tags: , , ,

9 Responses to “Runtime font sharing.”

  1. 지돌스타 Says:

    내용 너무 잘봤습니다.
    결국 해결법은 applicationDomain과 관련있겠군요.

  2. Hangun's World Says:

    제가 잘 못 이해한 부분이 있어서, 본문 내용을 약간 수정했습니다. (applicationDomain 부분 삭제)

    지돌스타님의 포스트와 플래시 도움말을 다시 읽어 보면서, 뭔가 조금 이상하다 싶어 다시 확인해 보니, 이 문제는 ApplicationDomain이 아니라 SecurityDomain과 관련된 것이었네요. ㅠㅠ
    타 도메인 사이의 통신은 결국 보안 샌드박스의 문제인 것이고, 이는 SecurityDomain이 관장하고 있습니다.

  3. 엄철진 Says:

    오오오 한사장~ 잘 쓸꼐 히히힣

  4. 지돌스타 Says:

    저도 ApplicationDomain이 있어서 그건줄 알았는데….
    생각해보니 이상하다 싶었었던 중에 다시 내용보러 왔더니 바뀌었네요. ㅎㅎㅎ

  5. Hangun's World Says:

    //엄철진
    ㅇㅇ 구래

    //지돌스타
    네, 그냥 securityDomain, applicationDomain 둘 다 설정해주고 되는 것만 확인하고, 아무 생각없이 applicationDomain으로 해결한 것이라 믿어 버렸습니다. 그런데 나중에 좀 더 자세히 들여다보니 그게 아니더군요.
    그나마 지돌스타님의 글을 보면서 뭔가 이상하다 느껴서 다행이지, 망신당할 뻔 했네요 ㅎㅎㅎ

  6. 금돌 Says:

    잘쓰겠습니다^^

  7. Hangun's World Says:

    넹 ^^
    이걸로 멋진거 많이 만들어 주세요.

  8. 스핀 Says:

    아 이거 필요할 때 딱 맞춰서 봤습니다.. 정말 감사합니다 >.<b (오리누나 추천글입니다)ㅋ

    그럼 담 정모때 보아요..!!

  9. Hangun's World Says:

    별말씀을요.
    그나저나 어디 정모일까요??? ^^
    빠르면 담주 FITC에서 보게될지 모르겠군여.

Leave a Reply