Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(ui): a lineHeight's extra space belongs half above the text, not all below

yhh10946497dc6f
diff --git a/sdk/src/ui/text/inline-image-plugin.ts b/sdk/src/ui/text/inline-image-plugin.ts
index b10cdc453..cbb222b86 100644
--- a/sdk/src/ui/text/inline-image-plugin.ts
+++ b/sdk/src/ui/text/inline-image-plugin.ts
@@ -120,9 +120,13 @@ export class InlineImagePlugin implements Plugin {
// same shift `drawTextWith` applies to the glyphs (mirror it so the
// images track the text).
const slack = h - layout.lineHeight;
- const vshift = t.verticalAlign === TextVerticalAlign.Middle ? slack / 2
+ const perLine = t.lineHeight > 0 ? t.lineHeight * t.fontSize : t.fontSize * 1.2;
+ // …including the half-leading it charges above the first baseline,
+ // or an image floats where the glyph beside it no longer is.
+ const halfLeading = Math.max(0, (perLine - t.fontSize) / 2);
+ const vshift = halfLeading + (t.verticalAlign === TextVerticalAlign.Middle ? slack / 2
: t.verticalAlign === TextVerticalAlign.Bottom ? slack
- : 0;
+ : 0);
syncChildren(e, t, images, vshift);
}
diff --git a/sdk/src/ui/text/text-renderer.ts b/sdk/src/ui/text/text-renderer.ts
index 7db0c60ca..cd142e5ae 100644
--- a/sdk/src/ui/text/text-renderer.ts
+++ b/sdk/src/ui/text/text-renderer.ts
@@ -120,6 +120,13 @@ export function drawTextWith(atlas: GlyphAtlas, sink: GlyphBatchSink, p: DrawTex
const slack = boxHeight - layout.lineHeight;
originY -= p.verticalAlign === 1 ? slack / 2 : slack; // 1 middle, 2 bottom
}
+ // Half-leading. A lineHeight above 1em adds space around the text, and that
+ // space belongs half above the first baseline and half below the last — the
+ // caller hands us a baseline a flat 0.8em under the line top, which puts all
+ // of it below. The block then rides (lineHeight - 1em)/2 too high inside its
+ // own box: at the default 1.2, every centred label sits 0.1em above where it
+ // should, which on a 60px label is a visible 6px.
+ originY -= Math.max(0, ((p.lineHeight ?? p.fontSizePx * 1.2) - p.fontSizePx) / 2);
// A string can reference glyphs across several atlas pages; each page is a
// distinct texture, so group by page and emit one batch per page.
diff --git a/sdk/tests/ui-text-renderer.test.ts b/sdk/tests/ui-text-renderer.test.ts
index e4af5ccd7..edeb9668a 100644
--- a/sdk/tests/ui-text-renderer.test.ts
+++ b/sdk/tests/ui-text-renderer.test.ts
@@ -147,6 +147,17 @@ describe('REARCH_GUI P1.3b: drawTextWith', () => {
expect(bottom).toBeGreaterThan(middle);
});
+ it('centres on the em box, not on the baseline, so leading does not push text up', () => {
+ // A lineHeight above 1em adds space around the line; half belongs above the
+ // first baseline. Charge it all below and every centred label rides
+ // (lineHeight - 1em)/2 too high — 0.1em at the 1.2 default, which is what a
+ // 60px label showed as a visible 6px.
+ const box = { boxHeight: 200, verticalAlign: 1, fontSizePx: 24 };
+ const tight = drawFirst({ ...box, lineHeight: 24 }).y; // 1.0em: no leading to split
+ const loose = drawFirst({ ...box, lineHeight: 24 * 1.2 }).y;
+ expect(loose).toBeCloseTo(tight, 5); // y-up: extra leading must not raise the block
+ });
+
it('horizontal align works inside a fixed box even with word-wrap off (boxWidth, no maxWidth)', () => {
// boxWidth aligns within the box independently of maxWidth (wrap). The block was
// previously left-anchored whenever wrap was off — align silently did nothing.