Remote TextPass, merged with UIPass

This commit is contained in:
Dynamitos committed 2026-10-20 22:01:51 +02:00
1 parent 332c47fc9d
commit 17dc1e4d88
11 files changed
+191 -179

No files matched your search

+29 -5
View File
@@ -45,11 +45,15 @@ UVector2 FontAsset::shapeText(std::string_view view, uint32 fontSize, Array<Rend
uint32 num_glyphs;
hb_glyph_info_t* glyphs = hb_buffer_get_glyph_infos(buf, &num_glyphs);
hb_glyph_position_t* positions = hb_buffer_get_glyph_positions(buf, &num_glyphs);
Vector2 min = Vector2(std::numeric_limits<float>::max());
Vector2 max = Vector2(std::numeric_limits<float>::lowest());
int32 xppem, yppem;
hb_font_get_scale(hb_font, &xppem, &yppem);
Vector2 cursor = Vector2(0);
// Baseline (in pixels, relative to the text block's top-left) = the font's ascender.
// FreeType gives it in 1/64-pixel units after FT_Set_Pixel_Sizes; convert to pixels.
// Screen space is y-down; the baseline sits `ascender` px *below* the block's top edge.
// (FT metrics ascender is positive-up, so the baseline screen y = +ascender in y-down.)
float ascenderPx = (float)ft_face->size->metrics.ascender / 64.0f;
Vector2 cursor = Vector2(0, ascenderPx);
for (uint32 i = 0; i < num_glyphs; ++i) {
hb_codepoint_t glyphid = glyphs[i].codepoint;
float xOffset = (float)positions[i].x_offset * fontSize / xppem;
@@ -58,17 +62,37 @@ UVector2 FontAsset::shapeText(std::string_view view, uint32 fontSize, Array<Rend
float yAdvance = (float)positions[i].y_advance * fontSize / yppem;
Vector2 position = cursor + Vector2(xOffset, yOffset);
Vector2 dimensions = fontSizes[fontSize].glyphs[glyphid].size;
// FreeType bitmap_top = distance from the baseline to the TOP of the bitmap
// (positive up). In y-down screen space the bitmap's top edge is therefore at
// (baselineY - bitmap_top). bearing.x (bitmap_left) shifts the bitmap right of the
// pen. This is what was missing: without it every glyph's top sat on the cursor y
// (text top), giving top-aligned instead of baseline-aligned text.
float bearingX = (float)fontSizes[fontSize].glyphs[glyphid].bearing.x;
float bitmapTop = (float)fontSizes[fontSize].glyphs[glyphid].bearing.y;
position.x += bearingX;
position.y += -bitmapTop;
render.add(RenderGlyph{
.texture = fontSizes[fontSize].glyphs[glyphid].texture,
.position = position,
.dimensions = dimensions,
});
cursor += Vector2(xAdvance, yAdvance);
min = Vector2(std::min(min.x, position.x), std::min(min.y, position.y));
max = Vector2(std::max(max.x, position.x + dimensions.x), std::max(min.y, position.y + dimensions.y));
// Track the right-most x extent (for auto width). Height is NOT derived from the
// bitmap extents: it must be the full line height (ascender - descender) or
// auto-sized elements would clip descenders. See below.
max.x = std::max(max.x, position.x + dimensions.x);
}
hb_buffer_destroy(buf);
return UVector2(max - min);
// Width = right-most x extent (glyphs start at x>=0, so max.x is the full width).
// Height = line height from the font metrics: ascender (above baseline) plus the
// descender depth (FT descender is negative-up => its magnitude is below baseline).
// This is the correct auto-size: it fits the whole em, including descenders.
float descenderPx = (float)ft_face->size->metrics.descender / 64.0f; // <= 0 (down is negative-up)
float lineHeightPx = ascenderPx - descenderPx;
if (max.x <= 0.0f) { // empty string
return UVector2(0, 0);
}
return UVector2((uint32)std::ceil(max.x), (uint32)std::ceil(lineHeightPx));
}
void FontAsset::loadGlyphs(uint32 fontSize) {