@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,93 @@
1
+ import { BINARY_COUNT_BYTES, BINARY_HEADER_BYTES, BINARY_NUMBER_BYTES, BINARY_POINT_BYTES, BINARY_TRIANGLE_BYTES, COMPONENTS_OF_A_POINT, CORNERS_OF_A_TRIANGLE, NUMBERS_OF_A_TRIANGLE, POINTS_OF_A_FACET, SECOND_VERTEX_SLOT, THIRD_VERTEX_SLOT, } from "./stl.constants";
2
+ import { cross3, subtract3, unitVectorOf } from "../geometry/vectors";
3
+ const ASCII_FACET = new RegExp("facet\\s+normal\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+outer\\s+loop" +
4
+ "\\s+vertex\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+vertex\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+vertex\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)" +
5
+ "\\s+endloop\\s+endfacet", "gi");
6
+ const TEXT_OPENING = /^(?:)?\s*(?:solid|facet)/i;
7
+ const OPENING_BYTES = 80;
8
+ const CHARACTERS_AT_ONCE = 0x8000;
9
+ const FIRST_TRIANGLE_AT = BINARY_HEADER_BYTES + BINARY_COUNT_BYTES;
10
+ const negatedText = (value) => (value.startsWith("-") ? value.slice(1) : `-${value.startsWith("+") ? value.slice(1) : value}`);
11
+ const leavesEveryAxis = (axes) => !axes.mirrors && [axes.x, axes.y, axes.z].every(([component, negated], at) => component === at && !negated);
12
+ function countedTriangles(bytes) {
13
+ if (bytes.length < FIRST_TRIANGLE_AT) {
14
+ return 0;
15
+ }
16
+ return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(BINARY_HEADER_BYTES, true);
17
+ }
18
+ function heldTriangles(bytes) {
19
+ return Math.max(0, Math.floor((bytes.length - FIRST_TRIANGLE_AT) / BINARY_TRIANGLE_BYTES));
20
+ }
21
+ function charactersOf(bytes) {
22
+ let characters = "";
23
+ for (let at = 0; at < bytes.length; at += CHARACTERS_AT_ONCE) {
24
+ characters += String.fromCharCode(...bytes.subarray(at, at + CHARACTERS_AT_ONCE));
25
+ }
26
+ return characters;
27
+ }
28
+ function bytesOf(characters) {
29
+ const bytes = new Uint8Array(characters.length);
30
+ for (let at = 0; at < characters.length; at++) {
31
+ bytes[at] = characters.charCodeAt(at);
32
+ }
33
+ return bytes;
34
+ }
35
+ function isTextStl(bytes) {
36
+ const holdsItsCount = bytes.length >= FIRST_TRIANGLE_AT && countedTriangles(bytes) <= heldTriangles(bytes);
37
+ return !holdsItsCount && TEXT_OPENING.test(charactersOf(bytes.subarray(0, OPENING_BYTES)));
38
+ }
39
+ function mappedBinary(bytes, axes) {
40
+ const mapped = bytes.slice();
41
+ const source = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
42
+ const target = new DataView(mapped.buffer);
43
+ const count = Math.min(countedTriangles(bytes), heldTriangles(bytes));
44
+ const copyPoint = (from, to) => {
45
+ [axes.x, axes.y, axes.z].forEach(([component, negated], at) => {
46
+ const value = source.getFloat32(from + component * BINARY_NUMBER_BYTES, true);
47
+ target.setFloat32(to + at * BINARY_NUMBER_BYTES, negated ? -value : value, true);
48
+ });
49
+ };
50
+ for (let triangle = 0; triangle < count; triangle++) {
51
+ const at = FIRST_TRIANGLE_AT + triangle * BINARY_TRIANGLE_BYTES;
52
+ copyPoint(at, at);
53
+ copyPoint(at + BINARY_POINT_BYTES, at + BINARY_POINT_BYTES);
54
+ copyPoint(at + SECOND_VERTEX_SLOT * BINARY_POINT_BYTES, at + (axes.mirrors ? THIRD_VERTEX_SLOT : SECOND_VERTEX_SLOT) * BINARY_POINT_BYTES);
55
+ copyPoint(at + THIRD_VERTEX_SLOT * BINARY_POINT_BYTES, at + (axes.mirrors ? SECOND_VERTEX_SLOT : THIRD_VERTEX_SLOT) * BINARY_POINT_BYTES);
56
+ }
57
+ return mapped;
58
+ }
59
+ function mappedAscii(bytes, axes) {
60
+ const point = (values) => [axes.x, axes.y, axes.z].map(([component, negated]) => (negated ? negatedText(values[component]) : values[component])).join(" ");
61
+ const mapped = charactersOf(bytes).replace(ASCII_FACET, (_facet, ...numbers) => {
62
+ const [normal, a, b, c] = Array.from({ length: POINTS_OF_A_FACET }, (_point, at) => point(numbers.slice(at * COMPONENTS_OF_A_POINT, (at + 1) * COMPONENTS_OF_A_POINT)));
63
+ const [second, third] = axes.mirrors ? [c, b] : [b, c];
64
+ return `facet normal ${normal}\n outer loop\n vertex ${a}\n vertex ${second}\n vertex ${third}\n endloop\nendfacet`;
65
+ });
66
+ return bytesOf(mapped);
67
+ }
68
+ export function stlOfTriangles(parts) {
69
+ var _a;
70
+ const count = parts.reduce((sum, corners) => sum + Math.floor(corners.length / NUMBERS_OF_A_TRIANGLE), 0);
71
+ const bytes = new Uint8Array(BINARY_HEADER_BYTES + BINARY_COUNT_BYTES + count * BINARY_TRIANGLE_BYTES);
72
+ const view = new DataView(bytes.buffer);
73
+ view.setUint32(BINARY_HEADER_BYTES, count, true);
74
+ let at = BINARY_HEADER_BYTES + BINARY_COUNT_BYTES;
75
+ for (const corners of parts) {
76
+ for (let from = 0; from + NUMBERS_OF_A_TRIANGLE <= corners.length; from += NUMBERS_OF_A_TRIANGLE) {
77
+ const [a, b, c] = Array.from({ length: CORNERS_OF_A_TRIANGLE }, (_corner, corner) => {
78
+ const first = from + corner * COMPONENTS_OF_A_POINT;
79
+ return [corners[first], corners[first + 1], corners[first + 2]];
80
+ });
81
+ const normal = (_a = unitVectorOf(cross3(subtract3(b, a), subtract3(c, a)))) !== null && _a !== void 0 ? _a : [0, 0, 0];
82
+ [...normal, ...a, ...b, ...c].forEach((value, slot) => view.setFloat32(at + slot * BINARY_NUMBER_BYTES, value, true));
83
+ at += BINARY_TRIANGLE_BYTES;
84
+ }
85
+ }
86
+ return bytes;
87
+ }
88
+ export function stlWithAxes(bytes, axes) {
89
+ if (leavesEveryAxis(axes)) {
90
+ return bytes.slice();
91
+ }
92
+ return isTextStl(bytes) ? mappedAscii(bytes, axes) : mappedBinary(bytes, axes);
93
+ }
@@ -0,0 +1,48 @@
1
+ import type { WorkBudget } from "../work-budget";
2
+ import type { XmlNode } from "./svg-xml";
3
+ export interface CssDeclaration {
4
+ property: string;
5
+ value: string;
6
+ important: boolean;
7
+ }
8
+ type AttributeOperator = "" | "=" | "~=" | "|=" | "^=" | "$=" | "*=";
9
+ type Combinator = "descendant" | "child";
10
+ interface AttributeTest {
11
+ name: string;
12
+ operator: AttributeOperator;
13
+ value: string;
14
+ }
15
+ interface CompoundSelector {
16
+ tag?: string;
17
+ id?: string;
18
+ classes: string[];
19
+ attributes: AttributeTest[];
20
+ }
21
+ interface ComplexSelector {
22
+ compounds: CompoundSelector[];
23
+ combinators: Combinator[];
24
+ }
25
+ export interface CssRule {
26
+ selector: ComplexSelector;
27
+ specificity: number;
28
+ order: number;
29
+ declarations: CssDeclaration[];
30
+ }
31
+ export interface Stylesheet {
32
+ rules: CssRule[];
33
+ unsupported: string[];
34
+ }
35
+ export interface CssMatch {
36
+ declaration: CssDeclaration;
37
+ specificity: number;
38
+ order: number;
39
+ }
40
+ export type CssMatches = Map<XmlNode, CssMatch[]>;
41
+ export interface CssMatching {
42
+ matches: CssMatches;
43
+ exhausted: boolean;
44
+ }
45
+ export declare function parseDeclarations(text: string | undefined): CssDeclaration[];
46
+ export declare function parseStylesheets(texts: string[], properties: ReadonlySet<string>): Stylesheet;
47
+ export declare function matchStylesheet(root: XmlNode, sheet: Stylesheet, budget: WorkBudget): CssMatching;
48
+ export {};
@@ -0,0 +1,444 @@
1
+ import { MAX_AT_RULE_NESTING, SPECIFICITY_CLASS, SPECIFICITY_ID, SPECIFICITY_TYPE, STEPS_PER_ANCESTOR_TABLE, STEPS_PER_MATCHED_DECLARATION, } from "./svg-reader.constants";
2
+ const COMMENT_OPEN = "/*";
3
+ const COMMENT_CLOSE = "*/";
4
+ const UNDECIDED = 0;
5
+ const DECIDED_YES = 1;
6
+ const DECIDED_NO = 2;
7
+ const IMPORTANT = /!\s*important\s*$/i;
8
+ const NOT_SPACE = /\S/g;
9
+ const IDENTIFIER = /^-?[_a-zA-Z\u00a0-\uffff][\w\u00a0-\uffff-]*/;
10
+ const ATTRIBUTE = /^\[\s*([^\s~|^$*=\]]+)\s*(?:([~|^$*]?=)\s*(?:"([^"]*)"|'([^']*)'|([^\s\]]+)))?\s*\]/;
11
+ const BLOCK_AT_RULES_WITH_RULES = new Set(["media", "supports", "layer", "document"]);
12
+ function splitTopLevel(text, separator) {
13
+ const parts = [];
14
+ let depth = 0;
15
+ let quote = "";
16
+ let start = 0;
17
+ for (let i = 0; i < text.length; i++) {
18
+ const ch = text[i];
19
+ if (quote) {
20
+ if (ch === quote) {
21
+ quote = "";
22
+ }
23
+ }
24
+ else if (ch === "\"" || ch === "'") {
25
+ quote = ch;
26
+ }
27
+ else if (ch === "(" || ch === "[") {
28
+ depth++;
29
+ }
30
+ else if (ch === ")" || ch === "]") {
31
+ depth = Math.max(0, depth - 1);
32
+ }
33
+ else if (ch === separator && depth === 0) {
34
+ parts.push(text.slice(start, i));
35
+ start = i + 1;
36
+ }
37
+ }
38
+ parts.push(text.slice(start));
39
+ return parts;
40
+ }
41
+ function withoutComments(text) {
42
+ const kept = [];
43
+ let at = 0;
44
+ for (let open = text.indexOf(COMMENT_OPEN); open !== -1; open = text.indexOf(COMMENT_OPEN, at)) {
45
+ kept.push(text.slice(at, open));
46
+ const close = text.indexOf(COMMENT_CLOSE, open + COMMENT_OPEN.length);
47
+ if (close === -1) {
48
+ return kept.join("");
49
+ }
50
+ at = close + COMMENT_CLOSE.length;
51
+ }
52
+ kept.push(text.slice(at));
53
+ return kept.join("");
54
+ }
55
+ export function parseDeclarations(text) {
56
+ if (!text) {
57
+ return [];
58
+ }
59
+ const declarations = [];
60
+ for (const part of splitTopLevel(withoutComments(text), ";")) {
61
+ const colon = part.indexOf(":");
62
+ if (colon === -1) {
63
+ continue;
64
+ }
65
+ const property = part.slice(0, colon).trim().toLowerCase();
66
+ let value = part.slice(colon + 1).trim();
67
+ const important = IMPORTANT.test(value);
68
+ if (important) {
69
+ value = value.replace(IMPORTANT, "").trim();
70
+ }
71
+ if (property && value) {
72
+ declarations.push({ property, value, important });
73
+ }
74
+ }
75
+ return declarations;
76
+ }
77
+ function strongestDeclarations(text, properties) {
78
+ const strongest = new Map();
79
+ for (const declaration of parseDeclarations(text)) {
80
+ if (!properties.has(declaration.property)) {
81
+ continue;
82
+ }
83
+ const current = strongest.get(declaration.property);
84
+ if (!current || declaration.important || !current.important) {
85
+ strongest.set(declaration.property, declaration);
86
+ }
87
+ }
88
+ return [...strongest.values()];
89
+ }
90
+ function compileCompound(text) {
91
+ var _a, _b, _c, _d;
92
+ const compound = { classes: [], attributes: [] };
93
+ let rest = text;
94
+ if (rest.startsWith("*")) {
95
+ rest = rest.slice(1);
96
+ }
97
+ else {
98
+ const tag = IDENTIFIER.exec(rest);
99
+ if (tag) {
100
+ compound.tag = tag[0].toLowerCase();
101
+ rest = rest.slice(tag[0].length);
102
+ }
103
+ }
104
+ while (rest.length > 0) {
105
+ const lead = rest[0];
106
+ if (lead === "#" || lead === ".") {
107
+ const name = IDENTIFIER.exec(rest.slice(1));
108
+ if (!name) {
109
+ return undefined;
110
+ }
111
+ if (lead === "#") {
112
+ compound.id = name[0];
113
+ }
114
+ else {
115
+ compound.classes.push(name[0]);
116
+ }
117
+ rest = rest.slice(1 + name[0].length);
118
+ }
119
+ else if (lead === "[") {
120
+ const attribute = ATTRIBUTE.exec(rest);
121
+ if (!attribute) {
122
+ return undefined;
123
+ }
124
+ const operator = ((_a = attribute[2]) !== null && _a !== void 0 ? _a : "");
125
+ compound.attributes.push({ name: attribute[1], operator, value: (_d = (_c = (_b = attribute[3]) !== null && _b !== void 0 ? _b : attribute[4]) !== null && _c !== void 0 ? _c : attribute[5]) !== null && _d !== void 0 ? _d : "" });
126
+ rest = rest.slice(attribute[0].length);
127
+ }
128
+ else {
129
+ return undefined;
130
+ }
131
+ }
132
+ return compound;
133
+ }
134
+ function selectorTokens(text) {
135
+ const tokens = [];
136
+ for (const word of text.trim().split(/\s+/)) {
137
+ word.split(">").forEach((part, k) => {
138
+ if (k > 0) {
139
+ tokens.push(">");
140
+ }
141
+ if (part.length > 0) {
142
+ tokens.push(part);
143
+ }
144
+ });
145
+ }
146
+ return tokens;
147
+ }
148
+ function compileSelector(text) {
149
+ const tokens = selectorTokens(text);
150
+ const compounds = [];
151
+ const combinators = [];
152
+ let pending;
153
+ for (const token of tokens) {
154
+ if (token === ">") {
155
+ if (compounds.length === 0 || pending === "child") {
156
+ return undefined;
157
+ }
158
+ pending = "child";
159
+ continue;
160
+ }
161
+ const compound = compileCompound(token);
162
+ if (!compound) {
163
+ return undefined;
164
+ }
165
+ if (compounds.length > 0) {
166
+ combinators.push(pending !== null && pending !== void 0 ? pending : "descendant");
167
+ }
168
+ compounds.push(compound);
169
+ pending = undefined;
170
+ }
171
+ return compounds.length === 0 || pending !== undefined ? undefined : { compounds, combinators };
172
+ }
173
+ function specificityOf(selector) {
174
+ return selector.compounds.reduce((sum, c) => sum
175
+ + (c.id === undefined ? 0 : SPECIFICITY_ID)
176
+ + (c.classes.length + c.attributes.length) * SPECIFICITY_CLASS
177
+ + (c.tag === undefined ? 0 : SPECIFICITY_TYPE), 0);
178
+ }
179
+ function firstNotSpace(text, from) {
180
+ var _a, _b;
181
+ NOT_SPACE.lastIndex = from;
182
+ return (_b = (_a = NOT_SPACE.exec(text)) === null || _a === void 0 ? void 0 : _a.index) !== null && _b !== void 0 ? _b : text.length;
183
+ }
184
+ function closingBrace(text, open) {
185
+ let depth = 0;
186
+ for (let i = open; i < text.length; i++) {
187
+ if (text[i] === "{") {
188
+ depth++;
189
+ }
190
+ if (text[i] === "}") {
191
+ depth--;
192
+ if (depth === 0) {
193
+ return i;
194
+ }
195
+ }
196
+ }
197
+ return text.length;
198
+ }
199
+ function collectRules(text, sheet, order, nesting, properties) {
200
+ var _a, _b, _c;
201
+ let at = 0;
202
+ let open = text.indexOf("{");
203
+ let semicolon = text.indexOf(";");
204
+ while (at < text.length) {
205
+ if (open !== -1 && open < at) {
206
+ open = text.indexOf("{", at);
207
+ }
208
+ if (semicolon !== -1 && semicolon < at) {
209
+ semicolon = text.indexOf(";", at);
210
+ }
211
+ const first = firstNotSpace(text, at);
212
+ const endsBeforeBlock = semicolon !== -1 && (open === -1 || semicolon < open);
213
+ if (text[first] === "@" && endsBeforeBlock) {
214
+ const statement = text.slice(first, semicolon).trim();
215
+ if (!/^@charset\b/i.test(statement)) {
216
+ sheet.unsupported.push(statement);
217
+ }
218
+ at = semicolon + 1;
219
+ continue;
220
+ }
221
+ if (open === -1) {
222
+ return;
223
+ }
224
+ const prelude = text.slice(first, open).trim();
225
+ const close = closingBrace(text, open);
226
+ const body = text.slice(open + 1, close);
227
+ at = close + 1;
228
+ if (prelude.startsWith("@")) {
229
+ const name = (_c = (_b = (_a = /^@([\w-]+)/.exec(prelude)) === null || _a === void 0 ? void 0 : _a[1]) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== null && _c !== void 0 ? _c : "";
230
+ if (BLOCK_AT_RULES_WITH_RULES.has(name)) {
231
+ if (nesting < MAX_AT_RULE_NESTING) {
232
+ collectRules(body, sheet, order, nesting + 1, properties);
233
+ }
234
+ else {
235
+ sheet.unsupported.push(prelude);
236
+ }
237
+ }
238
+ continue;
239
+ }
240
+ const declarations = strongestDeclarations(body, properties);
241
+ for (const selectorText of splitTopLevel(prelude, ",")) {
242
+ const selector = compileSelector(selectorText);
243
+ if (!selector) {
244
+ if (selectorText.trim()) {
245
+ sheet.unsupported.push(selectorText.trim());
246
+ }
247
+ continue;
248
+ }
249
+ sheet.rules.push({ selector, specificity: specificityOf(selector), order: order.next++, declarations });
250
+ }
251
+ }
252
+ }
253
+ export function parseStylesheets(texts, properties) {
254
+ const sheet = { rules: [], unsupported: [] };
255
+ const order = { next: 0 };
256
+ texts.forEach((text) => collectRules(withoutComments(text), sheet, order, 0, properties));
257
+ return sheet;
258
+ }
259
+ function wordLists() {
260
+ const known = new Map();
261
+ return (node, attribute) => {
262
+ var _a, _b, _c;
263
+ const ofNode = (_a = known.get(node)) !== null && _a !== void 0 ? _a : new Map();
264
+ known.set(node, ofNode);
265
+ const listed = (_b = ofNode.get(attribute)) !== null && _b !== void 0 ? _b : new Set(((_c = node.attrs[attribute]) !== null && _c !== void 0 ? _c : "").split(/\s+/).filter((word) => word.length > 0));
266
+ ofNode.set(attribute, listed);
267
+ return listed;
268
+ };
269
+ }
270
+ function comparedCharacters(test, actual) {
271
+ if (test.operator === "" || test.operator === "~=") {
272
+ return 0;
273
+ }
274
+ return test.operator === "*=" ? actual.length + test.value.length : test.value.length;
275
+ }
276
+ function attributeMatches(test, node, words, budget) {
277
+ const actual = node.attrs[test.name];
278
+ if (actual === undefined) {
279
+ return false;
280
+ }
281
+ if (!budget.spend(comparedCharacters(test, actual))) {
282
+ return false;
283
+ }
284
+ switch (test.operator) {
285
+ case "": return true;
286
+ case "=": return actual === test.value;
287
+ case "~=": return words(node, test.name).has(test.value);
288
+ case "|=": return actual === test.value || actual.startsWith(`${test.value}-`);
289
+ case "^=": return test.value !== "" && actual.startsWith(test.value);
290
+ case "$=": return test.value !== "" && actual.endsWith(test.value);
291
+ default: return test.value !== "" && actual.includes(test.value);
292
+ }
293
+ }
294
+ function compoundMatches(compound, node, words, budget) {
295
+ if (!budget.spend(1 + compound.classes.length + compound.attributes.length)) {
296
+ return false;
297
+ }
298
+ if (compound.tag !== undefined && compound.tag !== node.tag) {
299
+ return false;
300
+ }
301
+ if (compound.id !== undefined && node.attrs["id"] !== compound.id) {
302
+ return false;
303
+ }
304
+ if (compound.classes.length > 0) {
305
+ const own = words(node, "class");
306
+ if (!compound.classes.every((c) => own.has(c))) {
307
+ return false;
308
+ }
309
+ }
310
+ return compound.attributes.every((test) => attributeMatches(test, node, words, budget));
311
+ }
312
+ function fitsAncestors(selector, ancestors, words, budget) {
313
+ const { compounds, combinators } = selector;
314
+ const last = compounds.length - 1;
315
+ if (last === 0) {
316
+ return true;
317
+ }
318
+ if (last > ancestors.length) {
319
+ return false;
320
+ }
321
+ const width = ancestors.length + 1;
322
+ if (!budget.spend(STEPS_PER_ANCESTOR_TABLE + last * width)) {
323
+ return false;
324
+ }
325
+ const placedAt = new Uint8Array(last * width);
326
+ const aboveAt = new Uint8Array(last * width);
327
+ const remembered = (table, at, decide) => {
328
+ if (table[at] === UNDECIDED) {
329
+ table[at] = decide() ? DECIDED_YES : DECIDED_NO;
330
+ }
331
+ return table[at] === DECIDED_YES;
332
+ };
333
+ const placed = (k, depth) => k <= depth
334
+ && remembered(placedAt, k * width + depth, () => compoundMatches(compounds[k], ancestors[depth], words, budget) && (k === 0 || fitsAbove(k, depth)));
335
+ const someAbove = (k, depth) => k < depth
336
+ && remembered(aboveAt, k * width + depth, () => placed(k, depth - 1) || someAbove(k, depth - 1));
337
+ const fitsAbove = (k, depth) => combinators[k - 1] === "child"
338
+ ? depth > 0 && placed(k - 1, depth - 1)
339
+ : someAbove(k - 1, depth);
340
+ return fitsAbove(last, ancestors.length);
341
+ }
342
+ function indexRules(rules) {
343
+ const index = { byId: new Map(), byClass: new Map(), byTag: new Map(), universal: [] };
344
+ const add = (map, key, rule) => {
345
+ const list = map.get(key);
346
+ if (list) {
347
+ list.push(rule);
348
+ }
349
+ else {
350
+ map.set(key, [rule]);
351
+ }
352
+ };
353
+ for (const rule of rules) {
354
+ const subject = rule.selector.compounds[rule.selector.compounds.length - 1];
355
+ if (subject.id !== undefined) {
356
+ add(index.byId, subject.id, rule);
357
+ }
358
+ else if (subject.classes.length > 0) {
359
+ add(index.byClass, subject.classes[0], rule);
360
+ }
361
+ else if (subject.tag !== undefined) {
362
+ add(index.byTag, subject.tag, rule);
363
+ }
364
+ else {
365
+ index.universal.push(rule);
366
+ }
367
+ }
368
+ return index;
369
+ }
370
+ function candidateLists(index, node, words) {
371
+ const lists = [index.universal];
372
+ const id = node.attrs["id"];
373
+ const byId = id === undefined ? undefined : index.byId.get(id);
374
+ if (byId) {
375
+ lists.push(byId);
376
+ }
377
+ for (const word of words(node, "class")) {
378
+ const byClass = index.byClass.get(word);
379
+ if (byClass) {
380
+ lists.push(byClass);
381
+ }
382
+ }
383
+ const byTag = index.byTag.get(node.tag);
384
+ if (byTag) {
385
+ lists.push(byTag);
386
+ }
387
+ return lists;
388
+ }
389
+ const strongerInSheet = (a, b) => a.declaration.important !== b.declaration.important
390
+ ? a.declaration.important
391
+ : a.specificity !== b.specificity ? a.specificity > b.specificity : a.order > b.order;
392
+ export function matchStylesheet(root, sheet, budget) {
393
+ const matches = new Map();
394
+ if (sheet.rules.length === 0) {
395
+ return { matches, exhausted: false };
396
+ }
397
+ const index = indexRules(sheet.rules);
398
+ const words = wordLists();
399
+ const ancestors = [];
400
+ const fits = (rule, underParent) => {
401
+ const known = underParent.get(rule);
402
+ if (known !== undefined) {
403
+ return known;
404
+ }
405
+ const decided = fitsAncestors(rule.selector, ancestors, words, budget);
406
+ underParent.set(rule, decided);
407
+ return decided;
408
+ };
409
+ const visit = (node, underParent) => {
410
+ const winners = new Map();
411
+ for (const rules of candidateLists(index, node, words)) {
412
+ for (const rule of rules) {
413
+ const compounds = rule.selector.compounds;
414
+ if (compoundMatches(compounds[compounds.length - 1], node, words, budget) && fits(rule, underParent)
415
+ && budget.spend(STEPS_PER_MATCHED_DECLARATION * rule.declarations.length)) {
416
+ rule.declarations.forEach((declaration) => {
417
+ const match = { declaration, specificity: rule.specificity, order: rule.order };
418
+ const current = winners.get(declaration.property);
419
+ if (!current || strongerInSheet(match, current)) {
420
+ winners.set(declaration.property, match);
421
+ }
422
+ });
423
+ }
424
+ if (budget.exhausted) {
425
+ return;
426
+ }
427
+ }
428
+ }
429
+ if (winners.size > 0) {
430
+ matches.set(node, [...winners.values()]);
431
+ }
432
+ const underNode = new Map();
433
+ ancestors.push(node);
434
+ for (const child of node.children) {
435
+ visit(child, underNode);
436
+ if (budget.exhausted) {
437
+ break;
438
+ }
439
+ }
440
+ ancestors.pop();
441
+ };
442
+ visit(root, new Map());
443
+ return { matches, exhausted: budget.exhausted };
444
+ }
@@ -0,0 +1,2 @@
1
+ import * as Inputs from "../../../../inputs";
2
+ export declare const lineTypeOfDashes: (dashes: readonly number[], patternUnit: () => number | undefined) => Inputs.IO.lineTypeEnum | undefined;
@@ -0,0 +1,35 @@
1
+ import * as Inputs from "../../../../inputs";
2
+ import { LINE_TYPE_PATTERNS } from "../drawing/drawing.constants";
3
+ import { DASH_MATCH_SHARE } from "./svg-reading.constants";
4
+ const DASHED_TYPES = Object.values(Inputs.IO.lineTypeEnum).filter(lineType => LINE_TYPE_PATTERNS[lineType].elements.length > 0);
5
+ const totalOf = (lengths) => lengths.reduce((total, length) => total + length, 0);
6
+ const dashMatches = (dashes) => {
7
+ const total = totalOf(dashes);
8
+ const matches = [];
9
+ for (const lineType of DASHED_TYPES) {
10
+ const pattern = LINE_TYPE_PATTERNS[lineType].elements.map(element => Math.abs(element));
11
+ if (dashes.length % pattern.length !== 0) {
12
+ continue;
13
+ }
14
+ const period = totalOf(pattern);
15
+ const unit = total * pattern.length / (period * dashes.length);
16
+ const slack = DASH_MATCH_SHARE * unit * period;
17
+ if (unit > 0 && dashes.every((dash, at) => Math.abs(dash - unit * pattern[at % pattern.length]) <= slack)) {
18
+ matches.push({ lineType, unit });
19
+ }
20
+ }
21
+ return matches;
22
+ };
23
+ export const lineTypeOfDashes = (dashes, patternUnit) => {
24
+ var _a;
25
+ const matches = dashMatches(dashes);
26
+ if (matches.length <= 1) {
27
+ return (_a = matches[0]) === null || _a === void 0 ? void 0 : _a.lineType;
28
+ }
29
+ const unit = patternUnit();
30
+ if (unit === undefined || !(unit > 0)) {
31
+ return matches[0].lineType;
32
+ }
33
+ const offBy = (match) => Math.abs(Math.log(match.unit / unit));
34
+ return matches.reduce((best, match) => (offBy(match) < offBy(best) ? match : best)).lineType;
35
+ };
@@ -0,0 +1,26 @@
1
+ import type { AffineMatrix2 } from "../../geometry/affine2";
2
+ import type { ViewportSize } from "./svg-numbers";
3
+ import type { XmlNode } from "./svg-xml";
4
+ export type ViewBox = [number, number, number, number];
5
+ export interface DocumentFrame {
6
+ viewBox?: ViewBox;
7
+ width?: number;
8
+ height?: number;
9
+ pageTransform: AffineMatrix2;
10
+ pageInverse: AffineMatrix2;
11
+ contentViewport: ViewportSize;
12
+ millimetresPerUserUnit: number;
13
+ }
14
+ type AxisAlignment = "min" | "mid" | "max";
15
+ export interface AspectRatio {
16
+ stretch: boolean;
17
+ x: AxisAlignment;
18
+ y: AxisAlignment;
19
+ slice: boolean;
20
+ }
21
+ export declare function parseViewBox(text: string | undefined): ViewBox | undefined;
22
+ export declare function parseAspectRatio(preserveAspectRatio: string | undefined): AspectRatio;
23
+ export declare function fittedViewBox(box: ViewBox, width: number, height: number, aspect: AspectRatio): AffineMatrix2;
24
+ export declare function viewBoxTransform(box: ViewBox, width: number, height: number, preserveAspectRatio: string | undefined): AffineMatrix2;
25
+ export declare function documentFrame(root: XmlNode): DocumentFrame;
26
+ export {};