@officedk/pptxdk 0.0.0-stage → 0.2.0

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 (206) hide show
  1. package/LICENSE +21 -0
  2. package/dist/build-zrZuX4fK.d.ts +3 -0
  3. package/dist/build.d.ts +35 -0
  4. package/dist/build.js +128 -0
  5. package/dist/capabilities/dist/all.js +34 -0
  6. package/dist/capabilities/dist/box/box-codegen.js +84 -0
  7. package/dist/capabilities/dist/box/box-declaration.js +32 -0
  8. package/dist/capabilities/dist/box/box-elements.js +17 -0
  9. package/dist/capabilities/dist/box/box.js +142 -0
  10. package/dist/capabilities/dist/chart/chart-codegen.js +79 -0
  11. package/dist/capabilities/dist/chart/chart-declaration.js +7 -0
  12. package/dist/capabilities/dist/chart/chart-elements.js +12 -0
  13. package/dist/capabilities/dist/chart/chart-import.js +2403 -0
  14. package/dist/capabilities/dist/chart/chart-labels.js +86 -0
  15. package/dist/capabilities/dist/chart/chart-model.js +9 -0
  16. package/dist/capabilities/dist/chart/chart-schemas.js +1088 -0
  17. package/dist/capabilities/dist/chart/chart-xml.js +788 -0
  18. package/dist/capabilities/dist/chart/chart.js +1119 -0
  19. package/dist/capabilities/dist/chart/preview/axes-and-grid.js +311 -0
  20. package/dist/capabilities/dist/chart/preview/bar-column.js +143 -0
  21. package/dist/capabilities/dist/chart/preview/data-labels.js +334 -0
  22. package/dist/capabilities/dist/chart/preview/error-bars.js +213 -0
  23. package/dist/capabilities/dist/chart/preview/index.js +192 -0
  24. package/dist/capabilities/dist/chart/preview/layout.js +592 -0
  25. package/dist/capabilities/dist/chart/preview/legend.js +273 -0
  26. package/dist/capabilities/dist/chart/preview/line-area.js +404 -0
  27. package/dist/capabilities/dist/chart/preview/markers.js +100 -0
  28. package/dist/capabilities/dist/chart/preview/number-format.js +247 -0
  29. package/dist/capabilities/dist/chart/preview/pie-doughnut.js +159 -0
  30. package/dist/capabilities/dist/chart/preview/scales.js +365 -0
  31. package/dist/capabilities/dist/chart/preview/scatter.js +131 -0
  32. package/dist/capabilities/dist/chart/preview/style.js +60 -0
  33. package/dist/capabilities/dist/chart/preview/text-render.js +116 -0
  34. package/dist/capabilities/dist/chart/preview/trendlines.js +537 -0
  35. package/dist/capabilities/dist/chart/preview/types.js +5 -0
  36. package/dist/capabilities/dist/document.js +168 -0
  37. package/dist/capabilities/dist/font/font-embedding.js +119 -0
  38. package/dist/capabilities/dist/font/font-engine.js +834 -0
  39. package/dist/capabilities/dist/font/font-import.js +766 -0
  40. package/dist/capabilities/dist/font/font-subset.js +55 -0
  41. package/dist/capabilities/dist/font/font.js +72 -0
  42. package/dist/capabilities/dist/group/group-codegen.js +74 -0
  43. package/dist/capabilities/dist/group/group-declaration.js +41 -0
  44. package/dist/capabilities/dist/group/group-elements.js +12 -0
  45. package/dist/capabilities/dist/group/group-import.js +649 -0
  46. package/dist/capabilities/dist/group/group-xml.js +8 -0
  47. package/dist/capabilities/dist/group/group.js +260 -0
  48. package/dist/capabilities/dist/image/gif-decoder.js +244 -0
  49. package/dist/capabilities/dist/image/image-codegen.js +164 -0
  50. package/dist/capabilities/dist/image/image-declaration.js +8 -0
  51. package/dist/capabilities/dist/image/image-decode.js +244 -0
  52. package/dist/capabilities/dist/image/image-elements.js +12 -0
  53. package/dist/capabilities/dist/image/image-import.js +984 -0
  54. package/dist/capabilities/dist/image/image-schemas.js +147 -0
  55. package/dist/capabilities/dist/image/image-xml.js +12 -0
  56. package/dist/capabilities/dist/image/image.js +552 -0
  57. package/dist/capabilities/dist/line/line-codegen.js +89 -0
  58. package/dist/capabilities/dist/line/line-declaration.js +72 -0
  59. package/dist/capabilities/dist/line/line-elements.js +12 -0
  60. package/dist/capabilities/dist/line/line-import.js +504 -0
  61. package/dist/capabilities/dist/line/line-xml.js +9 -0
  62. package/dist/capabilities/dist/line/line.js +331 -0
  63. package/dist/capabilities/dist/masters/footer-placeholder.js +683 -0
  64. package/dist/capabilities/dist/masters/masters-codegen.js +69 -0
  65. package/dist/capabilities/dist/masters/masters.js +250 -0
  66. package/dist/capabilities/dist/motion/motion-api.js +757 -0
  67. package/dist/capabilities/dist/motion/motion-codegen.js +406 -0
  68. package/dist/capabilities/dist/motion/motion-elements.js +43 -0
  69. package/dist/capabilities/dist/motion/motion-import.js +1568 -0
  70. package/dist/capabilities/dist/motion/motion-plan.js +2340 -0
  71. package/dist/capabilities/dist/motion/motion-query.js +423 -0
  72. package/dist/capabilities/dist/motion/motion-schemas.js +339 -0
  73. package/dist/capabilities/dist/motion/motion.js +1617 -0
  74. package/dist/capabilities/dist/notes/notes-codegen.js +19 -0
  75. package/dist/capabilities/dist/notes/notes-declaration.js +8 -0
  76. package/dist/capabilities/dist/notes/notes-elements.js +12 -0
  77. package/dist/capabilities/dist/notes/notes-import.js +292 -0
  78. package/dist/capabilities/dist/notes/notes-schemas.js +38 -0
  79. package/dist/capabilities/dist/notes/notes-xml.js +38 -0
  80. package/dist/capabilities/dist/notes/notes.js +194 -0
  81. package/dist/capabilities/dist/placeholder/placeholder-codegen.js +54 -0
  82. package/dist/capabilities/dist/placeholder/placeholder-elements.js +13 -0
  83. package/dist/capabilities/dist/placeholder/placeholder-schemas.js +33 -0
  84. package/dist/capabilities/dist/placeholder/placeholder.js +451 -0
  85. package/dist/capabilities/dist/shape/shape-codegen.js +236 -0
  86. package/dist/capabilities/dist/shape/shape-declaration.js +45 -0
  87. package/dist/capabilities/dist/shape/shape-elements.js +12 -0
  88. package/dist/capabilities/dist/shape/shape-import.js +628 -0
  89. package/dist/capabilities/dist/shape/shape-xml.js +6 -0
  90. package/dist/capabilities/dist/shape/shape.js +320 -0
  91. package/dist/capabilities/dist/shared/geometry.js +1681 -0
  92. package/dist/capabilities/dist/shared/identity.js +205 -0
  93. package/dist/capabilities/dist/shared/morph-api.js +11 -0
  94. package/dist/capabilities/dist/shared/placeholder-import.js +238 -0
  95. package/dist/capabilities/dist/shared/preset-definitions.js +22423 -0
  96. package/dist/capabilities/dist/shared/style-inheritance.js +161 -0
  97. package/dist/capabilities/dist/shared/text-schemas.js +241 -0
  98. package/dist/capabilities/dist/shared/text-service.js +2252 -0
  99. package/dist/capabilities/dist/shared/visual.js +1197 -0
  100. package/dist/capabilities/dist/slide-background/slide-background-codegen.js +44 -0
  101. package/dist/capabilities/dist/slide-background/slide-background-import.js +166 -0
  102. package/dist/capabilities/dist/slide-background/slide-background-xml.js +34 -0
  103. package/dist/capabilities/dist/slide-background/slide-background.js +238 -0
  104. package/dist/capabilities/dist/table/table-codegen.js +125 -0
  105. package/dist/capabilities/dist/table/table-declaration.js +15 -0
  106. package/dist/capabilities/dist/table/table-elements.js +27 -0
  107. package/dist/capabilities/dist/table/table-import.js +606 -0
  108. package/dist/capabilities/dist/table/table-schemas.js +334 -0
  109. package/dist/capabilities/dist/table/table-xml.js +30 -0
  110. package/dist/capabilities/dist/table/table.js +739 -0
  111. package/dist/capabilities/dist/text/text-codegen.js +198 -0
  112. package/dist/capabilities/dist/text/text-declaration.js +27 -0
  113. package/dist/capabilities/dist/text/text-elements.js +54 -0
  114. package/dist/capabilities/dist/text/text-import.js +1224 -0
  115. package/dist/capabilities/dist/text/text-xml.js +9 -0
  116. package/dist/capabilities/dist/text/text.js +320 -0
  117. package/dist/capabilities/dist/transition/transition-api.js +136 -0
  118. package/dist/capabilities/dist/transition/transition-codegen.js +45 -0
  119. package/dist/capabilities/dist/transition/transition-import.js +477 -0
  120. package/dist/capabilities/dist/transition/transition.js +260 -0
  121. package/dist/capability-BbKW9w4O.d.ts +2131 -0
  122. package/dist/cli.js +20 -0
  123. package/dist/codegen/dist/assets.js +79 -0
  124. package/dist/codegen/dist/codegen.js +691 -0
  125. package/dist/codegen/dist/identifiers.js +203 -0
  126. package/dist/codegen/dist/index.js +3 -0
  127. package/dist/codegen/dist/printer.js +143 -0
  128. package/dist/codegen/dist/types.js +77 -0
  129. package/dist/core/dist/author-ids.js +51 -0
  130. package/dist/core/dist/author-schemas.js +330 -0
  131. package/dist/core/dist/block-flex-layout.js +801 -0
  132. package/dist/core/dist/build.js +4 -0
  133. package/dist/core/dist/capability.js +15 -0
  134. package/dist/core/dist/declaration.js +16 -0
  135. package/dist/core/dist/diagnostics.js +353 -0
  136. package/dist/core/dist/document-internals.js +26 -0
  137. package/dist/core/dist/document.js +417 -0
  138. package/dist/core/dist/font-seal.js +204 -0
  139. package/dist/core/dist/geometry-schemas.js +256 -0
  140. package/dist/core/dist/grid-layout.js +449 -0
  141. package/dist/core/dist/host.js +40 -0
  142. package/dist/core/dist/index.js +15 -0
  143. package/dist/core/dist/input-values.js +61 -0
  144. package/dist/core/dist/layout-protocol.js +61 -0
  145. package/dist/core/dist/layout-schemas.js +201 -0
  146. package/dist/core/dist/layout.js +340 -0
  147. package/dist/core/dist/masters.js +194 -0
  148. package/dist/core/dist/morph.js +30 -0
  149. package/dist/core/dist/placeholders.js +275 -0
  150. package/dist/core/dist/query.js +190 -0
  151. package/dist/core/dist/resolution.js +12 -0
  152. package/dist/core/dist/resolve.js +2246 -0
  153. package/dist/core/dist/stage-options.js +300 -0
  154. package/dist/core/dist/theme.js +1023 -0
  155. package/dist/core/dist/transform-geometry.js +72 -0
  156. package/dist/document-41spD7bq.d.ts +1350 -0
  157. package/dist/emit/dist/emit.js +957 -0
  158. package/dist/emit/dist/index.js +5 -0
  159. package/dist/emit/dist/ooxml.js +661 -0
  160. package/dist/emit/dist/shape-tree.js +12 -0
  161. package/dist/import/dist/alternate-content.js +54 -0
  162. package/dist/import/dist/diagnostics.js +157 -0
  163. package/dist/import/dist/fonts.js +309 -0
  164. package/dist/import/dist/import.js +513 -0
  165. package/dist/import/dist/index.js +16 -0
  166. package/dist/import/dist/masters.js +1293 -0
  167. package/dist/import/dist/mechanism.js +10 -0
  168. package/dist/import/dist/namespaces.js +61 -0
  169. package/dist/import/dist/notes-masters.js +300 -0
  170. package/dist/import/dist/opc.js +233 -0
  171. package/dist/import/dist/resources.js +166 -0
  172. package/dist/import/dist/sections.js +139 -0
  173. package/dist/import/dist/themes.js +777 -0
  174. package/dist/import/dist/visual.js +821 -0
  175. package/dist/import/dist/xml-fragments.js +277 -0
  176. package/dist/import/dist/xml-reader.js +267 -0
  177. package/dist/index-1ag7U9Jt.d.ts +63 -0
  178. package/dist/index-BEwhk7c8.d.ts +72 -0
  179. package/dist/index-cK2KtukZ.d.ts +161 -0
  180. package/dist/index-v77CTJev.d.ts +55 -0
  181. package/dist/index.d.ts +8 -0
  182. package/dist/index.js +82 -0
  183. package/dist/node.d.ts +53 -0
  184. package/dist/node.js +225 -0
  185. package/dist/pptxdk/src/cli-commands.js +907 -0
  186. package/dist/pptxdk/src/compose.js +53 -0
  187. package/dist/pptxdk/src/slide.js +9 -0
  188. package/dist/preview/dist/index.js +342 -0
  189. package/dist/preview/dist/options.js +40 -0
  190. package/dist/preview/dist/svg-guard.js +1089 -0
  191. package/dist/preview.d.ts +12 -0
  192. package/dist/preview.js +29 -0
  193. package/dist/react/dist/append.js +66 -0
  194. package/dist/react/dist/document-adapter.js +404 -0
  195. package/dist/react/dist/elements.js +82 -0
  196. package/dist/react/dist/from-vanilla.js +57 -0
  197. package/dist/react/dist/index.js +89 -0
  198. package/dist/react/dist/mount.js +34 -0
  199. package/dist/react/dist/reconciler.js +290 -0
  200. package/dist/react.d.ts +235 -0
  201. package/dist/react.js +31 -0
  202. package/dist/transition-6n8GZxSg.d.ts +9 -0
  203. package/dist/vanilla.d.ts +4 -0
  204. package/dist/vanilla.js +15 -0
  205. package/package.json +79 -3
  206. package/README.md +0 -3
