HarfBuzz中hb_position_t类型所使用的具体单位是什么?
hb_position_t Units in HarfBuzz Great question! Let's break this down clearly so you have a solid grasp:
- The
hb_position_ttype (which is just an alias forint32_t) in HarfBuzz uses a 26.6 fixed-point number format. Put simply, each unit represents 1/64 of a pixel (or more accurately, 1/64 of your target device's base coordinate unit—this is almost always pixels when working with libraries like FreeType, as in the example you looked at).
Why this 1/64 unit?
This fractional format is a smart choice for typography libraries:
- The 6-bit fractional part gives enough precision for smooth glyph kerning, positioning, and scaling without relying on slower floating-point math.
- When paired with FreeType, the font's native design units (based on its EM size) get scaled directly into this 26.6 format, making it easy to map to on-screen pixel positions.
Proof from the Example Code
If you look closely at the hello-harfbuzz-freetype.c sample, you'll see concrete confirmation: the code converts hb_position_t values to actual pixel positions by dividing by 64. Here's a simplified snippet that shows this:
int x_pos = 0; for (int i = 0; i < glyph_count; i++) { hb_glyph_position_t *pos = &glyph_positions[i]; // Convert offset from 26.6 fixed-point to pixels int draw_x = (x_pos + pos->x_offset) / 64; draw_glyph(draw_x, y_pos); // Advance position using the scaled advance value x_pos += pos->x_advance; }
This division by 64 is the smoking gun—without it, the glyph positions would be 64x too large, which confirms each hb_position_t unit is 1/64 of a pixel.
Quick Side Note
While pixels are the most common target, if you're working with vector graphics or high-DPI displays, you might adjust the scaling factor—but the core unit of hb_position_t remains 1/64 of whatever base coordinate unit you're using.
内容的提问来源于stack exchange,提问作者bb94

