#124

Emoji are measured too narrow without a canvas

Openmetonymopened 28d ago

Description

When no canvas is available, as on a server, Pele measures text with metricsMeasurer. It measures emoji much narrower than color emoji fonts draw them. Boxes around emoji come out too tight, and their text can run past the edges.

Steps to reproduce

Render this diagram with render() in Node or Bun, where there is no canvas:

flowchart LR
  A["✅ Passed"] --> B["🚀 Deploy"] --> C["✅ ✅ ✅ ⚠️ ⚠️"]

Expected

Each emoji is measured at about the width a browser draws it. Chrome's canvas on macOS (Apple Color Emoji) measures ✅ at these widths:

Font sizeWidth
12px1.25em
14px1.286em
16px1.25em
17.6px1.193em
20px1.1em
24px1em

🚀, ⚠️, ❌ and ⏳ measure the same at 16px.

Actual

In src/text/measurer.ts:

  • ✅ (U+2705) and ⚠ (U+26A0) fall through to the default of 580 units, so they measure 0.58em.
  • 🚀 (U+1F680) measures 1em. Its high surrogate counts as wide, and its low surrogate as zero width.

In a browser Pele measures with the canvas, so only server-side rendering has this problem. The same diagram, viewed in Chrome:

Rendered in Bun (no canvas)Rendered in the browser (canvas)

Environment

  • pele 1.13.0 (npm), rendered in Bun 1.4.4
  • Chrome 156 on macOS for the reference widths

Possible fix

Give common emoji about 1.29em in metricsMeasurer, the widest width above, so boxes fit at any size. I patched this locally with a range check before isWide():

  • U+2300–23FF, U+2600–27BF and U+2B00–2BFF
  • high surrogates U+D83C–D83E

I can open a PR.

If a fixed width is not a good fit, exporting metricsMeasurer would also work. Hosts could then wrap it and pass their own measurer. Today TextMeasurer is exported only as a type, and the default measurer can't be reached.

Activity

0

No activity yet.