2010-05-16 79 views
0

我想知道如果我可以動態地在Flex中嵌入字體。我想爲不同的用戶嵌入不同的字體,所以我不想在同一個Flex文件中嵌入所有可能的字體。如果可能的話,請您發佈示例代碼。我可以動態地在Flex中嵌入字體嗎?

+0

那豈不是更容易掌握一些SWF是基於嵌入字體在平臺上?說winxp.swf加載你的應用在windows xp上不存在的應用使用的字體,osx.swf加載osx缺失的字體等。當客戶端加載應用時,爲正確的平臺加載正確的字體swf。 – 2010-05-16 11:54:36

回答

2

你可以在Actionscript中做到這一點。我主要使用這個技巧來使用Flash IDE中編譯器不支持的opentype字體,並創建可以延遲加載的字體庫(僅在需要時),但您也可以使用它來選擇性加載字體。如果你的服務器上有mxmlc編譯器,你甚至可以生成fontlib.as文件並根據命令進行編譯。

// fontlib.as 
// font library file 
package { 
    import flash.display.Sprite; 

    public class fontlib extends Sprite { 
     [Embed(source = 'font/path/FontFile.otf', fontName = 'FontFile', unicodeRange = 'U+0020-U+007E,U+00AB,etc...')] 
     public static var FontFile:Class; 
     public static const FontFile_name:String = "FontFile"; // matches 'fontName' in embed 

     public function fontlib() { 
     } 
    } 
} 

這可以被編譯,像這樣:

mxmlc fontlib.as 

您可以在應用程序中使用這樣的:

// Main.as 
// document class 
package { 
    import flash.text.Font; 
    import flash.display.Loader; 
    import flash.events.Event; 
    import flash.system.ApplicationDomain; 
    import flash.text.StyleSheet; 

    public var fontsLoader:Loader; 
    public var fontFile:String = ""; 
    public var ss:StyleSheet; 

    public function Main() { 
     fontsLoader = new Loader(); 
     fontsLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onFontsLoadComplete); 
    } 

    private function _onFontsLoadComplete(e:Event):void { 
     var fontlib:Class = e.target.applicationDomain.getDefinition('fontlib'); 

     Font.registerFont(fontlib.FontFile); // registers font 
     fontFile = fontlib.FontFile_name;  // name the font was loaded as 

     // actually using the font looks like this: 
     ss = new StyleSheet(); 
     ss.parseCSS("div { fontFamily: " + fontFile + "; }"); 
    } 
} 
相關問題