From 2a8648bfeaaea906492685924bffb7017c5bf13e Mon Sep 17 00:00:00 2001 From: Goldloli <39011904+Goldloli@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:58:10 +0800 Subject: [PATCH] Text: keep the top of a stroked label's outline under the "hanging" baseline on macOS --- packages/melonjs/CHANGELOG.md | 1 + packages/melonjs/src/renderable/text/textmetrics.js | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/melonjs/CHANGELOG.md b/packages/melonjs/CHANGELOG.md index 3419546c1e..510312352e 100644 --- a/packages/melonjs/CHANGELOG.md +++ b/packages/melonjs/CHANGELOG.md @@ -7,6 +7,7 @@ ### Fixed - `NineSliceSprite` blinks with `flicker()`, as a `Sprite` does; it ignored it +- Text: a stroked multi-line label with `textBaseline: "hanging"` keeps the top of its outline on macOS, where Helvetica's hanging metrics made the ink padding exactly as tall as the ink, shearing its first row off the canvas ([#1870](https://github.com/melonjs/melonJS/issues/1870)) - glTF: a model compressed with Draco or meshopt, which the loader cannot decode, says so in the console, naming the extension, instead of failing on an accessor error that did not mention it ## [20.16.0] (melonJS 2) - _2026-11-17_ diff --git a/packages/melonjs/src/renderable/text/textmetrics.js b/packages/melonjs/src/renderable/text/textmetrics.js index b7a46b7b59..3fe998a737 100644 --- a/packages/melonjs/src/renderable/text/textmetrics.js +++ b/packages/melonjs/src/renderable/text/textmetrics.js @@ -8,17 +8,22 @@ import setContextStyle, { strokeReach } from "./textstyle.js"; * @import Renderable from "../renderable.js"; */ /** - * One pixel of slack on top of the reported ink extent. + * Two pixels of slack on top of the reported ink extent. * * `actualBoundingBox*` describes the OUTLINE, and the rasterizer paints past * it: antialiasing and pixel snapping both put ink outside what the metrics * promise. Measured against two engines, the painted ink reached a full pixel - * beyond the reported bound — enough to shear the top row off a stroke. Over- - * padding costs nothing but invisible canvas headroom. + * beyond the reported bound — enough to shear the top row off a stroke. One + * more is needed when the padding rounds to the demand exactly: on macOS, + * Helvetica under the "hanging" baseline reports an ascent of -0.05px at + * 20px, so `ceil(ascent + stroke)` leaves 0.05px of headroom and the painted + * ink — ascent plus the stroke's reach plus its own spill — starts on the + * canvas' first row. A second pixel absorbs that worst case. Over-padding + * costs nothing but invisible canvas headroom. * @ignore * @internal */ -const INK_SLACK = 1; +const INK_SLACK = 2; /** * The share of the em to pad by when the ink extent cannot be measured at all.