If your product lets people type their own text onto a poster, someone can type an emoji. string.length answers one question, how many UTF-16 code units are in the string, and a person looking at the poster is asking something else.
This note checks how bad the damage is when you wrap text on a canvas the simple way: add one code unit at a time, measure, break when the line gets too wide. The usual worry is lone surrogates, half an emoji that renders as a question mark box. The run below showed something else.
One short string, three different counts
The test string is 全家出动👨👩👧👦去市集🇯🇵, a made-up caption with a family emoji and a flag in it. The sample is self-made, and the numbers come from one string, one system font (a system sans, 20px), one Apple-silicon Mac, one run each in three browser engines. The counts and the three lines matched in all three.
const s = '全家出动👨👩👧👦去市集🇯🇵'; const seg = new Intl.Segmenter('zh', { granularity: 'grapheme' }); const graphemes = [...seg.segment(s)].map(x => x.segment); const byUnit = []; let cur = ''; for (const u of s.split('')) { if (cur && ctx.measureText(cur + u).width > 110) { byUnit.push(cur); cur = u; } else cur += u; } byUnit.push(cur); console.log({ length: s.length, codePoints: [...s].length, graphemes: graphemes.length, familyUnits: '👨👩👧👦'.length, byUnit, wellFormed: byUnit.map(l => l.isWellFormed()) }); The snippet prints one object, and the table below lists every field it printed, none dropped. It assumes a canvas context ctx with the 20px font already set.
| What I asked | Result |
|---|---|
length (code units) | 22 |
Code points ([...s].length) | 16 |
| Graphemes (what a reader sees as one character) | 9 |
| Code units in the family emoji alone | 11 |
| Lines when wrapping by code unit at 110px | 全家出动👨 / 👩👧👦去市集🇯 / 🇵 |
Any line with a lone surrogate (isWellFormed() false) | none of the three |
Nine things on the screen, sixteen code points, twenty-two units. That gap is the whole story, and it shows up in a caption this short.
Where it breaks
The worst case people expect did not show up. The loop walks code units, yet in this run every break landed between two complete characters, never inside a surrogate pair. All three lines passed isWellFormed(). If you only test for broken surrogates, this approach looks fine.
It is not fine. The family emoji is four people glued together with zero-width joiners, and the first line ends after the first person and a joiner. The second line starts with the other three. The flag is two regional indicator letters, and the line break fell between them, so the last line is a lone 🇵. Nothing is corrupt at the string level. It is just wrong for the person who typed it, and it would show on the exported poster.
This run used one string at one width. Other widths would probably cut in other places, and no search for a width that produces a lone surrogate was done, so "no lone surrogates" is not a guarantee. On five other self-made texts at three widths each, code unit and grapheme wrapping produced identical lines, so the damage only shows when a cut lands inside a sequence.
Wrap on graphemes instead
Paste one family emoji and one flag into your text box and look at where the wrapper cuts. To fix it, wrap on graphemes. Intl.Segmenter with granularity: 'grapheme' hands you the nine units above, and you measure and break between those. In the loop above that means iterating the segments instead of s.split('').
Two things this run did not test. First, whether older engines segment newer emoji sequences the same way as the three engines here. Second, whether breaking between graphemes is enough for a poster. It keeps the emoji whole, but it does not stop a closing quote or a full-width comma from starting a line: in the emoji-and-combining-marks sample at 200 px, grapheme wrapping put a comma at the start of a line, and that is a separate rule. The counts above are measured; that grapheme wrapping fixes the split emoji is the reasoning part.
As background, in the ImgIng online editor, https://imging.ai/, a text layer stays on one line until the author presses Enter, so the line breaks there are the author's decision. That does not help when your users type the text.