[新闻资讯] 借助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:配置
我使用的目录结构如下:- convert.rb (the ruby script)
- compile.sh (the shell script)
- fonts/ (a directory for font files - .ttf and .otf)
- as/ (a directory for the generated ActionScript)
- swfs/ (a directory for the compiled swf files)
复制代码 步骤2:准备字体
一切都是自动的其实是一个善意的谎言。因为(据我所知)你并不能检查一个字体文件是否是常规、加粗、斜体或加粗斜体(Flex可以理解的风格和样式),你必须遵循命名惯例以便Ruby脚本可以解析。你将会使用字体名字创建一个ActionScript类名,因此必须是一个合法的名字。
以下就是使用Arial字体的例子- Arial.ttf (regular fontWeight and normal fontStyle)
- Arial_Italic.ttf (regular fontWeight and italic fontStyle)
- Arial_Bold.ttf (bold fontWeight and normal fontStyle)
- Arial_BoldItalic.ttf (bold fontWeight and italic fontStyle)
复制代码 步骤3:Ruby脚本- require 'find'
- # delete any previous as
- Find.find( 'as' ) do | as |
- if File.extname( as ) == ".as"
- File.unlink as
- end
- end
- # generate new as
- Find.find( 'fonts' ) do | font |
- if File.file?( font )
- # extension name
- ext = File.extname( font )
- # is font
- if ext == ".ttf" || ext == ".otf"
- # file name
- full_name = File.basename( font )
- name = File.basename( font, ext )
- # font weight and style
- font_weight = ""
- font_style = ""
- if full_name.include? "_Bold" + ext
- font_weight = "fontWeight='bold',"
- end
- if full_name.include? "_Italic" + ext
- font_style = "fontStyle='italic',"
- end
- if full_name.include? "_BoldItalic" + ext
- font_weight = "fontWeight='bold',"
- font_style = "fontStyle='italic',"
- end
- # generate as
- f = File.new("as/#{name}.as", "w+")
- f.write "package
- {
- import flash.display.Sprite;
- public class #{name} extends Sprite
- {
- [Embed(source='../fonts/#{full_name}', fontName='#{name}', #{font_weight} #{font_style} unicodeRange='U+0000-U+00FF,U+2100-U+214F')]
- public var Font:Class;
- }
- }"
- # some helpful output
- print ',"', "#{name}", '"'
- end
- end
- end
复制代码 在TextMate中你可以打开脚本按Command-R键运行它。或者借助'ruby convert.rb'命令行运行。一旦脚本被运行就会在'as'目录下生成一个即将被编译的ActionScript文件。
步骤4:Shell脚本- echo "<h2>Fonts Custom Compile</h2>";
- echo "<code> Started @ `date "+%H:%M:%S"`</code><br />";
- for i in `cd as/; ls *.as`; do
- swf=`echo $i | awk -F. '{print $1}'`
- swf="$swf.swf"
- "/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;
- 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 两
诏曰: