@bitbybit-dev/base 1.6.1 → 1.7.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 (192) hide show
  1. package/lib/api/GlobalCDNProvider.js +1 -1
  2. package/lib/api/inputs/base-inputs.d.ts +36 -7
  3. package/lib/api/inputs/base-inputs.js +10 -0
  4. package/lib/api/inputs/color-inputs.d.ts +54 -1
  5. package/lib/api/inputs/color-inputs.js +73 -1
  6. package/lib/api/inputs/frame-inputs.d.ts +2 -1
  7. package/lib/api/inputs/frame-inputs.js +2 -1
  8. package/lib/api/inputs/io-inputs.d.ts +527 -59
  9. package/lib/api/inputs/io-inputs.js +432 -40
  10. package/lib/api/inputs/line-inputs.d.ts +17 -0
  11. package/lib/api/inputs/line-inputs.js +15 -0
  12. package/lib/api/inputs/math-inputs.d.ts +62 -0
  13. package/lib/api/inputs/math-inputs.js +82 -0
  14. package/lib/api/inputs/mesh-inputs.d.ts +16 -0
  15. package/lib/api/inputs/mesh-inputs.js +14 -0
  16. package/lib/api/inputs/transforms-inputs.d.ts +76 -0
  17. package/lib/api/inputs/transforms-inputs.js +85 -0
  18. package/lib/api/kernel-calls/index.d.ts +1 -0
  19. package/lib/api/kernel-calls/index.js +1 -0
  20. package/lib/api/kernel-calls/settings.d.ts +82 -0
  21. package/lib/api/kernel-calls/settings.js +123 -0
  22. package/lib/api/recipes/surface-mesh.d.ts +2 -1
  23. package/lib/api/recipes/surface-mesh.js +5 -3
  24. package/lib/api/resolved-inputs/index.d.ts +32 -2
  25. package/lib/api/services/base-bitbybit.d.ts +12 -0
  26. package/lib/api/services/base-bitbybit.js +19 -3
  27. package/lib/api/services/color.d.ts +36 -2
  28. package/lib/api/services/color.js +60 -11
  29. package/lib/api/services/helpers/color/color-forms.constants.d.ts +10 -0
  30. package/lib/api/services/helpers/color/color-forms.constants.js +10 -0
  31. package/lib/api/services/helpers/color/color-forms.d.ts +3 -0
  32. package/lib/api/services/helpers/color/color-forms.js +27 -0
  33. package/lib/api/services/helpers/color/css-colors.constants.d.ts +9 -0
  34. package/lib/api/services/helpers/color/css-colors.constants.js +158 -0
  35. package/lib/api/services/helpers/color/css-colors.d.ts +1 -0
  36. package/lib/api/services/helpers/color/css-colors.js +61 -0
  37. package/lib/api/services/helpers/curves/bezier-pieces.constants.d.ts +5 -0
  38. package/lib/api/services/helpers/curves/bezier-pieces.constants.js +5 -0
  39. package/lib/api/services/helpers/curves/bezier-pieces.d.ts +24 -0
  40. package/lib/api/services/helpers/curves/bezier-pieces.js +149 -0
  41. package/lib/api/services/helpers/curves/conic-arcs.constants.d.ts +5 -0
  42. package/lib/api/services/helpers/curves/conic-arcs.constants.js +5 -0
  43. package/lib/api/services/helpers/curves/conic-arcs.d.ts +24 -0
  44. package/lib/api/services/helpers/curves/conic-arcs.js +85 -0
  45. package/lib/api/services/helpers/curves/nurbs-forms.d.ts +20 -0
  46. package/lib/api/services/helpers/curves/nurbs-forms.js +112 -0
  47. package/lib/api/services/helpers/curves/nurbs.d.ts +4 -0
  48. package/lib/api/services/helpers/curves/nurbs.js +10 -9
  49. package/lib/api/services/helpers/files/drawing/drawing-budget.d.ts +3 -0
  50. package/lib/api/services/helpers/files/drawing/drawing-budget.js +24 -0
  51. package/lib/api/services/helpers/files/drawing/drawing-geometry.d.ts +18 -0
  52. package/lib/api/services/helpers/files/drawing/drawing-geometry.js +84 -0
  53. package/lib/api/services/helpers/files/drawing/drawing-model.d.ts +5 -0
  54. package/lib/api/services/helpers/files/drawing/drawing-model.js +252 -0
  55. package/lib/api/services/helpers/files/drawing/drawing-types.d.ts +60 -0
  56. package/lib/api/services/helpers/files/drawing/drawing-types.js +1 -0
  57. package/lib/api/services/helpers/files/drawing/drawing.constants.d.ts +28 -0
  58. package/lib/api/services/helpers/files/drawing/drawing.constants.js +29 -0
  59. package/lib/api/services/helpers/files/dxf/dxf-colors.d.ts +5 -0
  60. package/lib/api/services/helpers/files/dxf/dxf-colors.js +45 -0
  61. package/lib/api/services/helpers/files/dxf/dxf-entities.d.ts +17 -0
  62. package/lib/api/services/helpers/files/dxf/dxf-entities.js +193 -0
  63. package/lib/api/services/helpers/files/dxf/dxf-file-text.d.ts +1 -0
  64. package/lib/api/services/helpers/files/dxf/dxf-file-text.js +50 -0
  65. package/lib/api/services/helpers/files/dxf/dxf-generator.d.ts +2 -36
  66. package/lib/api/services/helpers/files/dxf/dxf-generator.js +81 -523
  67. package/lib/api/services/helpers/files/dxf/dxf-reader-dtos.d.ts +6 -0
  68. package/lib/api/services/helpers/files/dxf/dxf-reader-dtos.js +81 -0
  69. package/lib/api/services/helpers/files/dxf/dxf-reader-geometry.d.ts +21 -0
  70. package/lib/api/services/helpers/files/dxf/dxf-reader-geometry.js +67 -0
  71. package/lib/api/services/helpers/files/dxf/dxf-reader-types.d.ts +69 -0
  72. package/lib/api/services/helpers/files/dxf/dxf-reader-types.js +1 -0
  73. package/lib/api/services/helpers/files/dxf/dxf-reader.constants.d.ts +71 -0
  74. package/lib/api/services/helpers/files/dxf/dxf-reader.constants.js +78 -0
  75. package/lib/api/services/helpers/files/dxf/dxf-reader.d.ts +2 -0
  76. package/lib/api/services/helpers/files/dxf/dxf-reader.js +556 -0
  77. package/lib/api/services/helpers/files/dxf/dxf-tables.d.ts +27 -0
  78. package/lib/api/services/helpers/files/dxf/dxf-tables.js +247 -0
  79. package/lib/api/services/helpers/files/dxf/dxf-text.d.ts +26 -0
  80. package/lib/api/services/helpers/files/dxf/dxf-text.js +97 -0
  81. package/lib/api/services/helpers/files/dxf/dxf-tokens.d.ts +9 -0
  82. package/lib/api/services/helpers/files/dxf/dxf-tokens.js +65 -0
  83. package/lib/api/services/helpers/files/dxf/dxf.constants.d.ts +32 -1
  84. package/lib/api/services/helpers/files/dxf/dxf.constants.js +34 -1
  85. package/lib/api/services/helpers/files/file-bytes.d.ts +1 -0
  86. package/lib/api/services/helpers/files/file-bytes.js +15 -0
  87. package/lib/api/services/helpers/files/ground-axes.d.ts +2 -0
  88. package/lib/api/services/helpers/files/ground-axes.js +4 -0
  89. package/lib/api/services/helpers/files/mesh-file-axes.d.ts +7 -0
  90. package/lib/api/services/helpers/files/mesh-file-axes.js +17 -0
  91. package/lib/api/services/helpers/files/stl-axes.d.ts +4 -0
  92. package/lib/api/services/helpers/files/stl-axes.js +22 -0
  93. package/lib/api/services/helpers/files/stl-types.d.ts +7 -0
  94. package/lib/api/services/helpers/files/stl-types.js +1 -0
  95. package/lib/api/services/helpers/files/stl.constants.d.ts +11 -0
  96. package/lib/api/services/helpers/files/stl.constants.js +11 -0
  97. package/lib/api/services/helpers/files/stl.d.ts +3 -0
  98. package/lib/api/services/helpers/files/stl.js +93 -0
  99. package/lib/api/services/helpers/files/svg/svg-css.d.ts +48 -0
  100. package/lib/api/services/helpers/files/svg/svg-css.js +444 -0
  101. package/lib/api/services/helpers/files/svg/svg-dash-patterns.d.ts +2 -0
  102. package/lib/api/services/helpers/files/svg/svg-dash-patterns.js +35 -0
  103. package/lib/api/services/helpers/files/svg/svg-document.d.ts +26 -0
  104. package/lib/api/services/helpers/files/svg/svg-document.js +89 -0
  105. package/lib/api/services/helpers/files/svg/svg-memo.d.ts +1 -0
  106. package/lib/api/services/helpers/files/svg/svg-memo.js +8 -0
  107. package/lib/api/services/helpers/files/svg/svg-normalizer.d.ts +6 -0
  108. package/lib/api/services/helpers/files/svg/svg-normalizer.js +433 -0
  109. package/lib/api/services/helpers/files/svg/svg-numbers.d.ts +15 -0
  110. package/lib/api/services/helpers/files/svg/svg-numbers.js +61 -0
  111. package/lib/api/services/helpers/files/svg/svg-path-data.constants.d.ts +8 -0
  112. package/lib/api/services/helpers/files/svg/svg-path-data.constants.js +10 -0
  113. package/lib/api/services/helpers/files/svg/svg-path-data.d.ts +9 -0
  114. package/lib/api/services/helpers/files/svg/svg-path-data.js +203 -0
  115. package/lib/api/services/helpers/files/svg/svg-path-types.d.ts +33 -0
  116. package/lib/api/services/helpers/files/svg/svg-path-types.js +1 -0
  117. package/lib/api/services/helpers/files/svg/svg-reader.constants.d.ts +35 -0
  118. package/lib/api/services/helpers/files/svg/svg-reader.constants.js +35 -0
  119. package/lib/api/services/helpers/files/svg/svg-reading.constants.d.ts +9 -0
  120. package/lib/api/services/helpers/files/svg/svg-reading.constants.js +9 -0
  121. package/lib/api/services/helpers/files/svg/svg-reading.d.ts +6 -0
  122. package/lib/api/services/helpers/files/svg/svg-reading.js +233 -0
  123. package/lib/api/services/helpers/files/svg/svg-scene-types.d.ts +54 -0
  124. package/lib/api/services/helpers/files/svg/svg-scene-types.js +1 -0
  125. package/lib/api/services/helpers/files/svg/svg-shapes.d.ts +6 -0
  126. package/lib/api/services/helpers/files/svg/svg-shapes.js +102 -0
  127. package/lib/api/services/helpers/files/svg/svg-strokes.d.ts +38 -0
  128. package/lib/api/services/helpers/files/svg/svg-strokes.js +108 -0
  129. package/lib/api/services/helpers/files/svg/svg-style.d.ts +26 -0
  130. package/lib/api/services/helpers/files/svg/svg-style.js +211 -0
  131. package/lib/api/services/helpers/files/svg/svg-transform.d.ts +10 -0
  132. package/lib/api/services/helpers/files/svg/svg-transform.js +76 -0
  133. package/lib/api/services/helpers/files/svg/svg-writer.d.ts +16 -0
  134. package/lib/api/services/helpers/files/svg/svg-writer.js +153 -0
  135. package/lib/api/services/helpers/files/svg/svg-xml.d.ts +11 -0
  136. package/lib/api/services/helpers/files/svg/svg-xml.js +322 -0
  137. package/lib/api/services/helpers/files/svg/svg.constants.d.ts +13 -0
  138. package/lib/api/services/helpers/files/svg/svg.constants.js +13 -0
  139. package/lib/api/services/helpers/files/work-budget.d.ts +11 -0
  140. package/lib/api/services/helpers/files/work-budget.js +26 -0
  141. package/lib/api/services/helpers/geometry/affine2.d.ts +8 -0
  142. package/lib/api/services/helpers/geometry/affine2.js +18 -0
  143. package/lib/api/services/helpers/geometry/ellipse-arcs.d.ts +14 -0
  144. package/lib/api/services/helpers/geometry/ellipse-arcs.js +96 -0
  145. package/lib/api/services/helpers/geometry/frame-axes.d.ts +3 -1
  146. package/lib/api/services/helpers/geometry/polygon-nesting.constants.d.ts +6 -0
  147. package/lib/api/services/helpers/geometry/polygon-nesting.constants.js +6 -0
  148. package/lib/api/services/helpers/geometry/polygon-nesting.d.ts +31 -0
  149. package/lib/api/services/helpers/geometry/polygon-nesting.js +251 -0
  150. package/lib/api/services/helpers/geometry/quaternions.constants.d.ts +3 -0
  151. package/lib/api/services/helpers/geometry/quaternions.constants.js +3 -0
  152. package/lib/api/services/helpers/geometry/quaternions.d.ts +14 -0
  153. package/lib/api/services/helpers/geometry/quaternions.js +71 -0
  154. package/lib/api/services/helpers/geometry/scalars.constants.d.ts +1 -0
  155. package/lib/api/services/helpers/geometry/scalars.constants.js +1 -0
  156. package/lib/api/services/helpers/geometry/scalars.d.ts +3 -0
  157. package/lib/api/services/helpers/geometry/scalars.js +29 -1
  158. package/lib/api/services/helpers/geometry/vectors.d.ts +1 -0
  159. package/lib/api/services/helpers/geometry/vectors.js +6 -0
  160. package/lib/api/services/helpers/mesh/mesh-proximity.constants.d.ts +1 -0
  161. package/lib/api/services/helpers/mesh/mesh-proximity.constants.js +1 -0
  162. package/lib/api/services/helpers/mesh/mesh-proximity.d.ts +1 -0
  163. package/lib/api/services/helpers/mesh/mesh-proximity.js +40 -40
  164. package/lib/api/services/io/drawing.d.ts +170 -0
  165. package/lib/api/services/io/drawing.js +212 -0
  166. package/lib/api/services/io/dxf.d.ts +134 -0
  167. package/lib/api/services/io/dxf.js +167 -0
  168. package/lib/api/services/io/index.d.ts +4 -0
  169. package/lib/api/services/io/index.js +4 -0
  170. package/lib/api/services/io/io.d.ts +14 -0
  171. package/lib/api/services/io/io.js +15 -0
  172. package/lib/api/services/io/svg.d.ts +64 -0
  173. package/lib/api/services/io/svg.js +102 -0
  174. package/lib/api/services/line.d.ts +17 -0
  175. package/lib/api/services/line.js +20 -1
  176. package/lib/api/services/math.d.ts +35 -0
  177. package/lib/api/services/math.js +56 -1
  178. package/lib/api/services/mesh.d.ts +17 -0
  179. package/lib/api/services/mesh.js +27 -1
  180. package/lib/api/services/point.d.ts +4 -1
  181. package/lib/api/services/point.js +9 -4
  182. package/lib/api/services/recipes.d.ts +3 -0
  183. package/lib/api/services/recipes.js +4 -1
  184. package/lib/api/services/text.d.ts +4 -1
  185. package/lib/api/services/text.js +8 -3
  186. package/lib/api/services/transforms.d.ts +119 -1
  187. package/lib/api/services/transforms.js +158 -30
  188. package/package.json +1 -1
  189. package/lib/api/services/helpers/files/dxf/dxf.d.ts +0 -87
  190. package/lib/api/services/helpers/files/dxf/dxf.js +0 -111
  191. package/lib/api/services/io.d.ts +0 -9
  192. package/lib/api/services/io.js +0 -10
