I have a lot of ideas for blog posts that are on my generic "to do" list.

In fact, any time someone suggests a potential topic these days, I already had the topic on my list of things to cover some day....

I was looking at my blog summary page a moment ago and I realized that this is going to be blog post #1708.

Apropos of nothing, you might be thinking. But I'll explain why this was interesting to me.

You see, I tend to think that there are a few core posts that I do which have a lot more to do with real influencing/assistance, like The jury will give this string no weight or the Converting a project to Unicode series or the Private fonts: for members only post or the Getting all you can out of a keyboard layout series.

I have been building this one up in my mind for a while now -- in fact, since I first talked about it last year in Font embedding -- the intro: a sample that really shows how font embedding can work. I hadn't gotten to it yet, but it was on the list.

Then a few days ago Scott Hanselman asked me:

I’ve seen the Custom Culture stuff, but I’m wondering if anyone’s done a sample (and with what font) showing Amharic on Vista? I’d like to post about it and enable some Ethiopians.

I had to remind him that we actually added Amharic as a locale to Vista (as I sometimes have to do with Scott!), and it did suggest to him something that really might be important:

Hm…I’ll try making a WinForms app in Amharic…I’ll let you know. Since Vista [h]as am-ET I guess we don’t need it…although, it’d be nice to talk about how to write a WinForms app that is ONE SOURCE, TWO OS’s. Meaning, it would know what to do on XP vs. Vista. Can we copy the font over to XP?

That “straddling” sample would be VERY valuable for those languages that were added in Vista.

Now copying the font file for Nyala is indeed a violation of the EULA, even to another Windows box. But it suddenly occurred to me that this might be the perfect time to provide a font embedding sample!

After a bunch of work between other work and meetings and email and such (and by the way special thanks to Sergey Malkin and David Brown for their assistance here!).

Warning: do not violate the license for any font file from Microsoft or any other source. You can use the licensing information in the Font Properties Extension to find out if you are allowed to do it!

First a few gratuitous screen shots of the sample, on Vista (with higher DPI settings):

and on Server 2003 (which does not have the font or the locale or anything, and with ClearType turned off):

and on XP SP2 (again without the font or the locale or anything, and with ClearType turned on):

Notice how the bottom TextBox control does not show the text on the platforms that do not have the font, while all of them can display the text in that one on top.

And in fact if you used a custom culture to add am-ET, also known as Amharic (Ethiopia) or አማርኛ (ኢትዮጵያ), one can get even more of the support running on both platforms, just as Scott was hoping for!

Ok, enough with the build-up, let's jump in....

You can download the project from here. It basically relies on a few of the font embedding API functions:

  • TTEmbedFont -- given a device context containing a specific font that is legal to embed, creates the compressed binary file that can be embedded;
  • TTLoadEmbeddedFont -- given that compressed file, uncompresses it and turns it into a font that can be used within the process;
  • TTDeleteEmbeddedFont -- removes the embedded font's information when you are done with it.

The sample was a bit more involved as it had to make use of the PrivateFontCollection class to load the font within WinForms, because the load is only valid within the process but GDI+ does not load any font that is not known to it at the time it has started up. Luckily, by using a technique similar to the one I used in Private fonts: for members only, you can load up the font that is ready to go in GDI/Uniscribe and cause it to be available to your managed application controls as well!

The logic is:

  • On >= Vista, if the embedded font file has not there, it is created.
  • On all platforms, it tries to use the font, loading it up into a private name so it won't have trouble loading on platforms that contain the font.

NOTE: The sample download does NOT include the binary file containing the embedded font file. To get that file you have to run the sample on Vista and it will create a ~150kb file named "NyalaSIAO.bin" in the same directory as the EXE. From there you can put the EXE and the .BIN file on the downlevel machine and display Amharic in your application to your heart's content, provided you are just using it in your application.

In the real world you probably would not set up your application the way I did the sample -- you would probably embed the font as a resource like that font about private fonts did, and you'd likely only create it during development, not on the user's machine later. But it should be enough to get you started....

I will talk more about the code soon (and the embedding support and what happens with it) in an upcoming post. :-)

And I'll probably do an unmanaged sample too, at some point. Because I knew even 15 years ago that when someone at Microsoft talks about how easy something is to use, if they provide no samples for it, even after years pass, that we might well be full of crap and that it is hard.

 

This post brought to you by (U+12a2, a.k.a. ETHIOPIC SYLLABLE GLOTTAL I)