@officedk/pptxdk 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/dist/build-zrZuX4fK.d.ts +3 -0
- package/dist/build.d.ts +35 -0
- package/dist/build.js +128 -0
- package/dist/capabilities/dist/all.js +34 -0
- package/dist/capabilities/dist/box/box-codegen.js +84 -0
- package/dist/capabilities/dist/box/box-declaration.js +32 -0
- package/dist/capabilities/dist/box/box-elements.js +17 -0
- package/dist/capabilities/dist/box/box.js +142 -0
- package/dist/capabilities/dist/chart/chart-codegen.js +79 -0
- package/dist/capabilities/dist/chart/chart-declaration.js +7 -0
- package/dist/capabilities/dist/chart/chart-elements.js +12 -0
- package/dist/capabilities/dist/chart/chart-import.js +2403 -0
- package/dist/capabilities/dist/chart/chart-labels.js +86 -0
- package/dist/capabilities/dist/chart/chart-model.js +9 -0
- package/dist/capabilities/dist/chart/chart-schemas.js +1088 -0
- package/dist/capabilities/dist/chart/chart-xml.js +788 -0
- package/dist/capabilities/dist/chart/chart.js +1119 -0
- package/dist/capabilities/dist/chart/preview/axes-and-grid.js +311 -0
- package/dist/capabilities/dist/chart/preview/bar-column.js +143 -0
- package/dist/capabilities/dist/chart/preview/data-labels.js +334 -0
- package/dist/capabilities/dist/chart/preview/error-bars.js +213 -0
- package/dist/capabilities/dist/chart/preview/index.js +192 -0
- package/dist/capabilities/dist/chart/preview/layout.js +592 -0
- package/dist/capabilities/dist/chart/preview/legend.js +273 -0
- package/dist/capabilities/dist/chart/preview/line-area.js +404 -0
- package/dist/capabilities/dist/chart/preview/markers.js +100 -0
- package/dist/capabilities/dist/chart/preview/number-format.js +247 -0
- package/dist/capabilities/dist/chart/preview/pie-doughnut.js +159 -0
- package/dist/capabilities/dist/chart/preview/scales.js +365 -0
- package/dist/capabilities/dist/chart/preview/scatter.js +131 -0
- package/dist/capabilities/dist/chart/preview/style.js +60 -0
- package/dist/capabilities/dist/chart/preview/text-render.js +116 -0
- package/dist/capabilities/dist/chart/preview/trendlines.js +537 -0
- package/dist/capabilities/dist/chart/preview/types.js +5 -0
- package/dist/capabilities/dist/document.js +168 -0
- package/dist/capabilities/dist/font/font-embedding.js +119 -0
- package/dist/capabilities/dist/font/font-engine.js +834 -0
- package/dist/capabilities/dist/font/font-import.js +766 -0
- package/dist/capabilities/dist/font/font-subset.js +55 -0
- package/dist/capabilities/dist/font/font.js +72 -0
- package/dist/capabilities/dist/group/group-codegen.js +74 -0
- package/dist/capabilities/dist/group/group-declaration.js +41 -0
- package/dist/capabilities/dist/group/group-elements.js +12 -0
- package/dist/capabilities/dist/group/group-import.js +649 -0
- package/dist/capabilities/dist/group/group-xml.js +8 -0
- package/dist/capabilities/dist/group/group.js +260 -0
- package/dist/capabilities/dist/image/gif-decoder.js +244 -0
- package/dist/capabilities/dist/image/image-codegen.js +164 -0
- package/dist/capabilities/dist/image/image-declaration.js +8 -0
- package/dist/capabilities/dist/image/image-decode.js +244 -0
- package/dist/capabilities/dist/image/image-elements.js +12 -0
- package/dist/capabilities/dist/image/image-import.js +984 -0
- package/dist/capabilities/dist/image/image-schemas.js +147 -0
- package/dist/capabilities/dist/image/image-xml.js +12 -0
- package/dist/capabilities/dist/image/image.js +552 -0
- package/dist/capabilities/dist/line/line-codegen.js +89 -0
- package/dist/capabilities/dist/line/line-declaration.js +72 -0
- package/dist/capabilities/dist/line/line-elements.js +12 -0
- package/dist/capabilities/dist/line/line-import.js +504 -0
- package/dist/capabilities/dist/line/line-xml.js +9 -0
- package/dist/capabilities/dist/line/line.js +331 -0
- package/dist/capabilities/dist/masters/footer-placeholder.js +683 -0
- package/dist/capabilities/dist/masters/masters-codegen.js +69 -0
- package/dist/capabilities/dist/masters/masters.js +250 -0
- package/dist/capabilities/dist/motion/motion-api.js +757 -0
- package/dist/capabilities/dist/motion/motion-codegen.js +406 -0
- package/dist/capabilities/dist/motion/motion-elements.js +43 -0
- package/dist/capabilities/dist/motion/motion-import.js +1568 -0
- package/dist/capabilities/dist/motion/motion-plan.js +2340 -0
- package/dist/capabilities/dist/motion/motion-query.js +423 -0
- package/dist/capabilities/dist/motion/motion-schemas.js +339 -0
- package/dist/capabilities/dist/motion/motion.js +1617 -0
- package/dist/capabilities/dist/notes/notes-codegen.js +19 -0
- package/dist/capabilities/dist/notes/notes-declaration.js +8 -0
- package/dist/capabilities/dist/notes/notes-elements.js +12 -0
- package/dist/capabilities/dist/notes/notes-import.js +292 -0
- package/dist/capabilities/dist/notes/notes-schemas.js +38 -0
- package/dist/capabilities/dist/notes/notes-xml.js +38 -0
- package/dist/capabilities/dist/notes/notes.js +194 -0
- package/dist/capabilities/dist/placeholder/placeholder-codegen.js +54 -0
- package/dist/capabilities/dist/placeholder/placeholder-elements.js +13 -0
- package/dist/capabilities/dist/placeholder/placeholder-schemas.js +33 -0
- package/dist/capabilities/dist/placeholder/placeholder.js +451 -0
- package/dist/capabilities/dist/shape/shape-codegen.js +236 -0
- package/dist/capabilities/dist/shape/shape-declaration.js +45 -0
- package/dist/capabilities/dist/shape/shape-elements.js +12 -0
- package/dist/capabilities/dist/shape/shape-import.js +628 -0
- package/dist/capabilities/dist/shape/shape-xml.js +6 -0
- package/dist/capabilities/dist/shape/shape.js +320 -0
- package/dist/capabilities/dist/shared/geometry.js +1681 -0
- package/dist/capabilities/dist/shared/identity.js +205 -0
- package/dist/capabilities/dist/shared/morph-api.js +11 -0
- package/dist/capabilities/dist/shared/placeholder-import.js +238 -0
- package/dist/capabilities/dist/shared/preset-definitions.js +22423 -0
- package/dist/capabilities/dist/shared/style-inheritance.js +161 -0
- package/dist/capabilities/dist/shared/text-schemas.js +241 -0
- package/dist/capabilities/dist/shared/text-service.js +2252 -0
- package/dist/capabilities/dist/shared/visual.js +1197 -0
- package/dist/capabilities/dist/slide-background/slide-background-codegen.js +44 -0
- package/dist/capabilities/dist/slide-background/slide-background-import.js +166 -0
- package/dist/capabilities/dist/slide-background/slide-background-xml.js +34 -0
- package/dist/capabilities/dist/slide-background/slide-background.js +238 -0
- package/dist/capabilities/dist/table/table-codegen.js +125 -0
- package/dist/capabilities/dist/table/table-declaration.js +15 -0
- package/dist/capabilities/dist/table/table-elements.js +27 -0
- package/dist/capabilities/dist/table/table-import.js +606 -0
- package/dist/capabilities/dist/table/table-schemas.js +334 -0
- package/dist/capabilities/dist/table/table-xml.js +30 -0
- package/dist/capabilities/dist/table/table.js +739 -0
- package/dist/capabilities/dist/text/text-codegen.js +198 -0
- package/dist/capabilities/dist/text/text-declaration.js +27 -0
- package/dist/capabilities/dist/text/text-elements.js +54 -0
- package/dist/capabilities/dist/text/text-import.js +1224 -0
- package/dist/capabilities/dist/text/text-xml.js +9 -0
- package/dist/capabilities/dist/text/text.js +320 -0
- package/dist/capabilities/dist/transition/transition-api.js +136 -0
- package/dist/capabilities/dist/transition/transition-codegen.js +45 -0
- package/dist/capabilities/dist/transition/transition-import.js +477 -0
- package/dist/capabilities/dist/transition/transition.js +260 -0
- package/dist/capability-BbKW9w4O.d.ts +2131 -0
- package/dist/cli.js +20 -0
- package/dist/codegen/dist/assets.js +79 -0
- package/dist/codegen/dist/codegen.js +691 -0
- package/dist/codegen/dist/identifiers.js +203 -0
- package/dist/codegen/dist/index.js +3 -0
- package/dist/codegen/dist/printer.js +143 -0
- package/dist/codegen/dist/types.js +77 -0
- package/dist/core/dist/author-ids.js +51 -0
- package/dist/core/dist/author-schemas.js +330 -0
- package/dist/core/dist/block-flex-layout.js +801 -0
- package/dist/core/dist/build.js +4 -0
- package/dist/core/dist/capability.js +15 -0
- package/dist/core/dist/declaration.js +16 -0
- package/dist/core/dist/diagnostics.js +353 -0
- package/dist/core/dist/document-internals.js +26 -0
- package/dist/core/dist/document.js +417 -0
- package/dist/core/dist/font-seal.js +204 -0
- package/dist/core/dist/geometry-schemas.js +256 -0
- package/dist/core/dist/grid-layout.js +449 -0
- package/dist/core/dist/host.js +40 -0
- package/dist/core/dist/index.js +15 -0
- package/dist/core/dist/input-values.js +61 -0
- package/dist/core/dist/layout-protocol.js +61 -0
- package/dist/core/dist/layout-schemas.js +201 -0
- package/dist/core/dist/layout.js +340 -0
- package/dist/core/dist/masters.js +194 -0
- package/dist/core/dist/morph.js +30 -0
- package/dist/core/dist/placeholders.js +275 -0
- package/dist/core/dist/query.js +190 -0
- package/dist/core/dist/resolution.js +12 -0
- package/dist/core/dist/resolve.js +2246 -0
- package/dist/core/dist/stage-options.js +300 -0
- package/dist/core/dist/theme.js +1023 -0
- package/dist/core/dist/transform-geometry.js +72 -0
- package/dist/document-41spD7bq.d.ts +1350 -0
- package/dist/emit/dist/emit.js +957 -0
- package/dist/emit/dist/index.js +5 -0
- package/dist/emit/dist/ooxml.js +661 -0
- package/dist/emit/dist/shape-tree.js +12 -0
- package/dist/import/dist/alternate-content.js +54 -0
- package/dist/import/dist/diagnostics.js +157 -0
- package/dist/import/dist/fonts.js +309 -0
- package/dist/import/dist/import.js +513 -0
- package/dist/import/dist/index.js +16 -0
- package/dist/import/dist/masters.js +1293 -0
- package/dist/import/dist/mechanism.js +10 -0
- package/dist/import/dist/namespaces.js +61 -0
- package/dist/import/dist/notes-masters.js +300 -0
- package/dist/import/dist/opc.js +233 -0
- package/dist/import/dist/resources.js +166 -0
- package/dist/import/dist/sections.js +139 -0
- package/dist/import/dist/themes.js +777 -0
- package/dist/import/dist/visual.js +821 -0
- package/dist/import/dist/xml-fragments.js +277 -0
- package/dist/import/dist/xml-reader.js +267 -0
- package/dist/index-1ag7U9Jt.d.ts +63 -0
- package/dist/index-BEwhk7c8.d.ts +72 -0
- package/dist/index-cK2KtukZ.d.ts +161 -0
- package/dist/index-v77CTJev.d.ts +55 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +82 -0
- package/dist/node.d.ts +53 -0
- package/dist/node.js +225 -0
- package/dist/pptxdk/src/cli-commands.js +907 -0
- package/dist/pptxdk/src/compose.js +53 -0
- package/dist/pptxdk/src/slide.js +9 -0
- package/dist/preview/dist/index.js +342 -0
- package/dist/preview/dist/options.js +40 -0
- package/dist/preview/dist/svg-guard.js +1089 -0
- package/dist/preview.d.ts +12 -0
- package/dist/preview.js +29 -0
- package/dist/react/dist/append.js +66 -0
- package/dist/react/dist/document-adapter.js +404 -0
- package/dist/react/dist/elements.js +82 -0
- package/dist/react/dist/from-vanilla.js +57 -0
- package/dist/react/dist/index.js +89 -0
- package/dist/react/dist/mount.js +34 -0
- package/dist/react/dist/reconciler.js +290 -0
- package/dist/react.d.ts +235 -0
- package/dist/react.js +31 -0
- package/dist/transition-6n8GZxSg.d.ts +9 -0
- package/dist/vanilla.d.ts +4 -0
- package/dist/vanilla.js +15 -0
- package/package.json +79 -3
- package/README.md +0 -3
|
@@ -0,0 +1,1681 @@
|
|
|
1
|
+
import { CapabilityError } from "../../../core/dist/diagnostics.js";
|
|
2
|
+
import { customGeometrySchema, geometryAdjustKeySchema, geometryAdjustSchema, geometrySchema, presetGeometryNameSchema, presetGeometryTuple } from "../../../core/dist/geometry-schemas.js";
|
|
3
|
+
import "../../../core/dist/capability.js";
|
|
4
|
+
import { pxToEmu } from "../../../emit/dist/ooxml.js";
|
|
5
|
+
import "../../../emit/dist/index.js";
|
|
6
|
+
import { A_NS, emuToPx } from "../../../import/dist/namespaces.js";
|
|
7
|
+
import { ooxmlBoolSchema, ooxmlIntSchema, ooxmlPositiveIntSchema } from "../../../import/dist/xml-fragments.js";
|
|
8
|
+
import "../../../import/dist/index.js";
|
|
9
|
+
import { presetDefinitions } from "./preset-definitions.js";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
// ../capabilities/dist/shared/geometry.js
|
|
12
|
+
const STANDARD_CARDINAL_CXN = [
|
|
13
|
+
["hc", "t"],
|
|
14
|
+
["l", "vc"],
|
|
15
|
+
["hc", "b"],
|
|
16
|
+
["r", "vc"]
|
|
17
|
+
];
|
|
18
|
+
/** Returns the 4 standard cardinal connection sites (top, left, bottom, right) for an un-transformed box. */
|
|
19
|
+
function standardBoxConnectionSites(box) {
|
|
20
|
+
const cx = box.x + box.width / 2;
|
|
21
|
+
const cy = box.y + box.height / 2;
|
|
22
|
+
return [
|
|
23
|
+
{
|
|
24
|
+
x: cx,
|
|
25
|
+
y: box.y
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
x: box.x,
|
|
29
|
+
y: cy
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
x: cx,
|
|
33
|
+
y: box.y + box.height
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
x: box.x + box.width,
|
|
37
|
+
y: cy
|
|
38
|
+
}
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
/** Applies flipH, flipV, and clockwise rotation (degrees) around `box` center to a list of points. */
|
|
42
|
+
function applyTransformToPoints(points, box, transform) {
|
|
43
|
+
const flipH = transform?.flipH ?? false;
|
|
44
|
+
const flipV = transform?.flipV ?? false;
|
|
45
|
+
const angleDeg = transform?.rotate ?? transform?.angle ?? 0;
|
|
46
|
+
if (!flipH && !flipV && angleDeg === 0) return points;
|
|
47
|
+
const cx = box.x + box.width / 2;
|
|
48
|
+
const cy = box.y + box.height / 2;
|
|
49
|
+
const rad = angleDeg * Math.PI / 180;
|
|
50
|
+
const cos = Math.cos(rad);
|
|
51
|
+
const sin = Math.sin(rad);
|
|
52
|
+
return points.map((pt) => {
|
|
53
|
+
const dx = (pt.x - cx) * (flipH ? -1 : 1);
|
|
54
|
+
const dy = (pt.y - cy) * (flipV ? -1 : 1);
|
|
55
|
+
return {
|
|
56
|
+
x: cx + dx * cos - dy * sin,
|
|
57
|
+
y: cy + dx * sin + dy * cos
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/** Returns the ECMA-376 / custom connection site count for an author `Geometry` value. */
|
|
62
|
+
function getGeometryConnectionSiteCount(geometry) {
|
|
63
|
+
if (geometry === void 0) return 4;
|
|
64
|
+
if (typeof geometry === "string") return (presetDefinitions[geometry]?.cxn ?? STANDARD_CARDINAL_CXN).length;
|
|
65
|
+
if (geometry.type === "preset") return (presetDefinitions[geometry.name]?.cxn ?? STANDARD_CARDINAL_CXN).length;
|
|
66
|
+
if (geometry.connectionSites !== void 0) return geometry.connectionSites.length;
|
|
67
|
+
return 4;
|
|
68
|
+
}
|
|
69
|
+
/** Complete DrawingML preset geometry enumeration from ECMA-376 ST_ShapeType. */
|
|
70
|
+
const presetGeometryNames = new Set(presetGeometryTuple);
|
|
71
|
+
function invalidGeometry(message) {
|
|
72
|
+
throw new CapabilityError("invalid-geometry", message);
|
|
73
|
+
}
|
|
74
|
+
function preset(name, adjust) {
|
|
75
|
+
if (!presetGeometryNameSchema.safeParse(name).success) throw new CapabilityError("unsupported-geometry", `Unsupported preset geometry ${String(name)}`);
|
|
76
|
+
if (adjust !== void 0) {
|
|
77
|
+
const parsedAdjust = geometryAdjustSchema.safeParse(adjust);
|
|
78
|
+
if (!parsedAdjust.success) invalidGeometry(`Geometry adjust ${String(parsedAdjust.error.issues[0]?.path[0] ?? "adjust")} must be an integer DrawingML parameter`);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
type: "preset",
|
|
82
|
+
name,
|
|
83
|
+
...adjust !== void 0 && { adjust }
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const blockArcOptionsSchema = z.strictObject({
|
|
87
|
+
startAngle: z.number().finite(),
|
|
88
|
+
endAngle: z.number().finite(),
|
|
89
|
+
thickness: z.number().finite()
|
|
90
|
+
}).readonly();
|
|
91
|
+
/**
|
|
92
|
+
* Native blockArc (annular arc segment). Angles are in degrees, measured clockwise from 3 o'clock, consistent with DrawingML;
|
|
93
|
+
* thickness represents the ring width as a fraction of the shape's shorter side.
|
|
94
|
+
*/
|
|
95
|
+
function blockArc(options) {
|
|
96
|
+
const parsed = blockArcOptionsSchema.safeParse(options);
|
|
97
|
+
if (!parsed.success) invalidGeometry(`blockArc.${String(parsed.error.issues[0]?.path[0] ?? "options")} must be a finite number`);
|
|
98
|
+
const angle = (degrees) => Math.round((degrees % 360 + 360) % 360 * 6e4);
|
|
99
|
+
return preset("blockArc", {
|
|
100
|
+
adj1: angle(parsed.data.startAngle),
|
|
101
|
+
adj2: angle(parsed.data.endAngle),
|
|
102
|
+
adj3: Math.round(parsed.data.thickness * 1e5)
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
const pathOptionsSchema = z.strictObject({
|
|
106
|
+
/** Dimensions of the local coordinate space, similar to an SVG viewBox starting from 0,0. */
|
|
107
|
+
viewBox: z.tuple([z.number().finite().positive(), z.number().finite().positive()]).readonly(),
|
|
108
|
+
fill: z.boolean().optional(),
|
|
109
|
+
stroke: z.boolean().optional(),
|
|
110
|
+
/** Fill rule for SVG path data; see `path`. */
|
|
111
|
+
fillRule: z.enum(["nonzero", "evenodd"]).optional()
|
|
112
|
+
}).readonly();
|
|
113
|
+
const pathDataSchema = z.union([z.string().refine((item) => item.trim() !== ""), z.array(z.string().refine((item) => item.trim() !== "")).min(1).readonly()]);
|
|
114
|
+
/**
|
|
115
|
+
* Creates custom geometry from SVG path data. Multiple subpaths in a single string constitute the same native path; pass an array for separately stroked paths.
|
|
116
|
+
*
|
|
117
|
+
* PPTX does not have fill rules, so different applications interpret them individually. The default `fillRule: "nonzero"` writes paths as-is;
|
|
118
|
+
* in all applications, inner subpaths whose direction opposes the outer contour form holes. When set to `"evenodd"`, subpaths alternate directions according to nesting
|
|
119
|
+
* depth, allowing applications to display even-odd fill results. Subpaths that overlap without nesting, or self-intersecting subpaths, cannot achieve consistency this way.
|
|
120
|
+
*/
|
|
121
|
+
function path(data, options) {
|
|
122
|
+
const parsedOptions = pathOptionsSchema.safeParse(options);
|
|
123
|
+
if (!parsedOptions.success) {
|
|
124
|
+
const key = parsedOptions.error.issues[0]?.path[0];
|
|
125
|
+
if (key === "fill") invalidGeometry("path.fill must be a boolean");
|
|
126
|
+
if (key === "stroke") invalidGeometry("path.stroke must be a boolean");
|
|
127
|
+
if (key === "fillRule") invalidGeometry("path.fillRule must be nonzero or evenodd");
|
|
128
|
+
invalidGeometry("path.viewBox must consist of two finite positive numbers");
|
|
129
|
+
}
|
|
130
|
+
const parsedData = pathDataSchema.safeParse(data);
|
|
131
|
+
if (!parsedData.success) {
|
|
132
|
+
if (typeof data !== "string" && !Array.isArray(data)) invalidGeometry("path data must be an SVG string or an array of strings");
|
|
133
|
+
invalidGeometry("path must contain at least one non-empty SVG path");
|
|
134
|
+
}
|
|
135
|
+
const validOptions = parsedOptions.data;
|
|
136
|
+
const list = typeof parsedData.data === "string" ? [parsedData.data] : parsedData.data;
|
|
137
|
+
return {
|
|
138
|
+
type: "custom",
|
|
139
|
+
width: validOptions.viewBox[0],
|
|
140
|
+
height: validOptions.viewBox[1],
|
|
141
|
+
paths: list.map((item) => {
|
|
142
|
+
const commands = parseSvgPath(item);
|
|
143
|
+
return {
|
|
144
|
+
commands: validOptions.fillRule === "evenodd" ? alternateByNesting(commands) : commands,
|
|
145
|
+
...validOptions.fill !== void 0 && { fill: validOptions.fill },
|
|
146
|
+
...validOptions.stroke !== void 0 && { stroke: validOptions.stroke }
|
|
147
|
+
};
|
|
148
|
+
})
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/** Splits subpaths by Z commands; when drawing continues after Z, it starts from the previous subpath start point. */
|
|
152
|
+
function subpaths(commands) {
|
|
153
|
+
const result = [];
|
|
154
|
+
let current;
|
|
155
|
+
let start = {
|
|
156
|
+
x: 0,
|
|
157
|
+
y: 0
|
|
158
|
+
};
|
|
159
|
+
for (const command of commands) {
|
|
160
|
+
if (command.op === "M") {
|
|
161
|
+
current = [command];
|
|
162
|
+
result.push(current);
|
|
163
|
+
start = command;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (current === void 0) {
|
|
167
|
+
current = [{
|
|
168
|
+
op: "M",
|
|
169
|
+
x: start.x,
|
|
170
|
+
y: start.y
|
|
171
|
+
}];
|
|
172
|
+
result.push(current);
|
|
173
|
+
}
|
|
174
|
+
current.push(command);
|
|
175
|
+
if (command.op === "Z") current = void 0;
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
/** Represents a subpath as a polygon and samples curves. */
|
|
180
|
+
function outline(subpath) {
|
|
181
|
+
const points = [];
|
|
182
|
+
let x = 0;
|
|
183
|
+
let y = 0;
|
|
184
|
+
for (const command of subpath) {
|
|
185
|
+
if (command.op === "Z") continue;
|
|
186
|
+
if (command.op === "C" || command.op === "Q") for (let step = 1; step < 8; step += 1) {
|
|
187
|
+
const t = step / 8;
|
|
188
|
+
const u = 1 - t;
|
|
189
|
+
points.push(command.op === "C" ? {
|
|
190
|
+
x: u * u * u * x + 3 * u * u * t * command.x1 + 3 * u * t * t * command.x2 + t * t * t * command.x,
|
|
191
|
+
y: u * u * u * y + 3 * u * u * t * command.y1 + 3 * u * t * t * command.y2 + t * t * t * command.y
|
|
192
|
+
} : {
|
|
193
|
+
x: u * u * x + 2 * u * t * command.x1 + t * t * command.x,
|
|
194
|
+
y: u * u * y + 2 * u * t * command.y1 + t * t * command.y
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
points.push({
|
|
198
|
+
x: command.x,
|
|
199
|
+
y: command.y
|
|
200
|
+
});
|
|
201
|
+
x = command.x;
|
|
202
|
+
y = command.y;
|
|
203
|
+
}
|
|
204
|
+
return points;
|
|
205
|
+
}
|
|
206
|
+
function signedArea(points) {
|
|
207
|
+
let area = 0;
|
|
208
|
+
points.forEach((point, index) => {
|
|
209
|
+
const next = points[(index + 1) % points.length] ?? point;
|
|
210
|
+
area += point.x * next.y - next.x * point.y;
|
|
211
|
+
});
|
|
212
|
+
return area / 2;
|
|
213
|
+
}
|
|
214
|
+
function contains(polygon, point) {
|
|
215
|
+
let inside = false;
|
|
216
|
+
polygon.forEach((a, index) => {
|
|
217
|
+
const b = polygon[(index + polygon.length - 1) % polygon.length] ?? a;
|
|
218
|
+
if (a.y > point.y !== b.y > point.y && point.x < (b.x - a.x) * (point.y - a.y) / (b.y - a.y) + a.x) inside = !inside;
|
|
219
|
+
});
|
|
220
|
+
return inside;
|
|
221
|
+
}
|
|
222
|
+
/** Draws the same subpath in reverse order. */
|
|
223
|
+
function reversed(subpath) {
|
|
224
|
+
const first = subpath[0];
|
|
225
|
+
if (first === void 0 || first.op !== "M") return [...subpath];
|
|
226
|
+
const closed = subpath.at(-1)?.op === "Z";
|
|
227
|
+
const segments = subpath.filter((command) => command.op === "L" || command.op === "C" || command.op === "Q");
|
|
228
|
+
const starts = [];
|
|
229
|
+
let end = first;
|
|
230
|
+
for (const segment of segments) {
|
|
231
|
+
starts.push(end);
|
|
232
|
+
end = segment;
|
|
233
|
+
}
|
|
234
|
+
if (closed && (end.x !== first.x || end.y !== first.y)) {
|
|
235
|
+
segments.push({
|
|
236
|
+
op: "L",
|
|
237
|
+
x: first.x,
|
|
238
|
+
y: first.y
|
|
239
|
+
});
|
|
240
|
+
starts.push(end);
|
|
241
|
+
end = first;
|
|
242
|
+
}
|
|
243
|
+
const result = [{
|
|
244
|
+
op: "M",
|
|
245
|
+
x: end.x,
|
|
246
|
+
y: end.y
|
|
247
|
+
}];
|
|
248
|
+
for (let index = segments.length - 1; index >= 0; index -= 1) {
|
|
249
|
+
const segment = segments[index];
|
|
250
|
+
const to = starts[index];
|
|
251
|
+
if (segment === void 0 || to === void 0) continue;
|
|
252
|
+
if (segment.op === "C") result.push({
|
|
253
|
+
op: "C",
|
|
254
|
+
x1: segment.x2,
|
|
255
|
+
y1: segment.y2,
|
|
256
|
+
x2: segment.x1,
|
|
257
|
+
y2: segment.y1,
|
|
258
|
+
x: to.x,
|
|
259
|
+
y: to.y
|
|
260
|
+
});
|
|
261
|
+
else if (segment.op === "Q") result.push({
|
|
262
|
+
op: "Q",
|
|
263
|
+
x1: segment.x1,
|
|
264
|
+
y1: segment.y1,
|
|
265
|
+
x: to.x,
|
|
266
|
+
y: to.y
|
|
267
|
+
});
|
|
268
|
+
else result.push({
|
|
269
|
+
op: "L",
|
|
270
|
+
x: to.x,
|
|
271
|
+
y: to.y
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
if (closed) result.push({ op: "Z" });
|
|
275
|
+
return result;
|
|
276
|
+
}
|
|
277
|
+
/** Outer contours are drawn in one direction, holes are reversed, and islands inside holes restore outer contour direction. */
|
|
278
|
+
function alternateByNesting(commands) {
|
|
279
|
+
const parts = subpaths(commands);
|
|
280
|
+
const outlines = parts.map(outline);
|
|
281
|
+
return parts.flatMap((part, index) => {
|
|
282
|
+
const shape = outlines[index] ?? [];
|
|
283
|
+
const area = signedArea(shape);
|
|
284
|
+
const probe = shape[0];
|
|
285
|
+
if (area === 0 || probe === void 0) return part;
|
|
286
|
+
const depth = outlines.filter((other, otherIndex) => otherIndex !== index && other.length > 2 && contains(other, probe)).length;
|
|
287
|
+
return Math.sign(area) === (depth % 2 === 0 ? 1 : -1) ? part : reversed(part);
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
const tokenPattern = /[MmLlHhVvCcSsQqTtAaZz]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?/gu;
|
|
291
|
+
function parseSvgPath(data) {
|
|
292
|
+
if (typeof data !== "string" || data.trim() === "") invalidGeometry("SVG path must be a non-empty string");
|
|
293
|
+
const tokens = [];
|
|
294
|
+
let cursor = 0;
|
|
295
|
+
for (const match of data.matchAll(tokenPattern)) {
|
|
296
|
+
const index = match.index;
|
|
297
|
+
if (!/^[\s,]*$/u.test(data.slice(cursor, index))) invalidGeometry(`SVG path contains unsupported character: ${data.slice(cursor, index)}`);
|
|
298
|
+
tokens.push(match[0]);
|
|
299
|
+
cursor = index + match[0].length;
|
|
300
|
+
}
|
|
301
|
+
if (!/^[\s,]*$/u.test(data.slice(cursor)) || tokens.length === 0) invalidGeometry(`SVG path contains unsupported or missing parameter: ${data.slice(cursor)}`);
|
|
302
|
+
const commands = [];
|
|
303
|
+
let index = 0;
|
|
304
|
+
let command = "";
|
|
305
|
+
let x = 0;
|
|
306
|
+
let y = 0;
|
|
307
|
+
let startX = 0;
|
|
308
|
+
let startY = 0;
|
|
309
|
+
let lastControl;
|
|
310
|
+
const isCommand = (token) => token !== void 0 && /^[A-Za-z]$/u.test(token);
|
|
311
|
+
const number = () => {
|
|
312
|
+
const token = tokens[index];
|
|
313
|
+
index += 1;
|
|
314
|
+
if (token === void 0 || isCommand(token)) throw new Error(`Invalid SVG path data: ${data}`);
|
|
315
|
+
const value = Number(token);
|
|
316
|
+
if (!Number.isFinite(value)) invalidGeometry("SVG path coordinate must be a finite number");
|
|
317
|
+
return value;
|
|
318
|
+
};
|
|
319
|
+
const flag = (name) => {
|
|
320
|
+
const token = tokens[index];
|
|
321
|
+
if (token !== "0" && token !== "1") invalidGeometry(`SVG arc ${name} must be a single 0 or 1`);
|
|
322
|
+
index += 1;
|
|
323
|
+
return token === "1";
|
|
324
|
+
};
|
|
325
|
+
while (index < tokens.length) {
|
|
326
|
+
if (isCommand(tokens[index])) {
|
|
327
|
+
command = tokens[index] ?? "";
|
|
328
|
+
index += 1;
|
|
329
|
+
} else if (command === "") invalidGeometry("SVG close command Z cannot be followed by implicit parameters");
|
|
330
|
+
const relative = command === command.toLowerCase();
|
|
331
|
+
const upper = command.toUpperCase();
|
|
332
|
+
const ox = relative ? x : 0;
|
|
333
|
+
const oy = relative ? y : 0;
|
|
334
|
+
switch (upper) {
|
|
335
|
+
case "M":
|
|
336
|
+
x = ox + number();
|
|
337
|
+
y = oy + number();
|
|
338
|
+
startX = x;
|
|
339
|
+
startY = y;
|
|
340
|
+
commands.push({
|
|
341
|
+
op: "M",
|
|
342
|
+
x,
|
|
343
|
+
y
|
|
344
|
+
});
|
|
345
|
+
command = relative ? "l" : "L";
|
|
346
|
+
lastControl = void 0;
|
|
347
|
+
break;
|
|
348
|
+
case "L":
|
|
349
|
+
x = ox + number();
|
|
350
|
+
y = oy + number();
|
|
351
|
+
commands.push({
|
|
352
|
+
op: "L",
|
|
353
|
+
x,
|
|
354
|
+
y
|
|
355
|
+
});
|
|
356
|
+
lastControl = void 0;
|
|
357
|
+
break;
|
|
358
|
+
case "H":
|
|
359
|
+
x = ox + number();
|
|
360
|
+
commands.push({
|
|
361
|
+
op: "L",
|
|
362
|
+
x,
|
|
363
|
+
y
|
|
364
|
+
});
|
|
365
|
+
lastControl = void 0;
|
|
366
|
+
break;
|
|
367
|
+
case "V":
|
|
368
|
+
y = oy + number();
|
|
369
|
+
commands.push({
|
|
370
|
+
op: "L",
|
|
371
|
+
x,
|
|
372
|
+
y
|
|
373
|
+
});
|
|
374
|
+
lastControl = void 0;
|
|
375
|
+
break;
|
|
376
|
+
case "C": {
|
|
377
|
+
const x1 = ox + number();
|
|
378
|
+
const y1 = oy + number();
|
|
379
|
+
const x2 = ox + number();
|
|
380
|
+
const y2 = oy + number();
|
|
381
|
+
x = ox + number();
|
|
382
|
+
y = oy + number();
|
|
383
|
+
commands.push({
|
|
384
|
+
op: "C",
|
|
385
|
+
x1,
|
|
386
|
+
y1,
|
|
387
|
+
x2,
|
|
388
|
+
y2,
|
|
389
|
+
x,
|
|
390
|
+
y
|
|
391
|
+
});
|
|
392
|
+
lastControl = {
|
|
393
|
+
x: x2,
|
|
394
|
+
y: y2,
|
|
395
|
+
kind: "C"
|
|
396
|
+
};
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
case "S": {
|
|
400
|
+
const x1 = lastControl?.kind === "C" ? 2 * x - lastControl.x : x;
|
|
401
|
+
const y1 = lastControl?.kind === "C" ? 2 * y - lastControl.y : y;
|
|
402
|
+
const x2 = ox + number();
|
|
403
|
+
const y2 = oy + number();
|
|
404
|
+
x = ox + number();
|
|
405
|
+
y = oy + number();
|
|
406
|
+
commands.push({
|
|
407
|
+
op: "C",
|
|
408
|
+
x1,
|
|
409
|
+
y1,
|
|
410
|
+
x2,
|
|
411
|
+
y2,
|
|
412
|
+
x,
|
|
413
|
+
y
|
|
414
|
+
});
|
|
415
|
+
lastControl = {
|
|
416
|
+
x: x2,
|
|
417
|
+
y: y2,
|
|
418
|
+
kind: "C"
|
|
419
|
+
};
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
case "Q": {
|
|
423
|
+
const x1 = ox + number();
|
|
424
|
+
const y1 = oy + number();
|
|
425
|
+
x = ox + number();
|
|
426
|
+
y = oy + number();
|
|
427
|
+
commands.push({
|
|
428
|
+
op: "Q",
|
|
429
|
+
x1,
|
|
430
|
+
y1,
|
|
431
|
+
x,
|
|
432
|
+
y
|
|
433
|
+
});
|
|
434
|
+
lastControl = {
|
|
435
|
+
x: x1,
|
|
436
|
+
y: y1,
|
|
437
|
+
kind: "Q"
|
|
438
|
+
};
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
case "T": {
|
|
442
|
+
const x1 = lastControl?.kind === "Q" ? 2 * x - lastControl.x : x;
|
|
443
|
+
const y1 = lastControl?.kind === "Q" ? 2 * y - lastControl.y : y;
|
|
444
|
+
x = ox + number();
|
|
445
|
+
y = oy + number();
|
|
446
|
+
commands.push({
|
|
447
|
+
op: "Q",
|
|
448
|
+
x1,
|
|
449
|
+
y1,
|
|
450
|
+
x,
|
|
451
|
+
y
|
|
452
|
+
});
|
|
453
|
+
lastControl = {
|
|
454
|
+
x: x1,
|
|
455
|
+
y: y1,
|
|
456
|
+
kind: "Q"
|
|
457
|
+
};
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
case "A": {
|
|
461
|
+
const rx = number();
|
|
462
|
+
const ry = number();
|
|
463
|
+
const rotation = number();
|
|
464
|
+
const largeArc = flag("large-arc-flag");
|
|
465
|
+
const sweep = flag("sweep-flag");
|
|
466
|
+
const endX = ox + number();
|
|
467
|
+
const endY = oy + number();
|
|
468
|
+
commands.push(...arcToCubics(x, y, rx, ry, rotation, largeArc, sweep, endX, endY));
|
|
469
|
+
x = endX;
|
|
470
|
+
y = endY;
|
|
471
|
+
lastControl = void 0;
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
case "Z":
|
|
475
|
+
commands.push({ op: "Z" });
|
|
476
|
+
x = startX;
|
|
477
|
+
y = startY;
|
|
478
|
+
lastControl = void 0;
|
|
479
|
+
command = "";
|
|
480
|
+
break;
|
|
481
|
+
default: invalidGeometry(`SVG path command ${command} is not supported`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (commands[0]?.op !== "M") invalidGeometry("SVG path must start with move command M");
|
|
485
|
+
return commands;
|
|
486
|
+
}
|
|
487
|
+
/** Converts elliptical arcs to cubic Bezier curves per SVG implementation note F.6. */
|
|
488
|
+
function arcToCubics(x1, y1, rxInput, ryInput, rotationDegrees, largeArc, sweep, x2, y2) {
|
|
489
|
+
if (x1 === x2 && y1 === y2) return [];
|
|
490
|
+
if (rxInput === 0 || ryInput === 0) return [{
|
|
491
|
+
op: "L",
|
|
492
|
+
x: x2,
|
|
493
|
+
y: y2
|
|
494
|
+
}];
|
|
495
|
+
const phi = rotationDegrees * Math.PI / 180;
|
|
496
|
+
const cos = Math.cos(phi);
|
|
497
|
+
const sin = Math.sin(phi);
|
|
498
|
+
const dx = (x1 - x2) / 2;
|
|
499
|
+
const dy = (y1 - y2) / 2;
|
|
500
|
+
const x1p = cos * dx + sin * dy;
|
|
501
|
+
const y1p = -sin * dx + cos * dy;
|
|
502
|
+
let rx = Math.abs(rxInput);
|
|
503
|
+
let ry = Math.abs(ryInput);
|
|
504
|
+
const lambda = x1p * x1p / (rx * rx) + y1p * y1p / (ry * ry);
|
|
505
|
+
if (lambda > 1) {
|
|
506
|
+
rx *= Math.sqrt(lambda);
|
|
507
|
+
ry *= Math.sqrt(lambda);
|
|
508
|
+
}
|
|
509
|
+
const numerator = rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;
|
|
510
|
+
const denominator = rx * rx * y1p * y1p + ry * ry * x1p * x1p;
|
|
511
|
+
const factor = (largeArc === sweep ? -1 : 1) * Math.sqrt(Math.max(0, numerator / denominator));
|
|
512
|
+
const cxp = factor * rx * y1p / ry;
|
|
513
|
+
const cyp = -factor * ry * x1p / rx;
|
|
514
|
+
const cx = cos * cxp - sin * cyp + (x1 + x2) / 2;
|
|
515
|
+
const cy = sin * cxp + cos * cyp + (y1 + y2) / 2;
|
|
516
|
+
const angle = (ux, uy, vx, vy) => {
|
|
517
|
+
const sign = ux * vy - uy * vx < 0 ? -1 : 1;
|
|
518
|
+
const dot = (ux * vx + uy * vy) / (Math.hypot(ux, uy) * Math.hypot(vx, vy));
|
|
519
|
+
return sign * Math.acos(Math.min(1, Math.max(-1, dot)));
|
|
520
|
+
};
|
|
521
|
+
const theta1 = angle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
|
|
522
|
+
let delta = angle((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);
|
|
523
|
+
if (!sweep && delta > 0) delta -= 2 * Math.PI;
|
|
524
|
+
if (sweep && delta < 0) delta += 2 * Math.PI;
|
|
525
|
+
const segments = Math.ceil(Math.abs(delta) / (Math.PI / 2));
|
|
526
|
+
const step = delta / segments;
|
|
527
|
+
const kappa = 4 / 3 * Math.tan(step / 4);
|
|
528
|
+
const point = (theta) => ({
|
|
529
|
+
x: cx + rx * Math.cos(theta) * cos - ry * Math.sin(theta) * sin,
|
|
530
|
+
y: cy + rx * Math.cos(theta) * sin + ry * Math.sin(theta) * cos
|
|
531
|
+
});
|
|
532
|
+
const derivative = (theta) => ({
|
|
533
|
+
x: -rx * Math.sin(theta) * cos - ry * Math.cos(theta) * sin,
|
|
534
|
+
y: -rx * Math.sin(theta) * sin + ry * Math.cos(theta) * cos
|
|
535
|
+
});
|
|
536
|
+
const result = [];
|
|
537
|
+
for (let segment = 0; segment < segments; segment += 1) {
|
|
538
|
+
const a = theta1 + segment * step;
|
|
539
|
+
const b = a + step;
|
|
540
|
+
const start = point(a);
|
|
541
|
+
const end = segment === segments - 1 ? {
|
|
542
|
+
x: x2,
|
|
543
|
+
y: y2
|
|
544
|
+
} : point(b);
|
|
545
|
+
const startDerivative = derivative(a);
|
|
546
|
+
const endDerivative = derivative(b);
|
|
547
|
+
result.push({
|
|
548
|
+
op: "C",
|
|
549
|
+
x1: start.x + kappa * startDerivative.x,
|
|
550
|
+
y1: start.y + kappa * startDerivative.y,
|
|
551
|
+
x2: end.x - kappa * endDerivative.x,
|
|
552
|
+
y2: end.y - kappa * endDerivative.y,
|
|
553
|
+
x: end.x,
|
|
554
|
+
y: end.y
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
return result;
|
|
558
|
+
}
|
|
559
|
+
function isRecord(value) {
|
|
560
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
561
|
+
}
|
|
562
|
+
function finiteGeometryNumber(value, label) {
|
|
563
|
+
if (typeof value !== "number" || !Number.isFinite(value)) throw new CapabilityError("invalid-visual-style", `${label} must be a finite number`);
|
|
564
|
+
return value;
|
|
565
|
+
}
|
|
566
|
+
/** Checks whether a path coordinate in point units (`value * 1000`) stays within DrawingML signed 32-bit bounds excluding `-2^31`. */
|
|
567
|
+
function isValidPathUnit(value) {
|
|
568
|
+
const units = Math.round(value * 1e3);
|
|
569
|
+
return Number.isSafeInteger(units) && Math.abs(units) <= 2147483647;
|
|
570
|
+
}
|
|
571
|
+
/** Checks whether a custom path viewBox dimension (`value * 1000`) stays strictly positive and within DrawingML bounds. */
|
|
572
|
+
function isValidPositivePathExtent(value) {
|
|
573
|
+
const units = Math.round(value * 1e3);
|
|
574
|
+
return Number.isSafeInteger(units) && units >= 1 && units <= 2147483647;
|
|
575
|
+
}
|
|
576
|
+
/** Checks whether every coordinate in a custom path stays within DrawingML path coordinate bounds. */
|
|
577
|
+
function arePathCommandsBounded(commands) {
|
|
578
|
+
return commands.every((cmd) => {
|
|
579
|
+
switch (cmd.op) {
|
|
580
|
+
case "Z": return true;
|
|
581
|
+
case "M":
|
|
582
|
+
case "L": return isValidPathUnit(cmd.x) && isValidPathUnit(cmd.y);
|
|
583
|
+
case "Q": return isValidPathUnit(cmd.x1) && isValidPathUnit(cmd.y1) && isValidPathUnit(cmd.x) && isValidPathUnit(cmd.y);
|
|
584
|
+
case "C": return isValidPathUnit(cmd.x1) && isValidPathUnit(cmd.y1) && isValidPathUnit(cmd.x2) && isValidPathUnit(cmd.y2) && isValidPathUnit(cmd.x) && isValidPathUnit(cmd.y);
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
function pointUnit(value, label) {
|
|
589
|
+
const coordinate = Math.round(finiteGeometryNumber(value, label) * 1e3);
|
|
590
|
+
if (!Number.isSafeInteger(coordinate) || Math.abs(coordinate) > 2147483647) invalidGeometry(`${label} exceeds DrawingML path coordinate range after conversion`);
|
|
591
|
+
return String(coordinate);
|
|
592
|
+
}
|
|
593
|
+
function validatePathCommand(command) {
|
|
594
|
+
switch (command.op) {
|
|
595
|
+
case "M":
|
|
596
|
+
case "L":
|
|
597
|
+
pointUnit(command.x, "path.x");
|
|
598
|
+
pointUnit(command.y, "path.y");
|
|
599
|
+
return;
|
|
600
|
+
case "C":
|
|
601
|
+
pointUnit(command.x1, "path.x1");
|
|
602
|
+
pointUnit(command.y1, "path.y1");
|
|
603
|
+
pointUnit(command.x2, "path.x2");
|
|
604
|
+
pointUnit(command.y2, "path.y2");
|
|
605
|
+
pointUnit(command.x, "path.x");
|
|
606
|
+
pointUnit(command.y, "path.y");
|
|
607
|
+
return;
|
|
608
|
+
case "Q":
|
|
609
|
+
pointUnit(command.x1, "path.x1");
|
|
610
|
+
pointUnit(command.y1, "path.y1");
|
|
611
|
+
pointUnit(command.x, "path.x");
|
|
612
|
+
pointUnit(command.y, "path.y");
|
|
613
|
+
return;
|
|
614
|
+
case "Z": return;
|
|
615
|
+
default: invalidGeometry("Custom path contains unsupported command");
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
/** Validates and resolves author Geometry (+ optional borderRadius) into a closed ResolvedGeometry model. */
|
|
619
|
+
function resolveGeometry(options) {
|
|
620
|
+
const { geometry: value, borderRadius: radius, box } = options;
|
|
621
|
+
if (isRecord(value) && value["type"] === "custom") {
|
|
622
|
+
if (radius !== void 0 && radius > 0) throw new CapabilityError("unsupported-style-combination", "borderRadius cannot be used together with custom geometry");
|
|
623
|
+
const parsedCustom = customGeometrySchema.safeParse(value);
|
|
624
|
+
if (!parsedCustom.success) {
|
|
625
|
+
const issue = parsedCustom.error.issues[0];
|
|
626
|
+
if (issue?.path[0] === "width" || issue?.path[0] === "height") {
|
|
627
|
+
if (typeof value[issue.path[0]] !== "number" || !Number.isFinite(value[issue.path[0]])) finiteGeometryNumber(value[issue.path[0]], `geometry.${issue.path[0]}`);
|
|
628
|
+
invalidGeometry("Custom geometry must have a positive viewBox and at least one path");
|
|
629
|
+
}
|
|
630
|
+
invalidGeometry(issue?.message ?? "Custom geometry must have a positive viewBox and at least one path");
|
|
631
|
+
}
|
|
632
|
+
const custom = parsedCustom.data;
|
|
633
|
+
const widthUnits = pointUnit(custom.width, "geometry.width");
|
|
634
|
+
const heightUnits = pointUnit(custom.height, "geometry.height");
|
|
635
|
+
if (Number(widthUnits) === 0 || Number(heightUnits) === 0) invalidGeometry("Custom geometry viewBox must retain positive width and height in path coordinate space after conversion");
|
|
636
|
+
const resolvedPaths = custom.paths.map((item) => {
|
|
637
|
+
for (const command of item.commands) validatePathCommand(command);
|
|
638
|
+
return {
|
|
639
|
+
commands: item.commands,
|
|
640
|
+
fill: item.fill ?? true,
|
|
641
|
+
stroke: item.stroke ?? true
|
|
642
|
+
};
|
|
643
|
+
});
|
|
644
|
+
const resolvedSites = custom.connectionSites?.map((site) => {
|
|
645
|
+
pointUnit(site.x, "connectionSite.x");
|
|
646
|
+
pointUnit(site.y, "connectionSite.y");
|
|
647
|
+
return {
|
|
648
|
+
x: site.x,
|
|
649
|
+
y: site.y
|
|
650
|
+
};
|
|
651
|
+
});
|
|
652
|
+
return {
|
|
653
|
+
geometry: {
|
|
654
|
+
kind: "custom",
|
|
655
|
+
width: custom.width,
|
|
656
|
+
height: custom.height,
|
|
657
|
+
paths: resolvedPaths,
|
|
658
|
+
...resolvedSites === void 0 ? {} : { connectionSites: resolvedSites }
|
|
659
|
+
},
|
|
660
|
+
name: "custom"
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
let rawName;
|
|
664
|
+
const adjust = {};
|
|
665
|
+
if (value === void 0) rawName = radius !== void 0 && radius > 0 ? "roundRect" : "rect";
|
|
666
|
+
else if (typeof value === "string") rawName = value;
|
|
667
|
+
else if (isRecord(value) && value["type"] === "preset" && typeof value["name"] === "string") {
|
|
668
|
+
rawName = value["name"];
|
|
669
|
+
if (value["adjust"] !== void 0) {
|
|
670
|
+
if (!isRecord(value["adjust"])) invalidGeometry("preset.adjust must be an object");
|
|
671
|
+
const parsedAdjust = geometryAdjustSchema.safeParse(value["adjust"]);
|
|
672
|
+
if (!parsedAdjust.success) invalidGeometry(`preset.adjust.${String(parsedAdjust.error.issues[0]?.path[0] ?? "adjust")} must be a finite integer`);
|
|
673
|
+
Object.assign(adjust, parsedAdjust.data);
|
|
674
|
+
}
|
|
675
|
+
} else invalidGeometry("geometry must be a preset name, preset(...), or custom path");
|
|
676
|
+
const parsedPresetName = presetGeometryNameSchema.safeParse(rawName);
|
|
677
|
+
if (!parsedPresetName.success) throw new CapabilityError("unsupported-geometry", `Unsupported preset geometry ${rawName}`);
|
|
678
|
+
let name = parsedPresetName.data;
|
|
679
|
+
if (radius !== void 0 && radius > 0) {
|
|
680
|
+
if (name !== "rect" && name !== "roundRect") throw new CapabilityError("unsupported-style-combination", "borderRadius is only applicable to rect or roundRect geometry");
|
|
681
|
+
name = "roundRect";
|
|
682
|
+
if (adjust["adj"] === void 0) {
|
|
683
|
+
const raw = Math.round(radius / Math.min(box.width, box.height) * 1e5);
|
|
684
|
+
adjust["adj"] = Math.max(0, Math.min(5e4, raw));
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return {
|
|
688
|
+
geometry: {
|
|
689
|
+
kind: "preset",
|
|
690
|
+
name,
|
|
691
|
+
adjust
|
|
692
|
+
},
|
|
693
|
+
name
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
function pathCommandXml(command) {
|
|
697
|
+
switch (command.op) {
|
|
698
|
+
case "M": return `<a:moveTo><a:pt x="${pointUnit(command.x, "path.x")}" y="${pointUnit(command.y, "path.y")}"/></a:moveTo>`;
|
|
699
|
+
case "L": return `<a:lnTo><a:pt x="${pointUnit(command.x, "path.x")}" y="${pointUnit(command.y, "path.y")}"/></a:lnTo>`;
|
|
700
|
+
case "C": return `<a:cubicBezTo><a:pt x="${pointUnit(command.x1, "path.x1")}" y="${pointUnit(command.y1, "path.y1")}"/><a:pt x="${pointUnit(command.x2, "path.x2")}" y="${pointUnit(command.y2, "path.y2")}"/><a:pt x="${pointUnit(command.x, "path.x")}" y="${pointUnit(command.y, "path.y")}"/></a:cubicBezTo>`;
|
|
701
|
+
case "Q": return `<a:quadBezTo><a:pt x="${pointUnit(command.x1, "path.x1")}" y="${pointUnit(command.y1, "path.y1")}"/><a:pt x="${pointUnit(command.x, "path.x")}" y="${pointUnit(command.y, "path.y")}"/></a:quadBezTo>`;
|
|
702
|
+
case "Z": return "<a:close/>";
|
|
703
|
+
default: return command;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
function clampPresetAdjustValue(presetName, key, amount) {
|
|
707
|
+
if (presetName === "roundRect" && key === "adj") return Math.max(0, Math.min(5e4, amount));
|
|
708
|
+
if (presetName === "blockArc") {
|
|
709
|
+
if (key === "adj1" || key === "adj2") return Math.max(0, Math.min(216e5, amount));
|
|
710
|
+
if (key === "adj3") return Math.max(0, Math.min(5e4, amount));
|
|
711
|
+
}
|
|
712
|
+
return amount;
|
|
713
|
+
}
|
|
714
|
+
/** Exhaustively serializes a ResolvedGeometry to DrawingML <a:prstGeom> or <a:custGeom>. */
|
|
715
|
+
function resolvedGeometryXml(geometry) {
|
|
716
|
+
switch (geometry.kind) {
|
|
717
|
+
case "preset": {
|
|
718
|
+
const values = Object.entries(geometry.adjust).map(([key, amount]) => `<a:gd name="${key}" fmla="val ${clampPresetAdjustValue(geometry.name, key, amount)}"/>`).join("");
|
|
719
|
+
return `<a:prstGeom prst="${geometry.name}"><a:avLst>${values}</a:avLst></a:prstGeom>`;
|
|
720
|
+
}
|
|
721
|
+
case "custom": {
|
|
722
|
+
const widthUnits = pointUnit(geometry.width, "geometry.width");
|
|
723
|
+
const heightUnits = pointUnit(geometry.height, "geometry.height");
|
|
724
|
+
const pathList = geometry.paths.map((pathItem) => {
|
|
725
|
+
const commands = pathItem.commands.map(pathCommandXml).join("");
|
|
726
|
+
return `<a:path w="${widthUnits}" h="${heightUnits}"${pathItem.fill ? "" : " fill=\"none\""}${pathItem.stroke ? "" : " stroke=\"0\""}>${commands}</a:path>`;
|
|
727
|
+
}).join("");
|
|
728
|
+
const sites = geometry.connectionSites ?? [];
|
|
729
|
+
return `<a:custGeom><a:avLst/>${sites.length === 0 ? "<a:gdLst/>" : `<a:gdLst>${sites.map((site, index) => {
|
|
730
|
+
const xUnits = pointUnit(site.x, "connectionSite.x");
|
|
731
|
+
const yUnits = pointUnit(site.y, "connectionSite.y");
|
|
732
|
+
return `<a:gd name="cxnX${index}" fmla="*/ ${xUnits} w ${widthUnits}"/><a:gd name="cxnY${index}" fmla="*/ ${yUnits} h ${heightUnits}"/>`;
|
|
733
|
+
}).join("")}</a:gdLst>`}<a:ahLst/>${sites.length === 0 ? "<a:cxnLst/>" : `<a:cxnLst>${sites.map((_site, index) => `<a:cxn ang="0"><a:pos x="cxnX${index}" y="cxnY${index}"/></a:cxn>`).join("")}</a:cxnLst>`}<a:rect l="l" t="t" r="r" b="b"/><a:pathLst>${pathList}</a:pathLst></a:custGeom>`;
|
|
734
|
+
}
|
|
735
|
+
default: return geometry;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
const drawingAngleToRad = (ang) => ang / 6e4 * (Math.PI / 180);
|
|
739
|
+
const radToDrawingAngle = (rad) => rad * (180 / Math.PI) * 6e4;
|
|
740
|
+
function formatSvgCoord(value) {
|
|
741
|
+
if (Object.is(value, -0) || value === 0) return 0;
|
|
742
|
+
return Math.round(value * 1e6) / 1e6;
|
|
743
|
+
}
|
|
744
|
+
function commandsToSvgD(commands) {
|
|
745
|
+
return commands.map((command) => {
|
|
746
|
+
switch (command.op) {
|
|
747
|
+
case "Z": return "Z";
|
|
748
|
+
case "M":
|
|
749
|
+
case "L": return `${command.op}${formatSvgCoord(command.x)} ${formatSvgCoord(command.y)}`;
|
|
750
|
+
case "Q": return `Q${formatSvgCoord(command.x1)} ${formatSvgCoord(command.y1)} ${formatSvgCoord(command.x)} ${formatSvgCoord(command.y)}`;
|
|
751
|
+
case "C": return `C${formatSvgCoord(command.x1)} ${formatSvgCoord(command.y1)} ${formatSvgCoord(command.x2)} ${formatSvgCoord(command.y2)} ${formatSvgCoord(command.x)} ${formatSvgCoord(command.y)}`;
|
|
752
|
+
default: return command;
|
|
753
|
+
}
|
|
754
|
+
}).join(" ");
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Unwraps the DrawingML polar angle onto the continuous eccentric parameter angle of an ellipse
|
|
758
|
+
* with semi-axes (rx, ry), preserving exact winding across arbitrary sweep angles.
|
|
759
|
+
*/
|
|
760
|
+
function eccentricAngle(theta, rx, ry) {
|
|
761
|
+
if (rx === ry) return theta;
|
|
762
|
+
const base = Math.atan2(rx * Math.sin(theta), ry * Math.cos(theta));
|
|
763
|
+
return base + 2 * Math.PI * Math.round((theta - base) / (2 * Math.PI));
|
|
764
|
+
}
|
|
765
|
+
function drawingMlArcToLocalCubics(curX, curY, wR, hR, stAng, swAng) {
|
|
766
|
+
if (swAng === 0) return {
|
|
767
|
+
commands: [],
|
|
768
|
+
endX: curX,
|
|
769
|
+
endY: curY
|
|
770
|
+
};
|
|
771
|
+
const rx = Math.max(0, wR);
|
|
772
|
+
const ry = Math.max(0, hR);
|
|
773
|
+
if (rx === 0 && ry === 0) return {
|
|
774
|
+
commands: [],
|
|
775
|
+
endX: curX,
|
|
776
|
+
endY: curY
|
|
777
|
+
};
|
|
778
|
+
const theta1 = drawingAngleToRad(stAng);
|
|
779
|
+
const theta2 = theta1 + drawingAngleToRad(swAng);
|
|
780
|
+
if (rx === 0 || ry === 0) {
|
|
781
|
+
const phi1 = rx === 0 ? Math.sin(theta1) >= 0 ? Math.PI / 2 : -Math.PI / 2 : Math.cos(theta1) >= 0 ? 0 : Math.PI;
|
|
782
|
+
const phi2 = rx === 0 ? Math.sin(theta2) >= 0 ? Math.PI / 2 : -Math.PI / 2 : Math.cos(theta2) >= 0 ? 0 : Math.PI;
|
|
783
|
+
const cx = curX - rx * Math.cos(phi1);
|
|
784
|
+
const cy = curY - ry * Math.sin(phi1);
|
|
785
|
+
const endX = cx + rx * Math.cos(phi2);
|
|
786
|
+
const endY = cy + ry * Math.sin(phi2);
|
|
787
|
+
return {
|
|
788
|
+
commands: [{
|
|
789
|
+
op: "L",
|
|
790
|
+
x: endX,
|
|
791
|
+
y: endY
|
|
792
|
+
}],
|
|
793
|
+
endX,
|
|
794
|
+
endY
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
const phi1 = eccentricAngle(theta1, rx, ry);
|
|
798
|
+
const phi2 = eccentricAngle(theta2, rx, ry);
|
|
799
|
+
const deltaPhi = phi2 - phi1;
|
|
800
|
+
const cx = curX - rx * Math.cos(phi1);
|
|
801
|
+
const cy = curY - ry * Math.sin(phi1);
|
|
802
|
+
const segments = Math.max(1, Math.ceil(Math.abs(deltaPhi) / (Math.PI / 2)));
|
|
803
|
+
const step = deltaPhi / segments;
|
|
804
|
+
const kappa = 4 / 3 * Math.tan(step / 4);
|
|
805
|
+
const result = [];
|
|
806
|
+
for (let segment = 0; segment < segments; segment += 1) {
|
|
807
|
+
const a = phi1 + segment * step;
|
|
808
|
+
const b = a + step;
|
|
809
|
+
const paX = cx + rx * Math.cos(a);
|
|
810
|
+
const paY = cy + ry * Math.sin(a);
|
|
811
|
+
const daX = -rx * Math.sin(a);
|
|
812
|
+
const daY = ry * Math.cos(a);
|
|
813
|
+
const pbX = cx + rx * Math.cos(b);
|
|
814
|
+
const pbY = cy + ry * Math.sin(b);
|
|
815
|
+
const dbX = -rx * Math.sin(b);
|
|
816
|
+
const dbY = ry * Math.cos(b);
|
|
817
|
+
result.push({
|
|
818
|
+
op: "C",
|
|
819
|
+
x1: paX + kappa * daX,
|
|
820
|
+
y1: paY + kappa * daY,
|
|
821
|
+
x2: pbX - kappa * dbX,
|
|
822
|
+
y2: pbY - kappa * dbY,
|
|
823
|
+
x: pbX,
|
|
824
|
+
y: pbY
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
return {
|
|
828
|
+
commands: result,
|
|
829
|
+
endX: cx + rx * Math.cos(phi2),
|
|
830
|
+
endY: cy + ry * Math.sin(phi2)
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
function createDrawingMlGuideEvaluator(wEmu, hEmu) {
|
|
834
|
+
const ss = Math.min(wEmu, hEmu);
|
|
835
|
+
const ls = Math.max(wEmu, hEmu);
|
|
836
|
+
const builtins = {
|
|
837
|
+
w: wEmu,
|
|
838
|
+
h: hEmu,
|
|
839
|
+
l: 0,
|
|
840
|
+
t: 0,
|
|
841
|
+
r: wEmu,
|
|
842
|
+
b: hEmu,
|
|
843
|
+
hc: wEmu / 2,
|
|
844
|
+
vc: hEmu / 2,
|
|
845
|
+
ss,
|
|
846
|
+
ls,
|
|
847
|
+
cd2: 108e5,
|
|
848
|
+
cd4: 54e5,
|
|
849
|
+
cd8: 27e5,
|
|
850
|
+
"3cd4": 162e5,
|
|
851
|
+
"3cd8": 81e5,
|
|
852
|
+
"5cd8": 135e5,
|
|
853
|
+
"7cd8": 189e5
|
|
854
|
+
};
|
|
855
|
+
const env = /* @__PURE__ */ new Map();
|
|
856
|
+
const resolveToken = (token) => {
|
|
857
|
+
const trimmed = token.trim();
|
|
858
|
+
if (trimmed.length === 0) return void 0;
|
|
859
|
+
const bound = env.get(trimmed);
|
|
860
|
+
if (bound !== void 0) return bound;
|
|
861
|
+
const builtin = builtins[trimmed];
|
|
862
|
+
if (builtin !== void 0) return builtin;
|
|
863
|
+
const denomMatch = /^(wd|hd|ssd)(\d+)$/u.exec(trimmed);
|
|
864
|
+
if (denomMatch !== null) {
|
|
865
|
+
const kind = denomMatch[1];
|
|
866
|
+
const divisor = Number(denomMatch[2]);
|
|
867
|
+
if (divisor <= 0) return void 0;
|
|
868
|
+
return kind === "wd" ? wEmu / divisor : kind === "hd" ? hEmu / divisor : ss / divisor;
|
|
869
|
+
}
|
|
870
|
+
if (!/^-?(?:\d+(?:\.\d+)?|\.\d+)$/u.test(trimmed)) return;
|
|
871
|
+
const numeric = Number(trimmed);
|
|
872
|
+
return Number.isFinite(numeric) ? numeric : void 0;
|
|
873
|
+
};
|
|
874
|
+
const evaluateFormula = (formula) => {
|
|
875
|
+
const parts = formula.trim().split(/\s+/u);
|
|
876
|
+
const op = parts[0];
|
|
877
|
+
if (op === void 0 || op.length === 0) return void 0;
|
|
878
|
+
const argAt = (index) => {
|
|
879
|
+
const token = parts[index];
|
|
880
|
+
return token === void 0 ? void 0 : resolveToken(token);
|
|
881
|
+
};
|
|
882
|
+
const x = argAt(1);
|
|
883
|
+
if (x === void 0) return void 0;
|
|
884
|
+
let result;
|
|
885
|
+
switch (op) {
|
|
886
|
+
case "val":
|
|
887
|
+
result = x;
|
|
888
|
+
break;
|
|
889
|
+
case "abs":
|
|
890
|
+
result = Math.abs(x);
|
|
891
|
+
break;
|
|
892
|
+
case "sqrt":
|
|
893
|
+
result = x <= 0 ? 0 : Math.sqrt(x);
|
|
894
|
+
break;
|
|
895
|
+
case "at2":
|
|
896
|
+
case "cos":
|
|
897
|
+
case "sin":
|
|
898
|
+
case "tan":
|
|
899
|
+
case "max":
|
|
900
|
+
case "min": {
|
|
901
|
+
const y = argAt(2);
|
|
902
|
+
if (y === void 0) return void 0;
|
|
903
|
+
if (op === "at2") result = radToDrawingAngle(Math.atan2(y, x));
|
|
904
|
+
else if (op === "cos") result = x * Math.cos(drawingAngleToRad(y));
|
|
905
|
+
else if (op === "sin") result = x * Math.sin(drawingAngleToRad(y));
|
|
906
|
+
else if (op === "tan") result = x * Math.tan(drawingAngleToRad(y));
|
|
907
|
+
else if (op === "max") result = Math.max(x, y);
|
|
908
|
+
else result = Math.min(x, y);
|
|
909
|
+
break;
|
|
910
|
+
}
|
|
911
|
+
case "*/":
|
|
912
|
+
case "+-":
|
|
913
|
+
case "+/":
|
|
914
|
+
case "?:":
|
|
915
|
+
case "ifelse":
|
|
916
|
+
case "cat2":
|
|
917
|
+
case "sat2":
|
|
918
|
+
case "mod":
|
|
919
|
+
case "pin": {
|
|
920
|
+
const y = argAt(2);
|
|
921
|
+
const z = argAt(3);
|
|
922
|
+
if (y === void 0 || z === void 0) return void 0;
|
|
923
|
+
if (op === "*/") result = z === 0 ? 0 : x * y / z;
|
|
924
|
+
else if (op === "+-") result = x + y - z;
|
|
925
|
+
else if (op === "+/") result = z === 0 ? 0 : (x + y) / z;
|
|
926
|
+
else if (op === "?:" || op === "ifelse") result = x > 0 ? y : z;
|
|
927
|
+
else if (op === "cat2") result = x * Math.cos(Math.atan2(z, y));
|
|
928
|
+
else if (op === "sat2") result = x * Math.sin(Math.atan2(z, y));
|
|
929
|
+
else if (op === "mod") result = Math.sqrt(x * x + y * y + z * z);
|
|
930
|
+
else result = y < x ? x : y > z ? z : y;
|
|
931
|
+
break;
|
|
932
|
+
}
|
|
933
|
+
default: return;
|
|
934
|
+
}
|
|
935
|
+
return Number.isFinite(result) ? result : void 0;
|
|
936
|
+
};
|
|
937
|
+
return {
|
|
938
|
+
resolveToken,
|
|
939
|
+
evaluateFormula,
|
|
940
|
+
setGuide(name, value) {
|
|
941
|
+
env.set(name, value);
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
}
|
|
945
|
+
function evaluatePresetGeometry(name, adjust, box) {
|
|
946
|
+
const def = presetDefinitions[name];
|
|
947
|
+
const W = Math.max(1, pxToEmu(box.width));
|
|
948
|
+
const H = Math.max(1, pxToEmu(box.height));
|
|
949
|
+
const evaluator = createDrawingMlGuideEvaluator(W, H);
|
|
950
|
+
const arg = (token) => {
|
|
951
|
+
const resolved = evaluator.resolveToken(token);
|
|
952
|
+
if (resolved === void 0) invalidGeometry(`Preset ${name} references unknown guide token ${token}`);
|
|
953
|
+
return resolved;
|
|
954
|
+
};
|
|
955
|
+
const evalFormula = (formula) => {
|
|
956
|
+
const evaluated = evaluator.evaluateFormula(formula);
|
|
957
|
+
if (evaluated === void 0) invalidGeometry(`Preset ${name} uses invalid guide formula ${formula}`);
|
|
958
|
+
return evaluated;
|
|
959
|
+
};
|
|
960
|
+
if (def.av !== void 0) for (const [key, formula] of def.av) evaluator.setGuide(key, evalFormula(formula));
|
|
961
|
+
for (const [key, value] of Object.entries(adjust)) evaluator.setGuide(key, value);
|
|
962
|
+
if (def.gd !== void 0) for (const [key, formula] of def.gd) evaluator.setGuide(key, evalFormula(formula));
|
|
963
|
+
const rectTokens = def.rect ?? [
|
|
964
|
+
"l",
|
|
965
|
+
"t",
|
|
966
|
+
"r",
|
|
967
|
+
"b"
|
|
968
|
+
];
|
|
969
|
+
const rectL = arg(rectTokens[0]);
|
|
970
|
+
const rectT = arg(rectTokens[1]);
|
|
971
|
+
const rectR = arg(rectTokens[2]);
|
|
972
|
+
const rectB = arg(rectTokens[3]);
|
|
973
|
+
const textRect = {
|
|
974
|
+
x: box.x + rectL / W * box.width,
|
|
975
|
+
y: box.y + rectT / H * box.height,
|
|
976
|
+
width: Math.max(0, (rectR - rectL) / W * box.width),
|
|
977
|
+
height: Math.max(0, (rectB - rectT) / H * box.height)
|
|
978
|
+
};
|
|
979
|
+
return {
|
|
980
|
+
paths: def.paths.map((pathDef) => {
|
|
981
|
+
const pathW = pathDef.w ?? W;
|
|
982
|
+
const pathH = pathDef.h ?? H;
|
|
983
|
+
const scaleX = box.width / pathW;
|
|
984
|
+
const scaleY = box.height / pathH;
|
|
985
|
+
const toPageX = (lx) => box.x + lx * scaleX;
|
|
986
|
+
const toPageY = (ly) => box.y + ly * scaleY;
|
|
987
|
+
const mapCmdToPage = (cmd) => {
|
|
988
|
+
switch (cmd.op) {
|
|
989
|
+
case "Z": return cmd;
|
|
990
|
+
case "M":
|
|
991
|
+
case "L": return {
|
|
992
|
+
op: cmd.op,
|
|
993
|
+
x: toPageX(cmd.x),
|
|
994
|
+
y: toPageY(cmd.y)
|
|
995
|
+
};
|
|
996
|
+
case "Q": return {
|
|
997
|
+
op: "Q",
|
|
998
|
+
x1: toPageX(cmd.x1),
|
|
999
|
+
y1: toPageY(cmd.y1),
|
|
1000
|
+
x: toPageX(cmd.x),
|
|
1001
|
+
y: toPageY(cmd.y)
|
|
1002
|
+
};
|
|
1003
|
+
case "C": return {
|
|
1004
|
+
op: "C",
|
|
1005
|
+
x1: toPageX(cmd.x1),
|
|
1006
|
+
y1: toPageY(cmd.y1),
|
|
1007
|
+
x2: toPageX(cmd.x2),
|
|
1008
|
+
y2: toPageY(cmd.y2),
|
|
1009
|
+
x: toPageX(cmd.x),
|
|
1010
|
+
y: toPageY(cmd.y)
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
const commands = [];
|
|
1015
|
+
let curX = 0;
|
|
1016
|
+
let curY = 0;
|
|
1017
|
+
let subStartX = 0;
|
|
1018
|
+
let subStartY = 0;
|
|
1019
|
+
for (const cmd of pathDef.cmds) switch (cmd[0]) {
|
|
1020
|
+
case "M":
|
|
1021
|
+
curX = arg(cmd[1]);
|
|
1022
|
+
curY = arg(cmd[2]);
|
|
1023
|
+
subStartX = curX;
|
|
1024
|
+
subStartY = curY;
|
|
1025
|
+
commands.push({
|
|
1026
|
+
op: "M",
|
|
1027
|
+
x: toPageX(curX),
|
|
1028
|
+
y: toPageY(curY)
|
|
1029
|
+
});
|
|
1030
|
+
break;
|
|
1031
|
+
case "L":
|
|
1032
|
+
curX = arg(cmd[1]);
|
|
1033
|
+
curY = arg(cmd[2]);
|
|
1034
|
+
commands.push({
|
|
1035
|
+
op: "L",
|
|
1036
|
+
x: toPageX(curX),
|
|
1037
|
+
y: toPageY(curY)
|
|
1038
|
+
});
|
|
1039
|
+
break;
|
|
1040
|
+
case "Q": {
|
|
1041
|
+
const x1 = arg(cmd[1]);
|
|
1042
|
+
const y1 = arg(cmd[2]);
|
|
1043
|
+
curX = arg(cmd[3]);
|
|
1044
|
+
curY = arg(cmd[4]);
|
|
1045
|
+
commands.push({
|
|
1046
|
+
op: "Q",
|
|
1047
|
+
x1: toPageX(x1),
|
|
1048
|
+
y1: toPageY(y1),
|
|
1049
|
+
x: toPageX(curX),
|
|
1050
|
+
y: toPageY(curY)
|
|
1051
|
+
});
|
|
1052
|
+
break;
|
|
1053
|
+
}
|
|
1054
|
+
case "C": {
|
|
1055
|
+
const x1 = arg(cmd[1]);
|
|
1056
|
+
const y1 = arg(cmd[2]);
|
|
1057
|
+
const x2 = arg(cmd[3]);
|
|
1058
|
+
const y2 = arg(cmd[4]);
|
|
1059
|
+
curX = arg(cmd[5]);
|
|
1060
|
+
curY = arg(cmd[6]);
|
|
1061
|
+
commands.push({
|
|
1062
|
+
op: "C",
|
|
1063
|
+
x1: toPageX(x1),
|
|
1064
|
+
y1: toPageY(y1),
|
|
1065
|
+
x2: toPageX(x2),
|
|
1066
|
+
y2: toPageY(y2),
|
|
1067
|
+
x: toPageX(curX),
|
|
1068
|
+
y: toPageY(curY)
|
|
1069
|
+
});
|
|
1070
|
+
break;
|
|
1071
|
+
}
|
|
1072
|
+
case "A": {
|
|
1073
|
+
const wR = arg(cmd[1]);
|
|
1074
|
+
const hR = arg(cmd[2]);
|
|
1075
|
+
const stAng = arg(cmd[3]);
|
|
1076
|
+
const swAng = arg(cmd[4]);
|
|
1077
|
+
const arc = drawingMlArcToLocalCubics(curX, curY, wR, hR, stAng, swAng);
|
|
1078
|
+
for (const cubic of arc.commands) commands.push(mapCmdToPage(cubic));
|
|
1079
|
+
curX = arc.endX;
|
|
1080
|
+
curY = arc.endY;
|
|
1081
|
+
break;
|
|
1082
|
+
}
|
|
1083
|
+
case "Z":
|
|
1084
|
+
curX = subStartX;
|
|
1085
|
+
curY = subStartY;
|
|
1086
|
+
commands.push({ op: "Z" });
|
|
1087
|
+
}
|
|
1088
|
+
return {
|
|
1089
|
+
d: commandsToSvgD(commands),
|
|
1090
|
+
commands,
|
|
1091
|
+
fillMode: pathDef.fill ?? "norm",
|
|
1092
|
+
stroke: pathDef.stroke ?? true
|
|
1093
|
+
};
|
|
1094
|
+
}),
|
|
1095
|
+
textRect,
|
|
1096
|
+
connectionSites: (def.cxn ?? STANDARD_CARDINAL_CXN).map(([cxToken, cyToken]) => ({
|
|
1097
|
+
x: box.x + arg(cxToken) / W * box.width,
|
|
1098
|
+
y: box.y + arg(cyToken) / H * box.height
|
|
1099
|
+
}))
|
|
1100
|
+
};
|
|
1101
|
+
}
|
|
1102
|
+
function evaluateCustomGeometry(geometry, box) {
|
|
1103
|
+
const mapX = (value) => box.x + value * box.width / geometry.width;
|
|
1104
|
+
const mapY = (value) => box.y + value * box.height / geometry.height;
|
|
1105
|
+
return {
|
|
1106
|
+
paths: geometry.paths.map((pathItem) => {
|
|
1107
|
+
const commands = pathItem.commands.map((command) => {
|
|
1108
|
+
switch (command.op) {
|
|
1109
|
+
case "Z": return command;
|
|
1110
|
+
case "M":
|
|
1111
|
+
case "L": return {
|
|
1112
|
+
op: command.op,
|
|
1113
|
+
x: mapX(command.x),
|
|
1114
|
+
y: mapY(command.y)
|
|
1115
|
+
};
|
|
1116
|
+
case "Q": return {
|
|
1117
|
+
op: "Q",
|
|
1118
|
+
x1: mapX(command.x1),
|
|
1119
|
+
y1: mapY(command.y1),
|
|
1120
|
+
x: mapX(command.x),
|
|
1121
|
+
y: mapY(command.y)
|
|
1122
|
+
};
|
|
1123
|
+
case "C": return {
|
|
1124
|
+
op: "C",
|
|
1125
|
+
x1: mapX(command.x1),
|
|
1126
|
+
y1: mapY(command.y1),
|
|
1127
|
+
x2: mapX(command.x2),
|
|
1128
|
+
y2: mapY(command.y2),
|
|
1129
|
+
x: mapX(command.x),
|
|
1130
|
+
y: mapY(command.y)
|
|
1131
|
+
};
|
|
1132
|
+
default: return command;
|
|
1133
|
+
}
|
|
1134
|
+
});
|
|
1135
|
+
return {
|
|
1136
|
+
d: commandsToSvgD(commands),
|
|
1137
|
+
commands,
|
|
1138
|
+
fillMode: pathItem.fill ? "norm" : "none",
|
|
1139
|
+
stroke: pathItem.stroke
|
|
1140
|
+
};
|
|
1141
|
+
}),
|
|
1142
|
+
textRect: box,
|
|
1143
|
+
connectionSites: geometry.connectionSites !== void 0 ? geometry.connectionSites.map((site) => ({
|
|
1144
|
+
x: mapX(site.x),
|
|
1145
|
+
y: mapY(site.y)
|
|
1146
|
+
})) : standardBoxConnectionSites(box)
|
|
1147
|
+
};
|
|
1148
|
+
}
|
|
1149
|
+
/** Exhaustively evaluates any ResolvedGeometry into page-space SVG paths and embedded textRect. */
|
|
1150
|
+
function evaluateGeometry(geometry, box) {
|
|
1151
|
+
switch (geometry.kind) {
|
|
1152
|
+
case "preset": return evaluatePresetGeometry(geometry.name, geometry.adjust, box);
|
|
1153
|
+
case "custom": return evaluateCustomGeometry(geometry, box);
|
|
1154
|
+
default: return geometry;
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
/** Modulates RGB hex color lightness using DrawingML HSL lumMod/lumOff rules. */
|
|
1158
|
+
function shadeHex(hex, lumMod, lumOff) {
|
|
1159
|
+
const [r, g, b] = [
|
|
1160
|
+
0,
|
|
1161
|
+
2,
|
|
1162
|
+
4
|
|
1163
|
+
].map((start) => Number.parseInt(hex.slice(start, start + 2), 16) / 255);
|
|
1164
|
+
const max = Math.max(r, g, b);
|
|
1165
|
+
const min = Math.min(r, g, b);
|
|
1166
|
+
const lightness = (max + min) / 2;
|
|
1167
|
+
const delta = max - min;
|
|
1168
|
+
const saturation = delta === 0 ? 0 : delta / (1 - Math.abs(2 * lightness - 1));
|
|
1169
|
+
const hue = delta === 0 ? 0 : max === r ? ((g - b) / delta + 6) % 6 : max === g ? (b - r) / delta + 2 : (r - g) / delta + 4;
|
|
1170
|
+
const shaded = Math.min(1, Math.max(0, lightness * lumMod + lumOff));
|
|
1171
|
+
const chroma = (1 - Math.abs(2 * shaded - 1)) * saturation;
|
|
1172
|
+
const x = chroma * (1 - Math.abs(hue % 2 - 1));
|
|
1173
|
+
const m = shaded - chroma / 2;
|
|
1174
|
+
const [r1, g1, b1] = hue < 1 ? [
|
|
1175
|
+
chroma,
|
|
1176
|
+
x,
|
|
1177
|
+
0
|
|
1178
|
+
] : hue < 2 ? [
|
|
1179
|
+
x,
|
|
1180
|
+
chroma,
|
|
1181
|
+
0
|
|
1182
|
+
] : hue < 3 ? [
|
|
1183
|
+
0,
|
|
1184
|
+
chroma,
|
|
1185
|
+
x
|
|
1186
|
+
] : hue < 4 ? [
|
|
1187
|
+
0,
|
|
1188
|
+
x,
|
|
1189
|
+
chroma
|
|
1190
|
+
] : hue < 5 ? [
|
|
1191
|
+
x,
|
|
1192
|
+
0,
|
|
1193
|
+
chroma
|
|
1194
|
+
] : [
|
|
1195
|
+
chroma,
|
|
1196
|
+
0,
|
|
1197
|
+
x
|
|
1198
|
+
];
|
|
1199
|
+
return [
|
|
1200
|
+
r1,
|
|
1201
|
+
g1,
|
|
1202
|
+
b1
|
|
1203
|
+
].map((channel) => Math.round((channel + m) * 255).toString(16).padStart(2, "0")).join("").toUpperCase();
|
|
1204
|
+
}
|
|
1205
|
+
const PresetGeomAttrSchema = z.object({ prst: z.string().min(1) });
|
|
1206
|
+
const AdjustGuideXmlSchema = z.object({
|
|
1207
|
+
name: z.string().min(1),
|
|
1208
|
+
fmla: z.string().regex(/^val\s+(-?\d+)$/u)
|
|
1209
|
+
});
|
|
1210
|
+
const ooxmlInt32Schema = ooxmlIntSchema.refine((value) => value >= -2147483648 && value <= 2147483647);
|
|
1211
|
+
const ooxmlPositiveInt32Schema = ooxmlPositiveIntSchema.refine((value) => value <= 2147483647);
|
|
1212
|
+
const CustomPathAttrSchema = z.object({
|
|
1213
|
+
w: ooxmlPositiveInt32Schema.optional(),
|
|
1214
|
+
h: ooxmlPositiveInt32Schema.optional(),
|
|
1215
|
+
fill: z.enum([
|
|
1216
|
+
"none",
|
|
1217
|
+
"norm",
|
|
1218
|
+
"lighten",
|
|
1219
|
+
"lightenLess",
|
|
1220
|
+
"darken",
|
|
1221
|
+
"darkenLess"
|
|
1222
|
+
]).optional(),
|
|
1223
|
+
stroke: ooxmlBoolSchema.optional()
|
|
1224
|
+
});
|
|
1225
|
+
const CustomPtAttrSchema = z.object({
|
|
1226
|
+
x: ooxmlInt32Schema,
|
|
1227
|
+
y: ooxmlInt32Schema
|
|
1228
|
+
});
|
|
1229
|
+
const CustomArcAttrSchema = z.object({
|
|
1230
|
+
wR: ooxmlIntSchema,
|
|
1231
|
+
hR: ooxmlIntSchema,
|
|
1232
|
+
stAng: ooxmlIntSchema,
|
|
1233
|
+
swAng: ooxmlIntSchema
|
|
1234
|
+
});
|
|
1235
|
+
/** Reads DrawingML `<a:prstGeom>` (and `<a:avLst>`) into author `Geometry`, emitting localized diagnostics on invalid/unsupported values. */
|
|
1236
|
+
function readPresetGeometryXml(prstGeomCursor, location, diagnostics) {
|
|
1237
|
+
const raw = prstGeomCursor.attrsObject(["prst"]);
|
|
1238
|
+
const parsedAttr = PresetGeomAttrSchema.safeParse(raw);
|
|
1239
|
+
if (!parsedAttr.success) {
|
|
1240
|
+
diagnostics.push({
|
|
1241
|
+
code: "invalid-xml-value",
|
|
1242
|
+
category: "corrupt-input",
|
|
1243
|
+
disposition: "recovered",
|
|
1244
|
+
location: {
|
|
1245
|
+
...location,
|
|
1246
|
+
xpath: prstGeomCursor.xpath
|
|
1247
|
+
},
|
|
1248
|
+
attributeOrElement: "a:prstGeom/@prst",
|
|
1249
|
+
rawValue: raw["prst"] ?? ""
|
|
1250
|
+
});
|
|
1251
|
+
prstGeomCursor.ignoreEmptyChild(A_NS, "avLst");
|
|
1252
|
+
return "rect";
|
|
1253
|
+
}
|
|
1254
|
+
const parsedName = presetGeometryNameSchema.safeParse(parsedAttr.data.prst);
|
|
1255
|
+
if (!parsedName.success) {
|
|
1256
|
+
diagnostics.push({
|
|
1257
|
+
code: "unsupported-mechanism",
|
|
1258
|
+
category: "unsupported",
|
|
1259
|
+
disposition: "omitted",
|
|
1260
|
+
location: {
|
|
1261
|
+
...location,
|
|
1262
|
+
xpath: prstGeomCursor.xpath
|
|
1263
|
+
},
|
|
1264
|
+
namespaceUri: A_NS,
|
|
1265
|
+
localName: "prstGeom",
|
|
1266
|
+
mechanismHint: `preset:${parsedAttr.data.prst}`
|
|
1267
|
+
});
|
|
1268
|
+
prstGeomCursor.ignoreEmptyChild(A_NS, "avLst");
|
|
1269
|
+
return "rect";
|
|
1270
|
+
}
|
|
1271
|
+
const adjust = {};
|
|
1272
|
+
const avLst = prstGeomCursor.child(A_NS, "avLst");
|
|
1273
|
+
if (avLst !== void 0) for (const gd of avLst.children(A_NS, "gd")) {
|
|
1274
|
+
const rawGd = gd.attrsObject(["name", "fmla"]);
|
|
1275
|
+
const parsedXml = AdjustGuideXmlSchema.safeParse(rawGd);
|
|
1276
|
+
if (!parsedXml.success) {
|
|
1277
|
+
diagnostics.push({
|
|
1278
|
+
code: "invalid-xml-value",
|
|
1279
|
+
category: "corrupt-input",
|
|
1280
|
+
disposition: "omitted",
|
|
1281
|
+
location: {
|
|
1282
|
+
...location,
|
|
1283
|
+
xpath: gd.xpath
|
|
1284
|
+
},
|
|
1285
|
+
attributeOrElement: "a:gd",
|
|
1286
|
+
rawValue: JSON.stringify(rawGd)
|
|
1287
|
+
});
|
|
1288
|
+
continue;
|
|
1289
|
+
}
|
|
1290
|
+
if (!geometryAdjustKeySchema.safeParse(parsedXml.data.name).success) {
|
|
1291
|
+
diagnostics.push({
|
|
1292
|
+
code: "unsupported-mechanism",
|
|
1293
|
+
category: "unsupported",
|
|
1294
|
+
disposition: "omitted",
|
|
1295
|
+
location: {
|
|
1296
|
+
...location,
|
|
1297
|
+
xpath: gd.xpath
|
|
1298
|
+
},
|
|
1299
|
+
namespaceUri: A_NS,
|
|
1300
|
+
localName: "gd",
|
|
1301
|
+
mechanismHint: `geometry-adjust:${parsedXml.data.name}`
|
|
1302
|
+
});
|
|
1303
|
+
continue;
|
|
1304
|
+
}
|
|
1305
|
+
const match = /^val\s+(-?\d+)$/u.exec(parsedXml.data.fmla);
|
|
1306
|
+
if (match !== null) {
|
|
1307
|
+
const value = Number(match[1]);
|
|
1308
|
+
if (Number.isSafeInteger(value)) adjust[parsedXml.data.name] = value;
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
return Object.keys(adjust).length === 0 ? parsedName.data : {
|
|
1312
|
+
type: "preset",
|
|
1313
|
+
name: parsedName.data,
|
|
1314
|
+
adjust
|
|
1315
|
+
};
|
|
1316
|
+
}
|
|
1317
|
+
function readCustomPoint(ptCursor, location, diagnostics) {
|
|
1318
|
+
const raw = ptCursor.attrsObject(["x", "y"]);
|
|
1319
|
+
const parsed = CustomPtAttrSchema.safeParse(raw);
|
|
1320
|
+
if (!parsed.success) {
|
|
1321
|
+
const isFormulaRef = raw["x"] !== void 0 && !/^-?\d+$/u.test(raw["x"]) || raw["y"] !== void 0 && !/^-?\d+$/u.test(raw["y"]);
|
|
1322
|
+
diagnostics.push(isFormulaRef ? {
|
|
1323
|
+
code: "unsupported-mechanism",
|
|
1324
|
+
category: "unsupported",
|
|
1325
|
+
disposition: "omitted",
|
|
1326
|
+
location: {
|
|
1327
|
+
...location,
|
|
1328
|
+
xpath: ptCursor.xpath
|
|
1329
|
+
},
|
|
1330
|
+
namespaceUri: A_NS,
|
|
1331
|
+
localName: "pt",
|
|
1332
|
+
mechanismHint: "custom-geometry-guide-reference"
|
|
1333
|
+
} : {
|
|
1334
|
+
code: "invalid-xml-value",
|
|
1335
|
+
category: "corrupt-input",
|
|
1336
|
+
disposition: "omitted",
|
|
1337
|
+
location: {
|
|
1338
|
+
...location,
|
|
1339
|
+
xpath: ptCursor.xpath
|
|
1340
|
+
},
|
|
1341
|
+
attributeOrElement: "a:pt",
|
|
1342
|
+
rawValue: JSON.stringify(raw)
|
|
1343
|
+
});
|
|
1344
|
+
return;
|
|
1345
|
+
}
|
|
1346
|
+
return {
|
|
1347
|
+
x: parsed.data.x / 1e3,
|
|
1348
|
+
y: parsed.data.y / 1e3
|
|
1349
|
+
};
|
|
1350
|
+
}
|
|
1351
|
+
/** Reads DrawingML `<a:custGeom>` into `CustomGeometry`, reporting localized diagnostics on unsupported guide formulas or invalid paths. */
|
|
1352
|
+
function readCustomGeometryXml(custGeomCursor, fallbackExtentUnits, location, diagnostics) {
|
|
1353
|
+
const hasPositiveShapeExtent = Number.isFinite(fallbackExtentUnits.w) && Number.isFinite(fallbackExtentUnits.h) && fallbackExtentUnits.w > 0 && fallbackExtentUnits.h > 0;
|
|
1354
|
+
const guideEval = createDrawingMlGuideEvaluator(fallbackExtentUnits.w, fallbackExtentUnits.h);
|
|
1355
|
+
const readGuideList = (listCursor) => {
|
|
1356
|
+
if (listCursor === void 0) return;
|
|
1357
|
+
for (const gd of listCursor.children(A_NS, "gd")) {
|
|
1358
|
+
const { name, fmla } = gd.attrsObject(["name", "fmla"]);
|
|
1359
|
+
if (hasPositiveShapeExtent && name !== void 0 && fmla !== void 0) {
|
|
1360
|
+
const value = guideEval.evaluateFormula(fmla);
|
|
1361
|
+
if (value !== void 0) guideEval.setGuide(name, value);
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
};
|
|
1365
|
+
readGuideList(custGeomCursor.child(A_NS, "avLst"));
|
|
1366
|
+
readGuideList(custGeomCursor.child(A_NS, "gdLst"));
|
|
1367
|
+
custGeomCursor.ignoreEmptyChild(A_NS, "ahLst");
|
|
1368
|
+
const cxnLstCursor = custGeomCursor.child(A_NS, "cxnLst");
|
|
1369
|
+
const rawCxnPositions = [];
|
|
1370
|
+
if (cxnLstCursor !== void 0) for (const cxnCursor of cxnLstCursor.children(A_NS, "cxn")) {
|
|
1371
|
+
cxnCursor.consumeAttr("ang");
|
|
1372
|
+
const posCursor = cxnCursor.child(A_NS, "pos");
|
|
1373
|
+
if (posCursor === void 0) {
|
|
1374
|
+
rawCxnPositions.push({
|
|
1375
|
+
xpath: cxnCursor.xpath,
|
|
1376
|
+
rawValue: "missing <a:pos>"
|
|
1377
|
+
});
|
|
1378
|
+
continue;
|
|
1379
|
+
}
|
|
1380
|
+
const rawPos = posCursor.attrsObject(["x", "y"]);
|
|
1381
|
+
const posX = hasPositiveShapeExtent && rawPos["x"] !== void 0 ? guideEval.resolveToken(rawPos["x"]) : void 0;
|
|
1382
|
+
const posY = hasPositiveShapeExtent && rawPos["y"] !== void 0 ? guideEval.resolveToken(rawPos["y"]) : void 0;
|
|
1383
|
+
rawCxnPositions.push({
|
|
1384
|
+
xpath: posCursor.xpath,
|
|
1385
|
+
rawValue: JSON.stringify(rawPos),
|
|
1386
|
+
...posX === void 0 ? {} : { posX },
|
|
1387
|
+
...posY === void 0 ? {} : { posY }
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1390
|
+
const rect = custGeomCursor.child(A_NS, "rect");
|
|
1391
|
+
if (rect !== void 0) {
|
|
1392
|
+
rect.consumeAttr("l");
|
|
1393
|
+
rect.consumeAttr("t");
|
|
1394
|
+
rect.consumeAttr("r");
|
|
1395
|
+
rect.consumeAttr("b");
|
|
1396
|
+
}
|
|
1397
|
+
const pathLst = custGeomCursor.child(A_NS, "pathLst");
|
|
1398
|
+
if (pathLst === void 0) {
|
|
1399
|
+
diagnostics.push({
|
|
1400
|
+
code: "invalid-xml-value",
|
|
1401
|
+
category: "corrupt-input",
|
|
1402
|
+
disposition: "omitted",
|
|
1403
|
+
location: {
|
|
1404
|
+
...location,
|
|
1405
|
+
xpath: custGeomCursor.xpath
|
|
1406
|
+
},
|
|
1407
|
+
attributeOrElement: "a:custGeom/a:pathLst"
|
|
1408
|
+
});
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
const paths = [];
|
|
1412
|
+
let viewWidth;
|
|
1413
|
+
let viewHeight;
|
|
1414
|
+
for (const pathCursor of pathLst.children(A_NS, "path")) {
|
|
1415
|
+
const rawPath = pathCursor.attrsObject([
|
|
1416
|
+
"w",
|
|
1417
|
+
"h",
|
|
1418
|
+
"fill",
|
|
1419
|
+
"stroke"
|
|
1420
|
+
]);
|
|
1421
|
+
const parsedPath = CustomPathAttrSchema.safeParse(rawPath);
|
|
1422
|
+
if (!parsedPath.success) {
|
|
1423
|
+
diagnostics.push({
|
|
1424
|
+
code: "invalid-xml-value",
|
|
1425
|
+
category: "corrupt-input",
|
|
1426
|
+
disposition: "omitted",
|
|
1427
|
+
location: {
|
|
1428
|
+
...location,
|
|
1429
|
+
xpath: pathCursor.xpath
|
|
1430
|
+
},
|
|
1431
|
+
attributeOrElement: "a:path",
|
|
1432
|
+
rawValue: JSON.stringify(rawPath)
|
|
1433
|
+
});
|
|
1434
|
+
continue;
|
|
1435
|
+
}
|
|
1436
|
+
const pathWUnits = parsedPath.data.w ?? fallbackExtentUnits.w;
|
|
1437
|
+
const pathHUnits = parsedPath.data.h ?? fallbackExtentUnits.h;
|
|
1438
|
+
const pathWidth = pathWUnits / 1e3;
|
|
1439
|
+
const pathHeight = pathHUnits / 1e3;
|
|
1440
|
+
viewWidth ??= pathWidth;
|
|
1441
|
+
viewHeight ??= pathHeight;
|
|
1442
|
+
const scaleX = pathWidth === viewWidth ? 1 : viewWidth / pathWidth;
|
|
1443
|
+
const scaleY = pathHeight === viewHeight ? 1 : viewHeight / pathHeight;
|
|
1444
|
+
const commands = [];
|
|
1445
|
+
let curX = 0;
|
|
1446
|
+
let curY = 0;
|
|
1447
|
+
let subStartX = 0;
|
|
1448
|
+
let subStartY = 0;
|
|
1449
|
+
let pathFailed = false;
|
|
1450
|
+
for (const cmdCursor of pathCursor.remainingElementChildren()) {
|
|
1451
|
+
if (cmdCursor.namespaceUri !== "http://schemas.openxmlformats.org/drawingml/2006/main") {
|
|
1452
|
+
diagnostics.push({
|
|
1453
|
+
code: "unconsumed-xml",
|
|
1454
|
+
category: "unsupported",
|
|
1455
|
+
disposition: "omitted",
|
|
1456
|
+
kind: "element",
|
|
1457
|
+
location: {
|
|
1458
|
+
...location,
|
|
1459
|
+
xpath: cmdCursor.xpath
|
|
1460
|
+
},
|
|
1461
|
+
namespaceUri: cmdCursor.namespaceUri,
|
|
1462
|
+
localName: cmdCursor.localName
|
|
1463
|
+
});
|
|
1464
|
+
continue;
|
|
1465
|
+
}
|
|
1466
|
+
switch (cmdCursor.localName) {
|
|
1467
|
+
case "moveTo": {
|
|
1468
|
+
const pt = cmdCursor.child(A_NS, "pt");
|
|
1469
|
+
const coords = pt === void 0 ? void 0 : readCustomPoint(pt, location, diagnostics);
|
|
1470
|
+
if (coords === void 0) {
|
|
1471
|
+
pathFailed = true;
|
|
1472
|
+
break;
|
|
1473
|
+
}
|
|
1474
|
+
curX = coords.x * scaleX;
|
|
1475
|
+
curY = coords.y * scaleY;
|
|
1476
|
+
subStartX = curX;
|
|
1477
|
+
subStartY = curY;
|
|
1478
|
+
commands.push({
|
|
1479
|
+
op: "M",
|
|
1480
|
+
x: curX,
|
|
1481
|
+
y: curY
|
|
1482
|
+
});
|
|
1483
|
+
break;
|
|
1484
|
+
}
|
|
1485
|
+
case "lnTo": {
|
|
1486
|
+
const pt = cmdCursor.child(A_NS, "pt");
|
|
1487
|
+
const coords = pt === void 0 ? void 0 : readCustomPoint(pt, location, diagnostics);
|
|
1488
|
+
if (coords === void 0) {
|
|
1489
|
+
pathFailed = true;
|
|
1490
|
+
break;
|
|
1491
|
+
}
|
|
1492
|
+
curX = coords.x * scaleX;
|
|
1493
|
+
curY = coords.y * scaleY;
|
|
1494
|
+
commands.push({
|
|
1495
|
+
op: "L",
|
|
1496
|
+
x: curX,
|
|
1497
|
+
y: curY
|
|
1498
|
+
});
|
|
1499
|
+
break;
|
|
1500
|
+
}
|
|
1501
|
+
case "cubicBezTo": {
|
|
1502
|
+
const pts = cmdCursor.children(A_NS, "pt");
|
|
1503
|
+
if (pts.length !== 3) {
|
|
1504
|
+
pathFailed = true;
|
|
1505
|
+
break;
|
|
1506
|
+
}
|
|
1507
|
+
const p1 = readCustomPoint(pts[0], location, diagnostics);
|
|
1508
|
+
const p2 = readCustomPoint(pts[1], location, diagnostics);
|
|
1509
|
+
const p3 = readCustomPoint(pts[2], location, diagnostics);
|
|
1510
|
+
if (p1 === void 0 || p2 === void 0 || p3 === void 0) {
|
|
1511
|
+
pathFailed = true;
|
|
1512
|
+
break;
|
|
1513
|
+
}
|
|
1514
|
+
curX = p3.x * scaleX;
|
|
1515
|
+
curY = p3.y * scaleY;
|
|
1516
|
+
commands.push({
|
|
1517
|
+
op: "C",
|
|
1518
|
+
x1: p1.x * scaleX,
|
|
1519
|
+
y1: p1.y * scaleY,
|
|
1520
|
+
x2: p2.x * scaleX,
|
|
1521
|
+
y2: p2.y * scaleY,
|
|
1522
|
+
x: curX,
|
|
1523
|
+
y: curY
|
|
1524
|
+
});
|
|
1525
|
+
break;
|
|
1526
|
+
}
|
|
1527
|
+
case "quadBezTo": {
|
|
1528
|
+
const pts = cmdCursor.children(A_NS, "pt");
|
|
1529
|
+
if (pts.length !== 2) {
|
|
1530
|
+
pathFailed = true;
|
|
1531
|
+
break;
|
|
1532
|
+
}
|
|
1533
|
+
const p1 = readCustomPoint(pts[0], location, diagnostics);
|
|
1534
|
+
const p2 = readCustomPoint(pts[1], location, diagnostics);
|
|
1535
|
+
if (p1 === void 0 || p2 === void 0) {
|
|
1536
|
+
pathFailed = true;
|
|
1537
|
+
break;
|
|
1538
|
+
}
|
|
1539
|
+
curX = p2.x * scaleX;
|
|
1540
|
+
curY = p2.y * scaleY;
|
|
1541
|
+
commands.push({
|
|
1542
|
+
op: "Q",
|
|
1543
|
+
x1: p1.x * scaleX,
|
|
1544
|
+
y1: p1.y * scaleY,
|
|
1545
|
+
x: curX,
|
|
1546
|
+
y: curY
|
|
1547
|
+
});
|
|
1548
|
+
break;
|
|
1549
|
+
}
|
|
1550
|
+
case "arcTo": {
|
|
1551
|
+
const rawArc = cmdCursor.attrsObject([
|
|
1552
|
+
"wR",
|
|
1553
|
+
"hR",
|
|
1554
|
+
"stAng",
|
|
1555
|
+
"swAng"
|
|
1556
|
+
]);
|
|
1557
|
+
const parsedArc = CustomArcAttrSchema.safeParse(rawArc);
|
|
1558
|
+
if (!parsedArc.success) {
|
|
1559
|
+
diagnostics.push({
|
|
1560
|
+
code: "unsupported-mechanism",
|
|
1561
|
+
category: "unsupported",
|
|
1562
|
+
disposition: "omitted",
|
|
1563
|
+
location: {
|
|
1564
|
+
...location,
|
|
1565
|
+
xpath: cmdCursor.xpath
|
|
1566
|
+
},
|
|
1567
|
+
namespaceUri: A_NS,
|
|
1568
|
+
localName: "arcTo",
|
|
1569
|
+
mechanismHint: "custom-geometry-arc-guide"
|
|
1570
|
+
});
|
|
1571
|
+
pathFailed = true;
|
|
1572
|
+
break;
|
|
1573
|
+
}
|
|
1574
|
+
const arc = drawingMlArcToLocalCubics(curX / scaleX, curY / scaleY, parsedArc.data.wR / 1e3, parsedArc.data.hR / 1e3, parsedArc.data.stAng, parsedArc.data.swAng);
|
|
1575
|
+
for (const cubic of arc.commands) if (cubic.op === "C") commands.push({
|
|
1576
|
+
op: "C",
|
|
1577
|
+
x1: cubic.x1 * scaleX,
|
|
1578
|
+
y1: cubic.y1 * scaleY,
|
|
1579
|
+
x2: cubic.x2 * scaleX,
|
|
1580
|
+
y2: cubic.y2 * scaleY,
|
|
1581
|
+
x: cubic.x * scaleX,
|
|
1582
|
+
y: cubic.y * scaleY
|
|
1583
|
+
});
|
|
1584
|
+
else if (cubic.op === "L") commands.push({
|
|
1585
|
+
op: "L",
|
|
1586
|
+
x: cubic.x * scaleX,
|
|
1587
|
+
y: cubic.y * scaleY
|
|
1588
|
+
});
|
|
1589
|
+
curX = arc.endX * scaleX;
|
|
1590
|
+
curY = arc.endY * scaleY;
|
|
1591
|
+
break;
|
|
1592
|
+
}
|
|
1593
|
+
case "close":
|
|
1594
|
+
curX = subStartX;
|
|
1595
|
+
curY = subStartY;
|
|
1596
|
+
commands.push({ op: "Z" });
|
|
1597
|
+
break;
|
|
1598
|
+
default: diagnostics.push({
|
|
1599
|
+
code: "unsupported-mechanism",
|
|
1600
|
+
category: "unsupported",
|
|
1601
|
+
disposition: "omitted",
|
|
1602
|
+
location: {
|
|
1603
|
+
...location,
|
|
1604
|
+
xpath: cmdCursor.xpath
|
|
1605
|
+
},
|
|
1606
|
+
namespaceUri: cmdCursor.namespaceUri,
|
|
1607
|
+
localName: cmdCursor.localName
|
|
1608
|
+
});
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
if (pathFailed || commands.length === 0) continue;
|
|
1612
|
+
if (!arePathCommandsBounded(commands)) {
|
|
1613
|
+
diagnostics.push({
|
|
1614
|
+
code: "invalid-xml-value",
|
|
1615
|
+
category: "corrupt-input",
|
|
1616
|
+
disposition: "omitted",
|
|
1617
|
+
location: {
|
|
1618
|
+
...location,
|
|
1619
|
+
xpath: pathCursor.xpath
|
|
1620
|
+
},
|
|
1621
|
+
attributeOrElement: "a:path"
|
|
1622
|
+
});
|
|
1623
|
+
continue;
|
|
1624
|
+
}
|
|
1625
|
+
paths.push({
|
|
1626
|
+
commands,
|
|
1627
|
+
...parsedPath.data.fill === "none" ? { fill: false } : {},
|
|
1628
|
+
...parsedPath.data.stroke === false ? { stroke: false } : {}
|
|
1629
|
+
});
|
|
1630
|
+
}
|
|
1631
|
+
if (viewWidth === void 0 || viewHeight === void 0 || viewWidth <= 0 || viewHeight <= 0 || paths.length === 0) return;
|
|
1632
|
+
let importedConnectionSites;
|
|
1633
|
+
if (rawCxnPositions.length > 0) {
|
|
1634
|
+
const mappedSites = [];
|
|
1635
|
+
let failedCxn;
|
|
1636
|
+
for (const rawSite of rawCxnPositions) {
|
|
1637
|
+
if (!hasPositiveShapeExtent || rawSite.posX === void 0 || rawSite.posY === void 0 || !Number.isFinite(rawSite.posX) || !Number.isFinite(rawSite.posY) || Math.abs(emuToPx(rawSite.posX)) > 1e6 || Math.abs(emuToPx(rawSite.posY)) > 1e6) {
|
|
1638
|
+
failedCxn = {
|
|
1639
|
+
xpath: rawSite.xpath,
|
|
1640
|
+
rawValue: rawSite.rawValue
|
|
1641
|
+
};
|
|
1642
|
+
break;
|
|
1643
|
+
}
|
|
1644
|
+
const mappedX = rawSite.posX / fallbackExtentUnits.w * viewWidth;
|
|
1645
|
+
const mappedY = rawSite.posY / fallbackExtentUnits.h * viewHeight;
|
|
1646
|
+
const unitX = Math.round(mappedX * 1e3);
|
|
1647
|
+
const unitY = Math.round(mappedY * 1e3);
|
|
1648
|
+
if (!Number.isFinite(mappedX) || !Number.isFinite(mappedY) || !Number.isSafeInteger(unitX) || Math.abs(unitX) > 2147483647 || !Number.isSafeInteger(unitY) || Math.abs(unitY) > 2147483647) {
|
|
1649
|
+
failedCxn = {
|
|
1650
|
+
xpath: rawSite.xpath,
|
|
1651
|
+
rawValue: rawSite.rawValue
|
|
1652
|
+
};
|
|
1653
|
+
break;
|
|
1654
|
+
}
|
|
1655
|
+
mappedSites.push({
|
|
1656
|
+
x: mappedX,
|
|
1657
|
+
y: mappedY
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1660
|
+
if (failedCxn !== void 0) diagnostics.push({
|
|
1661
|
+
code: "invalid-xml-value",
|
|
1662
|
+
category: "corrupt-input",
|
|
1663
|
+
disposition: "omitted",
|
|
1664
|
+
location: {
|
|
1665
|
+
...location,
|
|
1666
|
+
xpath: failedCxn.xpath
|
|
1667
|
+
},
|
|
1668
|
+
attributeOrElement: "a:cxnLst",
|
|
1669
|
+
rawValue: failedCxn.rawValue
|
|
1670
|
+
});
|
|
1671
|
+
else if (mappedSites.length > 0) importedConnectionSites = mappedSites;
|
|
1672
|
+
}
|
|
1673
|
+
return {
|
|
1674
|
+
type: "custom",
|
|
1675
|
+
width: viewWidth,
|
|
1676
|
+
height: viewHeight,
|
|
1677
|
+
paths,
|
|
1678
|
+
...importedConnectionSites === void 0 ? {} : { connectionSites: importedConnectionSites }
|
|
1679
|
+
};
|
|
1680
|
+
}
|
|
1681
|
+
export { applyTransformToPoints, arePathCommandsBounded, blockArc, customGeometrySchema, evaluateGeometry, geometryAdjustKeySchema, geometryAdjustSchema, geometrySchema, getGeometryConnectionSiteCount, isValidPathUnit, isValidPositivePathExtent, parseSvgPath, path, pathOptionsSchema, preset, presetGeometryNameSchema, presetGeometryNames, presetGeometryTuple, readCustomGeometryXml, readPresetGeometryXml, resolveGeometry, resolvedGeometryXml, shadeHex, standardBoxConnectionSites };
|