@@ -0,0 +1,153 @@
1
+ import * as Inputs from "../../../../inputs";
2
+ import { InputError } from "../../../../kernel-calls";
3
+ import { degreesOf } from "../../geometry/scalars";
4
+ import { distance2 } from "../../geometry/vectors";
5
+ import { drawingBounds, drawingSize, patternUnitOf } from "../drawing/drawing-geometry";
6
+ import { writerBudgetOf } from "../drawing/drawing-budget";
7
+ import { DEFAULT_LAYER, LEAST_TOLERANCE_SHARE, LINE_TYPE_PATTERNS } from "../drawing/drawing.constants";
8
+ import { reversedStroke, strokeEnd, strokeOf } from "./svg-strokes";
9
+ import { ID_UNSAFE, LEAST_VIEW_SPAN, MARGIN_SHARE, PATH_COLOR, STROKE_SHARE, SVG_DECIMALS, SVG_JOIN_DISTANCE, SVG_UNITS, TRAILING_FRACTION_ZEROS } from "./svg.constants";
10
+ export const svgNumber = (value) => {
11
+ const text = value.toFixed(SVG_DECIMALS).replace(TRAILING_FRACTION_ZEROS, "$1");
12
+ return text === "-0" ? "0" : text;
13
+ };
14
+ const tooLarge = (writing) => new InputError(`\`${writing.property}\` is too large to write as SVG: it takes more than ${writing.budget.capacity} steps, each character written and each control point blended counted, the most a drawing of its size may take.`, writing.property);
15
+ const charged = (writing, text, chargedBefore = 0) => {
16
+ if (!writing.budget.spend(text.length - chargedBefore + 1)) {
17
+ throw tooLarge(writing);
18
+ }
19
+ return text;
20
+ };
21
+ const joined = (writing, texts, separator) => {
22
+ try {
23
+ return texts.join(separator);
24
+ }
25
+ catch (_a) {
26
+ throw tooLarge(writing);
27
+ }
28
+ };
29
+ const pointText = (point, writing) => `${svgNumber(point[0])} ${svgNumber(writing.flipY ? -point[1] : point[1])}`;
30
+ const moveText = (move, writing) => {
31
+ switch (move.kind) {
32
+ case "L":
33
+ return `L${pointText(move.to, writing)}`;
34
+ case "Q":
35
+ return `Q${pointText(move.control, writing)} ${pointText(move.to, writing)}`;
36
+ case "C":
37
+ return `C${pointText(move.control1, writing)} ${pointText(move.control2, writing)} ${pointText(move.to, writing)}`;
38
+ case "A": {
39
+ const rotation = degreesOf(writing.flipY ? -move.rotation : move.rotation);
40
+ const sweep = move.counterclockwise !== writing.flipY ? 1 : 0;
41
+ return `A${svgNumber(move.radiusX)} ${svgNumber(move.radiusY)} ${svgNumber(rotation)} ${move.large ? 1 : 0} ${sweep} ${pointText(move.to, writing)}`;
42
+ }
43
+ }
44
+ };
45
+ const near = (a, b) => a !== undefined && distance2(a, b) <= SVG_JOIN_DISTANCE;
46
+ const pathText = (path, writing) => {
47
+ const words = [];
48
+ let current;
49
+ let subpathStart;
50
+ for (const segment of path.segments) {
51
+ let stroke = strokeOf(segment, writing.tolerance, writing.budget);
52
+ if (writing.budget.exhausted) {
53
+ throw tooLarge(writing);
54
+ }
55
+ if (!near(current, stroke.start) && !stroke.closed && near(current, strokeEnd(stroke))) {
56
+ stroke = reversedStroke(stroke);
57
+ }
58
+ if (!near(current, stroke.start)) {
59
+ words.push(charged(writing, `M${pointText(stroke.start, writing)}`));
60
+ subpathStart = stroke.start;
61
+ }
62
+ for (const move of stroke.moves) {
63
+ words.push(charged(writing, moveText(move, writing)));
64
+ }
65
+ current = strokeEnd(stroke);
66
+ if (stroke.moves.length > 0 && near(subpathStart, current)) {
67
+ words.push(charged(writing, "Z"));
68
+ current = subpathStart;
69
+ }
70
+ }
71
+ return joined(writing, words, " ");
72
+ };
73
+ const boundedWriting = (parts, flipY, tolerance, property) => {
74
+ const budget = writerBudgetOf(parts);
75
+ const bounds = drawingBounds(parts, tolerance, budget);
76
+ const writing = { flipY, tolerance: Math.max(tolerance, LEAST_TOLERANCE_SHARE * drawingSize(bounds)), budget, property };
77
+ if (budget.exhausted) {
78
+ throw tooLarge(writing);
79
+ }
80
+ return [writing, bounds];
81
+ };
82
+ export const pathDataOf = (path, options) => {
83
+ const part = { layer: DEFAULT_LAYER, color: PATH_COLOR, lineType: Inputs.IO.lineTypeEnum.continuous, paths: [path] };
84
+ return pathText(path, boundedWriting([part], options.flipY, options.tolerance, options.property)[0]);
85
+ };
86
+ const escaped = (text) => text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
87
+ const viewSpanOf = (from, extent, margin, least) => {
88
+ const span = extent + 2 * margin;
89
+ return span >= least ? { from: from - margin, span } : { from: from - margin - (least - span) / 2, span: least };
90
+ };
91
+ const layersOf = (parts) => {
92
+ var _a;
93
+ const layers = new Map();
94
+ const taken = new Set();
95
+ const nextSuffix = new Map();
96
+ for (const part of parts) {
97
+ const key = part.layer.toUpperCase();
98
+ const known = layers.get(key);
99
+ if (known !== undefined) {
100
+ known.parts.push(part);
101
+ continue;
102
+ }
103
+ const base = `layer-${part.layer.replace(ID_UNSAFE, "-")}`;
104
+ let id = base;
105
+ if (taken.has(id)) {
106
+ let count = (_a = nextSuffix.get(base)) !== null && _a !== void 0 ? _a : 2;
107
+ while (taken.has(`${base}-${count}`)) {
108
+ count++;
109
+ }
110
+ id = `${base}-${count}`;
111
+ nextSuffix.set(base, count + 1);
112
+ }
113
+ taken.add(id);
114
+ layers.set(key, { id, label: part.layer, parts: [part] });
115
+ }
116
+ return [...layers.values()];
117
+ };
118
+ const dashText = (lineType, unit) => {
119
+ const elements = LINE_TYPE_PATTERNS[lineType].elements;
120
+ return elements.length === 0 ? "" : ` stroke-dasharray="${elements.map(element => svgNumber(Math.abs(element) * unit)).join(" ")}"`;
121
+ };
122
+ export const svgDocumentOf = (parts, options) => {
123
+ var _a, _b, _c, _d;
124
+ const [writing, bounds] = boundedWriting(parts, true, options.tolerance, options.property);
125
+ const size = drawingSize(bounds);
126
+ const margin = (_a = options.margin) !== null && _a !== void 0 ? _a : size * MARGIN_SHARE;
127
+ const strokeWidth = Math.max((_b = options.strokeWidth) !== null && _b !== void 0 ? _b : size * STROKE_SHARE, LEAST_VIEW_SPAN);
128
+ const across = viewSpanOf((_c = bounds === null || bounds === void 0 ? void 0 : bounds.min[0]) !== null && _c !== void 0 ? _c : 0, bounds === undefined ? 0 : bounds.max[0] - bounds.min[0], margin, strokeWidth);
129
+ const down = viewSpanOf(-((_d = bounds === null || bounds === void 0 ? void 0 : bounds.max[1]) !== null && _d !== void 0 ? _d : 0), bounds === undefined ? 0 : bounds.max[1] - bounds.min[1], margin, strokeWidth);
130
+ const unit = options.unit === undefined ? undefined : SVG_UNITS[options.unit];
131
+ const sized = unit === undefined ? "" : ` width="${svgNumber(across.span * unit[1])}${unit[0]}" height="${svgNumber(down.span * unit[1])}${unit[0]}"`;
132
+ const patternUnit = patternUnitOf(bounds);
133
+ const lines = [
134
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>",
135
+ `<svg xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" version="1.1"${sized} viewBox="${[across.from, down.from, across.span, down.span].map(svgNumber).join(" ")}">`,
136
+ ].map(line => charged(writing, line));
137
+ for (const layer of layersOf(parts)) {
138
+ lines.push(charged(writing, ` <g id="${escaped(layer.id)}" inkscape:groupmode="layer" inkscape:label="${escaped(layer.label)}" fill="none" stroke-width="${svgNumber(strokeWidth)}" stroke-linecap="round" stroke-linejoin="round">`));
139
+ for (const part of layer.parts) {
140
+ lines.push(charged(writing, ` <g stroke="${part.color}"${dashText(part.lineType, patternUnit)}>`));
141
+ for (const path of part.paths) {
142
+ const data = pathText(path, writing);
143
+ if (data !== "") {
144
+ lines.push(charged(writing, ` <path d="${data}"/>`, data.length));
145
+ }
146
+ }
147
+ lines.push(charged(writing, " </g>"));
148
+ }
149
+ lines.push(charged(writing, " </g>"));
150
+ }
151
+ lines.push(charged(writing, "</svg>"), "");
152
+ return joined(writing, lines, "\n");
153
+ };
@@ -0,0 +1,11 @@
1
+ export type XmlAttributes = Record<string, string>;
2
+ export interface XmlNode {
3
+ tag: string;
4
+ prefix: string;
5
+ namespace?: string;
6
+ attrs: XmlAttributes;
7
+ attrNamespaces?: XmlAttributes;
8
+ children: XmlNode[];
9
+ text?: string;
10
+ }
11
+ export declare function parseXml(input: string): XmlNode | undefined;
@@ -0,0 +1,322 @@
1
+ import { InputError } from "../../../../kernel-calls";
2
+ import { WorkBudget } from "../work-budget";
3
+ import { DECIMAL_RADIX, ENTITY_EXPANSION_CEILING, ENTITY_EXPANSION_FLOOR, ENTITY_EXPANSION_PER_INPUT_CHARACTER, HEX_RADIX, LAST_CODE_POINT, MAX_XML_NESTING, REPLACEMENT_CHARACTER, } from "./svg-reader.constants";
4
+ const RAW_TEXT_TAGS = new Set(["style", "script"]);
5
+ const BUILT_IN_ENTITIES = [["lt", "<"], ["gt", ">"], ["amp", "&"], ["quot", "\""], ["apos", "'"]];
6
+ const ENTITY_REFERENCE = /&(#x[0-9a-fA-F]+|#[0-9]+|[A-Za-z_][\w.-]*);/g;
7
+ const ENTITY_DECLARATION = /<!ENTITY\s+([A-Za-z_][\w.-]*)\s+(?:"([^"]*)"|'([^']*)')\s*>/g;
8
+ const SPACE = /\s/;
9
+ const COMMENT_OPEN = "<!--";
10
+ const COMMENT_CLOSE = "-->";
11
+ const CDATA_OPEN = "<![CDATA[";
12
+ const CDATA_CLOSE = "]]>";
13
+ const INSTRUCTION_OPEN = "<?";
14
+ const INSTRUCTION_CLOSE = "?>";
15
+ const DECLARATION_OPEN = "<!";
16
+ const END_TAG_OPEN = "</";
17
+ const XMLNS_PREFIX = "xmlns:";
18
+ const NOTHING_DECLARED = [];
19
+ const isSpace = (ch) => ch !== undefined && SPACE.test(ch);
20
+ function entitiesFor(input) {
21
+ const budget = WorkBudget.forInput(input.length, ENTITY_EXPANSION_PER_INPUT_CHARACTER, ENTITY_EXPANSION_FLOOR, ENTITY_EXPANSION_CEILING);
22
+ return { values: new Map(BUILT_IN_ENTITIES), budget };
23
+ }
24
+ function character(codePoint) {
25
+ return codePoint <= LAST_CODE_POINT ? String.fromCodePoint(codePoint) : REPLACEMENT_CHARACTER;
26
+ }
27
+ function decodeEntities(text, entities) {
28
+ if (text.indexOf("&") === -1) {
29
+ return text;
30
+ }
31
+ return text.replace(ENTITY_REFERENCE, (whole, name) => {
32
+ if (name.startsWith("#x")) {
33
+ return character(parseInt(name.slice(2), HEX_RADIX));
34
+ }
35
+ if (name.startsWith("#")) {
36
+ return character(parseInt(name.slice(1), DECIMAL_RADIX));
37
+ }
38
+ const value = entities.values.get(name);
39
+ if (value === undefined) {
40
+ return whole;
41
+ }
42
+ if (!entities.budget.spend(value.length)) {
43
+ throw new InputError(`The SVG's entities expand to more than ${entities.budget.capacity} characters, more than the importer reads from a file of its size; replace the entity references with their text and import it again.`, "svg");
44
+ }
45
+ return value;
46
+ });
47
+ }
48
+ function splitName(rawName) {
49
+ const colon = rawName.indexOf(":");
50
+ return colon >= 0 ? [rawName.slice(0, colon), rawName.slice(colon + 1).toLowerCase()] : ["", rawName.toLowerCase()];
51
+ }
52
+ function readStartTag(input, from, entities) {
53
+ const n = input.length;
54
+ let i = from + 1;
55
+ while (i < n && !isSpace(input[i]) && input[i] !== ">" && input[i] !== "/") {
56
+ i++;
57
+ }
58
+ const rawName = input.slice(from + 1, i);
59
+ const attrs = Object.create(null);
60
+ while (i < n) {
61
+ while (i < n && isSpace(input[i])) {
62
+ i++;
63
+ }
64
+ if (i >= n) {
65
+ return undefined;
66
+ }
67
+ if (input[i] === ">") {
68
+ return { rawName, attrs, selfClosing: false, end: i + 1 };
69
+ }
70
+ if (input[i] === "/" && input[i + 1] === ">") {
71
+ return { rawName, attrs, selfClosing: true, end: i + 2 };
72
+ }
73
+ const nameStart = i;
74
+ while (i < n && !isSpace(input[i]) && input[i] !== "=" && input[i] !== ">" && !(input[i] === "/" && input[i + 1] === ">")) {
75
+ i++;
76
+ }
77
+ const name = input.slice(nameStart, i);
78
+ if (name.length === 0) {
79
+ i++;
80
+ continue;
81
+ }
82
+ while (i < n && isSpace(input[i])) {
83
+ i++;
84
+ }
85
+ if (input[i] !== "=") {
86
+ attrs[name] = "";
87
+ continue;
88
+ }
89
+ i++;
90
+ while (i < n && isSpace(input[i])) {
91
+ i++;
92
+ }
93
+ const quote = input[i];
94
+ if (quote === "\"" || quote === "'") {
95
+ const close = input.indexOf(quote, i + 1);
96
+ if (close === -1) {
97
+ return undefined;
98
+ }
99
+ attrs[name] = decodeEntities(input.slice(i + 1, close), entities);
100
+ i = close + 1;
101
+ }
102
+ else {
103
+ const valueStart = i;
104
+ while (i < n && !isSpace(input[i]) && input[i] !== ">" && !(input[i] === "/" && input[i + 1] === ">")) {
105
+ i++;
106
+ }
107
+ attrs[name] = decodeEntities(input.slice(valueStart, i), entities);
108
+ }
109
+ }
110
+ return undefined;
111
+ }
112
+ function skipDoctype(input, from, entities) {
113
+ var _a;
114
+ const n = input.length;
115
+ let i = from + DECLARATION_OPEN.length;
116
+ let quote = "";
117
+ while (i < n) {
118
+ const ch = input[i];
119
+ if (quote) {
120
+ if (ch === quote) {
121
+ quote = "";
122
+ }
123
+ }
124
+ else if (ch === "\"" || ch === "'") {
125
+ quote = ch;
126
+ }
127
+ else if (ch === "[") {
128
+ const close = input.indexOf("]", i + 1);
129
+ const subset = input.slice(i + 1, close === -1 ? n : close);
130
+ for (const declared of subset.matchAll(ENTITY_DECLARATION)) {
131
+ const name = declared[1];
132
+ if (!entities.values.has(name)) {
133
+ entities.values.set(name, decodeEntities((_a = declared[2]) !== null && _a !== void 0 ? _a : declared[3], entities));
134
+ }
135
+ }
136
+ i = close === -1 ? n : close;
137
+ }
138
+ else if (ch === ">") {
139
+ return i + 1;
140
+ }
141
+ i++;
142
+ }
143
+ return n;
144
+ }
145
+ function namedAt(input, at, rawName) {
146
+ for (let k = 0; k < rawName.length; k++) {
147
+ const ch = input[at + k];
148
+ if (ch === undefined || ch.toLowerCase() !== rawName[k].toLowerCase()) {
149
+ return false;
150
+ }
151
+ }
152
+ return true;
153
+ }
154
+ function rawTextEnd(input, from, rawName) {
155
+ const n = input.length;
156
+ for (let at = input.indexOf(END_TAG_OPEN, from); at !== -1; at = input.indexOf(END_TAG_OPEN, at + END_TAG_OPEN.length)) {
157
+ if (!namedAt(input, at + END_TAG_OPEN.length, rawName)) {
158
+ continue;
159
+ }
160
+ let close = at + END_TAG_OPEN.length + rawName.length;
161
+ while (close < n && isSpace(input[close])) {
162
+ close++;
163
+ }
164
+ if (input[close] === ">") {
165
+ return [at, close + 1];
166
+ }
167
+ }
168
+ return [n, n];
169
+ }
170
+ function rawTextOf(raw, entities) {
171
+ const parts = [];
172
+ let at = 0;
173
+ while (at < raw.length) {
174
+ const open = raw.indexOf(CDATA_OPEN, at);
175
+ const close = open === -1 ? -1 : raw.indexOf(CDATA_CLOSE, open + CDATA_OPEN.length);
176
+ if (close === -1) {
177
+ parts.push(decodeEntities(raw.slice(at), entities));
178
+ break;
179
+ }
180
+ parts.push(decodeEntities(raw.slice(at, open), entities), raw.slice(open + CDATA_OPEN.length, close));
181
+ at = close + CDATA_CLOSE.length;
182
+ }
183
+ return parts.join("");
184
+ }
185
+ function readRawText(input, from, rawName, entities) {
186
+ const [end, after] = rawTextEnd(input, from, rawName);
187
+ return [rawTextOf(input.slice(from, end), entities), after];
188
+ }
189
+ function declarationsOf(attrs) {
190
+ let own;
191
+ for (const name of Object.keys(attrs)) {
192
+ if (name === "xmlns" || name.startsWith(XMLNS_PREFIX)) {
193
+ own = own !== null && own !== void 0 ? own : new Map();
194
+ own.set(name === "xmlns" ? "" : name.slice(XMLNS_PREFIX.length), attrs[name]);
195
+ }
196
+ }
197
+ return own;
198
+ }
199
+ function namespaceIn(scopes, prefix) {
200
+ const bound = scopes.get(prefix);
201
+ return bound === undefined ? undefined : bound[bound.length - 1];
202
+ }
203
+ function attributeNamespacesOf(attrs, own, scopes) {
204
+ var _a;
205
+ let resolved;
206
+ for (const name of Object.keys(attrs)) {
207
+ const colon = name.indexOf(":");
208
+ if (colon <= 0 || name.startsWith(XMLNS_PREFIX)) {
209
+ continue;
210
+ }
211
+ const prefix = name.slice(0, colon);
212
+ const namespace = (_a = own === null || own === void 0 ? void 0 : own.get(prefix)) !== null && _a !== void 0 ? _a : namespaceIn(scopes, prefix);
213
+ if (namespace === undefined) {
214
+ continue;
215
+ }
216
+ resolved = resolved !== null && resolved !== void 0 ? resolved : Object.create(null);
217
+ resolved[name] = namespace;
218
+ }
219
+ return resolved;
220
+ }
221
+ function enterScope(scopes, own) {
222
+ const declared = [];
223
+ own.forEach((uri, prefix) => {
224
+ const bound = scopes.get(prefix);
225
+ if (bound) {
226
+ bound.push(uri);
227
+ }
228
+ else {
229
+ scopes.set(prefix, [uri]);
230
+ }
231
+ declared.push(prefix);
232
+ });
233
+ return declared;
234
+ }
235
+ function leaveScope(scopes, declared) {
236
+ declared.forEach((prefix) => scopes.get(prefix).pop());
237
+ }
238
+ function skipPast(input, marker, from) {
239
+ const at = input.indexOf(marker, from);
240
+ return at === -1 ? input.length : at + marker.length;
241
+ }
242
+ function closeElement(stack, rawName, scopes) {
243
+ for (let at = stack.length - 1; at > 0; at--) {
244
+ if (stack[at].rawName === rawName) {
245
+ for (let closing = stack.length - 1; closing >= at; closing--) {
246
+ leaveScope(scopes, stack[closing].declared);
247
+ }
248
+ stack.length = at;
249
+ return;
250
+ }
251
+ }
252
+ }
253
+ export function parseXml(input) {
254
+ var _a;
255
+ const n = input.length;
256
+ const entities = entitiesFor(input);
257
+ const root = { tag: "#root", prefix: "", attrs: Object.create(null), children: [] };
258
+ const scopes = new Map();
259
+ const stack = [{ node: root, rawName: "", declared: NOTHING_DECLARED }];
260
+ let i = 0;
261
+ while (i < n) {
262
+ const lt = input.indexOf("<", i);
263
+ if (lt === -1) {
264
+ break;
265
+ }
266
+ i = lt;
267
+ if (input.startsWith(COMMENT_OPEN, i)) {
268
+ i = skipPast(input, COMMENT_CLOSE, i + COMMENT_OPEN.length);
269
+ }
270
+ else if (input.startsWith(CDATA_OPEN, i)) {
271
+ i = skipPast(input, CDATA_CLOSE, i + CDATA_OPEN.length);
272
+ }
273
+ else if (input.startsWith(INSTRUCTION_OPEN, i)) {
274
+ i = skipPast(input, INSTRUCTION_CLOSE, i + INSTRUCTION_OPEN.length);
275
+ }
276
+ else if (input.startsWith(DECLARATION_OPEN, i)) {
277
+ i = skipDoctype(input, i, entities);
278
+ }
279
+ else if (input.startsWith(END_TAG_OPEN, i)) {
280
+ const end = input.indexOf(">", i);
281
+ if (end === -1) {
282
+ break;
283
+ }
284
+ closeElement(stack, input.slice(i + END_TAG_OPEN.length, end).trim(), scopes);
285
+ i = end + 1;
286
+ }
287
+ else {
288
+ const start = readStartTag(input, i, entities);
289
+ if (!start) {
290
+ break;
291
+ }
292
+ i = start.end;
293
+ const parent = stack[stack.length - 1];
294
+ const [prefix, tag] = splitName(start.rawName);
295
+ const own = declarationsOf(start.attrs);
296
+ const node = { tag, prefix, attrs: start.attrs, children: [] };
297
+ const namespace = (_a = own === null || own === void 0 ? void 0 : own.get(prefix)) !== null && _a !== void 0 ? _a : namespaceIn(scopes, prefix);
298
+ if (namespace !== undefined) {
299
+ node.namespace = namespace;
300
+ }
301
+ const attrNamespaces = attributeNamespacesOf(start.attrs, own, scopes);
302
+ if (attrNamespaces) {
303
+ node.attrNamespaces = attrNamespaces;
304
+ }
305
+ parent.node.children.push(node);
306
+ if (start.selfClosing) {
307
+ continue;
308
+ }
309
+ if (RAW_TEXT_TAGS.has(tag)) {
310
+ const [text, after] = readRawText(input, i, start.rawName, entities);
311
+ node.text = text;
312
+ i = after;
313
+ continue;
314
+ }
315
+ if (stack.length > MAX_XML_NESTING) {
316
+ throw new InputError(`The SVG nests its elements more than ${MAX_XML_NESTING} deep, deeper than the importer reads; flatten its groups and import it again.`, "svg");
317
+ }
318
+ stack.push({ node, rawName: start.rawName, declared: own ? enterScope(scopes, own) : NOTHING_DECLARED });
319
+ }
320
+ }
321
+ return root.children[0];
322
+ }
@@ -0,0 +1,13 @@
1
+ export declare const QUADRATIC_DEGREE = 2;
2
+ export declare const CUBIC_DEGREE = 3;
3
+ export declare const SVG_ARC_PIECE: number;
4
+ export declare const EDGE_ON_SHARE = 1e-12;
5
+ export declare const SVG_DECIMALS = 6;
6
+ export declare const TRAILING_FRACTION_ZEROS: RegExp;
7
+ export declare const LEAST_VIEW_SPAN: number;
8
+ export declare const SVG_JOIN_DISTANCE = 1e-9;
9
+ export declare const STROKE_SHARE: number;
10
+ export declare const MARGIN_SHARE: number;
11
+ export declare const SVG_UNITS: Readonly<Record<string, readonly [string, number]>>;
12
+ export declare const ID_UNSAFE: RegExp;
13
+ export declare const PATH_COLOR = "#000000";
@@ -0,0 +1,13 @@
1
+ export const QUADRATIC_DEGREE = 2;
2
+ export const CUBIC_DEGREE = 3;
3
+ export const SVG_ARC_PIECE = Math.PI / 2;
4
+ export const EDGE_ON_SHARE = 1e-12;
5
+ export const SVG_DECIMALS = 6;
6
+ export const TRAILING_FRACTION_ZEROS = /(\.\d*?[1-9])0+$|\.0+$/;
7
+ export const LEAST_VIEW_SPAN = Math.pow(10, -SVG_DECIMALS);
8
+ export const SVG_JOIN_DISTANCE = 1e-9;
9
+ export const STROKE_SHARE = 1 / 500;
10
+ export const MARGIN_SHARE = 1 / 40;
11
+ export const SVG_UNITS = { mm: ["mm", 1], cm: ["cm", 1], m: ["cm", 100], in: ["in", 1] };
12
+ export const ID_UNSAFE = /[^A-Za-z0-9_.-]+/g;
13
+ export const PATH_COLOR = "#000000";
@@ -0,0 +1,11 @@
1
+ export declare class WorkBudget {
2
+ readonly capacity: number;
3
+ private used;
4
+ constructor(capacity: number);
5
+ static forInput(inputSize: number, perUnit: number, floor: number, ceiling?: number): WorkBudget;
6
+ get spent(): number;
7
+ get remaining(): number;
8
+ get exhausted(): boolean;
9
+ spend(units: number): boolean;
10
+ affords(units: number): boolean;
11
+ }
@@ -0,0 +1,26 @@
1
+ export class WorkBudget {
2
+ constructor(capacity) {
3
+ this.capacity = capacity;
4
+ this.used = 0;
5
+ }
6
+ static forInput(inputSize, perUnit, floor, ceiling = Number.POSITIVE_INFINITY) {
7
+ const scaled = Number.isFinite(inputSize) && inputSize > 0 ? inputSize * perUnit : 0;
8
+ return new WorkBudget(Math.min(ceiling, Math.max(floor, scaled)));
9
+ }
10
+ get spent() {
11
+ return this.used;
12
+ }
13
+ get remaining() {
14
+ return Math.max(0, this.capacity - this.used);
15
+ }
16
+ get exhausted() {
17
+ return this.used > this.capacity;
18
+ }
19
+ spend(units) {
20
+ this.used += units > 0 ? units : 0;
21
+ return this.used <= this.capacity;
22
+ }
23
+ affords(units) {
24
+ return this.used + (units > 0 ? units : 0) <= this.capacity;
25
+ }
26
+ }
@@ -0,0 +1,8 @@
1
+ import type { Base } from "../../../inputs/base-inputs";
2
+ export type AffineMatrix2 = readonly [number, number, number, number, number, number];
3
+ export declare const AFFINE_IDENTITY2: AffineMatrix2;
4
+ export declare const affineProduct2: (first: AffineMatrix2, second: AffineMatrix2) => AffineMatrix2;
5
+ export declare const affinePoint2: (matrix: AffineMatrix2, point: Base.Point2) => Base.Point2;
6
+ export declare const affineTranslation2: (x: number, y: number) => AffineMatrix2;
7
+ export declare const affineScaling2: (x: number, y: number) => AffineMatrix2;
8
+ export declare const affineAreaScale2: (matrix: AffineMatrix2) => number;
@@ -0,0 +1,18 @@
1
+ import { cross2 } from "./vectors";
2
+ export const AFFINE_IDENTITY2 = [1, 0, 0, 1, 0, 0];
3
+ export const affineProduct2 = (first, second) => {
4
+ const [a1, b1, c1, d1, e1, f1] = first;
5
+ const [a2, b2, c2, d2, e2, f2] = second;
6
+ return [
7
+ a1 * a2 + c1 * b2,
8
+ b1 * a2 + d1 * b2,
9
+ a1 * c2 + c1 * d2,
10
+ b1 * c2 + d1 * d2,
11
+ a1 * e2 + c1 * f2 + e1,
12
+ b1 * e2 + d1 * f2 + f1,
13
+ ];
14
+ };
15
+ export const affinePoint2 = (matrix, point) => [matrix[0] * point[0] + matrix[2] * point[1] + matrix[4], matrix[1] * point[0] + matrix[3] * point[1] + matrix[5]];
16
+ export const affineTranslation2 = (x, y) => [1, 0, 0, 1, x, y];
17
+ export const affineScaling2 = (x, y) => [x, 0, 0, y, 0, 0];
18
+ export const affineAreaScale2 = (matrix) => Math.sqrt(Math.abs(cross2([matrix[0], matrix[1]], [matrix[2], matrix[3]])));
@@ -0,0 +1,14 @@
1
+ import type { AffineMatrix2 } from "./affine2";
2
+ import type * as Inputs from "../../../inputs";
3
+ type Point2 = Inputs.Base.Point2;
4
+ export interface EllipseArc2 {
5
+ center: Point2;
6
+ rx: number;
7
+ ry: number;
8
+ xAxisRotation: number;
9
+ startAngle: number;
10
+ deltaAngle: number;
11
+ }
12
+ export declare const ellipseArcOfEndpoints2: (from: Point2, to: Point2, radiusX: number, radiusY: number, xAxisRotation: number, largeArc: boolean, sweep: boolean) => EllipseArc2 | undefined;
13
+ export declare const affineEllipseArc2: (matrix: AffineMatrix2, arc: EllipseArc2) => EllipseArc2;
14
+ export {};