← All writings

Writing · Ep. 8 · 0x0B18–0x0BF8 · Reverse engineering

Devlog #3: English broke the font renderer, so I rebuilt it

The original font atlas only has fixed-width glyphs. Here's how I added a proportional Latin font for the OreGairu English Patch.

The problem

Japanese text looks fine in a monospaced grid: every kana and kanji is the same width. English does not. With the stock font, i takes as much room as W, and a single line of dialogue overflows the text box.

A fixed-width glyph grid next to a proportional one
Left: the stock grid. Right: the rebuilt atlas with per-glyph advances.

The fix is to give every glyph its own advance width and teach the renderer to use it.

Reading the glyph table

Each entry in the font table is eight bytes. I only needed three fields:

struct Glyph {
    uint16_t codepoint;
    uint16_t atlas_index;
    uint8_t  width;     // always 24 in the stock font
    uint8_t  height;
    int8_t   bearing_x;
    uint8_t  advance;   // the field we actually care about
};

Field

Stock value

After the rebuild

width

24

6–24

advance

24

glyph width + 1

bearing_x

0

−1 to 2

Measuring a line

A line now fits when the sum of its advances stays under the box width:

∑i=1nadvance⁡(ci)≤Wbox\sum_{i=1}^{n} \operatorname{advance}(c_i) \le W_{\text{box}}

With the stock font that sum was simply 24n24n, which is why short English lines still overflowed.

Next

Text-box reflow: wrapping on word boundaries instead of character counts. The atlas rebuild itself now lives in Font Atlas Studio.