`

as3运行时加载字体

    博客分类:
  • AS
阅读更多

http://nochump.com/blog/?p=20。还有个想法是,css嵌入的字体编译成swf.相当于动态修改css来改变字体

 

 

<!-- mid column --> <!-- header -->

my density has bought me to you

nochump.com

<!-- top tab navigation --> <!-- /top tabs -->
<!-- /header --> <!-- mid content -->

AS3 Runtime Font Loading

 

Here’s a little experiment with loading fonts dynamically at runtime. So what’s this good for? Well let’s say you have a flash application where you want to allow users to customize the text formatting for some text box. Typically you would provide a small set of fonts which would all get embedded into the swf but this isn’t very flexible or ideal. Changing font’s or adding new one’s require rebuilding the swf. Also if only one or two fonts get used then all the other extra fonts do nothing but bloat the swf size. For this situation, it would be ideal to externalize the fonts and only load in the one’s needed.

Flash never supported this feature but some developer’s found ways around it. In particular, there was Shared Fonts Manager . However the author’s approach, although very impressive and useful, was a hack and not the easiest to use. With the major changes in flash 9, I got curious if runtime font loading was easier to do now and fortunately it is.

Approach

This isn’t new or anything, apparently all assets embedded in a swf become associated with a class and the ApplicationDomain object is the way to access classes in other swfs. The Flex documentation describes these topics but here, the focus is on fonts.

So first off, create the external font asset swf with something like the following:

package {

	import flash.display.Sprite;

	public class FontLibrary extends Sprite {

		[Embed(systemFont='Bank Gothic', fontName='emBank Gothic', mimeType='application/x-font')]
		public static var BankGothicFont:Class;

	}

}

Keep track of the class names you use and the fontName attribute of the Embed statement as that will be font name you’ll have to use in a text format. You can embed as many fonts within the swf as you like. I won’t describe the other features of the embed statement as they have been pretty well covered elsewhere.

After the swf is built (for this example let’s call it fontlibrary.swf), you load it like any normal swf via a flash.display.Loader object. After it is loaded, you just need to register the font class associated with the embedded font. Here’s an example of loading and registering the font library swf from above.

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader.load(new URLRequest("fontlibrary.swf"));

private function completeHandler(event:Event):void {
	var FontLibrary:Class = event.target.applicationDomain.getDefinition("FontLibrary") as Class;
	Font.registerFont(FontLibrary.BankGothicFont);
	var tf:TextField = new TextField();
	tf.defaultTextFormat = new TextFormat("emBank Gothic", 12, 0);
	tf.embedFonts = true;
	tf.rotation = 15; // to demonstrate the embed worked
	tf.text = "blah blah blahnblah blah blahnblah blah blah ";
	addChild(tf);
}

If you’d like to grab a little demo with source, here’s a download .

More Thoughts

Another approach for the font asset swfs is to keep the main application class empty and include specific font classes thru the -source-path and -includes compiler options. A font class would then look something like this:

package {

	import flash.text.Font;

	[Embed(systemFont='Bank Gothic', fontName='emBank Gothic', mimeType='application/x-font')]
	public class BankGothicNormalFont extends Font {}

}

With this it won’t matter what the definition name of the main application class is (which was “FontLibrary” in the earlier example) or would you need to know the name of the static property associated with the font, you would just register the included font class such as “BankGothicNormalFont”. I prefer this approach better because of this and that it gives a little more control over to my build scripts. Say you want a font swf to contain more than one font, with the first approach you’ll need to edit the main application class to add the embeds but here you can just edit your build script to include the other font classes (assuming they’re already written).

Another thought was using the component compiler (compc). With compc you can embed non-source files directly however I have not tried it with fonts. It doesn’t seem like it gives you controls over the font style, weight, or other attributes available with the embed syntax. I played with some other options but none seem that great.

There are also runtime shared libraries (RSL) and modules with 2.0.1. I haven’t used them so if I’m wrong please let me know but from my understanding, RSLs do externalize assets and load at runtime however they are loaded when the swf loads. There is no control in actionscript for the loading of RSLs it appears. As for modules, I’m guessing they would be the better approach but not sure what advantages they offer over just using the ApplicationDomain object. Perhaps it has some management features I dunno. I can see a problem when loading and registering the same font swf twice. Well I’ll have to look into using modules.

分享到:
评论

相关推荐

    AS3动态加载字体

    在ActionScript 3 (AS3)中,动态加载字体是一种技术,允许开发者在应用程序运行时加载和使用新的字体。这可以极大地提高应用的灵活性,尤其是对于那些需要展示各种独特字体或者用户可定制字体的应用。本知识点将深入...

    runtime-font-loader-as3:运行时字体加载器 as3

    `runtime-font-loader-as3`是一个针对AS3开发的库,用于在运行时动态加载字体资源,从而提高应用程序的响应速度和用户体验。这个库是ASC2(ActionScript Compiler 2)的优化版本,由ru.etcs.utils.FontLoader提供...

    office 2007 加载项 (save as pdf)

    在转换过程中,“Save as PDF”加载项会尽可能保留原始文档的样式、字体和图像质量。这对于那些需要保持设计一致性或希望确保接收方查看文档时无误的用户来说非常有价值。例如,设计师可能希望确保他们的布局在不同...

    FLASH AS3 loading 源文件 源代码

    在Flash AS3编程中,加载(Loading)是关键功能之一,允许开发者动态地在应用程序运行时引入外部资源,如图像、SWF文件、XML或文本数据。本资源包含了一个名为"loading_as3"的示例项目,其中包括了loading_as3.fla...

    flash as3 苹果菜单

    在本项目中,我们关注的是使用ActionScript 3(AS3)在Flash环境中开发的一款类似于苹果操作系统的菜单。ActionScript是Adobe Flash Professional和Flash Player支持的编程语言,主要用于创建交互式内容、动画以及富...

    AS3官方图文混排包

    5. **动态内容加载**:在运行时动态添加、删除或更新图文内容。 6. **性能优化**:通过优化的渲染引擎,确保在大量图文数据下的流畅显示。 综上所述,AS3官方图文混排包是Flex 3开发中的重要工具,它扩展了标准Flex...

    as3比较详尽的工具类

    tryRun.as ---------------------- try catch运行函数 url: Address.as ---------------------- 设置为首页/添加收藏夹/获取地址栏地址并且复制到右键 URL.as ---------------------- 跳转html/获取文件地址 ...

    用AS3写的滚动字幕

    标题中的“用AS3写的滚动字幕”是指使用ActionScript 3.0,这是一种基于ECMAScript的脚本语言,广泛应用于Adobe Flash Professional和Flex框架中。ActionScript 3.0是Flash Player支持的主要编程语言,它为创建交互...

    网络监测代码as3

    3. **assets**:这个目录通常包含应用程序所需的静态资源,如图片、字体或声音文件。在监测网络性能时,如果应用涉及到加载这些资源,我们可能需要关注它们的加载时间和效率。 在网络监测AS3的实践中,以下是一些...

    falsh.Programming.Description.Pane.AS3.code.rar_as3

    3. **文本字段(TextField)**:用于展示描述内容,AS3中的TextField类是处理文本的核心,可以设置字体、大小、颜色,以及是否可编辑等属性。 4. **事件处理**:为了实现交互性,你需要监听用户的行为,如点击按钮...

    kivy中文支持全局替换微软雅黑替换字体.rar

    2. **加载字体**:在Kivy应用中,你可以使用`kv`语言或Python代码来加载字体。在`.kv`文件中,可以设置`&lt;Label&gt;`或其他文本类的全局样式,例如: ```kv Label: font_name: 'path/to/微软雅黑.ttf' ``` 或者在...

    MP3跨域音频频谱AS3播放器

    5. **其他资源**:可能包括图片、字体或其他支持播放器运行的素材。 总的来说,MP3跨域音频频谱AS3播放器是Web开发中的一个创新工具,它利用AS3的强大功能,结合频谱分析,为用户提供了既美观又实用的音乐播放体验...

    AS3的常用类和函数大全

    ### AS3的常用类和函数大全 在Adobe Flash平台中,ActionScript 3(简称AS3)是一种广泛使用的编程语言,适用于创建交互式应用程序、游戏和动画等内容。本篇文章将根据给定的信息来深入探讨AS3中的一些常用类和函数...

    Flash加载swf文件的沙箱问题

    在标签“源码”中,我们可以查看到`LoadEmbedFont.as`文件,这很可能是一个用于加载并嵌入字体的AS3(ActionScript 3)脚本。在处理字体加载时,沙箱问题同样存在。如果SWF需要加载外部的字体资源,它必须确保正确...

    flash actionscript3游戏开发之 5种将fla中的资源嵌入到Flex或者纯AS3项目中的方法.zip

    如果不希望在编译时嵌入SWF,可以选择在运行时动态加载。使用 `Loader` 类可以实现这一功能: ```as3 var loader:Loader = new Loader(); loader.load(new URLRequest("myResources.swf")); loader....

    as3.0 cookbook 完整版

    - **文本渲染**:AS3提供了丰富的文本渲染功能,包括字体样式、颜色和排版选项。开发者可以根据需求定制文本的外观。 - **富文本支持**:富文本支持使得AS3能够处理包含多种格式和样式的文本内容,这对于制作复杂的...

    AS3 Starling 中文基础教程

    ### AS3 Starling 中文基础教程 #### 一、Starling 概述 **Starling** 是一个基于 **Stage3D** 技术构建的 **2D** 开发框架,适用于 **ActionScript 3 (AS3)**。它利用 **GPU** 加速特性,简化了 **Stage3D API** ...

    as读取android源码

    AS会自动识别项目结构并加载所有模块。 在导入过程中,可能会遇到编译问题,如缺少SDK版本或NDK。此时,你需要在AS的设置中配置相应的Android SDK和NDK路径。进入`File` &gt; `Project Structure`,在`SDK Location`中...

    Python matplotlib修改默认字体的操作

    # 加载字体 my_font = fm.FontProperties(fname='C:\\Windows\\Fonts\\YaHei.Consolas.1.12.ttf') # 设置图例字体 plt.legend(loc='best', prop=my_font) plt.show() ``` 这段代码首先导入必要的模块,并准备数据...

Global site tag (gtag.js) - Google Analytics