ultra-text 1.0.0 → 1.0.1

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.
Files changed (134) hide show
  1. package/README.md +168 -85
  2. package/THIRD_PARTY_NOTICES.md +3 -0
  3. package/dist/assets/{cpuSdf.worker-Du9o8q4p.js → cpuSdf.worker-xS57yQbP.js} +13 -11
  4. package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +1 -0
  5. package/dist/atlas/appendAtlas.js +2 -1
  6. package/dist/atlas/appendAtlas.js.map +1 -1
  7. package/dist/atlas/buildAtlas.js +3 -14
  8. package/dist/atlas/buildAtlas.js.map +1 -1
  9. package/dist/atlas/cpuSdf.js +12 -10
  10. package/dist/atlas/cpuSdf.js.map +1 -1
  11. package/dist/atlas/cpuWorker.js +1 -1
  12. package/dist/atlas/cpuWorker.js.map +1 -1
  13. package/dist/atlas/gpuAtlas.d.ts +7 -6
  14. package/dist/atlas/gpuAtlas.js +280 -145
  15. package/dist/atlas/gpuAtlas.js.map +1 -1
  16. package/dist/atlas/jfaWgsl.js +201 -73
  17. package/dist/atlas/jfaWgsl.js.map +1 -1
  18. package/dist/atlas/rasterize.d.ts +2 -0
  19. package/dist/atlas/rasterize.js +2 -87
  20. package/dist/atlas/rasterize.js.map +1 -1
  21. package/dist/dom/DomSdf.js +66 -15
  22. package/dist/dom/DomSdf.js.map +1 -1
  23. package/dist/dom/DomSlug.d.ts +26 -0
  24. package/dist/dom/DomSlug.js +640 -0
  25. package/dist/dom/DomSlug.js.map +1 -0
  26. package/dist/dom/SlugText.d.ts +18 -0
  27. package/dist/dom/SlugText.js +94 -0
  28. package/dist/dom/SlugText.js.map +1 -0
  29. package/dist/dom/Text.js +4 -67
  30. package/dist/dom/Text.js.map +1 -1
  31. package/dist/dom/fontCanvas.d.ts +2 -0
  32. package/dist/dom/fontCanvas.js +101 -0
  33. package/dist/dom/fontCanvas.js.map +1 -0
  34. package/dist/dom/layoutSignature.js +29 -2
  35. package/dist/dom/layoutSignature.js.map +1 -1
  36. package/dist/dom/snapshot.js +251 -96
  37. package/dist/dom/snapshot.js.map +1 -1
  38. package/dist/dom/textHost.d.ts +11 -0
  39. package/dist/dom/textHost.js +68 -0
  40. package/dist/dom/textHost.js.map +1 -0
  41. package/dist/slug/SlugMaterial.d.ts +3 -2
  42. package/dist/slug/SlugMaterial.js +28 -2
  43. package/dist/slug/SlugMaterial.js.map +1 -1
  44. package/dist/slug/coverage.d.ts +1 -1
  45. package/dist/slug/coverage.js +1 -1
  46. package/dist/slug/coverage.js.map +1 -1
  47. package/dist/slug/preprocessCore.d.ts +1 -2
  48. package/dist/slug/preprocessCore.js +39 -16
  49. package/dist/slug/preprocessCore.js.map +1 -1
  50. package/dist/slug/runtime.js +6 -4
  51. package/dist/slug/runtime.js.map +1 -1
  52. package/dist/slug/standaloneRuntime.d.ts +39 -0
  53. package/dist/slug/standaloneRuntime.js +140 -0
  54. package/dist/slug/standaloneRuntime.js.map +1 -0
  55. package/dist/slug/truetype.d.ts +22 -0
  56. package/dist/slug/truetype.js +494 -0
  57. package/dist/slug/truetype.js.map +1 -0
  58. package/dist/slug-vanilla.d.ts +364 -0
  59. package/dist/slug-vanilla.js +1461 -0
  60. package/dist/slug-vanilla.js.map +1 -0
  61. package/dist/slug.d.ts +21 -0
  62. package/dist/slug.js +19 -0
  63. package/dist/text/GlyphTextMaterial.d.ts +11 -11
  64. package/dist/text/GlyphTextMaterial.js +98 -29
  65. package/dist/text/GlyphTextMaterial.js.map +1 -1
  66. package/dist/text/SdfTextMaterial.d.ts +5 -4
  67. package/dist/text/SdfTextMaterial.js +22 -4
  68. package/dist/text/SdfTextMaterial.js.map +1 -1
  69. package/dist/types.d.ts +63 -14
  70. package/dist/vanilla.d.ts +3 -0
  71. package/dist/vanilla.js +132 -59
  72. package/dist/vanilla.js.map +1 -1
  73. package/dist/windfoil/WindfoilMaterial.d.ts +2 -1
  74. package/dist/windfoil/WindfoilMaterial.js +8 -1
  75. package/dist/windfoil/WindfoilMaterial.js.map +1 -1
  76. package/dist/windfoil/abortable.d.ts +2 -0
  77. package/dist/windfoil/abortable.js +26 -0
  78. package/dist/windfoil/abortable.js.map +1 -0
  79. package/dist/windfoil/color.js +2 -1
  80. package/dist/windfoil/color.js.map +1 -1
  81. package/dist/windfoil/coverage.d.ts +3 -20
  82. package/dist/windfoil/coverage.js +7 -20
  83. package/dist/windfoil/coverage.js.map +1 -1
  84. package/dist/windfoil/emoji.js +1 -1
  85. package/dist/windfoil/emoji.js.map +1 -1
  86. package/dist/windfoil/emojiMetrics.js +1 -1
  87. package/dist/windfoil/emojiMetrics.js.map +1 -1
  88. package/dist/windfoil/geometry.js +100 -33
  89. package/dist/windfoil/geometry.js.map +1 -1
  90. package/dist/windfoil/layerGrid.js +14 -10
  91. package/dist/windfoil/layerGrid.js.map +1 -1
  92. package/dist/windfoil/layerNormalization.d.ts +1 -1
  93. package/dist/windfoil/layerNormalization.js +1 -1
  94. package/dist/windfoil/layerNormalization.js.map +1 -1
  95. package/dist/windfoil/outlineValidation.d.ts +86 -0
  96. package/dist/windfoil/outlineValidation.js +65 -0
  97. package/dist/windfoil/outlineValidation.js.map +1 -0
  98. package/dist/windfoil/preprocess.js +3 -2
  99. package/dist/windfoil/preprocess.js.map +1 -1
  100. package/dist/windfoil/preprocessCore.d.ts +4 -85
  101. package/dist/windfoil/preprocessCore.js +210 -206
  102. package/dist/windfoil/preprocessCore.js.map +1 -1
  103. package/dist/windfoil/requests.js +1 -1
  104. package/dist/windfoil/requests.js.map +1 -1
  105. package/dist/windfoil/runtime.d.ts +2 -1
  106. package/dist/windfoil/runtime.js +3 -2
  107. package/dist/windfoil/runtime.js.map +1 -1
  108. package/dist/windfoil/shapeResources.js +2 -1
  109. package/dist/windfoil/shapeResources.js.map +1 -1
  110. package/dist/windfoil/shapes.js +1 -1
  111. package/dist/windfoil/skirt.d.ts +22 -0
  112. package/dist/windfoil/skirt.js +20 -0
  113. package/dist/windfoil/skirt.js.map +1 -0
  114. package/dist/windfoil/storage.d.ts +2 -2
  115. package/dist/windfoil/storage.js +1 -22
  116. package/dist/windfoil/storage.js.map +1 -1
  117. package/dist/windfoil/trace/contours.d.ts +0 -5
  118. package/dist/windfoil/trace/contours.js +81 -114
  119. package/dist/windfoil/trace/contours.js.map +1 -1
  120. package/dist/windfoil/trace/fit.d.ts +0 -6
  121. package/dist/windfoil/trace/fit.js +304 -308
  122. package/dist/windfoil/trace/fit.js.map +1 -1
  123. package/dist/windfoil/trace/outline.js +10 -10
  124. package/dist/windfoil/trace/outline.js.map +1 -1
  125. package/dist/windfoil/trace/types.d.ts +5 -3
  126. package/dist/windfoil/tracedOutlines.d.ts +37 -1
  127. package/dist/windfoil/tracedOutlines.js +172 -33
  128. package/dist/windfoil/tracedOutlines.js.map +1 -1
  129. package/package.json +57 -8
  130. package/skills/ultra-text/SKILL.md +114 -0
  131. package/skills/ultra-text/references/frameworks.md +43 -0
  132. package/skills/ultra-text/references/three-tsl.md +78 -0
  133. package/skills/ultra-text/references/vanilla-webgpu.md +95 -0
  134. package/dist/assets/cpuSdf.worker-Du9o8q4p.js.map +0 -1
