js.documents 2.1.0 → 2.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 (71) hide show
  1. package/README.md +43 -29
  2. package/dist/codecs/registry.cjs +7 -0
  3. package/dist/codecs/registry.js +7 -0
  4. package/dist/convert/capability.cjs +5 -0
  5. package/dist/convert/capability.js +5 -0
  6. package/dist/convert/codec.cjs +10 -0
  7. package/dist/convert/codec.d.cts +3 -1
  8. package/dist/convert/codec.d.ts +3 -1
  9. package/dist/convert/codec.js +11 -3
  10. package/dist/convert/composition.cjs +17 -0
  11. package/dist/convert/composition.d.cts +5 -2
  12. package/dist/convert/composition.d.ts +5 -2
  13. package/dist/convert/composition.js +17 -0
  14. package/dist/convert/convert.cjs +16 -0
  15. package/dist/convert/convert.d.cts +13 -1
  16. package/dist/convert/convert.d.ts +13 -1
  17. package/dist/convert/convert.js +13 -1
  18. package/dist/convert/local.cjs +2 -1
  19. package/dist/convert/local.js +2 -1
  20. package/dist/convert/port.cjs +1 -0
  21. package/dist/convert/port.d.cts +2 -0
  22. package/dist/convert/port.d.ts +2 -0
  23. package/dist/convert/port.js +1 -0
  24. package/dist/edit/odg/scaffold.js +8 -8
  25. package/dist/edit/odp/scaffold.js +8 -8
  26. package/dist/edit/ods/scaffold.js +8 -8
  27. package/dist/edit/odt/scaffold.js +8 -8
  28. package/dist/index.cjs +21 -0
  29. package/dist/index.d.cts +8 -4
  30. package/dist/index.d.ts +8 -4
  31. package/dist/index.js +8 -4
  32. package/dist/metadata/write.cjs +1 -0
  33. package/dist/metadata/write.js +1 -0
  34. package/dist/model/bytes.cjs +5 -0
  35. package/dist/model/bytes.d.cts +2 -1
  36. package/dist/model/bytes.d.ts +2 -1
  37. package/dist/model/bytes.js +5 -1
  38. package/dist/svg/diagnostics.cjs +19 -0
  39. package/dist/svg/diagnostics.d.cts +10 -0
  40. package/dist/svg/diagnostics.d.ts +10 -0
  41. package/dist/svg/diagnostics.js +18 -0
  42. package/dist/svg/paint.cjs +817 -0
  43. package/dist/svg/paint.d.cts +19 -0
  44. package/dist/svg/paint.d.ts +19 -0
  45. package/dist/svg/paint.js +814 -0
  46. package/dist/svg/path.cjs +337 -0
  47. package/dist/svg/path.d.cts +22 -0
  48. package/dist/svg/path.d.ts +22 -0
  49. package/dist/svg/path.js +336 -0
  50. package/dist/svg/read.cjs +677 -0
  51. package/dist/svg/read.d.cts +12 -0
  52. package/dist/svg/read.d.ts +12 -0
  53. package/dist/svg/read.js +675 -0
  54. package/dist/svg/text.cjs +22 -0
  55. package/dist/svg/text.d.cts +8 -0
  56. package/dist/svg/text.d.ts +8 -0
  57. package/dist/svg/text.js +19 -0
  58. package/dist/svg/transform.cjs +206 -0
  59. package/dist/svg/transform.d.cts +23 -0
  60. package/dist/svg/transform.d.ts +23 -0
  61. package/dist/svg/transform.js +197 -0
  62. package/dist/svg/units.cjs +47 -0
  63. package/dist/svg/units.d.cts +12 -0
  64. package/dist/svg/units.d.ts +12 -0
  65. package/dist/svg/units.js +44 -0
  66. package/dist/svg/write.cjs +127 -0
  67. package/dist/svg/write.d.cts +23 -0
  68. package/dist/svg/write.d.ts +23 -0
  69. package/dist/svg/write.js +123 -0
  70. package/dist/xml/odf-text.js +2 -2
  71. package/package.json +1 -1
