A Journey Through Font Rendering

In the wide world of programming, there are a few dark corners that many prefer to avoid and instead leverage the well-vetted libraries that are already there. [Phillip Tennen] is not one of those people, and when the urge came to improve font rendering for his hobby OS, axle, he got to work writing a TrueType font renderer.

For almost a decade, the OS used a map table encoding all characters as 8×8 bitmaps. While scaling works fine, nonfractional scaling values are hard to read, and fractional scaling values are jagged and blocky. TrueType and font rendering, in general, are often considered dark magic. Font files (.ttf) are structured similarly to Mach-O (the binary format for macOS), with sections containing tagged tables. The font has the concept of glyphs and characters. Glyphs show up on the screen, and characters are the UTF/Unicode values that get translated into glyphs by the font. Three critical tables are glyf (the set of points forming the shape for each glyph), hmtx (how to space the characters), and cmap (how to turn Unicode code points into glyphs).

Seeing the curtain pulled back from the format itself makes it seem easy. In reality, there are all sorts of gotchas along the way. There are multiple types of glyphs, such as polygons, blanks, or compound glyphs. Sometimes, control points in the glyphs need to be inferred. Curves need to be interpolated. Enclosed parts of the polygon need to be filled in. And this doesn’t even get to the hinting system.

Inside many fonts are tiny programs that run on the TrueType VM. When a font is rendered at low enough resolutions, the default control points will lose their curves and become blobs. E’s become C, and D’s become O’s. So, the hinting system allows the font to nudge the control points to better fit on the grid. Of course, [Phillip] goes into even more quirks and details in a wonderful write-up about his learnings. Ultimately, axle has a much better-looking renderer, we get a great afternoon read, and fonts seem a little less like forbidden magic.

Maybe someday [Phillip] will implement other font rendering techniques, such as SDF-based text renderers. But for now, it’s quite the upgrade. The source code is available on GitHub.

8 thoughts on “A Journey Through Font Rendering

  1. SDF based font rendering would NOT be an upgrade. It is a cheaper technique that trades memory space with cpu/gpu time. It is between sprite based and curve/polygon rendering.

  2. Font rendering is one of the things that I wish was handled by the windowing system on Linux. At least that way there wouldn’t be a font rendering library instance for every GUI based program. *sigh*

    1. For any complex graphics or theming, that would require passing the glyph bitmaps back and forth between windowing system and application. It would also make it harder to port applications to different operating systems or to Wayland.

      What harm do you find from having a font rendering library instance per application? Even the RAM should be mostly shared when most of the applications are using libfreetype.

    2. It certainly used to be.
      Then some people decided they wanted antialiased fonts.
      Then some people decided they wanted RGB error in their antialiased fonts.
      Then some people decided they wanted to pretend their screen wasn’t made of pixels, and made fonts a blurry mess for anyone with a screen below 200dpi.
      So here we are.

      1. “Then some people decided they wanted RGB error in their antialiased fonts.”
        ClearType looks good to me. I don’t notice the small RGB error. But color perception is subjective and some people notice it more than others. So it’s not for everyone. Luckily it can be turned off.

        “Then some people decided they wanted to pretend their screen wasn’t made of pixels, and made fonts a blurry mess for anyone with a screen below 200dpi.”
        Care to explain?

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.