@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.
- package/lib/api/GlobalCDNProvider.js +1 -1
- package/lib/api/inputs/base-inputs.d.ts +36 -7
- package/lib/api/inputs/base-inputs.js +10 -0
- package/lib/api/inputs/color-inputs.d.ts +54 -1
- package/lib/api/inputs/color-inputs.js +73 -1
- package/lib/api/inputs/frame-inputs.d.ts +2 -1
- package/lib/api/inputs/frame-inputs.js +2 -1
- package/lib/api/inputs/io-inputs.d.ts +527 -59
- package/lib/api/inputs/io-inputs.js +432 -40
- package/lib/api/inputs/line-inputs.d.ts +17 -0
- package/lib/api/inputs/line-inputs.js +15 -0
- package/lib/api/inputs/math-inputs.d.ts +62 -0
- package/lib/api/inputs/math-inputs.js +82 -0
- package/lib/api/inputs/mesh-inputs.d.ts +16 -0
- package/lib/api/inputs/mesh-inputs.js +14 -0
- package/lib/api/inputs/transforms-inputs.d.ts +76 -0
- package/lib/api/inputs/transforms-inputs.js +85 -0
- package/lib/api/kernel-calls/index.d.ts +1 -0
- package/lib/api/kernel-calls/index.js +1 -0
- package/lib/api/kernel-calls/settings.d.ts +82 -0
- package/lib/api/kernel-calls/settings.js +123 -0
- package/lib/api/recipes/surface-mesh.d.ts +2 -1
- package/lib/api/recipes/surface-mesh.js +5 -3
- package/lib/api/resolved-inputs/index.d.ts +32 -2
- package/lib/api/services/base-bitbybit.d.ts +12 -0
- package/lib/api/services/base-bitbybit.js +19 -3
- package/lib/api/services/color.d.ts +36 -2
- package/lib/api/services/color.js +60 -11
- package/lib/api/services/helpers/color/color-forms.constants.d.ts +10 -0
- package/lib/api/services/helpers/color/color-forms.constants.js +10 -0
- package/lib/api/services/helpers/color/color-forms.d.ts +3 -0
- package/lib/api/services/helpers/color/color-forms.js +27 -0
- package/lib/api/services/helpers/color/css-colors.constants.d.ts +9 -0
- package/lib/api/services/helpers/color/css-colors.constants.js +158 -0
- package/lib/api/services/helpers/color/css-colors.d.ts +1 -0
- package/lib/api/services/helpers/color/css-colors.js +61 -0
- package/lib/api/services/helpers/curves/bezier-pieces.constants.d.ts +5 -0
- package/lib/api/services/helpers/curves/bezier-pieces.constants.js +5 -0
- package/lib/api/services/helpers/curves/bezier-pieces.d.ts +24 -0
- package/lib/api/services/helpers/curves/bezier-pieces.js +149 -0
- package/lib/api/services/helpers/curves/conic-arcs.constants.d.ts +5 -0
- package/lib/api/services/helpers/curves/conic-arcs.constants.js +5 -0
- package/lib/api/services/helpers/curves/conic-arcs.d.ts +24 -0
- package/lib/api/services/helpers/curves/conic-arcs.js +85 -0
- package/lib/api/services/helpers/curves/nurbs-forms.d.ts +20 -0
- package/lib/api/services/helpers/curves/nurbs-forms.js +112 -0
- package/lib/api/services/helpers/curves/nurbs.d.ts +4 -0
- package/lib/api/services/helpers/curves/nurbs.js +10 -9
- package/lib/api/services/helpers/files/drawing/drawing-budget.d.ts +3 -0
- package/lib/api/services/helpers/files/drawing/drawing-budget.js +24 -0
- package/lib/api/services/helpers/files/drawing/drawing-geometry.d.ts +18 -0
- package/lib/api/services/helpers/files/drawing/drawing-geometry.js +84 -0
- package/lib/api/services/helpers/files/drawing/drawing-model.d.ts +5 -0
- package/lib/api/services/helpers/files/drawing/drawing-model.js +252 -0
- package/lib/api/services/helpers/files/drawing/drawing-types.d.ts +60 -0
- package/lib/api/services/helpers/files/drawing/drawing-types.js +1 -0
- package/lib/api/services/helpers/files/drawing/drawing.constants.d.ts +28 -0
- package/lib/api/services/helpers/files/drawing/drawing.constants.js +29 -0
- package/lib/api/services/helpers/files/dxf/dxf-colors.d.ts +5 -0
- package/lib/api/services/helpers/files/dxf/dxf-colors.js +45 -0
- package/lib/api/services/helpers/files/dxf/dxf-entities.d.ts +17 -0
- package/lib/api/services/helpers/files/dxf/dxf-entities.js +193 -0
- package/lib/api/services/helpers/files/dxf/dxf-file-text.d.ts +1 -0
- package/lib/api/services/helpers/files/dxf/dxf-file-text.js +50 -0
- package/lib/api/services/helpers/files/dxf/dxf-generator.d.ts +2 -36
- package/lib/api/services/helpers/files/dxf/dxf-generator.js +81 -523
- package/lib/api/services/helpers/files/dxf/dxf-reader-dtos.d.ts +6 -0
- package/lib/api/services/helpers/files/dxf/dxf-reader-dtos.js +81 -0
- package/lib/api/services/helpers/files/dxf/dxf-reader-geometry.d.ts +21 -0
- package/lib/api/services/helpers/files/dxf/dxf-reader-geometry.js +67 -0
- package/lib/api/services/helpers/files/dxf/dxf-reader-types.d.ts +69 -0
- package/lib/api/services/helpers/files/dxf/dxf-reader-types.js +1 -0
- package/lib/api/services/helpers/files/dxf/dxf-reader.constants.d.ts +71 -0
- package/lib/api/services/helpers/files/dxf/dxf-reader.constants.js +78 -0
- package/lib/api/services/helpers/files/dxf/dxf-reader.d.ts +2 -0
- package/lib/api/services/helpers/files/dxf/dxf-reader.js +556 -0
- package/lib/api/services/helpers/files/dxf/dxf-tables.d.ts +27 -0
- package/lib/api/services/helpers/files/dxf/dxf-tables.js +247 -0
- package/lib/api/services/helpers/files/dxf/dxf-text.d.ts +26 -0
- package/lib/api/services/helpers/files/dxf/dxf-text.js +97 -0
- package/lib/api/services/helpers/files/dxf/dxf-tokens.d.ts +9 -0
- package/lib/api/services/helpers/files/dxf/dxf-tokens.js +65 -0
- package/lib/api/services/helpers/files/dxf/dxf.constants.d.ts +32 -1
- package/lib/api/services/helpers/files/dxf/dxf.constants.js +34 -1
- package/lib/api/services/helpers/files/file-bytes.d.ts +1 -0
- package/lib/api/services/helpers/files/file-bytes.js +15 -0
- package/lib/api/services/helpers/files/ground-axes.d.ts +2 -0
- package/lib/api/services/helpers/files/ground-axes.js +4 -0
- package/lib/api/services/helpers/files/mesh-file-axes.d.ts +7 -0
- package/lib/api/services/helpers/files/mesh-file-axes.js +17 -0
- package/lib/api/services/helpers/files/stl-axes.d.ts +4 -0
- package/lib/api/services/helpers/files/stl-axes.js +22 -0
- package/lib/api/services/helpers/files/stl-types.d.ts +7 -0
- package/lib/api/services/helpers/files/stl-types.js +1 -0
- package/lib/api/services/helpers/files/stl.constants.d.ts +11 -0
- package/lib/api/services/helpers/files/stl.constants.js +11 -0
- package/lib/api/services/helpers/files/stl.d.ts +3 -0
- package/lib/api/services/helpers/files/stl.js +93 -0
- package/lib/api/services/helpers/files/svg/svg-css.d.ts +48 -0
- package/lib/api/services/helpers/files/svg/svg-css.js +444 -0
- package/lib/api/services/helpers/files/svg/svg-dash-patterns.d.ts +2 -0
- package/lib/api/services/helpers/files/svg/svg-dash-patterns.js +35 -0
- package/lib/api/services/helpers/files/svg/svg-document.d.ts +26 -0
- package/lib/api/services/helpers/files/svg/svg-document.js +89 -0
- package/lib/api/services/helpers/files/svg/svg-memo.d.ts +1 -0
- package/lib/api/services/helpers/files/svg/svg-memo.js +8 -0
- package/lib/api/services/helpers/files/svg/svg-normalizer.d.ts +6 -0
- package/lib/api/services/helpers/files/svg/svg-normalizer.js +433 -0
- package/lib/api/services/helpers/files/svg/svg-numbers.d.ts +15 -0
- package/lib/api/services/helpers/files/svg/svg-numbers.js +61 -0
- package/lib/api/services/helpers/files/svg/svg-path-data.constants.d.ts +8 -0
- package/lib/api/services/helpers/files/svg/svg-path-data.constants.js +10 -0
- package/lib/api/services/helpers/files/svg/svg-path-data.d.ts +9 -0
- package/lib/api/services/helpers/files/svg/svg-path-data.js +203 -0
- package/lib/api/services/helpers/files/svg/svg-path-types.d.ts +33 -0
- package/lib/api/services/helpers/files/svg/svg-path-types.js +1 -0
- package/lib/api/services/helpers/files/svg/svg-reader.constants.d.ts +35 -0
- package/lib/api/services/helpers/files/svg/svg-reader.constants.js +35 -0
- package/lib/api/services/helpers/files/svg/svg-reading.constants.d.ts +9 -0
- package/lib/api/services/helpers/files/svg/svg-reading.constants.js +9 -0
- package/lib/api/services/helpers/files/svg/svg-reading.d.ts +6 -0
- package/lib/api/services/helpers/files/svg/svg-reading.js +233 -0
- package/lib/api/services/helpers/files/svg/svg-scene-types.d.ts +54 -0
- package/lib/api/services/helpers/files/svg/svg-scene-types.js +1 -0
- package/lib/api/services/helpers/files/svg/svg-shapes.d.ts +6 -0
- package/lib/api/services/helpers/files/svg/svg-shapes.js +102 -0
- package/lib/api/services/helpers/files/svg/svg-strokes.d.ts +38 -0
- package/lib/api/services/helpers/files/svg/svg-strokes.js +108 -0
- package/lib/api/services/helpers/files/svg/svg-style.d.ts +26 -0
- package/lib/api/services/helpers/files/svg/svg-style.js +211 -0
- package/lib/api/services/helpers/files/svg/svg-transform.d.ts +10 -0
- package/lib/api/services/helpers/files/svg/svg-transform.js +76 -0
- package/lib/api/services/helpers/files/svg/svg-writer.d.ts +16 -0
- package/lib/api/services/helpers/files/svg/svg-writer.js +153 -0
- package/lib/api/services/helpers/files/svg/svg-xml.d.ts +11 -0
- package/lib/api/services/helpers/files/svg/svg-xml.js +322 -0
- package/lib/api/services/helpers/files/svg/svg.constants.d.ts +13 -0
- package/lib/api/services/helpers/files/svg/svg.constants.js +13 -0
- package/lib/api/services/helpers/files/work-budget.d.ts +11 -0
- package/lib/api/services/helpers/files/work-budget.js +26 -0
- package/lib/api/services/helpers/geometry/affine2.d.ts +8 -0
- package/lib/api/services/helpers/geometry/affine2.js +18 -0
- package/lib/api/services/helpers/geometry/ellipse-arcs.d.ts +14 -0
- package/lib/api/services/helpers/geometry/ellipse-arcs.js +96 -0
- package/lib/api/services/helpers/geometry/frame-axes.d.ts +3 -1
- package/lib/api/services/helpers/geometry/polygon-nesting.constants.d.ts +6 -0
- package/lib/api/services/helpers/geometry/polygon-nesting.constants.js +6 -0
- package/lib/api/services/helpers/geometry/polygon-nesting.d.ts +31 -0
- package/lib/api/services/helpers/geometry/polygon-nesting.js +251 -0
- package/lib/api/services/helpers/geometry/quaternions.constants.d.ts +3 -0
- package/lib/api/services/helpers/geometry/quaternions.constants.js +3 -0
- package/lib/api/services/helpers/geometry/quaternions.d.ts +14 -0
- package/lib/api/services/helpers/geometry/quaternions.js +71 -0
- package/lib/api/services/helpers/geometry/scalars.constants.d.ts +1 -0
- package/lib/api/services/helpers/geometry/scalars.constants.js +1 -0
- package/lib/api/services/helpers/geometry/scalars.d.ts +3 -0
- package/lib/api/services/helpers/geometry/scalars.js +29 -1
- package/lib/api/services/helpers/geometry/vectors.d.ts +1 -0
- package/lib/api/services/helpers/geometry/vectors.js +6 -0
- package/lib/api/services/helpers/mesh/mesh-proximity.constants.d.ts +1 -0
- package/lib/api/services/helpers/mesh/mesh-proximity.constants.js +1 -0
- package/lib/api/services/helpers/mesh/mesh-proximity.d.ts +1 -0
- package/lib/api/services/helpers/mesh/mesh-proximity.js +40 -40
- package/lib/api/services/io/drawing.d.ts +170 -0
- package/lib/api/services/io/drawing.js +212 -0
- package/lib/api/services/io/dxf.d.ts +134 -0
- package/lib/api/services/io/dxf.js +167 -0
- package/lib/api/services/io/index.d.ts +4 -0
- package/lib/api/services/io/index.js +4 -0
- package/lib/api/services/io/io.d.ts +14 -0
- package/lib/api/services/io/io.js +15 -0
- package/lib/api/services/io/svg.d.ts +64 -0
- package/lib/api/services/io/svg.js +102 -0
- package/lib/api/services/line.d.ts +17 -0
- package/lib/api/services/line.js +20 -1
- package/lib/api/services/math.d.ts +35 -0
- package/lib/api/services/math.js +56 -1
- package/lib/api/services/mesh.d.ts +17 -0
- package/lib/api/services/mesh.js +27 -1
- package/lib/api/services/point.d.ts +4 -1
- package/lib/api/services/point.js +9 -4
- package/lib/api/services/recipes.d.ts +3 -0
- package/lib/api/services/recipes.js +4 -1
- package/lib/api/services/text.d.ts +4 -1
- package/lib/api/services/text.js +8 -3
- package/lib/api/services/transforms.d.ts +119 -1
- package/lib/api/services/transforms.js +158 -30
- package/package.json +1 -1
- package/lib/api/services/helpers/files/dxf/dxf.d.ts +0 -87
- package/lib/api/services/helpers/files/dxf/dxf.js +0 -111
- package/lib/api/services/io.d.ts +0 -9
- package/lib/api/services/io.js +0 -10
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { NO_VIEWPORT, lengthInPixels } from "./svg-numbers";
|
|
2
|
+
import { scanSvgNumbers } from "./svg-path-data";
|
|
3
|
+
export function lengthsIn(attrs, viewport) {
|
|
4
|
+
return (name, axis) => lengthInPixels(attrs[name], viewport, axis);
|
|
5
|
+
}
|
|
6
|
+
function ellipseSubpath(cx, cy, rx, ry) {
|
|
7
|
+
if (!(rx > 0 && ry > 0)) {
|
|
8
|
+
return [];
|
|
9
|
+
}
|
|
10
|
+
const start = [cx + rx, cy];
|
|
11
|
+
const mid = [cx - rx, cy];
|
|
12
|
+
const arc1 = {
|
|
13
|
+
type: "arc", to: mid, center: [cx, cy], rx, ry,
|
|
14
|
+
xAxisRotation: 0, startAngle: 0, deltaAngle: Math.PI,
|
|
15
|
+
};
|
|
16
|
+
const arc2 = {
|
|
17
|
+
type: "arc", to: start, center: [cx, cy], rx, ry,
|
|
18
|
+
xAxisRotation: 0, startAngle: Math.PI, deltaAngle: Math.PI,
|
|
19
|
+
};
|
|
20
|
+
return [{ start, segments: [arc1, arc2], closed: true }];
|
|
21
|
+
}
|
|
22
|
+
function parsePoints(value) {
|
|
23
|
+
const nums = scanSvgNumbers(value).numbers;
|
|
24
|
+
const pts = [];
|
|
25
|
+
for (let i = 0; i + 1 < nums.length; i += 2) {
|
|
26
|
+
pts.push([nums[i], nums[i + 1]]);
|
|
27
|
+
}
|
|
28
|
+
return pts;
|
|
29
|
+
}
|
|
30
|
+
function polySubpath(pts, closed) {
|
|
31
|
+
if (pts.length < 2) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
const segments = [];
|
|
35
|
+
for (let i = 1; i < pts.length; i++) {
|
|
36
|
+
segments.push({ type: "line", to: pts[i] });
|
|
37
|
+
}
|
|
38
|
+
return [{ start: pts[0], segments, closed }];
|
|
39
|
+
}
|
|
40
|
+
function rectSubpath(x, y, w, h, rxIn, ryIn) {
|
|
41
|
+
if (!(w > 0 && h > 0)) {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
const rx = Math.min(rxIn, w / 2);
|
|
45
|
+
const ry = Math.min(ryIn, h / 2);
|
|
46
|
+
if (rx <= 0 || ry <= 0) {
|
|
47
|
+
return polySubpath([[x, y], [x + w, y], [x + w, y + h], [x, y + h]], true);
|
|
48
|
+
}
|
|
49
|
+
const arc = (to, center, start) => ({
|
|
50
|
+
type: "arc", to, center, rx, ry, xAxisRotation: 0, startAngle: start, deltaAngle: Math.PI / 2,
|
|
51
|
+
});
|
|
52
|
+
const start = [x + rx, y];
|
|
53
|
+
const segments = [
|
|
54
|
+
{ type: "line", to: [x + w - rx, y] },
|
|
55
|
+
arc([x + w, y + ry], [x + w - rx, y + ry], -Math.PI / 2),
|
|
56
|
+
{ type: "line", to: [x + w, y + h - ry] },
|
|
57
|
+
arc([x + w - rx, y + h], [x + w - rx, y + h - ry], 0),
|
|
58
|
+
{ type: "line", to: [x + rx, y + h] },
|
|
59
|
+
arc([x, y + h - ry], [x + rx, y + h - ry], Math.PI / 2),
|
|
60
|
+
{ type: "line", to: [x, y + ry] },
|
|
61
|
+
arc([x + rx, y], [x + rx, y + ry], Math.PI),
|
|
62
|
+
];
|
|
63
|
+
return [{ start, segments, closed: true }];
|
|
64
|
+
}
|
|
65
|
+
function cornerRadii(lengthOf) {
|
|
66
|
+
var _a, _b;
|
|
67
|
+
const valid = (value) => (value !== undefined && value >= 0 ? value : undefined);
|
|
68
|
+
const rx = valid(lengthOf("rx", "x"));
|
|
69
|
+
const ry = valid(lengthOf("ry", "y"));
|
|
70
|
+
return [(_a = rx !== null && rx !== void 0 ? rx : ry) !== null && _a !== void 0 ? _a : 0, (_b = ry !== null && ry !== void 0 ? ry : rx) !== null && _b !== void 0 ? _b : 0];
|
|
71
|
+
}
|
|
72
|
+
export function shapeToSubpaths(tag, attrs, lengthOf = lengthsIn(attrs, NO_VIEWPORT)) {
|
|
73
|
+
var _a, _b;
|
|
74
|
+
const at = (name, axis) => { var _a; return (_a = lengthOf(name, axis)) !== null && _a !== void 0 ? _a : 0; };
|
|
75
|
+
switch (tag) {
|
|
76
|
+
case "rect": {
|
|
77
|
+
const [rx, ry] = cornerRadii(lengthOf);
|
|
78
|
+
return rectSubpath(at("x", "x"), at("y", "y"), at("width", "x"), at("height", "y"), rx, ry);
|
|
79
|
+
}
|
|
80
|
+
case "circle": {
|
|
81
|
+
const r = at("r", "other");
|
|
82
|
+
return ellipseSubpath(at("cx", "x"), at("cy", "y"), r, r);
|
|
83
|
+
}
|
|
84
|
+
case "ellipse": {
|
|
85
|
+
const rx = lengthOf("rx", "x");
|
|
86
|
+
const ry = lengthOf("ry", "y");
|
|
87
|
+
return ellipseSubpath(at("cx", "x"), at("cy", "y"), (_a = rx !== null && rx !== void 0 ? rx : ry) !== null && _a !== void 0 ? _a : 0, (_b = ry !== null && ry !== void 0 ? ry : rx) !== null && _b !== void 0 ? _b : 0);
|
|
88
|
+
}
|
|
89
|
+
case "line":
|
|
90
|
+
return [{
|
|
91
|
+
start: [at("x1", "x"), at("y1", "y")],
|
|
92
|
+
segments: [{ type: "line", to: [at("x2", "x"), at("y2", "y")] }],
|
|
93
|
+
closed: false,
|
|
94
|
+
}];
|
|
95
|
+
case "polyline":
|
|
96
|
+
return polySubpath(parsePoints(attrs["points"]), false);
|
|
97
|
+
case "polygon":
|
|
98
|
+
return polySubpath(parsePoints(attrs["points"]), true);
|
|
99
|
+
default:
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type * as Inputs from "../../../../inputs";
|
|
2
|
+
import type { DrawingSegment } from "../drawing/drawing-types";
|
|
3
|
+
import type { WorkBudget } from "../work-budget";
|
|
4
|
+
type Point2 = Inputs.Base.Point2;
|
|
5
|
+
export interface SvgLineMove {
|
|
6
|
+
readonly kind: "L";
|
|
7
|
+
readonly to: Point2;
|
|
8
|
+
}
|
|
9
|
+
export interface SvgArcMove {
|
|
10
|
+
readonly kind: "A";
|
|
11
|
+
readonly radiusX: number;
|
|
12
|
+
readonly radiusY: number;
|
|
13
|
+
readonly rotation: number;
|
|
14
|
+
readonly large: boolean;
|
|
15
|
+
readonly counterclockwise: boolean;
|
|
16
|
+
readonly to: Point2;
|
|
17
|
+
}
|
|
18
|
+
export interface SvgQuadraticMove {
|
|
19
|
+
readonly kind: "Q";
|
|
20
|
+
readonly control: Point2;
|
|
21
|
+
readonly to: Point2;
|
|
22
|
+
}
|
|
23
|
+
export interface SvgCubicMove {
|
|
24
|
+
readonly kind: "C";
|
|
25
|
+
readonly control1: Point2;
|
|
26
|
+
readonly control2: Point2;
|
|
27
|
+
readonly to: Point2;
|
|
28
|
+
}
|
|
29
|
+
export type SvgMove = SvgLineMove | SvgArcMove | SvgQuadraticMove | SvgCubicMove;
|
|
30
|
+
export interface SvgStroke {
|
|
31
|
+
readonly start: Point2;
|
|
32
|
+
readonly moves: readonly SvgMove[];
|
|
33
|
+
readonly closed: boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare const strokeOf: (segment: DrawingSegment, tolerance: number, budget?: WorkBudget) => SvgStroke;
|
|
36
|
+
export declare const reversedStroke: (stroke: SvgStroke) => SvgStroke;
|
|
37
|
+
export declare const strokeEnd: (stroke: SvgStroke) => Point2;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { cubicsOfPiece } from "../../curves/bezier-pieces";
|
|
2
|
+
import { conicArcExtremes, principalArcOf } from "../../curves/conic-arcs";
|
|
3
|
+
import { bezierPiecesOf, isRationalPiece } from "../../curves/nurbs-forms";
|
|
4
|
+
import { add2, cross2, dot2, length2, scale2 } from "../../geometry/vectors";
|
|
5
|
+
import { bulgeArcOfSpan, circleArcOf, ellipseArcOf, polylineSpanEnd, polylineSpans } from "../drawing/drawing-geometry";
|
|
6
|
+
import { CUBIC_DEGREE, EDGE_ON_SHARE, QUADRATIC_DEGREE, SVG_ARC_PIECE } from "./svg.constants";
|
|
7
|
+
const flat = (point) => [point[0], point[1]];
|
|
8
|
+
const shadowOf = (point) => [point[0], point[1], 0];
|
|
9
|
+
const endOf = (stroke) => stroke.moves.length === 0 ? stroke.start : stroke.moves[stroke.moves.length - 1].to;
|
|
10
|
+
const conicStroke = (arc) => {
|
|
11
|
+
const center = flat(arc.frame.center);
|
|
12
|
+
const x = flat(arc.frame.xAxis);
|
|
13
|
+
const y = flat(arc.frame.yAxis);
|
|
14
|
+
const closed = Math.abs(arc.sweep) >= 2 * Math.PI;
|
|
15
|
+
const shadow = { frame: { center: shadowOf(arc.frame.center), xAxis: shadowOf(arc.frame.xAxis), yAxis: shadowOf(arc.frame.yAxis) }, start: arc.start, sweep: arc.sweep };
|
|
16
|
+
if (Math.abs(cross2(x, y)) <= EDGE_ON_SHARE * Math.max(dot2(x, x), dot2(y, y))) {
|
|
17
|
+
const along = length2([x[0], y[0]]) >= length2([x[1], y[1]]) ? 0 : 1;
|
|
18
|
+
const turns = conicArcExtremes(shadow, [along]).map(flat);
|
|
19
|
+
return { start: turns[0], moves: turns.slice(1).map((to) => ({ kind: "L", to })), closed };
|
|
20
|
+
}
|
|
21
|
+
const principal = principalArcOf(shadow);
|
|
22
|
+
const u = flat(principal.frame.xAxis);
|
|
23
|
+
const v = flat(principal.frame.yAxis);
|
|
24
|
+
const pointAt = (angle) => add2(center, add2(scale2(u, Math.cos(angle)), scale2(v, Math.sin(angle))));
|
|
25
|
+
const pieces = Math.max(1, Math.ceil(Math.abs(arc.sweep) / SVG_ARC_PIECE));
|
|
26
|
+
const step = arc.sweep / pieces;
|
|
27
|
+
const from = principal.start;
|
|
28
|
+
const counterclockwise = (cross2(u, v) > 0) === (step > 0);
|
|
29
|
+
const rotation = Math.atan2(u[1], u[0]);
|
|
30
|
+
const moves = Array.from({ length: pieces }, (_, piece) => ({
|
|
31
|
+
kind: "A",
|
|
32
|
+
radiusX: length2(u),
|
|
33
|
+
radiusY: length2(v),
|
|
34
|
+
rotation,
|
|
35
|
+
large: false,
|
|
36
|
+
counterclockwise,
|
|
37
|
+
to: pointAt(from + (piece + 1) * step),
|
|
38
|
+
}));
|
|
39
|
+
return { start: pointAt(from), moves, closed };
|
|
40
|
+
};
|
|
41
|
+
const bulgeMove = (polyline, span) => {
|
|
42
|
+
const to = flat(polylineSpanEnd(polyline, span));
|
|
43
|
+
const arc = bulgeArcOfSpan(polyline, span);
|
|
44
|
+
if (arc === undefined) {
|
|
45
|
+
return { kind: "L", to };
|
|
46
|
+
}
|
|
47
|
+
const radius = Math.abs(arc.frame.xAxis[0]);
|
|
48
|
+
return { kind: "A", radiusX: radius, radiusY: radius, rotation: 0, large: Math.abs(arc.sweep) > Math.PI, counterclockwise: arc.sweep > 0, to };
|
|
49
|
+
};
|
|
50
|
+
const polylineStroke = (polyline) => ({
|
|
51
|
+
start: flat(polyline.points[0]),
|
|
52
|
+
moves: Array.from({ length: polylineSpans(polyline) }, (_, span) => bulgeMove(polyline, span)),
|
|
53
|
+
closed: polyline.closed,
|
|
54
|
+
});
|
|
55
|
+
const flatPiece = (piece) => ({ points: piece.points.map((point) => [point[0], point[1], 0]), weights: piece.weights });
|
|
56
|
+
const pieceMoves = (piece, tolerance, budget) => {
|
|
57
|
+
const points = piece.points.map(flat);
|
|
58
|
+
const degree = points.length - 1;
|
|
59
|
+
if (!isRationalPiece(piece) && degree === 1) {
|
|
60
|
+
return [{ kind: "L", to: points[1] }];
|
|
61
|
+
}
|
|
62
|
+
if (!isRationalPiece(piece) && degree === QUADRATIC_DEGREE) {
|
|
63
|
+
return [{ kind: "Q", control: points[1], to: points[2] }];
|
|
64
|
+
}
|
|
65
|
+
if (!isRationalPiece(piece) && degree === CUBIC_DEGREE) {
|
|
66
|
+
return [{ kind: "C", control1: points[1], control2: points[2], to: points[CUBIC_DEGREE] }];
|
|
67
|
+
}
|
|
68
|
+
return cubicsOfPiece(flatPiece(piece), tolerance, budget).map((cubic) => ({ kind: "C", control1: flat(cubic.control1), control2: flat(cubic.control2), to: flat(cubic.end) }));
|
|
69
|
+
};
|
|
70
|
+
export const strokeOf = (segment, tolerance, budget) => {
|
|
71
|
+
switch (segment.kind) {
|
|
72
|
+
case "line":
|
|
73
|
+
return { start: flat(segment.start), moves: [{ kind: "L", to: flat(segment.end) }], closed: false };
|
|
74
|
+
case "circle":
|
|
75
|
+
case "arc":
|
|
76
|
+
return conicStroke(circleArcOf(segment));
|
|
77
|
+
case "ellipse":
|
|
78
|
+
return conicStroke(ellipseArcOf(segment));
|
|
79
|
+
case "polyline":
|
|
80
|
+
return polylineStroke(segment);
|
|
81
|
+
case "spline": {
|
|
82
|
+
const pieces = bezierPiecesOf(segment.curve, budget);
|
|
83
|
+
const start = pieces.length === 0 ? segment.curve.points[0] : pieces[0].points[0];
|
|
84
|
+
return { start: flat(start), moves: pieces.flatMap(piece => pieceMoves(piece, tolerance, budget)), closed: false };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const reversedMove = (move, to) => {
|
|
89
|
+
switch (move.kind) {
|
|
90
|
+
case "L":
|
|
91
|
+
return { kind: "L", to };
|
|
92
|
+
case "A":
|
|
93
|
+
return Object.assign(Object.assign({}, move), { counterclockwise: !move.counterclockwise, to });
|
|
94
|
+
case "Q":
|
|
95
|
+
return { kind: "Q", control: move.control, to };
|
|
96
|
+
case "C":
|
|
97
|
+
return { kind: "C", control1: move.control2, control2: move.control1, to };
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
export const reversedStroke = (stroke) => {
|
|
101
|
+
const starts = [stroke.start, ...stroke.moves.slice(0, -1).map(move => move.to)];
|
|
102
|
+
return {
|
|
103
|
+
start: endOf(stroke),
|
|
104
|
+
moves: stroke.moves.map((move, at) => reversedMove(move, starts[at])).reverse(),
|
|
105
|
+
closed: stroke.closed,
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
export const strokeEnd = endOf;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CssMatches } from "./svg-css";
|
|
2
|
+
import type { SvgStyle } from "./svg-scene-types";
|
|
3
|
+
import type { SvgLength, ViewportSize } from "./svg-numbers";
|
|
4
|
+
import type { XmlNode } from "./svg-xml";
|
|
5
|
+
export type StyleProps = Record<string, string>;
|
|
6
|
+
export interface StyleState {
|
|
7
|
+
props: StyleProps;
|
|
8
|
+
opacity: number;
|
|
9
|
+
opacitySet: boolean;
|
|
10
|
+
displayNone: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface StyleReadings {
|
|
13
|
+
opacity: (text: string | undefined) => number | undefined;
|
|
14
|
+
length: (text: string | undefined) => SvgLength | undefined;
|
|
15
|
+
dashes: (text: string | undefined) => readonly SvgLength[] | undefined;
|
|
16
|
+
paintsNothing: (paint: string) => boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare const STYLE_PROPERTIES: ReadonlySet<string>;
|
|
19
|
+
export declare const INITIAL_STYLE: StyleState;
|
|
20
|
+
export declare const isKeyword: (value: string | undefined, keyword: string) => boolean;
|
|
21
|
+
export declare function specifiedValues(node: XmlNode, css: CssMatches): StyleProps;
|
|
22
|
+
export declare function parseOpacity(value: string): number | undefined;
|
|
23
|
+
export declare function computeStyle(specified: StyleProps, parent: StyleState, readings: StyleReadings): StyleState;
|
|
24
|
+
export declare function toSvgStyle(state: StyleState, viewport: ViewportSize, strokeScale: number, readings: StyleReadings): SvgStyle;
|
|
25
|
+
export declare function styleReadings(): StyleReadings;
|
|
26
|
+
export declare function fillsArea(style: SvgStyle, readings?: StyleReadings): boolean;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { parseDeclarations } from "./svg-css";
|
|
2
|
+
import { remembered } from "./svg-memo";
|
|
3
|
+
import { pixelsOf, readLength } from "./svg-numbers";
|
|
4
|
+
import { COLOR_PARTS_WITH_ALPHA, MAX_DASH_VALUES, PERCENT } from "./svg-reader.constants";
|
|
5
|
+
const PRESENTATION_PROPERTIES = [
|
|
6
|
+
"fill", "stroke", "stroke-width", "stroke-dasharray", "opacity", "fill-opacity", "stroke-opacity",
|
|
7
|
+
"fill-rule", "display", "visibility", "color", "clip-path", "mask",
|
|
8
|
+
];
|
|
9
|
+
export const STYLE_PROPERTIES = new Set([...PRESENTATION_PROPERTIES, "transform"]);
|
|
10
|
+
const INHERITED = ["fill", "stroke", "stroke-width", "stroke-dasharray", "fill-opacity", "stroke-opacity", "fill-rule", "visibility", "color"];
|
|
11
|
+
const DASH_SEPARATORS = /\s*,\s*|\s+/;
|
|
12
|
+
const DEFAULT_COLOR = "black";
|
|
13
|
+
const RANK_ATTRIBUTE = 0;
|
|
14
|
+
const RANK_SHEET = 1;
|
|
15
|
+
const RANK_INLINE = 2;
|
|
16
|
+
const RANK_SHEET_IMPORTANT = 3;
|
|
17
|
+
const RANK_INLINE_IMPORTANT = 4;
|
|
18
|
+
export const INITIAL_STYLE = { props: {}, opacity: 1, opacitySet: false, displayNone: false };
|
|
19
|
+
export const isKeyword = (value, keyword) => value !== undefined && value.length === keyword.length && value.toLowerCase() === keyword;
|
|
20
|
+
const outranks = (a, b) => a.rank !== b.rank ? a.rank > b.rank : a.order >= b.order;
|
|
21
|
+
function offer(winners, property, candidate) {
|
|
22
|
+
if (!STYLE_PROPERTIES.has(property)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const current = winners.get(property);
|
|
26
|
+
if (!current || outranks(candidate, current)) {
|
|
27
|
+
winners.set(property, candidate);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export function specifiedValues(node, css) {
|
|
31
|
+
var _a;
|
|
32
|
+
const winners = new Map();
|
|
33
|
+
for (const property of PRESENTATION_PROPERTIES) {
|
|
34
|
+
const value = node.attrs[property];
|
|
35
|
+
if (value !== undefined && value.trim() !== "") {
|
|
36
|
+
offer(winners, property, { value: value.trim(), rank: RANK_ATTRIBUTE, order: 0 });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const match of (_a = css.get(node)) !== null && _a !== void 0 ? _a : []) {
|
|
40
|
+
offer(winners, match.declaration.property, {
|
|
41
|
+
value: match.declaration.value,
|
|
42
|
+
rank: match.declaration.important ? RANK_SHEET_IMPORTANT : RANK_SHEET,
|
|
43
|
+
order: match.order,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
parseDeclarations(node.attrs["style"]).forEach((declaration, order) => {
|
|
47
|
+
offer(winners, declaration.property, { value: declaration.value, rank: declaration.important ? RANK_INLINE_IMPORTANT : RANK_INLINE, order });
|
|
48
|
+
});
|
|
49
|
+
const values = {};
|
|
50
|
+
winners.forEach((candidate, property) => { values[property] = candidate.value; });
|
|
51
|
+
return values;
|
|
52
|
+
}
|
|
53
|
+
export function parseOpacity(value) {
|
|
54
|
+
const trimmed = value.trim();
|
|
55
|
+
const number = parseFloat(trimmed);
|
|
56
|
+
if (Number.isNaN(number)) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
const fraction = trimmed.endsWith("%") ? number / PERCENT : number;
|
|
60
|
+
return Math.min(1, Math.max(0, fraction));
|
|
61
|
+
}
|
|
62
|
+
export function computeStyle(specified, parent, readings) {
|
|
63
|
+
const props = {};
|
|
64
|
+
for (const property of INHERITED) {
|
|
65
|
+
const inherited = parent.props[property];
|
|
66
|
+
if (inherited !== undefined) {
|
|
67
|
+
props[property] = inherited;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
for (const property of Object.keys(specified)) {
|
|
71
|
+
const value = specified[property];
|
|
72
|
+
const fromParent = parent.props[property];
|
|
73
|
+
if (isKeyword(value, "inherit") || (isKeyword(value, "unset") && INHERITED.includes(property))) {
|
|
74
|
+
if (fromParent === undefined) {
|
|
75
|
+
delete props[property];
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
props[property] = fromParent;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if (isKeyword(value, "initial") || isKeyword(value, "unset")) {
|
|
82
|
+
delete props[property];
|
|
83
|
+
}
|
|
84
|
+
else if (property === "color" && isKeyword(value, "currentcolor")) {
|
|
85
|
+
if (parent.props["color"] === undefined) {
|
|
86
|
+
delete props["color"];
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
props["color"] = parent.props["color"];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
props[property] = value;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const own = readings.opacity(props["opacity"]);
|
|
97
|
+
return {
|
|
98
|
+
props,
|
|
99
|
+
opacity: parent.opacity * (own !== null && own !== void 0 ? own : 1),
|
|
100
|
+
opacitySet: parent.opacitySet || own !== undefined,
|
|
101
|
+
displayNone: parent.displayNone || isKeyword(props["display"], "none"),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function parseDashArray(text) {
|
|
105
|
+
const trimmed = text.trim();
|
|
106
|
+
if (trimmed === "" || isKeyword(trimmed, "none")) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
const lengths = [];
|
|
110
|
+
for (const token of trimmed.split(DASH_SEPARATORS)) {
|
|
111
|
+
const length = readLength(token);
|
|
112
|
+
if (length === undefined || length.value < 0) {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
lengths.push(length);
|
|
116
|
+
}
|
|
117
|
+
return lengths.some((length) => length.value > 0) ? lengths : undefined;
|
|
118
|
+
}
|
|
119
|
+
function dashArrayOf(dashes, viewport, strokeScale) {
|
|
120
|
+
if (dashes === undefined || dashes.length > MAX_DASH_VALUES) {
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
const scaled = dashes.map((dash) => pixelsOf(dash, viewport, "other") * strokeScale);
|
|
124
|
+
return dashes.length % 2 === 0 ? scaled : scaled.concat(scaled);
|
|
125
|
+
}
|
|
126
|
+
function paint(value, props) {
|
|
127
|
+
var _a;
|
|
128
|
+
if (value === undefined) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
return isKeyword(value, "currentcolor") ? (_a = props["color"]) !== null && _a !== void 0 ? _a : DEFAULT_COLOR : value;
|
|
132
|
+
}
|
|
133
|
+
export function toSvgStyle(state, viewport, strokeScale, readings) {
|
|
134
|
+
const props = state.props;
|
|
135
|
+
const style = {};
|
|
136
|
+
const fill = paint(props["fill"], props);
|
|
137
|
+
if (fill !== undefined) {
|
|
138
|
+
style.fill = fill;
|
|
139
|
+
}
|
|
140
|
+
const stroke = paint(props["stroke"], props);
|
|
141
|
+
if (stroke !== undefined) {
|
|
142
|
+
style.stroke = stroke;
|
|
143
|
+
}
|
|
144
|
+
const strokeWidth = readings.length(props["stroke-width"]);
|
|
145
|
+
if (strokeWidth !== undefined) {
|
|
146
|
+
style.strokeWidth = pixelsOf(strokeWidth, viewport, "other") * strokeScale;
|
|
147
|
+
}
|
|
148
|
+
const dashArray = dashArrayOf(readings.dashes(props["stroke-dasharray"]), viewport, strokeScale);
|
|
149
|
+
if (dashArray !== undefined) {
|
|
150
|
+
style.strokeDashArray = dashArray;
|
|
151
|
+
}
|
|
152
|
+
if (state.opacitySet) {
|
|
153
|
+
style.opacity = state.opacity;
|
|
154
|
+
}
|
|
155
|
+
const fillOpacity = readings.opacity(props["fill-opacity"]);
|
|
156
|
+
if (fillOpacity !== undefined) {
|
|
157
|
+
style.fillOpacity = fillOpacity;
|
|
158
|
+
}
|
|
159
|
+
const strokeOpacity = readings.opacity(props["stroke-opacity"]);
|
|
160
|
+
if (strokeOpacity !== undefined) {
|
|
161
|
+
style.strokeOpacity = strokeOpacity;
|
|
162
|
+
}
|
|
163
|
+
const fillRule = props["fill-rule"];
|
|
164
|
+
if (isKeyword(fillRule, "evenodd")) {
|
|
165
|
+
style.fillRule = "evenodd";
|
|
166
|
+
}
|
|
167
|
+
else if (isKeyword(fillRule, "nonzero")) {
|
|
168
|
+
style.fillRule = "nonzero";
|
|
169
|
+
}
|
|
170
|
+
const visibility = props["visibility"];
|
|
171
|
+
if (state.displayNone || isKeyword(visibility, "hidden") || isKeyword(visibility, "collapse")) {
|
|
172
|
+
style.hidden = true;
|
|
173
|
+
}
|
|
174
|
+
return style;
|
|
175
|
+
}
|
|
176
|
+
function isTransparent(color) {
|
|
177
|
+
const value = color.trim().toLowerCase();
|
|
178
|
+
if (value === "transparent") {
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
if (/^#(?:[0-9a-f]{3}0|[0-9a-f]{6}00)$/.test(value)) {
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
const functional = /^(?:rgb|hsl)a?\((.*)\)$/.exec(value);
|
|
185
|
+
if (!functional) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
const parts = functional[1].split(/[\s,/]+/).filter((p) => p.length > 0);
|
|
189
|
+
return parts.length === COLOR_PARTS_WITH_ALPHA && parseOpacity(parts[COLOR_PARTS_WITH_ALPHA - 1]) === 0;
|
|
190
|
+
}
|
|
191
|
+
function paintsNothing(paint) {
|
|
192
|
+
return isKeyword(paint.trim(), "none") || isTransparent(paint);
|
|
193
|
+
}
|
|
194
|
+
export function styleReadings() {
|
|
195
|
+
const opacities = new Map();
|
|
196
|
+
const lengths = new Map();
|
|
197
|
+
const dashArrays = new Map();
|
|
198
|
+
const unpainted = new Map();
|
|
199
|
+
return {
|
|
200
|
+
opacity: (text) => text === undefined ? undefined : remembered(opacities, text, parseOpacity),
|
|
201
|
+
length: (text) => text === undefined ? undefined : remembered(lengths, text, readLength),
|
|
202
|
+
dashes: (text) => text === undefined ? undefined : remembered(dashArrays, text, parseDashArray),
|
|
203
|
+
paintsNothing: (paint) => remembered(unpainted, paint, paintsNothing),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
export function fillsArea(style, readings = styleReadings()) {
|
|
207
|
+
if (style.fill !== undefined && readings.paintsNothing(style.fill)) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
return style.fillOpacity !== 0 && style.opacity !== 0;
|
|
211
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AffineMatrix2 } from "../../geometry/affine2";
|
|
2
|
+
import type { SvgSegment, SvgSubpath } from "./svg-scene-types";
|
|
3
|
+
export interface ParsedTransform {
|
|
4
|
+
matrix: AffineMatrix2;
|
|
5
|
+
malformed: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function parseTransformList(value: string | undefined | null): ParsedTransform;
|
|
8
|
+
export declare function parseTransform(value: string | undefined | null): AffineMatrix2;
|
|
9
|
+
export declare function transformSegment(m: AffineMatrix2, seg: SvgSegment): SvgSegment;
|
|
10
|
+
export declare function transformSubpath(m: AffineMatrix2, sp: SvgSubpath): SvgSubpath;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { AFFINE_IDENTITY2, affinePoint2, affineProduct2, affineScaling2, affineTranslation2 } from "../../geometry/affine2";
|
|
2
|
+
import { affineEllipseArc2 } from "../../geometry/ellipse-arcs";
|
|
3
|
+
import { radiansOf } from "../../geometry/scalars";
|
|
4
|
+
import { scanSvgNumbers } from "./svg-path-data";
|
|
5
|
+
import { MATRIX_ARGUMENTS, ROTATE_ABOUT_POINT_ARGUMENTS } from "./svg-reader.constants";
|
|
6
|
+
const TRANSFORM_ITEM = /[\s,]*([A-Za-z]+)\s*\(([^)]*)\)/y;
|
|
7
|
+
const ONLY_SEPARATORS = /^[\s,]*$/;
|
|
8
|
+
function rotation(nums) {
|
|
9
|
+
const ang = radiansOf(nums[0] || 0);
|
|
10
|
+
const cos = Math.cos(ang);
|
|
11
|
+
const sin = Math.sin(ang);
|
|
12
|
+
const rot = [cos, sin, -sin, cos, 0, 0];
|
|
13
|
+
if (nums.length < ROTATE_ABOUT_POINT_ARGUMENTS) {
|
|
14
|
+
return rot;
|
|
15
|
+
}
|
|
16
|
+
const cx = nums[1];
|
|
17
|
+
const cy = nums[2];
|
|
18
|
+
return affineProduct2(affineTranslation2(cx, cy), affineProduct2(rot, affineTranslation2(-cx, -cy)));
|
|
19
|
+
}
|
|
20
|
+
const TRANSFORM_ITEMS = new Map([
|
|
21
|
+
["matrix", { counts: [MATRIX_ARGUMENTS], matrix: nums => [nums[0], nums[1], nums[2], nums[3], nums[4], nums[5]] }],
|
|
22
|
+
["translate", { counts: [1, 2], matrix: nums => affineTranslation2(nums[0] || 0, nums[1] || 0) }],
|
|
23
|
+
["scale", { counts: [1, 2], matrix: nums => affineScaling2(nums[0], nums.length > 1 ? nums[1] : nums[0]) }],
|
|
24
|
+
["rotate", { counts: [1, ROTATE_ABOUT_POINT_ARGUMENTS], matrix: rotation }],
|
|
25
|
+
["skewX", { counts: [1], matrix: nums => [1, 0, Math.tan(radiansOf(nums[0] || 0)), 1, 0, 0] }],
|
|
26
|
+
["skewY", { counts: [1], matrix: nums => [1, Math.tan(radiansOf(nums[0] || 0)), 0, 1, 0, 0] }],
|
|
27
|
+
]);
|
|
28
|
+
export function parseTransformList(value) {
|
|
29
|
+
if (!value) {
|
|
30
|
+
return { matrix: AFFINE_IDENTITY2, malformed: false };
|
|
31
|
+
}
|
|
32
|
+
let matrix = AFFINE_IDENTITY2;
|
|
33
|
+
let at = 0;
|
|
34
|
+
while (at < value.length) {
|
|
35
|
+
TRANSFORM_ITEM.lastIndex = at;
|
|
36
|
+
const match = TRANSFORM_ITEM.exec(value);
|
|
37
|
+
if (!match) {
|
|
38
|
+
return ONLY_SEPARATORS.test(value.slice(at)) ? { matrix, malformed: false } : { matrix: AFFINE_IDENTITY2, malformed: true };
|
|
39
|
+
}
|
|
40
|
+
at = TRANSFORM_ITEM.lastIndex;
|
|
41
|
+
const scanned = scanSvgNumbers(match[2]);
|
|
42
|
+
const item = TRANSFORM_ITEMS.get(match[1]);
|
|
43
|
+
if (!item || scanned.malformed || !item.counts.includes(scanned.numbers.length)) {
|
|
44
|
+
return { matrix: AFFINE_IDENTITY2, malformed: true };
|
|
45
|
+
}
|
|
46
|
+
matrix = affineProduct2(matrix, item.matrix(scanned.numbers));
|
|
47
|
+
}
|
|
48
|
+
return { matrix, malformed: false };
|
|
49
|
+
}
|
|
50
|
+
export function parseTransform(value) {
|
|
51
|
+
return parseTransformList(value).matrix;
|
|
52
|
+
}
|
|
53
|
+
export function transformSegment(m, seg) {
|
|
54
|
+
switch (seg.type) {
|
|
55
|
+
case "line":
|
|
56
|
+
return { type: "line", to: affinePoint2(m, seg.to) };
|
|
57
|
+
case "quad":
|
|
58
|
+
return { type: "quad", c: affinePoint2(m, seg.c), to: affinePoint2(m, seg.to) };
|
|
59
|
+
case "cubic":
|
|
60
|
+
return {
|
|
61
|
+
type: "cubic",
|
|
62
|
+
c1: affinePoint2(m, seg.c1),
|
|
63
|
+
c2: affinePoint2(m, seg.c2),
|
|
64
|
+
to: affinePoint2(m, seg.to),
|
|
65
|
+
};
|
|
66
|
+
case "arc":
|
|
67
|
+
return Object.assign({ type: "arc", to: affinePoint2(m, seg.to) }, affineEllipseArc2(m, seg));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export function transformSubpath(m, sp) {
|
|
71
|
+
return {
|
|
72
|
+
start: affinePoint2(m, sp.start),
|
|
73
|
+
segments: sp.segments.map((s) => transformSegment(m, s)),
|
|
74
|
+
closed: sp.closed,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { DrawingPart, DrawingPath } from "../drawing/drawing-types";
|
|
2
|
+
export interface SvgDocumentOptions {
|
|
3
|
+
readonly unit: string | undefined;
|
|
4
|
+
readonly strokeWidth: number | undefined;
|
|
5
|
+
readonly margin: number | undefined;
|
|
6
|
+
readonly tolerance: number;
|
|
7
|
+
readonly property: string;
|
|
8
|
+
}
|
|
9
|
+
export interface SvgPathOptions {
|
|
10
|
+
readonly flipY: boolean;
|
|
11
|
+
readonly tolerance: number;
|
|
12
|
+
readonly property: string;
|
|
13
|
+
}
|
|
14
|
+
export declare const svgNumber: (value: number) => string;
|
|
15
|
+
export declare const pathDataOf: (path: DrawingPath, options: SvgPathOptions) => string;
|
|
16
|
+
export declare const svgDocumentOf: (parts: readonly DrawingPart[], options: SvgDocumentOptions) => string;
|