Learn: position the mask, then blend it
The foreground color and the glyph’s mask are separate. Each grayscale sample represents coverage, not an RGB color. When the canvas y-axis points down, a bitmap with bearing bitmap_top starts at baseline_y - bitmap_top. Its x position starts at the pen plus bitmap_left; the next pen comes from advance, not from the bitmap’s right edge.
Run it: mix one opaque channel
./build/examples/core-lab gray-coverage
void gray_coverage() {
constexpr unsigned fg = 255, bg = 0;
for (unsigned coverage : {0u, 128u, 255u}) {
const unsigned mixed = (fg * coverage + bg * (255 - coverage) + 127) / 255;
std::cout << coverage << " -> " << mixed << '\n';
}
}
Expect 0 -> 0, 128 -> 128, 255 -> 255. Try it: swap foreground and background; full coverage now produces 0. The +127 rounds the division by 255 to nearest for these nonnegative integers. This toy mixes one channel; it is not a glyph renderer.
Build it: record and paint clipped glyph masks
Your challenge: Draw an ASCII line using the measurements from Stage 16. Record enough data that replay doesn’t depend on what face->glyph happens to contain later.
Load the glyph with FT_LOAD_RENDER or render the loaded outline with FT_Render_Glyph. For this course accept FT_PIXEL_MODE_GRAY; validate mask dimensions, pitch, and coverage levels. Copy rows to owned normalized coverage storage before loading another glyph. pitch is a signed row byte step, not width; read the bitmap contract before copying, and either handle negative pitch deliberately or reject it with a clear diagnostic. For non-256 num_grays normalize to 0..255 or reject the profile explicitly.
Use the same advances and baseline as measurement; keep text clips from the layout/scene. Compute the visible destination rectangle first, then derive the corresponding source mask offsets. Blend each RGB channel over the opaque buffer. A space can advance without adding a paint command. Do not start an atlas, cache, shaping engine, or Unicode fallback in this stage.
Check it
Blend synthetic coverage 0, 128, 255 over RGB (0,0,0) with white foreground; expect per-channel values 0, 128, 255. Clip a known mask at the left and bottom and verify source offsets and sentinels. Load two different glyphs before replay and confirm the first has not changed. Measure and paint "A A" with the same face/size; test that ink has a gap where the space advanced. Compare invariants, not compositor screenshots.
python3 course/check_core.py text-paint
Hints and troubleshooting
Hint: all letters look like the last one, or the text moves up unexpectedly
Check whether you saved a pointer owned by the reusable glyph slot instead of copying its pixels. Print baseline, bitmap bearings, and computed top-left for one glyph before debugging blending.