Friday 18 April 2008

Unicode, font, SDL and graphics.

I have just added the feature to allow people to configure the games in STPPC2x. On the way, knowing that my graphical design skills are somewhere in the realm of a small five-year-old with a large crayon, I wanted a quick way to "pretty" up the interface without having to draw anything myself.

I needed a checkbox, which is quite an easy thing to draw anyway. I wanted one that I could tick, cross or leave blank and which wouldn't look like I'd drawn a square box around a poorly fitting line-based cross or tick.

In the end, I looked at what resources I already had included in the project (allowing code re-use, not to mention keeping necessary dependencies to a minimum) and I already had SDL_ttf and the DejaVu font. The font was a random choice because I just wanted a "free to distribute" font that was small and easy to read, but it turned out to work very well.

Hidden away in the depths of the Unicode specification, there are a multitude of symbols that hardly anyone ever uses or even sees. These, apparently, include things like several styles of cross/tick (including "ballot" styles, made to look like a hand-drawn cross/tick), chess figures, dominoes, dice, and all manner of symbols, logos and signs. The mathematical symbols were also out of this world, line-drawing characters, "disabled" symbols, you name it. There are even circled numbers and letters, which prevent having to "guess" how big a number is going to render, fudging the font size and then circling it manually.

As it turned out, the DejaVu fonts have remarkably good Unicode support (the only other real competitor that I have personally seen is Arial Unicode, but obviously there are distribution problems with that one) and included all of the symbols that I felt like using.

But someone could easily write a board game, or avoid spending hours recreating a symbol in a vector format, using the Unicode characters, precise sizing and the DejaVu font. And, obviously, being a font it scales well to all sizes, can be coloured etc to your heart's content and can be rendered inline with text without causing problems. And the DejaVu fonts only take up half-a-megabyte, if that, and contains thousands of pieces of "clipart".

So, with SDL and SDL_ttf, it was simply a matter of changing my conservative SDL_RenderTextSolid to SDL_RenderUTF8Solid and figure out how to include the characters in a C string (Gucharmap on Slackware Linux was a big help here because it gives you the C escape codes for the UTF8 versions of any character in a Unicode font). No more memory is used, no more code is needed if you already have font-rendering code in there, and only a tiny amount of disk space is used for the whole thing. It was a great find and it's improved the look of the menu I was writing immensely.

No comments: