This section demonstrate algorithms which use the concepts previously defined to render text, whatever layout you use. It assumes simple text handling suitable for scripts like Latin or Cyrillic, using a one-to-one relationship between input character codes and output glyphs indices. Scripts like Arabic or Khmer, which need a ‘shaping engine’ to do the character code to glyph index conversion, are beyond the scope (and should be done by proper layout engines like Pango anyway).
In this first example, we will generate a simple string of text in the Latin script, i.e., with a horizontal left-to-right layout. Using exclusively pixel metrics, the process looks like:
Note that kerning isn't part of this algorithm.
This algorithm can be used for hinting modes that don't apply horizontal hinting. It essentially provides WYSIWYG text layout. Text rendering is very similar to the algorithm described in subsection 1, with the following few differences:
The pen position is expressed in fractional pixels.
The hinted outline must be shifted horizontally to the proper sub-pixel position.
The advance width is expressed in fractional pixels, and isn't necessarily an integer.
Here an improved version of the algorithm:
Adding kerning to the basic text rendering algorithm is easy: When a kerning pair is found, simply add the scaled kerning distance to the pen position before step 4. Of course, the distance should be rounded in the case of algorithm 1, though it doesn't need to for algorithm 2. This gives us:
Algorithm 1 with kerning:
Algorithm 2 with kerning:
The process of laying out right-to-left scripts like (modern) Hebrew text is very similar. The only difference is that the pen position must be decremented before the glyph rendering (remember: the advance width is always positive, even for Hebrew glyphs).
Right-to-left algorithm 1:
The changes to algorithm 2, as well as the inclusion of kerning are left as an exercise to the reader.
Laying out vertical text uses exactly the same processes, with the following significant differences:
The baseline is vertical, and the vertical metrics must be used instead of the horizontal one.
The left bearing is usually negative, but this doesn't change the fact that the glyph origin must be located on the baseline.
The advance height is always positive, so the pen position must be decremented if one wants to write top to bottom (assuming the Y axis is oriented upwards).
Here is the algorithm:
Last update: 13-Feb-2018