Search code examples
fontsrenderingvector-graphicstext-rendering

What basic primitives my render engine needs to support for drawing OTF fonts?


I am writing my own render engine for a very specific use case and I need to be able to draw a text. At first, I thought that it would be easier to create my own font file where I would by hand define each character using primitives that my engine supports, but after some time, I thought that maybe it is possible to use a standardized vector fonts format such as OTF.

Currently, I am able to render:

  • Point
  • Line
  • Circle line/Filled Circle
  • Triangle

So before jumping into the coding, first I want to know what OTF is made from (What basic primitives I need to add to my project to render these fonts).


Solution

  • The OpenType spec uses vector formats to describe glyph outlines. There are two different vector formats supported:

    • TrueType outlines use quadratic beziers. See here for an overview. Apple's TrueType spec provides more details on the quadratic beziers. TrueType outlines are stored in a 'glyf' table.

    • Compact Font Format, which is based on PostScript, uses various drawing commands and uses cubic beziers for curves. CFF outlines are stored in a 'CFF ' or 'CFF2' table. In terms of graphic primitives, CFF and CFF2 formats are comparable.

    You need to implement scan conversion which determines the pixels that are "on" for a given size.

    You also need to process hints. For TrueType, this is like implementing a small Turing machine that interprets op codes and data from the font file and manages a stack to tune the outline before scan conversion.

    You may find the FreeType project useful in understanding what is needed. Or, you can save a lot of time and just use FreeType.