@bitbybit-dev/base 1.6.2 → 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 (163) hide show
  1. package/lib/api/GlobalCDNProvider.js +1 -1
  2. package/lib/api/inputs/base-inputs.d.ts +20 -7
  3. package/lib/api/inputs/base-inputs.js +10 -0
  4. package/lib/api/inputs/io-inputs.d.ts +527 -59
  5. package/lib/api/inputs/io-inputs.js +432 -40
  6. package/lib/api/inputs/line-inputs.d.ts +17 -0
  7. package/lib/api/inputs/line-inputs.js +15 -0
  8. package/lib/api/kernel-calls/index.d.ts +1 -0
  9. package/lib/api/kernel-calls/index.js +1 -0
  10. package/lib/api/kernel-calls/settings.d.ts +82 -0
  11. package/lib/api/kernel-calls/settings.js +123 -0
  12. package/lib/api/recipes/surface-mesh.d.ts +2 -1
  13. package/lib/api/recipes/surface-mesh.js +5 -3
  14. package/lib/api/resolved-inputs/index.d.ts +22 -2
  15. package/lib/api/services/base-bitbybit.d.ts +12 -0
  16. package/lib/api/services/base-bitbybit.js +19 -3
  17. package/lib/api/services/color.js +2 -3
  18. package/lib/api/services/helpers/color/color-forms.constants.d.ts +3 -0
  19. package/lib/api/services/helpers/color/color-forms.constants.js +3 -0
  20. package/lib/api/services/helpers/color/color-forms.d.ts +1 -0
  21. package/lib/api/services/helpers/color/color-forms.js +4 -1
  22. package/lib/api/services/helpers/color/css-colors.constants.d.ts +9 -0
  23. package/lib/api/services/helpers/color/css-colors.constants.js +158 -0
  24. package/lib/api/services/helpers/color/css-colors.d.ts +1 -0
  25. package/lib/api/services/helpers/color/css-colors.js +61 -0
  26. package/lib/api/services/helpers/curves/bezier-pieces.constants.d.ts +5 -0
  27. package/lib/api/services/helpers/curves/bezier-pieces.constants.js +5 -0
  28. package/lib/api/services/helpers/curves/bezier-pieces.d.ts +24 -0
  29. package/lib/api/services/helpers/curves/bezier-pieces.js +149 -0
  30. package/lib/api/services/helpers/curves/conic-arcs.constants.d.ts +5 -0
  31. package/lib/api/services/helpers/curves/conic-arcs.constants.js +5 -0
  32. package/lib/api/services/helpers/curves/conic-arcs.d.ts +24 -0
  33. package/lib/api/services/helpers/curves/conic-arcs.js +85 -0
  34. package/lib/api/services/helpers/curves/nurbs-forms.d.ts +20 -0
  35. package/lib/api/services/helpers/curves/nurbs-forms.js +112 -0
  36. package/lib/api/services/helpers/curves/nurbs.d.ts +4 -0
  37. package/lib/api/services/helpers/curves/nurbs.js +10 -9
  38. package/lib/api/services/helpers/files/drawing/drawing-budget.d.ts +3 -0
  39. package/lib/api/services/helpers/files/drawing/drawing-budget.js +24 -0
  40. package/lib/api/services/helpers/files/drawing/drawing-geometry.d.ts +18 -0
  41. package/lib/api/services/helpers/files/drawing/drawing-geometry.js +84 -0
  42. package/lib/api/services/helpers/files/drawing/drawing-model.d.ts +5 -0
  43. package/lib/api/services/helpers/files/drawing/drawing-model.js +252 -0
  44. package/lib/api/services/helpers/files/drawing/drawing-types.d.ts +60 -0
  45. package/lib/api/services/helpers/files/drawing/drawing-types.js +1 -0
  46. package/lib/api/services/helpers/files/drawing/drawing.constants.d.ts +28 -0
  47. package/lib/api/services/helpers/files/drawing/drawing.constants.js +29 -0
  48. package/lib/api/services/helpers/files/dxf/dxf-colors.d.ts +5 -0
  49. package/lib/api/services/helpers/files/dxf/dxf-colors.js +45 -0
  50. package/lib/api/services/helpers/files/dxf/dxf-entities.d.ts +17 -0
  51. package/lib/api/services/helpers/files/dxf/dxf-entities.js +193 -0
  52. package/lib/api/services/helpers/files/dxf/dxf-file-text.d.ts +1 -0
  53. package/lib/api/services/helpers/files/dxf/dxf-file-text.js +50 -0
  54. package/lib/api/services/helpers/files/dxf/dxf-generator.d.ts +2 -36
  55. package/lib/api/services/helpers/files/dxf/dxf-generator.js +81 -523
  56. package/lib/api/services/helpers/files/dxf/dxf-reader-dtos.d.ts +6 -0
  57. package/lib/api/services/helpers/files/dxf/dxf-reader-dtos.js +81 -0
  58. package/lib/api/services/helpers/files/dxf/dxf-reader-geometry.d.ts +21 -0
  59. package/lib/api/services/helpers/files/dxf/dxf-reader-geometry.js +67 -0
  60. package/lib/api/services/helpers/files/dxf/dxf-reader-types.d.ts +69 -0
  61. package/lib/api/services/helpers/files/dxf/dxf-reader-types.js +1 -0
  62. package/lib/api/services/helpers/files/dxf/dxf-reader.constants.d.ts +71 -0
  63. package/lib/api/services/helpers/files/dxf/dxf-reader.constants.js +78 -0
  64. package/lib/api/services/helpers/files/dxf/dxf-reader.d.ts +2 -0
  65. package/lib/api/services/helpers/files/dxf/dxf-reader.js +556 -0
  66. package/lib/api/services/helpers/files/dxf/dxf-tables.d.ts +27 -0
  67. package/lib/api/services/helpers/files/dxf/dxf-tables.js +247 -0
  68. package/lib/api/services/helpers/files/dxf/dxf-text.d.ts +26 -0
  69. package/lib/api/services/helpers/files/dxf/dxf-text.js +97 -0
  70. package/lib/api/services/helpers/files/dxf/dxf-tokens.d.ts +9 -0
  71. package/lib/api/services/helpers/files/dxf/dxf-tokens.js +65 -0
  72. package/lib/api/services/helpers/files/dxf/dxf.constants.d.ts +32 -1
  73. package/lib/api/services/helpers/files/dxf/dxf.constants.js +34 -1
  74. package/lib/api/services/helpers/files/file-bytes.d.ts +1 -0
  75. package/lib/api/services/helpers/files/file-bytes.js +15 -0
  76. package/lib/api/services/helpers/files/ground-axes.d.ts +2 -0
  77. package/lib/api/services/helpers/files/ground-axes.js +4 -0
  78. package/lib/api/services/helpers/files/mesh-file-axes.d.ts +7 -0
  79. package/lib/api/services/helpers/files/mesh-file-axes.js +17 -0
  80. package/lib/api/services/helpers/files/stl-axes.d.ts +4 -0
  81. package/lib/api/services/helpers/files/stl-axes.js +22 -0
  82. package/lib/api/services/helpers/files/stl-types.d.ts +7 -0
  83. package/lib/api/services/helpers/files/stl-types.js +1 -0
  84. package/lib/api/services/helpers/files/stl.constants.d.ts +11 -0
  85. package/lib/api/services/helpers/files/stl.constants.js +11 -0
  86. package/lib/api/services/helpers/files/stl.d.ts +3 -0
  87. package/lib/api/services/helpers/files/stl.js +93 -0
  88. package/lib/api/services/helpers/files/svg/svg-css.d.ts +48 -0
  89. package/lib/api/services/helpers/files/svg/svg-css.js +444 -0
  90. package/lib/api/services/helpers/files/svg/svg-dash-patterns.d.ts +2 -0
  91. package/lib/api/services/helpers/files/svg/svg-dash-patterns.js +35 -0
  92. package/lib/api/services/helpers/files/svg/svg-document.d.ts +26 -0
  93. package/lib/api/services/helpers/files/svg/svg-document.js +89 -0
  94. package/lib/api/services/helpers/files/svg/svg-memo.d.ts +1 -0
  95. package/lib/api/services/helpers/files/svg/svg-memo.js +8 -0
  96. package/lib/api/services/helpers/files/svg/svg-normalizer.d.ts +6 -0
  97. package/lib/api/services/helpers/files/svg/svg-normalizer.js +433 -0
  98. package/lib/api/services/helpers/files/svg/svg-numbers.d.ts +15 -0
  99. package/lib/api/services/helpers/files/svg/svg-numbers.js +61 -0
  100. package/lib/api/services/helpers/files/svg/svg-path-data.constants.d.ts +8 -0
  101. package/lib/api/services/helpers/files/svg/svg-path-data.constants.js +10 -0
  102. package/lib/api/services/helpers/files/svg/svg-path-data.d.ts +9 -0
  103. package/lib/api/services/helpers/files/svg/svg-path-data.js +203 -0
  104. package/lib/api/services/helpers/files/svg/svg-path-types.d.ts +33 -0
  105. package/lib/api/services/helpers/files/svg/svg-path-types.js +1 -0
  106. package/lib/api/services/helpers/files/svg/svg-reader.constants.d.ts +35 -0
  107. package/lib/api/services/helpers/files/svg/svg-reader.constants.js +35 -0
  108. package/lib/api/services/helpers/files/svg/svg-reading.constants.d.ts +9 -0
  109. package/lib/api/services/helpers/files/svg/svg-reading.constants.js +9 -0
  110. package/lib/api/services/helpers/files/svg/svg-reading.d.ts +6 -0
  111. package/lib/api/services/helpers/files/svg/svg-reading.js +233 -0
  112. package/lib/api/services/helpers/files/svg/svg-scene-types.d.ts +54 -0
  113. package/lib/api/services/helpers/files/svg/svg-scene-types.js +1 -0
  114. package/lib/api/services/helpers/files/svg/svg-shapes.d.ts +6 -0
  115. package/lib/api/services/helpers/files/svg/svg-shapes.js +102 -0
  116. package/lib/api/services/helpers/files/svg/svg-strokes.d.ts +38 -0
  117. package/lib/api/services/helpers/files/svg/svg-strokes.js +108 -0
  118. package/lib/api/services/helpers/files/svg/svg-style.d.ts +26 -0
  119. package/lib/api/services/helpers/files/svg/svg-style.js +211 -0
  120. package/lib/api/services/helpers/files/svg/svg-transform.d.ts +10 -0
  121. package/lib/api/services/helpers/files/svg/svg-transform.js +76 -0
  122. package/lib/api/services/helpers/files/svg/svg-writer.d.ts +16 -0
  123. package/lib/api/services/helpers/files/svg/svg-writer.js +153 -0
  124. package/lib/api/services/helpers/files/svg/svg-xml.d.ts +11 -0
  125. package/lib/api/services/helpers/files/svg/svg-xml.js +322 -0
  126. package/lib/api/services/helpers/files/svg/svg.constants.d.ts +13 -0
  127. package/lib/api/services/helpers/files/svg/svg.constants.js +13 -0
  128. package/lib/api/services/helpers/files/work-budget.d.ts +11 -0
  129. package/lib/api/services/helpers/files/work-budget.js +26 -0
  130. package/lib/api/services/helpers/geometry/affine2.d.ts +8 -0
  131. package/lib/api/services/helpers/geometry/affine2.js +18 -0
  132. package/lib/api/services/helpers/geometry/ellipse-arcs.d.ts +14 -0
  133. package/lib/api/services/helpers/geometry/ellipse-arcs.js +96 -0
  134. package/lib/api/services/helpers/geometry/frame-axes.d.ts +3 -1
  135. package/lib/api/services/helpers/geometry/polygon-nesting.constants.d.ts +6 -0
  136. package/lib/api/services/helpers/geometry/polygon-nesting.constants.js +6 -0
  137. package/lib/api/services/helpers/geometry/polygon-nesting.d.ts +31 -0
  138. package/lib/api/services/helpers/geometry/polygon-nesting.js +251 -0
  139. package/lib/api/services/helpers/geometry/vectors.d.ts +1 -0
  140. package/lib/api/services/helpers/geometry/vectors.js +6 -0
  141. package/lib/api/services/io/drawing.d.ts +170 -0
  142. package/lib/api/services/io/drawing.js +212 -0
  143. package/lib/api/services/io/dxf.d.ts +134 -0
  144. package/lib/api/services/io/dxf.js +167 -0
  145. package/lib/api/services/io/index.d.ts +4 -0
  146. package/lib/api/services/io/index.js +4 -0
  147. package/lib/api/services/io/io.d.ts +14 -0
  148. package/lib/api/services/io/io.js +15 -0
  149. package/lib/api/services/io/svg.d.ts +64 -0
  150. package/lib/api/services/io/svg.js +102 -0
  151. package/lib/api/services/line.d.ts +17 -0
  152. package/lib/api/services/line.js +20 -1
  153. package/lib/api/services/point.d.ts +4 -1
  154. package/lib/api/services/point.js +9 -4
  155. package/lib/api/services/recipes.d.ts +3 -0
  156. package/lib/api/services/recipes.js +4 -1
  157. package/lib/api/services/text.d.ts +4 -1
  158. package/lib/api/services/text.js +8 -3
  159. package/package.json +1 -1
  160. package/lib/api/services/helpers/files/dxf/dxf.d.ts +0 -87
  161. package/lib/api/services/helpers/files/dxf/dxf.js +0 -111
  162. package/lib/api/services/io.d.ts +0 -9
  163. package/lib/api/services/io.js +0 -10
