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 { registerDeferredColorBleed } from "./gpuCommon.js";
1
2
  import { isEmojiCandidate, isEmojiGrapheme } from "../dom/clusters.js";
2
3
  import { createCanvas, isColorCandidate, setFontContext, waitForFonts } from "../dom/fontCanvas.js";
3
4
  // src/atlas/rasterize.ts
@@ -17,7 +18,7 @@ function align4(value) {
17
18
  }
18
19
  /** @internal Measures one text glyph's slot; `appendAtlas.ts` shares it with the cold build. */
19
20
  function measureGlyph(context, request, padding, shaping, candidate = false, lastFont = null) {
20
- const nextFont = setFontContext(context, request, shaping, lastFont);
21
+ setFontContext(context, request, shaping, lastFont);
21
22
  const metrics = context.measureText(request.character);
22
23
  const slice = request.shaping?.slice;
23
24
  const from = slice?.[0] ?? 0;
@@ -31,15 +32,19 @@ function measureGlyph(context, request, padding, shaping, candidate = false, las
31
32
  const planeX0 = -Math.ceil(left + padding - drawOffsetX) - drawOffsetX;
32
33
  const planeX1 = right + padding;
33
34
  const inkBounds = request.shaping !== void 0 || candidate || isEmojiCandidate(request.character);
34
- const planeY0 = -Math.max(descent, inkBounds ? metrics.actualBoundingBoxDescent : 0) - padding;
35
- const planeY1 = Math.ceil(Math.max(ascent, inkBounds ? metrics.actualBoundingBoxAscent : 0) + padding);
35
+ const hasActualY = !inkBounds && Number.isFinite(metrics.actualBoundingBoxAscent) && Number.isFinite(metrics.actualBoundingBoxDescent) && metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent >= 0;
36
+ const boxY0 = -Math.max(descent, inkBounds ? metrics.actualBoundingBoxDescent : 0) - padding;
37
+ const boxY1 = Math.ceil(Math.max(ascent, inkBounds ? metrics.actualBoundingBoxAscent : 0) + padding);
38
+ const planeY0 = hasActualY ? Math.floor(-metrics.actualBoundingBoxDescent - padding) : boxY0;
39
+ const planeY1 = hasActualY ? Math.ceil(metrics.actualBoundingBoxAscent + padding) : boxY1;
40
+ const boxHeight = Math.max(2, Math.ceil(boxY1 - boxY0) + 2);
36
41
  const ink = inkBounds || slice ? void 0 : [
37
42
  -metrics.actualBoundingBoxLeft,
38
43
  -metrics.actualBoundingBoxDescent,
39
44
  metrics.actualBoundingBoxRight,
40
45
  metrics.actualBoundingBoxAscent
41
46
  ];
42
- return [{
47
+ return {
43
48
  request,
44
49
  width: Math.max(2, Math.ceil(planeX1 - planeX0) + 2),
45
50
  height: Math.max(2, Math.ceil(planeY1 - planeY0) + 2),
@@ -50,10 +55,12 @@ function measureGlyph(context, request, padding, shaping, candidate = false, las
50
55
  descent,
51
56
  advance,
52
57
  drawOffsetX,
58
+ ...hasActualY ? { boxHeight } : {},
53
59
  ...ink?.every(Number.isFinite) ? { ink } : {},
54
60
  x: 0,
55
- y: 0
56
- }, nextFont];
61
+ y: 0,
62
+ flags: 0
63
+ };
57
64
  }
58
65
  /**
59
66
  * @internal The slot of a glyph drawn at (glyph.x, glyph.y) of a `width`×`height` atlas. The
@@ -67,31 +74,41 @@ function slotOf(glyph, padding, bakeSize, width, height, banded, uncoloured) {
67
74
  const planeY0 = glyph.planeY0 + .5;
68
75
  const planeX1 = glyph.planeX1 - (slice && slice[1] < 1 ? glyph.planeX1 - glyph.advance : .5);
69
76
  const planeY1 = glyph.planeY1 - .5;
70
- const uv = (x0, y0, x1, y1) => ({
71
- u0: (glyph.x + 1 + x0 - glyph.planeX0) / width,
72
- v0: (glyph.y + 1 + glyph.planeY1 - y1) / height,
73
- u1: (glyph.x + 1 + x1 - glyph.planeX0) / width,
74
- v1: (glyph.y + 1 + glyph.planeY1 - y0) / height
75
- });
77
+ const baseU = glyph.x + 1 - glyph.planeX0;
78
+ const baseV = glyph.y + 1 + glyph.planeY1;
79
+ const invW = 1 / width;
80
+ const invH = 1 / height;
76
81
  const ink = banded ? void 0 : glyph.ink;
77
- const reach = padding / 2 + 2 + .006 * bakeSize;
78
- const sx0 = ink ? Math.max(planeX0, Math.floor(ink[0] - reach)) : 0;
79
- const sy0 = ink ? Math.max(planeY0, Math.floor(ink[1] - reach)) : 0;
80
- const sx1 = ink ? Math.min(planeX1, Math.ceil(ink[2] + reach)) : 0;
81
- const sy1 = ink ? Math.min(planeY1, Math.ceil(ink[3] + reach)) : 0;
82
+ let support;
83
+ if (ink) {
84
+ const reach = padding / 2 + 2 + .006 * bakeSize;
85
+ const sx0 = Math.max(planeX0, Math.floor(ink[0] - reach));
86
+ const sy0 = Math.max(planeY0, Math.floor(ink[1] - reach));
87
+ const sx1 = Math.min(planeX1, Math.ceil(ink[2] + reach));
88
+ const sy1 = Math.min(planeY1, Math.ceil(ink[3] + reach));
89
+ if (sx1 > sx0 && sy1 > sy0) support = {
90
+ u0: (baseU + sx0) * invW,
91
+ v0: (baseV - sy1) * invH,
92
+ u1: (baseU + sx1) * invW,
93
+ v1: (baseV - sy0) * invH
94
+ };
95
+ }
82
96
  return {
83
97
  ...uncoloured ? { color: false } : {},
84
98
  key: glyph.request.key,
85
99
  character: glyph.request.character,
86
100
  fontKey: glyph.request.font.key,
87
- ...uv(planeX0, planeY0, planeX1, planeY1),
101
+ u0: (baseU + planeX0) * invW,
102
+ v0: (baseV - planeY1) * invH,
103
+ u1: (baseU + planeX1) * invW,
104
+ v1: (baseV - planeY0) * invH,
88
105
  planeX0,
89
106
  planeY0,
90
107
  planeX1,
91
108
  planeY1,
92
109
  descent: glyph.descent,
93
110
  advance: glyph.advance,
94
- ...ink && sx1 > sx0 && sy1 > sy0 ? { support: uv(sx0, sy0, sx1, sy1) } : {}
111
+ ...support ? { support } : {}
95
112
  };
96
113
  }
97
114
  /**
@@ -180,36 +197,98 @@ function packShelves(glyphs, width, maxSize) {
180
197
  height: align4(y + rowHeight)
181
198
  };
182
199
  }
183
- var skylineX = /* @__PURE__ */ new Int32Array(256);
184
- var skylineY = /* @__PURE__ */ new Int32Array(256);
185
- var skylineW = /* @__PURE__ */ new Int32Array(256);
186
- /** Bottom-left skyline packing fills the vertical gaps that fixed-height shelves leave behind. */
187
- function packSkyline(glyphs, width, maxSize) {
188
- const needed = glyphs.length + 2;
189
- if (skylineX.length < needed) {
190
- skylineX = new Int32Array(needed);
191
- skylineY = new Int32Array(needed);
192
- skylineW = new Int32Array(needed);
200
+ var DIRECT_SHELF_LIMIT = 24;
201
+ function preparePackingGroup(glyphs) {
202
+ const first = glyphs[0];
203
+ const last = glyphs[glyphs.length - 1];
204
+ if (first.width === last.width && first.height === last.height) return {
205
+ glyphs,
206
+ prefixWidths: null,
207
+ area: first.width * first.height * glyphs.length
208
+ };
209
+ const prefixWidths = glyphs.length > DIRECT_SHELF_LIMIT ? new Float64Array(glyphs.length + 1) : null;
210
+ let area = 0;
211
+ for (let i = 0; i < glyphs.length; i++) {
212
+ const g = glyphs[i];
213
+ if (prefixWidths) prefixWidths[i + 1] = prefixWidths[i] + g.width;
214
+ area += g.width * g.height;
215
+ }
216
+ return {
217
+ glyphs,
218
+ prefixWidths,
219
+ area
220
+ };
221
+ }
222
+ /** Measures greedy shelves from their row ends. Groups are sorted tallest first. */
223
+ function measureShelves(group, width, limit) {
224
+ const { glyphs, prefixWidths } = group;
225
+ const count = glyphs.length;
226
+ if (!prefixWidths) {
227
+ if (count <= DIRECT_SHELF_LIMIT) return packShelves(glyphs, width, limit);
228
+ const { width: gw, height: gh } = glyphs[0];
229
+ if (gw > width) {
230
+ lastOverflowWidth = Math.min(lastOverflowWidth, gw);
231
+ return null;
232
+ }
233
+ if (gh > limit) return null;
234
+ const columns = Math.floor(width / gw);
235
+ if (count > columns) lastOverflowWidth = Math.min(lastOverflowWidth, (columns + 1) * gw);
236
+ const height = Math.ceil(count / columns) * gh;
237
+ return height > limit ? null : {
238
+ width: align4(Math.min(count, columns) * gw),
239
+ height: align4(height)
240
+ };
193
241
  }
194
- const sx = skylineX;
195
- const sy = skylineY;
196
- const sw = skylineW;
197
- sx[0] = 0;
198
- sy[0] = 0;
199
- sw[0] = width;
200
- let skylineLen = 1;
201
242
  let usedWidth = 0;
202
- let usedHeight = 0;
203
- const shift = (to, from) => {
204
- sx.copyWithin(to, from, skylineLen);
205
- sy.copyWithin(to, from, skylineLen);
206
- sw.copyWithin(to, from, skylineLen);
243
+ let height = 0;
244
+ for (let start = 0; start < count;) {
245
+ const first = glyphs[start];
246
+ if (first.width > width) {
247
+ lastOverflowWidth = Math.min(lastOverflowWidth, first.width);
248
+ return null;
249
+ }
250
+ height += first.height;
251
+ if (height > limit) return null;
252
+ const target = prefixWidths[start] + width;
253
+ let lo = start + 1;
254
+ let hi = count;
255
+ while (lo < hi) {
256
+ const mid = lo + (hi - lo + 1 >>> 1);
257
+ if (prefixWidths[mid] <= target) lo = mid;
258
+ else hi = mid - 1;
259
+ }
260
+ const used = prefixWidths[lo] - prefixWidths[start];
261
+ if (used > usedWidth) usedWidth = used;
262
+ if (lo < count) {
263
+ const nextWidth = glyphs[lo].width;
264
+ lastOverflowWidth = Math.min(lastOverflowWidth, nextWidth > width ? nextWidth : used + nextWidth);
265
+ }
266
+ start = lo;
267
+ }
268
+ return {
269
+ width: align4(usedWidth),
270
+ height: align4(height)
207
271
  };
272
+ }
273
+ var skylineNodes = /* @__PURE__ */ new Int32Array(768);
274
+ function recordSkylineOverflow(width, maxSize, wasteBudget) {
275
+ lastOverflowWidth = Math.min(lastOverflowWidth, width + Math.ceil(-wasteBudget / maxSize));
276
+ return null;
277
+ }
278
+ /** Bottom-left skyline packing fills the vertical gaps that fixed-height shelves leave behind. */
279
+ function packSkyline({ glyphs, area }, width, maxSize, place) {
280
+ const needed = (glyphs.length + 2) * 3;
281
+ if (skylineNodes.length < needed) skylineNodes = new Int32Array(needed);
282
+ const nodes = skylineNodes;
283
+ nodes[0] = 0;
284
+ nodes[1] = 0;
285
+ nodes[2] = width;
286
+ let skylineEnd = 3;
287
+ let usedWidth = 0;
288
+ let usedHeight = 0;
208
289
  const minH = glyphs[glyphs.length - 1].height;
209
- let wasteBudget = width * maxSize;
210
- const over = () => (lastOverflowWidth = Math.min(lastOverflowWidth, width + Math.ceil(-wasteBudget / maxSize)), null);
211
- for (const g of glyphs) wasteBudget -= g.width * g.height;
212
- if (wasteBudget < 0) return over();
290
+ let wasteBudget = width * maxSize - area;
291
+ if (wasteBudget < 0) return recordSkylineOverflow(width, maxSize, wasteBudget);
213
292
  for (const glyph of glyphs) {
214
293
  const gw = glyph.width;
215
294
  const gh = glyph.height;
@@ -217,30 +296,31 @@ function packSkyline(glyphs, width, maxSize) {
217
296
  let bestIndex = -1;
218
297
  let bestBottom = Infinity;
219
298
  let bestWaste = Infinity;
220
- const tail = skylineLen - 1;
221
- let maxAllowedY = sw[tail] >= gw && sy[tail] < maxSize - gh ? sy[tail] : maxSize - gh;
222
- for (let index = 0; index < skylineLen; index++) {
223
- const firstY = sy[index];
299
+ const tail = skylineEnd - 3;
300
+ const tailY = nodes[tail + 1];
301
+ let maxAllowedY = nodes[tail + 2] >= gw ? Math.min(tailY, maxSize - gh) : maxSize - gh;
302
+ for (let index = 0; index < skylineEnd; index += 3) {
303
+ const firstY = nodes[index + 1];
224
304
  if (firstY > maxAllowedY) continue;
225
- const x = sx[index];
305
+ const x = nodes[index];
226
306
  if (x + gw > width) {
227
307
  if (x + gw < lastOverflowWidth) lastOverflowWidth = x + gw;
228
308
  break;
229
309
  }
230
- let remaining = gw - sw[index];
310
+ let remaining = gw - nodes[index + 2];
231
311
  let y = firstY;
232
312
  let waste = 0;
233
313
  if (remaining > 0) {
234
- let coveredArea = sw[index] * firstY;
235
- for (let cursor = index + 1;; cursor++) {
236
- const nodeY = sy[cursor];
314
+ let coveredArea = nodes[index + 2] * firstY;
315
+ for (let cursor = index + 3;; cursor += 3) {
316
+ const nodeY = nodes[cursor + 1];
237
317
  if (nodeY > maxAllowedY) {
238
318
  index = cursor;
239
319
  remaining = 1;
240
320
  break;
241
321
  }
242
322
  if (nodeY > y) y = nodeY;
243
- const nodeW = sw[cursor];
323
+ const nodeW = nodes[cursor + 2];
244
324
  coveredArea += (remaining < nodeW ? remaining : nodeW) * nodeY;
245
325
  if ((remaining -= nodeW) <= 0) break;
246
326
  }
@@ -257,37 +337,39 @@ function packSkyline(glyphs, width, maxSize) {
257
337
  }
258
338
  }
259
339
  if (maxAllowedY >= 0 && usedWidth + gw < lastOverflowWidth) lastOverflowWidth = usedWidth + gw;
260
- if (bestIndex < 0 || (wasteBudget -= bestWaste + (bestBottom > maxSize - minH ? gw * (maxSize - bestBottom) : 0)) < 0) return bestIndex < 0 ? null : over();
261
- const gx = sx[bestIndex];
340
+ if (bestIndex < 0 || (wasteBudget -= bestWaste + (bestBottom > maxSize - minH ? gw * (maxSize - bestBottom) : 0)) < 0) return bestIndex < 0 ? null : recordSkylineOverflow(width, maxSize, wasteBudget);
341
+ const gx = nodes[bestIndex];
262
342
  const right = gx + gw;
263
- glyph.x = gx;
264
- glyph.y = bestBottom - gh;
343
+ if (place) {
344
+ glyph.x = gx;
345
+ glyph.y = bestBottom - gh;
346
+ }
265
347
  let cursor = bestIndex;
266
- while (cursor < skylineLen && sx[cursor] + sw[cursor] <= right) cursor++;
267
- if (cursor < skylineLen && sx[cursor] < right) {
268
- sw[cursor] = sx[cursor] + sw[cursor] - right;
269
- sx[cursor] = right;
348
+ while (cursor < skylineEnd && nodes[cursor] + nodes[cursor + 2] <= right) cursor += 3;
349
+ if (cursor < skylineEnd && nodes[cursor] < right) {
350
+ nodes[cursor + 2] = nodes[cursor] + nodes[cursor + 2] - right;
351
+ nodes[cursor] = right;
270
352
  }
271
- let newCount = 1;
272
- if (bestIndex > 0 && sy[bestIndex - 1] === bestBottom) {
273
- sw[bestIndex - 1] = sw[bestIndex - 1] + gw;
274
- newCount = 0;
275
- if (cursor < skylineLen && sy[cursor] === bestBottom) {
276
- sw[bestIndex - 1] = sw[bestIndex - 1] + sw[cursor];
277
- cursor++;
353
+ let insertLength = 3;
354
+ if (bestIndex > 0 && nodes[bestIndex - 2] === bestBottom) {
355
+ nodes[bestIndex - 1] = nodes[bestIndex - 1] + gw;
356
+ insertLength = 0;
357
+ if (cursor < skylineEnd && nodes[cursor + 1] === bestBottom) {
358
+ nodes[bestIndex - 1] = nodes[bestIndex - 1] + nodes[cursor + 2];
359
+ cursor += 3;
278
360
  }
279
- } else if (cursor < skylineLen && sy[cursor] === bestBottom) {
280
- sx[cursor] = gx;
281
- sw[cursor] = sw[cursor] + gw;
282
- newCount = 0;
361
+ } else if (cursor < skylineEnd && nodes[cursor + 1] === bestBottom) {
362
+ nodes[cursor] = gx;
363
+ nodes[cursor + 2] = nodes[cursor + 2] + gw;
364
+ insertLength = 0;
283
365
  }
284
- const delta = newCount - (cursor - bestIndex);
285
- if (delta !== 0 && cursor < skylineLen) shift(cursor + delta, cursor);
286
- skylineLen += delta;
287
- if (newCount) {
288
- sx[bestIndex] = gx;
289
- sy[bestIndex] = bestBottom;
290
- sw[bestIndex] = gw;
366
+ const delta = insertLength - (cursor - bestIndex);
367
+ if (delta !== 0 && cursor < skylineEnd) nodes.copyWithin(cursor + delta, cursor, skylineEnd);
368
+ skylineEnd += delta;
369
+ if (insertLength) {
370
+ nodes[bestIndex] = gx;
371
+ nodes[bestIndex + 1] = bestBottom;
372
+ nodes[bestIndex + 2] = gw;
291
373
  }
292
374
  if (right > usedWidth) usedWidth = right;
293
375
  if (bestBottom > usedHeight) usedHeight = bestBottom;
@@ -298,13 +380,18 @@ function packSkyline(glyphs, width, maxSize) {
298
380
  };
299
381
  }
300
382
  /** Packs each group into rows of its own, one group below the other. */
301
- function packStacked(groups, width, limit, pack) {
383
+ function packGroups(groups, width, limit, skyline, place = false) {
384
+ if (!groups.length) return {
385
+ width: 4,
386
+ height: 4
387
+ };
302
388
  let packedWidth = 0;
303
389
  let height = 0;
304
390
  for (const group of groups) {
305
- const packed = pack(group, width, limit - height);
306
- if (!packed || height + packed.height > limit) return null;
307
- for (const glyph of group) glyph.y += height;
391
+ const available = limit - height;
392
+ const packed = skyline ? packSkyline(group, width, available, place) : place ? packShelves(group.glyphs, width, available) : measureShelves(group, width, available);
393
+ if (!packed || groups.length > 1 && height + packed.height > limit) return null;
394
+ if (place && height) for (const glyph of group.glyphs) glyph.y += height;
308
395
  packedWidth = Math.max(packedWidth, packed.width);
309
396
  height += packed.height;
310
397
  }
@@ -313,17 +400,6 @@ function packStacked(groups, width, limit, pack) {
313
400
  height
314
401
  };
315
402
  }
316
- function rememberPacking(glyphs, size, positions) {
317
- for (let index = 0; index < glyphs.length; index++) {
318
- const glyph = glyphs[index];
319
- positions[index * 2] = glyph.x;
320
- positions[index * 2 + 1] = glyph.y;
321
- }
322
- return {
323
- width: size.width,
324
- height: size.height
325
- };
326
- }
327
403
  function isBetterPacking(candidate, current) {
328
404
  return (candidate.width * candidate.height - current.width * current.height || Math.max(candidate.width, candidate.height) - Math.max(current.width, current.height) || candidate.width - current.width) < 0;
329
405
  }
@@ -348,28 +424,30 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
348
424
  const shaping = shapingModule?.setShapingContext;
349
425
  throwIfAborted(options.signal);
350
426
  const canvas = createCanvas(document);
427
+ let returned = false;
351
428
  try {
352
429
  const previous = options.previousAtlas?.bakeSize === bakeSize && options.previousAtlas.pxRange === padding ? options.previousAtlas.glyphs : void 0;
353
- const context = canvas.getContext("2d", { willReadFrequently: !options.gpuUpload || requests.some((request) => isColorCandidate(request, options.colorGlyphs) && (request.shape !== void 0 || (options.colorGlyphs === "all" || isEmojiGrapheme(request.character)) && previous?.get(request.key)?.color !== false)) });
430
+ const willReadFrequently = !options.gpuUpload || requests.some((request) => isColorCandidate(request, options.colorGlyphs) && (request.shape !== void 0 || (options.colorGlyphs === "all" || isEmojiGrapheme(request.character)) && previous?.get(request.key)?.color !== false));
431
+ const context = canvas.getContext("2d", { willReadFrequently });
354
432
  if (!context) throw new Error("rasterizeGlyphAtlas: Canvas 2D is unavailable");
355
433
  const packingLimit = maxAtlasSize - maxAtlasSize % 4;
356
434
  let lastFont = null;
357
435
  const measured = requests.map((request) => {
358
436
  if (request.shape) return shapes.measureShape(request, bakeSize, padding);
359
- const [glyph, nextFont] = measureGlyph(context, request, padding, shaping, isColorCandidate(request, options.colorGlyphs), lastFont);
360
- lastFont = nextFont;
437
+ const glyph = measureGlyph(context, request, padding, shaping, isColorCandidate(request, options.colorGlyphs), lastFont);
438
+ lastFont = request.font;
361
439
  return glyph;
362
440
  });
363
441
  measured.sort((a, b) => b.height - a.height || b.width - a.width || (a.request.key < b.request.key ? -1 : a.request.key > b.request.key ? 1 : 0));
364
- const banded = /* @__PURE__ */ new Set();
365
- const uncoloured = /* @__PURE__ */ new Set();
442
+ let bandedCount = 0;
366
443
  const tested = [];
367
444
  for (const glyph of measured) {
368
445
  if (!isColorCandidate(glyph.request, options.colorGlyphs)) continue;
369
446
  const known = glyph.request.shape ? void 0 : previous?.get(glyph.request.key)?.color;
370
- if (known === false) uncoloured.add(glyph);
447
+ if (known === false) glyph.flags = 2;
371
448
  else {
372
- banded.add(glyph);
449
+ glyph.flags = 1;
450
+ bandedCount++;
373
451
  if (!glyph.request.shape && known === void 0) tested.push(glyph);
374
452
  }
375
453
  }
@@ -377,27 +455,36 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
377
455
  shapes.drawShapeSlot(context, glyph, bakeSize);
378
456
  });
379
457
  const drawGlyph = (glyph, fill) => {
380
- painter.draw(glyph, !glyph.request.shape && !banded.has(glyph), fill);
458
+ painter.draw(glyph, !glyph.request.shape && (glyph.flags & 1) === 0, fill);
381
459
  };
382
- const colorGlyphs = banded.size ? await import("./colorGlyphs.js") : void 0;
460
+ const colorGlyphs = bandedCount ? await import("./colorGlyphs.js") : void 0;
383
461
  throwIfAborted(options.signal);
384
462
  const scratch = tested.length ? packShelves(tested, packingLimit, packingLimit) : null;
385
463
  if (scratch) {
386
- canvas.width = scratch.width;
387
- canvas.height = scratch.height;
388
- painter.reset();
389
- const paint = (fill) => {
390
- context.clearRect(0, 0, canvas.width, canvas.height);
391
- for (const glyph of tested) drawGlyph(glyph, fill);
392
- return context.getImageData(0, 0, canvas.width, canvas.height).data;
393
- };
394
- const dark = paint("#000");
395
- const { colored } = colorGlyphs.encodeColorBand(paint("#fff"), dark, canvas.width, canvas.height, 0, tested);
396
- tested.forEach((glyph, index) => {
397
- if (colored[index]) return;
398
- banded.delete(glyph);
399
- uncoloured.add(glyph);
400
- });
464
+ const scratchCanvas = willReadFrequently ? null : createCanvas(document);
465
+ try {
466
+ const targetCanvas = scratchCanvas ?? canvas;
467
+ targetCanvas.width = scratch.width;
468
+ targetCanvas.height = scratch.height;
469
+ const targetContext = scratchCanvas ? scratchCanvas.getContext("2d", { willReadFrequently: true }) : context;
470
+ if (!targetContext) throw new Error("rasterizeGlyphAtlas: Canvas 2D is unavailable");
471
+ const targetPainter = scratchCanvas ? glyphPainter(targetContext, shaping, options.signal) : painter;
472
+ targetPainter.reset();
473
+ const paint = (fill) => {
474
+ targetContext.clearRect(0, 0, scratch.width, scratch.height);
475
+ for (const glyph of tested) targetPainter.draw(glyph, false, fill);
476
+ return targetContext.getImageData(0, 0, scratch.width, scratch.height).data;
477
+ };
478
+ const dark = paint("#000");
479
+ const light = paint("#fff");
480
+ for (const glyph of tested) if (!colorGlyphs.slotHasColor(light, dark, scratch.width, glyph)) glyph.flags = 2;
481
+ } finally {
482
+ if (scratchCanvas) {
483
+ scratchCanvas.width = 0;
484
+ scratchCanvas.height = 0;
485
+ scratchCanvas.remove();
486
+ }
487
+ }
401
488
  }
402
489
  const textRows = [];
403
490
  const shapeRows = [];
@@ -409,7 +496,7 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
409
496
  totalArea += glyph.width * glyph.height;
410
497
  if (glyph.width > widestGlyph) widestGlyph = glyph.width;
411
498
  if (glyph.width > packingLimit || glyph.height > packingLimit) exceedsLimit = true;
412
- if (banded.has(glyph)) band.push(glyph);
499
+ if (glyph.flags & 1) band.push(glyph);
413
500
  else if (glyph.request.shape) shapeRows.push(glyph);
414
501
  else textRows.push(glyph);
415
502
  }
@@ -418,36 +505,81 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
418
505
  shapeRows,
419
506
  band
420
507
  ].filter((group) => group.length);
421
- const packGlyphs = (pack, width, limit) => groups.length > 1 ? packStacked(groups, width, limit, pack) : pack(measured, width, limit);
422
508
  const impossible = totalArea > packingLimit * packingLimit || exceedsLimit;
423
- const bestPositions = new Int32Array(measured.length * 2);
424
- let shelfWidth = Math.min(packingLimit, Math.max(64, nextPowerOfTwo(Math.sqrt(totalArea))));
425
- let packed = impossible ? null : packGlyphs(packShelves, shelfWidth, packingLimit);
426
- while (!packed && shelfWidth < packingLimit) {
427
- shelfWidth = Math.min(packingLimit, shelfWidth * 2);
428
- packed = packGlyphs(packShelves, shelfWidth, packingLimit);
509
+ const packing = impossible ? [] : groups.map(preparePackingGroup);
510
+ let bestWidth = Math.min(packingLimit, Math.max(64, nextPowerOfTwo(Math.sqrt(totalArea))));
511
+ let bestLimit = packingLimit;
512
+ let bestSkyline = false;
513
+ let best = impossible ? null : packGroups(packing, bestWidth, bestLimit, false);
514
+ while (!best && !impossible && bestWidth < packingLimit) {
515
+ bestWidth = Math.min(packingLimit, bestWidth * 2);
516
+ best = packGroups(packing, bestWidth, bestLimit, false);
429
517
  }
430
- let best = packed && !impossible ? rememberPacking(measured, packed, bestPositions) : null;
431
518
  if (!impossible && measured.length > 0) {
432
519
  const minRequiredLimit = groups.reduce((sum, g) => sum + align4(g[0].height), 0);
433
520
  const minimumWidth = Math.min(packingLimit, align4(Math.max(widestGlyph, Math.ceil(totalArea / packingLimit))));
434
- const widthStep = align4(Math.max(4, Math.ceil((packingLimit - minimumWidth) / 64)));
521
+ const packerCount = groups.some((g) => g[0].height !== g[g.length - 1].height) ? 2 : 1;
435
522
  const maxHeightFor = (width) => best ? Math.min(packingLimit, Math.floor(best.width * best.height / width)) : packingLimit;
436
- const nextW = [minimumWidth, minimumWidth];
437
- for (let width = minimumWidth;; width = Math.min(packingLimit, width + widthStep)) {
523
+ let shelfBestWidth = best?.width ?? minimumWidth;
524
+ let nextShelfW = minimumWidth;
525
+ for (let width = minimumWidth; width <= packingLimit; width += 4) {
438
526
  throwIfAborted(options.signal);
439
527
  if (maxHeightFor(width) < minRequiredLimit) break;
528
+ if (width < nextShelfW) continue;
440
529
  const minH = align4(Math.max(minRequiredLimit, Math.ceil(totalArea / width)));
441
- for (let i = 0; i < 2; i++) {
442
- if (width < nextW[i]) continue;
443
- const maxCandidateHeight = maxHeightFor(width);
444
- if (minH > maxCandidateHeight) break;
445
- lastOverflowWidth = Infinity;
446
- const candidate = packGlyphs(i ? packSkyline : packShelves, width, maxCandidateHeight);
447
- nextW[i] = lastOverflowWidth;
448
- if (candidate && (!best || isBetterPacking(candidate, best))) best = rememberPacking(measured, candidate, bestPositions);
530
+ const maxCandidateHeight = maxHeightFor(width);
531
+ if (minH > maxCandidateHeight) break;
532
+ lastOverflowWidth = Infinity;
533
+ const candidate = packGroups(packing, width, maxCandidateHeight, false);
534
+ nextShelfW = lastOverflowWidth;
535
+ if (candidate && (!best || isBetterPacking(candidate, best))) {
536
+ best = candidate;
537
+ bestWidth = width;
538
+ bestLimit = maxCandidateHeight;
539
+ bestSkyline = false;
540
+ shelfBestWidth = best.width;
541
+ }
542
+ }
543
+ if (packerCount === 2) {
544
+ const trySkylineRange = (start, end, step) => {
545
+ const lo = Math.max(minimumWidth, align4(start));
546
+ const hi = Math.min(packingLimit, align4(end));
547
+ let nextSkyW = lo;
548
+ for (let width = lo; width <= hi; width += step) {
549
+ throwIfAborted(options.signal);
550
+ if (maxHeightFor(width) < minRequiredLimit) break;
551
+ if (width < nextSkyW) continue;
552
+ const minH = align4(Math.max(minRequiredLimit, Math.ceil(totalArea / width)));
553
+ const maxCandidateHeight = maxHeightFor(width);
554
+ if (minH > maxCandidateHeight) break;
555
+ lastOverflowWidth = Infinity;
556
+ const candidate = packGroups(packing, width, maxCandidateHeight, true);
557
+ nextSkyW = lastOverflowWidth;
558
+ if (candidate && (!best || isBetterPacking(candidate, best))) {
559
+ best = candidate;
560
+ bestWidth = width;
561
+ bestLimit = maxCandidateHeight;
562
+ bestSkyline = true;
563
+ }
564
+ }
565
+ };
566
+ const refineAround = (center) => {
567
+ trySkylineRange(center - 48, center + 48, 16);
568
+ const c2 = best?.width ?? center;
569
+ trySkylineRange(c2 - 12, c2 + 12, 4);
570
+ };
571
+ if (measured.length <= 24 || packingLimit - minimumWidth <= 128) trySkylineRange(minimumWidth, packingLimit, 4);
572
+ else {
573
+ trySkylineRange(minimumWidth, packingLimit, 64);
574
+ if (best) {
575
+ const maxValidW = align4(Math.min(packingLimit, Math.floor(best.width * best.height / minRequiredLimit)));
576
+ if (maxValidW >= minimumWidth) trySkylineRange(maxValidW, maxValidW, 4);
577
+ }
578
+ const skyCoarseW = best?.width ?? minimumWidth;
579
+ refineAround(skyCoarseW);
580
+ if (shelfBestWidth !== skyCoarseW) refineAround(shelfBestWidth);
581
+ if (!best) trySkylineRange(minimumWidth, packingLimit, 4);
449
582
  }
450
- if (width === packingLimit) break;
451
583
  }
452
584
  }
453
585
  if (!best) {
@@ -466,16 +598,17 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
466
598
  }
467
599
  throw new Error(`rasterizeGlyphAtlas: ${requests.length} glyphs exceed the ${maxAtlasSize}×${maxAtlasSize} atlas limit`);
468
600
  }
469
- for (let index = 0; index < measured.length; index++) {
470
- measured[index].x = bestPositions[index * 2];
471
- measured[index].y = bestPositions[index * 2 + 1];
472
- }
601
+ packGroups(packing, bestWidth, bestLimit, bestSkyline, true);
473
602
  let textBottom = !textRows.length ? 0 : groups.length > 1 ? topOf(groups[1]) : best.height;
474
603
  if (options.appendable === "headroom" && textRows.length) {
475
604
  let widestText = 0;
476
- for (const glyph of textRows) if (glyph.width > widestText) widestText = glyph.width;
605
+ let tallestFontBox = textRows[0].height;
606
+ for (const glyph of textRows) {
607
+ if (glyph.width > widestText) widestText = glyph.width;
608
+ if (glyph.boxHeight !== void 0 && glyph.boxHeight > tallestFontBox) tallestFontBox = glyph.boxHeight;
609
+ }
477
610
  best.width = Math.min(packingLimit, Math.max(best.width, align4(2 * widestText)));
478
- const room = Math.min(packingLimit - best.height, align4(Math.max(textRows[0].height, textBottom / 4)));
611
+ const room = Math.min(packingLimit - best.height, align4(Math.max(tallestFontBox, textBottom / 4)));
479
612
  if (room > 0) {
480
613
  for (const glyph of measured) if (glyph.y >= textBottom) glyph.y += room;
481
614
  best.height += room;
@@ -485,7 +618,7 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
485
618
  canvas.width = best.width;
486
619
  canvas.height = best.height;
487
620
  painter.reset();
488
- if (scratch) context.clearRect(0, 0, canvas.width, canvas.height);
621
+ if (scratch && willReadFrequently) context.clearRect(0, 0, canvas.width, canvas.height);
489
622
  const bandTop = band.length ? topOf(band) : 0;
490
623
  const bandHeight = canvas.height - bandTop;
491
624
  let black;
@@ -495,17 +628,32 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
495
628
  context.clearRect(0, bandTop, canvas.width, bandHeight);
496
629
  }
497
630
  const glyphSlots = /* @__PURE__ */ new Map();
631
+ const byFont = /* @__PURE__ */ new Map();
632
+ const shapeGlyphs = [];
498
633
  for (const glyph of measured) {
499
- drawGlyph(glyph);
500
- glyphSlots.set(glyph.request.key, slotOf(glyph, padding, bakeSize, canvas.width, canvas.height, banded.has(glyph), uncoloured.has(glyph)));
634
+ if (glyph.request.shape) {
635
+ shapeGlyphs.push(glyph);
636
+ continue;
637
+ }
638
+ let group = byFont.get(glyph.request.font.key);
639
+ if (!group) byFont.set(glyph.request.font.key, group = []);
640
+ group.push(glyph);
501
641
  }
642
+ const drawAndRecord = (glyph) => {
643
+ drawGlyph(glyph);
644
+ glyphSlots.set(glyph.request.key, slotOf(glyph, padding, bakeSize, canvas.width, canvas.height, (glyph.flags & 1) !== 0, (glyph.flags & 2) !== 0));
645
+ };
646
+ for (const group of byFont.values()) for (const glyph of group) drawAndRecord(glyph);
647
+ for (const glyph of shapeGlyphs) drawAndRecord(glyph);
502
648
  let colorBand;
503
649
  if (black) {
504
650
  const white = context.getImageData(0, bandTop, canvas.width, bandHeight).data;
505
- const encoded = colorGlyphs.encodeColorBand(white, black, canvas.width, bandHeight, padding, band.map((glyph) => ({
651
+ const bandSlots = band.map((glyph) => ({
506
652
  ...glyph,
507
653
  y: glyph.y - bandTop
508
- })));
654
+ }));
655
+ const deferBleed = options.deferColorBleed === true && padding > 0 && canvas.width * bandHeight >= 16384;
656
+ const encoded = colorGlyphs.encodeColorBand(white, black, canvas.width, bandHeight, deferBleed ? 0 : padding, bandSlots);
509
657
  for (const [index, glyph] of band.entries()) {
510
658
  const slot = glyphSlots.get(glyph.request.key);
511
659
  glyphSlots.set(glyph.request.key, {
@@ -513,18 +661,36 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
513
661
  color: encoded.colored[index]
514
662
  });
515
663
  }
516
- if (encoded.colored.some(Boolean)) colorBand = {
517
- top: bandTop,
518
- height: bandHeight,
519
- data: encoded.data
520
- };
664
+ if (encoded.colored.some(Boolean)) {
665
+ if (deferBleed) {
666
+ const w = canvas.width;
667
+ const h = bandHeight;
668
+ const range = padding;
669
+ const encodeFn = colorGlyphs.encodeColorBand;
670
+ registerDeferredColorBleed(encoded.data, {
671
+ white,
672
+ black,
673
+ width: w,
674
+ height: h,
675
+ pxRange: range,
676
+ applyCpu: () => {
677
+ encoded.data.set(encodeFn(white, black, w, h, range, []).data);
678
+ }
679
+ });
680
+ }
681
+ colorBand = {
682
+ top: bandTop,
683
+ height: bandHeight,
684
+ data: encoded.data
685
+ };
686
+ }
521
687
  }
522
688
  const shapeTop = shapeRows.length ? topOf(shapeRows) : void 0;
523
689
  if (options.appendable) {
524
690
  let x = 0;
525
691
  let y = 0;
526
692
  let rowHeight = 0;
527
- for (const glyph of textRows) if (glyph.y > y && glyph.y + textRows[0].height <= textBottom) y = glyph.y;
693
+ for (const glyph of textRows) if (glyph.y > y && glyph.y + glyph.height <= textBottom) y = glyph.y;
528
694
  for (const glyph of textRows) {
529
695
  const reach = glyph.y + glyph.height - y;
530
696
  if (reach > 0) {
@@ -544,6 +710,7 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
544
710
  rowHeight
545
711
  });
546
712
  }
713
+ returned = true;
547
714
  return {
548
715
  ...colorBand ? { colorBand } : {},
549
716
  ...shapeTop !== void 0 ? { shapeTop } : {},
@@ -555,6 +722,10 @@ async function rasterizeGlyphAtlas(document, requests, options = {}) {
555
722
  glyphs: glyphSlots
556
723
  };
557
724
  } finally {
725
+ if (!returned) {
726
+ canvas.width = 0;
727
+ canvas.height = 0;
728
+ }
558
729
  canvas.remove();
559
730
  }
560
731
  }