Yet Another Font Class

Inspired by Bortels work and the idea of rendering characters to images for faster drawing, I played around with taking a bitmap font and converting it to a family of Codea images. I picked BDF for my starting format (fontforge can convert anything to that) and have some scripts that will convert a font file that is in BDF format into a lua data structure, together with a class file that does the actual rendering.

Buried in the code is a library of conversion routines between decimal, hexadecimal, binary, and octal. I found all but the octal on the internet at http://www.dialectronics.com/Lua/code/BinDecHex.shtml (licence allows modification and reuse); adding octal support was the work of moments. I also had to extend the inbuilt string.gfind to unicode as when rendering a string then one wants to examine it character by character and not every character is a single byte.

Included are three fonts from the standard Linux X11 distribution: the prosaically named 8x16 and 10x20, and a proportional font that is a Times Roman font (just to show that it can be done, and done easily). (Actually, the 8x16 was the one that caused the most problems as its encoding is just weird.)

These are bitmap fonts so don’t scale well, but it’s easy enough to generate the bitmaps at a variety of heights. I’ve tried to make it so that the code isn’t too bloated with unnecessary stuff if one doesn’t use all the fonts, or all the glyphs in a font, but it would be nice to be able to conditionally include a file!

The code is at http://www.math.ntnu.no/~stacey/HowDidIDoThat/iPad/Codea.html

465k! Woof. Nice fonts, though, and good to have something that can take arbitrary fonts from other places if need be, as well as support non-ASCII characters; Codea has a surprisingly large (to me) Non-American fan base. The size really does point out the need for Codea to be able to handle binary files of some type - either from the web via socket, or imported, or whatever.

Codea has a surprisingly large (to me) Non-American fan base. 

What’s so surprising about it? We are, after all, the 99%… :slight_smile:

Dunno. It shouldn’t be surprising. It’s like when you realize most really good comedy movie and tv performers are actually Canadian. :slight_smile:

Thank you, Andrew. Now we have more fonts options. :slight_smile:

There are hundreds of beautiful bitmap fonts with various sizes and free licensed as well, on the net. Would you tell us how to import them into Codea and then use them with your class? I’d like to try. TIA.

It’s quite simple.

  1. Convert your font to BDF. Fontforge can do this via the “Generate Fonts” option in the file menu. There’s a fontforge script on my Codea page which will do this from the commandline (though it doesn’t - yet - contain an option for setting the size, so for that you’d have to work in fontforge itself).

  2. Run the perl script bdf2lua.pl on the resultant BDF file (bdf2lua.pl font.bdf) and capture the output. Until we get draggable tabs, the filename of the output should come after Font lexicographically (I use Font<fontname>.lua). This is almost the complete font file.

  3. The missing part is the first line, which should be of the form Font["font name"] = function().

I guess the perl script could add that if given a name on the command line. The perl script could probably also call fontforge as a subprocess. I’ll make these changes sometime soon, but for now the above is what I do.

Bortels: Most of that 465k is from the Times Roman font (318k). It has 902 glyphs, compared with just over 200 for the other two (separately). Plus its glyphs are bigger (38 x 48) so need more data to define them. Just be glad that I didn’t put the STIX fonts in …

BinDexHex was a good find