zoukankan      html  css  js  c++  java
  • 在纯AS工程中嵌入个别字体方法

    2009-11-26 10:00

    前两天接到主管任务,让研究在纯AS工程中嵌入个别字体。在网上找了一大通,也只找到了在Flex项目中使用CSS也嵌入个别字体的方法。最后很无奈的发现网上好像没有我想找的资料。就在此时让我有个想法,在CSS中控制嵌入字体使用的是unicodeRange属性,那我能不能在Embed标签中也使用它呢?实验发现,Embed标签中是可以使用unicodeRange属性的。下面说说我在纯AS工程中是怎么嵌入个别字体的。

    工程开始之前先告诉大家,我要嵌入的字符是"空山无人,水流花开。",要使用的字体是楷体字;

    1.打包要嵌入字体到一个Swf文件中

    (1).新建一个纯AS工程,内容如下:

    代码
    1 package {
    2  import flash.display.Sprite;
    3  import flash.system.Security;
    4  import flash.text.Font;
    5
    6  public class LoadFont extends Sprite
    7 {
    8 [Embed(source="C:/WINDOWS/Fonts/SIMKAI.TTF",fontName="kai",mimeType="application/x-font",unicodeRange="U+7a7a,U+5c71,U+65e0,U+4eba,U+ff0c,U+6c34,U+6d41,U+82b1,U+5f00,U+3002")]
    9 public var RoundHand:Class;
    10
    11 public function LoadFont()
    12 {
    13 }
    14 }
    15 }

    其中,source的值就是字符所在位置(控制面板--字体--楷体,点右键就可以看到其所在位置),

    fontName的值就是该字体名称,只要确保唯一,可随意起。

    mimeType的值照抄。

    unicodeRange的值就是"空山无人,水流花开。",这几个字对应的unicode

    RoundHand就是该字体绑定的类,也是唯一的。

    工程建好后编译,会在bin-debug目录下生成一个名为LoadFont.swf文件。

    2.在工程中使用字体

    在自己的工程中想要使用该字体,先要用下一段:

    代码
    1 fontLoader = new Loader();
    2 fontLoader.load(new URLRequest("LoadFont.swf的路径"), new LoaderContext(false, loaderInfo.applicationDomain));
    3 fontLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
    4
    5 private function completeHandler(e:Event):void
    6 {
    7 fontLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
    8 fontLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
    9 var MyFont:Class = getDefinitionByName("LoadFont_RoundHand") as Class;
    10 // 注册全局字体
    11 Font.registerFont(MyFont);
    12 var myFont:Font = new MyFont() as Font;
    13 // 应用字体
    14 var format:TextFormat = new TextFormat(myFont.fontName, 25, null, true);
    15 txt.defaultTextFormat = format;
    16 // 嵌入字体
    17 txt.embedFonts = true;
    18 txt.text = "空山无人,水流花开。";
    19 }
    20
    21

    其中注意getDefinitionByName的参数格式为“所在类名+"_"+embed的类名”。

    代码
    1 整个工程如下:
    2
    3 package
    4 {
    5 import flash.display.Loader;
    6 import flash.display.Sprite;
    7 import flash.display.StageAlign;
    8 import flash.display.StageQuality;
    9 import flash.display.StageScaleMode;
    10 import flash.events.Event;
    11 import flash.events.ProgressEvent;
    12 import flash.net.URLRequest;
    13 import flash.system.LoaderContext;
    14 import flash.text.Font;
    15 import flash.text.TextField;
    16 import flash.text.TextFormat;
    17 import flash.utils.getDefinitionByName;
    18
    19 public class FontTest extends Sprite
    20 {
    21 private var txt:TextField;
    22 private var fontLoader:Loader;
    23 public function FontTest():void
    24 {
    25 if(stage!=null){
    26 init();
    27 }else{
    28 addEventListener(Event.ADDED_TO_STAGE,init);
    29 }
    30 }
    31 private function init():void{
    32 stage.quality = StageQuality.HIGH;
    33 stage.scaleMode = StageScaleMode.NO_SCALE;
    34 stage.align = StageAlign.TOP_LEFT;
    35 stage.showDefaultContextMenu = false;
    36
    37 txt = new TextField();
    38 txt.x = stage.stageWidth/3;
    39 txt.y = stage.stageHeight/2;
    40 txt.width = 500;
    41 txt.height = 30;
    42 addChild(txt);
    43
    44 fontLoader = new Loader();
    45 fontLoader.load(new URLRequest("LoadFont.swf"), new LoaderContext(false, loaderInfo.applicationDomain));
    46 fontLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
    47 fontLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    48 }
    49 private function progressHandler(e:ProgressEvent):void
    50 {
    51 var ratio:int = e.bytesLoaded / e.bytesTotal * 100;
    52 txt.text = "loading... " + ratio + "%";
    53 }
    54
    55 private function completeHandler(e:Event):void
    56 {
    57 fontLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
    58 fontLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
    59 var MyFont:Class = getDefinitionByName("LoadFont_RoundHand") as Class;
    60 // 注册全局字体
    61 Font.registerFont(MyFont);
    62 var myFont:Font = new MyFont() as Font;
    63 // 应用字体
    64 var format:TextFormat = new TextFormat(myFont.fontName, 25, null, true);
    65 txt.defaultTextFormat = format;
    66 // 嵌入字体
    67 txt.embedFonts = true;
    68 txt.text = "空山无人,水流花开。";
    69 }
    70 }
    71 }
    72
  • 相关阅读:
    [LeetCode] Power of Three 判断3的次方数
    [LeetCode] 322. Coin Change 硬币找零
    [LeetCode] 321. Create Maximum Number 创建最大数
    ITK 3.20.1 VS2010 Configuration 配置
    VTK 5.10.1 VS2010 Configuration 配置
    FLTK 1.3.3 MinGW 4.9.1 Configuration 配置
    FLTK 1.1.10 VS2010 Configuration 配置
    Inheritance, Association, Aggregation, and Composition 类的继承,关联,聚合和组合的区别
    [LeetCode] Bulb Switcher 灯泡开关
    [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
  • 原文地址:https://www.cnblogs.com/crkay/p/1747895.html
Copyright © 2011-2022 走看看