SPB Git

Repository Public

text-measure.ts 4.4 KB 88 lines typescript

1/**2 * Deterministic text measurement: an estimate of the advance width of a string set in Inter, from a per-character3 * table (units: 1/1000 em, regular weight). No canvas and no DOM, so the server, the browser and the export worker4 * compute exactly the same layout. The estimate errs slightly on the wide side so labels never spill out of a node.5 */6const W: Record<string, number> = {7  " ": 280, "!": 290, '"': 420, "#": 640, "$": 640, "%": 880, "&": 660, "'": 230, "(": 370, ")": 370, "*": 500, "+": 660,8  ",": 290, "-": 460, ".": 290, "/": 380, ":": 290, ";": 290, "<": 660, "=": 660, ">": 660, "?": 520, "@": 980,9  "[": 370, "\\": 380, "]": 370, "^": 480, "_": 480, "`": 400, "{": 380, "|": 340, "}": 380, "~": 660,10  "0": 640, "1": 480, "2": 610, "3": 630, "4": 650, "5": 620, "6": 630, "7": 580, "8": 630, "9": 630,11  A: 690, B: 660, C: 730, D: 720, E: 610, F: 590, G: 750, H: 750, I: 280, J: 550, K: 670, L: 570, M: 900, N: 760,12  O: 770, P: 640, Q: 770, R: 650, S: 650, T: 640, U: 740, V: 690, W: 990, X: 670, Y: 670, Z: 640,13  a: 570, b: 610, c: 560, d: 610, e: 580, f: 350, g: 610, h: 590, i: 250, j: 250, k: 550, l: 250, m: 880, n: 590,14  o: 600, p: 610, q: 610, r: 380, s: 530, t: 360, u: 590, v: 550, w: 800, x: 540, y: 550, z: 550,15  "–": 520, "—": 880, "…": 800, "·": 290, "•": 400, "’": 230, "‘": 230, "“": 420, "”": 420, "«": 520, "»": 520,16  "→": 900, "←": 900, "↔": 900, "↑": 620, "↓": 620, "×": 660, "÷": 660, "±": 660, "≤": 660, "≥": 660, "≠": 660, "≈": 660,17  "∞": 880, "∑": 720, "∂": 620, "√": 640, "°": 380, "€": 640, " ": 280, " ": 180, "−": 660,18};19const DEFAULT_WIDTH = 620;20const WIDE = /[ᄀ-ᅟ⺀-꓏가-힣豈-﫿︰-﹏＀-⦆¢-₩]/;2122function charWidth(ch: string): number {23  const direct = W[ch];24  if (direct !== undefined) return direct;25  const base = ch.normalize("NFD")[0];26  const stripped = base !== undefined ? W[base] : undefined;27  if (stripped !== undefined) return stripped;28  if (WIDE.test(ch) || (ch.codePointAt(0) ?? 0) > 0xffff) return 1000;29  if (/[α-ωά-ώ]/.test(ch)) return 600;30  if (/[Α-Ω]/.test(ch)) return 700;31  return DEFAULT_WIDTH;32}3334/** Bolder cuts of Inter are wider; linear between 400 and 700. */35const weightFactor = (weight: number): number => 1 + Math.max(0, Math.min(500, weight - 400)) * 0.0002;3637/** Estimated width in px of `text` at `fontSize` px. */38export function measureText(text: string, fontSize: number, weight = 400): number {39  let units = 0;40  for (const ch of text) units += charWidth(ch);41  return Math.round(units * fontSize * weightFactor(weight)) / 1000;42}4344export interface WrapOptions { weight?: number; maxLines?: number }4546/**47 * Greedy word wrap to at most `maxLines` lines no wider than `maxWidth` px. Over-long words are broken; text that48 * does not fit ends with an ellipsis. Always returns at least one line.49 */50export function wrapText(text: string, maxWidth: number, fontSize: number, opts: WrapOptions = {}): string[] {51  const weight = opts.weight ?? 400;52  const maxLines = Math.max(1, opts.maxLines ?? 3);53  const fits = (s: string) => measureText(s, fontSize, weight) <= maxWidth;54  const words = text.trim().split(/\s+/).filter(Boolean);55  if (words.length === 0) return [""];56  const lines: string[] = [];57  let line = "";58  const pushBroken = (word: string) => {59    // Break a word that is wider than a whole line.60    let rest = word;61    while (rest && !fits(rest)) {62      const chars = Array.from(rest);63      let n = chars.length - 1;64      while (n > 1 && !fits(chars.slice(0, n).join("") + "-")) n--;65      lines.push(chars.slice(0, n).join("") + "-");66      rest = chars.slice(n).join("");67    }68    line = rest;69  };70  for (const word of words) {71    const candidate = line ? `${line} ${word}` : word;72    if (fits(candidate)) { line = candidate; continue; }73    if (line) lines.push(line);74    line = "";75    if (fits(word)) line = word; else pushBroken(word);76  }77  if (line) lines.push(line);78  if (lines.length <= maxLines) return lines;79  const kept = lines.slice(0, maxLines);80  let last = Array.from(kept[maxLines - 1]);81  while (last.length > 1 && !fits(last.join("") + "…")) last = last.slice(0, -1);82  kept[maxLines - 1] = last.join("").replace(/[\s,;:.-]+$/, "") + "…";83  return kept;84}8586/** Width of the widest line. */87export const blockWidth = (lines: string[], fontSize: number, weight = 400): number => lines.reduce((m, l) => Math.max(m, measureText(l, fontSize, weight)), 0);88
Files main
Menu

Theme