@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.
- package/lib/api/GlobalCDNProvider.js +1 -1
- package/lib/api/inputs/base-inputs.d.ts +20 -7
- package/lib/api/inputs/base-inputs.js +10 -0
- 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/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 +22 -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.js +2 -3
- package/lib/api/services/helpers/color/color-forms.constants.d.ts +3 -0
- package/lib/api/services/helpers/color/color-forms.constants.js +3 -0
- package/lib/api/services/helpers/color/color-forms.d.ts +1 -0
- package/lib/api/services/helpers/color/color-forms.js +4 -1
- 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/vectors.d.ts +1 -0
- package/lib/api/services/helpers/geometry/vectors.js +6 -0
- 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/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/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,89 @@
|
|
|
1
|
+
import { AFFINE_IDENTITY2, affineProduct2, affineScaling2, affineTranslation2 } from "../../geometry/affine2";
|
|
2
|
+
import { NO_VIEWPORT, lengthInPixels, millimetresPerPixel } from "./svg-numbers";
|
|
3
|
+
import { scanSvgNumbers } from "./svg-path-data";
|
|
4
|
+
const VIEW_BOX_NUMBERS = 4;
|
|
5
|
+
const ALIGNMENT = /^x(min|mid|max)y(min|mid|max)$/;
|
|
6
|
+
export function parseViewBox(text) {
|
|
7
|
+
const scanned = scanSvgNumbers(text);
|
|
8
|
+
const [minX, minY, width, height] = scanned.numbers;
|
|
9
|
+
if (scanned.malformed || scanned.numbers.length !== VIEW_BOX_NUMBERS || !(width > 0) || !(height > 0)) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
return [minX, minY, width, height];
|
|
13
|
+
}
|
|
14
|
+
function alignedOffset(alignment, room) {
|
|
15
|
+
if (alignment === "min") {
|
|
16
|
+
return 0;
|
|
17
|
+
}
|
|
18
|
+
return alignment === "mid" ? room / 2 : room;
|
|
19
|
+
}
|
|
20
|
+
export function parseAspectRatio(preserveAspectRatio) {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
const words = (preserveAspectRatio !== null && preserveAspectRatio !== void 0 ? preserveAspectRatio : "").trim().toLowerCase().split(/\s+/).filter((w) => w !== "defer");
|
|
23
|
+
const align = (_a = words[0]) !== null && _a !== void 0 ? _a : "xmidymid";
|
|
24
|
+
const parts = (_b = ALIGNMENT.exec(align)) !== null && _b !== void 0 ? _b : ["", "mid", "mid"];
|
|
25
|
+
return { stretch: align === "none", x: parts[1], y: parts[2], slice: words[1] === "slice" };
|
|
26
|
+
}
|
|
27
|
+
function viewBoxFit(box, width, height, aspect) {
|
|
28
|
+
const [, , boxWidth, boxHeight] = box;
|
|
29
|
+
const sx = width / boxWidth;
|
|
30
|
+
const sy = height / boxHeight;
|
|
31
|
+
if (aspect.stretch) {
|
|
32
|
+
return { scaleX: sx, scaleY: sy, offsetX: 0, offsetY: 0 };
|
|
33
|
+
}
|
|
34
|
+
const s = aspect.slice ? Math.max(sx, sy) : Math.min(sx, sy);
|
|
35
|
+
return { scaleX: s, scaleY: s, offsetX: alignedOffset(aspect.x, width - boxWidth * s), offsetY: alignedOffset(aspect.y, height - boxHeight * s) };
|
|
36
|
+
}
|
|
37
|
+
export function fittedViewBox(box, width, height, aspect) {
|
|
38
|
+
const fit = viewBoxFit(box, width, height, aspect);
|
|
39
|
+
const scaled = affineProduct2(affineScaling2(fit.scaleX, fit.scaleY), affineTranslation2(-box[0], -box[1]));
|
|
40
|
+
return aspect.stretch ? scaled : affineProduct2(affineTranslation2(fit.offsetX, fit.offsetY), scaled);
|
|
41
|
+
}
|
|
42
|
+
function unfittedViewBox(box, width, height, aspect) {
|
|
43
|
+
const fit = viewBoxFit(box, width, height, aspect);
|
|
44
|
+
return affineProduct2(affineTranslation2(box[0], box[1]), affineProduct2(affineScaling2(1 / fit.scaleX, 1 / fit.scaleY), affineTranslation2(-fit.offsetX, -fit.offsetY)));
|
|
45
|
+
}
|
|
46
|
+
export function viewBoxTransform(box, width, height, preserveAspectRatio) {
|
|
47
|
+
return fittedViewBox(box, width, height, parseAspectRatio(preserveAspectRatio));
|
|
48
|
+
}
|
|
49
|
+
function absoluteLength(text, axis) {
|
|
50
|
+
if (text === undefined || text.trim().endsWith("%")) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const pixels = lengthInPixels(text, NO_VIEWPORT, axis);
|
|
54
|
+
return pixels !== undefined && pixels > 0 ? pixels : undefined;
|
|
55
|
+
}
|
|
56
|
+
export function documentFrame(root) {
|
|
57
|
+
const box = parseViewBox(root.attrs["viewBox"]);
|
|
58
|
+
let width = absoluteLength(root.attrs["width"], "x");
|
|
59
|
+
let height = absoluteLength(root.attrs["height"], "y");
|
|
60
|
+
if (box) {
|
|
61
|
+
if (width === undefined && height === undefined) {
|
|
62
|
+
width = box[2];
|
|
63
|
+
height = box[3];
|
|
64
|
+
}
|
|
65
|
+
else if (width === undefined) {
|
|
66
|
+
width = height * box[2] / box[3];
|
|
67
|
+
}
|
|
68
|
+
else if (height === undefined) {
|
|
69
|
+
height = width * box[3] / box[2];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const aspect = parseAspectRatio(root.attrs["preserveAspectRatio"]);
|
|
73
|
+
const fitted = box && width !== undefined && height !== undefined ? { box, width, height } : undefined;
|
|
74
|
+
const pageTransform = fitted ? fittedViewBox(fitted.box, fitted.width, fitted.height, aspect) : AFFINE_IDENTITY2;
|
|
75
|
+
const frame = {
|
|
76
|
+
pageTransform,
|
|
77
|
+
pageInverse: fitted ? unfittedViewBox(fitted.box, fitted.width, fitted.height, aspect) : AFFINE_IDENTITY2,
|
|
78
|
+
contentViewport: box ? { width: box[2], height: box[3] } : width !== undefined && height !== undefined ? { width, height } : NO_VIEWPORT,
|
|
79
|
+
millimetresPerUserUnit: pageTransform[0] * millimetresPerPixel(),
|
|
80
|
+
};
|
|
81
|
+
if (box) {
|
|
82
|
+
frame.viewBox = box;
|
|
83
|
+
}
|
|
84
|
+
if (width !== undefined && height !== undefined) {
|
|
85
|
+
frame.width = width;
|
|
86
|
+
frame.height = height;
|
|
87
|
+
}
|
|
88
|
+
return frame;
|
|
89
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function remembered<K, V>(known: Map<K, V>, key: K, read: (key: K) => V): V;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Base } from "../../../../inputs/base-inputs";
|
|
2
|
+
import type { SvgScene } from "./svg-scene-types";
|
|
3
|
+
export interface NormalizeOptions {
|
|
4
|
+
unit?: `${Base.unitOfLengthEnum}` | undefined;
|
|
5
|
+
}
|
|
6
|
+
export declare function normalizeSvg(svg: string, options?: NormalizeOptions): SvgScene;
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import { AFFINE_IDENTITY2, affineAreaScale2, affineProduct2, affineScaling2, affineTranslation2 } from "../../geometry/affine2";
|
|
2
|
+
import { millimetresPerUnitOf } from "../../geometry/lengths";
|
|
3
|
+
import { WorkBudget } from "../work-budget";
|
|
4
|
+
import { matchStylesheet, parseStylesheets } from "./svg-css";
|
|
5
|
+
import { documentFrame, fittedViewBox, parseAspectRatio, parseViewBox } from "./svg-document";
|
|
6
|
+
import { remembered } from "./svg-memo";
|
|
7
|
+
import { millimetresPerPixel, pixelsOf } from "./svg-numbers";
|
|
8
|
+
import { parseSvgPathData } from "./svg-path-data";
|
|
9
|
+
import { shapeToSubpaths } from "./svg-shapes";
|
|
10
|
+
import { INITIAL_STYLE, STYLE_PROPERTIES, computeStyle, isKeyword, specifiedValues, styleReadings, toSvgStyle } from "./svg-style";
|
|
11
|
+
import { parseTransformList, transformSubpath } from "./svg-transform";
|
|
12
|
+
import { parseXml } from "./svg-xml";
|
|
13
|
+
import { MAX_DASH_VALUES, MAX_LISTED_SELECTORS, MAX_LISTED_TAGS, MAX_USE_DEPTH, MAX_VISITED_ELEMENTS, MAX_WALK_DEPTH, SEGMENT_CEILING, SEGMENT_FLOOR, SEGMENTS_PER_INPUT_CHARACTER, SELECTOR_MATCH_STEP_CEILING, SELECTOR_MATCH_STEP_FLOOR, SELECTOR_MATCH_STEPS_PER_INPUT_CHARACTER, } from "./svg-reader.constants";
|
|
14
|
+
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
15
|
+
const INKSCAPE_NAMESPACE = "http://www.inkscape.org/namespaces/inkscape";
|
|
16
|
+
const INKSCAPE_LAYER = "layer";
|
|
17
|
+
const DRAWABLE = new Set(["path", "rect", "circle", "ellipse", "line", "polyline", "polygon"]);
|
|
18
|
+
const SIZED_BY_VIEWPORT = new Set(["rect", "circle", "ellipse", "line"]);
|
|
19
|
+
const CONTAINERS = new Set(["svg", "g", "a", "switch", "use"]);
|
|
20
|
+
const SILENT = new Set([
|
|
21
|
+
"defs", "symbol", "clippath", "mask", "marker", "pattern", "metadata", "title", "desc", "style", "script",
|
|
22
|
+
"view", "set", "animate", "animatemotion", "animatetransform", "animatecolor", "mpath", "lineargradient",
|
|
23
|
+
"radialgradient", "stop", "filter", "cursor", "font", "font-face", "glyph", "missing-glyph", "hkern", "vkern",
|
|
24
|
+
"color-profile", "solidcolor",
|
|
25
|
+
]);
|
|
26
|
+
const UNSUPPORTED_HINTS = new Map([
|
|
27
|
+
["text", " Convert text to paths before exporting."],
|
|
28
|
+
["image", " Raster images have no geometry to import."],
|
|
29
|
+
["foreignobject", " Its content is not SVG."],
|
|
30
|
+
]);
|
|
31
|
+
const malformedTransformWarning = (text) => `A transform could not be read (\`${text}\`) and is ignored, as a browser ignores it.`;
|
|
32
|
+
const unlistedTagsWarning = (count) => `${count} more kind${count === 1 ? "" : "s"} of element ${count === 1 ? "is" : "are"} not supported and contribute${count === 1 ? "s" : ""} no geometry.`;
|
|
33
|
+
function segmentsDrawn(subpath) {
|
|
34
|
+
const last = subpath.segments[subpath.segments.length - 1];
|
|
35
|
+
const closesWithLine = subpath.closed && last !== undefined && (last.to[0] !== subpath.start[0] || last.to[1] !== subpath.start[1]);
|
|
36
|
+
return subpath.segments.length + (closesWithLine ? 1 : 0);
|
|
37
|
+
}
|
|
38
|
+
const isSvgElement = (node) => node.namespace === SVG_NAMESPACE || (node.namespace === undefined && (node.prefix === "" || node.prefix === "svg"));
|
|
39
|
+
const isRenderedChoice = (node) => { var _a; return isSvgElement(node) && !SILENT.has(node.tag) && !((_a = node.attrs["requiredExtensions"]) !== null && _a !== void 0 ? _a : "").trim(); };
|
|
40
|
+
function inkscapeAttribute(node, localName) {
|
|
41
|
+
const namespaces = node.attrNamespaces;
|
|
42
|
+
if (!namespaces) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
const name = Object.keys(namespaces).find((attribute) => namespaces[attribute] === INKSCAPE_NAMESPACE && attribute.slice(attribute.indexOf(":") + 1) === localName);
|
|
46
|
+
return name === undefined ? undefined : node.attrs[name];
|
|
47
|
+
}
|
|
48
|
+
function layerNameOf(node) {
|
|
49
|
+
var _a, _b, _c;
|
|
50
|
+
if (node.tag !== "g" || ((_a = inkscapeAttribute(node, "groupmode")) === null || _a === void 0 ? void 0 : _a.trim()) !== INKSCAPE_LAYER) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
return (_c = (_b = inkscapeAttribute(node, "label")) !== null && _b !== void 0 ? _b : node.attrs["id"]) !== null && _c !== void 0 ? _c : "";
|
|
54
|
+
}
|
|
55
|
+
function findSvgRoot(node) {
|
|
56
|
+
if (!node) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
if (node.tag === "svg" && isSvgElement(node)) {
|
|
60
|
+
return node;
|
|
61
|
+
}
|
|
62
|
+
for (const child of node.children) {
|
|
63
|
+
const found = findSvgRoot(child);
|
|
64
|
+
if (found) {
|
|
65
|
+
return found;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
function collect(node, visit) {
|
|
71
|
+
const pending = [node];
|
|
72
|
+
while (pending.length > 0) {
|
|
73
|
+
const current = pending.pop();
|
|
74
|
+
visit(current);
|
|
75
|
+
for (let at = current.children.length - 1; at >= 0; at--) {
|
|
76
|
+
pending.push(current.children[at]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function givenLength(length, viewport, axis) {
|
|
81
|
+
const available = axis === "x" ? viewport.width : viewport.height;
|
|
82
|
+
if (length === undefined || (length.unit === "%" && available <= 0)) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
return pixelsOf(length, viewport, axis);
|
|
86
|
+
}
|
|
87
|
+
function viewportLength(given, available, boxSize) {
|
|
88
|
+
return given !== null && given !== void 0 ? given : (available > 0 ? available : boxSize !== null && boxSize !== void 0 ? boxSize : 0);
|
|
89
|
+
}
|
|
90
|
+
function hrefOf(node) {
|
|
91
|
+
if (node.attrs["href"] !== undefined) {
|
|
92
|
+
return node.attrs["href"].trim();
|
|
93
|
+
}
|
|
94
|
+
const linked = Object.keys(node.attrs).find((name) => name.endsWith(":href"));
|
|
95
|
+
return linked === undefined ? undefined : node.attrs[linked].trim();
|
|
96
|
+
}
|
|
97
|
+
class SceneWalker {
|
|
98
|
+
constructor(ids, css, segments) {
|
|
99
|
+
this.ids = ids;
|
|
100
|
+
this.css = css;
|
|
101
|
+
this.segments = segments;
|
|
102
|
+
this.elements = [];
|
|
103
|
+
this.warnings = [];
|
|
104
|
+
this.warned = new Set();
|
|
105
|
+
this.warnedTags = new Set();
|
|
106
|
+
this.unlistedTags = 0;
|
|
107
|
+
this.onPath = new Set();
|
|
108
|
+
this.readings = styleReadings();
|
|
109
|
+
this.outlines = new Map();
|
|
110
|
+
this.sizedOutlines = new Map();
|
|
111
|
+
this.transforms = new Map();
|
|
112
|
+
this.specified = new Map();
|
|
113
|
+
this.viewports = new Map();
|
|
114
|
+
this.references = new Map();
|
|
115
|
+
this.choices = new Map();
|
|
116
|
+
this.layers = new Map();
|
|
117
|
+
this.visited = 0;
|
|
118
|
+
this.segmentsSpent = false;
|
|
119
|
+
}
|
|
120
|
+
warnOnce(key, message) {
|
|
121
|
+
if (this.warned.has(key)) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
this.warned.add(key);
|
|
125
|
+
this.warnings.push(message);
|
|
126
|
+
}
|
|
127
|
+
styleOf(node, parent) {
|
|
128
|
+
const specified = remembered(this.specified, node, (n) => specifiedValues(n, this.css));
|
|
129
|
+
return computeStyle(specified, parent, this.readings);
|
|
130
|
+
}
|
|
131
|
+
walkRoot(root, context) {
|
|
132
|
+
this.onPath.add(root);
|
|
133
|
+
try {
|
|
134
|
+
this.walkChildren(root, context);
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
this.onPath.delete(root);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
walkChildren(node, context) {
|
|
141
|
+
for (const child of node.children) {
|
|
142
|
+
this.walk(child, context);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
walk(node, context) {
|
|
146
|
+
var _a;
|
|
147
|
+
if (this.segmentsSpent) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (this.visited >= MAX_VISITED_ELEMENTS) {
|
|
151
|
+
this.warnOnce("budget", `The SVG holds more than ${MAX_VISITED_ELEMENTS} elements once its <use> references are expanded; the rest is not imported.`);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
this.visited++;
|
|
155
|
+
if (!isSvgElement(node) || SILENT.has(node.tag)) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (context.depth > MAX_WALK_DEPTH) {
|
|
159
|
+
this.warnOnce("depth", `The SVG nests more than ${MAX_WALK_DEPTH} levels once its <use> references are expanded; deeper elements are not imported.`);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (!DRAWABLE.has(node.tag) && !CONTAINERS.has(node.tag)) {
|
|
163
|
+
this.warnUnsupported(node.tag);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const style = this.styleOf(node, context.style);
|
|
167
|
+
this.warnAboutStyle(style);
|
|
168
|
+
const layer = (_a = remembered(this.layers, node, layerNameOf)) !== null && _a !== void 0 ? _a : context.layer;
|
|
169
|
+
const inner = Object.assign(Object.assign({}, context), { matrix: affineProduct2(context.matrix, this.transformOf(node)), style, depth: context.depth + 1, layer });
|
|
170
|
+
this.onPath.add(node);
|
|
171
|
+
try {
|
|
172
|
+
this.visit(node, inner);
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
this.onPath.delete(node);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
warnUnsupported(tag) {
|
|
179
|
+
var _a;
|
|
180
|
+
if (this.warnedTags.has(tag)) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
this.warnedTags.add(tag);
|
|
184
|
+
if (this.warnedTags.size > MAX_LISTED_TAGS) {
|
|
185
|
+
this.unlistedTags++;
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
this.warnings.push(`<${tag}> is not supported and contributes no geometry.${(_a = UNSUPPORTED_HINTS.get(tag)) !== null && _a !== void 0 ? _a : ""}`);
|
|
189
|
+
}
|
|
190
|
+
finish() {
|
|
191
|
+
if (this.unlistedTags > 0) {
|
|
192
|
+
this.warnings.push(unlistedTagsWarning(this.unlistedTags));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
transformIn(text) {
|
|
196
|
+
if (text === undefined) {
|
|
197
|
+
return AFFINE_IDENTITY2;
|
|
198
|
+
}
|
|
199
|
+
const parsed = parseTransformList(text);
|
|
200
|
+
if (parsed.malformed) {
|
|
201
|
+
this.warnOnce("transform", malformedTransformWarning(text));
|
|
202
|
+
}
|
|
203
|
+
return parsed.matrix;
|
|
204
|
+
}
|
|
205
|
+
transformOf(node) {
|
|
206
|
+
return remembered(this.transforms, node, (n) => this.transformIn(n.attrs["transform"]));
|
|
207
|
+
}
|
|
208
|
+
visit(node, context) {
|
|
209
|
+
switch (node.tag) {
|
|
210
|
+
case "g":
|
|
211
|
+
case "a":
|
|
212
|
+
this.walkChildren(node, context);
|
|
213
|
+
break;
|
|
214
|
+
case "switch": {
|
|
215
|
+
const chosen = remembered(this.choices, node, (n) => n.children.find(isRenderedChoice));
|
|
216
|
+
if (chosen) {
|
|
217
|
+
this.walk(chosen, context);
|
|
218
|
+
}
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
case "svg":
|
|
222
|
+
this.walkViewport(node, context, {});
|
|
223
|
+
break;
|
|
224
|
+
case "use":
|
|
225
|
+
this.instantiate(node, context);
|
|
226
|
+
break;
|
|
227
|
+
default:
|
|
228
|
+
this.addElement(node, context);
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
warnAboutStyle(style) {
|
|
233
|
+
var _a, _b;
|
|
234
|
+
for (const property of ["clip-path", "mask"]) {
|
|
235
|
+
const value = style.props[property];
|
|
236
|
+
if (value !== undefined && !isKeyword(value.trim(), "none")) {
|
|
237
|
+
this.warnOnce(property, `${property} is not applied; the elements it would cut are imported whole.`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (style.props["transform"] !== undefined) {
|
|
241
|
+
this.warnOnce("css-transform", "A transform set in CSS or a style attribute is not applied; use the transform attribute.");
|
|
242
|
+
}
|
|
243
|
+
if (((_b = (_a = this.readings.dashes(style.props["stroke-dasharray"])) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > MAX_DASH_VALUES) {
|
|
244
|
+
this.warnOnce("dashes", `A stroke-dasharray holds more than ${MAX_DASH_VALUES} lengths, more than the importer reads; its strokes are read as solid lines.`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
lengthIn(text, viewport, axis) {
|
|
248
|
+
const length = this.readings.length(text);
|
|
249
|
+
return length === undefined ? undefined : pixelsOf(length, viewport, axis);
|
|
250
|
+
}
|
|
251
|
+
walkViewport(node, context, override) {
|
|
252
|
+
var _a, _b, _c, _d;
|
|
253
|
+
const x = (_a = this.lengthIn(node.attrs["x"], context.viewport, "x")) !== null && _a !== void 0 ? _a : 0;
|
|
254
|
+
const y = (_b = this.lengthIn(node.attrs["y"], context.viewport, "y")) !== null && _b !== void 0 ? _b : 0;
|
|
255
|
+
const { box, aspect } = remembered(this.viewports, node, (n) => ({ box: parseViewBox(n.attrs["viewBox"]), aspect: parseAspectRatio(n.attrs["preserveAspectRatio"]) }));
|
|
256
|
+
const givenWidth = givenLength(this.readings.length((_c = override.width) !== null && _c !== void 0 ? _c : node.attrs["width"]), context.viewport, "x");
|
|
257
|
+
const givenHeight = givenLength(this.readings.length((_d = override.height) !== null && _d !== void 0 ? _d : node.attrs["height"]), context.viewport, "y");
|
|
258
|
+
if ((givenWidth !== undefined && givenWidth <= 0) || (givenHeight !== undefined && givenHeight <= 0)) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const width = viewportLength(givenWidth, context.viewport.width, box === null || box === void 0 ? void 0 : box[2]);
|
|
262
|
+
const height = viewportLength(givenHeight, context.viewport.height, box === null || box === void 0 ? void 0 : box[3]);
|
|
263
|
+
const sized = width > 0 && height > 0;
|
|
264
|
+
const placed = affineProduct2(context.matrix, affineTranslation2(x, y));
|
|
265
|
+
this.walkChildren(node, Object.assign(Object.assign({}, context), { matrix: box && sized ? affineProduct2(placed, fittedViewBox(box, width, height, aspect)) : placed, viewport: box ? { width: box[2], height: box[3] } : sized ? { width, height } : context.viewport }));
|
|
266
|
+
}
|
|
267
|
+
referenceOf(use) {
|
|
268
|
+
return remembered(this.references, use, (node) => {
|
|
269
|
+
const href = hrefOf(node);
|
|
270
|
+
if (!href) {
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
if (!href.startsWith("#")) {
|
|
274
|
+
this.warnOnce("use-external", `A <use> refers to another file (\`${href}\`), which is not imported.`);
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
const target = this.ids.get(href.slice(1));
|
|
278
|
+
if (!target) {
|
|
279
|
+
this.warnOnce("use-missing", `A <use> refers to \`${href}\`, which the document does not hold; it imports nothing.`);
|
|
280
|
+
}
|
|
281
|
+
return { target, cycleWarning: `A <use> refers to \`${href}\`, which contains that <use> itself; it imports nothing.` };
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
instantiate(use, context) {
|
|
285
|
+
var _a, _b;
|
|
286
|
+
const reference = this.referenceOf(use);
|
|
287
|
+
const target = reference === null || reference === void 0 ? void 0 : reference.target;
|
|
288
|
+
if (!reference || !target) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
if (this.onPath.has(target)) {
|
|
292
|
+
this.warnOnce("use-cycle", reference.cycleWarning);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (context.useDepth >= MAX_USE_DEPTH) {
|
|
296
|
+
this.warnOnce("use-depth", `<use> references nest more than ${MAX_USE_DEPTH} deep; deeper ones import nothing.`);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
const x = (_a = this.lengthIn(use.attrs["x"], context.viewport, "x")) !== null && _a !== void 0 ? _a : 0;
|
|
300
|
+
const y = (_b = this.lengthIn(use.attrs["y"], context.viewport, "y")) !== null && _b !== void 0 ? _b : 0;
|
|
301
|
+
const placed = Object.assign(Object.assign({}, context), { matrix: affineProduct2(context.matrix, affineTranslation2(x, y)), useDepth: context.useDepth + 1 });
|
|
302
|
+
const size = { width: use.attrs["width"], height: use.attrs["height"] };
|
|
303
|
+
if (target.tag === "symbol" && isSvgElement(target)) {
|
|
304
|
+
this.instantiateSymbol(target, placed, size);
|
|
305
|
+
}
|
|
306
|
+
else if (target.tag === "svg" && isSvgElement(target)) {
|
|
307
|
+
this.onPath.add(target);
|
|
308
|
+
try {
|
|
309
|
+
this.walkViewport(target, Object.assign(Object.assign({}, placed), { style: this.styleOf(target, placed.style) }), size);
|
|
310
|
+
}
|
|
311
|
+
finally {
|
|
312
|
+
this.onPath.delete(target);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
this.walk(target, placed);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
instantiateSymbol(symbol, context, size) {
|
|
320
|
+
const style = this.styleOf(symbol, context.style);
|
|
321
|
+
this.warnAboutStyle(style);
|
|
322
|
+
this.onPath.add(symbol);
|
|
323
|
+
try {
|
|
324
|
+
this.walkViewport(symbol, Object.assign(Object.assign({}, context), { style }), size);
|
|
325
|
+
}
|
|
326
|
+
finally {
|
|
327
|
+
this.onPath.delete(symbol);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
addElement(node, context) {
|
|
331
|
+
const local = this.subpathsOf(node, context.viewport);
|
|
332
|
+
if (local.length === 0) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const count = local.reduce((sum, sp) => sum + segmentsDrawn(sp), 0);
|
|
336
|
+
if (!this.segments.affords(count)) {
|
|
337
|
+
this.segmentsSpent = true;
|
|
338
|
+
this.warnOnce("segments", `The SVG holds more than ${this.segments.capacity} path segments once its <use> references are expanded, more than the importer builds from a file of its size; the rest is not imported.`);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
this.segments.spend(count);
|
|
342
|
+
const element = {
|
|
343
|
+
tag: node.tag,
|
|
344
|
+
style: toSvgStyle(context.style, context.viewport, affineAreaScale2(context.matrix), this.readings),
|
|
345
|
+
subpaths: local.map((sp) => transformSubpath(context.matrix, sp)),
|
|
346
|
+
};
|
|
347
|
+
if (node.attrs["id"]) {
|
|
348
|
+
element.id = node.attrs["id"];
|
|
349
|
+
}
|
|
350
|
+
if (node.attrs["class"]) {
|
|
351
|
+
element.className = node.attrs["class"];
|
|
352
|
+
}
|
|
353
|
+
if (context.layer !== undefined) {
|
|
354
|
+
element.layer = context.layer;
|
|
355
|
+
}
|
|
356
|
+
this.elements.push(element);
|
|
357
|
+
}
|
|
358
|
+
subpathsOf(node, viewport) {
|
|
359
|
+
if (node.tag === "path") {
|
|
360
|
+
return remembered(this.outlines, node, (n) => { var _a; return parseSvgPathData((_a = n.attrs["d"]) !== null && _a !== void 0 ? _a : ""); });
|
|
361
|
+
}
|
|
362
|
+
if (!SIZED_BY_VIEWPORT.has(node.tag)) {
|
|
363
|
+
return remembered(this.outlines, node, (n) => shapeToSubpaths(n.tag, n.attrs));
|
|
364
|
+
}
|
|
365
|
+
const inViewports = remembered(this.sizedOutlines, node, () => new Map());
|
|
366
|
+
return remembered(inViewports, `${viewport.width} ${viewport.height}`, () => shapeToSubpaths(node.tag, node.attrs, this.lengthsIn(node, viewport)));
|
|
367
|
+
}
|
|
368
|
+
lengthsIn(node, viewport) {
|
|
369
|
+
return (name, axis) => this.lengthIn(node.attrs[name], viewport, axis);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
function stylesheetTexts(root) {
|
|
373
|
+
const texts = [];
|
|
374
|
+
collect(root, (node) => {
|
|
375
|
+
var _a;
|
|
376
|
+
const type = (_a = node.attrs["type"]) === null || _a === void 0 ? void 0 : _a.trim().toLowerCase();
|
|
377
|
+
if (node.tag === "style" && isSvgElement(node) && node.text && (!type || type === "text/css")) {
|
|
378
|
+
texts.push(node.text);
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
return texts;
|
|
382
|
+
}
|
|
383
|
+
function idsOf(root) {
|
|
384
|
+
const ids = new Map();
|
|
385
|
+
collect(root, (node) => {
|
|
386
|
+
const id = node.attrs["id"];
|
|
387
|
+
if (id !== undefined && !ids.has(id)) {
|
|
388
|
+
ids.set(id, node);
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
return ids;
|
|
392
|
+
}
|
|
393
|
+
function unsupportedSelectorsWarning(selectors) {
|
|
394
|
+
const unique = [...new Set(selectors)];
|
|
395
|
+
const listed = unique.slice(0, MAX_LISTED_SELECTORS).map((s) => `\`${s}\``).join(", ");
|
|
396
|
+
const more = unique.length > MAX_LISTED_SELECTORS ? ` and ${unique.length - MAX_LISTED_SELECTORS} more` : "";
|
|
397
|
+
return `These CSS rules cannot be matched and are not applied: ${listed}${more}.`;
|
|
398
|
+
}
|
|
399
|
+
export function normalizeSvg(svg, options = {}) {
|
|
400
|
+
const root = findSvgRoot(parseXml(svg));
|
|
401
|
+
if (!root) {
|
|
402
|
+
return { elements: [], warnings: ["No <svg> root element found."] };
|
|
403
|
+
}
|
|
404
|
+
const frame = documentFrame(root);
|
|
405
|
+
const sheet = parseStylesheets(stylesheetTexts(root), STYLE_PROPERTIES);
|
|
406
|
+
const matching = WorkBudget.forInput(svg.length, SELECTOR_MATCH_STEPS_PER_INPUT_CHARACTER, SELECTOR_MATCH_STEP_FLOOR, SELECTOR_MATCH_STEP_CEILING);
|
|
407
|
+
const css = matchStylesheet(root, sheet, matching);
|
|
408
|
+
const walker = new SceneWalker(idsOf(root), css.matches, WorkBudget.forInput(svg.length, SEGMENTS_PER_INPUT_CHARACTER, SEGMENT_FLOOR, SEGMENT_CEILING));
|
|
409
|
+
if (sheet.unsupported.length > 0) {
|
|
410
|
+
walker.warnings.push(unsupportedSelectorsWarning(sheet.unsupported));
|
|
411
|
+
}
|
|
412
|
+
if (css.exhausted) {
|
|
413
|
+
walker.warnings.push(`The stylesheet takes more than ${matching.capacity} steps to match against the document, more than the importer allows for a file of its size; the elements after that point are imported without its rules.`);
|
|
414
|
+
}
|
|
415
|
+
const rootTransform = walker.transformIn(root.attrs["transform"]);
|
|
416
|
+
const unitsPerPixel = options.unit === undefined ? undefined : millimetresPerPixel() / millimetresPerUnitOf(options.unit);
|
|
417
|
+
const onPage = affineProduct2(rootTransform, frame.pageTransform);
|
|
418
|
+
const inUserUnits = rootTransform === AFFINE_IDENTITY2 ? AFFINE_IDENTITY2 : affineProduct2(frame.pageInverse, onPage);
|
|
419
|
+
const matrix = unitsPerPixel === undefined ? inUserUnits : affineProduct2(affineScaling2(unitsPerPixel, unitsPerPixel), onPage);
|
|
420
|
+
const style = walker.styleOf(root, INITIAL_STYLE);
|
|
421
|
+
walker.warnAboutStyle(style);
|
|
422
|
+
walker.walkRoot(root, { matrix, style, viewport: frame.contentViewport, useDepth: 0, depth: 1, layer: undefined });
|
|
423
|
+
walker.finish();
|
|
424
|
+
const scene = { elements: walker.elements, warnings: walker.warnings, millimetresPerUserUnit: frame.millimetresPerUserUnit };
|
|
425
|
+
if (frame.viewBox) {
|
|
426
|
+
scene.viewBox = frame.viewBox;
|
|
427
|
+
}
|
|
428
|
+
if (frame.width !== undefined && frame.height !== undefined) {
|
|
429
|
+
scene.width = frame.width;
|
|
430
|
+
scene.height = frame.height;
|
|
431
|
+
}
|
|
432
|
+
return scene;
|
|
433
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface ViewportSize {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
}
|
|
5
|
+
export type LengthAxis = "x" | "y" | "other";
|
|
6
|
+
export interface SvgLength {
|
|
7
|
+
value: number;
|
|
8
|
+
unit: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const NO_VIEWPORT: ViewportSize;
|
|
11
|
+
export declare function millimetresPerPixel(): number;
|
|
12
|
+
export declare function percentReference(viewport: ViewportSize, axis: LengthAxis): number;
|
|
13
|
+
export declare function readLength(text: string): SvgLength | undefined;
|
|
14
|
+
export declare function pixelsOf(length: SvgLength, viewport: ViewportSize, axis: LengthAxis): number;
|
|
15
|
+
export declare function lengthInPixels(text: string | undefined, viewport: ViewportSize, axis: LengthAxis): number | undefined;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { length2 } from "../../geometry/vectors";
|
|
2
|
+
import { millimetresPerUnitOf } from "../../geometry/lengths";
|
|
3
|
+
import { svgNumberAt } from "./svg-path-data";
|
|
4
|
+
import { EX_PER_EM, FONT_SIZE_PIXELS, PERCENT, PICAS_PER_INCH, PIXELS_PER_INCH, POINTS_PER_INCH } from "./svg-reader.constants";
|
|
5
|
+
const UNIT = /[a-zA-Z%]*/y;
|
|
6
|
+
const SPACE = /\s/;
|
|
7
|
+
const PIXELS_PER_MILLIMETRE = PIXELS_PER_INCH / millimetresPerUnitOf("in");
|
|
8
|
+
const PIXELS_PER_UNIT = new Map([
|
|
9
|
+
["", 1],
|
|
10
|
+
["px", 1],
|
|
11
|
+
["in", PIXELS_PER_INCH],
|
|
12
|
+
["mm", PIXELS_PER_MILLIMETRE],
|
|
13
|
+
["cm", PIXELS_PER_MILLIMETRE * millimetresPerUnitOf("cm")],
|
|
14
|
+
["pt", PIXELS_PER_INCH / POINTS_PER_INCH],
|
|
15
|
+
["pc", PIXELS_PER_INCH / PICAS_PER_INCH],
|
|
16
|
+
["em", FONT_SIZE_PIXELS],
|
|
17
|
+
["rem", FONT_SIZE_PIXELS],
|
|
18
|
+
["ex", FONT_SIZE_PIXELS * EX_PER_EM],
|
|
19
|
+
]);
|
|
20
|
+
export const NO_VIEWPORT = { width: 0, height: 0 };
|
|
21
|
+
export function millimetresPerPixel() {
|
|
22
|
+
return 1 / PIXELS_PER_MILLIMETRE;
|
|
23
|
+
}
|
|
24
|
+
export function percentReference(viewport, axis) {
|
|
25
|
+
if (axis === "x") {
|
|
26
|
+
return viewport.width;
|
|
27
|
+
}
|
|
28
|
+
if (axis === "y") {
|
|
29
|
+
return viewport.height;
|
|
30
|
+
}
|
|
31
|
+
return length2([viewport.width, viewport.height]) / Math.SQRT2;
|
|
32
|
+
}
|
|
33
|
+
function skipSpace(text, from) {
|
|
34
|
+
let at = from;
|
|
35
|
+
while (at < text.length && SPACE.test(text[at])) {
|
|
36
|
+
at++;
|
|
37
|
+
}
|
|
38
|
+
return at;
|
|
39
|
+
}
|
|
40
|
+
export function readLength(text) {
|
|
41
|
+
const read = svgNumberAt(text, skipSpace(text, 0));
|
|
42
|
+
if (!read) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
UNIT.lastIndex = skipSpace(text, read[1]);
|
|
46
|
+
const unit = UNIT.exec(text)[0].toLowerCase();
|
|
47
|
+
if (skipSpace(text, UNIT.lastIndex) !== text.length) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
return unit === "%" || PIXELS_PER_UNIT.has(unit) ? { value: read[0], unit } : undefined;
|
|
51
|
+
}
|
|
52
|
+
export function pixelsOf(length, viewport, axis) {
|
|
53
|
+
if (length.unit === "%") {
|
|
54
|
+
return length.value / PERCENT * percentReference(viewport, axis);
|
|
55
|
+
}
|
|
56
|
+
return length.value * PIXELS_PER_UNIT.get(length.unit);
|
|
57
|
+
}
|
|
58
|
+
export function lengthInPixels(text, viewport, axis) {
|
|
59
|
+
const length = text === undefined ? undefined : readLength(text);
|
|
60
|
+
return length === undefined ? undefined : pixelsOf(length, viewport, axis);
|
|
61
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const ARGUMENTS_PER_PATH_COMMAND: ReadonlyMap<string, number>;
|
|
2
|
+
export declare const ARC_COMMAND = "a";
|
|
3
|
+
export declare const MOVE_COMMAND = "m";
|
|
4
|
+
export declare const ARC_FLAG_ARGUMENTS: ReadonlySet<number>;
|
|
5
|
+
export declare const CUBIC_END_ARGUMENT = 4;
|
|
6
|
+
export declare const ARC_END_ARGUMENT = 5;
|
|
7
|
+
export declare const QUADRATIC_TO_CUBIC_SHARE: number;
|
|
8
|
+
export declare const QUARTER_TURN: number;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const ARGUMENTS_PER_PATH_COMMAND = new Map([
|
|
2
|
+
["m", 2], ["l", 2], ["h", 1], ["v", 1], ["c", 6], ["s", 4], ["q", 4], ["t", 2], ["a", 7], ["z", 0],
|
|
3
|
+
]);
|
|
4
|
+
export const ARC_COMMAND = "a";
|
|
5
|
+
export const MOVE_COMMAND = "m";
|
|
6
|
+
export const ARC_FLAG_ARGUMENTS = new Set([3, 4]);
|
|
7
|
+
export const CUBIC_END_ARGUMENT = 4;
|
|
8
|
+
export const ARC_END_ARGUMENT = 5;
|
|
9
|
+
export const QUADRATIC_TO_CUBIC_SHARE = 2 / 3;
|
|
10
|
+
export const QUARTER_TURN = Math.PI / 2;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type * as Inputs from "../../../../inputs";
|
|
2
|
+
import type { ScannedSvgNumbers, SvgSubpath } from "./svg-path-types";
|
|
3
|
+
type Point2 = Inputs.Base.Point2;
|
|
4
|
+
type Read = readonly [number, number];
|
|
5
|
+
export declare const svgNumberAt: (text: string, at: number) => Read | undefined;
|
|
6
|
+
export declare const scanSvgNumbers: (text: string | undefined) => ScannedSvgNumbers;
|
|
7
|
+
export declare const parseSvgPathData: (d: string) => SvgSubpath[];
|
|
8
|
+
export declare const svgSubpathPoints: (subpath: SvgSubpath, perCurve: number, perQuarterTurn: number) => Point2[];
|
|
9
|
+
export {};
|