Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

fix(text-fit): count BMP emoji-presentation symbols as two units (#91)

Ngo Quoc Viet146b6d4e16ef
diff --git a/archify.zip b/archify.zip
index 760376f7..53b4980e 100644
Binary files a/archify.zip and b/archify.zip differ
diff --git a/archify/renderers/shared/utils.mjs b/archify/renderers/shared/utils.mjs
index 739a8e6f..84120b4d 100644
--- a/archify/renderers/shared/utils.mjs
+++ b/archify/renderers/shared/utils.mjs
@@ -165,10 +165,46 @@ export function applyTemplate(template, { title, subtitle, svg, cards, visualPre
// forms (notably U+FF61–U+FF9F Katakana) out of this set. The explicit ranges
// also cover vertical punctuation and supplementary East Asian scripts that
// literal glyph ranges made difficult to audit.
-const FULLWIDTH_RE = /[\u1100-\u115F\u2329-\u232A\u2E80-\uA4CF\uAC00-\uD7A3\uF900-\uFAFF\uFE10-\uFE19\uFE30-\uFE6F\uFF01-\uFF60\uFFE0-\uFFE6\u{16FE0}-\u{18DFF}\u{1AFF0}-\u{1AFFF}\u{1B000}-\u{1B2FF}\u{1F000}-\u{1FAFF}\u{20000}-\u{3FFFD}]/u;
+// Code points that take two columns of advance width: East Asian Wide and
+// Fullwidth per UAX #11, tracking Unicode 17.0. That takes in the BMP symbols
+// carrying emoji presentation (U+2705, U+2B50, U+26A1, U+231B, ...), which
+// render at the same square advance as the supplementary-plane emoji already
+// listed here, and Hangul Jamo Extended-A. Two boundary calls worth naming:
+// Unicode 16.0 reclassified the trigrams (U+2630-U+2637) and the monogram /
+// digram symbols (U+268A-U+268F) from Neutral to Wide, so both are in; and
+// Hangul Jamo Extended-A stops at U+A97C, its last assigned jamo, because
+// U+A97D-U+A97F are unassigned, and unassigned code points outside the CJK
+// ranges UAX #11 names default to Neutral rather than Wide. Spelled out as
+// ranges because V8 has no \p{East_Asian_Width=W} property escape.
+const FULLWIDTH_RE = /[\u1100-\u115F\u231A-\u231B\u2329-\u232A\u23E9-\u23EC\u23F0\u23F3\u25FD-\u25FE\u2614-\u2615\u2630-\u2637\u2648-\u2653\u267F\u268A-\u268F\u2693\u26A1\u26AA-\u26AB\u26BD-\u26BE\u26C4-\u26C5\u26CE\u26D4\u26EA\u26F2-\u26F3\u26F5\u26FA\u26FD\u2705\u270A-\u270B\u2728\u274C\u274E\u2753-\u2755\u2757\u2795-\u2797\u27B0\u27BF\u2B1B-\u2B1C\u2B50\u2B55\u2E80-\uA4CF\uA960-\uA97C\uAC00-\uD7A3\uF900-\uFAFF\uFE10-\uFE19\uFE30-\uFE6F\uFF01-\uFF60\uFFE0-\uFFE6\u{16FE0}-\u{18DFF}\u{1AFF0}-\u{1AFFF}\u{1B000}-\u{1B2FF}\u{1F000}-\u{1FAFF}\u{20000}-\u{3FFFD}]/u;
+
+// A variation selector (U+FE00-U+FE0F) carries no advance of its own: it
+// re-presents the character before it. VS15 (U+FE0E) asks for text
+// presentation, which renders narrow; VS16 (U+FE0F) asks for emoji
+// presentation, which renders at the square emoji advance. So a base plus a
+// selector is measured from the selector, not from the base -- otherwise
+// widening the emoji-presentation bases above turns U+2B50 U+FE0F from two
+// units into three while the glyph on screen stays one square, and leaves
+// U+2708 U+FE0F at two only because its base happens to be narrow.
+//
+// A selector following a base that cannot take emoji presentation is
+// malformed input; measuring it wide is the safe direction here, since
+// over-measuring pads a box while under-measuring spills the label out of it.
+const VARIATION_SELECTOR_FIRST = 0xfe00;
+const VARIATION_SELECTOR_LAST = 0xfe0f;
+const VARIATION_SELECTOR_TEXT = 0xfe0e;
+const VARIATION_SELECTOR_EMOJI = 0xfe0f;
export function textUnits(text) {
+ const chars = Array.from(String(text ?? ''));
let units = 0;
- for (const ch of String(text ?? '')) units += FULLWIDTH_RE.test(ch) ? 2 : 1;
+ for (let i = 0; i < chars.length; i += 1) {
+ const codePoint = chars[i].codePointAt(0);
+ if (codePoint >= VARIATION_SELECTOR_FIRST && codePoint <= VARIATION_SELECTOR_LAST) continue;
+ const next = i + 1 < chars.length ? chars[i + 1].codePointAt(0) : -1;
+ if (next === VARIATION_SELECTOR_EMOJI) units += 2;
+ else if (next === VARIATION_SELECTOR_TEXT) units += 1;
+ else units += FULLWIDTH_RE.test(chars[i]) ? 2 : 1;
+ }
return units;
}
diff --git a/archify/test/geometry.test.mjs b/archify/test/geometry.test.mjs
index b7a5e142..4064b005 100644
--- a/archify/test/geometry.test.mjs
+++ b/archify/test/geometry.test.mjs
@@ -595,6 +595,57 @@ test('textUnits follows wide and halfwidth East Asian presentation boundaries',
assert.equal(textUnits('ㄱ'), 2); // Hangul compatibility letter is wide
assert.equal(textUnits('︐︙'), 4); // vertical punctuation forms are wide
assert.equal(textUnits('カタカナ'), 4); // halfwidth Katakana stays one unit per glyph
+ assert.equal(textUnits('ꥠ'), 2); // Hangul Jamo Extended-A is wide
+});
+
+test('textUnits counts emoji-presentation symbols in the BMP as wide', () => {
+ // These render at the same square advance as the supplementary-plane emoji,
+ // so counting them as one unit under-measures a label and lets it overflow
+ // its node while the layout receipt still reads clean.
+ assert.equal(textUnits('✅'), 2);
+ assert.equal(textUnits('⭐'), 2);
+ assert.equal(textUnits('⚡'), 2);
+ assert.equal(textUnits('⌛'), 2);
+ assert.equal(textUnits('⏰'), 2);
+ assert.equal(textUnits('⛔'), 2);
+ assert.equal(textUnits('❗'), 2);
+ assert.equal(textUnits('⬛'), 2);
+ assert.equal(textUnits('☕'), 2);
+ assert.equal(textUnits('♿'), 2);
+ assert.equal(textUnits('✅ Done'), 7);
+ // Narrow and ambiguous neighbours in the same blocks stay one unit.
+ assert.equal(textUnits('→'), 1); // rightwards arrow
+ assert.equal(textUnits('☎'), 1); // black telephone
+ assert.equal(textUnits('①'), 1); // circled digit one
+ // Unicode 16.0 moved these from Neutral to Wide.
+ assert.equal(textUnits('☰'), 2); // trigram for heaven
+ assert.equal(textUnits('☷'), 2); // trigram for earth
+ assert.equal(textUnits('⚊'), 2); // monogram for yang
+ assert.equal(textUnits('⚏'), 2); // digram for greater yin
+ // Hangul Jamo Extended-A stops at its last assigned jamo; the unassigned
+ // tail of the block defaults to Neutral.
+ assert.equal(textUnits('ꥼ'), 2);
+ assert.equal(textUnits('꥽'), 1);
+});
+
+test('textUnits measures a variation-selector sequence from the selector', () => {
+ // VS16 asks for emoji presentation: the pair renders as one square, so it
+ // must stay two units even though the base is now counted wide on its own.
+ assert.equal(textUnits('⭐️'), 2); // star
+ assert.equal(textUnits('✅️'), 2); // check mark button
+ assert.equal(textUnits('☕️'), 2); // hot beverage
+ assert.equal(textUnits('⚡️'), 2); // high voltage
+ // Same rule the other way: a narrow base forced to emoji presentation
+ // renders as a square and is two units, not one.
+ assert.equal(textUnits('✈️'), 2); // airplane
+ assert.equal(textUnits('❤️'), 2); // red heart
+ // VS15 asks for text presentation, which renders narrow.
+ assert.equal(textUnits('⭐︎'), 1);
+ assert.equal(textUnits('✈︎'), 1);
+ // The selector never adds width of its own, alone or in a run.
+ assert.equal(textUnits('️'), 0);
+ assert.equal(textUnits('✅️ Done'), 7);
+ assert.equal(textUnits('⭐️⭐️'), 4);
});
test('semantic sigils cover every component and lifecycle kind without literal color', () => {