@visualli/core 0.2.17 → 0.2.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2696,23 +2696,49 @@ var PILL = {
2696
2696
  maxGrow: 1.3
2697
2697
  };
2698
2698
  var PILL_CHROME = { w: 2 * PILL.padX + 2 * PILL.border, h: PILL.padTop + PILL.padBottom + 2 * PILL.border };
2699
+ function graphemes(text) {
2700
+ const out = [], joins = /^[\p{M}\u200d\ufe00-\ufe0f\u{1f3fb}-\u{1f3ff}\u{e0020}-\u{e007f}]$/u, flag = /^[\u{1f1e6}-\u{1f1ff}]$/u;
2701
+ for (const c of Array.from(text)) {
2702
+ const last = out.length - 1;
2703
+ if (last >= 0 && (joins.test(c) || out[last].endsWith("\u200D") || flag.test(c) && Array.from(out[last]).every((x) => flag.test(x)) && Array.from(out[last]).length % 2 === 1)) out[last] += c;
2704
+ else out.push(c);
2705
+ }
2706
+ return out;
2707
+ }
2699
2708
  function pillLayout(name, measure, scale = 1) {
2700
2709
  const px = PILL.fontPx * scale, room = PILL.maxWidth - PILL_CHROME.w, m = (s) => measure(s, px);
2701
- const words = name.split(/\s+/).filter(Boolean);
2702
- let lines = [], cur = "";
2703
- for (const w of words) {
2710
+ const rows = [];
2711
+ let cur = "";
2712
+ for (const w of name.split(/\s+/).filter(Boolean)) {
2713
+ if (m(w) > room) {
2714
+ if (cur) rows.push({ text: cur, mid: false });
2715
+ let part = "";
2716
+ for (const g of graphemes(w)) {
2717
+ if (part && m(part + g) > room) {
2718
+ rows.push({ text: part, mid: true });
2719
+ part = g;
2720
+ } else part += g;
2721
+ }
2722
+ cur = part;
2723
+ continue;
2724
+ }
2704
2725
  const next = cur ? `${cur} ${w}` : w;
2705
2726
  if (cur && m(next) > room) {
2706
- lines.push(cur);
2727
+ rows.push({ text: cur, mid: false });
2707
2728
  cur = w;
2708
2729
  } else cur = next;
2709
2730
  }
2710
- if (cur) lines.push(cur);
2731
+ if (cur) rows.push({ text: cur, mid: false });
2732
+ let lines = rows.map((r) => r.text);
2711
2733
  if (!lines.length) lines = [""];
2712
- if (lines.length > PILL.maxLines) {
2713
- let last = lines.slice(PILL.maxLines - 1).join(" ");
2714
- while (last.length > 1 && m(last + "\u2026") > room) last = last.slice(0, -1);
2715
- lines = [...lines.slice(0, PILL.maxLines - 1), last.replace(/\s+$/, "") + "\u2026"];
2734
+ if (rows.length > PILL.maxLines) {
2735
+ let rest = "";
2736
+ rows.slice(PILL.maxLines - 1).forEach((r, i, a) => {
2737
+ rest += r.text + (i < a.length - 1 && !r.mid ? " " : "");
2738
+ });
2739
+ const gs = graphemes(rest);
2740
+ while (gs.length > 1 && m(gs.join("") + "\u2026") > room) gs.pop();
2741
+ lines = [...lines.slice(0, PILL.maxLines - 1), gs.join("").replace(/\s+$/, "") + "\u2026"];
2716
2742
  }
2717
2743
  const widest = Math.max(...lines.map(m));
2718
2744
  return { lines, w: Math.min(PILL.maxWidth, widest + PILL_CHROME.w), h: lines.length * px * PILL.lineHeight + PILL_CHROME.h };