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
@@ -1,56 +1,8 @@
1
- var WINDFOIL_MAX_LAYERS = 1024;
1
+ import { checkFloat32, float32Bits, readBox, readFiniteFloat32, readFlatCoordinates } from "./outlineValidation.js";
2
2
  var FLOAT32_FAR = 3e38;
3
3
  function throwIfAborted(signal) {
4
4
  if (signal?.aborted) throw signal.reason ?? new DOMException("Windfoil preprocessing was aborted", "AbortError");
5
5
  }
6
- function readFiniteFloat32(value, label) {
7
- if (typeof value !== "number" || !Number.isFinite(value)) throw new TypeError(`${label} must be a finite number`);
8
- if (!Number.isFinite(Math.fround(value))) throw new RangeError(`${label} must be representable as a finite float32`);
9
- return value;
10
- }
11
- function readFlatNumbers(value, label, expectedLength) {
12
- if (typeof value !== "object" || value === null || !("length" in value)) throw new TypeError(`${label} must be an array-like list of numbers`);
13
- const length = value.length;
14
- if (!Number.isSafeInteger(length) || length < 0) throw new RangeError(`${label}.length must be a non-negative safe integer`);
15
- if (expectedLength !== void 0 && length !== expectedLength) throw new RangeError(`${label} must contain exactly ${expectedLength} numbers`);
16
- const source = value;
17
- const result = new Array(length);
18
- for (let index = 0; index < result.length; index++) {
19
- const item = source[index];
20
- result[index] = typeof item === "number" && Number.isFinite(item) && Number.isFinite(Math.fround(item)) ? item : readFiniteFloat32(item, `${label}[${index}]`);
21
- }
22
- return result;
23
- }
24
- function readFlatCoordinates(value, label) {
25
- if (value instanceof Float32Array) {
26
- for (let index = 0; index < value.length; index++) if (!Number.isFinite(value[index])) readFiniteFloat32(value[index], `${label}[${index}]`);
27
- return value;
28
- }
29
- if (value instanceof Float64Array) {
30
- for (let index = 0; index < value.length; index++) if (!Number.isFinite(Math.fround(value[index]))) readFiniteFloat32(value[index], `${label}[${index}]`);
31
- return value;
32
- }
33
- if (Array.isArray(value)) {
34
- const arr = value;
35
- const len = arr.length;
36
- for (let index = 0; index < len; index++) {
37
- const item = arr[index];
38
- if (typeof item !== "number" || !Number.isFinite(Math.fround(item))) readFiniteFloat32(item, `${label}[${index}]`);
39
- }
40
- return arr;
41
- }
42
- return readFlatNumbers(value, label);
43
- }
44
- function readBox(value, label) {
45
- const v = Array.isArray(value) && value.length === 4 && typeof value[0] === "number" && typeof value[1] === "number" && typeof value[2] === "number" && typeof value[3] === "number" && Number.isFinite(Math.fround(value[0]) + Math.fround(value[1]) + Math.fround(value[2]) + Math.fround(value[3])) ? value : readFlatNumbers(value, label, 4);
46
- if (v[0] > v[2] || v[1] > v[3]) throw new RangeError(`${label} minima must not exceed its maxima`);
47
- return [
48
- v[0],
49
- v[1],
50
- v[2],
51
- v[3]
52
- ];
53
- }
54
6
  function normalizeOutline(value, label, normalizeLayers) {
55
7
  if (typeof value !== "object" || value === null) throw new TypeError(`${label} must be an object`);
56
8
  const outline = value;
@@ -107,25 +59,23 @@ function equalValues(left, right) {
107
59
  const a = left, b = right, keys = Object.keys(a).filter((key) => key !== "paintLabel" && a[key] !== void 0), otherKeys = Object.keys(b).filter((key) => key !== "paintLabel" && b[key] !== void 0);
108
60
  return keys.length === otherKeys.length && keys.every((key) => equalValues(a[key], b[key]));
109
61
  }
62
+ function sameNums(a, b) {
63
+ if (a.length !== b.length) return false;
64
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
65
+ return true;
66
+ }
110
67
  function layersEqual(left, right) {
111
68
  if (left === right) return true;
112
69
  if (left?.length !== right?.length) return false;
113
70
  if (!left || !right) return true;
114
71
  for (let i = 0; i < left.length; i++) {
115
72
  const a = left[i], b = right[i];
116
- if (a.paintKind !== b.paintKind || a.bbox[0] !== b.bbox[0] || a.bbox[1] !== b.bbox[1] || a.bbox[2] !== b.bbox[2] || a.bbox[3] !== b.bbox[3] || a.rgba[0] !== b.rgba[0] || a.rgba[1] !== b.rgba[1] || a.rgba[2] !== b.rgba[2] || a.rgba[3] !== b.rgba[3] || a.quadratics.length !== b.quadratics.length) return false;
117
- for (let j = 0; j < a.quadratics.length; j++) if (a.quadratics[j] !== b.quadratics[j]) return false;
118
- if (!equalValues(a.paint, b.paint)) return false;
73
+ if (a.paintKind !== b.paintKind || !sameNums(a.bbox, b.bbox) || !sameNums(a.rgba, b.rgba) || !sameNums(a.quadratics, b.quadratics) || !equalValues(a.paint, b.paint)) return false;
119
74
  }
120
75
  return true;
121
76
  }
122
77
  function outlinesEqual(left, right) {
123
- if (left === right) return true;
124
- if (left.unitsPerEm !== right.unitsPerEm || left.ascent !== right.ascent || left.descent !== right.descent) return false;
125
- for (let index = 0; index < left.bbox.length; index++) if (left.bbox[index] !== right.bbox[index]) return false;
126
- if (left.quadratics.length !== right.quadratics.length) return false;
127
- for (let index = 0; index < left.quadratics.length; index++) if (left.quadratics[index] !== right.quadratics[index]) return false;
128
- return layersEqual(left.layers, right.layers) && equalValues(left.clipBox, right.clipBox);
78
+ return left === right || left.unitsPerEm === right.unitsPerEm && left.ascent === right.ascent && left.descent === right.descent && sameNums(left.bbox, right.bbox) && sameNums(left.quadratics, right.quadratics) && layersEqual(left.layers, right.layers) && equalValues(left.clipBox, right.clipBox);
129
79
  }
130
80
  function extremumParameter(start, control, end) {
131
81
  const secondDifference = start - 2 * control + end;
@@ -164,18 +114,8 @@ function splitOutline(quadratics, signal) {
164
114
  for (let offset = 0; offset < quadLen; offset += 6) {
165
115
  if ((offset & 1023) === 0 && offset % 1536 === 0) throwIfAborted(signal);
166
116
  const x0 = quadratics[offset], y0 = quadratics[offset + 1], controlX = quadratics[offset + 2], controlY = quadratics[offset + 3], x1 = quadratics[offset + 4], y1 = quadratics[offset + 5];
167
- if (controlX >= (x0 < x1 ? x0 : x1) && controlX <= (x0 > x1 ? x0 : x1) && controlY >= (y0 < y1 ? y0 : y1) && controlY <= (y0 > y1 ? y0 : y1)) {
168
- output[outLen] = x0;
169
- output[outLen + 1] = y0;
170
- output[outLen + 2] = controlX;
171
- output[outLen + 3] = controlY;
172
- output[outLen + 4] = x1;
173
- output[outLen + 5] = y1;
174
- outLen += 6;
175
- continue;
176
- }
177
- const xExtremum = extremumParameter(x0, controlX, x1);
178
- const yExtremum = extremumParameter(y0, controlY, y1);
117
+ const xExtremum = controlX >= (x0 < x1 ? x0 : x1) && controlX <= (x0 > x1 ? x0 : x1) ? void 0 : extremumParameter(x0, controlX, x1);
118
+ const yExtremum = controlY >= (y0 < y1 ? y0 : y1) && controlY <= (y0 > y1 ? y0 : y1) ? void 0 : extremumParameter(y0, controlY, y1);
179
119
  if (xExtremum === void 0 && yExtremum === void 0) {
180
120
  output[outLen] = x0;
181
121
  output[outLen + 1] = y0;
@@ -231,10 +171,10 @@ function monotoneRoot(a, hy, start, end, value, rising) {
231
171
  const root = -c / den;
232
172
  return root <= 0 ? 0 : root >= 1 ? 1 : root;
233
173
  }
234
- function bandWindingArea(pieces, bucket, xReference, bandMin, bandMax) {
174
+ function bandWindingArea(pieces, bucketData, bucketOffset, bucketLen, xReference, bandMin, bandMax) {
235
175
  let area = 0;
236
- for (let i = 0, n = bucket.length; i < n; i++) {
237
- const offset = bucket[i] * 6;
176
+ for (let i = 0; i < bucketLen; i++) {
177
+ const offset = bucketData[bucketOffset + i] * 6;
238
178
  const y0 = pieces[offset + 1];
239
179
  const y1 = pieces[offset + 5];
240
180
  if (y0 === y1) continue;
@@ -271,13 +211,6 @@ function bandWindingArea(pieces, bucket, xReference, bandMin, bandMax) {
271
211
  }
272
212
  return area;
273
213
  }
274
- var scratchF32 = /* @__PURE__ */ new Float32Array(1);
275
- var scratchU32 = new Uint32Array(scratchF32.buffer);
276
- function float32Bits(value, label) {
277
- scratchF32[0] = value;
278
- if (!Number.isFinite(scratchF32[0])) throw new RangeError(`${label} must be representable as a finite float32`);
279
- return scratchU32[0];
280
- }
281
214
  /**
282
215
  * The x at which as many band pieces start left of a fragment as end right of it.
283
216
  *
@@ -305,11 +238,19 @@ function assertUint32(value, label) {
305
238
  var scratchPieceXMin = /* @__PURE__ */ new Float64Array(256);
306
239
  var scratchPieceXMax = /* @__PURE__ */ new Float64Array(256);
307
240
  var scratchPieceHasYSpan = /* @__PURE__ */ new Uint8Array(256);
241
+ var scratchPieceFirstBand = /* @__PURE__ */ new Uint8Array(256);
242
+ var scratchPieceLastBand = /* @__PURE__ */ new Uint8Array(256);
308
243
  var scratchGather = /* @__PURE__ */ new Int32Array(256);
309
244
  var scratchByXMin = /* @__PURE__ */ new Int32Array(256);
310
245
  var scratchCurves = /* @__PURE__ */ new Float32Array(0);
311
246
  var scratchRows = /* @__PURE__ */ new Uint32Array(0);
312
- var scratchBuckets = Array.from({ length: 64 }, () => []);
247
+ var scratchBucketData = /* @__PURE__ */ new Int32Array(512);
248
+ var scratchBucketCounts = /* @__PURE__ */ new Int32Array(64);
249
+ var scratchBucketOffsets = /* @__PURE__ */ new Int32Array(65);
250
+ var scratchBandClipped = /* @__PURE__ */ new Float64Array(256);
251
+ var scratchBandSource = /* @__PURE__ */ new Int32Array(256);
252
+ var scratchBandXMin = /* @__PURE__ */ new Float64Array(256);
253
+ var scratchBandXMax = /* @__PURE__ */ new Float64Array(256);
313
254
  function ensureCurveCapacity(required, currentLen) {
314
255
  if (required <= scratchCurves.length) return scratchCurves;
315
256
  const next = new Float32Array(Math.max(scratchCurves.length * 2, required, 1024));
@@ -324,23 +265,30 @@ function ensureRowCapacity(required, currentLen) {
324
265
  scratchRows = next;
325
266
  return next;
326
267
  }
327
- function fileBands(pieces, pieceCount, y0, y1, packed, targetPiecesPerBand, signal) {
268
+ function fileBands(pieces, pieceCount, y0, y1, clipScale, packed, targetPiecesPerBand, signal) {
328
269
  const bandCount = chooseBandCount(pieceCount, targetPiecesPerBand);
329
270
  const invH = bandCount > 1 && y1 > y0 ? bandCount / (y1 - y0) : 0;
330
- const buckets = scratchBuckets;
331
- for (let band = 0; band < bandCount; band++) buckets[band].length = 0;
271
+ scratchBucketCounts.fill(0, 0, bandCount);
332
272
  if (pieceCount > scratchPieceXMin.length) {
333
273
  const cap = Math.max(scratchPieceXMin.length * 2, pieceCount);
334
274
  scratchPieceXMin = new Float64Array(cap);
335
275
  scratchPieceXMax = new Float64Array(cap);
336
276
  scratchPieceHasYSpan = new Uint8Array(cap);
277
+ scratchPieceFirstBand = new Uint8Array(cap);
278
+ scratchPieceLastBand = new Uint8Array(cap);
337
279
  scratchGather = new Int32Array(cap);
338
280
  scratchByXMin = new Int32Array(cap);
281
+ scratchBandSource = new Int32Array(cap);
282
+ scratchBandXMin = new Float64Array(cap);
283
+ scratchBandXMax = new Float64Array(cap);
339
284
  }
340
285
  const pieceXMin = scratchPieceXMin;
341
286
  const pieceXMax = scratchPieceXMax;
342
287
  const pieceHasYSpan = scratchPieceHasYSpan;
288
+ const pieceFirstBand = scratchPieceFirstBand;
289
+ const pieceLastBand = scratchPieceLastBand;
343
290
  let xLeft = Number.POSITIVE_INFINITY;
291
+ let xRight = Number.NEGATIVE_INFINITY;
344
292
  for (let pieceIndex = 0; pieceIndex < pieceCount; pieceIndex++) {
345
293
  if ((pieceIndex & 255) === 0) throwIfAborted(signal);
346
294
  const offset = pieceIndex * 6;
@@ -353,7 +301,9 @@ function fileBands(pieces, pieceCount, y0, y1, packed, targetPiecesPerBand, sign
353
301
  if (py2 > yMax) yMax = py2;
354
302
  const firstBand = bandIndex(yMin, y0, invH, bandCount);
355
303
  const lastBand = bandIndex(yMax, y0, invH, bandCount);
356
- for (let band = firstBand; band <= lastBand; band++) buckets[band].push(pieceIndex);
304
+ pieceFirstBand[pieceIndex] = firstBand;
305
+ pieceLastBand[pieceIndex] = lastBand;
306
+ for (let band = firstBand; band <= lastBand; band++) scratchBucketCounts[band]++;
357
307
  const fPy0 = Math.fround(py0);
358
308
  pieceHasYSpan[pieceIndex] = fPy0 !== Math.fround(py1) || fPy0 !== Math.fround(py2) ? 1 : 0;
359
309
  const px0 = pieces[offset];
@@ -366,77 +316,141 @@ function fileBands(pieces, pieceCount, y0, y1, packed, targetPiecesPerBand, sign
366
316
  pieceXMin[pieceIndex] = xMin;
367
317
  pieceXMax[pieceIndex] = xMax;
368
318
  if (xMin < xLeft) xLeft = xMin;
319
+ if (xMax > xRight) xRight = xMax;
320
+ }
321
+ let totalBanded = 0;
322
+ for (let band = 0; band < bandCount; band++) {
323
+ scratchBucketOffsets[band] = totalBanded;
324
+ totalBanded += scratchBucketCounts[band];
325
+ scratchBucketCounts[band] = scratchBucketOffsets[band];
326
+ }
327
+ scratchBucketOffsets[bandCount] = totalBanded;
328
+ if (totalBanded > scratchBucketData.length) scratchBucketData = new Int32Array(Math.max(scratchBucketData.length * 2, totalBanded));
329
+ const bucketData = scratchBucketData;
330
+ for (let pieceIndex = 0; pieceIndex < pieceCount; pieceIndex++) {
331
+ const firstBand = pieceFirstBand[pieceIndex];
332
+ const lastBand = pieceLastBand[pieceIndex];
333
+ for (let band = firstBand; band <= lastBand; band++) bucketData[scratchBucketCounts[band]++] = pieceIndex;
369
334
  }
370
335
  const rowBase = packed.rowLen / 6;
371
336
  assertUint32(rowBase, "rowBase");
372
337
  const exactBandH = bandCount > 1 ? (y1 - y0) / bandCount : y1 - y0;
373
- const roundedInvH = Math.fround(invH);
374
- if (!Number.isFinite(roundedInvH)) throw new RangeError("inverse band height must be representable as a finite float32");
375
- const bandH = bandCount > 1 && roundedInvH > 0 ? Math.fround(1 / roundedInvH) : Math.fround(exactBandH);
376
- if (!Number.isFinite(bandH)) throw new RangeError("band height must be representable as a finite float32");
338
+ const roundedInvH = checkFloat32(invH, "inverse band height");
339
+ const bandH = checkFloat32(bandCount > 1 && roundedInvH > 0 ? 1 / roundedInvH : exactBandH, "band height");
377
340
  const gather = scratchGather;
378
341
  const byXMin = scratchByXMin;
342
+ const bandSource = scratchBandSource;
343
+ const bandXMin = scratchBandXMin;
344
+ const bandXMax = scratchBandXMax;
379
345
  const rows = ensureRowCapacity(packed.rowLen + bandCount * 6, packed.rowLen);
346
+ const fY0 = Math.fround(y0);
347
+ const clipEps = Math.max(exactBandH, bandH) * .002 + clipScale * 6103515625e-14;
348
+ const clipBands = clipScale > 0 && bandCount > 1 && exactBandH > 0;
349
+ const minClipSpan = xRight > xLeft ? (xRight - xLeft) * .04 : 0;
380
350
  for (let band = 0; band < bandCount; band++) {
381
351
  throwIfAborted(signal);
382
- const bucket = buckets[band];
383
- const bucketLen = bucket.length;
384
- for (let i = 1; i < bucketLen; i++) {
385
- const idx = bucket[i];
386
- const key = pieceXMax[idx];
387
- let j = i - 1;
388
- while (j >= 0 && pieceXMax[bucket[j]] < key) {
389
- bucket[j + 1] = bucket[j];
390
- j--;
391
- }
392
- bucket[j + 1] = idx;
393
- }
352
+ const bucketOffset = scratchBucketOffsets[band];
353
+ const bucketLen = scratchBucketOffsets[band + 1] - bucketOffset;
394
354
  let gatherLen = 0;
355
+ let clippedFloats = 0;
395
356
  let xMin = FLOAT32_FAR;
396
357
  let xMax = -3e38;
358
+ const clipLo = !clipBands || band === 0 ? -Infinity : Math.min(y0 + band * exactBandH, fY0 + band * bandH) - clipEps;
359
+ const clipHi = !clipBands || band + 1 === bandCount ? Infinity : Math.max(y0 + (band + 1) * exactBandH, fY0 + (band + 1) * bandH) + clipEps;
397
360
  for (let i = 0; i < bucketLen; i++) {
398
- const pieceIndex = bucket[i];
399
- const pMin = pieceXMin[pieceIndex];
400
- const pMax = pieceXMax[pieceIndex];
401
- if (pMin < xMin) xMin = pMin;
402
- if (pMax > xMax) xMax = pMax;
403
- if (pieceHasYSpan[pieceIndex]) {
404
- gather[gatherLen] = pieceIndex;
405
- byXMin[gatherLen] = pieceIndex;
406
- gatherLen++;
361
+ const pieceIndex = bucketData[bucketOffset + i];
362
+ let pMin = pieceXMin[pieceIndex];
363
+ let pMax = pieceXMax[pieceIndex];
364
+ if (!pieceHasYSpan[pieceIndex]) {
365
+ if (pMin < xMin) xMin = pMin;
366
+ if (pMax > xMax) xMax = pMax;
367
+ continue;
407
368
  }
408
- }
409
- for (let i = 1; i < gatherLen; i++) {
410
- const idx = byXMin[i];
411
- const key = pieceXMin[idx];
412
- let j = i - 1;
413
- while (j >= 0 && pieceXMin[byXMin[j]] > key) {
414
- byXMin[j + 1] = byXMin[j];
415
- j--;
369
+ const offset = pieceIndex * 6;
370
+ let sourceOff = offset;
371
+ if (clipBands && pMax - pMin > minClipSpan) {
372
+ const needLo = pieceFirstBand[pieceIndex] < band;
373
+ const needHi = pieceLastBand[pieceIndex] > band;
374
+ if (needLo || needHi) {
375
+ const py0 = pieces[offset + 1];
376
+ const py1 = pieces[offset + 3];
377
+ const py2 = pieces[offset + 5];
378
+ const rising = py2 >= py0;
379
+ const ay = py0 - 2 * py1 + py2;
380
+ const hy = py1 - py0;
381
+ const ta = rising ? needLo ? monotoneRoot(ay, hy, py0, py2, clipLo, true) : 0 : needHi ? monotoneRoot(ay, hy, py0, py2, clipHi, false) : 0;
382
+ const tb = rising ? needHi ? monotoneRoot(ay, hy, py0, py2, clipHi, true) : 1 : needLo ? monotoneRoot(ay, hy, py0, py2, clipLo, false) : 1;
383
+ if ((ta > 0 || tb < 1) && tb > ta) {
384
+ const px0 = pieces[offset], px1 = pieces[offset + 2], px2 = pieces[offset + 4], ua = 1 - ta, ub = 1 - tb, qx0 = ua * ua * px0 + 2 * ua * ta * px1 + ta * ta * px2, qy0 = ua * ua * py0 + 2 * ua * ta * py1 + ta * ta * py2, qx2 = ub * ub * px0 + 2 * ub * tb * px1 + tb * tb * px2, qy2 = ub * ub * py0 + 2 * ub * tb * py1 + tb * tb * py2, cMinX = qx0 < qx2 ? qx0 : qx2, cMaxX = qx0 > qx2 ? qx0 : qx2, cMinY = qy0 < qy2 ? qy0 : qy2, cMaxY = qy0 > qy2 ? qy0 : qy2;
385
+ let qx1 = ua * ub * px0 + (ua * tb + ta * ub) * px1 + ta * tb * px2, qy1 = ua * ub * py0 + (ua * tb + ta * ub) * py1 + ta * tb * py2;
386
+ if (qx1 < cMinX) qx1 = cMinX;
387
+ else if (qx1 > cMaxX) qx1 = cMaxX;
388
+ if (qy1 < cMinY) qy1 = cMinY;
389
+ else if (qy1 > cMaxY) qy1 = cMaxY;
390
+ pMin = cMinX;
391
+ pMax = cMaxX;
392
+ if (clippedFloats + 6 > scratchBandClipped.length) {
393
+ const grown = new Float64Array(Math.max(scratchBandClipped.length * 2, clippedFloats + 6));
394
+ grown.set(scratchBandClipped.subarray(0, clippedFloats));
395
+ scratchBandClipped = grown;
396
+ }
397
+ scratchBandClipped[clippedFloats] = qx0;
398
+ scratchBandClipped[clippedFloats + 1] = qy0;
399
+ scratchBandClipped[clippedFloats + 2] = qx1;
400
+ scratchBandClipped[clippedFloats + 3] = qy1;
401
+ scratchBandClipped[clippedFloats + 4] = qx2;
402
+ scratchBandClipped[clippedFloats + 5] = qy2;
403
+ sourceOff = ~clippedFloats;
404
+ clippedFloats += 6;
405
+ }
406
+ }
416
407
  }
417
- byXMin[j + 1] = idx;
408
+ if (pMin < xMin) xMin = pMin;
409
+ if (pMax > xMax) xMax = pMax;
410
+ bandSource[gatherLen] = sourceOff;
411
+ bandXMin[gatherLen] = pMin;
412
+ bandXMax[gatherLen] = pMax;
413
+ gather[gatherLen] = gatherLen;
414
+ byXMin[gatherLen] = gatherLen;
415
+ gatherLen++;
418
416
  }
419
417
  const curveStart = packed.curveLen / 6;
420
418
  assertUint32(curveStart, "curveStart");
421
419
  assertUint32(gatherLen, "curveCount");
422
420
  const curves = ensureCurveCapacity(packed.curveLen + gatherLen * 12, packed.curveLen);
421
+ const bandClipped = scratchBandClipped;
423
422
  let curveWrite = packed.curveLen;
424
423
  for (let traversal = 0; traversal < 2; traversal++) {
425
424
  const list = traversal ? byXMin : gather;
425
+ const keys = traversal ? bandXMin : bandXMax;
426
+ for (let i = 1; i < gatherLen; i++) {
427
+ const idx = list[i], key = keys[idx];
428
+ let j = i - 1;
429
+ while (j >= 0 && (traversal ? keys[list[j]] > key : keys[list[j]] < key)) {
430
+ list[j + 1] = list[j];
431
+ j--;
432
+ }
433
+ list[j + 1] = idx;
434
+ }
435
+ const q2Start = curveWrite + gatherLen * 4;
426
436
  for (let i = 0; i < gatherLen; i++) {
427
- const offset = list[i] * 6;
428
- curves[curveWrite] = pieces[offset];
429
- curves[curveWrite + 1] = pieces[offset + 1];
430
- curves[curveWrite + 2] = pieces[offset + 2];
431
- curves[curveWrite + 3] = pieces[offset + 3];
432
- curves[curveWrite + 4] = pieces[offset + 4];
433
- curves[curveWrite + 5] = pieces[offset + 5];
434
- curveWrite += 6;
437
+ const srcOff = bandSource[list[i]];
438
+ const src = srcOff < 0 ? bandClipped : pieces;
439
+ const off = srcOff < 0 ? ~srcOff : srcOff;
440
+ const pairOff = curveWrite + i * 4;
441
+ const midOff = q2Start + i * 2;
442
+ curves[pairOff] = src[off];
443
+ curves[pairOff + 1] = src[off + 1];
444
+ curves[pairOff + 2] = src[off + 4];
445
+ curves[pairOff + 3] = src[off + 5];
446
+ curves[midOff] = src[off + 2];
447
+ curves[midOff + 1] = src[off + 3];
435
448
  }
449
+ curveWrite += gatherLen * 6;
436
450
  }
437
451
  packed.curveLen = curveWrite;
438
- const split = balancedSplit(byXMin, gather, gatherLen, pieceXMin, pieceXMax);
439
- const density = (pieceCount ? bandWindingArea(pieces, bucket, xLeft, y0 + band * exactBandH, y0 + (band + 1) * exactBandH) : 0) / Math.max(bandH * (xMax - xMin), 1e-30);
452
+ const split = balancedSplit(byXMin, gather, gatherLen, bandXMin, bandXMax);
453
+ const density = (pieceCount ? bandWindingArea(pieces, bucketData, bucketOffset, bucketLen, xLeft, y0 + band * exactBandH, y0 + (band + 1) * exactBandH) : 0) / Math.max(bandH * (xMax - xMin), 1e-30);
440
454
  const rowWrite = packed.rowLen;
441
455
  rows[rowWrite] = curveStart;
442
456
  rows[rowWrite + 1] = gatherLen;
@@ -453,9 +467,6 @@ function fileBands(pieces, pieceCount, y0, y1, packed, targetPiecesPerBand, sign
453
467
  invH: roundedInvH
454
468
  };
455
469
  }
456
- function compareKeys(left, right) {
457
- return left[0] < right[0] ? -1 : left[0] > right[0] ? 1 : 0;
458
- }
459
470
  function shouldTransposeLayer(pieces, pieceFloats, bbox, monotonePieceCount, bands) {
460
471
  const xLow = bbox[0], xHigh = bbox[2], xExtent = xHigh - xLow;
461
472
  const yLow = bbox[1], yHigh = bbox[3], yExtent = yHigh - yLow;
@@ -501,92 +512,81 @@ function preprocessWindfoilGlyphs(sources, options = {}) {
501
512
  let paintLayers = 0;
502
513
  let layered = false;
503
514
  try {
504
- const sortedGlyphs = [...unique.entries()].sort(compareKeys);
515
+ const sortedGlyphs = [...unique.entries()].sort((a, b) => a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0);
505
516
  for (const [key, outline] of sortedGlyphs) {
506
517
  throwIfAborted(options.signal);
507
- const layers = [];
508
- const orient = outline.layers && options.orientLayers?.(outline);
509
- const inputs = outline.layers ?? [{
510
- quadratics: outline.quadratics,
511
- bbox: outline.bbox,
512
- paint: { type: "foreground" },
513
- paintKind: 0,
514
- rgba: [
515
- 0,
516
- 0,
517
- 0,
518
- 1
519
- ],
520
- paintLabel: "paint"
521
- }];
522
- for (const layer of inputs) {
518
+ let rowBase = 0;
519
+ let bandCount = 0;
520
+ let bandH = 0;
521
+ let invH = 0;
522
+ let hasFirst = false;
523
+ let glyphPieces = 0;
524
+ const richLayers = outline.layers;
525
+ const layers = richLayers ? [] : void 0;
526
+ const orient = richLayers && options.orientLayers?.(outline);
527
+ const glyphBox = outline.clipBox ?? outline.bbox;
528
+ const glyphScale = richLayers ? 0 : Math.max(Math.abs(outline.bbox[0]), Math.abs(outline.bbox[1]), Math.abs(outline.bbox[2]), Math.abs(outline.bbox[3]), Math.abs(glyphBox[0]) + (glyphBox[2] - glyphBox[0]), Math.abs(glyphBox[1]) + (glyphBox[3] - glyphBox[1]), 1e-6);
529
+ const layerCount = richLayers ? richLayers.length : 1;
530
+ for (let l = 0; l < layerCount; l++) {
523
531
  throwIfAborted(options.signal);
524
- if (outline.layers && (!layer.quadratics.length || layer.paintKind === 1 && layer.rgba[3] === 0)) continue;
525
- const pieceFloats = splitOutline(layer.quadratics, options.signal);
532
+ const rich = richLayers?.[l];
533
+ if (rich && (!rich.quadratics.length || rich.paintKind === 1 && rich.rgba[3] === 0)) continue;
534
+ const pieceFloats = splitOutline(rich ? rich.quadratics : outline.quadratics, options.signal);
526
535
  const monotonePieceCount = pieceFloats / 6;
527
- if (outline.layers && monotonePieceCount === 0) continue;
536
+ if (rich && monotonePieceCount === 0) continue;
528
537
  monotonePieces += monotonePieceCount;
538
+ glyphPieces += monotonePieceCount;
539
+ if (monotonePieceCount > 0) paintLayers++;
529
540
  const pieces = scratchPieces;
541
+ const bbox = rich ? rich.bbox : outline.bbox;
530
542
  const bands = chooseBandCount(monotonePieceCount, targetPiecesPerBand);
531
- const transposed = !!orient && bands > 1 && shouldTransposeLayer(pieces, pieceFloats, layer.bbox, monotonePieceCount, bands);
543
+ const transposed = !!orient && bands > 1 && shouldTransposeLayer(pieces, pieceFloats, bbox, monotonePieceCount, bands);
532
544
  if (transposed) for (let i = 0; i < pieceFloats; i += 2) {
533
545
  const temp = pieces[i];
534
546
  pieces[i] = pieces[i + 1];
535
547
  pieces[i + 1] = temp;
536
548
  }
537
- const bandHeader = fileBands(pieces, monotonePieceCount, layer.bbox[transposed ? 0 : 1], layer.bbox[transposed ? 2 : 3], packed, targetPiecesPerBand, options.signal);
538
- const layerHeader = transposed ? {
539
- rowBase: bandHeader.rowBase,
540
- bandCount: bandHeader.bandCount,
541
- bandH: bandHeader.bandH,
542
- invH: bandHeader.invH,
543
- transposed: true,
544
- bbox: layer.bbox,
545
- monotonePieceCount,
546
- paintKind: layer.paintKind,
547
- rgba: layer.rgba,
548
- paint: layer.paint,
549
- paintLabel: layer.paintLabel
550
- } : {
551
- rowBase: bandHeader.rowBase,
552
- bandCount: bandHeader.bandCount,
553
- bandH: bandHeader.bandH,
554
- invH: bandHeader.invH,
555
- bbox: layer.bbox,
556
- monotonePieceCount,
557
- paintKind: layer.paintKind,
558
- rgba: layer.rgba,
559
- paint: layer.paint,
560
- paintLabel: layer.paintLabel
561
- };
562
- layers.push(layerHeader);
563
- }
564
- const first = layers[0] ?? {
565
- rowBase: 0,
566
- bandCount: 0,
567
- bandH: 0,
568
- invH: 0
569
- };
570
- let glyphPieces = 0;
571
- for (const layer of layers) {
572
- if (layer.monotonePieceCount > 0) paintLayers++;
573
- glyphPieces += layer.monotonePieceCount;
574
- if (!layered && (layer.paintKind !== 0 || layer.rgba[3] !== 1)) layered = true;
549
+ const h = fileBands(pieces, monotonePieceCount, bbox[transposed ? 0 : 1], bbox[transposed ? 2 : 3], glyphScale, packed, targetPiecesPerBand, options.signal);
550
+ if (!hasFirst) {
551
+ hasFirst = true;
552
+ rowBase = h.rowBase;
553
+ bandCount = h.bandCount;
554
+ bandH = h.bandH;
555
+ invH = h.invH;
556
+ }
557
+ if (rich && layers) {
558
+ if (!layered && (rich.paintKind !== 0 || rich.rgba[3] !== 1)) layered = true;
559
+ const lh = {
560
+ rowBase: h.rowBase,
561
+ bandCount: h.bandCount,
562
+ bandH: h.bandH,
563
+ invH: h.invH,
564
+ bbox: rich.bbox,
565
+ monotonePieceCount,
566
+ paintKind: rich.paintKind,
567
+ rgba: rich.rgba,
568
+ paint: rich.paint,
569
+ paintLabel: rich.paintLabel
570
+ };
571
+ if (transposed) lh.transposed = true;
572
+ layers.push(lh);
573
+ }
575
574
  }
576
- if (layers.length > 1) layered = true;
577
- glyphs.set(key, {
578
- rowBase: first.rowBase,
579
- bandCount: first.bandCount,
580
- bandH: first.bandH,
581
- invH: first.invH,
575
+ if (layers && layers.length > 1) layered = true;
576
+ const header = {
577
+ rowBase,
578
+ bandCount,
579
+ bandH,
580
+ invH,
582
581
  unitsPerEm: outline.unitsPerEm,
583
582
  ascent: outline.ascent,
584
583
  descent: outline.descent,
585
584
  bbox: outline.bbox,
586
- monotonePieceCount: glyphPieces,
587
- ...outline.layers ? { layers } : {},
588
- ...outline.clipBox ? { clipBox: outline.clipBox } : {}
589
- });
585
+ monotonePieceCount: glyphPieces
586
+ };
587
+ if (layers) header.layers = layers;
588
+ if (outline.clipBox) header.clipBox = outline.clipBox;
589
+ glyphs.set(key, header);
590
590
  }
591
591
  throwIfAborted(options.signal);
592
592
  let bandedPieces = 0;
@@ -606,18 +606,22 @@ function preprocessWindfoilGlyphs(sources, options = {}) {
606
606
  }
607
607
  };
608
608
  } finally {
609
- if (scratchPieces.length > 98304) {
610
- for (let i = 0; i < 64; i++) scratchBuckets[i].length = 0;
611
- scratchPieces = /* @__PURE__ */ new Float64Array(1024);
612
- }
609
+ if (scratchPieces.length > 98304) scratchPieces = /* @__PURE__ */ new Float64Array(1024);
610
+ if (scratchBucketData.length > 65536) scratchBucketData = /* @__PURE__ */ new Int32Array(512);
611
+ if (scratchBandClipped.length > 65536) scratchBandClipped = /* @__PURE__ */ new Float64Array(256);
613
612
  if (scratchCurves.length > 262144) scratchCurves = /* @__PURE__ */ new Float32Array(0);
614
613
  if (scratchRows.length > 65536) scratchRows = /* @__PURE__ */ new Uint32Array(0);
615
614
  if (scratchPieceXMin.length > 16384) {
616
615
  scratchPieceXMin = /* @__PURE__ */ new Float64Array(256);
617
616
  scratchPieceXMax = /* @__PURE__ */ new Float64Array(256);
618
617
  scratchPieceHasYSpan = /* @__PURE__ */ new Uint8Array(256);
618
+ scratchPieceFirstBand = /* @__PURE__ */ new Uint8Array(256);
619
+ scratchPieceLastBand = /* @__PURE__ */ new Uint8Array(256);
619
620
  scratchGather = /* @__PURE__ */ new Int32Array(256);
620
621
  scratchByXMin = /* @__PURE__ */ new Int32Array(256);
622
+ scratchBandSource = /* @__PURE__ */ new Int32Array(256);
623
+ scratchBandXMin = /* @__PURE__ */ new Float64Array(256);
624
+ scratchBandXMax = /* @__PURE__ */ new Float64Array(256);
621
625
  }
622
626
  }
623
627
  }
@@ -628,6 +632,6 @@ function preprocessWindfoilOutlines(sources, options = {}) {
628
632
  outline: source.outline
629
633
  })), options);
630
634
  }
631
- export { WINDFOIL_MAX_LAYERS, preprocessWindfoilGlyphs, preprocessWindfoilOutlines, readBox, readFiniteFloat32, readFlatCoordinates, readFlatNumbers, validateWindfoilGlyphOutline };
635
+ export { preprocessWindfoilGlyphs, preprocessWindfoilOutlines, readBox, readFiniteFloat32, readFlatCoordinates, validateWindfoilGlyphOutline };
632
636
 
633
637
  //# sourceMappingURL=preprocessCore.js.map