@@ -0,0 +1,203 @@
1
+ import { conicArcOfEllipseArc, ellipseFramePoint } from "../../curves/conic-arcs";
2
+ import { pointOnCubic2 } from "../../geometry/arcs";
3
+ import { ellipseArcOfEndpoints2 } from "../../geometry/ellipse-arcs";
4
+ import { radiansOf } from "../../geometry/scalars";
5
+ import { between2, scale2, subtract2 } from "../../geometry/vectors";
6
+ import { ARC_COMMAND, ARC_END_ARGUMENT, ARC_FLAG_ARGUMENTS, ARGUMENTS_PER_PATH_COMMAND, CUBIC_END_ARGUMENT, MOVE_COMMAND, QUADRATIC_TO_CUBIC_SHARE, QUARTER_TURN, } from "./svg-path-data.constants";
7
+ const NUMBER = /[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/y;
8
+ const SEPARATOR = /[\s,]/;
9
+ const skipSeparators = (text, from) => {
10
+ let at = from;
11
+ while (at < text.length && SEPARATOR.test(text[at])) {
12
+ at++;
13
+ }
14
+ return at;
15
+ };
16
+ export const svgNumberAt = (text, at) => {
17
+ NUMBER.lastIndex = at;
18
+ const match = NUMBER.exec(text);
19
+ const value = match ? parseFloat(match[0]) : Number.NaN;
20
+ return Number.isFinite(value) ? [value, NUMBER.lastIndex] : undefined;
21
+ };
22
+ export const scanSvgNumbers = (text) => {
23
+ const numbers = [];
24
+ if (!text) {
25
+ return { numbers, malformed: false };
26
+ }
27
+ for (let at = skipSeparators(text, 0); at < text.length; at = skipSeparators(text, at)) {
28
+ const read = svgNumberAt(text, at);
29
+ if (!read) {
30
+ return { numbers, malformed: true };
31
+ }
32
+ numbers.push(read[0]);
33
+ at = read[1];
34
+ }
35
+ return { numbers, malformed: false };
36
+ };
37
+ const flagAt = (text, at) => {
38
+ const ch = text[at];
39
+ return ch === "0" || ch === "1" ? [ch === "1" ? 1 : 0, at + 1] : undefined;
40
+ };
41
+ const argumentGroupAt = (d, from, lower, count) => {
42
+ const args = [];
43
+ let at = from;
44
+ for (let k = 0; k < count; k++) {
45
+ at = skipSeparators(d, at);
46
+ const read = lower === ARC_COMMAND && ARC_FLAG_ARGUMENTS.has(k) ? flagAt(d, at) : svgNumberAt(d, at);
47
+ if (!read) {
48
+ break;
49
+ }
50
+ args.push(read[0]);
51
+ at = read[1];
52
+ }
53
+ return { args, end: at };
54
+ };
55
+ const pathTokens = (d) => {
56
+ const tokens = [];
57
+ let at = skipSeparators(d, 0);
58
+ while (at < d.length) {
59
+ const command = d[at];
60
+ const lower = command.toLowerCase();
61
+ const count = ARGUMENTS_PER_PATH_COMMAND.get(lower);
62
+ at++;
63
+ if (count === 0) {
64
+ tokens.push({ command, args: [] });
65
+ }
66
+ else if (count !== undefined) {
67
+ for (let first = true;; first = false) {
68
+ const group = argumentGroupAt(d, at, lower, count);
69
+ at = group.end;
70
+ if (group.args.length < count) {
71
+ break;
72
+ }
73
+ tokens.push({ command: !first && lower === MOVE_COMMAND ? (command === MOVE_COMMAND ? "l" : "L") : command, args: group.args });
74
+ at = skipSeparators(d, at);
75
+ if (at >= d.length || ARGUMENTS_PER_PATH_COMMAND.has(d[at].toLowerCase())) {
76
+ break;
77
+ }
78
+ }
79
+ }
80
+ at = skipSeparators(d, at);
81
+ }
82
+ return tokens;
83
+ };
84
+ export const parseSvgPathData = (d) => {
85
+ const subpaths = [];
86
+ let current;
87
+ let cursor = [0, 0];
88
+ let subpathStart = [0, 0];
89
+ let lastCubicControl;
90
+ let lastQuadControl;
91
+ const startSubpath = (point) => {
92
+ current = { start: [point[0], point[1]], segments: [], closed: false };
93
+ subpaths.push(current);
94
+ subpathStart = [point[0], point[1]];
95
+ };
96
+ const push = (segment) => {
97
+ if (!current) {
98
+ startSubpath(cursor);
99
+ }
100
+ current.segments.push(segment);
101
+ };
102
+ const reflected = (control) => (control ? subtract2(scale2(cursor, 2), control) : [cursor[0], cursor[1]]);
103
+ for (const { command, args: a } of pathTokens(d)) {
104
+ const relative = command === command.toLowerCase();
105
+ const baseX = relative ? cursor[0] : 0;
106
+ const baseY = relative ? cursor[1] : 0;
107
+ const at = (k) => [baseX + a[k], baseY + a[k + 1]];
108
+ let cubicControl;
109
+ let quadControl;
110
+ switch (command.toLowerCase()) {
111
+ case "m":
112
+ cursor = at(0);
113
+ startSubpath(cursor);
114
+ break;
115
+ case "l":
116
+ case "h":
117
+ case "v": {
118
+ const lower = command.toLowerCase();
119
+ const to = lower === "l" ? at(0) : lower === "h" ? [baseX + a[0], cursor[1]] : [cursor[0], baseY + a[0]];
120
+ push({ type: "line", to });
121
+ cursor = to;
122
+ break;
123
+ }
124
+ case "c": {
125
+ const to = at(CUBIC_END_ARGUMENT);
126
+ cubicControl = at(2);
127
+ push({ type: "cubic", c1: at(0), c2: cubicControl, to });
128
+ cursor = to;
129
+ break;
130
+ }
131
+ case "s": {
132
+ const to = at(2);
133
+ cubicControl = at(0);
134
+ push({ type: "cubic", c1: reflected(lastCubicControl), c2: cubicControl, to });
135
+ cursor = to;
136
+ break;
137
+ }
138
+ case "q": {
139
+ const to = at(2);
140
+ quadControl = at(0);
141
+ push({ type: "quad", c: quadControl, to });
142
+ cursor = to;
143
+ break;
144
+ }
145
+ case "t": {
146
+ const to = at(0);
147
+ quadControl = reflected(lastQuadControl);
148
+ push({ type: "quad", c: quadControl, to });
149
+ cursor = to;
150
+ break;
151
+ }
152
+ case "a": {
153
+ const to = at(ARC_END_ARGUMENT);
154
+ if (cursor[0] !== to[0] || cursor[1] !== to[1]) {
155
+ const arc = ellipseArcOfEndpoints2(cursor, to, a[0], a[1], radiansOf(a[2]), a[3] === 1, a[4] === 1);
156
+ push(arc ? Object.assign({ type: "arc", to }, arc) : { type: "line", to });
157
+ }
158
+ cursor = to;
159
+ break;
160
+ }
161
+ default:
162
+ if (current) {
163
+ current.closed = true;
164
+ cursor = [subpathStart[0], subpathStart[1]];
165
+ }
166
+ current = undefined;
167
+ break;
168
+ }
169
+ lastCubicControl = cubicControl;
170
+ lastQuadControl = quadControl;
171
+ }
172
+ return subpaths.filter((subpath) => subpath.segments.length > 0);
173
+ };
174
+ const segmentPoints = (from, segment, perCurve, perQuarterTurn, out) => {
175
+ if (segment.type === "line") {
176
+ out.push(segment.to);
177
+ return;
178
+ }
179
+ if (segment.type === "arc") {
180
+ const frame = conicArcOfEllipseArc(segment).frame;
181
+ const steps = Math.max(1, perCurve, Math.ceil(Math.abs(segment.deltaAngle) / QUARTER_TURN * perQuarterTurn));
182
+ for (let k = 1; k <= steps; k++) {
183
+ const point = ellipseFramePoint(frame, segment.startAngle + segment.deltaAngle * k / steps);
184
+ out.push([point[0], point[1]]);
185
+ }
186
+ return;
187
+ }
188
+ const [c1, c2] = segment.type === "cubic"
189
+ ? [segment.c1, segment.c2]
190
+ : [between2(from, segment.c, QUADRATIC_TO_CUBIC_SHARE), between2(segment.to, segment.c, QUADRATIC_TO_CUBIC_SHARE)];
191
+ for (let k = 1; k <= perCurve; k++) {
192
+ out.push(pointOnCubic2(from, c1, c2, segment.to, k / perCurve));
193
+ }
194
+ };
195
+ export const svgSubpathPoints = (subpath, perCurve, perQuarterTurn) => {
196
+ const points = [subpath.start];
197
+ let from = subpath.start;
198
+ for (const segment of subpath.segments) {
199
+ segmentPoints(from, segment, perCurve, perQuarterTurn, points);
200
+ from = segment.to;
201
+ }
202
+ return points;
203
+ };
@@ -0,0 +1,33 @@
1
+ import type * as Inputs from "../../../../inputs";
2
+ import type { EllipseArc2 } from "../../geometry/ellipse-arcs";
3
+ type Point2 = Inputs.Base.Point2;
4
+ export interface SvgLineSegment {
5
+ type: "line";
6
+ to: Point2;
7
+ }
8
+ export interface SvgQuadSegment {
9
+ type: "quad";
10
+ c: Point2;
11
+ to: Point2;
12
+ }
13
+ export interface SvgCubicSegment {
14
+ type: "cubic";
15
+ c1: Point2;
16
+ c2: Point2;
17
+ to: Point2;
18
+ }
19
+ export interface SvgArcSegment extends EllipseArc2 {
20
+ type: "arc";
21
+ to: Point2;
22
+ }
23
+ export type SvgSegment = SvgLineSegment | SvgQuadSegment | SvgCubicSegment | SvgArcSegment;
24
+ export interface SvgSubpath {
25
+ start: Point2;
26
+ segments: SvgSegment[];
27
+ closed: boolean;
28
+ }
29
+ export interface ScannedSvgNumbers {
30
+ numbers: number[];
31
+ malformed: boolean;
32
+ }
33
+ export {};
@@ -0,0 +1,35 @@
1
+ export declare const PIXELS_PER_INCH = 96;
2
+ export declare const POINTS_PER_INCH = 72;
3
+ export declare const PICAS_PER_INCH = 6;
4
+ export declare const FONT_SIZE_PIXELS = 16;
5
+ export declare const EX_PER_EM = 0.5;
6
+ export declare const PERCENT = 100;
7
+ export declare const COLOR_PARTS_WITH_ALPHA = 4;
8
+ export declare const MAX_XML_NESTING = 256;
9
+ export declare const ENTITY_EXPANSION_FLOOR = 65536;
10
+ export declare const ENTITY_EXPANSION_PER_INPUT_CHARACTER = 8;
11
+ export declare const ENTITY_EXPANSION_CEILING = 134217728;
12
+ export declare const LAST_CODE_POINT = 1114111;
13
+ export declare const REPLACEMENT_CHARACTER = "\uFFFD";
14
+ export declare const MAX_WALK_DEPTH = 512;
15
+ export declare const MAX_USE_DEPTH = 16;
16
+ export declare const MAX_VISITED_ELEMENTS = 500000;
17
+ export declare const SEGMENTS_PER_INPUT_CHARACTER = 2;
18
+ export declare const SEGMENT_FLOOR = 25000;
19
+ export declare const SEGMENT_CEILING = 120000;
20
+ export declare const MAX_LISTED_SELECTORS = 5;
21
+ export declare const MAX_LISTED_TAGS = 5;
22
+ export declare const MAX_DASH_VALUES = 64;
23
+ export declare const MAX_AT_RULE_NESTING = 32;
24
+ export declare const SELECTOR_MATCH_STEPS_PER_INPUT_CHARACTER = 64;
25
+ export declare const SELECTOR_MATCH_STEP_FLOOR = 2000000;
26
+ export declare const SELECTOR_MATCH_STEP_CEILING = 64000000;
27
+ export declare const STEPS_PER_ANCESTOR_TABLE = 8;
28
+ export declare const STEPS_PER_MATCHED_DECLARATION = 8;
29
+ export declare const SPECIFICITY_ID = 1000000;
30
+ export declare const SPECIFICITY_CLASS = 1000;
31
+ export declare const SPECIFICITY_TYPE = 1;
32
+ export declare const MATRIX_ARGUMENTS = 6;
33
+ export declare const ROTATE_ABOUT_POINT_ARGUMENTS = 3;
34
+ export declare const HEX_RADIX = 16;
35
+ export declare const DECIMAL_RADIX = 10;
@@ -0,0 +1,35 @@
1
+ export const PIXELS_PER_INCH = 96;
2
+ export const POINTS_PER_INCH = 72;
3
+ export const PICAS_PER_INCH = 6;
4
+ export const FONT_SIZE_PIXELS = 16;
5
+ export const EX_PER_EM = 0.5;
6
+ export const PERCENT = 100;
7
+ export const COLOR_PARTS_WITH_ALPHA = 4;
8
+ export const MAX_XML_NESTING = 256;
9
+ export const ENTITY_EXPANSION_FLOOR = 65536;
10
+ export const ENTITY_EXPANSION_PER_INPUT_CHARACTER = 8;
11
+ export const ENTITY_EXPANSION_CEILING = 134217728;
12
+ export const LAST_CODE_POINT = 0x10ffff;
13
+ export const REPLACEMENT_CHARACTER = "\uFFFD";
14
+ export const MAX_WALK_DEPTH = 512;
15
+ export const MAX_USE_DEPTH = 16;
16
+ export const MAX_VISITED_ELEMENTS = 500000;
17
+ export const SEGMENTS_PER_INPUT_CHARACTER = 2;
18
+ export const SEGMENT_FLOOR = 25000;
19
+ export const SEGMENT_CEILING = 120000;
20
+ export const MAX_LISTED_SELECTORS = 5;
21
+ export const MAX_LISTED_TAGS = 5;
22
+ export const MAX_DASH_VALUES = 64;
23
+ export const MAX_AT_RULE_NESTING = 32;
24
+ export const SELECTOR_MATCH_STEPS_PER_INPUT_CHARACTER = 64;
25
+ export const SELECTOR_MATCH_STEP_FLOOR = 2000000;
26
+ export const SELECTOR_MATCH_STEP_CEILING = 64000000;
27
+ export const STEPS_PER_ANCESTOR_TABLE = 8;
28
+ export const STEPS_PER_MATCHED_DECLARATION = 8;
29
+ export const SPECIFICITY_ID = 1000000;
30
+ export const SPECIFICITY_CLASS = 1000;
31
+ export const SPECIFICITY_TYPE = 1;
32
+ export const MATRIX_ARGUMENTS = 6;
33
+ export const ROTATE_ABOUT_POINT_ARGUMENTS = 3;
34
+ export const HEX_RADIX = 16;
35
+ export const DECIMAL_RADIX = 10;
@@ -0,0 +1,9 @@
1
+ export declare const FULL_TURN: number;
2
+ export declare const SAME_CONIC_SHARE = 0.000001;
3
+ export declare const NUMBER_PRECISION_SLACK = 0.00001;
4
+ export declare const LEAST_ARC_SWEEP = 1e-9;
5
+ export declare const POINT_NOISE_SHARE = 1e-12;
6
+ export declare const DASH_MATCH_SHARE = 0.001;
7
+ export declare const LAYER_STAND_IN = "_";
8
+ export declare const UNREAD_PAINT = "#000000";
9
+ export declare const HIGH_SURROGATE_AT_END: RegExp;
@@ -0,0 +1,9 @@
1
+ export const FULL_TURN = 2 * Math.PI;
2
+ export const SAME_CONIC_SHARE = 1e-6;
3
+ export const NUMBER_PRECISION_SLACK = 1e-5;
4
+ export const LEAST_ARC_SWEEP = 1e-9;
5
+ export const POINT_NOISE_SHARE = 1e-12;
6
+ export const DASH_MATCH_SHARE = 1e-3;
7
+ export const LAYER_STAND_IN = "_";
8
+ export const UNREAD_PAINT = "#000000";
9
+ export const HIGH_SURROGATE_AT_END = /[\uD800-\uDBFF]$/;
@@ -0,0 +1,6 @@
1
+ import * as Inputs from "../../../../inputs";
2
+ export interface SvgReadingOptions {
3
+ readonly unit: Inputs.Base.unitOfLengthEnum | undefined;
4
+ readonly includeInvisible: boolean;
5
+ }
6
+ export declare const readSvgDrawing: (svg: string, options: SvgReadingOptions) => Inputs.IO.SvgDrawingDto;
@@ -0,0 +1,233 @@
1
+ import { affineScaling2 } from "../../geometry/affine2";
2
+ import * as Inputs from "../../../../inputs";
3
+ import { cssColorHex } from "../../color/css-colors";
4
+ import { angleWithinTurn, conicArcOfEllipseArc } from "../../curves/conic-arcs";
5
+ import { clampedUniformKnots } from "../../curves/nurbs-forms";
6
+ import { millimetresPerUnitOf } from "../../geometry/lengths";
7
+ import { distance2, distance3, length3, scale3 } from "../../geometry/vectors";
8
+ import { drawingBounds, patternUnitOf } from "../drawing/drawing-geometry";
9
+ import { writerBudgetOf } from "../drawing/drawing-budget";
10
+ import { drawingPartsOf } from "../drawing/drawing-model";
11
+ import { DEFAULT_LAYER, FIRST_PRINTABLE_CODE, FORBIDDEN_LAYER_CHARACTERS, FULL_TURN_DEGREES } from "../drawing/drawing.constants";
12
+ import { conicSegmentDto } from "../dxf/dxf-reader-dtos";
13
+ import { DXF_MAX_NAME_LENGTH } from "../dxf/dxf.constants";
14
+ import { lineTypeOfDashes } from "./svg-dash-patterns";
15
+ import { remembered } from "./svg-memo";
16
+ import { normalizeSvg } from "./svg-normalizer";
17
+ import { styleReadings } from "./svg-style";
18
+ import { transformSubpath } from "./svg-transform";
19
+ import { FULL_TURN, HIGH_SURROGATE_AT_END, LAYER_STAND_IN, LEAST_ARC_SWEEP, NUMBER_PRECISION_SLACK, POINT_NOISE_SHARE, SAME_CONIC_SHARE, UNREAD_PAINT, } from "./svg-reading.constants";
20
+ const MIRROR_Y = affineScaling2(1, -1);
21
+ const plural = (count, one, many) => (count === 1 ? one : many);
22
+ const pointOf = (point) => [point[0], point[1], 0];
23
+ const samePoint = (a, b) => distance2(a, b) <= POINT_NOISE_SHARE * Math.max(Math.abs(a[0]), Math.abs(a[1]), Math.abs(b[0]), Math.abs(b[1]));
24
+ const lineDto = (from, to) => (samePoint(from, to) ? [] : [{ start: pointOf(from), end: pointOf(to) }]);
25
+ const bezierDto = (controlPoints) => ({
26
+ controlPoints: controlPoints.map(pointOf),
27
+ degree: controlPoints.length - 1,
28
+ knots: clampedUniformKnots(controlPoints.length, controlPoints.length - 1),
29
+ });
30
+ const curveDtos = (from, segment) => {
31
+ switch (segment.type) {
32
+ case "line":
33
+ return lineDto(from, segment.to);
34
+ case "quad":
35
+ return [bezierDto([from, segment.c, segment.to])];
36
+ case "cubic":
37
+ return [bezierDto([from, segment.c1, segment.c2, segment.to])];
38
+ }
39
+ };
40
+ const isSameConic = (run, next) => {
41
+ const { frame } = run.arc;
42
+ const major = length3(frame.xAxis);
43
+ const minor = length3(frame.yAxis);
44
+ const near = (a, b) => distance3(a, b) <= run.tolerance;
45
+ const sameSize = Math.abs(length3(next.frame.xAxis) - major) <= run.tolerance && Math.abs(length3(next.frame.yAxis) - minor) <= run.tolerance;
46
+ const sameAxes = major - minor <= run.tolerance
47
+ || (near(next.frame.xAxis, frame.xAxis) && near(next.frame.yAxis, frame.yAxis))
48
+ || (near(next.frame.xAxis, scale3(frame.xAxis, -1)) && near(next.frame.yAxis, scale3(frame.yAxis, -1)));
49
+ return sameSize && sameAxes && near(next.frame.center, frame.center);
50
+ };
51
+ const isWhole = (run) => Math.abs(run.arc.sweep) >= FULL_TURN || (Math.abs(run.arc.sweep) > Math.PI && distance2(run.from, run.to) <= run.tolerance);
52
+ const continues = (run, next) => Math.sign(run.arc.sweep) === Math.sign(next.sweep) && !isWhole(run) && isSameConic(run, next);
53
+ const counterclockwise = (run) => {
54
+ const { arc } = run;
55
+ if (isWhole(run)) {
56
+ return { frame: arc.frame, start: 0, sweep: FULL_TURN };
57
+ }
58
+ const start = arc.sweep < 0 ? arc.start + arc.sweep : arc.start;
59
+ return { frame: arc.frame, start: angleWithinTurn(start, FULL_TURN), sweep: Math.abs(arc.sweep) };
60
+ };
61
+ const anglesWithinTurn = (dto) => ("startAngle" in dto ? Object.assign(Object.assign({}, dto), { startAngle: angleWithinTurn(dto.startAngle, FULL_TURN_DEGREES), endAngle: angleWithinTurn(dto.endAngle, FULL_TURN_DEGREES) }) : dto);
62
+ const cutName = (name) => {
63
+ const cut = name.slice(0, DXF_MAX_NAME_LENGTH);
64
+ return HIGH_SURROGATE_AT_END.test(cut) ? cut.slice(0, -1) : cut;
65
+ };
66
+ const isLayerCharacter = (character) => character.charCodeAt(0) >= FIRST_PRINTABLE_CODE && !FORBIDDEN_LAYER_CHARACTERS.test(character);
67
+ const unitOfDrawing = (paths) => {
68
+ try {
69
+ const parts = drawingPartsOf([{ paths }], "parts");
70
+ return patternUnitOf(drawingBounds(parts, 0, writerBudgetOf(parts)));
71
+ }
72
+ catch (_a) {
73
+ return undefined;
74
+ }
75
+ };
76
+ class SvgReading {
77
+ constructor(includeInvisible, precision) {
78
+ this.includeInvisible = includeInvisible;
79
+ this.precision = precision;
80
+ this.readings = styleReadings();
81
+ this.colors = new Map();
82
+ this.layers = new Map();
83
+ this.hidden = 0;
84
+ this.unreadColors = 0;
85
+ this.unmatchedDashes = 0;
86
+ this.renamedLayers = 0;
87
+ this.cutLayers = 0;
88
+ this.flatArcs = 0;
89
+ }
90
+ elementOf(element) {
91
+ if (element.style.hidden === true && !this.includeInvisible) {
92
+ this.hidden++;
93
+ return undefined;
94
+ }
95
+ const paths = element.subpaths.flatMap(subpath => this.pathsOf(subpath));
96
+ if (paths.length === 0) {
97
+ return undefined;
98
+ }
99
+ return { layer: this.layerOf(element.layer), color: this.colorOf(element), dashes: element.style.strokeDashArray, paths };
100
+ }
101
+ lineTypeOf(dashes, patternUnit) {
102
+ if (dashes === undefined || !dashes.some(dash => dash > 0)) {
103
+ return Inputs.IO.lineTypeEnum.continuous;
104
+ }
105
+ const lineType = lineTypeOfDashes(dashes, patternUnit);
106
+ if (lineType === undefined) {
107
+ this.unmatchedDashes++;
108
+ return Inputs.IO.lineTypeEnum.dashed;
109
+ }
110
+ return lineType;
111
+ }
112
+ warnings() {
113
+ const counted = [
114
+ [this.hidden, `${this.hidden} hidden ${plural(this.hidden, "element was", "elements were")} left out: set includeInvisible to read ${plural(this.hidden, "it", "them")}.`],
115
+ [this.unreadColors, `${this.unreadColors} ${plural(this.unreadColors, "element is", "elements are")} painted with a gradient, a pattern or a color other than a hex, rgb() or named color; ${plural(this.unreadColors, "it was", "they were")} read as black.`],
116
+ [this.unmatchedDashes, `${this.unmatchedDashes} ${plural(this.unmatchedDashes, "element is", "elements are")} dashed in a pattern no line type draws; ${plural(this.unmatchedDashes, "it was", "they were")} read as dashed.`],
117
+ [this.renamedLayers, `${this.renamedLayers} layer ${plural(this.renamedLayers, "name holds", "names hold")} characters a layer name may not (< > / \\ " : ; ? * | = \` or a control character); each was replaced by ${LAYER_STAND_IN}.`],
118
+ [this.cutLayers, `${this.cutLayers} layer ${plural(this.cutLayers, "name was", "names were")} longer than ${DXF_MAX_NAME_LENGTH} characters, the most a layer name holds, and ${plural(this.cutLayers, "was", "were")} cut to ${plural(this.cutLayers, "its", "their")} first ${DXF_MAX_NAME_LENGTH}.`],
119
+ [this.flatArcs, `${this.flatArcs} ${plural(this.flatArcs, "arc was", "arcs were")} left out: a transform flattens ${plural(this.flatArcs, "it", "them")} to no width.`],
120
+ ];
121
+ return counted.filter(([count]) => count > 0).map(([, warning]) => warning);
122
+ }
123
+ pathsOf(given) {
124
+ const subpath = transformSubpath(MIRROR_Y, given);
125
+ const segments = [];
126
+ let run;
127
+ let from = subpath.start;
128
+ for (const segment of subpath.segments) {
129
+ if (segment.type === "arc") {
130
+ const arc = conicArcOfEllipseArc(segment);
131
+ if (run !== undefined && continues(run, arc)) {
132
+ run = Object.assign(Object.assign({}, run), { arc: { frame: run.arc.frame, start: run.arc.start, sweep: run.arc.sweep + arc.sweep }, to: segment.to });
133
+ }
134
+ else {
135
+ this.pushConic(segments, run);
136
+ run = { arc, from, to: segment.to, tolerance: Math.max(SAME_CONIC_SHARE * length3(arc.frame.xAxis), this.precision) };
137
+ }
138
+ }
139
+ else {
140
+ this.pushConic(segments, run);
141
+ run = undefined;
142
+ segments.push(...curveDtos(from, segment));
143
+ }
144
+ from = segment.to;
145
+ }
146
+ this.pushConic(segments, run);
147
+ if (subpath.closed) {
148
+ segments.push(...lineDto(from, subpath.start));
149
+ }
150
+ return segments.length === 0 ? [] : [{ segments }];
151
+ }
152
+ pushConic(segments, run) {
153
+ if (run === undefined) {
154
+ return;
155
+ }
156
+ const arc = counterclockwise(run);
157
+ if (arc.sweep < LEAST_ARC_SWEEP) {
158
+ segments.push(...lineDto(run.from, run.to));
159
+ return;
160
+ }
161
+ const dto = conicSegmentDto({ kind: "conic", center: arc.frame.center, xAxis: arc.frame.xAxis, yAxis: arc.frame.yAxis, start: arc.start, sweep: arc.sweep });
162
+ if (dto === undefined) {
163
+ this.flatArcs++;
164
+ }
165
+ else {
166
+ segments.push(anglesWithinTurn(dto));
167
+ }
168
+ }
169
+ colorOf(element) {
170
+ const paint = [element.style.stroke, element.style.fill].find(value => value !== undefined && !this.readings.paintsNothing(value));
171
+ if (paint === undefined) {
172
+ return UNREAD_PAINT;
173
+ }
174
+ const hex = remembered(this.colors, paint, cssColorHex);
175
+ if (hex === undefined) {
176
+ this.unreadColors++;
177
+ }
178
+ return hex !== null && hex !== void 0 ? hex : UNREAD_PAINT;
179
+ }
180
+ layerOf(label) {
181
+ return label === undefined ? DEFAULT_LAYER : remembered(this.layers, label, given => this.layerNameOf(given));
182
+ }
183
+ layerNameOf(label) {
184
+ let name = label.trim();
185
+ if (name.length > DXF_MAX_NAME_LENGTH) {
186
+ this.cutLayers++;
187
+ name = cutName(name).trim();
188
+ }
189
+ const allowed = Array.from(name, character => (isLayerCharacter(character) ? character : LAYER_STAND_IN)).join("");
190
+ if (allowed !== name) {
191
+ this.renamedLayers++;
192
+ }
193
+ return allowed === "" ? DEFAULT_LAYER : allowed;
194
+ }
195
+ }
196
+ export const readSvgDrawing = (svg, options) => {
197
+ var _a, _b;
198
+ const scene = normalizeSvg(svg, options.unit === undefined ? {} : { unit: options.unit });
199
+ const userUnit = options.unit === undefined ? 1 : ((_a = scene.millimetresPerUserUnit) !== null && _a !== void 0 ? _a : millimetresPerUnitOf(options.unit)) / millimetresPerUnitOf(options.unit);
200
+ const reading = new SvgReading(options.includeInvisible, NUMBER_PRECISION_SLACK * userUnit);
201
+ const elements = [];
202
+ for (const element of scene.elements) {
203
+ const read = reading.elementOf(element);
204
+ if (read !== undefined) {
205
+ elements.push(read);
206
+ }
207
+ }
208
+ let patternUnit;
209
+ let measured = false;
210
+ const unitOfAll = () => {
211
+ if (!measured) {
212
+ measured = true;
213
+ patternUnit = unitOfDrawing(elements.flatMap(element => element.paths));
214
+ }
215
+ return patternUnit;
216
+ };
217
+ const parts = new Map();
218
+ for (const element of elements) {
219
+ const lineType = reading.lineTypeOf(element.dashes, unitOfAll);
220
+ const key = `${element.layer}\n${element.color}\n${lineType}`;
221
+ const part = (_b = parts.get(key)) !== null && _b !== void 0 ? _b : { layer: element.layer, color: element.color, lineType, paths: [] };
222
+ parts.set(key, part);
223
+ for (const path of element.paths) {
224
+ part.paths.push(path);
225
+ }
226
+ }
227
+ return {
228
+ parts: [...parts.values()],
229
+ unit: options.unit,
230
+ viewBox: scene.viewBox === undefined ? undefined : [...scene.viewBox],
231
+ warnings: scene.warnings.concat(reading.warnings()),
232
+ };
233
+ };
@@ -0,0 +1,54 @@
1
+ import type { SvgSubpath } from "./svg-path-types";
2
+ export type { SvgArcSegment, SvgCubicSegment, SvgLineSegment, SvgQuadSegment, SvgSegment, SvgSubpath, } from "./svg-path-types";
3
+ /**
4
+ * Presentation metadata resolved for an element through the style cascade.
5
+ * Values are normalized but kept close to SVG semantics so callers can use them
6
+ * directly (e.g. colour a 3D shape by `fill`, or use `strokeWidth` as extrusion
7
+ * or ribbon thickness, the "strength" of a stroke).
8
+ */
9
+ export interface SvgStyle {
10
+ /** Resolved fill colour (e.g. "#ff0000", "none"), or undefined if not set. */
11
+ fill?: string;
12
+ /** Resolved stroke colour, or undefined if not set. */
13
+ stroke?: string;
14
+ /** Stroke width in the root's user units, scaled by the element's transforms. */
15
+ strokeWidth?: number;
16
+ /** Dash and gap lengths of a dashed stroke, scaled as `strokeWidth` is; an odd list is repeated once, as SVG draws it. Left out for a solid stroke. */
17
+ strokeDashArray?: number[];
18
+ /** Opacity in [0,1], multiplied down from every ancestor that sets one. */
19
+ opacity?: number;
20
+ fillOpacity?: number;
21
+ strokeOpacity?: number;
22
+ /** SVG fill rule that governs how holes are determined. */
23
+ fillRule?: "nonzero" | "evenodd";
24
+ /** True when the element or an ancestor has display:none, or the element resolves to visibility:hidden. */
25
+ hidden?: boolean;
26
+ }
27
+ /** A single drawable SVG element, normalized to subpaths + resolved style. */
28
+ export interface SvgElement {
29
+ /** SVG tag name: "path" | "rect" | "circle" | "ellipse" | "line" | "polyline" | "polygon". */
30
+ tag: string;
31
+ /** `id` attribute, if any. */
32
+ id?: string;
33
+ /** `class` attribute, verbatim, if any. */
34
+ className?: string;
35
+ /** The Inkscape layer the element is drawn on: the label, else the id, of the nearest enclosing group Inkscape marks as a layer. */
36
+ layer?: string;
37
+ /** Resolved presentation style. */
38
+ style: SvgStyle;
39
+ /** Geometry as subpaths in SVG user space (transforms already baked in). */
40
+ subpaths: SvgSubpath[];
41
+ }
42
+ /** The whole parsed document reduced to drawable elements + frame info. */
43
+ export interface SvgScene {
44
+ elements: SvgElement[];
45
+ /** viewBox as [minX, minY, width, height] if present. */
46
+ viewBox?: [number, number, number, number];
47
+ /** The page's width and height in CSS pixels, 96 to the inch, when both are known, from `width` and `height` or from the view box. */
48
+ width?: number;
49
+ height?: number;
50
+ /** Millimetres one root user unit stands for horizontally, from the page size and the view box. */
51
+ millimetresPerUserUnit?: number;
52
+ /** Non-fatal issues encountered while parsing (unsupported features, etc.). */
53
+ warnings: string[];
54
+ }
@@ -0,0 +1,6 @@
1
+ import type { SvgSubpath } from "./svg-scene-types";
2
+ import type { LengthAxis, ViewportSize } from "./svg-numbers";
3
+ import type { XmlAttributes } from "./svg-xml";
4
+ export type LengthOf = (name: string, axis: LengthAxis) => number | undefined;
5
+ export declare function lengthsIn(attrs: XmlAttributes, viewport: ViewportSize): LengthOf;
6
+ export declare function shapeToSubpaths(tag: string, attrs: XmlAttributes, lengthOf?: LengthOf): SvgSubpath[];