ultra-text 1.0.1 → 1.0.3

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 (237) hide show
  1. package/README.md +339 -1039
  2. package/dist/assets/cpuSdf.worker-Dy4P5_TI.js +466 -0
  3. package/dist/assets/cpuSdf.worker-Dy4P5_TI.js.map +1 -0
  4. package/dist/assets/traceOutline.worker-B5uVKmR4.js +1725 -0
  5. package/dist/assets/traceOutline.worker-B5uVKmR4.js.map +1 -0
  6. package/dist/atlas/appendAtlas.js +97 -31
  7. package/dist/atlas/appendAtlas.js.map +1 -1
  8. package/dist/atlas/buildAtlas.js +4 -3
  9. package/dist/atlas/buildAtlas.js.map +1 -1
  10. package/dist/atlas/colorGlyphs.d.ts +0 -10
  11. package/dist/atlas/colorGlyphs.js +97 -155
  12. package/dist/atlas/colorGlyphs.js.map +1 -1
  13. package/dist/atlas/colorTexture.d.ts +1 -0
  14. package/dist/atlas/colorTexture.js +64 -0
  15. package/dist/atlas/colorTexture.js.map +1 -0
  16. package/dist/atlas/cpuAtlas.d.ts +4 -1
  17. package/dist/atlas/cpuAtlas.js +14 -7
  18. package/dist/atlas/cpuAtlas.js.map +1 -1
  19. package/dist/atlas/cpuSdf.d.ts +1 -1
  20. package/dist/atlas/cpuSdf.js +235 -68
  21. package/dist/atlas/cpuSdf.js.map +1 -1
  22. package/dist/atlas/cpuWorker.d.ts +11 -3
  23. package/dist/atlas/cpuWorker.js +179 -22
  24. package/dist/atlas/cpuWorker.js.map +1 -1
  25. package/dist/atlas/gpuAtlas.js +188 -287
  26. package/dist/atlas/gpuAtlas.js.map +1 -1
  27. package/dist/atlas/gpuCommon.d.ts +257 -0
  28. package/dist/atlas/gpuCommon.js +845 -0
  29. package/dist/atlas/gpuCommon.js.map +1 -0
  30. package/dist/atlas/jfaWgsl.d.ts +1 -1
  31. package/dist/atlas/jfaWgsl.js +226 -249
  32. package/dist/atlas/jfaWgsl.js.map +1 -1
  33. package/dist/atlas/rasterize.d.ts +22 -0
  34. package/dist/atlas/rasterize.js +329 -158
  35. package/dist/atlas/rasterize.js.map +1 -1
  36. package/dist/atlas/shapeRaster.js +2 -1
  37. package/dist/atlas/shapeRaster.js.map +1 -1
  38. package/dist/atlas/vanillaBounds.d.ts +64 -0
  39. package/dist/atlas/vanillaBounds.js +42 -0
  40. package/dist/atlas/vanillaBounds.js.map +1 -0
  41. package/dist/atlas/vanillaCore.d.ts +100 -0
  42. package/dist/atlas/vanillaCore.js +1065 -0
  43. package/dist/atlas/vanillaCore.js.map +1 -0
  44. package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
  45. package/dist/atlas/vanillaCpuAtlas.js +72 -0
  46. package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
  47. package/dist/atlas/vanillaExtras.d.ts +8 -0
  48. package/dist/atlas/vanillaExtras.js +167 -0
  49. package/dist/atlas/vanillaExtras.js.map +1 -0
  50. package/dist/atlas/vanillaGpuHelpers.d.ts +295 -0
  51. package/dist/atlas/vanillaGpuHelpers.js +354 -0
  52. package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
  53. package/dist/dom/DomSdf.js +243 -297
  54. package/dist/dom/DomSdf.js.map +1 -1
  55. package/dist/dom/DomSlug.js +94 -165
  56. package/dist/dom/DomSlug.js.map +1 -1
  57. package/dist/dom/Text.d.ts +4 -1
  58. package/dist/dom/Text.js +5 -1
  59. package/dist/dom/Text.js.map +1 -1
  60. package/dist/dom/browserShaping.js +31 -14
  61. package/dist/dom/browserShaping.js.map +1 -1
  62. package/dist/dom/clusters.js +13 -2
  63. package/dist/dom/clusters.js.map +1 -1
  64. package/dist/dom/cssEffects.d.ts +4 -0
  65. package/dist/dom/cssEffects.js +72 -0
  66. package/dist/dom/cssEffects.js.map +1 -0
  67. package/dist/dom/domAnalytic.d.ts +53 -0
  68. package/dist/dom/domAnalytic.js +282 -0
  69. package/dist/dom/domAnalytic.js.map +1 -0
  70. package/dist/dom/domExtras.d.ts +27 -0
  71. package/dist/dom/domExtras.js +137 -0
  72. package/dist/dom/domExtras.js.map +1 -0
  73. package/dist/dom/fontCanvas.d.ts +16 -0
  74. package/dist/dom/fontCanvas.js +31 -13
  75. package/dist/dom/fontCanvas.js.map +1 -1
  76. package/dist/dom/layoutSignature.js +175 -57
  77. package/dist/dom/layoutSignature.js.map +1 -1
  78. package/dist/dom/markup.d.ts +10 -0
  79. package/dist/dom/markup.js +16 -1
  80. package/dist/dom/markup.js.map +1 -1
  81. package/dist/dom/pretextLayout.js +78 -28
  82. package/dist/dom/pretextLayout.js.map +1 -1
  83. package/dist/dom/snapshot.d.ts +4 -0
  84. package/dist/dom/snapshot.js +891 -484
  85. package/dist/dom/snapshot.js.map +1 -1
  86. package/dist/dom/snapshotsEqual.d.ts +7 -0
  87. package/dist/dom/snapshotsEqual.js +68 -0
  88. package/dist/dom/snapshotsEqual.js.map +1 -0
  89. package/dist/dom/svgCapture.d.ts +8 -1
  90. package/dist/dom/svgCapture.js +105 -28
  91. package/dist/dom/svgCapture.js.map +1 -1
  92. package/dist/dom/textTransform.d.ts +13 -1
  93. package/dist/dom/textTransform.js +31 -13
  94. package/dist/dom/textTransform.js.map +1 -1
  95. package/dist/dom/tone.js +91 -44
  96. package/dist/dom/tone.js.map +1 -1
  97. package/dist/index.js +4 -3
  98. package/dist/slug/SlugMaterial.d.ts +17 -3
  99. package/dist/slug/SlugMaterial.js +98 -26
  100. package/dist/slug/SlugMaterial.js.map +1 -1
  101. package/dist/slug/cff.d.ts +5 -0
  102. package/dist/slug/cff.js +780 -0
  103. package/dist/slug/cff.js.map +1 -0
  104. package/dist/slug/coverage.d.ts +19 -15
  105. package/dist/slug/coverage.js +380 -225
  106. package/dist/slug/coverage.js.map +1 -1
  107. package/dist/slug/preprocessCore.d.ts +6 -2
  108. package/dist/slug/preprocessCore.js +199 -148
  109. package/dist/slug/preprocessCore.js.map +1 -1
  110. package/dist/slug/runtime.d.ts +1 -0
  111. package/dist/slug/runtime.js +113 -74
  112. package/dist/slug/runtime.js.map +1 -1
  113. package/dist/slug/standaloneRuntime.d.ts +3 -2
  114. package/dist/slug/standaloneRuntime.js +112 -67
  115. package/dist/slug/standaloneRuntime.js.map +1 -1
  116. package/dist/slug/truetype.d.ts +14 -6
  117. package/dist/slug/truetype.js +356 -255
  118. package/dist/slug/truetype.js.map +1 -1
  119. package/dist/slug/vanillaCore.d.ts +410 -0
  120. package/dist/slug/vanillaCore.js +1630 -0
  121. package/dist/slug/vanillaCore.js.map +1 -0
  122. package/dist/slug-vanilla.d.ts +13 -364
  123. package/dist/slug-vanilla.js +7 -1454
  124. package/dist/slug.d.ts +2 -2
  125. package/dist/slug.js +7 -7
  126. package/dist/text/GlyphTextMaterial.d.ts +6 -1
  127. package/dist/text/GlyphTextMaterial.js +16 -12
  128. package/dist/text/GlyphTextMaterial.js.map +1 -1
  129. package/dist/text/RectMaterial.js +6 -10
  130. package/dist/text/RectMaterial.js.map +1 -1
  131. package/dist/text/SdfTextMaterial.d.ts +9 -3
  132. package/dist/text/SdfTextMaterial.js +125 -38
  133. package/dist/text/SdfTextMaterial.js.map +1 -1
  134. package/dist/text/decorations.d.ts +7 -2
  135. package/dist/text/decorations.js +187 -79
  136. package/dist/text/decorations.js.map +1 -1
  137. package/dist/text/geometry.d.ts +3 -22
  138. package/dist/text/geometryUtils.d.ts +16 -1
  139. package/dist/text/geometryUtils.js +83 -8
  140. package/dist/text/geometryUtils.js.map +1 -1
  141. package/dist/text/glyphGeometry.d.ts +9 -0
  142. package/dist/text/glyphGeometry.js +434 -0
  143. package/dist/text/glyphGeometry.js.map +1 -0
  144. package/dist/text/index.d.ts +2 -1
  145. package/dist/text/rectGeometry.d.ts +13 -0
  146. package/dist/text/rectGeometry.js +101 -0
  147. package/dist/text/rectGeometry.js.map +1 -0
  148. package/dist/text/reflow.d.ts +23 -1
  149. package/dist/text/reflow.js +223 -94
  150. package/dist/text/reflow.js.map +1 -1
  151. package/dist/text/snap.d.ts +15 -0
  152. package/dist/text/snap.js +26 -0
  153. package/dist/text/snap.js.map +1 -0
  154. package/dist/text/tone.js +2 -2
  155. package/dist/text/tone.js.map +1 -1
  156. package/dist/types.d.ts +23 -7
  157. package/dist/vanilla.d.ts +13 -363
  158. package/dist/vanilla.js +7 -1474
  159. package/dist/windfoil/WindfoilCompositeMaterial.d.ts +1 -1
  160. package/dist/windfoil/WindfoilCompositeMaterial.js +10 -3
  161. package/dist/windfoil/WindfoilCompositeMaterial.js.map +1 -1
  162. package/dist/windfoil/WindfoilMaterial.d.ts +12 -5
  163. package/dist/windfoil/WindfoilMaterial.js +71 -8
  164. package/dist/windfoil/WindfoilMaterial.js.map +1 -1
  165. package/dist/windfoil/compositeGeometry.js +12 -5
  166. package/dist/windfoil/compositeGeometry.js.map +1 -1
  167. package/dist/windfoil/coverage.d.ts +10 -2
  168. package/dist/windfoil/coverage.js +147 -59
  169. package/dist/windfoil/coverage.js.map +1 -1
  170. package/dist/windfoil/geometry.d.ts +17 -2
  171. package/dist/windfoil/geometry.js +220 -57
  172. package/dist/windfoil/geometry.js.map +1 -1
  173. package/dist/windfoil/layerGrid.js +8 -8
  174. package/dist/windfoil/layerGrid.js.map +1 -1
  175. package/dist/windfoil/mixed.d.ts +1 -0
  176. package/dist/windfoil/mixed.js +57 -29
  177. package/dist/windfoil/mixed.js.map +1 -1
  178. package/dist/windfoil/preprocess.js +2 -2
  179. package/dist/windfoil/preprocessCore.d.ts +2 -0
  180. package/dist/windfoil/preprocessCore.js +69 -37
  181. package/dist/windfoil/preprocessCore.js.map +1 -1
  182. package/dist/windfoil/requests.d.ts +1 -1
  183. package/dist/windfoil/requests.js +11 -4
  184. package/dist/windfoil/requests.js.map +1 -1
  185. package/dist/windfoil/resolveOutline.d.ts +9 -0
  186. package/dist/windfoil/resolveOutline.js +20 -0
  187. package/dist/windfoil/resolveOutline.js.map +1 -0
  188. package/dist/windfoil/runtime.d.ts +4 -1
  189. package/dist/windfoil/runtime.js +69 -63
  190. package/dist/windfoil/runtime.js.map +1 -1
  191. package/dist/windfoil/shapeGeometry.js +80 -34
  192. package/dist/windfoil/shapeGeometry.js.map +1 -1
  193. package/dist/windfoil/sizeSwitch.d.ts +1 -1
  194. package/dist/windfoil/sizeSwitch.js +13 -8
  195. package/dist/windfoil/sizeSwitch.js.map +1 -1
  196. package/dist/windfoil/storage.d.ts +78 -6
  197. package/dist/windfoil/storage.js +166 -11
  198. package/dist/windfoil/storage.js.map +1 -1
  199. package/dist/windfoil/svg/curves.d.ts +20 -0
  200. package/dist/windfoil/svg/curves.js +145 -42
  201. package/dist/windfoil/svg/curves.js.map +1 -1
  202. package/dist/windfoil/svg/document.js +10 -1
  203. package/dist/windfoil/svg/document.js.map +1 -1
  204. package/dist/windfoil/svg/pathConverter.js +66 -50
  205. package/dist/windfoil/svg/pathConverter.js.map +1 -1
  206. package/dist/windfoil/svg/pathData.d.ts +9 -0
  207. package/dist/windfoil/svg/pathData.js +142 -109
  208. package/dist/windfoil/svg/pathData.js.map +1 -1
  209. package/dist/windfoil/svg/winding.js +34 -10
  210. package/dist/windfoil/svg/winding.js.map +1 -1
  211. package/dist/windfoil/trace/contours.d.ts +5 -0
  212. package/dist/windfoil/trace/contours.js +256 -62
  213. package/dist/windfoil/trace/contours.js.map +1 -1
  214. package/dist/windfoil/trace/fit.d.ts +3 -1
  215. package/dist/windfoil/trace/fit.js +915 -265
  216. package/dist/windfoil/trace/fit.js.map +1 -1
  217. package/dist/windfoil/trace/outline.js +55 -30
  218. package/dist/windfoil/trace/outline.js.map +1 -1
  219. package/dist/windfoil/trace/traceOutline.worker.d.ts +24 -0
  220. package/dist/windfoil/trace/traceWorker.d.ts +13 -0
  221. package/dist/windfoil/trace/traceWorker.js +192 -0
  222. package/dist/windfoil/trace/traceWorker.js.map +1 -0
  223. package/dist/windfoil/trace/types.d.ts +10 -0
  224. package/dist/windfoil/tracedOutlines.d.ts +2 -0
  225. package/dist/windfoil/tracedOutlines.js +393 -103
  226. package/dist/windfoil/tracedOutlines.js.map +1 -1
  227. package/package.json +48 -29
  228. package/skills/ultra-text/SKILL.md +9 -10
  229. package/skills/ultra-text/references/frameworks.md +46 -2
  230. package/skills/ultra-text/references/three-tsl.md +27 -25
  231. package/skills/ultra-text/references/vanilla-webgpu.md +5 -2
  232. package/dist/assets/cpuSdf.worker-xS57yQbP.js +0 -322
  233. package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +0 -1
  234. package/dist/slug-vanilla.js.map +0 -1
  235. package/dist/text/geometry.js +0 -348
  236. package/dist/text/geometry.js.map +0 -1
  237. package/dist/vanilla.js.map +0 -1