@@ -0,0 +1,1089 @@
1
+ import { bytesSha256 } from "../../core/dist/font-seal.js";
2
+ import "../../core/dist/capability.js";
3
+ // ../preview/dist/svg-guard.js
4
+ const EPSILON = 1e-6;
5
+ const SAFE_OFFSCREEN_PAD = 512;
6
+ const MAX_BLUR_STD_DEVIATION = 256;
7
+ const RASTER_TOKEN_PREFIX = "urn:pptxdk-raster:";
8
+ function createPageRasterRegistry(idPrefix) {
9
+ const entries = [];
10
+ const byBytesRef = /* @__PURE__ */ new Map();
11
+ const byDigest = /* @__PURE__ */ new Map();
12
+ return {
13
+ idPrefix,
14
+ get size() {
15
+ return entries.length;
16
+ },
17
+ register(resource) {
18
+ let byType = byBytesRef.get(resource.bytes);
19
+ const refHit = byType?.get(resource.contentType);
20
+ if (refHit !== void 0) return `${RASTER_TOKEN_PREFIX}${refHit.id}`;
21
+ const digestKey = `${resource.contentType}:${bytesSha256(resource.bytes)}`;
22
+ let entry = byDigest.get(digestKey);
23
+ if (entry === void 0) {
24
+ entry = {
25
+ id: entries.length,
26
+ contentType: resource.contentType,
27
+ bytes: resource.bytes
28
+ };
29
+ entries.push(entry);
30
+ byDigest.set(digestKey, entry);
31
+ }
32
+ if (byType === void 0) {
33
+ byType = /* @__PURE__ */ new Map();
34
+ byBytesRef.set(resource.bytes, byType);
35
+ }
36
+ byType.set(resource.contentType, entry);
37
+ return `${RASTER_TOKEN_PREFIX}${entry.id}`;
38
+ },
39
+ getEntry(id) {
40
+ return entries[id];
41
+ },
42
+ getDataUri(entry) {
43
+ entry.dataUri ??= `data:${entry.contentType};base64,${Buffer.from(entry.bytes).toString("base64")}`;
44
+ return entry.dataUri;
45
+ }
46
+ };
47
+ }
48
+ const IDENTITY_MATRIX = [
49
+ 1,
50
+ 0,
51
+ 0,
52
+ 1,
53
+ 0,
54
+ 0
55
+ ];
56
+ function decodeXmlEntities(value) {
57
+ return value.replaceAll("&quot;", "\"").replaceAll("&apos;", "'").replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&");
58
+ }
59
+ function encodeXmlAttr(value) {
60
+ return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;");
61
+ }
62
+ function parseAttributes(raw) {
63
+ const attrs = /* @__PURE__ */ new Map();
64
+ for (const match of raw.matchAll(/([A-Za-z_:][\w:.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)')/gu)) {
65
+ const key = match[1];
66
+ if (key === void 0) continue;
67
+ const val = match[2] ?? match[3] ?? "";
68
+ attrs.set(key, decodeXmlEntities(val));
69
+ }
70
+ return attrs;
71
+ }
72
+ function parseSvgTree(svg) {
73
+ const root = [];
74
+ const stack = [];
75
+ const tagPattern = /<(\/?)([A-Za-z_:][\w:.-]*)([^>]*?)(\/?)>/gu;
76
+ let lastIndex = 0;
77
+ const pushNode = (node) => {
78
+ const parent = stack.at(-1);
79
+ if (parent !== void 0) parent.children.push(node);
80
+ else root.push(node);
81
+ };
82
+ for (const match of svg.matchAll(tagPattern)) {
83
+ const index = match.index ?? 0;
84
+ if (index > lastIndex) {
85
+ const text = svg.slice(lastIndex, index);
86
+ if (text !== "") pushNode({
87
+ kind: "text",
88
+ text
89
+ });
90
+ }
91
+ lastIndex = index + match[0].length;
92
+ const isClosing = match[1] === "/";
93
+ const tag = match[2];
94
+ const rawAttrs = match[3] ?? "";
95
+ const selfClosing = match[4] === "/";
96
+ if (isClosing) {
97
+ while (stack.length > 0) if (stack.pop().tag === tag) break;
98
+ continue;
99
+ }
100
+ const element = {
101
+ kind: "element",
102
+ tag,
103
+ attrs: parseAttributes(rawAttrs),
104
+ children: [],
105
+ selfClosing
106
+ };
107
+ pushNode(element);
108
+ if (!selfClosing) stack.push(element);
109
+ }
110
+ if (lastIndex < svg.length) {
111
+ const tail = svg.slice(lastIndex);
112
+ if (tail !== "") pushNode({
113
+ kind: "text",
114
+ text: tail
115
+ });
116
+ }
117
+ return root;
118
+ }
119
+ function serializeSvgNode(node) {
120
+ if (node.kind === "text") return node.text;
121
+ const attrsParts = [];
122
+ for (const [key, value] of node.attrs.entries()) attrsParts.push(`${key}="${encodeXmlAttr(value)}"`);
123
+ const attrString = attrsParts.length === 0 ? "" : ` ${attrsParts.join(" ")}`;
124
+ if (node.selfClosing && node.children.length === 0) return `<${node.tag}${attrString}/>`;
125
+ const inner = node.children.map(serializeSvgNode).join("");
126
+ return `<${node.tag}${attrString}>${inner}</${node.tag}>`;
127
+ }
128
+ function formatNumber(value) {
129
+ const rounded = Math.round(value * 1e6) / 1e6;
130
+ return Object.is(rounded, -0) ? "0" : String(rounded);
131
+ }
132
+ function parseNumber(value) {
133
+ if (value === void 0) return void 0;
134
+ const trimmed = value.trim();
135
+ if (trimmed === "" || trimmed.endsWith("%")) return void 0;
136
+ const parsed = Number(trimmed);
137
+ return Number.isFinite(parsed) ? parsed : NaN;
138
+ }
139
+ function parseLengthAttr(value, referenceSpan, defaultValue) {
140
+ if (value === void 0) return defaultValue;
141
+ const trimmed = value.trim();
142
+ if (trimmed === "") return NaN;
143
+ if (trimmed.endsWith("%")) {
144
+ const pct = Number(trimmed.slice(0, -1));
145
+ return Number.isFinite(pct) ? pct / 100 * referenceSpan : NaN;
146
+ }
147
+ const parsed = Number(trimmed);
148
+ return Number.isFinite(parsed) ? parsed : NaN;
149
+ }
150
+ function hasNonFiniteAttr(element, keys) {
151
+ for (const key of keys) {
152
+ const raw = element.attrs.get(key);
153
+ if (raw === void 0) continue;
154
+ const trimmed = raw.trim();
155
+ if (trimmed === "") return true;
156
+ const numStr = trimmed.endsWith("%") ? trimmed.slice(0, -1) : trimmed;
157
+ if (!Number.isFinite(Number(numStr))) return true;
158
+ }
159
+ return false;
160
+ }
161
+ function multiplyMatrix(m1, m2) {
162
+ return [
163
+ m1[0] * m2[0] + m1[2] * m2[1],
164
+ m1[1] * m2[0] + m1[3] * m2[1],
165
+ m1[0] * m2[2] + m1[2] * m2[3],
166
+ m1[1] * m2[2] + m1[3] * m2[3],
167
+ m1[0] * m2[4] + m1[2] * m2[5] + m1[4],
168
+ m1[1] * m2[4] + m1[3] * m2[5] + m1[5]
169
+ ];
170
+ }
171
+ function parseTransformAttr(transformStr) {
172
+ if (transformStr === void 0 || transformStr.trim() === "") return IDENTITY_MATRIX;
173
+ let current = IDENTITY_MATRIX;
174
+ const opPattern = /([A-Za-z]+)\s*\(([^)]*)\)/gu;
175
+ let matchedAny = false;
176
+ for (const match of transformStr.matchAll(opPattern)) {
177
+ matchedAny = true;
178
+ const fn = match[1];
179
+ const nums = (match[2] ?? "").trim().split(/[\s,]+/u).filter((token) => token !== "").map(Number);
180
+ if (nums.some((n) => !Number.isFinite(n))) return null;
181
+ let next;
182
+ switch (fn) {
183
+ case "translate":
184
+ if (nums.length < 1) return null;
185
+ next = [
186
+ 1,
187
+ 0,
188
+ 0,
189
+ 1,
190
+ nums[0],
191
+ nums[1] ?? 0
192
+ ];
193
+ break;
194
+ case "scale": {
195
+ if (nums.length < 1) return null;
196
+ const sx = nums[0];
197
+ next = [
198
+ sx,
199
+ 0,
200
+ 0,
201
+ nums[1] ?? sx,
202
+ 0,
203
+ 0
204
+ ];
205
+ break;
206
+ }
207
+ case "rotate": {
208
+ if (nums.length < 1) return null;
209
+ const rad = nums[0] * Math.PI / 180;
210
+ const cos = Math.cos(rad);
211
+ const sin = Math.sin(rad);
212
+ const cx = nums[1] ?? 0;
213
+ const cy = nums[2] ?? 0;
214
+ if (cx === 0 && cy === 0) next = [
215
+ cos,
216
+ sin,
217
+ -sin,
218
+ cos,
219
+ 0,
220
+ 0
221
+ ];
222
+ else {
223
+ const toOrigin = [
224
+ 1,
225
+ 0,
226
+ 0,
227
+ 1,
228
+ cx,
229
+ cy
230
+ ];
231
+ const rot = [
232
+ cos,
233
+ sin,
234
+ -sin,
235
+ cos,
236
+ 0,
237
+ 0
238
+ ];
239
+ const back = [
240
+ 1,
241
+ 0,
242
+ 0,
243
+ 1,
244
+ -cx,
245
+ -cy
246
+ ];
247
+ next = multiplyMatrix(multiplyMatrix(toOrigin, rot), back);
248
+ }
249
+ break;
250
+ }
251
+ case "skewX":
252
+ if (nums.length < 1) return null;
253
+ next = [
254
+ 1,
255
+ 0,
256
+ Math.tan(nums[0] * Math.PI / 180),
257
+ 1,
258
+ 0,
259
+ 0
260
+ ];
261
+ break;
262
+ case "skewY":
263
+ if (nums.length < 1) return null;
264
+ next = [
265
+ 1,
266
+ Math.tan(nums[0] * Math.PI / 180),
267
+ 0,
268
+ 1,
269
+ 0,
270
+ 0
271
+ ];
272
+ break;
273
+ case "matrix":
274
+ if (nums.length !== 6) return null;
275
+ next = [
276
+ nums[0],
277
+ nums[1],
278
+ nums[2],
279
+ nums[3],
280
+ nums[4],
281
+ nums[5]
282
+ ];
283
+ break;
284
+ default: return null;
285
+ }
286
+ current = multiplyMatrix(current, next);
287
+ }
288
+ if (!matchedAny) return null;
289
+ if (current.some((v) => !Number.isFinite(v))) return null;
290
+ const det = current[0] * current[3] - current[1] * current[2];
291
+ if (!Number.isFinite(det) || Math.abs(det) <= 1e-12) return null;
292
+ return current;
293
+ }
294
+ function transformBbox(bbox, m) {
295
+ const corners = [
296
+ [bbox.minX, bbox.minY],
297
+ [bbox.maxX, bbox.minY],
298
+ [bbox.maxX, bbox.maxY],
299
+ [bbox.minX, bbox.maxY]
300
+ ];
301
+ let minX = Number.POSITIVE_INFINITY;
302
+ let minY = Number.POSITIVE_INFINITY;
303
+ let maxX = Number.NEGATIVE_INFINITY;
304
+ let maxY = Number.NEGATIVE_INFINITY;
305
+ for (const [x, y] of corners) {
306
+ const tx = m[0] * x + m[2] * y + m[4];
307
+ const ty = m[1] * x + m[3] * y + m[5];
308
+ if (!Number.isFinite(tx) || !Number.isFinite(ty)) return {
309
+ minX: NaN,
310
+ minY: NaN,
311
+ maxX: NaN,
312
+ maxY: NaN
313
+ };
314
+ if (tx < minX) minX = tx;
315
+ if (ty < minY) minY = ty;
316
+ if (tx > maxX) maxX = tx;
317
+ if (ty > maxY) maxY = ty;
318
+ }
319
+ return {
320
+ minX,
321
+ minY,
322
+ maxX,
323
+ maxY
324
+ };
325
+ }
326
+ function boxesIntersect(a, b) {
327
+ if (!Number.isFinite(a.minX) || !Number.isFinite(a.minY) || !Number.isFinite(a.maxX) || !Number.isFinite(a.maxY)) return false;
328
+ return a.maxX >= b.minX && a.minX <= b.maxX && a.maxY >= b.minY && a.minY <= b.maxY;
329
+ }
330
+ function extractUrlId(attrValue) {
331
+ if (attrValue === void 0) return void 0;
332
+ return /^url\(#([^)]+)\)$/u.exec(attrValue.trim())?.[1];
333
+ }
334
+ function pathBbox(d) {
335
+ if (d.trim() === "") return null;
336
+ const tokens = d.match(/[A-Za-z]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?|NaN|Infinity|-Infinity/gu);
337
+ if (tokens === null || tokens.length === 0) return null;
338
+ let index = 0;
339
+ let cmd = "";
340
+ let curX = 0;
341
+ let curY = 0;
342
+ let startX = 0;
343
+ let startY = 0;
344
+ let minX = Number.POSITIVE_INFINITY;
345
+ let minY = Number.POSITIVE_INFINITY;
346
+ let maxX = Number.NEGATIVE_INFINITY;
347
+ let maxY = Number.NEGATIVE_INFINITY;
348
+ const recordPoint = (x, y) => {
349
+ if (!Number.isFinite(x) || !Number.isFinite(y)) return false;
350
+ if (x < minX) minX = x;
351
+ if (y < minY) minY = y;
352
+ if (x > maxX) maxX = x;
353
+ if (y > maxY) maxY = y;
354
+ return true;
355
+ };
356
+ const nextNum = () => {
357
+ const token = tokens[index];
358
+ if (token === void 0 || /^[A-Za-z]$/u.test(token)) return void 0;
359
+ index += 1;
360
+ return Number(token);
361
+ };
362
+ while (index < tokens.length) {
363
+ const tok = tokens[index];
364
+ if (/^[A-Za-z]$/u.test(tok) && tok !== "NaN" && tok !== "Infinity") {
365
+ cmd = tok;
366
+ index += 1;
367
+ if (cmd === "Z" || cmd === "z") {
368
+ curX = startX;
369
+ curY = startY;
370
+ }
371
+ continue;
372
+ }
373
+ if (cmd === "") return null;
374
+ const rel = cmd === cmd.toLowerCase();
375
+ const upper = cmd.toUpperCase();
376
+ switch (upper) {
377
+ case "M":
378
+ case "L":
379
+ case "T": {
380
+ const x = nextNum();
381
+ const y = nextNum();
382
+ if (x === void 0 || y === void 0 || !Number.isFinite(x) || !Number.isFinite(y)) return null;
383
+ curX = rel ? curX + x : x;
384
+ curY = rel ? curY + y : y;
385
+ if (!recordPoint(curX, curY)) return null;
386
+ if (upper === "M") {
387
+ startX = curX;
388
+ startY = curY;
389
+ cmd = rel ? "l" : "L";
390
+ }
391
+ break;
392
+ }
393
+ case "H": {
394
+ const x = nextNum();
395
+ if (x === void 0 || !Number.isFinite(x)) return null;
396
+ curX = rel ? curX + x : x;
397
+ if (!recordPoint(curX, curY)) return null;
398
+ break;
399
+ }
400
+ case "V": {
401
+ const y = nextNum();
402
+ if (y === void 0 || !Number.isFinite(y)) return null;
403
+ curY = rel ? curY + y : y;
404
+ if (!recordPoint(curX, curY)) return null;
405
+ break;
406
+ }
407
+ case "C": {
408
+ const x1 = nextNum();
409
+ const y1 = nextNum();
410
+ const x2 = nextNum();
411
+ const y2 = nextNum();
412
+ const x = nextNum();
413
+ const y = nextNum();
414
+ if (x1 === void 0 || y1 === void 0 || x2 === void 0 || y2 === void 0 || x === void 0 || y === void 0 || !Number.isFinite(x1) || !Number.isFinite(y1) || !Number.isFinite(x2) || !Number.isFinite(y2) || !Number.isFinite(x) || !Number.isFinite(y)) return null;
415
+ const ox = rel ? curX : 0;
416
+ const oy = rel ? curY : 0;
417
+ if (!recordPoint(ox + x1, oy + y1) || !recordPoint(ox + x2, oy + y2) || !recordPoint(ox + x, oy + y)) return null;
418
+ curX = ox + x;
419
+ curY = oy + y;
420
+ break;
421
+ }
422
+ case "S":
423
+ case "Q": {
424
+ const x1 = nextNum();
425
+ const y1 = nextNum();
426
+ const x = nextNum();
427
+ const y = nextNum();
428
+ if (x1 === void 0 || y1 === void 0 || x === void 0 || y === void 0 || !Number.isFinite(x1) || !Number.isFinite(y1) || !Number.isFinite(x) || !Number.isFinite(y)) return null;
429
+ const ox = rel ? curX : 0;
430
+ const oy = rel ? curY : 0;
431
+ if (!recordPoint(ox + x1, oy + y1) || !recordPoint(ox + x, oy + y)) return null;
432
+ curX = ox + x;
433
+ curY = oy + y;
434
+ break;
435
+ }
436
+ case "A": {
437
+ const rx = nextNum();
438
+ const ry = nextNum();
439
+ const rot = nextNum();
440
+ const large = nextNum();
441
+ const sweep = nextNum();
442
+ const x = nextNum();
443
+ const y = nextNum();
444
+ if (rx === void 0 || ry === void 0 || rot === void 0 || large === void 0 || sweep === void 0 || x === void 0 || y === void 0 || !Number.isFinite(rx) || !Number.isFinite(ry) || !Number.isFinite(rot) || !Number.isFinite(large) || !Number.isFinite(sweep) || !Number.isFinite(x) || !Number.isFinite(y)) return null;
445
+ const ox = rel ? curX : 0;
446
+ const oy = rel ? curY : 0;
447
+ if (!recordPoint(curX, curY) || !recordPoint(ox + x, oy + y)) return null;
448
+ curX = ox + x;
449
+ curY = oy + y;
450
+ break;
451
+ }
452
+ default: return null;
453
+ }
454
+ }
455
+ if (!Number.isFinite(minX)) return null;
456
+ return {
457
+ minX,
458
+ minY,
459
+ maxX,
460
+ maxY
461
+ };
462
+ }
463
+ function clampRectToSafeViewport(x, y, width, height, safeViewport) {
464
+ if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(width) || !Number.isFinite(height) || width <= EPSILON || height <= EPSILON) return null;
465
+ const minX = Math.max(x, safeViewport.minX);
466
+ const minY = Math.max(y, safeViewport.minY);
467
+ const maxX = Math.min(x + width, safeViewport.maxX);
468
+ const maxY = Math.min(y + height, safeViewport.maxY);
469
+ const clampedW = maxX - minX;
470
+ const clampedH = maxY - minY;
471
+ if (clampedW <= EPSILON || clampedH <= EPSILON) return null;
472
+ return {
473
+ x: minX,
474
+ y: minY,
475
+ width: clampedW,
476
+ height: clampedH
477
+ };
478
+ }
479
+ function extractPointsBbox(pointsAttr) {
480
+ const nums = pointsAttr.trim().split(/[\s,]+/u).filter((token) => token !== "").map(Number);
481
+ if (nums.length < 4 || nums.length % 2 !== 0 || nums.some((n) => !Number.isFinite(n))) return null;
482
+ let minX = Number.POSITIVE_INFINITY;
483
+ let minY = Number.POSITIVE_INFINITY;
484
+ let maxX = Number.NEGATIVE_INFINITY;
485
+ let maxY = Number.NEGATIVE_INFINITY;
486
+ for (let i = 0; i < nums.length; i += 2) {
487
+ const x = nums[i];
488
+ const y = nums[i + 1];
489
+ if (x < minX) minX = x;
490
+ if (y < minY) minY = y;
491
+ if (x > maxX) maxX = x;
492
+ if (y > maxY) maxY = y;
493
+ }
494
+ return {
495
+ minX,
496
+ minY,
497
+ maxX,
498
+ maxY
499
+ };
500
+ }
501
+ function lastGradientStopColor(gradient) {
502
+ const last = gradient.children.filter((child) => child.kind === "element" && child.tag === "stop").at(-1);
503
+ if (last === void 0) return void 0;
504
+ const color = last.attrs.get("stop-color");
505
+ if (color === void 0 || color.trim() === "") return void 0;
506
+ const opacity = last.attrs.get("stop-opacity");
507
+ return {
508
+ color,
509
+ ...opacity === void 0 ? {} : { opacity }
510
+ };
511
+ }
512
+ function sanitizeGeometryElementBbox(element, canvas, safeViewport, requireArea) {
513
+ switch (element.tag) {
514
+ case "rect": {
515
+ if (hasNonFiniteAttr(element, [
516
+ "x",
517
+ "y",
518
+ "width",
519
+ "height",
520
+ "rx",
521
+ "ry"
522
+ ])) return null;
523
+ const x = parseLengthAttr(element.attrs.get("x"), canvas.width, 0);
524
+ const y = parseLengthAttr(element.attrs.get("y"), canvas.height, 0);
525
+ const width = parseLengthAttr(element.attrs.get("width"), canvas.width, 0);
526
+ const height = parseLengthAttr(element.attrs.get("height"), canvas.height, 0);
527
+ if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(width) || !Number.isFinite(height) || width <= EPSILON || height <= EPSILON) return null;
528
+ if (x < safeViewport.minX || y < safeViewport.minY || x + width > safeViewport.maxX || y + height > safeViewport.maxY) {
529
+ const clamped = clampRectToSafeViewport(x, y, width, height, safeViewport);
530
+ if (clamped === null) return null;
531
+ if (!element.attrs.get("width")?.trim().endsWith("%")) {
532
+ element.attrs.set("x", formatNumber(clamped.x));
533
+ element.attrs.set("y", formatNumber(clamped.y));
534
+ element.attrs.set("width", formatNumber(clamped.width));
535
+ element.attrs.set("height", formatNumber(clamped.height));
536
+ }
537
+ return {
538
+ minX: clamped.x,
539
+ minY: clamped.y,
540
+ maxX: clamped.x + clamped.width,
541
+ maxY: clamped.y + clamped.height
542
+ };
543
+ }
544
+ return {
545
+ minX: x,
546
+ minY: y,
547
+ maxX: x + width,
548
+ maxY: y + height
549
+ };
550
+ }
551
+ case "image": {
552
+ if (hasNonFiniteAttr(element, [
553
+ "x",
554
+ "y",
555
+ "width",
556
+ "height"
557
+ ])) return null;
558
+ const x = parseNumber(element.attrs.get("x")) ?? 0;
559
+ const y = parseNumber(element.attrs.get("y")) ?? 0;
560
+ const width = parseNumber(element.attrs.get("width")) ?? 0;
561
+ const height = parseNumber(element.attrs.get("height")) ?? 0;
562
+ if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(width) || !Number.isFinite(height) || width <= EPSILON || height <= EPSILON) return null;
563
+ return {
564
+ minX: x,
565
+ minY: y,
566
+ maxX: x + width,
567
+ maxY: y + height
568
+ };
569
+ }
570
+ case "path": {
571
+ const d = element.attrs.get("d");
572
+ if (d === void 0) return null;
573
+ const bbox = pathBbox(d);
574
+ if (bbox === null) return null;
575
+ if (requireArea && (bbox.maxX - bbox.minX <= EPSILON || bbox.maxY - bbox.minY <= EPSILON)) return null;
576
+ return bbox;
577
+ }
578
+ case "circle": {
579
+ if (hasNonFiniteAttr(element, [
580
+ "cx",
581
+ "cy",
582
+ "r"
583
+ ])) return null;
584
+ const cx = parseNumber(element.attrs.get("cx")) ?? 0;
585
+ const cy = parseNumber(element.attrs.get("cy")) ?? 0;
586
+ const r = parseNumber(element.attrs.get("r")) ?? 0;
587
+ if (!Number.isFinite(cx) || !Number.isFinite(cy) || !Number.isFinite(r) || r <= EPSILON) return null;
588
+ return {
589
+ minX: cx - r,
590
+ minY: cy - r,
591
+ maxX: cx + r,
592
+ maxY: cy + r
593
+ };
594
+ }
595
+ case "ellipse": {
596
+ if (hasNonFiniteAttr(element, [
597
+ "cx",
598
+ "cy",
599
+ "rx",
600
+ "ry"
601
+ ])) return null;
602
+ const cx = parseNumber(element.attrs.get("cx")) ?? 0;
603
+ const cy = parseNumber(element.attrs.get("cy")) ?? 0;
604
+ const rx = parseNumber(element.attrs.get("rx")) ?? 0;
605
+ const ry = parseNumber(element.attrs.get("ry")) ?? 0;
606
+ if (!Number.isFinite(cx) || !Number.isFinite(cy) || !Number.isFinite(rx) || !Number.isFinite(ry) || rx <= EPSILON || ry <= EPSILON) return null;
607
+ return {
608
+ minX: cx - rx,
609
+ minY: cy - ry,
610
+ maxX: cx + rx,
611
+ maxY: cy + ry
612
+ };
613
+ }
614
+ case "line": {
615
+ if (hasNonFiniteAttr(element, [
616
+ "x1",
617
+ "y1",
618
+ "x2",
619
+ "y2"
620
+ ])) return null;
621
+ const x1 = parseNumber(element.attrs.get("x1")) ?? 0;
622
+ const y1 = parseNumber(element.attrs.get("y1")) ?? 0;
623
+ const x2 = parseNumber(element.attrs.get("x2")) ?? 0;
624
+ const y2 = parseNumber(element.attrs.get("y2")) ?? 0;
625
+ if (!Number.isFinite(x1) || !Number.isFinite(y1) || !Number.isFinite(x2) || !Number.isFinite(y2)) return null;
626
+ return {
627
+ minX: Math.min(x1, x2),
628
+ minY: Math.min(y1, y2),
629
+ maxX: Math.max(x1, x2),
630
+ maxY: Math.max(y1, y2)
631
+ };
632
+ }
633
+ case "polygon":
634
+ case "polyline": {
635
+ const points = element.attrs.get("points");
636
+ if (points === void 0) return null;
637
+ const bbox = extractPointsBbox(points);
638
+ if (bbox === null) return null;
639
+ if (requireArea && (bbox.maxX - bbox.minX <= EPSILON || bbox.maxY - bbox.minY <= EPSILON)) return null;
640
+ return bbox;
641
+ }
642
+ case "use": {
643
+ if (hasNonFiniteAttr(element, [
644
+ "x",
645
+ "y",
646
+ "width",
647
+ "height"
648
+ ])) return null;
649
+ const x = parseNumber(element.attrs.get("x")) ?? 0;
650
+ const y = parseNumber(element.attrs.get("y")) ?? 0;
651
+ const width = parseNumber(element.attrs.get("width")) ?? 1;
652
+ const height = parseNumber(element.attrs.get("height")) ?? 1;
653
+ if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(width) || !Number.isFinite(height) || width <= EPSILON || height <= EPSILON) return null;
654
+ return {
655
+ minX: x,
656
+ minY: y,
657
+ maxX: x + width,
658
+ maxY: y + height
659
+ };
660
+ }
661
+ default: return null;
662
+ }
663
+ }
664
+ function sanitizeDefsBlock(defsElement, canvas, safeViewport, state) {
665
+ const keptDefs = [];
666
+ for (const child of defsElement.children) {
667
+ if (child.kind !== "element") continue;
668
+ const id = child.attrs.get("id");
669
+ switch (child.tag) {
670
+ case "clipPath": {
671
+ const units = child.attrs.get("clipPathUnits") ?? "userSpaceOnUse";
672
+ const keptChildren = [];
673
+ for (const item of child.children) {
674
+ if (item.kind !== "element") continue;
675
+ const localMatrix = parseTransformAttr(item.attrs.get("transform"));
676
+ if (localMatrix === null) continue;
677
+ const bbox = sanitizeGeometryElementBbox(item, canvas, safeViewport, true);
678
+ if (bbox === null) continue;
679
+ if (units === "userSpaceOnUse") {
680
+ if (!boxesIntersect(transformBbox(bbox, localMatrix), safeViewport)) continue;
681
+ }
682
+ keptChildren.push(item);
683
+ }
684
+ if (keptChildren.length === 0) {
685
+ if (id !== void 0) state.droppedClipIds.add(id);
686
+ continue;
687
+ }
688
+ child.children.length = 0;
689
+ child.children.push(...keptChildren);
690
+ keptDefs.push(child);
691
+ break;
692
+ }
693
+ case "linearGradient": {
694
+ if (hasNonFiniteAttr(child, [
695
+ "x1",
696
+ "y1",
697
+ "x2",
698
+ "y2"
699
+ ])) {
700
+ if (id !== void 0) {
701
+ const fallback = lastGradientStopColor(child);
702
+ if (fallback !== void 0) state.gradientFallbacks.set(id, fallback);
703
+ else state.droppedGradientIds.add(id);
704
+ }
705
+ continue;
706
+ }
707
+ const x1 = parseLengthAttr(child.attrs.get("x1"), 1, 0);
708
+ const y1 = parseLengthAttr(child.attrs.get("y1"), 1, 0);
709
+ const x2 = parseLengthAttr(child.attrs.get("x2"), 1, 1);
710
+ const y2 = parseLengthAttr(child.attrs.get("y2"), 1, 0);
711
+ if (!Number.isFinite(x1) || !Number.isFinite(y1) || !Number.isFinite(x2) || !Number.isFinite(y2) || Math.hypot(x2 - x1, y2 - y1) <= EPSILON) {
712
+ if (id !== void 0) {
713
+ const fallback = lastGradientStopColor(child);
714
+ if (fallback !== void 0) state.gradientFallbacks.set(id, fallback);
715
+ else state.droppedGradientIds.add(id);
716
+ }
717
+ continue;
718
+ }
719
+ keptDefs.push(child);
720
+ break;
721
+ }
722
+ case "radialGradient": {
723
+ if (hasNonFiniteAttr(child, [
724
+ "cx",
725
+ "cy",
726
+ "r",
727
+ "fx",
728
+ "fy"
729
+ ])) {
730
+ if (id !== void 0) {
731
+ const fallback = lastGradientStopColor(child);
732
+ if (fallback !== void 0) state.gradientFallbacks.set(id, fallback);
733
+ else state.droppedGradientIds.add(id);
734
+ }
735
+ continue;
736
+ }
737
+ const r = parseLengthAttr(child.attrs.get("r"), 1, .5);
738
+ if (!Number.isFinite(r) || r <= EPSILON) {
739
+ if (id !== void 0) {
740
+ const fallback = lastGradientStopColor(child);
741
+ if (fallback !== void 0) state.gradientFallbacks.set(id, fallback);
742
+ else state.droppedGradientIds.add(id);
743
+ }
744
+ continue;
745
+ }
746
+ keptDefs.push(child);
747
+ break;
748
+ }
749
+ case "filter": {
750
+ if (hasNonFiniteAttr(child, [
751
+ "x",
752
+ "y",
753
+ "width",
754
+ "height"
755
+ ])) {
756
+ if (id !== void 0) state.droppedFilterIds.add(id);
757
+ continue;
758
+ }
759
+ if ((child.attrs.get("filterUnits") ?? "objectBoundingBox") === "userSpaceOnUse") {
760
+ const clamped = clampRectToSafeViewport(parseNumber(child.attrs.get("x")) ?? 0, parseNumber(child.attrs.get("y")) ?? 0, parseNumber(child.attrs.get("width")) ?? 0, parseNumber(child.attrs.get("height")) ?? 0, safeViewport);
761
+ if (clamped === null) {
762
+ if (id !== void 0) state.droppedFilterIds.add(id);
763
+ continue;
764
+ }
765
+ child.attrs.set("x", formatNumber(clamped.x));
766
+ child.attrs.set("y", formatNumber(clamped.y));
767
+ child.attrs.set("width", formatNumber(clamped.width));
768
+ child.attrs.set("height", formatNumber(clamped.height));
769
+ }
770
+ let validFilter = true;
771
+ for (const prim of child.children) {
772
+ if (prim.kind !== "element") continue;
773
+ if (hasNonFiniteAttr(prim, [
774
+ "stdDeviation",
775
+ "dx",
776
+ "dy"
777
+ ])) {
778
+ validFilter = false;
779
+ break;
780
+ }
781
+ const stdRaw = prim.attrs.get("stdDeviation");
782
+ if (stdRaw !== void 0) {
783
+ const std = parseNumber(stdRaw);
784
+ if (std === void 0 || !Number.isFinite(std) || std < 0) {
785
+ validFilter = false;
786
+ break;
787
+ }
788
+ if (std > MAX_BLUR_STD_DEVIATION) prim.attrs.set("stdDeviation", formatNumber(MAX_BLUR_STD_DEVIATION));
789
+ }
790
+ }
791
+ if (!validFilter) {
792
+ if (id !== void 0) state.droppedFilterIds.add(id);
793
+ continue;
794
+ }
795
+ keptDefs.push(child);
796
+ break;
797
+ }
798
+ case "mask":
799
+ if (hasNonFiniteAttr(child, [
800
+ "x",
801
+ "y",
802
+ "width",
803
+ "height"
804
+ ])) {
805
+ if (id !== void 0) state.droppedMaskIds.add(id);
806
+ continue;
807
+ }
808
+ if ((child.attrs.get("maskUnits") ?? "objectBoundingBox") === "userSpaceOnUse") {
809
+ const clamped = clampRectToSafeViewport(parseNumber(child.attrs.get("x")) ?? 0, parseNumber(child.attrs.get("y")) ?? 0, parseNumber(child.attrs.get("width")) ?? 0, parseNumber(child.attrs.get("height")) ?? 0, safeViewport);
810
+ if (clamped === null) {
811
+ if (id !== void 0) state.droppedMaskIds.add(id);
812
+ continue;
813
+ }
814
+ child.attrs.set("x", formatNumber(clamped.x));
815
+ child.attrs.set("y", formatNumber(clamped.y));
816
+ child.attrs.set("width", formatNumber(clamped.width));
817
+ child.attrs.set("height", formatNumber(clamped.height));
818
+ }
819
+ keptDefs.push(child);
820
+ break;
821
+ case "pattern":
822
+ if (hasNonFiniteAttr(child, [
823
+ "x",
824
+ "y",
825
+ "width",
826
+ "height"
827
+ ])) {
828
+ if (id !== void 0) state.droppedPatternIds.add(id);
829
+ continue;
830
+ }
831
+ if ((child.attrs.get("patternUnits") ?? "objectBoundingBox") === "userSpaceOnUse") {
832
+ const x = parseNumber(child.attrs.get("x")) ?? 0;
833
+ const y = parseNumber(child.attrs.get("y")) ?? 0;
834
+ const width = parseNumber(child.attrs.get("width")) ?? 0;
835
+ const height = parseNumber(child.attrs.get("height")) ?? 0;
836
+ const clamped = clampRectToSafeViewport(x, y, width, height, safeViewport);
837
+ if (clamped === null) {
838
+ if (id !== void 0) state.droppedPatternIds.add(id);
839
+ continue;
840
+ }
841
+ if (x < safeViewport.minX || y < safeViewport.minY || x + width > safeViewport.maxX || y + height > safeViewport.maxY) {
842
+ child.attrs.set("x", formatNumber(clamped.x));
843
+ child.attrs.set("y", formatNumber(clamped.y));
844
+ child.attrs.set("width", formatNumber(clamped.width));
845
+ child.attrs.set("height", formatNumber(clamped.height));
846
+ for (const pChild of child.children) {
847
+ if (pChild.kind !== "element") continue;
848
+ if (pChild.tag === "rect" || pChild.tag === "image") {
849
+ const cw = parseNumber(pChild.attrs.get("width")) ?? 0;
850
+ const ch = parseNumber(pChild.attrs.get("height")) ?? 0;
851
+ if (cw > clamped.width) pChild.attrs.set("width", formatNumber(clamped.width));
852
+ if (ch > clamped.height) pChild.attrs.set("height", formatNumber(clamped.height));
853
+ }
854
+ }
855
+ }
856
+ }
857
+ keptDefs.push(child);
858
+ break;
859
+ default: keptDefs.push(child);
860
+ }
861
+ }
862
+ defsElement.children.length = 0;
863
+ defsElement.children.push(...keptDefs);
864
+ }
865
+ function applyPaintRefFallbacks(element, state) {
866
+ const clipId = extractUrlId(element.attrs.get("clip-path"));
867
+ if (clipId !== void 0 && state.droppedClipIds.has(clipId)) return false;
868
+ const maskId = extractUrlId(element.attrs.get("mask"));
869
+ if (maskId !== void 0 && state.droppedMaskIds.has(maskId)) return false;
870
+ const filterId = extractUrlId(element.attrs.get("filter"));
871
+ if (filterId !== void 0 && state.droppedFilterIds.has(filterId)) element.attrs.delete("filter");
872
+ for (const attr of ["fill", "stroke"]) {
873
+ const refId = extractUrlId(element.attrs.get(attr));
874
+ if (refId === void 0) continue;
875
+ const fallback = state.gradientFallbacks.get(refId);
876
+ if (fallback !== void 0) {
877
+ element.attrs.set(attr, fallback.color);
878
+ if (fallback.opacity !== void 0) element.attrs.set(`${attr}-opacity`, fallback.opacity);
879
+ continue;
880
+ }
881
+ if (state.droppedGradientIds.has(refId) || state.droppedPatternIds.has(refId)) element.attrs.set(attr, "none");
882
+ }
883
+ return true;
884
+ }
885
+ function sanitizeBodyNode(node, ctm, canvas, safeViewport, state) {
886
+ if (node.kind === "text") return node;
887
+ if (node.tag === "defs") {
888
+ sanitizeDefsBlock(node, canvas, safeViewport, state);
889
+ return node.children.length === 0 ? null : node;
890
+ }
891
+ const localMatrix = parseTransformAttr(node.attrs.get("transform"));
892
+ if (localMatrix === null) return null;
893
+ const nextCtm = multiplyMatrix(ctm, localMatrix);
894
+ if (nextCtm.some((v) => !Number.isFinite(v))) return null;
895
+ if (!applyPaintRefFallbacks(node, state)) return null;
896
+ if (node.tag === "g") {
897
+ const keptChildren = [];
898
+ for (const child of node.children) {
899
+ const sanitizedChild = sanitizeBodyNode(child, nextCtm, canvas, safeViewport, state);
900
+ if (sanitizedChild !== null) keptChildren.push(sanitizedChild);
901
+ }
902
+ if (keptChildren.length === 0) return null;
903
+ node.children.length = 0;
904
+ node.children.push(...keptChildren);
905
+ return node;
906
+ }
907
+ const hasStroke = node.attrs.get("stroke") !== void 0 && node.attrs.get("stroke") !== "none";
908
+ if (hasNonFiniteAttr(node, [
909
+ "stroke-width",
910
+ "opacity",
911
+ "fill-opacity",
912
+ "stroke-opacity"
913
+ ])) return null;
914
+ const strokeWidth = parseNumber(node.attrs.get("stroke-width")) ?? 0;
915
+ if (!Number.isFinite(strokeWidth) || strokeWidth < 0) return null;
916
+ if (node.tag === "rect" && (node.attrs.get("width")?.trim().endsWith("%") || node.attrs.get("height")?.trim().endsWith("%"))) return node;
917
+ const localBbox = sanitizeGeometryElementBbox(node, canvas, safeViewport, !hasStroke);
918
+ if (localBbox === null) return null;
919
+ const pad = hasStroke ? Math.max(2, strokeWidth) : 0;
920
+ if (!boxesIntersect(transformBbox({
921
+ minX: localBbox.minX - pad,
922
+ minY: localBbox.minY - pad,
923
+ maxX: localBbox.maxX + pad,
924
+ maxY: localBbox.maxY + pad
925
+ }, nextCtm), safeViewport)) return null;
926
+ if (!hasStroke && node.attrs.get("fill") === "none" && node.attrs.get("clip-path") === void 0) return null;
927
+ return node;
928
+ }
929
+ function resolveRasterTokensInSvgTree(svgRoot, registry) {
930
+ const occurrences = [];
931
+ const groups = /* @__PURE__ */ new Map();
932
+ const visitChildren = (parent) => {
933
+ for (let i = 0; i < parent.children.length; i += 1) {
934
+ const child = parent.children[i];
935
+ if (child === void 0 || child.kind !== "element") continue;
936
+ if (child.tag === "image") {
937
+ const href = child.attrs.get("href");
938
+ if (href !== void 0 && href.startsWith(RASTER_TOKEN_PREFIX)) {
939
+ const id = Number(href.slice(18));
940
+ const entry = Number.isInteger(id) && id >= 0 ? registry.getEntry(id) : void 0;
941
+ if (entry !== void 0) {
942
+ const x = child.attrs.get("x") ?? "0";
943
+ const y = child.attrs.get("y") ?? "0";
944
+ const width = child.attrs.get("width");
945
+ const height = child.attrs.get("height");
946
+ const preserveAspectRatio = child.attrs.get("preserveAspectRatio");
947
+ const numX = Number(x);
948
+ const numY = Number(y);
949
+ const numW = Number(width);
950
+ const numH = Number(height);
951
+ const groupKey = !(width !== void 0 && height !== void 0 && Number.isFinite(numX) && Number.isFinite(numY) && Number.isFinite(numW) && Number.isFinite(numH) && numW > EPSILON && numH > EPSILON) ? void 0 : preserveAspectRatio === "none" ? `${entry.id}|none` : `${entry.id}|${preserveAspectRatio ?? ""}|${width}|${height}`;
952
+ const occurrence = {
953
+ parent,
954
+ childIndex: i,
955
+ element: child,
956
+ entry,
957
+ x,
958
+ y,
959
+ width,
960
+ height,
961
+ preserveAspectRatio,
962
+ groupKey
963
+ };
964
+ occurrences.push(occurrence);
965
+ if (groupKey !== void 0) {
966
+ let bucket = groups.get(groupKey);
967
+ if (bucket === void 0) {
968
+ bucket = [];
969
+ groups.set(groupKey, bucket);
970
+ }
971
+ bucket.push(occurrence);
972
+ }
973
+ }
974
+ }
975
+ }
976
+ visitChildren(child);
977
+ }
978
+ };
979
+ visitChildren(svgRoot);
980
+ if (occurrences.length === 0) return;
981
+ for (const occurrence of occurrences) if (occurrence.groupKey === void 0 || (groups.get(occurrence.groupKey)?.length ?? 0) < 2) occurrence.element.attrs.set("href", registry.getDataUri(occurrence.entry));
982
+ const sharedDefImages = [];
983
+ let sharedSeq = 0;
984
+ for (const members of groups.values()) {
985
+ if (members.length < 2) continue;
986
+ const first = members[0];
987
+ sharedSeq += 1;
988
+ const sharedId = `${registry.idPrefix}-${sharedSeq}`;
989
+ const isUnitStretch = first.preserveAspectRatio === "none";
990
+ const defAttrs = /* @__PURE__ */ new Map([
991
+ ["id", sharedId],
992
+ ["x", "0"],
993
+ ["y", "0"],
994
+ ["width", isUnitStretch ? "1" : first.width],
995
+ ["height", isUnitStretch ? "1" : first.height]
996
+ ]);
997
+ if (first.preserveAspectRatio !== void 0) defAttrs.set("preserveAspectRatio", first.preserveAspectRatio);
998
+ defAttrs.set("href", registry.getDataUri(first.entry));
999
+ sharedDefImages.push({
1000
+ kind: "element",
1001
+ tag: "image",
1002
+ attrs: defAttrs,
1003
+ children: [],
1004
+ selfClosing: true
1005
+ });
1006
+ for (const member of members) {
1007
+ const passthroughAttrs = /* @__PURE__ */ new Map();
1008
+ for (const [key, value] of member.element.attrs.entries()) {
1009
+ if (key === "href" || key === "x" || key === "y" || key === "width" || key === "height" || key === "preserveAspectRatio") continue;
1010
+ passthroughAttrs.set(key, value);
1011
+ }
1012
+ const useAttrs = /* @__PURE__ */ new Map([["href", `#${sharedId}`]]);
1013
+ const transform = isUnitStretch ? `translate(${member.x} ${member.y}) scale(${member.width} ${member.height})` : Number(member.x) === 0 && Number(member.y) === 0 ? void 0 : `translate(${member.x} ${member.y})`;
1014
+ if (transform !== void 0) useAttrs.set("transform", transform);
1015
+ const useNode = {
1016
+ kind: "element",
1017
+ tag: "use",
1018
+ attrs: useAttrs,
1019
+ children: [],
1020
+ selfClosing: true
1021
+ };
1022
+ member.parent.children[member.childIndex] = passthroughAttrs.size === 0 ? useNode : {
1023
+ kind: "element",
1024
+ tag: "g",
1025
+ attrs: passthroughAttrs,
1026
+ children: [useNode],
1027
+ selfClosing: false
1028
+ };
1029
+ }
1030
+ }
1031
+ if (sharedDefImages.length > 0) svgRoot.children.unshift({
1032
+ kind: "element",
1033
+ tag: "defs",
1034
+ attrs: /* @__PURE__ */ new Map(),
1035
+ children: sharedDefImages,
1036
+ selfClosing: false
1037
+ });
1038
+ }
1039
+ /**
1040
+ * Sanitizes an assembled single-page SVG before handing it to `@resvg/resvg-js`,
1041
+ * eliminating zero-area clipPaths/masks/filters, degenerate gradients, non-finite numbers,
1042
+ * and extreme off-canvas geometry that trigger Rust `geom.rs` panics, then materializes
1043
+ * registered page raster tokens into either inline `<image>` or shared `<defs>` + `<use>`.
1044
+ */
1045
+ function sanitizeSvgForRasterization(svg, dimensions, rasterRegistry) {
1046
+ const width = Number.isFinite(dimensions.width) && dimensions.width > EPSILON ? dimensions.width : 1;
1047
+ const height = Number.isFinite(dimensions.height) && dimensions.height > EPSILON ? dimensions.height : 1;
1048
+ const safeViewport = {
1049
+ minX: -512,
1050
+ minY: -512,
1051
+ maxX: width + SAFE_OFFSCREEN_PAD,
1052
+ maxY: height + SAFE_OFFSCREEN_PAD
1053
+ };
1054
+ const state = {
1055
+ droppedClipIds: /* @__PURE__ */ new Set(),
1056
+ droppedMaskIds: /* @__PURE__ */ new Set(),
1057
+ droppedPatternIds: /* @__PURE__ */ new Set(),
1058
+ droppedFilterIds: /* @__PURE__ */ new Set(),
1059
+ droppedGradientIds: /* @__PURE__ */ new Set(),
1060
+ gradientFallbacks: /* @__PURE__ */ new Map()
1061
+ };
1062
+ const nodes = parseSvgTree(svg);
1063
+ const svgRoot = nodes.find((node) => node.kind === "element" && node.tag === "svg");
1064
+ if (svgRoot === void 0) return svg;
1065
+ const collectDefs = (el) => {
1066
+ for (const child of el.children) {
1067
+ if (child.kind !== "element") continue;
1068
+ if (child.tag === "defs") sanitizeDefsBlock(child, {
1069
+ width,
1070
+ height
1071
+ }, safeViewport, state);
1072
+ else collectDefs(child);
1073
+ }
1074
+ };
1075
+ collectDefs(svgRoot);
1076
+ const keptRootChildren = [];
1077
+ for (const child of svgRoot.children) {
1078
+ const sanitized = sanitizeBodyNode(child, IDENTITY_MATRIX, {
1079
+ width,
1080
+ height
1081
+ }, safeViewport, state);
1082
+ if (sanitized !== null) keptRootChildren.push(sanitized);
1083
+ }
1084
+ svgRoot.children.length = 0;
1085
+ svgRoot.children.push(...keptRootChildren);
1086
+ if (rasterRegistry !== void 0 && rasterRegistry.size > 0) resolveRasterTokensInSvgTree(svgRoot, rasterRegistry);
1087
+ return nodes.map(serializeSvgNode).join("");
1088
+ }
1089
+ export { createPageRasterRegistry, sanitizeSvgForRasterization };