[3D框架]Alternativa3D 专题总编:D5Power [游戏开发]Flash游戏常见功能与实现 专题总编:D5Power [游戏开发]游戏性能优化 专题总编:D5Power [游戏开发]D5RPG 专题总编:D5Power [游戏开发]AI-Steering编程 专题总编:D5Power [MVC框架]FlashBuilder_和_PureMVC 专题总编:D5Power [移动开发]Flash移动开发 专题总编:D5Power [游戏开发]Flixel 横板游戏制作教程 专题总编:mztknb [入门系列专题]★天地培训★新手入门-AS3开发案例 专题总编:S_eVent [游戏开发]做一个像植物大战僵尸的Flash游戏 专题总编:逐日|者 [入门系列专题]Flash动画学习指南 [入门系列专题]从零开始学AS3游戏开发 专题总编:D5Power [MVC框架]Cairngorm专题 专题总编:taotao5453 [MVC框架]PureMVC专题 专题总编:夜歸人 [流媒体服务器]Red 5专题 专题总编:Lost_in_Flash [工具使用]Flash develop专题 专题总编:阿新 [游戏开发]图形处理专题 专题总编:a355112007020 [游戏开发]寻路专题 专题总编:smallghost [3D引擎]Away3D专题 专题总编:xiao198304 [3D引擎]Sandy3D专题 专题总编:sindney [UI框架]Aswing专题 专题总编:393725437 [多媒体]音乐播放专题 专题总编:xiaohaiyong [网络通信]Socket专题 专题总编:smallghost [网络通信]P2P专题 专题总编:eity0323 [网络通信]AMF专题 专题总编:forever_to_our & crylg [性能优化]文件大小专题 专题总编:KengPanda [性能优化]内存回收专题 专题总编:KengPanda [物理引擎]Box2D专题 专题总编:smallghost [物理引擎]APE专题 专题总编:tcper [交互设计]增强现专题实 专题总编:evin [后台语言]Java专题 专题总编:cloud21 [后台语言] C#专题 专题总编:smallghost [Flex]Flex4专题 相关专题总编:iceblue
返回列表 发帖

[新闻资讯] 借助Ruby为Flex自动编译字体

  • 资讯类型: 翻译
  • 来源页面: http://blog.alastairdawson.com/2009/06/30/automate-font-compiling-for-flex-with-ruby/
  • 资讯原标题: Automate font compiling for Flex with Ruby
  • 资讯原作者: Alastair
  • 翻译词数: 词
  • 我的评论:
    对这篇文你有啥看法,跟贴说说吧!欢迎口水和板砖,哈哈。欢迎大家和我们一同分享更多资讯。

    本帖最后由 达达 于 2009-8-29 12:53 编辑

    我之所以提出这个方法是因为我最近的项目需要将100种字体编译进SWF文件中。仅仅靠手动的话我会发疯的。因此我编写了一个ruby脚本和shell脚本自动完成这项任务

    步骤1:配置
    我使用的目录结构如下:
    1. convert.rb (the ruby script)
    2. compile.sh (the shell script)
    3. fonts/ (a directory for font files - .ttf and .otf)
    4. as/ (a directory for the generated ActionScript)
    5. swfs/ (a directory for the compiled swf files)
    复制代码
    步骤2:准备字体
    一切都是自动的其实是一个善意的谎言。因为(据我所知)你并不能检查一个字体文件是否是常规、加粗、斜体或加粗斜体(Flex可以理解的风格和样式),你必须遵循命名惯例以便Ruby脚本可以解析。你将会使用字体名字创建一个ActionScript类名,因此必须是一个合法的名字。
    以下就是使用Arial字体的例子
    1. Arial.ttf (regular fontWeight and normal fontStyle)
    2. Arial_Italic.ttf (regular fontWeight and italic fontStyle)
    3. Arial_Bold.ttf (bold fontWeight and normal fontStyle)
    4. Arial_BoldItalic.ttf (bold fontWeight and italic fontStyle)
    复制代码
    步骤3:Ruby脚本
    1. require 'find'

    2. # delete any previous as
    3. Find.find( 'as' ) do | as |
    4.   if File.extname( as ) == ".as"
    5.     File.unlink as
    6.   end
    7. end

    8. # generate new as
    9. Find.find( 'fonts' ) do | font |
    10.   if File.file?( font )
    11.     # extension name
    12.     ext = File.extname( font )
    13.     # is font
    14.     if ext == ".ttf" || ext == ".otf"
    15.       # file name
    16.       full_name = File.basename( font )
    17.       name = File.basename( font, ext )
    18.       # font weight and style
    19.       font_weight = ""
    20.       font_style = ""
    21.       if full_name.include? "_Bold" + ext
    22.         font_weight = "fontWeight='bold',"
    23.       end
    24.       if full_name.include? "_Italic" + ext
    25.         font_style = "fontStyle='italic',"
    26.       end
    27.       if full_name.include? "_BoldItalic" + ext
    28.         font_weight = "fontWeight='bold',"
    29.         font_style = "fontStyle='italic',"
    30.       end
    31.       # generate as
    32.       f = File.new("as/#{name}.as",  "w+")
    33.       f.write "package
    34.       {
    35.         import flash.display.Sprite;

    36.         public class #{name} extends Sprite
    37.         {
    38.           [Embed(source='../fonts/#{full_name}', fontName='#{name}', #{font_weight} #{font_style} unicodeRange='U+0000-U+00FF,U+2100-U+214F')]
    39.           public var Font:Class;
    40.         }
    41.       }"
    42.       # some helpful output
    43.       print ',"', "#{name}", '"'
    44.     end
    45.   end
    46. end
    复制代码
    在TextMate中你可以打开脚本按Command-R键运行它。或者借助'ruby convert.rb'命令行运行。一旦脚本被运行就会在'as'目录下生成一个即将被编译的ActionScript文件。

    步骤4:Shell脚本
    1. echo "<h2>Fonts Custom Compile</h2>";
    2. echo "<code> Started @ `date "+%H:%M:%S"`</code><br />";

    3. for i in `cd as/; ls *.as`; do
    4.   swf=`echo $i |  awk -F. '{print $1}'`
    5.   swf="$swf.swf"
    6.   "/Applications/Adobe Flex Builder 3/sdks/3.3.0.4589/bin/mxmlc" -file-specs="as/$i" -o="swfs/$swf" -managers="flash.fonts.AFEFontManager" 2>&1;
    7. done
    复制代码
    需要为脚本指定Flex SDK 位置。我的是"/Applications/Adobe Flex Builder 3/sdks/3.3.0.4589/bin/mxmlc"。-managers="flash.fonts.AFEFontManager是其中暗含的技巧,没有它90%的字体都可以编译,但剩下的就没这么幸运了,所以我们需要添加这个参数。

    TextMate中按Command-R运行脚本,每种字体的编译大约需要5秒钟,我的超过100中字体大约需要10分钟。

    SWF文件被编译并将在运行时加载到Flex程序中。为100多种字体创建ActionScript本来需要很多天才能完成,但是借助少量的脚本只需要不到一个小时。
    1

    评分人数

    • 达达

    达达 赐本贴 威望 + 1 银子 + 20  诏曰:

    More...楼主的最近发帖

    最新精华帖

    好贴,顶了

    TOP

    what's out

    TOP

    返回列表

    江湖传闻 关闭


    天地行·上海讲师招募火热进行中…

    予人玫瑰手有余香,你想成为讲师么?想与更多的精英开发者分享你的经验么? 快来申请9RIA天地行讲师吧!在这里通通满足你! ...


    查看