@@ -37,10 +37,14 @@ function physicalAlign(value, rtl) {
37
37
  const side = value.replace("-webkit-", "");
38
38
  return /^(left|right|center|justify)$/.test(side) ? side : side === "end" !== rtl ? "right" : "left";
39
39
  }
40
+ var INV_2_22 = 2 ** -22;
41
+ var MAX_SHARED_FONT_CACHES = 256;
42
+ var SINGLE_CHAR = Array.from({ length: 128 }, (_, i) => String.fromCharCode(i));
40
43
  var sameBox = (a, b) => !!b && a.left === b.left && a.top === b.top && a.width === b.width && a.height === b.height;
41
44
  var colorProbe = null;
42
45
  var sharedKerningContext;
43
46
  var sharedWidthsByFont = /* @__PURE__ */ new Map();
47
+ var sharedAsciiWidthsByFont = /* @__PURE__ */ new Map();
44
48
  var cachedTextRuns = /* @__PURE__ */ new WeakMap();
45
49
  var lastFontSetSize = -1;
46
50
  var TRANSPARENT_SRGBA = [
@@ -113,9 +117,9 @@ function fontMetrics(context, font, fontSize, cache) {
113
117
  * Chromium positions layout in 1/64ths of a layout pixel. Client rectangles are 32-bit
114
118
  * floats, so the slack grows with the coordinates themselves, not with their difference.
115
119
  */
116
- function offLayoutGrid(coordinate, origin, scale) {
117
- const units = (coordinate - origin) * 64 * scale;
118
- return Math.abs(units - Math.round(units)) > .02 + (Math.abs(coordinate) + Math.abs(origin)) * 64 * scale * 2 ** -22;
120
+ function offLayoutGrid(coordinate, origin, scale64, absOrigin) {
121
+ const units = (coordinate - origin) * scale64;
122
+ return Math.abs(units - Math.round(units)) > .02 + (Math.abs(coordinate) + absOrigin) * scale64 * INV_2_22;
119
123
  }
120
124
  function layoutDescents(context, boxHeights, scales, bakeSize, cache) {
121
125
  const candidates = [];
@@ -188,14 +192,29 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
188
192
  if (nativeBrowser && fonts.size !== lastFontSetSize) {
189
193
  lastFontSetSize = fonts.size;
190
194
  sharedWidthsByFont = /* @__PURE__ */ new Map();
195
+ sharedAsciiWidthsByFont = /* @__PURE__ */ new Map();
191
196
  cachedTextRuns = /* @__PURE__ */ new WeakMap();
192
197
  }
198
+ const getContextDescriptor = Object.getOwnPropertyDescriptor(view.HTMLCanvasElement.prototype, "getContext")?.value;
199
+ const nativeCanvas = nativeBrowser && !(getContextDescriptor && "mock" in getContextDescriptor);
193
200
  let share = !svg && !("mock" in view.getComputedStyle);
194
- try {
195
- for (const s of root.getRootNode().styleSheets ?? []) for (const r of s.cssRules) if (/[:+~]/.test(r.cssText)) share = false;
196
- } catch {
197
- share = false;
198
- }
201
+ let shareChecked = false;
202
+ const canShare = () => {
203
+ if (!share) return false;
204
+ if (!shareChecked) {
205
+ shareChecked = true;
206
+ try {
207
+ for (const s of root.getRootNode().styleSheets ?? []) for (const r of s.cssRules) if (/[:+~]/.test(r.cssText)) {
208
+ share = false;
209
+ return false;
210
+ }
211
+ } catch {
212
+ share = false;
213
+ return false;
214
+ }
215
+ }
216
+ return share;
217
+ };
199
218
  const pathStyles = /* @__PURE__ */ new Map();
200
219
  const nodes = /* @__PURE__ */ new Map();
201
220
  const nodeOf = (el) => {
@@ -206,7 +225,8 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
206
225
  const hasAttrs = !attrOnly || el.hasAttributes();
207
226
  let k = share && (el === root || u?.k !== void 0) ? `${u?.k ?? ""}>${el.localName}` : void 0;
208
227
  if (k && el !== root) for (const a of el.attributes) k += `\u0001${a.name}=${a.name === "style" ? a.value.replace(/(?:^|;)\s*(?:left|top|right|bottom|inset)\s*:[^;]*/gi, "") : a.value}`;
209
- const p = k ? pathStyles.get(k) : void 0;
228
+ let p = k ? pathStyles.get(k) : void 0;
229
+ if (p !== void 0 && !canShare()) p = void 0;
210
230
  rec = {
211
231
  el,
212
232
  u,
@@ -281,17 +301,44 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
281
301
  const boxHeights = /* @__PURE__ */ new Map();
282
302
  let offCssGrid = false;
283
303
  let offDeviceGrid = false;
284
- const offBox = (l, w, scale) => offLayoutGrid(l, rootLeft, scale) || offLayoutGrid(l + w, rootLeft, scale);
304
+ let gridDone = false;
305
+ const absRootLeft = Math.abs(rootLeft);
306
+ const scale64Device = 64 * deviceScale;
307
+ const offBox = (l, w, scale64) => offLayoutGrid(l, rootLeft, scale64, absRootLeft) || offLayoutGrid(l + w, rootLeft, scale64, absRootLeft);
285
308
  const trackLayoutGrid = (l, w) => {
286
- if (!offCssGrid) offCssGrid = offBox(l, w, 1);
287
- if (deviceScale !== 1 && !offDeviceGrid) offDeviceGrid = offBox(l, w, deviceScale);
309
+ if (gridDone) return;
310
+ if (!offCssGrid) offCssGrid = offBox(l, w, 64);
311
+ if (deviceScale !== 1 && !offDeviceGrid) offDeviceGrid = offBox(l, w, scale64Device);
312
+ if (offCssGrid && (deviceScale === 1 || offDeviceGrid)) gridDone = true;
288
313
  };
289
314
  const layoutScales = () => [...deviceScale === 1 || offDeviceGrid ? [] : [deviceScale], ...offCssGrid ? [] : [1]];
315
+ const measuresKerning = "OffscreenCanvas" in view;
316
+ let kerningContext;
317
+ let lastKerningFont = "";
318
+ const ensureKerningContext = () => {
319
+ if (kerningContext === void 0) {
320
+ kerningContext = (nativeBrowser ? sharedKerningContext : void 0) ?? new view.OffscreenCanvas(1, 1).getContext("2d");
321
+ if (kerningContext) kerningContext.letterSpacing = "0px";
322
+ if (nativeBrowser) sharedKerningContext = kerningContext;
323
+ }
324
+ return kerningContext;
325
+ };
326
+ const ensureFontProbe = () => {
327
+ if (fontProbe === void 0) {
328
+ if (nativeCanvas) {
329
+ const probe = ensureKerningContext();
330
+ if (probe) return fontProbe = probe;
331
+ }
332
+ fontProbe = document.createElement("canvas").getContext("2d");
333
+ }
334
+ return fontProbe;
335
+ };
290
336
  const readDecorations = (parent, layoutFont, fontSize, opacity) => {
291
337
  const owners = decorationsOf(parent);
292
338
  if (owners.length === 0) return EMPTY_DECORATION_OWNERS;
293
- fontProbe ??= document.createElement("canvas").getContext("2d");
294
- const [ascent, descent] = fontProbe ? fontMetrics(fontProbe, layoutFont, fontSize, metricsByFont) : [fontSize * .8, fontSize * .2];
339
+ const probe = ensureFontProbe();
340
+ const [ascent, descent] = probe ? fontMetrics(probe, layoutFont, fontSize, metricsByFont) : [fontSize * .8, fontSize * .2];
341
+ if (probe && probe === kerningContext) lastKerningFont = "";
295
342
  const result = [];
296
343
  for (const owner of owners) {
297
344
  const style = styleOf(owner);
@@ -320,7 +367,22 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
320
367
  const capturedKeys = /* @__PURE__ */ new Map();
321
368
  const fontEntries = /* @__PURE__ */ new Map();
322
369
  const fontRequestKey = (font) => options.textBackend === "windfoil" && font.opticalSizing === "auto" ? `${font.key}|${font.size}` : font.key;
323
- const requestKey = (font, keys, character) => {
370
+ const requestKey = (font, keys, character, asciiKeys) => {
371
+ const code = asciiKeys && character.length === 1 ? character.charCodeAt(0) : -1;
372
+ if (code >= 0 && code < 128) {
373
+ let key = asciiKeys[code];
374
+ if (key === void 0) {
375
+ key = `${fontRequestKey(font)}\u0000${character}`;
376
+ asciiKeys[code] = key;
377
+ keys.set(character, key);
378
+ requests.set(key, {
379
+ key,
380
+ character,
381
+ font
382
+ });
383
+ }
384
+ return key;
385
+ }
324
386
  let key = keys.get(character);
325
387
  if (key === void 0) {
326
388
  key = `${fontRequestKey(font)}\u0000${character}`;
@@ -333,19 +395,23 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
333
395
  }
334
396
  return key;
335
397
  };
336
- const measuresKerning = "OffscreenCanvas" in view;
337
- let kerningContext;
338
- let lastKerningFont = "";
339
- const canvasWidth = (cssFont, widths, text) => {
398
+ const canvasWidth = (cssFont, widths, asciiWidths, text) => {
399
+ const code = text.length === 1 ? text.charCodeAt(0) : -1;
400
+ if (code >= 0 && code < 128) {
401
+ let width = asciiWidths[code];
402
+ if (width < 0) {
403
+ const ctx = ensureKerningContext();
404
+ if (ctx && lastKerningFont !== cssFont) ctx.font = lastKerningFont = cssFont;
405
+ width = ctx?.measureText(text).width ?? 0;
406
+ asciiWidths[code] = width;
407
+ }
408
+ return width;
409
+ }
340
410
  let width = widths.get(text);
341
411
  if (width === void 0) {
342
- if (kerningContext === void 0) {
343
- kerningContext = (nativeBrowser ? sharedKerningContext : void 0) ?? new view.OffscreenCanvas(1, 1).getContext("2d");
344
- if (kerningContext) kerningContext.letterSpacing = "0px";
345
- if (nativeBrowser) sharedKerningContext = kerningContext;
346
- }
347
- if (kerningContext && lastKerningFont !== cssFont) kerningContext.font = lastKerningFont = cssFont;
348
- width = kerningContext?.measureText(text).width ?? 0;
412
+ const ctx = ensureKerningContext();
413
+ if (ctx && lastKerningFont !== cssFont) ctx.font = lastKerningFont = cssFont;
414
+ width = ctx?.measureText(text).width ?? 0;
349
415
  widths.set(text, width);
350
416
  }
351
417
  return width;
@@ -394,20 +460,35 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
394
460
  size
395
461
  };
396
462
  const reqKey = fontRequestKey(font);
397
- let keys = capturedKeys.get(reqKey);
398
- if (!keys) capturedKeys.set(reqKey, keys = /* @__PURE__ */ new Map());
463
+ let captured = capturedKeys.get(reqKey);
464
+ if (!captured) {
465
+ captured = [/* @__PURE__ */ new Map(), new Array(128)];
466
+ capturedKeys.set(reqKey, captured);
467
+ }
468
+ const [keys, asciiKeys] = captured;
399
469
  const layoutFont = font.canvasFont.replace(`${bakeSize}px`, `${size}px`);
400
470
  let widths = nativeBrowser ? sharedWidthsByFont.get(layoutFont) : void 0;
401
- if (!widths) {
402
- widths = /* @__PURE__ */ new Map();
403
- if (nativeBrowser) sharedWidthsByFont.set(layoutFont, widths);
471
+ let asciiWidths = nativeBrowser ? sharedAsciiWidthsByFont.get(layoutFont) : void 0;
472
+ if (!widths || !asciiWidths) {
473
+ if (nativeBrowser && sharedWidthsByFont.size >= MAX_SHARED_FONT_CACHES) {
474
+ sharedWidthsByFont.clear();
475
+ sharedAsciiWidthsByFont.clear();
476
+ }
477
+ widths ??= /* @__PURE__ */ new Map();
478
+ asciiWidths ??= (/* @__PURE__ */ new Float64Array(128)).fill(-1);
479
+ if (nativeBrowser) {
480
+ sharedWidthsByFont.set(layoutFont, widths);
481
+ sharedAsciiWidthsByFont.set(layoutFont, asciiWidths);
482
+ }
404
483
  }
405
484
  entry = [
406
485
  font,
407
486
  keys,
408
487
  layoutFont,
409
488
  widths,
410
- measuresKerning ? canvasWidth(layoutFont, widths, " ") : 0
489
+ measuresKerning ? canvasWidth(layoutFont, widths, asciiWidths, " ") : 0,
490
+ asciiKeys,
491
+ asciiWidths
411
492
  ];
412
493
  fontEntries.set(cacheKey, entry);
413
494
  }
@@ -419,7 +500,7 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
419
500
  if (isHidden(style)) return null;
420
501
  const opacity = opacityOf(parent);
421
502
  if (opacity <= 0) return null;
422
- const [font, keys, layoutFont, widths, space] = resolveFont(style);
503
+ const [font, keys, layoutFont, widths, space, asciiKeys, asciiWidths] = resolveFont(style);
423
504
  const spacing = parsePx(style.letterSpacing, 0, font.size);
424
505
  const wordSpacing = parsePx(style.wordSpacing, 0, font.size);
425
506
  const whiteSpace = style.whiteSpace;
@@ -427,9 +508,11 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
427
508
  decorations: readDecorations(parent, layoutFont, font.size, opacity),
428
509
  font,
429
510
  keys,
511
+ asciiKeys,
430
512
  fontSize: font.size * invScale,
431
513
  layoutFont,
432
514
  widths,
515
+ asciiWidths,
433
516
  letterSpacing: spacing,
434
517
  wordSpacing,
435
518
  space: space && (space + spacing + wordSpacing) * invScale,
@@ -440,22 +523,25 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
440
523
  };
441
524
  };
442
525
  const transformSeed = (node, transform) => transform.caseMap === "capitalize" ? precedingCharacter(node, styleOf) : void 0;
443
- const expansionBoxes = (share, box, font) => {
526
+ const expansionBoxes = (share, bLeft, bTop, bBottom, bWidth, bHeight, font) => {
444
527
  if (share.length <= (share.codePointAt(0) > 65535 ? 2 : 1)) return void 0;
445
528
  const parts = Array.from(share);
446
- fontProbe ??= document.createElement("canvas").getContext("2d");
447
- if (fontProbe) fontProbe.font = font.canvasFont;
448
- const advances = parts.map((part) => /^\p{M}/u.test(part) ? 0 : fontProbe?.measureText(part).width ?? 1);
529
+ const probe = ensureFontProbe();
530
+ if (probe) {
531
+ probe.font = font.canvasFont;
532
+ if (probe === kerningContext) lastKerningFont = "";
533
+ }
534
+ const advances = parts.map((part) => /^\p{M}/u.test(part) ? 0 : probe?.measureText(part).width ?? 1);
449
535
  const total = advances.reduce((sum, advance) => sum + advance, 0);
450
- let left = box.left;
536
+ let left = bLeft;
451
537
  return parts.map((c, index) => {
452
- const width = total > 0 ? box.width * advances[index] / total : box.width / parts.length;
538
+ const width = total > 0 ? bWidth * advances[index] / total : bWidth / parts.length;
453
539
  const item = {
454
540
  left,
455
- top: box.top,
456
- bottom: box.bottom,
541
+ top: bTop,
542
+ bottom: bBottom,
457
543
  width,
458
- height: box.height,
544
+ height: bHeight,
459
545
  c
460
546
  };
461
547
  left += width;
@@ -510,12 +596,13 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
510
596
  let maxRight = blockRect.right;
511
597
  let minTop = blockRect.top;
512
598
  let maxBottom = blockRect.bottom;
513
- const includeTextBox = (box) => {
514
- if (box.left < minLeft) minLeft = box.left;
515
- if (box.left + box.width > maxRight) maxRight = box.left + box.width;
516
- if (box.top < minTop) minTop = box.top;
517
- if (box.bottom > maxBottom) maxBottom = box.bottom;
599
+ const includeTextBox = (left, top, bottom, width) => {
600
+ if (left < minLeft) minLeft = left;
601
+ if (left + width > maxRight) maxRight = left + width;
602
+ if (top < minTop) minTop = top;
603
+ if (bottom > maxBottom) maxBottom = bottom;
518
604
  };
605
+ const includeTextBoxObj = (box) => includeTextBox(box.left, box.top, box.bottom, box.width);
519
606
  const blockStart = glyphs.length;
520
607
  const lineCountBefore = lineCount;
521
608
  let blockLeft = clip[0] + insetLeft * invScale;
@@ -542,7 +629,7 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
542
629
  captureShape(element, blockIndex, clip, sequence);
543
630
  } } : {},
544
631
  box: (font, box) => {
545
- includeTextBox(box);
632
+ includeTextBoxObj(box);
546
633
  if (measuresBaselines) {
547
634
  trackLayoutGrid(box.left, box.width);
548
635
  if (!boxHeights.has(font)) boxHeights.set(font, box.height);
@@ -562,9 +649,17 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
562
649
  } else if (layoutEngine === "range") {
563
650
  range ??= document.createRange();
564
651
  sequences.clear();
652
+ let hasLastSeq = false;
653
+ let lastSeqKey = 0;
654
+ let lastSeqNext = 0;
565
655
  const nextSequence = (lineKey) => {
656
+ if (hasLastSeq && lineKey === lastSeqKey) return lastSeqNext++;
657
+ if (hasLastSeq) sequences.set(lastSeqKey, lastSeqNext);
658
+ hasLastSeq = true;
659
+ lastSeqKey = lineKey;
566
660
  const seq = sequences.get(lineKey) ?? 0;
567
- sequences.set(lineKey, seq + 1);
661
+ lastSeqNext = seq + 1;
662
+ sequences.set(lineKey, lastSeqNext);
568
663
  return seq;
569
664
  };
570
665
  const walker = block.firstElementChild ? document.createTreeWalker(block, 5) : null;
@@ -612,7 +707,7 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
612
707
  previousCharacter = "";
613
708
  continue;
614
709
  }
615
- const { font, keys, fontSize, layoutFont, widths, letterSpacing, wordSpacing, space, keepsSpaces, color, z, decorations, transform } = record.inline;
710
+ const { font, keys, asciiKeys, fontSize, layoutFont, widths, asciiWidths, letterSpacing, wordSpacing, space, keepsSpaces, color, z, decorations, transform } = record.inline;
616
711
  let utf16Offset = 0;
617
712
  let textData = textNode.data;
618
713
  let shown = transform ? transformClusters(textData, transform, transformSeed(textNode, transform)) : void 0;
@@ -646,15 +741,47 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
646
741
  blockSynced = false;
647
742
  cachedTextRuns.set(textNode, hit = [
648
743
  runKey,
649
- [],
744
+ new Float64Array(textData.length * 5),
650
745
  void 0
651
746
  ]);
652
747
  }
653
748
  }
654
749
  const rects = hit?.[1];
750
+ const clusters = glyphClusters(textData);
751
+ const rawString = typeof clusters === "string";
752
+ const clusterArr = rawString ? null : clusters;
753
+ const totalItems = rawString ? textData.length : clusterArr.length;
754
+ let pos = 0;
655
755
  let ci = -1;
656
- for (const cluster of glyphClusters(textData)) {
756
+ while (pos < totalItems) {
657
757
  ci++;
758
+ if (rawString && !shown) {
759
+ if (textData.charCodeAt(pos) === 32) {
760
+ if (!gap || keepsSpaces) gap += space;
761
+ afterSpace = true;
762
+ hasMarkAnchor = hasKernPrevious = false;
763
+ previousCharacter = "";
764
+ pos++;
765
+ utf16Offset++;
766
+ continue;
767
+ }
768
+ }
769
+ let cluster;
770
+ let step;
771
+ if (rawString) {
772
+ const c0 = textData.charCodeAt(pos);
773
+ if (c0 >= 55296 && c0 <= 56319 && pos + 1 < totalItems && textData.charCodeAt(pos + 1) >= 56320 && textData.charCodeAt(pos + 1) <= 57343) {
774
+ step = 2;
775
+ cluster = textData.slice(pos, pos + 2);
776
+ } else {
777
+ step = 1;
778
+ cluster = c0 < 128 ? SINGLE_CHAR[c0] : textData[pos];
779
+ }
780
+ } else {
781
+ step = 1;
782
+ cluster = clusterArr[pos];
783
+ }
784
+ pos += step;
658
785
  const share = shown ? shown[clusterIndex++] : cluster;
659
786
  const nextOffset = utf16Offset + cluster.length;
660
787
  if (share === " " || share.length === 1 && share.charCodeAt(0) >= 9 && /^\s$/u.test(share)) {
@@ -672,36 +799,63 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
672
799
  utf16Offset = nextOffset;
673
800
  continue;
674
801
  }
675
- let r;
676
- if (rects && synced) r = rects[ci];
677
- else {
802
+ let bLeft;
803
+ let bTop;
804
+ let bBottom;
805
+ let bWidth;
806
+ let bHeight;
807
+ const boxOff = ci * 5;
808
+ if (rects && synced) {
809
+ bLeft = rects[boxOff];
810
+ bTop = rects[boxOff + 1];
811
+ bBottom = rects[boxOff + 2];
812
+ bWidth = rects[boxOff + 3];
813
+ bHeight = rects[boxOff + 4];
814
+ } else {
678
815
  range.setEnd(textNode, nextOffset);
679
816
  range.setStart(textNode, utf16Offset);
680
817
  const d = range.getBoundingClientRect();
681
- r = {
682
- left: d.left,
683
- top: d.top,
684
- bottom: d.bottom,
685
- width: d.width,
686
- height: d.height
687
- };
818
+ bLeft = d.left;
819
+ bTop = d.top;
820
+ bBottom = d.bottom;
821
+ bWidth = d.width;
822
+ bHeight = d.height;
688
823
  if (rects) {
689
- if (canSync && sameBox(r, rects[ci])) synced = true;
690
- else rects[ci] = r;
824
+ if (canSync && rects[boxOff] === bLeft && rects[boxOff + 1] === bTop && rects[boxOff + 3] === bWidth && rects[boxOff + 4] === bHeight) synced = true;
825
+ else {
826
+ rects[boxOff] = bLeft;
827
+ rects[boxOff + 1] = bTop;
828
+ rects[boxOff + 2] = bBottom;
829
+ rects[boxOff + 3] = bWidth;
830
+ rects[boxOff + 4] = bHeight;
831
+ }
691
832
  }
692
833
  }
693
834
  utf16Offset = nextOffset;
694
- if (measuresBaselines) trackLayoutGrid(r.left, r.width);
695
- const parts = share === cluster ? void 0 : expansionBoxes(share, r, font);
696
- for (let part = 0, partLen = parts ? parts.length : 1; part < partLen; part++) {
697
- const box = parts ? parts[part] : r;
698
- const character = parts ? parts[part].c : share;
699
- const { left: bLeft, top: bTop, bottom: bBottom, width: bWidth, height: bHeight } = box;
700
- if (bWidth <= 0 || bHeight <= 0) {
835
+ if (measuresBaselines) trackLayoutGrid(bLeft, bWidth);
836
+ const parts = share === cluster ? void 0 : expansionBoxes(share, bLeft, bTop, bBottom, bWidth, bHeight, font);
837
+ const partLen = parts ? parts.length : 1;
838
+ for (let part = 0; part < partLen; part++) {
839
+ let pLeft = bLeft;
840
+ let pTop = bTop;
841
+ let pBottom = bBottom;
842
+ let pWidth = bWidth;
843
+ let pHeight = bHeight;
844
+ let character = share;
845
+ if (parts) {
846
+ const box = parts[part];
847
+ pLeft = box.left;
848
+ pTop = box.top;
849
+ pBottom = box.bottom;
850
+ pWidth = box.width;
851
+ pHeight = box.height;
852
+ character = box.c;
853
+ }
854
+ if (pWidth <= 0 || pHeight <= 0) {
701
855
  const previous = glyphs[glyphs.length - 1];
702
856
  hasKernPrevious = false;
703
857
  if (!hasMarkAnchor || !previous || !/^\p{M}/u.test(character)) continue;
704
- const key = requestKey(font, keys, character);
858
+ const key = requestKey(font, keys, character, asciiKeys);
705
859
  const sequence = nextSequence(markAnchorLineKey);
706
860
  if (options.pixelSnap) paintLines[glyphs.length] = paintLines[glyphs.length - 1];
707
861
  glyphs.push({
@@ -720,43 +874,43 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
720
874
  });
721
875
  continue;
722
876
  }
723
- includeTextBox(box);
724
- const key = requestKey(font, keys, character);
877
+ includeTextBox(pLeft, pTop, pBottom, pWidth);
878
+ const key = requestKey(font, keys, character, asciiKeys);
725
879
  const previous = glyphs[glyphs.length - 1];
726
880
  const sameBlock = previous?.block === blockIndex;
727
881
  const emoji = emojiText && isEmojiCandidate(character);
728
- const sharesLine = sameBlock && hasPrevBounds && Math.min(bBottom, prevBottom) - Math.max(bTop, prevTop) > Math.min(bHeight, prevHeight) * .5;
882
+ const sharesLine = sameBlock && hasPrevBounds && Math.min(pBottom, prevBottom) - Math.max(pTop, prevTop) > Math.min(pHeight, prevHeight) * .5;
729
883
  const sameEmojiLine = sharesLine && (emoji || previousEmoji);
730
- const lineKey = sameEmojiLine ? markAnchorLineKey : Math.round(bTop * 4);
884
+ const lineKey = sameEmojiLine ? markAnchorLineKey : Math.round(pTop * 4);
731
885
  const sequence = nextSequence(lineKey);
732
- const lineBottom = sameEmojiLine ? previous.lineBottom : (rootBottom - bBottom) * invScale;
886
+ const lineBottom = sameEmojiLine ? previous.lineBottom : (rootBottom - pBottom) * invScale;
733
887
  const lineChanged = sameBlock && previous.lineBottom !== lineBottom;
734
888
  let kernShift = 0;
735
889
  let kernCarry = 0;
736
890
  let charWidth = -1;
737
891
  if (hasKernPrevious && measuresKerning && !parts && !emoji && !lineChanged) {
738
- const firstWidth = kernPrevWidth >= 0 ? kernPrevWidth : canvasWidth(layoutFont, widths, kernPrevChar);
739
- const shown = bLeft - kernPrevLeft - letterSpacing - firstWidth - kernPrevCarry;
892
+ const firstWidth = kernPrevWidth >= 0 ? kernPrevWidth : canvasWidth(layoutFont, widths, asciiWidths, kernPrevChar);
893
+ const shown = pLeft - kernPrevLeft - letterSpacing - firstWidth - kernPrevCarry;
740
894
  if (Math.abs(shown) > (nativeBrowser ? .02 : .0025)) {
741
- charWidth = canvasWidth(layoutFont, widths, character);
742
- const pair = canvasWidth(layoutFont, widths, kernPrevChar + character) - firstWidth - charWidth;
895
+ charWidth = canvasWidth(layoutFont, widths, asciiWidths, character);
896
+ const pair = canvasWidth(layoutFont, widths, asciiWidths, kernPrevChar + character) - firstWidth - charWidth;
743
897
  if (Math.abs(pair) > .01 && Math.abs(shown - pair / 2) <= Math.min(.25, Math.abs(pair) / 4)) {
744
898
  kernCarry = pair - shown;
745
899
  kernShift = kernCarry;
746
900
  }
747
901
  }
748
902
  }
749
- const originX = (bLeft - rootLeft + kernShift) * invScale;
750
- boxHeight ||= bHeight;
903
+ const originX = (pLeft - rootLeft + kernShift) * invScale;
904
+ boxHeight ||= pHeight;
751
905
  if (options.pixelSnap) {
752
- if (sharesLine) lineTops[lineTops.length - 1] = Math.min(lineTops.at(-1), bTop);
753
- else lineTops.push(bTop);
906
+ if (sharesLine) lineTops[lineTops.length - 1] = Math.min(lineTops.at(-1), pTop);
907
+ else lineTops.push(pTop);
754
908
  paintLines[glyphs.length] = lineTops.length - 1;
755
909
  }
756
910
  hasPrevBounds = true;
757
- prevTop = bTop;
758
- prevBottom = bBottom;
759
- prevHeight = bHeight;
911
+ prevTop = pTop;
912
+ prevBottom = pBottom;
913
+ prevHeight = pHeight;
760
914
  previousEmoji = emoji;
761
915
  const isCjkSoft = options.cjkLineBreaking === true && canBreakCjk(previousCharacter, character);
762
916
  if (sameBlock && !lineChanged && previous.shapeGroup === void 0) {
@@ -764,13 +918,13 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
764
918
  previous.advance = originX - previous.originX;
765
919
  }
766
920
  const soft = !sameBlock || isCjkSoft || afterSpace && !(forced && lineChanged);
767
- const lone = measuresKerning && !parts && !emoji ? (charWidth >= 0 ? charWidth : charWidth = canvasWidth(layoutFont, widths, character)) + letterSpacing : bWidth;
921
+ const lone = measuresKerning && !parts && !emoji ? (charWidth >= 0 ? charWidth : charWidth = canvasWidth(layoutFont, widths, asciiWidths, character)) + letterSpacing : pWidth;
768
922
  const glyph = {
769
923
  glyphKey: key,
770
924
  fontSize,
771
925
  originX,
772
926
  lineBottom,
773
- advance: (Math.abs(lone - bWidth) < .04 ? lone : bWidth) * invScale,
927
+ advance: (Math.abs(lone - pWidth) < .04 ? lone : pWidth) * invScale,
774
928
  block: blockIndex,
775
929
  breakBefore: soft ? "soft" : lineChanged ? "hard" : "none",
776
930
  clip,
@@ -791,7 +945,7 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
791
945
  else {
792
946
  hasKernPrevious = true;
793
947
  kernPrevChar = character;
794
- kernPrevLeft = bLeft;
948
+ kernPrevLeft = pLeft;
795
949
  kernPrevWidth = charWidth;
796
950
  kernPrevCarry = kernCarry;
797
951
  }
@@ -813,7 +967,7 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
813
967
  }
814
968
  lineCount += sequences.size;
815
969
  } else {
816
- const [lines, left, width] = pretextCapture[2](block, blockIndex, blockStyle, blockRect, clip, includeTextBox, align, lastAlign);
970
+ const [lines, left, width] = pretextCapture[2](block, blockIndex, blockStyle, blockRect, clip, includeTextBoxObj, align, lastAlign);
817
971
  lineCount += lines;
818
972
  blockLeft = left;
819
973
  blockWidth = width;
@@ -863,8 +1017,9 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
863
1017
  }
864
1018
  if (textRuns.length > 0) {
865
1019
  const scales = layoutScales();
866
- if (scales.length > 0 && (fontProbe ??= document.createElement("canvas").getContext("2d"))) {
867
- const [descents, scale] = layoutDescents(fontProbe, boxHeights, scales, bakeSize, metricsByFont);
1020
+ const probe = scales.length > 0 ? ensureFontProbe() : null;
1021
+ if (probe) {
1022
+ const [descents, scale] = layoutDescents(probe, boxHeights, scales, bakeSize, metricsByFont);
868
1023
  layoutScale = scale;
869
1024
  for (const [font, from, to] of textRuns) {
870
1025
  const descent = descents.get(font);
@@ -927,9 +1082,9 @@ function collectDomSnapshotSync(root, options, pretext, shaping, svg) {
927
1082
  for (const character of glyphClusters(options.extraGlyphs ?? "")) {
928
1083
  if (/^\s$/u.test(character)) continue;
929
1084
  const emoji = isEmojiCandidate(character);
930
- for (const [font, keys] of fontEntries.values()) {
1085
+ for (const [font, keys, , , , asciiKeys] of fontEntries.values()) {
931
1086
  if (!emoji && /\bemoji\b/i.test(font.family)) continue;
932
- requestKey(font, keys, character);
1087
+ requestKey(font, keys, character, asciiKeys);
933
1088
  }
934
1089
  }
935
1090
  if (svg) svg.normalizeSvgMotion(glyphs);