@@ -1,3 +1,4 @@
1
+ import { createCffGlyphDecoder } from "./cff.js";
1
2
  // src/slug/truetype.ts
2
3
  function isArrayBufferLike(value) {
3
4
  return value instanceof ArrayBuffer || Object.prototype.toString.call(value) === "[object ArrayBuffer]";
@@ -22,6 +23,11 @@ var ttExX = /* @__PURE__ */ new Float64Array(512);
22
23
  var ttExY = /* @__PURE__ */ new Float64Array(512);
23
24
  var ttExOn = /* @__PURE__ */ new Uint8Array(512);
24
25
  var ttQuads = /* @__PURE__ */ new Float32Array(1024);
26
+ var compX = /* @__PURE__ */ new Float64Array(256);
27
+ var compY = /* @__PURE__ */ new Float64Array(256);
28
+ var compOn = /* @__PURE__ */ new Uint8Array(256);
29
+ var compStarts = /* @__PURE__ */ new Uint32Array(32);
30
+ var compLens = /* @__PURE__ */ new Uint32Array(32);
25
31
  function ensurePointScratch(count) {
26
32
  if (count <= ttFlags.length) return;
27
33
  let size = ttFlags.length;
@@ -30,6 +36,35 @@ function ensurePointScratch(count) {
30
36
  ttXs = new Int32Array(size);
31
37
  ttYs = new Int32Array(size);
32
38
  }
39
+ function ensureCompositePoints(required, currentLen) {
40
+ if (required <= compX.length) return;
41
+ let size = compX.length;
42
+ while (size < required) size *= 2;
43
+ const nx = new Float64Array(size);
44
+ const ny = new Float64Array(size);
45
+ const no = new Uint8Array(size);
46
+ if (currentLen > 0) {
47
+ nx.set(compX.subarray(0, currentLen));
48
+ ny.set(compY.subarray(0, currentLen));
49
+ no.set(compOn.subarray(0, currentLen));
50
+ }
51
+ compX = nx;
52
+ compY = ny;
53
+ compOn = no;
54
+ }
55
+ function ensureCompositeContours(required, currentLen) {
56
+ if (required <= compStarts.length) return;
57
+ let size = compStarts.length;
58
+ while (size < required) size *= 2;
59
+ const ns = new Uint32Array(size);
60
+ const nl = new Uint32Array(size);
61
+ if (currentLen > 0) {
62
+ ns.set(compStarts.subarray(0, currentLen));
63
+ nl.set(compLens.subarray(0, currentLen));
64
+ }
65
+ compStarts = ns;
66
+ compLens = nl;
67
+ }
33
68
  function ensureExpandedScratch(count) {
34
69
  if (count <= ttExX.length) return;
35
70
  let size = ttExX.length;
@@ -46,17 +81,79 @@ function ensureQuadScratch(required, currentLen) {
46
81
  if (currentLen > 0) next.set(ttQuads.subarray(0, currentLen));
47
82
  ttQuads = next;
48
83
  }
84
+ function emitContourRange(xs, ys, onMask, first, len, quadLen) {
85
+ if (len < 2) return quadLen;
86
+ ensureExpandedScratch(len * 2);
87
+ let exLen = 0;
88
+ let firstOn = -1;
89
+ for (let i = 0; i < len; i++) {
90
+ const p0 = first + i;
91
+ const p1 = first + (i + 1 === len ? 0 : i + 1);
92
+ const x0 = xs[p0];
93
+ const y0 = ys[p0];
94
+ const on0 = onMask[p0] & 1;
95
+ if (on0 && firstOn < 0) firstOn = exLen;
96
+ ttExX[exLen] = x0;
97
+ ttExY[exLen] = y0;
98
+ ttExOn[exLen] = on0;
99
+ exLen++;
100
+ if (!on0 && !(onMask[p1] & 1)) {
101
+ if (firstOn < 0) firstOn = exLen;
102
+ ttExX[exLen] = (x0 + xs[p1]) * .5;
103
+ ttExY[exLen] = (y0 + ys[p1]) * .5;
104
+ ttExOn[exLen] = 1;
105
+ exLen++;
106
+ }
107
+ }
108
+ if (firstOn < 0) return quadLen;
109
+ ensureQuadScratch(quadLen + exLen * 6, quadLen);
110
+ let curX = ttExX[firstOn];
111
+ let curY = ttExY[firstOn];
112
+ let step = 1;
113
+ while (step <= exLen) {
114
+ const rawIdx = firstOn + step;
115
+ const idx = rawIdx >= exLen ? rawIdx - exLen : rawIdx;
116
+ if (ttExOn[idx]) {
117
+ const px = ttExX[idx];
118
+ const py = ttExY[idx];
119
+ ttQuads[quadLen++] = curX;
120
+ ttQuads[quadLen++] = curY;
121
+ ttQuads[quadLen++] = (curX + px) * .5;
122
+ ttQuads[quadLen++] = (curY + py) * .5;
123
+ ttQuads[quadLen++] = px;
124
+ ttQuads[quadLen++] = py;
125
+ curX = px;
126
+ curY = py;
127
+ step++;
128
+ } else {
129
+ const rawEnd = firstOn + step + 1;
130
+ const endIdx = rawEnd >= exLen ? rawEnd - exLen : rawEnd;
131
+ const endX = ttExX[endIdx];
132
+ const endY = ttExY[endIdx];
133
+ ttQuads[quadLen++] = curX;
134
+ ttQuads[quadLen++] = curY;
135
+ ttQuads[quadLen++] = ttExX[idx];
136
+ ttQuads[quadLen++] = ttExY[idx];
137
+ ttQuads[quadLen++] = endX;
138
+ ttQuads[quadLen++] = endY;
139
+ curX = endX;
140
+ curY = endY;
141
+ step += 2;
142
+ }
143
+ }
144
+ return quadLen;
145
+ }
49
146
  /**
50
- * Lightweight runtime TrueType (`.ttf` `glyf` table) outline reader for Slug.
51
- * Supports Unicode `cmap` formats 4 and 12, simple quadratic contours (including
52
- * implied midpoints and all-off-curve rings), and offset/affine composite glyphs.
147
+ * Lightweight runtime TrueType (`.ttf` `glyf` table) and OpenType CFF (`.otf` `CFF ` table)
148
+ * outline reader for Slug. Supports Unicode `cmap` formats 4 and 12, simple & composite TrueType
149
+ * contours, and CFF Type 2 CharStrings with adaptive cubic-to-quadratic conversion.
53
150
  */
54
151
  function parseTrueTypeFont(source) {
55
152
  const view = toDataView(source);
56
- if (view.byteLength < 12) throw new Error("parseTrueTypeFont: buffer is too short to be a TrueType font");
153
+ if (view.byteLength < 12) throw new Error("parseTrueTypeFont: buffer is too short to be a TrueType or OpenType font");
57
154
  const sfntVersion = view.getUint32(0);
58
- if (sfntVersion === 2001684018 || sfntVersion === 2001684038) throw new Error("parseTrueTypeFont: compressed WOFF/WOFF2 buffers are unsupported; pass an uncompressed .ttf buffer or omit outlineProvider to trace loaded webfonts");
59
- if (sfntVersion === 1330926671) throw new Error("parseTrueTypeFont: OpenType CFF (.otf) outlines are unsupported; pass a TrueType (.ttf) buffer or omit outlineProvider to trace loaded webfonts");
155
+ if (sfntVersion === 2001684018) throw new Error("parseTrueTypeFont: compressed WOFF2 buffers require a decodeWoff2 hook; pass an uncompressed .ttf/.otf buffer, supply decodeWoff2 to createFontOutlineProvider, or omit outlineProvider to trace loaded webfonts");
156
+ if (sfntVersion === 2001684038) throw new Error("parseTrueTypeFont: compressed WOFF buffers are unsupported; pass an uncompressed .ttf/.otf buffer or omit outlineProvider to trace loaded webfonts");
60
157
  const numTables = view.getUint16(4);
61
158
  const tables = /* @__PURE__ */ new Map();
62
159
  for (let i = 0; i < numTables; i++) {
@@ -65,6 +162,9 @@ function parseTrueTypeFont(source) {
65
162
  const tag = String.fromCharCode(view.getUint8(at), view.getUint8(at + 1), view.getUint8(at + 2), view.getUint8(at + 3));
66
163
  tables.set(tag, view.getUint32(at + 8));
67
164
  }
165
+ const isCff = sfntVersion === 1330926671 || tables.has("CFF ") && !tables.has("glyf");
166
+ const cffOffset = isCff ? tables.get("CFF ") : void 0;
167
+ if (isCff && cffOffset === void 0) throw new Error("parseTrueTypeFont: OpenType CFF (.otf) font is missing \"CFF \" table");
68
168
  const table = (tag) => {
69
169
  const offset = tables.get(tag);
70
170
  if (offset === void 0) throw new Error(`parseTrueTypeFont: missing "${tag}" table`);
@@ -72,15 +172,10 @@ function parseTrueTypeFont(source) {
72
172
  };
73
173
  const head = table("head");
74
174
  const unitsPerEm = view.getUint16(head + 18);
75
- const longOffsets = view.getInt16(head + 50) !== 0;
76
175
  const hhea = table("hhea");
77
176
  const ascent = view.getInt16(hhea + 4);
78
177
  const descent = view.getInt16(hhea + 6);
79
178
  const glyphCount = view.getUint16(table("maxp") + 4);
80
- const loca = table("loca");
81
- const glyf = table("glyf");
82
- const glyphOffset = (index) => longOffsets ? view.getUint32(loca + index * 4) : view.getUint16(loca + index * 2) * 2;
83
- const cmap = /* @__PURE__ */ new Map();
84
179
  const cmapBase = table("cmap");
85
180
  const subtableCount = view.getUint16(cmapBase + 2);
86
181
  let chosen = -1;
@@ -97,48 +192,75 @@ function parseTrueTypeFont(source) {
97
192
  }
98
193
  }
99
194
  if (chosen < 0) throw new Error("parseTrueTypeFont: font has no Unicode cmap subtable");
100
- if (chosenFormat === 4) {
101
- const segments = view.getUint16(chosen + 6) >> 1;
102
- const ends = chosen + 14;
103
- const starts = ends + segments * 2 + 2;
104
- const deltas = starts + segments * 2;
105
- const ranges = deltas + segments * 2;
106
- for (let segment = 0; segment < segments; segment++) {
107
- const end = view.getUint16(ends + segment * 2);
108
- const start = view.getUint16(starts + segment * 2);
109
- const delta = view.getInt16(deltas + segment * 2);
110
- const rangeOffset = view.getUint16(ranges + segment * 2);
111
- for (let code = start; code <= end && code !== 65535; code++) {
112
- let glyph = code;
113
- if (rangeOffset !== 0) {
114
- glyph = view.getUint16(ranges + segment * 2 + rangeOffset + (code - start) * 2);
115
- if (glyph === 0) continue;
195
+ const f4Segments = chosenFormat === 4 ? view.getUint16(chosen + 6) >> 1 : 0;
196
+ const f4Ends = chosen + 14;
197
+ const f4Starts = f4Ends + f4Segments * 2 + 2;
198
+ const f4Deltas = f4Starts + f4Segments * 2;
199
+ const f4Ranges = f4Deltas + f4Segments * 2;
200
+ const f12Groups = chosenFormat === 12 ? view.getUint32(chosen + 12) : 0;
201
+ const f12Base = chosen + 16;
202
+ const asciiCmap = (/* @__PURE__ */ new Int32Array(256)).fill(-2);
203
+ const cmapCache = /* @__PURE__ */ new Map();
204
+ const lookupGlyphIndex = (codePoint) => {
205
+ if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 1114111) return -1;
206
+ if (codePoint < 256) {
207
+ const cached = asciiCmap[codePoint];
208
+ if (cached !== -2) return cached;
209
+ } else {
210
+ const cached = cmapCache.get(codePoint);
211
+ if (cached !== void 0) return cached;
212
+ }
213
+ let result = -1;
214
+ if (chosenFormat === 4) {
215
+ if (codePoint < 65535 && f4Segments > 0) {
216
+ let lo = 0;
217
+ let hi = f4Segments - 1;
218
+ while (lo <= hi) {
219
+ const mid = lo + hi >>> 1;
220
+ if (codePoint > view.getUint16(f4Ends + mid * 2)) lo = mid + 1;
221
+ else hi = mid - 1;
222
+ }
223
+ if (lo < f4Segments) {
224
+ const start = view.getUint16(f4Starts + lo * 2);
225
+ if (codePoint >= start) {
226
+ const rangeOffset = view.getUint16(f4Ranges + lo * 2);
227
+ let glyph = codePoint;
228
+ if (rangeOffset !== 0) glyph = view.getUint16(f4Ranges + lo * 2 + rangeOffset + (codePoint - start) * 2);
229
+ if (glyph !== 0) {
230
+ glyph = glyph + view.getInt16(f4Deltas + lo * 2) & 65535;
231
+ if (glyph !== 0) result = glyph;
232
+ }
233
+ }
116
234
  }
117
- glyph = glyph + delta & 65535;
118
- if (glyph !== 0) cmap.set(code, glyph);
119
235
  }
120
- }
121
- } else {
122
- const groups = view.getUint32(chosen + 12);
123
- for (let group = 0; group < groups; group++) {
124
- const at = chosen + 16 + group * 12;
125
- const start = view.getUint32(at);
126
- const end = view.getUint32(at + 4);
127
- const first = view.getUint32(at + 8);
128
- for (let code = start; code <= end; code++) {
129
- const glyph = first + code - start;
130
- if (glyph !== 0) cmap.set(code, glyph);
236
+ } else if (f12Groups > 0) {
237
+ let lo = 0;
238
+ let hi = f12Groups - 1;
239
+ while (lo <= hi) {
240
+ const mid = lo + hi >>> 1;
241
+ if (codePoint < view.getUint32(f12Base + mid * 12)) hi = mid - 1;
242
+ else lo = mid + 1;
243
+ }
244
+ if (hi >= 0) {
245
+ const at = f12Base + hi * 12;
246
+ const start = view.getUint32(at);
247
+ if (codePoint <= view.getUint32(at + 4)) {
248
+ const glyph = view.getUint32(at + 8) + (codePoint - start) >>> 0;
249
+ if (glyph !== 0) result = glyph;
250
+ }
131
251
  }
132
252
  }
133
- }
134
- function decodeSimpleGlyph(at, contourCount, bbox) {
135
- if (contourCount === 0) return Object.freeze({
136
- unitsPerEm,
137
- ascent,
138
- descent,
139
- bbox,
140
- quadratics: EMPTY_QUADRATICS
141
- });
253
+ if (codePoint < 256) asciiCmap[codePoint] = result;
254
+ else cmapCache.set(codePoint, result);
255
+ return result;
256
+ };
257
+ const decodeCff = cffOffset !== void 0 ? createCffGlyphDecoder(view, cffOffset, unitsPerEm, ascent, descent) : null;
258
+ const longOffsets = decodeCff ? false : view.getInt16(head + 50) !== 0;
259
+ const loca = decodeCff ? 0 : table("loca");
260
+ const glyf = decodeCff ? 0 : table("glyf");
261
+ const glyphOffset = (index) => longOffsets ? view.getUint32(loca + index * 4) : view.getUint16(loca + index * 2) * 2;
262
+ function readSimplePoints(at, contourCount) {
263
+ if (contourCount === 0) return 0;
142
264
  if (contourCount > ttEnds.length) {
143
265
  let size = ttEnds.length;
144
266
  while (size < contourCount) size *= 2;
@@ -183,74 +305,23 @@ function parseTrueTypeFont(source) {
183
305
  }
184
306
  ttYs[i] = vy;
185
307
  }
308
+ return pointCount;
309
+ }
310
+ function decodeSimpleGlyph(at, contourCount, bbox) {
311
+ if (contourCount === 0) return Object.freeze({
312
+ unitsPerEm,
313
+ ascent,
314
+ descent,
315
+ bbox,
316
+ quadratics: EMPTY_QUADRATICS
317
+ });
318
+ readSimplePoints(at, contourCount);
186
319
  let quadLen = 0;
187
320
  let first = 0;
188
321
  for (let c = 0; c < contourCount; c++) {
189
322
  const last = ttEnds[c];
190
- const len = last - first + 1;
191
- if (len < 2) {
192
- first = last + 1;
193
- continue;
194
- }
195
- ensureExpandedScratch(len * 2);
196
- let exLen = 0;
197
- let firstOn = -1;
198
- for (let i = 0; i < len; i++) {
199
- const p0 = first + i;
200
- const p1 = first + (i + 1 === len ? 0 : i + 1);
201
- const x0 = ttXs[p0];
202
- const y0 = ttYs[p0];
203
- const on0 = ttFlags[p0] & 1;
204
- if (on0 && firstOn < 0) firstOn = exLen;
205
- ttExX[exLen] = x0;
206
- ttExY[exLen] = y0;
207
- ttExOn[exLen] = on0;
208
- exLen++;
209
- if (!on0 && !(ttFlags[p1] & 1)) {
210
- if (firstOn < 0) firstOn = exLen;
211
- ttExX[exLen] = (x0 + ttXs[p1]) * .5;
212
- ttExY[exLen] = (y0 + ttYs[p1]) * .5;
213
- ttExOn[exLen] = 1;
214
- exLen++;
215
- }
216
- }
323
+ quadLen = emitContourRange(ttXs, ttYs, ttFlags, first, last - first + 1, quadLen);
217
324
  first = last + 1;
218
- if (firstOn < 0) continue;
219
- ensureQuadScratch(quadLen + exLen * 6, quadLen);
220
- let curX = ttExX[firstOn];
221
- let curY = ttExY[firstOn];
222
- let step = 1;
223
- while (step <= exLen) {
224
- const rawIdx = firstOn + step;
225
- const idx = rawIdx >= exLen ? rawIdx - exLen : rawIdx;
226
- if (ttExOn[idx]) {
227
- const px = ttExX[idx];
228
- const py = ttExY[idx];
229
- ttQuads[quadLen++] = curX;
230
- ttQuads[quadLen++] = curY;
231
- ttQuads[quadLen++] = (curX + px) * .5;
232
- ttQuads[quadLen++] = (curY + py) * .5;
233
- ttQuads[quadLen++] = px;
234
- ttQuads[quadLen++] = py;
235
- curX = px;
236
- curY = py;
237
- step++;
238
- } else {
239
- const rawEnd = firstOn + step + 1;
240
- const endIdx = rawEnd >= exLen ? rawEnd - exLen : rawEnd;
241
- const endX = ttExX[endIdx];
242
- const endY = ttExY[endIdx];
243
- ttQuads[quadLen++] = curX;
244
- ttQuads[quadLen++] = curY;
245
- ttQuads[quadLen++] = ttExX[idx];
246
- ttQuads[quadLen++] = ttExY[idx];
247
- ttQuads[quadLen++] = endX;
248
- ttQuads[quadLen++] = endY;
249
- curX = endX;
250
- curY = endY;
251
- step += 2;
252
- }
253
- }
254
325
  }
255
326
  return Object.freeze({
256
327
  unitsPerEm,
@@ -260,150 +331,94 @@ function parseTrueTypeFont(source) {
260
331
  quadratics: ttQuads.slice(0, quadLen)
261
332
  });
262
333
  }
263
- function readContours(index, depth = 0) {
264
- if (index < 0 || index >= glyphCount || depth > 8) return null;
334
+ function appendGlyphContours(index, a, b, c, d, dx, dy, depth, state) {
335
+ if (index < 0 || index >= glyphCount || depth > 8) return false;
265
336
  const start = glyphOffset(index);
266
- if (glyphOffset(index + 1) <= start) return {
267
- contours: [],
268
- bbox: [
269
- 0,
270
- 0,
271
- 0,
272
- 0
273
- ]
274
- };
337
+ if (glyphOffset(index + 1) <= start) return true;
275
338
  const at = glyf + start;
276
339
  const contourCount = view.getInt16(at);
277
- const bbox = [
278
- view.getInt16(at + 2),
279
- view.getInt16(at + 4),
280
- view.getInt16(at + 6),
281
- view.getInt16(at + 8)
282
- ];
283
- const contours = [];
284
340
  if (contourCount >= 0) {
285
- const ends = [];
286
- for (let c = 0; c < contourCount; c++) ends.push(view.getUint16(at + 10 + c * 2));
287
- const pointCount = contourCount > 0 ? ends[contourCount - 1] + 1 : 0;
288
- let cursor = at + 10 + contourCount * 2;
289
- cursor += 2 + view.getUint16(cursor);
290
- const flags = [];
291
- while (flags.length < pointCount) {
292
- const flag = view.getUint8(cursor++);
293
- flags.push(flag);
294
- if (flag & 8) for (let repeat = view.getUint8(cursor++); repeat > 0; repeat--) flags.push(flag);
341
+ const pointCount = readSimplePoints(at, contourCount);
342
+ if (pointCount === 0) return true;
343
+ ensureCompositePoints(state.pointLen + pointCount, state.pointLen);
344
+ ensureCompositeContours(state.contourLen + contourCount, state.contourLen);
345
+ const ptBase = state.pointLen;
346
+ for (let p = 0; p < pointCount; p++) {
347
+ const x = ttXs[p];
348
+ const y = ttYs[p];
349
+ compX[ptBase + p] = a * x + c * y + dx;
350
+ compY[ptBase + p] = b * x + d * y + dy;
351
+ compOn[ptBase + p] = ttFlags[p] & 1;
295
352
  }
296
- const readAxis = (shortBit, sameBit) => {
297
- const values = [];
298
- let value = 0;
299
- for (const flag of flags) {
300
- if (flag & shortBit) {
301
- const delta = view.getUint8(cursor++);
302
- value += flag & sameBit ? delta : -delta;
303
- } else if (!(flag & sameBit)) {
304
- value += view.getInt16(cursor);
305
- cursor += 2;
306
- }
307
- values.push(value);
308
- }
309
- return values;
310
- };
311
- const xs = readAxis(2, 16);
312
- const ys = readAxis(4, 32);
313
353
  let first = 0;
314
- for (const last of ends) {
315
- const contour = [];
316
- for (let p = first; p <= last; p++) contour.push({
317
- x: xs[p],
318
- y: ys[p],
319
- on: (flags[p] & 1) === 1
320
- });
321
- contours.push(contour);
354
+ for (let k = 0; k < contourCount; k++) {
355
+ const last = ttEnds[k];
356
+ compStarts[state.contourLen] = ptBase + first;
357
+ compLens[state.contourLen] = last - first + 1;
358
+ state.contourLen++;
322
359
  first = last + 1;
323
360
  }
324
- return {
325
- contours,
326
- bbox
327
- };
361
+ state.pointLen = ptBase + pointCount;
362
+ return true;
328
363
  }
329
364
  let cursor = at + 10;
330
365
  for (;;) {
331
366
  const flags = view.getUint16(cursor);
332
367
  const component = view.getUint16(cursor + 2);
333
368
  cursor += 4;
334
- if (!(flags & 2)) return null;
335
- const dx = flags & 1 ? view.getInt16(cursor) : view.getInt8(cursor);
336
- const dy = flags & 1 ? view.getInt16(cursor + 2) : view.getInt8(cursor + 1);
369
+ if (!(flags & 2)) return false;
370
+ const cdx = flags & 1 ? view.getInt16(cursor) : view.getInt8(cursor);
371
+ const cdy = flags & 1 ? view.getInt16(cursor + 2) : view.getInt8(cursor + 1);
337
372
  cursor += flags & 1 ? 4 : 2;
338
- const f2dot14 = (offset) => view.getInt16(offset) / 16384;
339
- let a = 1, b = 0, c = 0, d = 1;
373
+ let ca = 1, cb = 0, cc = 0, cd = 1;
340
374
  if (flags & 8) {
341
- a = d = f2dot14(cursor);
375
+ ca = cd = view.getInt16(cursor) / 16384;
342
376
  cursor += 2;
343
377
  } else if (flags & 64) {
344
- a = f2dot14(cursor);
345
- d = f2dot14(cursor + 2);
378
+ ca = view.getInt16(cursor) / 16384;
379
+ cd = view.getInt16(cursor + 2) / 16384;
346
380
  cursor += 4;
347
381
  } else if (flags & 128) {
348
- a = f2dot14(cursor);
349
- b = f2dot14(cursor + 2);
350
- c = f2dot14(cursor + 4);
351
- d = f2dot14(cursor + 6);
382
+ ca = view.getInt16(cursor) / 16384;
383
+ cb = view.getInt16(cursor + 2) / 16384;
384
+ cc = view.getInt16(cursor + 4) / 16384;
385
+ cd = view.getInt16(cursor + 6) / 16384;
352
386
  cursor += 8;
353
387
  }
354
- const child = readContours(component, depth + 1);
355
- if (!child) return null;
356
- for (const contour of child.contours) contours.push(contour.map(({ x, y, on }) => ({
357
- x: a * x + c * y + dx,
358
- y: b * x + d * y + dy,
359
- on
360
- })));
361
- if (!(flags & 32)) return {
362
- contours,
363
- bbox
364
- };
388
+ if (!appendGlyphContours(component, a * ca + c * cb, b * ca + d * cb, a * cc + c * cd, b * cc + d * cd, a * cdx + c * cdy + dx, b * cdx + d * cdy + dy, depth + 1, state)) return false;
389
+ if (!(flags & 32)) return true;
365
390
  }
366
391
  }
367
- function toQuadratics(contours) {
368
- const out = [];
369
- for (const contour of contours) {
370
- if (contour.length < 2) continue;
371
- const expanded = [];
372
- for (let i = 0; i < contour.length; i++) {
373
- const pt = contour[i];
374
- const next = contour[(i + 1) % contour.length];
375
- expanded.push(pt);
376
- if (!pt.on && !next.on) expanded.push({
377
- x: (pt.x + next.x) * .5,
378
- y: (pt.y + next.y) * .5,
379
- on: true
380
- });
381
- }
382
- const firstOn = expanded.findIndex((pt) => pt.on);
383
- if (firstOn < 0) continue;
384
- const points = firstOn === 0 ? expanded : expanded.slice(firstOn).concat(expanded.slice(0, firstOn));
385
- let current = points[0];
386
- for (let i = 1; i <= points.length; i++) {
387
- const pt = points[i % points.length];
388
- if (pt.on) {
389
- out.push(current.x, current.y, (current.x + pt.x) * .5, (current.y + pt.y) * .5, pt.x, pt.y);
390
- current = pt;
391
- } else {
392
- const end = points[++i % points.length];
393
- out.push(current.x, current.y, pt.x, pt.y, end.x, end.y);
394
- current = end;
395
- }
396
- }
397
- }
398
- return new Float32Array(out);
392
+ function decodeCompositeGlyph(at, index) {
393
+ const bbox = Object.freeze([
394
+ view.getInt16(at + 2),
395
+ view.getInt16(at + 4),
396
+ view.getInt16(at + 6),
397
+ view.getInt16(at + 8)
398
+ ]);
399
+ const state = {
400
+ pointLen: 0,
401
+ contourLen: 0
402
+ };
403
+ if (!appendGlyphContours(index, 1, 0, 0, 1, 0, 0, 0, state)) return null;
404
+ let quadLen = 0;
405
+ for (let k = 0; k < state.contourLen; k++) quadLen = emitContourRange(compX, compY, compOn, compStarts[k], compLens[k], quadLen);
406
+ return Object.freeze({
407
+ unitsPerEm,
408
+ ascent,
409
+ descent,
410
+ bbox,
411
+ quadratics: ttQuads.slice(0, quadLen)
412
+ });
399
413
  }
400
414
  const cache = /* @__PURE__ */ new Map();
401
415
  const outlineForCodePoint = (codePoint) => {
402
- const index = cmap.get(codePoint);
403
- if (index === void 0) return null;
416
+ const index = lookupGlyphIndex(codePoint);
417
+ if (index < 0) return null;
404
418
  let entry = cache.get(index);
405
- if (entry === void 0 && !cache.has(index)) {
419
+ if (entry === void 0) {
406
420
  if (index < 0 || index >= glyphCount) entry = null;
421
+ else if (decodeCff) entry = decodeCff(index);
407
422
  else {
408
423
  const start = glyphOffset(index);
409
424
  if (glyphOffset(index + 1) <= start) entry = Object.freeze({
@@ -422,21 +437,12 @@ function parseTrueTypeFont(source) {
422
437
  view.getInt16(at + 6),
423
438
  view.getInt16(at + 8)
424
439
  ]));
425
- else {
426
- const decoded = readContours(index);
427
- entry = decoded ? Object.freeze({
428
- unitsPerEm,
429
- ascent,
430
- descent,
431
- bbox: Object.freeze(decoded.bbox),
432
- quadratics: toQuadratics(decoded.contours)
433
- }) : null;
434
- }
440
+ else entry = decodeCompositeGlyph(at, index);
435
441
  }
436
442
  }
437
443
  cache.set(index, entry);
438
444
  }
439
- return entry ?? null;
445
+ return entry;
440
446
  };
441
447
  return {
442
448
  unitsPerEm,
@@ -444,7 +450,7 @@ function parseTrueTypeFont(source) {
444
450
  descent,
445
451
  glyphCount,
446
452
  has(codePoint) {
447
- return cmap.has(codePoint);
453
+ return lookupGlyphIndex(codePoint) >= 0;
448
454
  },
449
455
  outlineForCodePoint,
450
456
  outline(character) {
@@ -454,41 +460,136 @@ function parseTrueTypeFont(source) {
454
460
  }
455
461
  };
456
462
  }
457
- function asParsedFont(entry) {
458
- if ("outline" in entry && typeof entry.outline === "function") return entry;
459
- return parseTrueTypeFont(entry);
463
+ var parseFont = parseTrueTypeFont;
464
+ function isWoff2Source(entry) {
465
+ const view = toDataView(entry);
466
+ return view.byteLength >= 4 && view.getUint32(0) === 2001684018;
467
+ }
468
+ function toUint8Array(entry) {
469
+ if (entry instanceof Uint8Array) return entry;
470
+ if (ArrayBuffer.isView(entry)) return new Uint8Array(entry.buffer, entry.byteOffset, entry.byteLength);
471
+ return new Uint8Array(entry);
472
+ }
473
+ function compileFontEntry(entry, decodeWoff2) {
474
+ if ("outline" in entry && typeof entry.outline === "function") return () => entry;
475
+ const binary = entry;
476
+ if (isWoff2Source(binary)) {
477
+ if (!decodeWoff2) parseTrueTypeFont(binary);
478
+ const decoder = decodeWoff2;
479
+ let cached = null;
480
+ let pending = null;
481
+ return () => {
482
+ if (cached) return cached;
483
+ if (pending) return pending;
484
+ const decoded = decoder(toUint8Array(binary));
485
+ if (typeof decoded.then === "function") {
486
+ pending = Promise.resolve(decoded).then((bytes) => {
487
+ cached = parseTrueTypeFont(bytes);
488
+ pending = null;
489
+ return cached;
490
+ }, (error) => {
491
+ pending = null;
492
+ throw error;
493
+ });
494
+ return pending;
495
+ }
496
+ cached = parseTrueTypeFont(decoded);
497
+ return cached;
498
+ };
499
+ }
500
+ const parsed = parseTrueTypeFont(binary);
501
+ return () => parsed;
460
502
  }
461
503
  function normalizeFamilyToken(family) {
462
504
  return family.trim().replace(/^['"]|['"]$/g, "").toLowerCase();
463
505
  }
506
+ function isParsedFont(value) {
507
+ return "outline" in value && typeof value.outline === "function";
508
+ }
464
509
  /**
465
- * Creates a `WindfoilOutlineProvider` from a single `.ttf` buffer/`TrueTypeFont` or a family-keyed
466
- * map/object of `.ttf` buffers (with optional `'*'` fallback key).
510
+ * Creates a `WindfoilOutlineProvider` from a single `.ttf`/`.otf` (or `.woff2` with `decodeWoff2`)
511
+ * buffer/`TrueTypeFont` or a family-keyed map/object of buffers (with optional `'*'` fallback key).
467
512
  */
468
- function createTrueTypeOutlineProvider(source) {
513
+ function createTrueTypeOutlineProvider(source, options) {
514
+ const decodeWoff2 = options?.decodeWoff2;
469
515
  if (isArrayBufferLike(source) || ArrayBuffer.isView(source) || "outline" in source && typeof source.outline === "function") {
470
- const font = asParsedFont(source);
516
+ const resolveFont = compileFontEntry(source, decodeWoff2);
471
517
  return (request, signal) => {
472
518
  signal?.throwIfAborted();
473
- return font.outline(request.character);
519
+ const fontOrPromise = resolveFont();
520
+ if (isParsedFont(fontOrPromise)) return fontOrPromise.outline(request.character);
521
+ return fontOrPromise.then((font) => {
522
+ signal?.throwIfAborted();
523
+ return font.outline(request.character);
524
+ });
474
525
  };
475
526
  }
476
527
  const families = /* @__PURE__ */ new Map();
477
528
  const entries = source instanceof Map ? source.entries() : Object.entries(source);
478
- for (const [key, value] of entries) families.set(key === "*" ? "*" : normalizeFamilyToken(key), asParsedFont(value));
479
- const fallbackFont = families.get("*") ?? null;
529
+ for (const [key, value] of entries) families.set(key === "*" ? "*" : normalizeFamilyToken(key), compileFontEntry(value, decodeWoff2));
530
+ const fallbackResolver = families.get("*") ?? null;
531
+ const matches = /* @__PURE__ */ new Map();
532
+ let lastFamily;
533
+ let tokens = [];
534
+ const resolveChainAsync = async (resolvers, startIndex, firstPromise, character, signal) => {
535
+ const firstFont = await firstPromise;
536
+ signal?.throwIfAborted();
537
+ const firstOutline = firstFont.outline(character);
538
+ if (firstOutline) return firstOutline;
539
+ for (let i = startIndex + 1; i < resolvers.length; i++) {
540
+ const next = resolvers[i]();
541
+ const font = isParsedFont(next) ? next : await next;
542
+ signal?.throwIfAborted();
543
+ const outline = font.outline(character);
544
+ if (outline) return outline;
545
+ }
546
+ return null;
547
+ };
548
+ const resolveToken = (token) => {
549
+ let matched = matches.get(token);
550
+ if (matched === void 0) {
551
+ matched = families.get(normalizeFamilyToken(token)) ?? null;
552
+ if (matches.size === 32) matches.clear();
553
+ matches.set(token, matched);
554
+ }
555
+ return matched;
556
+ };
480
557
  return (request, signal) => {
481
558
  signal?.throwIfAborted();
482
- for (const token of request.font.family.split(",")) {
483
- const matched = families.get(normalizeFamilyToken(token));
484
- if (matched) {
485
- const outline = matched.outline(request.character);
559
+ const family = request.font.family;
560
+ if (family !== lastFamily) {
561
+ tokens = family.split(",");
562
+ lastFamily = family;
563
+ }
564
+ for (let i = 0; i < tokens.length; i++) {
565
+ const matched = resolveToken(tokens[i]);
566
+ if (!matched) continue;
567
+ const fontOrPromise = matched();
568
+ if (isParsedFont(fontOrPromise)) {
569
+ const outline = fontOrPromise.outline(request.character);
486
570
  if (outline) return outline;
571
+ } else {
572
+ const tail = [matched];
573
+ for (let j = i + 1; j < tokens.length; j++) {
574
+ const next = resolveToken(tokens[j]);
575
+ if (next) tail.push(next);
576
+ }
577
+ if (fallbackResolver) tail.push(fallbackResolver);
578
+ return resolveChainAsync(tail, 0, fontOrPromise, request.character, signal);
487
579
  }
488
580
  }
489
- return fallbackFont?.outline(request.character) ?? null;
581
+ if (fallbackResolver) {
582
+ const fontOrPromise = fallbackResolver();
583
+ if (isParsedFont(fontOrPromise)) return fontOrPromise.outline(request.character);
584
+ return fontOrPromise.then((font) => {
585
+ signal?.throwIfAborted();
586
+ return font.outline(request.character);
587
+ });
588
+ }
589
+ return null;
490
590
  };
491
591
  }
492
- export { createTrueTypeOutlineProvider, parseTrueTypeFont };
592
+ var createFontOutlineProvider = createTrueTypeOutlineProvider;
593
+ export { createFontOutlineProvider, createTrueTypeOutlineProvider, parseFont, parseTrueTypeFont };
493
594
 
494
595
  //# sourceMappingURL=truetype.js.map