views:

3419

answers:

4

Imagine you're creating a presentation that will be translated into a dozen different languages, including ones with non-latin characters (Chinese, for instance). The client's branding style guide dictates that certain fonts are acceptable for certain languages. The content itself is required to be externalized, so that the text can be replaced without ever opening a .fla, or re-saving a graphic.

So we're talking dynamic TextField s, populated from XML, with the font swapped out to accommodate the viewer's locale - what's the most painless way to dynamically load only the fonts required (regardless of the filesize), and apply them as necessary? I'm looking for solutions using Flash only, not Flex.

A: 

Well, you'll some how have to know what language you're loading. Autodetection can be quite difficult.

Assuming you know what language the content is you can register fonts to be used in runtime using Font.registerFont(); With this you can load a swf that contains a font, register it, and it's available for future use in all textfields. print("code sample");

[Embed(src="someFont.ttf", fontFamily="myFont", mimeType="application/x-font")]
private var fontClass:Class;

...

Font.registerFont(fontClass);

Note that you'll have to compile using the flex compiler to support the Embed tag.

A good utility in addition is Font.hasGlyphs(), which you can use to check if the characters you are using are available in an embedded fonts. This can be used to either jump back to a system font or display a warning to you as the developer (if you forgot some characters or whatever). Subclassing TextField to do this check automatically may be a good idea.

If this is going to be online, in general embedding Chinese is something you should think about twice. Embedding all characters will increase the filesize a lot and afaik there aren't that many complete Chinese fonts available anyway. The technique above is of course still useful for many other languages.

Antti
auto-detection isn't part of it, luckily - in a situation where it wasn't known, I'd say the best solution would be to try and deduce the likely geographic location by looking at their IP address, the ISP associated with it, then make that language the default on a dropdown list of possible locales
matt lohkamp
What i'm saying here still applies to the question; embedding the fonts for use on runtime with dynamic content. Worked for a recent site with 23 languages anyway..If you're using flash you can still register the fonts and embed using the library. Using flex will make this _much_ easier though..
Antti
I know, I know - believe me, if I could I would. But it's gotta be just Flash, no Flex - how would you go about registering the fonts and embedding using the library in that situation?
matt lohkamp
A: 

First off, make sure you set expectations so that nobody thinks you'll be using embedded fonts in Asian languages like Chinese and Japanese. Unless you plan to load in several megabytes of glyphs, you'll be using device fonts, full stop. As such, it's reasonable to have guidelines on which class of font to use - _sans, _serif, _typewriter or their localized equivalents - but not guidelines on the font per se.

Secondly, in the past, my approach has always been to convince people that in most cases, they're probably better off with device fonts anyway. Using device fonts will cut your development time and avoid bugs. It will make your content load faster (if over the network), and in virtually all cases it will render faster. Often, it will be more clearly legible as well.

Clients like to demand that all their body text be in Garamond-Extra-British or whatever, but make sure they know the costs involved. If they see the light, you'll save yourself significant pain.

fenomas
No offense, but you didn't so much offer an answer, as a recommendation to avoid the problem - and not a very realistic one at that.
matt lohkamp
For instance, let's say your client is Intel - in my experience it's simply not possible to get around using "Neo Sans". Same with Cicso, or HP - the brand style guide is there for a reason, and if you're not willing to make them a project using their fonts, they'll pay someone else to do it instead
matt lohkamp
Whether my answer is realistic is not something you've given me enough information to know. If dynamically loading 5 megabytes of Chinese font is an option that's on the table, then you're not making very typical flash content.
fenomas
As for the rest, I'm familiar with big companies and brand guides, but it's reasonable - nay, preferable - to explain that there are costs involved that their brand guide doesn't take into account. If they want to pay those costs, fine; in my experience they often will not.
fenomas
fair enough, it's obvious we've had pretty different experiences with this - but for the moment, let's assume that the fonts mandated by the client are non-negotiable, and that they're willing to sacrifice load time to that end.
matt lohkamp
I guess I'll re-word the question for future clarification.
matt lohkamp
+1  A: 

This is a bit of a hassle in flash, but it is entirely possible. What you will need to do is to "embed" the font inside a separate swf, load this swf from your main application and then register that font inside that swf aswell. This will allow you to do runtime loading of fonts. This will require you to either specify the font per language in the application or in whatever files you store the translations in, which perhaps isn't perfect, but neither is embedding all fonts in your main application. That would amount to possibly hundreds of kb of fonts loaded for no reason.

This blog post has a good rundown of what you need to do to get it working. I have done this myself too, so if you're interested I can expand this post with some examples later.

grapefrukt
I'd love some examples! I ran across that blog post too, and it looked like it was what I needed... but I'm not great at abstract code concepts - I really need to see something solid and play with it before I understand it.
matt lohkamp
+1  A: 

I'm totally disappointed that no-one knew about this - oh well. I finally found someone who'd written a little class to handle the font-loading process, and gave directions on how to create the .swfs for it to load:

Flash AS3 Loading Fonts

Hopefully other people'll find this helpful - it turned out to be the magic bullet for my problems.

matt lohkamp