@@ -0,0 +1,675 @@
1
+ import { parseSvgDashStyle, parseSvgPaint } from "./paint.js";
2
+ import { parseSvgPathData } from "./path.js";
3
+ import { parseSvgLengthPt, parseSvgUserUnits, parseSvgViewBox } from "./units.js";
4
+ import { applyMatrix, composeMatrices, isAxisAligned, isNonReflectingSimilarity, meanScaleFactor, parseSvgTransform, similarityRotationDeg } from "./transform.js";
5
+ import { decodeEntities } from "ooxml.js";
6
+ import { CONTENT_FORMAT_VERSION } from "document-schema.js";
7
+ import { parseXml } from "odf.js";
8
+ //#region src/svg/read.ts
9
+ var SvgMissingRootElementError = class extends Error {
10
+ constructor() {
11
+ super("svg text must contain an <svg> root element");
12
+ this.name = "SvgMissingRootElementError";
13
+ }
14
+ };
15
+ const DEFAULT_WIDTH_PT = 225;
16
+ const DEFAULT_HEIGHT_PT = 112.5;
17
+ const KAPPA = 4 / 3 * (Math.SQRT2 - 1);
18
+ function localName(tag) {
19
+ const colon = tag.indexOf(":");
20
+ return colon === -1 ? tag : tag.slice(colon + 1);
21
+ }
22
+ function findAttribute(element, name) {
23
+ for (const attribute of element.attributes) if (localName(attribute.name) === name) return decodeEntities(attribute.value);
24
+ }
25
+ function elementChildren(node) {
26
+ return node.type === "element" ? node.children.filter((child) => child.type === "element") : [];
27
+ }
28
+ function textOf(element) {
29
+ let text = "";
30
+ for (const child of element.children) if (child.type === "text" || child.type === "cdata") text += decodeEntities(child.value);
31
+ return text;
32
+ }
33
+ function childPaintState(element, parent) {
34
+ return {
35
+ fillSpec: findAttribute(element, "fill") ?? parent.fillSpec,
36
+ strokeSpec: findAttribute(element, "stroke") ?? parent.strokeSpec,
37
+ strokeWidthSpec: findAttribute(element, "stroke-width") ?? parent.strokeWidthSpec,
38
+ fillRuleSpec: findAttribute(element, "fill-rule") ?? parent.fillRuleSpec,
39
+ dashSpec: findAttribute(element, "stroke-dasharray") ?? parent.dashSpec
40
+ };
41
+ }
42
+ function report(state, code, detail) {
43
+ state.sink?.(detail === void 0 ? { code } : {
44
+ code,
45
+ detail
46
+ });
47
+ }
48
+ function resolveFillPaint(state, spec, isFill) {
49
+ if (spec === void 0) return isFill ? {
50
+ r: 0,
51
+ g: 0,
52
+ b: 0
53
+ } : void 0;
54
+ const paint = parseSvgPaint(spec);
55
+ if (paint === void 0) {
56
+ report(state, "svg/paint-unsupported", spec);
57
+ return isFill ? {
58
+ r: 0,
59
+ g: 0,
60
+ b: 0
61
+ } : void 0;
62
+ }
63
+ if (paint.kind === "none") return;
64
+ if (paint.kind === "url") {
65
+ report(state, "svg/gradient-unsupported", `#${paint.fragment}`);
66
+ return;
67
+ }
68
+ if (paint.kind === "currentColor") {
69
+ report(state, "svg/paint-unsupported", "currentColor renders as black: the CSS color property is out of scope");
70
+ return {
71
+ r: 0,
72
+ g: 0,
73
+ b: 0
74
+ };
75
+ }
76
+ return paint.color;
77
+ }
78
+ function resolveStroke(state, paint, ctm) {
79
+ const color = resolveFillPaint(state, paint.strokeSpec, false);
80
+ if (color === void 0) return;
81
+ const widthPt = (parseSvgUserUnits(paint.strokeWidthSpec) ?? 1) * meanScaleFactor(ctm);
82
+ if (!(widthPt > 0)) return;
83
+ const style = parseSvgDashStyle(paint.dashSpec);
84
+ return style === void 0 ? {
85
+ color,
86
+ widthPt
87
+ } : {
88
+ color,
89
+ widthPt,
90
+ style
91
+ };
92
+ }
93
+ function resolvePaint(state, paint, ctm) {
94
+ return {
95
+ fill: resolveFillPaint(state, paint.fillSpec, true),
96
+ stroke: resolveStroke(state, paint, ctm)
97
+ };
98
+ }
99
+ function boxOfPoints(points) {
100
+ let minX = Number.POSITIVE_INFINITY;
101
+ let minY = Number.POSITIVE_INFINITY;
102
+ let maxX = Number.NEGATIVE_INFINITY;
103
+ let maxY = Number.NEGATIVE_INFINITY;
104
+ for (const point of points) {
105
+ minX = Math.min(minX, point.x);
106
+ minY = Math.min(minY, point.y);
107
+ maxX = Math.max(maxX, point.x);
108
+ maxY = Math.max(maxY, point.y);
109
+ }
110
+ return {
111
+ xPt: minX,
112
+ yPt: minY,
113
+ widthPt: maxX - minX,
114
+ heightPt: maxY - minY
115
+ };
116
+ }
117
+ function axisAlignedFrame(ctm, x, y, width, height) {
118
+ return boxOfPoints([
119
+ applyMatrix(ctm, x, y),
120
+ applyMatrix(ctm, x + width, y),
121
+ applyMatrix(ctm, x + width, y + height),
122
+ applyMatrix(ctm, x, y + height)
123
+ ]);
124
+ }
125
+ function similarityFrame(ctm, x, y, width, height) {
126
+ const centre = applyMatrix(ctm, x + width / 2, y + height / 2);
127
+ const scale = Math.hypot(ctm.a, ctm.b);
128
+ return {
129
+ xPt: centre.x - scale * width / 2,
130
+ yPt: centre.y - scale * height / 2,
131
+ widthPt: scale * width,
132
+ heightPt: scale * height
133
+ };
134
+ }
135
+ function buildPathVector(state, subpaths, ctm, paint, fillRule) {
136
+ const placed = subpaths.map((subpath) => ({
137
+ start: applyMatrix(ctm, subpath.start.x, subpath.start.y),
138
+ closed: subpath.closed,
139
+ segments: subpath.segments.map((segment) => segment.kind === "line" ? {
140
+ kind: "line",
141
+ to: applyMatrix(ctm, segment.to.x, segment.to.y)
142
+ } : {
143
+ kind: "cubic",
144
+ control1: applyMatrix(ctm, segment.control1.x, segment.control1.y),
145
+ control2: applyMatrix(ctm, segment.control2.x, segment.control2.y),
146
+ to: applyMatrix(ctm, segment.to.x, segment.to.y)
147
+ })
148
+ }));
149
+ const frame = boxOfPoints(placed.flatMap((subpath) => [subpath.start, ...subpath.segments.flatMap((segment) => segment.kind === "line" ? [segment.to] : [
150
+ segment.control1,
151
+ segment.control2,
152
+ segment.to
153
+ ])]));
154
+ if (frame.widthPt === 0 && frame.heightPt === 0) return;
155
+ const localSubpaths = placed.map((subpath) => ({
156
+ start: {
157
+ xPt: subpath.start.x - frame.xPt,
158
+ yPt: subpath.start.y - frame.yPt
159
+ },
160
+ closed: subpath.closed,
161
+ segments: subpath.segments.map((segment) => segment.kind === "line" ? {
162
+ kind: "line",
163
+ to: {
164
+ xPt: segment.to.x - frame.xPt,
165
+ yPt: segment.to.y - frame.yPt
166
+ }
167
+ } : {
168
+ kind: "cubic",
169
+ control1: {
170
+ xPt: segment.control1.x - frame.xPt,
171
+ yPt: segment.control1.y - frame.yPt
172
+ },
173
+ control2: {
174
+ xPt: segment.control2.x - frame.xPt,
175
+ yPt: segment.control2.y - frame.yPt
176
+ },
177
+ to: {
178
+ xPt: segment.to.x - frame.xPt,
179
+ yPt: segment.to.y - frame.yPt
180
+ }
181
+ })
182
+ }));
183
+ const sourceIndex = state.vectors.length;
184
+ return {
185
+ kind: "path",
186
+ frame,
187
+ subpaths: localSubpaths,
188
+ ...paint.fill !== void 0 ? { fill: paint.fill } : {},
189
+ ...fillRule !== void 0 ? { fillRule } : {},
190
+ ...paint.stroke !== void 0 ? { stroke: paint.stroke } : {},
191
+ paintOrder: state.paintOrder++,
192
+ sourcePath: `svg/vector[${sourceIndex}]`
193
+ };
194
+ }
195
+ function roundedRectSubpaths(x, y, width, height, rx, ry) {
196
+ const radiusX = Math.min(rx, width / 2);
197
+ const radiusY = Math.min(ry, height / 2);
198
+ const kx = radiusX * KAPPA;
199
+ const ky = radiusY * KAPPA;
200
+ return [{
201
+ start: {
202
+ x: x + radiusX,
203
+ y
204
+ },
205
+ closed: true,
206
+ segments: [
207
+ {
208
+ kind: "line",
209
+ to: {
210
+ x: x + width - radiusX,
211
+ y
212
+ }
213
+ },
214
+ {
215
+ kind: "cubic",
216
+ control1: {
217
+ x: x + width - radiusX + kx,
218
+ y
219
+ },
220
+ control2: {
221
+ x: x + width,
222
+ y: y + radiusY - ky
223
+ },
224
+ to: {
225
+ x: x + width,
226
+ y: y + radiusY
227
+ }
228
+ },
229
+ {
230
+ kind: "line",
231
+ to: {
232
+ x: x + width,
233
+ y: y + height - radiusY
234
+ }
235
+ },
236
+ {
237
+ kind: "cubic",
238
+ control1: {
239
+ x: x + width,
240
+ y: y + height - radiusY + ky
241
+ },
242
+ control2: {
243
+ x: x + width - radiusX + kx,
244
+ y: y + height
245
+ },
246
+ to: {
247
+ x: x + width - radiusX,
248
+ y: y + height
249
+ }
250
+ },
251
+ {
252
+ kind: "line",
253
+ to: {
254
+ x: x + radiusX,
255
+ y: y + height
256
+ }
257
+ },
258
+ {
259
+ kind: "cubic",
260
+ control1: {
261
+ x: x + radiusX - kx,
262
+ y: y + height
263
+ },
264
+ control2: {
265
+ x,
266
+ y: y + height - radiusY + ky
267
+ },
268
+ to: {
269
+ x,
270
+ y: y + height - radiusY
271
+ }
272
+ },
273
+ {
274
+ kind: "line",
275
+ to: {
276
+ x,
277
+ y: y + radiusY
278
+ }
279
+ },
280
+ {
281
+ kind: "cubic",
282
+ control1: {
283
+ x,
284
+ y: y + radiusY - ky
285
+ },
286
+ control2: {
287
+ x: x + radiusX - kx,
288
+ y
289
+ },
290
+ to: {
291
+ x: x + radiusX,
292
+ y
293
+ }
294
+ }
295
+ ]
296
+ }];
297
+ }
298
+ function ellipseSubpaths(cx, cy, rx, ry) {
299
+ const kx = rx * KAPPA;
300
+ const ky = ry * KAPPA;
301
+ return [{
302
+ start: {
303
+ x: cx + rx,
304
+ y: cy
305
+ },
306
+ closed: true,
307
+ segments: [
308
+ {
309
+ kind: "cubic",
310
+ control1: {
311
+ x: cx + rx,
312
+ y: cy + ky
313
+ },
314
+ control2: {
315
+ x: cx + kx,
316
+ y: cy + ry
317
+ },
318
+ to: {
319
+ x: cx,
320
+ y: cy + ry
321
+ }
322
+ },
323
+ {
324
+ kind: "cubic",
325
+ control1: {
326
+ x: cx - kx,
327
+ y: cy + ry
328
+ },
329
+ control2: {
330
+ x: cx - rx,
331
+ y: cy + ky
332
+ },
333
+ to: {
334
+ x: cx - rx,
335
+ y: cy
336
+ }
337
+ },
338
+ {
339
+ kind: "cubic",
340
+ control1: {
341
+ x: cx - rx,
342
+ y: cy - ky
343
+ },
344
+ control2: {
345
+ x: cx - kx,
346
+ y: cy - ry
347
+ },
348
+ to: {
349
+ x: cx,
350
+ y: cy - ry
351
+ }
352
+ },
353
+ {
354
+ kind: "cubic",
355
+ control1: {
356
+ x: cx + kx,
357
+ y: cy - ry
358
+ },
359
+ control2: {
360
+ x: cx + rx,
361
+ y: cy - ky
362
+ },
363
+ to: {
364
+ x: cx + rx,
365
+ y: cy
366
+ }
367
+ }
368
+ ]
369
+ }];
370
+ }
371
+ function userUnits(element, attr) {
372
+ return parseSvgUserUnits(findAttribute(element, attr)) ?? 0;
373
+ }
374
+ function readShape(state, element, ctm, paint) {
375
+ const name = localName(element.tag);
376
+ const id = findAttribute(element, "id");
377
+ const detail = id === void 0 ? name : `${name}#${id}`;
378
+ let fillRule;
379
+ const fillRuleSpec = paint.fillRuleSpec;
380
+ if (fillRuleSpec !== void 0 && fillRuleSpec !== "nonzero") if (fillRuleSpec === "evenodd") fillRule = "evenodd";
381
+ else report(state, "svg/paint-unsupported", fillRuleSpec);
382
+ const resolved = resolvePaint(state, paint, ctm);
383
+ if (resolved.fill === void 0 && resolved.stroke === void 0) {
384
+ report(state, "svg/element-skipped", `${detail}: nothing painted (fill and stroke both absent or none)`);
385
+ return;
386
+ }
387
+ if (name === "rect") {
388
+ const x = userUnits(element, "x");
389
+ const y = userUnits(element, "y");
390
+ const width = userUnits(element, "width");
391
+ const height = userUnits(element, "height");
392
+ if (width <= 0 || height <= 0) {
393
+ report(state, "svg/element-skipped", `${detail}: zero or negative size`);
394
+ return;
395
+ }
396
+ const rxAttr = parseSvgUserUnits(findAttribute(element, "rx"));
397
+ const ryAttr = parseSvgUserUnits(findAttribute(element, "ry"));
398
+ const rx = rxAttr ?? ryAttr ?? 0;
399
+ const ry = ryAttr ?? rxAttr ?? 0;
400
+ if (rx > 0 || ry > 0) {
401
+ const vector = buildPathVector(state, roundedRectSubpaths(x, y, width, height, rx, ry), ctm, resolved, fillRule);
402
+ if (vector !== void 0) state.vectors.push(vector);
403
+ return;
404
+ }
405
+ const rotated = !isAxisAligned(ctm) && isNonReflectingSimilarity(ctm);
406
+ const frame = rotated ? similarityFrame(ctm, x, y, width, height) : axisAlignedFrame(ctm, x, y, width, height);
407
+ if (frame.widthPt <= 0 || frame.heightPt <= 0) {
408
+ report(state, "svg/element-skipped", `${detail}: collapses to zero size under transform`);
409
+ return;
410
+ }
411
+ state.vectors.push({
412
+ kind: "rect",
413
+ frame,
414
+ ...rotated ? { rotationDeg: similarityRotationDeg(ctm) } : {},
415
+ ...resolved.fill !== void 0 ? { fill: resolved.fill } : {},
416
+ ...resolved.stroke !== void 0 ? { stroke: resolved.stroke } : {},
417
+ paintOrder: state.paintOrder++,
418
+ sourcePath: `svg/vector[${state.vectors.length}]`
419
+ });
420
+ return;
421
+ }
422
+ if (name === "circle" || name === "ellipse") {
423
+ const cx = userUnits(element, "cx");
424
+ const cy = userUnits(element, "cy");
425
+ const rx = name === "circle" ? userUnits(element, "r") : userUnits(element, "rx");
426
+ const ry = name === "circle" ? userUnits(element, "r") : userUnits(element, "ry");
427
+ if (rx <= 0 || ry <= 0) {
428
+ report(state, "svg/element-skipped", `${detail}: zero or negative radius`);
429
+ return;
430
+ }
431
+ if (!isAxisAligned(ctm) && !isNonReflectingSimilarity(ctm)) {
432
+ const vector = buildPathVector(state, ellipseSubpaths(cx, cy, rx, ry), ctm, resolved, fillRule);
433
+ if (vector !== void 0) state.vectors.push(vector);
434
+ return;
435
+ }
436
+ const rotated = !isAxisAligned(ctm);
437
+ const frame = rotated ? similarityFrame(ctm, cx - rx, cy - ry, rx * 2, ry * 2) : axisAlignedFrame(ctm, cx - rx, cy - ry, rx * 2, ry * 2);
438
+ if (frame.widthPt <= 0 || frame.heightPt <= 0) {
439
+ report(state, "svg/element-skipped", `${detail}: collapses to zero size under transform`);
440
+ return;
441
+ }
442
+ state.vectors.push({
443
+ kind: "ellipse",
444
+ frame,
445
+ ...rotated ? { rotationDeg: similarityRotationDeg(ctm) } : {},
446
+ ...resolved.fill !== void 0 ? { fill: resolved.fill } : {},
447
+ ...resolved.stroke !== void 0 ? { stroke: resolved.stroke } : {},
448
+ paintOrder: state.paintOrder++,
449
+ sourcePath: `svg/vector[${state.vectors.length}]`
450
+ });
451
+ return;
452
+ }
453
+ if (name === "line") {
454
+ if (resolved.stroke === void 0) {
455
+ report(state, "svg/element-skipped", `${detail}: a line paints only through its stroke, which is absent or none`);
456
+ return;
457
+ }
458
+ const from = applyMatrix(ctm, userUnits(element, "x1"), userUnits(element, "y1"));
459
+ const to = applyMatrix(ctm, userUnits(element, "x2"), userUnits(element, "y2"));
460
+ if (from.x === to.x && from.y === to.y) {
461
+ report(state, "svg/element-skipped", `${detail}: zero-length line`);
462
+ return;
463
+ }
464
+ state.vectors.push({
465
+ kind: "line",
466
+ from: {
467
+ xPt: from.x,
468
+ yPt: from.y
469
+ },
470
+ to: {
471
+ xPt: to.x,
472
+ yPt: to.y
473
+ },
474
+ stroke: resolved.stroke,
475
+ paintOrder: state.paintOrder++,
476
+ sourcePath: `svg/vector[${state.vectors.length}]`
477
+ });
478
+ return;
479
+ }
480
+ if (name === "polyline" || name === "polygon") {
481
+ const pointsRaw = findAttribute(element, "points");
482
+ if (pointsRaw === void 0) {
483
+ report(state, "svg/element-skipped", `${detail}: no points attribute`);
484
+ return;
485
+ }
486
+ const numbers = pointsRaw.trim().split(/[\s,]+/).filter((part) => part !== "").map(Number);
487
+ if (numbers.length === 0 || numbers.length % 2 !== 0 || !numbers.every((value) => Number.isFinite(value))) {
488
+ report(state, "svg/element-unsupported", `${detail}: malformed points attribute`);
489
+ return;
490
+ }
491
+ if (numbers.length < 4) {
492
+ report(state, "svg/element-skipped", `${detail}: fewer than two points`);
493
+ return;
494
+ }
495
+ const segments = [];
496
+ for (let i = 2; i < numbers.length; i += 2) segments.push({
497
+ kind: "line",
498
+ to: {
499
+ x: numbers[i],
500
+ y: numbers[i + 1]
501
+ }
502
+ });
503
+ const vector = buildPathVector(state, [{
504
+ start: {
505
+ x: numbers[0],
506
+ y: numbers[1]
507
+ },
508
+ closed: name === "polygon",
509
+ segments
510
+ }], ctm, resolved, fillRule);
511
+ if (vector === void 0) report(state, "svg/element-skipped", `${detail}: all points coincide`);
512
+ else state.vectors.push(vector);
513
+ return;
514
+ }
515
+ if (name === "path") {
516
+ const d = findAttribute(element, "d");
517
+ if (d === void 0 || d.trim() === "") {
518
+ report(state, "svg/element-skipped", `${detail}: no d attribute`);
519
+ return;
520
+ }
521
+ const parsed = parseSvgPathData(d);
522
+ if (parsed === void 0 || parsed.length === 0) {
523
+ report(state, "svg/element-unsupported", `${detail}: malformed or empty path data`);
524
+ return;
525
+ }
526
+ const vector = buildPathVector(state, parsed, ctm, resolved, fillRule);
527
+ if (vector === void 0) report(state, "svg/element-skipped", `${detail}: path collapses to a single point`);
528
+ else state.vectors.push(vector);
529
+ return;
530
+ }
531
+ }
532
+ const NON_RENDERING_ELEMENTS = /* @__PURE__ */ new Set([
533
+ "defs",
534
+ "title",
535
+ "desc",
536
+ "metadata",
537
+ "linearGradient",
538
+ "radialGradient",
539
+ "pattern",
540
+ "clipPath",
541
+ "mask",
542
+ "marker",
543
+ "symbol",
544
+ "script"
545
+ ]);
546
+ function walkElement(state, element, ctm, paint) {
547
+ const name = localName(element.tag);
548
+ const id = findAttribute(element, "id");
549
+ const detail = id === void 0 ? name : `${name}#${id}`;
550
+ if (findAttribute(element, "style") !== void 0) report(state, "svg/css-style-ignored", detail);
551
+ for (const opacityAttr of [
552
+ "opacity",
553
+ "fill-opacity",
554
+ "stroke-opacity"
555
+ ]) {
556
+ const raw = findAttribute(element, opacityAttr);
557
+ if (raw !== void 0) {
558
+ const value = Number(raw);
559
+ if (Number.isFinite(value) && value < 1) report(state, "svg/opacity-ignored", `${detail}: ${opacityAttr}=${raw}`);
560
+ }
561
+ }
562
+ if (name === "g" || name === "a") {
563
+ const own = parseSvgTransform(findAttribute(element, "transform"));
564
+ walkChildren(state, element, own === void 0 ? ctm : composeMatrices(ctm, own), childPaintState(element, paint));
565
+ return;
566
+ }
567
+ if (NON_RENDERING_ELEMENTS.has(name)) return;
568
+ if (name === "style") {
569
+ report(state, "svg/css-style-ignored", detail);
570
+ return;
571
+ }
572
+ if (name === "text" || name === "tspan" || name === "textPath" || name === "tref") {
573
+ report(state, "svg/text-unsupported", detail);
574
+ return;
575
+ }
576
+ if (name === "image") {
577
+ report(state, "svg/image-unsupported", detail);
578
+ return;
579
+ }
580
+ if (name === "use") {
581
+ report(state, "svg/use-unsupported", detail);
582
+ return;
583
+ }
584
+ if (name === "rect" || name === "circle" || name === "ellipse" || name === "line" || name === "polyline" || name === "polygon" || name === "path") {
585
+ const own = parseSvgTransform(findAttribute(element, "transform"));
586
+ readShape(state, element, own === void 0 ? ctm : composeMatrices(ctm, own), childPaintState(element, paint));
587
+ return;
588
+ }
589
+ report(state, "svg/element-unsupported", detail);
590
+ }
591
+ function walkChildren(state, element, ctm, paint) {
592
+ for (const child of elementChildren(element)) walkElement(state, child, ctm, paint);
593
+ }
594
+ function resolveRootGeometry(root, state) {
595
+ const attrWidth = parseSvgLengthPt(findAttribute(root, "width"));
596
+ const attrHeight = parseSvgLengthPt(findAttribute(root, "height"));
597
+ const parsedViewBox = parseSvgViewBox(findAttribute(root, "viewBox"));
598
+ const viewBox = parsedViewBox !== void 0 && parsedViewBox.width > 0 && parsedViewBox.height > 0 ? parsedViewBox : void 0;
599
+ let widthPt = attrWidth !== void 0 && attrWidth > 0 ? attrWidth : void 0;
600
+ let heightPt = attrHeight !== void 0 && attrHeight > 0 ? attrHeight : void 0;
601
+ if (widthPt === void 0 !== (heightPt === void 0)) {
602
+ widthPt = void 0;
603
+ heightPt = void 0;
604
+ }
605
+ if (widthPt === void 0 || heightPt === void 0) if (viewBox !== void 0) {
606
+ widthPt = viewBox.width;
607
+ heightPt = viewBox.height;
608
+ } else {
609
+ widthPt = DEFAULT_WIDTH_PT;
610
+ heightPt = DEFAULT_HEIGHT_PT;
611
+ report(state, "svg/default-size-assumed", "neither width/height nor a usable viewBox was present; assuming the CSS default replaced-element size of 300x150 px");
612
+ }
613
+ if (viewBox === void 0) return {
614
+ widthPt,
615
+ heightPt,
616
+ map: {
617
+ a: .75,
618
+ b: 0,
619
+ c: 0,
620
+ d: .75,
621
+ e: 0,
622
+ f: 0
623
+ }
624
+ };
625
+ const preserveAspectRatio = findAttribute(root, "preserveAspectRatio") ?? "xMidYMid meet";
626
+ if (preserveAspectRatio.trim() !== "none") {
627
+ const viewBoxAspect = viewBox.width / viewBox.height;
628
+ const pageAspect = widthPt / heightPt;
629
+ if (Math.abs(viewBoxAspect - pageAspect) > 1e-6 * Math.max(viewBoxAspect, pageAspect)) report(state, "svg/preserve-aspect-ratio-stretched", `viewBox aspect ${viewBoxAspect.toFixed(4)} stretched onto page aspect ${pageAspect.toFixed(4)} under preserveAspectRatio="${preserveAspectRatio.trim()}" (letterboxing is out of scope)`);
630
+ }
631
+ const sx = widthPt / viewBox.width;
632
+ const sy = heightPt / viewBox.height;
633
+ return {
634
+ widthPt,
635
+ heightPt,
636
+ map: {
637
+ a: sx,
638
+ b: 0,
639
+ c: 0,
640
+ d: sy,
641
+ e: -viewBox.minX * sx,
642
+ f: -viewBox.minY * sy
643
+ }
644
+ };
645
+ }
646
+ function readSvgContent(text, options) {
647
+ const root = parseXml(text).find((node) => node.type === "element" && localName(node.tag) === "svg");
648
+ if (root === void 0) throw new SvgMissingRootElementError();
649
+ const state = {
650
+ sink: options?.onSvgDiagnostic,
651
+ vectors: [],
652
+ paintOrder: 0
653
+ };
654
+ const rootGeometry = resolveRootGeometry(root, state);
655
+ const titleElement = elementChildren(root).find((child) => localName(child.tag) === "title");
656
+ const title = titleElement === void 0 ? void 0 : textOf(titleElement).trim();
657
+ const metadata = title === void 0 || title === "" ? {} : { title };
658
+ walkChildren(state, root, rootGeometry.map, childPaintState(root, {}));
659
+ const page = {
660
+ size: {
661
+ widthPt: rootGeometry.widthPt,
662
+ heightPt: rootGeometry.heightPt
663
+ },
664
+ shapes: [],
665
+ vectors: state.vectors
666
+ };
667
+ return {
668
+ kind: "drawing",
669
+ formatVersion: CONTENT_FORMAT_VERSION,
670
+ metadata,
671
+ pages: [page]
672
+ };
673
+ }
674
+ //#endregion
675
+ export { SvgMissingRootElementError, readSvgContent };
@@ -0,0 +1,22 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/svg/text.ts
3
+ var SvgInvalidUtf8Error = class extends Error {
4
+ constructor() {
5
+ super("svg text must be well-formed UTF-8");
6
+ this.name = "SvgInvalidUtf8Error";
7
+ }
8
+ };
9
+ function decodeSvgText(bytes) {
10
+ try {
11
+ return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
12
+ } catch {
13
+ throw new SvgInvalidUtf8Error();
14
+ }
15
+ }
16
+ function encodeSvgText(text) {
17
+ return new TextEncoder().encode(text);
18
+ }
19
+ //#endregion
20
+ exports.SvgInvalidUtf8Error = SvgInvalidUtf8Error;
21
+ exports.decodeSvgText = decodeSvgText;
22
+ exports.encodeSvgText = encodeSvgText;
@@ -0,0 +1,8 @@
1
+ //#region src/svg/text.d.ts
2
+ declare class SvgInvalidUtf8Error extends Error {
3
+ constructor();
4
+ }
5
+ declare function decodeSvgText(bytes: Uint8Array): string;
6
+ declare function encodeSvgText(text: string): Uint8Array<ArrayBuffer>;
7
+ //#endregion
8
+ export { SvgInvalidUtf8Error, decodeSvgText, encodeSvgText };
@@ -0,0 +1,8 @@
1
+ //#region src/svg/text.d.ts
2
+ declare class SvgInvalidUtf8Error extends Error {
3
+ constructor();
4
+ }
5
+ declare function decodeSvgText(bytes: Uint8Array): string;
6
+ declare function encodeSvgText(text: string): Uint8Array<ArrayBuffer>;
7
+ //#endregion
8
+ export { SvgInvalidUtf8Error, decodeSvgText, encodeSvgText };