今天在研究mornUI,结果看到Swift-tool ,所以研究了一下。
结果按照wiki里面的操作,字体就是报错,折腾了好久。发现原来要用绝对的资源路径。
比如:
<?xml version="1.0" encoding="utf-8"?>
<lib allowDomain="*">
<bitmapdata file="img/tank.png" class="tank"/>
<bitmapdata file="img/pic.jpg" class="pic"/>
<font file="G:\test\f\YouYuan.ttf" class="ArialFont"/>
</lib>
https://code.google.com/p/swift-tool/
Swift is a tool for packaging images, fonts, sounds, binary data to swf as runtime shared library or swc as precompiled library. You can load and access these resources at runtime, such as RSL(runtime shared library). Swfit is a free software written by Java, so the Java runtime is required.
Swift has two ways to use. The first is xml2lib, you can use it to compile resources specified in a xml. The other is dir2lib, you can use it to compile resources in a specified directory.
Swfit可以帮助你把图片、字体、声音、二进制数据等资源打包成一个swf文件(或者swc文件),你可以在运行时动态加载并访问这些资源,实现运行时共享库,减少主应用程序体积和加速下载。Swfit是一个Java开发的免费工具,因此你需要安装Java虚拟机才能使用它。
Swift有两种使用方式,一是xml2lib,顾名思义,它可以根据指定的xml文件内容编译成swf库文件。二是dir2lib,它则可以把指定文件夹中的所有有效资源编译成swf库文件。
Usage:
下面是一个xml库文件例子:
<?xml version="1.0" encoding="utf-8"?>
<lib allowDomain="*">
<bitmapdata file="image/img1.png" class="PNGBitmapData"/>
<bitmapdata file="image/img2.jpg" class="JPGBitmapData"/>
<bitmap file="image/img3.jpg" quality="80" class="JPGBitmap"/>
<bitmap file="image/img4.png" compression="true" quality="80" class="PNGBitmap"/>
<sprite file="image/img5.png" class="PNGSprite"/>
<sprite file="image/img6.jpg" class="JPGSprite"/>
<flexmovieclip file="image/img7.jpg" class="FlexMC"/>
<flexcontainer file="image/img8.jpg" class="FlexContainer"/>
<font file="Arial.ttf" unicode="u+0020-007e,u+00a0-036f" class="ArialFont" />
<font file="msyh.ttf" char="微软雅黑" charRange="a-z,A-Z"
unicode="u+5fae,u+8f6f,u+96c5,u+9ed1,u+4e00-u+4eff" class="MsyhFont" />
<sound file="sound/test.mp3" class="TestSound"/>
<bytearray file="data/test.xml" class="XMLData"/>
</lib>
支持资源类型有:
注意事项:
Usage:
注意事项:
下面简单说明一下如何使用动态库:
加载跟普通swf并无差别,值得注意的是在不同域下要考虑跨域安全问题,请参考相应文档。
var loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
loader.load(new URLRequest("library.swf"), context);
加载的library的类定义都可以通过其ApplicationDomain的getDefinition()方法获得,例如:
var BmpDataClass:Class = loader.content.loaderInfo.applicationDomain.getDefinition("JPGBitmapData") as Class;
var bmp:Bitmap = new Bitmap(new BmpDataClass());
addChild(bmp);
共享字体使用示例:
var fontClass:Class = loader.content.loaderInfo.applicationDomain.getDefinition("MsyhFont") as Class;
Font.registerFont(fontClass);
var fonts:Array = Font.enumerateFonts();
var font:Font = fonts[0];
var fontName:String = font.fontName;
var tf:TextField = new TextField();
tf.embedFonts = true;
tf.defaultTextFormat = new TextFormat(fontName);
addChild(tf);