Space Trader: Portability Requires Fonts

Well, it appears that Space Trader didn’t run on Linux. Once I looked inside, it was obvious why: I forced it to use a Windows-like Look-and-Feel, which is only available under JVMs for windows. this took about a minute to fix, but that’s when things really started not to work.

The Winforms version used pixel positioning and sizing, which is based on the size of fonts used by the application. The font used is called Microsoft Sans Serif, which is (As far as I understand) a better-looking version of Tahoma. Both fonts are owned by Microsoft, and are not available on Linux, Mac, or anything else. Using pixel sizing meant that if you don’t have the right font (Which java handles by choosing a default one), the text doesn’t fit the labels it was designed for. Not good-looking at all.

The first thing I did was to add an abstraction layer for the font choosing. The original code used new Font() every time it needed a font – which is OK by itself, but it also specified the family-name, font size and attributes (Bold, Italics, etc.) every time, which was a bit annoying. I’ve created a static collection of usable Font instances – based on the fonts used in the game. Only three or four were used throughout the game, so very simple search-and-replaces did the trick.

Next, I needed a font that is compatible (In terms of glyph size) with Tahoma. My first round of googling turned up Web Developers’ pages, which claim the best choices are Geneva (For the Mac) and Kalimati (For Ubuntu Linux). So naturally, I tried each, until I found something that was installed on the host computer.

That was a stupid move.
When it failed miserably, I did some more research. Kalimati, it appears, is a Devanagari font, which is the script for Hindi, Marathi and Nepali. The reason it was noted as a Tahoma-compatible font is that the author just took glyphs from Tahoma for all the English characters. Copyright issues aside, the font (a) lacks English-looking numerals, and (b) is defiantly not installed by default on western Linuces.

It was now clear that if I want to use any font, I need to bring it with me, and that I want to use a font that is Tahoma-compatible, and freely-distributable. And I found one – in the Wine package, who have already faced and solved the same problem. They created a free font, called Wine Tahoma.
Strangely enough, downloading it to windows and opening with Windows Font Viewer claimed © Microsoft, which was a bit strange. It turned out, the Viewer for some reason used the Tahoma font installed in the system, even tough I specifically handed it the font file. I finally shipped the file to a friend with a Linux station, which could confirm it as the right font.

Packaging a font turned out to be straight-forward – just include the TTF font with your jar, find it with stream = getClassLoader().getResourceAsStream(name), load it (Font.createFont(Font.TRUETYPE_FONT, stream)), and register it (GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font)). You can now use it just like a system font, by calling new Font(familyName) wherever you are.