@evotecit/officeimo 0.1.19 → 0.1.43
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/README.md +44 -10
- package/bundles/officeimo-canopyx-adapter.js +147 -65
- package/bundles/officeimo-canopyx-adapter.min.js +1 -1
- package/bundles/officeimo-canopyx-adapter.min.mjs +1 -1
- package/bundles/officeimo-canopyx-adapter.mjs +134 -53
- package/bundles/officeimo-canopyx.js +554 -234
- package/bundles/officeimo-canopyx.min.js +1 -1
- package/bundles/officeimo-canopyx.min.mjs +1 -1
- package/bundles/officeimo-canopyx.mjs +477 -158
- package/bundles/officeimo-chartforgex-adapter.js +1924 -0
- package/bundles/officeimo-chartforgex-adapter.min.js +2 -0
- package/bundles/officeimo-chartforgex-adapter.min.mjs +2 -0
- package/bundles/officeimo-chartforgex-adapter.mjs +1910 -0
- package/bundles/officeimo-chartforgex.js +13594 -0
- package/bundles/officeimo-chartforgex.min.js +2 -0
- package/bundles/officeimo-chartforgex.min.mjs +2 -0
- package/bundles/officeimo-chartforgex.mjs +13487 -0
- package/bundles/officeimo-csv.js +12 -7
- package/bundles/officeimo-csv.min.js +1 -1
- package/bundles/officeimo-csv.min.mjs +1 -1
- package/bundles/officeimo-csv.mjs +8 -3
- package/bundles/officeimo-datatables-adapter.js +11 -11
- package/bundles/officeimo-datatables-adapter.min.js +1 -1
- package/bundles/officeimo-datatables-adapter.min.mjs +1 -1
- package/bundles/officeimo-datatables-adapter.mjs +7 -7
- package/bundles/officeimo-datatables.js +418 -180
- package/bundles/officeimo-datatables.min.js +1 -1
- package/bundles/officeimo-datatables.min.mjs +1 -1
- package/bundles/officeimo-datatables.mjs +350 -112
- package/bundles/officeimo-execution.js +14 -9
- package/bundles/officeimo-execution.min.js +1 -1
- package/bundles/officeimo-execution.min.mjs +1 -1
- package/bundles/officeimo-execution.mjs +8 -3
- package/bundles/officeimo-pdf.js +286 -120
- package/bundles/officeimo-pdf.min.js +1 -1
- package/bundles/officeimo-pdf.min.mjs +1 -1
- package/bundles/officeimo-pdf.mjs +244 -78
- package/bundles/officeimo-xlsx.js +136 -59
- package/bundles/officeimo-xlsx.min.js +1 -1
- package/bundles/officeimo-xlsx.min.mjs +1 -1
- package/bundles/officeimo-xlsx.mjs +114 -37
- package/bundles/officeimo.js +416 -178
- package/bundles/officeimo.min.js +1 -1
- package/bundles/officeimo.min.mjs +1 -1
- package/bundles/officeimo.mjs +351 -113
- package/dist/core/progress.js +7 -2
- package/dist/index.d.ts +1 -1
- package/dist/integrations/canopyx/index.js +13 -4
- package/dist/integrations/canopyx/pdf-number.js +9 -6
- package/dist/integrations/canopyx/rule-predicates.d.ts +1 -1
- package/dist/integrations/canopyx/rule-predicates.js +2 -1
- package/dist/integrations/canopyx/rules.d.ts +1 -1
- package/dist/integrations/canopyx/rules.js +11 -13
- package/dist/integrations/canopyx/source.js +57 -7
- package/dist/integrations/canopyx/tones.d.ts +10 -0
- package/dist/integrations/canopyx/tones.js +12 -0
- package/dist/integrations/canopyx/types.d.ts +8 -1
- package/dist/integrations/chartforgex/capture.d.ts +31 -0
- package/dist/integrations/chartforgex/capture.js +103 -0
- package/dist/integrations/chartforgex/index.d.ts +13 -0
- package/dist/integrations/chartforgex/index.js +88 -0
- package/dist/integrations/chartforgex/svg-drawing.d.ts +5 -0
- package/dist/integrations/chartforgex/svg-drawing.js +775 -0
- package/dist/integrations/chartforgex/svg-geometry.d.ts +23 -0
- package/dist/integrations/chartforgex/svg-geometry.js +304 -0
- package/dist/integrations/chartforgex/svg-image.d.ts +9 -0
- package/dist/integrations/chartforgex/svg-image.js +46 -0
- package/dist/integrations/chartforgex/svg-markup.d.ts +10 -0
- package/dist/integrations/chartforgex/svg-markup.js +124 -0
- package/dist/integrations/chartforgex/svg-paint.d.ts +16 -0
- package/dist/integrations/chartforgex/svg-paint.js +146 -0
- package/dist/integrations/chartforgex/types.d.ts +77 -0
- package/dist/integrations/chartforgex/types.js +1 -0
- package/dist/integrations/chartforgex/winansi.d.ts +4 -0
- package/dist/integrations/chartforgex/winansi.js +48 -0
- package/dist/integrations/chartforgex/workbook.d.ts +12 -0
- package/dist/integrations/chartforgex/workbook.js +137 -0
- package/dist/pdf/document-drawings.d.ts +1 -1
- package/dist/pdf/document-drawings.js +91 -17
- package/dist/pdf/document-images.d.ts +7 -1
- package/dist/pdf/document-images.js +59 -24
- package/dist/pdf/document-types.d.ts +3 -1
- package/dist/pdf/document.d.ts +2 -2
- package/dist/pdf/document.js +3 -1
- package/dist/pdf/drawing-types.d.ts +37 -10
- package/dist/pdf/font-resources.d.ts +1 -1
- package/dist/pdf/font-resources.js +6 -3
- package/dist/pdf/index.d.ts +1 -1
- package/dist/pdf/pages.js +15 -0
- package/dist/pdf/png.d.ts +11 -2
- package/dist/pdf/png.js +50 -23
- package/dist/pdf/session.js +3 -2
- package/dist/pdf/settings.d.ts +2 -2
- package/dist/pdf/settings.js +7 -3
- package/dist/xlsx/options.js +2 -2
- package/dist/xlsx/portable.js +1 -1
- package/dist/xlsx/styles.js +28 -18
- package/dist/xlsx/types.d.ts +13 -1
- package/dist/xlsx/workbook.js +9 -1
- package/dist/xlsx/worksheet.d.ts +13 -1
- package/dist/xlsx/worksheet.js +65 -11
- package/package.json +9 -2
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
import { NotSupportedError, OfficeIMOError } from "../../core/errors.js";
|
|
2
|
+
import { checkAbort } from "../../core/iteration.js";
|
|
3
|
+
import { pdfNumber } from "../../pdf/objects.js";
|
|
4
|
+
import { capturePdfLimits, pdfDrawingLimits, pdfDrawingMaxDepth } from "../../pdf/settings.js";
|
|
5
|
+
import { svgDataImage } from "./svg-image.js";
|
|
6
|
+
import { parseSvgMarkup } from "./svg-markup.js";
|
|
7
|
+
import { apply, determinant, ellipse, identity, multiply, numbers, parsePathData, parseTransform, polyline, rectangle, scaleOf, transformCommands } from "./svg-geometry.js";
|
|
8
|
+
import { alpha, flatten, length, parseColor, parseStyle } from "./svg-paint.js";
|
|
9
|
+
import { winAnsiText } from "./winansi.js";
|
|
10
|
+
const inherited = new Set(["fill", "fill-opacity", "fill-rule", "stroke", "stroke-opacity", "stroke-width", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "paint-order",
|
|
11
|
+
"stroke-dasharray", "stroke-dashoffset", "font-size", "font-weight", "font-style", "text-anchor", "dominant-baseline", "alignment-baseline",
|
|
12
|
+
"color", "visibility", "white-space"]);
|
|
13
|
+
const own = ["opacity", "display", "clip-path", "mask", "filter", "marker-start", "marker-mid", "marker-end", "vector-effect", "overflow"];
|
|
14
|
+
const skipped = new Set(["defs", "clipPath", "mask", "marker", "pattern", "linearGradient", "radialGradient", "symbol", "script", "title", "desc",
|
|
15
|
+
"metadata", "filter", "stop", "font", "font-face"]);
|
|
16
|
+
const containers = new Set(["svg", "symbol", "g", "a", "use", "switch"]);
|
|
17
|
+
const shapes = new Set(["path", "rect", "circle", "ellipse", "line", "polyline", "polygon"]);
|
|
18
|
+
const initial = new Map([["fill", "black"], ["stroke", "none"], ["stroke-width", "1"], ["font-size", "16"], ["color", "black"]]);
|
|
19
|
+
const nonUniform = (m) => {
|
|
20
|
+
const tolerance = 1e-6, xScale = Math.hypot(m[0], m[1]), yScale = Math.hypot(m[2], m[3]);
|
|
21
|
+
return Math.abs(xScale - yScale) > tolerance * Math.max(xScale, yScale) ||
|
|
22
|
+
Math.abs(m[0] * m[2] + m[1] * m[3]) > tolerance * xScale * yScale;
|
|
23
|
+
};
|
|
24
|
+
export function svgToPdfDrawing(svg, options, version, signal) {
|
|
25
|
+
const limits = options.limits === undefined ? undefined : capturePdfLimits(options.limits);
|
|
26
|
+
const maxElements = limits?.maxDrawingElements ?? pdfDrawingLimits.maxDrawingElements;
|
|
27
|
+
const maxCommands = limits?.maxVectorCommands ?? pdfDrawingLimits.maxVectorCommands;
|
|
28
|
+
const maxWork = Math.max(4096, 16 * Math.max(maxElements, maxCommands));
|
|
29
|
+
let elementCount = 0, commandCount = 0, work = 0;
|
|
30
|
+
const charge = (amount = 1) => {
|
|
31
|
+
checkAbort(signal);
|
|
32
|
+
work += amount;
|
|
33
|
+
if (work > maxWork)
|
|
34
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "Chart SVG conversion work limit exceeded.");
|
|
35
|
+
};
|
|
36
|
+
const chargeText = (value) => { if (value)
|
|
37
|
+
charge(Math.ceil(value.length / 16)); };
|
|
38
|
+
const addElement = (target, element) => {
|
|
39
|
+
if (++elementCount > maxElements)
|
|
40
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxDrawingElements exceeded.");
|
|
41
|
+
target.push(element);
|
|
42
|
+
};
|
|
43
|
+
if (typeof svg === "string")
|
|
44
|
+
charge(Math.ceil(svg.length / 16));
|
|
45
|
+
const root = parseSvgMarkup(svg), ids = new Map(), reported = new Set();
|
|
46
|
+
const fonts = new Set(options.fonts ?? ["regular", "bold", "italic", "boldItalic"]), winAnsi = options.winAnsi !== false;
|
|
47
|
+
const index = (node) => {
|
|
48
|
+
charge();
|
|
49
|
+
const id = node.attributes.get("id");
|
|
50
|
+
if (id !== undefined && !ids.has(id))
|
|
51
|
+
ids.set(id, node);
|
|
52
|
+
for (const child of node.children)
|
|
53
|
+
if (typeof child !== "string")
|
|
54
|
+
index(child);
|
|
55
|
+
};
|
|
56
|
+
index(root);
|
|
57
|
+
// Skippable effects are reported once per code; reject turns the first into an error.
|
|
58
|
+
const diagnose = (code, element) => {
|
|
59
|
+
if (options.unsupported === "reject")
|
|
60
|
+
throw new NotSupportedError("Unsupported chart SVG feature: " + code + (element ? " (<" + element + ">)" : ""));
|
|
61
|
+
if (reported.has(code))
|
|
62
|
+
return;
|
|
63
|
+
reported.add(code);
|
|
64
|
+
const diagnostic = element === undefined ? { code } : { code, element };
|
|
65
|
+
options.onDiagnostic?.(diagnostic);
|
|
66
|
+
};
|
|
67
|
+
const attribute = (node, name) => node.attributes.get(name);
|
|
68
|
+
const viewport = (node, fontSize, fallbackWidth, fallbackHeight, nested = false, size) => {
|
|
69
|
+
const viewBox = attribute(node, "viewBox");
|
|
70
|
+
chargeText(viewBox);
|
|
71
|
+
const box = numbers(viewBox ?? ""), hasBox = box.length === 4 && box[2] > 0 && box[3] > 0;
|
|
72
|
+
const referenceWidth = nested || !hasBox ? fallbackWidth : box[2], referenceHeight = nested || !hasBox ? fallbackHeight : box[3];
|
|
73
|
+
const width = size?.width ?? length(attribute(node, "width"), fontSize, referenceWidth) ?? referenceWidth;
|
|
74
|
+
const height = size?.height ?? length(attribute(node, "height"), fontSize, referenceHeight) ?? referenceHeight;
|
|
75
|
+
if (!hasBox)
|
|
76
|
+
return { width, height, userWidth: width, userHeight: height, originZero: true, matrix: identity };
|
|
77
|
+
const aspect = (attribute(node, "preserveAspectRatio") ?? "xMidYMid meet").trim().split(/\s+/);
|
|
78
|
+
let sx = width / box[2], sy = height / box[3], tx = 0, ty = 0;
|
|
79
|
+
if (aspect[0] !== "none") {
|
|
80
|
+
const scale = aspect[1] === "slice" ? Math.max(sx, sy) : Math.min(sx, sy), align = aspect[0] ?? "xMidYMid";
|
|
81
|
+
tx = (width - box[2] * scale) * (align.includes("xMid") ? .5 : align.includes("xMax") ? 1 : 0);
|
|
82
|
+
ty = (height - box[3] * scale) * (align.includes("YMid") ? .5 : align.includes("YMax") ? 1 : 0);
|
|
83
|
+
sx = sy = scale;
|
|
84
|
+
}
|
|
85
|
+
const matrix = [sx, 0, 0, sy, tx - box[0] * sx, ty - box[1] * sy];
|
|
86
|
+
return { width, height, userWidth: box[2], userHeight: box[3], originZero: box[0] === 0 && box[1] === 0, matrix };
|
|
87
|
+
};
|
|
88
|
+
const outer = viewport(root, 16, 0, 0);
|
|
89
|
+
if (!(outer.width > 0 && outer.height > 0))
|
|
90
|
+
throw new TypeError("Chart SVG needs a positive width and height or viewBox.");
|
|
91
|
+
// Points at 96 CSS px per inch, reduced further only when the drawing would exceed the PDF drawing bounds.
|
|
92
|
+
const unit = Math.min(.75, 14400 / outer.width, 14400 / outer.height);
|
|
93
|
+
const drawingWidth = Math.min(14400, outer.width * unit), drawingHeight = Math.min(14400, outer.height * unit);
|
|
94
|
+
const elements = [];
|
|
95
|
+
function declarations(node) {
|
|
96
|
+
const inline = attribute(node, "style");
|
|
97
|
+
chargeText(inline);
|
|
98
|
+
return parseStyle(inline);
|
|
99
|
+
}
|
|
100
|
+
function localTransform(node, declared, parent, fontSize) {
|
|
101
|
+
const read = (name) => declared.get(name) ?? attribute(node, name);
|
|
102
|
+
const value = read("transform");
|
|
103
|
+
chargeText(value);
|
|
104
|
+
const matrix = parseTransform(value);
|
|
105
|
+
if (!matrix) {
|
|
106
|
+
diagnose("TRANSFORM_UNSUPPORTED", node.local);
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
if (matrix[0] === 1 && matrix[1] === 0 && matrix[2] === 0 && matrix[3] === 1)
|
|
110
|
+
return matrix;
|
|
111
|
+
const box = read("transform-box") ?? "view-box";
|
|
112
|
+
if (parent.viewportOriginZero && ["view-box", "initial", "unset"].includes(box)) {
|
|
113
|
+
const origin = read("transform-origin");
|
|
114
|
+
if (origin === undefined)
|
|
115
|
+
return matrix;
|
|
116
|
+
chargeText(origin);
|
|
117
|
+
const [first, second, third, extra] = origin.trim().split(/\s+/, 4);
|
|
118
|
+
const x = length(first, fontSize, parent.userWidth), y = length(second, fontSize, parent.userHeight);
|
|
119
|
+
if (x !== undefined && y !== undefined && (third === undefined || /^0(?:px)?$/.test(third)) && extra === undefined)
|
|
120
|
+
return [matrix[0], matrix[1], matrix[2], matrix[3], matrix[4] + x - matrix[0] * x - matrix[2] * y,
|
|
121
|
+
matrix[5] + y - matrix[1] * x - matrix[3] * y];
|
|
122
|
+
}
|
|
123
|
+
diagnose("TRANSFORM_ORIGIN_IGNORED", node.local);
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
function cascade(node, parent) {
|
|
127
|
+
const declared = declarations(node), values = new Map();
|
|
128
|
+
let style = parent.style, changed = false;
|
|
129
|
+
const read = (name) => declared.get(name) ?? attribute(node, name);
|
|
130
|
+
for (const name of inherited) {
|
|
131
|
+
const authored = read(name);
|
|
132
|
+
if (authored === undefined || authored === "inherit" || authored === "unset")
|
|
133
|
+
continue;
|
|
134
|
+
const value = authored === "initial" ? initial.get(name) : authored;
|
|
135
|
+
if (value === parent.style.get(name))
|
|
136
|
+
continue;
|
|
137
|
+
if (!changed) {
|
|
138
|
+
style = new Map(parent.style);
|
|
139
|
+
changed = true;
|
|
140
|
+
}
|
|
141
|
+
if (value === undefined)
|
|
142
|
+
style.delete(name);
|
|
143
|
+
else
|
|
144
|
+
style.set(name, value);
|
|
145
|
+
}
|
|
146
|
+
for (const name of own) {
|
|
147
|
+
const value = read(name);
|
|
148
|
+
if (value !== undefined)
|
|
149
|
+
values.set(name, value);
|
|
150
|
+
}
|
|
151
|
+
if (values.get("display") === "none")
|
|
152
|
+
return undefined;
|
|
153
|
+
const size = read("font-size");
|
|
154
|
+
const fontSize = size === "initial" ? 16 : size !== undefined ? fontSizeOf(size, parent.fontSize) : parent.fontSize;
|
|
155
|
+
const transform = localTransform(node, declared, parent, fontSize);
|
|
156
|
+
const overflow = values.get("overflow")?.trim();
|
|
157
|
+
const clipOverflowByDefault = node.local === "image" || ((node.local === "svg" || node.local === "symbol") && node !== root);
|
|
158
|
+
const overflowVisible = overflow === "inherit" ? parent.overflowVisible :
|
|
159
|
+
overflow === undefined || overflow === "revert" ? !clipOverflowByDefault : /^(?:visible(?:\s+visible)?|initial|unset)$/.test(overflow);
|
|
160
|
+
const opacityValue = values.get("opacity");
|
|
161
|
+
const localOpacity = version === 2 && opacityValue?.trim() === "inherit" ? parent.localOpacity : alpha(opacityValue) ?? 1;
|
|
162
|
+
const groupOpacity = version === 2 && containers.has(node.local) && localOpacity > 0 && localOpacity < 1;
|
|
163
|
+
if (!groupOpacity && localOpacity < 1 && localOpacity > 0 && node.local !== "image")
|
|
164
|
+
diagnose("OPACITY_FLATTENED", node.local);
|
|
165
|
+
const xmlSpace = attribute(node, "xml:space");
|
|
166
|
+
const context = { ...parent, matrix: multiply(parent.matrix, transform ?? identity),
|
|
167
|
+
imageTransformValid: parent.imageTransformValid && transform !== undefined, style, fontSize, localOpacity, opacity: parent.opacity * localOpacity,
|
|
168
|
+
overflowVisible, ...(xmlSpace === undefined ? {} : { xmlSpace }) };
|
|
169
|
+
return { context, own: values, declared, groupOpacity };
|
|
170
|
+
}
|
|
171
|
+
function fontSizeOf(value, parent) {
|
|
172
|
+
const keywords = { "xx-small": 9, "x-small": 10, small: 13, medium: 16, large: 18, "x-large": 24, "xx-large": 32 };
|
|
173
|
+
const size = keywords[value.trim()] ?? length(value, parent, parent);
|
|
174
|
+
return size !== undefined && size > 0 ? size : parent;
|
|
175
|
+
}
|
|
176
|
+
function paint(value, context, opacityName, element) {
|
|
177
|
+
if (value === undefined || value === "none")
|
|
178
|
+
return undefined;
|
|
179
|
+
charge(Math.ceil(value.length / 16));
|
|
180
|
+
let color;
|
|
181
|
+
const reference = /^url\(\s*["']?#([^"')]+)["']?\s*\)\s*(.*)$/.exec(value.trim());
|
|
182
|
+
if (reference) {
|
|
183
|
+
const target = ids.get(reference[1]), fallback = reference[2].trim();
|
|
184
|
+
if (target && (target.local === "linearGradient" || target.local === "radialGradient")) {
|
|
185
|
+
color = gradientColor(target);
|
|
186
|
+
diagnose("GRADIENT_FLATTENED", element);
|
|
187
|
+
}
|
|
188
|
+
else if (fallback && fallback !== "none")
|
|
189
|
+
color = parseColor(fallback);
|
|
190
|
+
else {
|
|
191
|
+
diagnose("PAINT_UNSUPPORTED", element);
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
else if (value.trim().toLowerCase() === "currentcolor") {
|
|
196
|
+
const current = context.style.get("color") ?? "black";
|
|
197
|
+
chargeText(current);
|
|
198
|
+
color = parseColor(current);
|
|
199
|
+
}
|
|
200
|
+
else
|
|
201
|
+
color = parseColor(value);
|
|
202
|
+
if (!color) {
|
|
203
|
+
diagnose("COLOR_UNSUPPORTED", element);
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
const effective = (alpha(context.style.get(opacityName)) ?? 1) * context.opacity;
|
|
207
|
+
if (color.a * effective < 1 && color.a * effective >= .002)
|
|
208
|
+
diagnose("OPACITY_FLATTENED", element);
|
|
209
|
+
return flatten(color, effective);
|
|
210
|
+
}
|
|
211
|
+
function gradientColor(node) {
|
|
212
|
+
const seen = new Set();
|
|
213
|
+
let current = node;
|
|
214
|
+
while (current && !seen.has(current)) {
|
|
215
|
+
charge();
|
|
216
|
+
seen.add(current);
|
|
217
|
+
for (const child of current.children) {
|
|
218
|
+
charge();
|
|
219
|
+
if (typeof child === "string" || child.local !== "stop")
|
|
220
|
+
continue;
|
|
221
|
+
const declared = declarations(child);
|
|
222
|
+
const stop = declared.get("stop-color") ?? attribute(child, "stop-color") ?? "black";
|
|
223
|
+
charge(Math.ceil(stop.length / 16));
|
|
224
|
+
const color = parseColor(stop);
|
|
225
|
+
const opacity = alpha(declared.get("stop-opacity") ?? attribute(child, "stop-opacity")) ?? 1;
|
|
226
|
+
return color && { ...color, a: color.a * opacity };
|
|
227
|
+
}
|
|
228
|
+
const href = (attribute(current, "href") ?? attribute(current, "xlink:href"))?.replace(/^#/, "");
|
|
229
|
+
current = href === undefined ? undefined : ids.get(href);
|
|
230
|
+
}
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
// Consecutive paths with identical paint merge into one element, which keeps dense scatter and bar charts within drawing
|
|
234
|
+
// budgets. Only strokes and simple shapes of one winding merge, so a nonzero fill never gains a hole.
|
|
235
|
+
const mergeKeys = new WeakMap();
|
|
236
|
+
function pushPath(target, path, key) {
|
|
237
|
+
const last = target[target.length - 1];
|
|
238
|
+
if (key !== undefined && last && last.kind === "path" && mergeKeys.get(last) === key && last.commands.length + path.commands.length <= 4096) {
|
|
239
|
+
last.commands.push(...path.commands);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (key !== undefined)
|
|
243
|
+
mergeKeys.set(path, key);
|
|
244
|
+
addElement(target, path);
|
|
245
|
+
}
|
|
246
|
+
function shape(node, context, target, values) {
|
|
247
|
+
const { userWidth, userHeight } = context;
|
|
248
|
+
const diagonal = Math.hypot(userWidth, userHeight) / Math.SQRT2;
|
|
249
|
+
const a = (name, reference = 0) => length(attribute(node, name), context.fontSize, reference) ?? 0;
|
|
250
|
+
let commands;
|
|
251
|
+
switch (node.local) {
|
|
252
|
+
case "rect": {
|
|
253
|
+
const width = a("width", userWidth), height = a("height", userHeight);
|
|
254
|
+
if (!(width > 0 && height > 0))
|
|
255
|
+
return;
|
|
256
|
+
const rxValue = attribute(node, "rx"), ryValue = attribute(node, "ry");
|
|
257
|
+
const rx = rxValue === undefined || rxValue === "auto" ? a("ry", userHeight) : a("rx", userWidth);
|
|
258
|
+
const ry = ryValue === undefined || ryValue === "auto" ? rx : a("ry", userHeight);
|
|
259
|
+
commands = rectangle(a("x", userWidth), a("y", userHeight), width, height, rx, ry);
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
case "circle": {
|
|
263
|
+
const r = a("r", diagonal);
|
|
264
|
+
if (!(r > 0))
|
|
265
|
+
return;
|
|
266
|
+
commands = ellipse(a("cx", userWidth), a("cy", userHeight), r, r);
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
case "ellipse": {
|
|
270
|
+
const rx = a("rx", userWidth), ry = a("ry", userHeight);
|
|
271
|
+
if (!(rx > 0 && ry > 0))
|
|
272
|
+
return;
|
|
273
|
+
commands = ellipse(a("cx", userWidth), a("cy", userHeight), rx, ry);
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
case "line":
|
|
277
|
+
commands = [{ op: "move", x: a("x1", userWidth), y: a("y1", userHeight) }, { op: "line", x: a("x2", userWidth), y: a("y2", userHeight) }];
|
|
278
|
+
break;
|
|
279
|
+
case "polyline":
|
|
280
|
+
case "polygon": {
|
|
281
|
+
const points = attribute(node, "points") ?? "";
|
|
282
|
+
charge(Math.ceil(points.length / 4));
|
|
283
|
+
commands = polyline(numbers(points), node.local === "polygon");
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
default: {
|
|
287
|
+
const data = attribute(node, "d") ?? "";
|
|
288
|
+
charge(Math.ceil(data.length / 4));
|
|
289
|
+
const parsed = parsePathData(data);
|
|
290
|
+
if (parsed.malformed)
|
|
291
|
+
diagnose("PATH_DATA_TRUNCATED", "path");
|
|
292
|
+
commands = parsed.commands;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (!commands.length)
|
|
296
|
+
return;
|
|
297
|
+
for (const marker of ["marker-start", "marker-mid", "marker-end"])
|
|
298
|
+
if (values.get(marker) && values.get(marker) !== "none")
|
|
299
|
+
diagnose("MARKER_IGNORED", node.local);
|
|
300
|
+
const fill = node.local === "line" ? undefined : paint(context.style.get("fill"), context, "fill-opacity", node.local);
|
|
301
|
+
const stroke = paint(context.style.get("stroke"), context, "stroke-opacity", node.local);
|
|
302
|
+
if (fill === undefined && stroke === undefined)
|
|
303
|
+
return;
|
|
304
|
+
const fixedStroke = values.get("vector-effect") === "non-scaling-stroke";
|
|
305
|
+
const scale = fixedStroke ? unit : scaleOf(context.matrix);
|
|
306
|
+
const width = (length(context.style.get("stroke-width"), context.fontSize, diagonal) ?? 1) * scale;
|
|
307
|
+
if (stroke !== undefined && !(width > 0) && fill === undefined)
|
|
308
|
+
return;
|
|
309
|
+
commandCount += commands.length;
|
|
310
|
+
if (commandCount > maxCommands)
|
|
311
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxVectorCommands exceeded.");
|
|
312
|
+
const path = { kind: "path", commands: transformCommands(commands, context.matrix) };
|
|
313
|
+
if (fill !== undefined) {
|
|
314
|
+
path.fill = fill;
|
|
315
|
+
if (context.style.get("fill-rule") === "evenodd")
|
|
316
|
+
path.fillRule = "evenodd";
|
|
317
|
+
}
|
|
318
|
+
if (stroke !== undefined && width > 0) {
|
|
319
|
+
if (!fixedStroke && nonUniform(context.matrix))
|
|
320
|
+
diagnose("STROKE_DISTORTED", node.local);
|
|
321
|
+
path.stroke = stroke;
|
|
322
|
+
path.strokeWidth = width;
|
|
323
|
+
const cap = context.style.get("stroke-linecap"), join = context.style.get("stroke-linejoin");
|
|
324
|
+
if (cap === "round" || cap === "square")
|
|
325
|
+
path.lineCap = cap;
|
|
326
|
+
if (join === "round" || join === "bevel")
|
|
327
|
+
path.lineJoin = join;
|
|
328
|
+
else {
|
|
329
|
+
const limit = Number(context.style.get("stroke-miterlimit") ?? "4");
|
|
330
|
+
if (limit >= 0 && limit < 1)
|
|
331
|
+
path.lineJoin = "bevel";
|
|
332
|
+
else if (limit >= 1 && limit <= 1000000)
|
|
333
|
+
path.miterLimit = limit;
|
|
334
|
+
else {
|
|
335
|
+
diagnose("MITER_LIMIT_UNSUPPORTED", node.local);
|
|
336
|
+
path.miterLimit = 4;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
const paintOrder = context.style.get("paint-order");
|
|
340
|
+
if (fill !== undefined && paintOrder && paintOrder !== "normal") {
|
|
341
|
+
chargeText(paintOrder);
|
|
342
|
+
const order = paintOrder.split(/\s+/), fillIndex = order.indexOf("fill"), strokeIndex = order.indexOf("stroke");
|
|
343
|
+
if (strokeIndex >= 0 && (fillIndex < 0 || strokeIndex < fillIndex))
|
|
344
|
+
diagnose("PAINT_ORDER_IGNORED", node.local);
|
|
345
|
+
}
|
|
346
|
+
const dashValue = context.style.get("stroke-dasharray");
|
|
347
|
+
chargeText(dashValue);
|
|
348
|
+
const dash = dashValue?.trim();
|
|
349
|
+
if (dash !== "none" && dash) {
|
|
350
|
+
const parts = dash.split(/[\s,]+/, 17).map(part => length(part, context.fontSize, diagonal));
|
|
351
|
+
if (parts.every(part => part !== undefined && part >= 0) && parts.some(part => part > 0)) {
|
|
352
|
+
const list = parts.map(part => part * scale);
|
|
353
|
+
const offsetValue = context.style.get("stroke-dashoffset");
|
|
354
|
+
chargeText(offsetValue);
|
|
355
|
+
const offset = (length(offsetValue, context.fontSize, diagonal) ?? 0) * scale;
|
|
356
|
+
if (list.length <= 16 && list.every(part => part <= 14400) && list.some(part => pdfNumber(part) !== "0") && Math.abs(offset) <= 1000000) {
|
|
357
|
+
path.dash = list;
|
|
358
|
+
if (offset)
|
|
359
|
+
path.dashOffset = offset;
|
|
360
|
+
}
|
|
361
|
+
else
|
|
362
|
+
diagnose("DASH_UNSUPPORTED", node.local);
|
|
363
|
+
}
|
|
364
|
+
else if (parts.some(part => part === undefined || part < 0))
|
|
365
|
+
diagnose("DASH_UNSUPPORTED", node.local);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (path.fill === undefined && path.stroke === undefined)
|
|
369
|
+
return;
|
|
370
|
+
const mergeable = path.fill === undefined ||
|
|
371
|
+
(path.stroke === undefined || path.stroke === path.fill) && path.fillRule === undefined && ["rect", "circle", "ellipse"].includes(node.local);
|
|
372
|
+
const det = determinant(context.matrix);
|
|
373
|
+
pushPath(target, path, mergeable ? JSON.stringify([path.fill, det < 0, path.stroke, path.strokeWidth, path.lineCap, path.lineJoin, path.miterLimit, path.dash, path.dashOffset]) : undefined);
|
|
374
|
+
}
|
|
375
|
+
function textRuns(node, context, target) {
|
|
376
|
+
const runs = [];
|
|
377
|
+
let x = 0, y = 0, current;
|
|
378
|
+
let hiddenAdvance = false, hiddenSpace = false;
|
|
379
|
+
const visible = (at) => at.style.get("visibility") !== "hidden" && at.style.get("visibility") !== "collapse" && at.opacity > 0;
|
|
380
|
+
const finishRun = () => {
|
|
381
|
+
const anchor = current?.context.style.get("text-anchor");
|
|
382
|
+
if (hiddenAdvance && current?.text && (anchor === "middle" || anchor === "end"))
|
|
383
|
+
diagnose("TEXT_POSITIONS_SIMPLIFIED", "text");
|
|
384
|
+
};
|
|
385
|
+
const walk = (element, at, first) => {
|
|
386
|
+
charge();
|
|
387
|
+
const preserve = at.xmlSpace === "preserve" || /^pre/.test(at.style.get("white-space") ?? "");
|
|
388
|
+
const list = (name, reference) => (attribute(element, name) ?? "").trim().split(/[\s,]+/).filter(Boolean)
|
|
389
|
+
.map(item => length(item, at.fontSize, reference)).filter((item) => item !== undefined);
|
|
390
|
+
const xs = list("x", at.userWidth), ys = list("y", at.userHeight), dxs = list("dx", at.userWidth), dys = list("dy", at.userHeight);
|
|
391
|
+
if (xs.length > 1 || ys.length > 1 || dxs.length > 1 || dys.length > 1 || attribute(element, "rotate") !== undefined)
|
|
392
|
+
diagnose("TEXT_POSITIONS_SIMPLIFIED", element.local);
|
|
393
|
+
const positioned = first || xs.length > 0 || ys.length > 0 || dxs.length > 0 || dys.length > 0;
|
|
394
|
+
// Without x, a move is relative to where the unmeasured earlier text ends.
|
|
395
|
+
if (!first && positioned && !xs.length && current?.text.trim())
|
|
396
|
+
diagnose("TEXT_POSITIONS_SIMPLIFIED", element.local);
|
|
397
|
+
if (positioned) {
|
|
398
|
+
finishRun();
|
|
399
|
+
if (xs.length) {
|
|
400
|
+
x = xs[0];
|
|
401
|
+
hiddenAdvance = false;
|
|
402
|
+
hiddenSpace = false;
|
|
403
|
+
}
|
|
404
|
+
if (ys.length)
|
|
405
|
+
y = ys[0];
|
|
406
|
+
x += dxs[0] ?? 0;
|
|
407
|
+
y += dys[0] ?? 0;
|
|
408
|
+
current = { x, y, text: "", context: at, preserve };
|
|
409
|
+
runs.push(current);
|
|
410
|
+
}
|
|
411
|
+
for (const child of element.children) {
|
|
412
|
+
if (typeof child === "string") {
|
|
413
|
+
charge(1 + Math.ceil(child.length / 16));
|
|
414
|
+
if (!current)
|
|
415
|
+
continue;
|
|
416
|
+
const advances = preserve && child.length > 0 || /[^\t\r\n ]/.test(child);
|
|
417
|
+
if (!visible(at)) {
|
|
418
|
+
hiddenAdvance ||= advances;
|
|
419
|
+
hiddenSpace ||= /[ \t]/.test(child) && /[^\t\r\n ]$/.test(current.text);
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
if (!advances && !visible(current.context))
|
|
423
|
+
continue;
|
|
424
|
+
if (advances) {
|
|
425
|
+
const lostCollapsedSpace = hiddenSpace && /[^\t\r\n ]$/.test(current.text) && /^[^\t\r\n ]/.test(child);
|
|
426
|
+
if (hiddenAdvance || lostCollapsedSpace)
|
|
427
|
+
diagnose("TEXT_POSITIONS_SIMPLIFIED", element.local);
|
|
428
|
+
hiddenAdvance = hiddenSpace = false;
|
|
429
|
+
}
|
|
430
|
+
if (!visible(current.context)) {
|
|
431
|
+
current.context = at;
|
|
432
|
+
current.preserve = preserve;
|
|
433
|
+
}
|
|
434
|
+
const from = current.context;
|
|
435
|
+
if (current.preserve !== preserve && /[\t\r\n ]/.test(child))
|
|
436
|
+
diagnose("TEXT_WHITESPACE_SIMPLIFIED", element.local);
|
|
437
|
+
if (advances && from.matrix.some((component, i) => component !== at.matrix[i]))
|
|
438
|
+
diagnose("TEXT_TRANSFORM_IGNORED", element.local);
|
|
439
|
+
if ((from.style !== at.style || from.fontSize !== at.fontSize || from.opacity !== at.opacity) && /\S/.test(child))
|
|
440
|
+
diagnose("TEXT_STYLE_MERGED", element.local);
|
|
441
|
+
current.text += child;
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
if (child.local !== "tspan" && child.local !== "a") {
|
|
445
|
+
if (child.local === "textPath")
|
|
446
|
+
diagnose("TEXT_PATH_IGNORED", "textPath");
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
const nested = cascade(child, at);
|
|
450
|
+
if (nested)
|
|
451
|
+
walk(child, nested.context, false);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
walk(node, context, true);
|
|
455
|
+
finishRun();
|
|
456
|
+
for (const run of runs) {
|
|
457
|
+
const style = run.context.style, { preserve } = run;
|
|
458
|
+
let text = run.text.replace(/\t/g, " ").replace(/[\r\n]/g, preserve ? " " : "");
|
|
459
|
+
if (!preserve)
|
|
460
|
+
text = text.replace(/ {2,}/g, " ");
|
|
461
|
+
if (run === runs[0] && !preserve)
|
|
462
|
+
text = text.replace(/^ +/, "");
|
|
463
|
+
if (run === runs[runs.length - 1] && !preserve)
|
|
464
|
+
text = text.replace(/ +$/, "");
|
|
465
|
+
if (!text)
|
|
466
|
+
continue;
|
|
467
|
+
if (style.get("visibility") === "hidden" || style.get("visibility") === "collapse")
|
|
468
|
+
continue;
|
|
469
|
+
const color = paint(style.get("fill"), run.context, "fill-opacity", "text");
|
|
470
|
+
if (color === undefined)
|
|
471
|
+
continue;
|
|
472
|
+
if ((style.get("stroke") ?? "none") !== "none")
|
|
473
|
+
diagnose("TEXT_STROKE_IGNORED", "text");
|
|
474
|
+
const m = run.context.matrix, scale = scaleOf(m);
|
|
475
|
+
if (nonUniform(m) || determinant(m) < 0)
|
|
476
|
+
diagnose("TEXT_DISTORTED", "text");
|
|
477
|
+
const size = run.context.fontSize * scale;
|
|
478
|
+
if (!(size > 0))
|
|
479
|
+
continue;
|
|
480
|
+
const baseline = style.get("dominant-baseline") ?? style.get("alignment-baseline") ?? "auto";
|
|
481
|
+
const shift = baseline === "middle" || baseline === "central" ? .35 : baseline === "hanging" || baseline === "text-before-edge" || baseline === "text-top" ? .8 :
|
|
482
|
+
baseline === "text-after-edge" || baseline === "ideographic" || baseline === "text-bottom" ? -.2 : 0;
|
|
483
|
+
const [px, py] = apply(m, run.x, run.y + shift * run.context.fontSize), rotation = Math.atan2(m[1], m[0]) * 180 / Math.PI;
|
|
484
|
+
const weight = style.get("font-weight") ?? "normal", italic = /italic|oblique/.test(style.get("font-style") ?? "");
|
|
485
|
+
const bold = weight === "bold" || weight === "bolder" || Number(weight) >= 600;
|
|
486
|
+
let slot = bold && italic ? "boldItalic" : bold ? "bold" : italic ? "italic" : "regular";
|
|
487
|
+
if (!fonts.has(slot))
|
|
488
|
+
slot = "regular";
|
|
489
|
+
const anchor = style.get("text-anchor");
|
|
490
|
+
const painted = winAnsi ? winAnsiText(text, () => diagnose("TEXT_SUBSTITUTED", "text")) : text.normalize("NFC");
|
|
491
|
+
const element = {
|
|
492
|
+
kind: "text", text: painted, x: px, y: py, font: slot, fontSize: size, color
|
|
493
|
+
};
|
|
494
|
+
if (!element.text)
|
|
495
|
+
continue;
|
|
496
|
+
if (anchor === "middle" || anchor === "end")
|
|
497
|
+
element.anchor = anchor;
|
|
498
|
+
if (Math.abs(rotation) > 1e-6)
|
|
499
|
+
element.rotation = Math.round(rotation * 1e6) / 1e6;
|
|
500
|
+
addElement(target, element);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
function hasText(node) {
|
|
504
|
+
return node.children.some(child => {
|
|
505
|
+
charge(typeof child === "string" ? 1 + Math.ceil(child.length / 16) : 1);
|
|
506
|
+
return typeof child === "string" ? /\S/.test(child) : hasText(child);
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
function rectangularClip(matrix, x, y, width, height) {
|
|
510
|
+
if (width <= 0 || height <= 0)
|
|
511
|
+
return null;
|
|
512
|
+
const axisAligned = Math.abs(matrix[1]) <= 1e-9 && Math.abs(matrix[2]) <= 1e-9;
|
|
513
|
+
const axesSwapped = Math.abs(matrix[0]) <= 1e-9 && Math.abs(matrix[3]) <= 1e-9;
|
|
514
|
+
if (!axisAligned && !axesSwapped)
|
|
515
|
+
return undefined;
|
|
516
|
+
const [x1, y1] = apply(matrix, x, y), [x2, y2] = apply(matrix, x + width, y + height);
|
|
517
|
+
const left = Math.max(0, Math.min(x1, x2)), top = Math.max(0, Math.min(y1, y2));
|
|
518
|
+
const right = Math.min(drawingWidth, Math.max(x1, x2)), bottom = Math.min(drawingHeight, Math.max(y1, y2));
|
|
519
|
+
if (![left, top, right, bottom].every(Number.isFinite))
|
|
520
|
+
return undefined;
|
|
521
|
+
if (right <= left || bottom <= top)
|
|
522
|
+
return null;
|
|
523
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
524
|
+
}
|
|
525
|
+
function clipFor(value, context, element) {
|
|
526
|
+
if (value === undefined || value === "none")
|
|
527
|
+
return undefined;
|
|
528
|
+
chargeText(value);
|
|
529
|
+
const id = /^url\(\s*["']?#([^"')]+)["']?\s*\)$/.exec(value.trim())?.[1], clip = id === undefined ? undefined : ids.get(id);
|
|
530
|
+
if (clip?.local !== "clipPath") {
|
|
531
|
+
diagnose("CLIP_IGNORED", element);
|
|
532
|
+
return undefined;
|
|
533
|
+
}
|
|
534
|
+
const clipStyle = declarations(clip);
|
|
535
|
+
const clipVisibility = clipStyle.get("visibility") ?? attribute(clip, "visibility");
|
|
536
|
+
let rect;
|
|
537
|
+
let rectStyle = initial;
|
|
538
|
+
for (const child of clip.children) {
|
|
539
|
+
charge();
|
|
540
|
+
if (typeof child === "string")
|
|
541
|
+
continue;
|
|
542
|
+
const style = declarations(child);
|
|
543
|
+
if ((style.get("display") ?? attribute(child, "display")) === "none")
|
|
544
|
+
continue;
|
|
545
|
+
const localVisibility = style.get("visibility") ?? attribute(child, "visibility");
|
|
546
|
+
const visibility = localVisibility === undefined || localVisibility === "inherit" || localVisibility === "unset" ? clipVisibility : localVisibility;
|
|
547
|
+
if (visibility === "hidden" || visibility === "collapse")
|
|
548
|
+
continue;
|
|
549
|
+
if (rect || child.local !== "rect") {
|
|
550
|
+
diagnose("CLIP_IGNORED", element);
|
|
551
|
+
return undefined;
|
|
552
|
+
}
|
|
553
|
+
rect = child;
|
|
554
|
+
rectStyle = style;
|
|
555
|
+
}
|
|
556
|
+
if (!rect)
|
|
557
|
+
return null;
|
|
558
|
+
if (clip.attributes.get("clipPathUnits") === "objectBoundingBox") {
|
|
559
|
+
diagnose("CLIP_IGNORED", element);
|
|
560
|
+
return undefined;
|
|
561
|
+
}
|
|
562
|
+
const a = (name, reference) => length(rectStyle.get(name) ?? attribute(rect, name), context.fontSize, reference);
|
|
563
|
+
const width = a("width", context.userWidth) ?? 0, height = a("height", context.userHeight) ?? 0;
|
|
564
|
+
if (width <= 0 || height <= 0)
|
|
565
|
+
return null;
|
|
566
|
+
const rx = a("rx", context.userWidth), ry = a("ry", context.userHeight);
|
|
567
|
+
if ((rx ?? ry ?? 0) > 0 && (ry ?? rx ?? 0) > 0) {
|
|
568
|
+
diagnose("CLIP_IGNORED", element);
|
|
569
|
+
return undefined;
|
|
570
|
+
}
|
|
571
|
+
const clipTransform = localTransform(clip, clipStyle, context, context.fontSize);
|
|
572
|
+
const rectTransform = localTransform(rect, rectStyle, context, context.fontSize);
|
|
573
|
+
if (!clipTransform || !rectTransform)
|
|
574
|
+
return undefined;
|
|
575
|
+
const r = multiply(context.matrix, multiply(clipTransform, rectTransform));
|
|
576
|
+
const x = a("x", context.userWidth) ?? 0, y = a("y", context.userHeight) ?? 0;
|
|
577
|
+
const shape = rectangularClip(r, x, y, width, height);
|
|
578
|
+
if (shape === undefined)
|
|
579
|
+
diagnose("CLIP_IGNORED", element);
|
|
580
|
+
return shape;
|
|
581
|
+
}
|
|
582
|
+
// One decoded source per distinct data URL, so repeated artwork shares one PDF image.
|
|
583
|
+
const decoded = new Map();
|
|
584
|
+
function placeImage(node, context, declared, target) {
|
|
585
|
+
const m = context.matrix;
|
|
586
|
+
// Embedded images are placed upright and require a fully resolved transform.
|
|
587
|
+
if (!context.imageTransformValid || Math.abs(m[1]) > 1e-9 || Math.abs(m[2]) > 1e-9 || !(m[0] > 0) || !(m[3] > 0)) {
|
|
588
|
+
diagnose("IMAGE_SKIPPED", "image");
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
const href = (attribute(node, "href") ?? attribute(node, "xlink:href"))?.trim();
|
|
592
|
+
let source = href === undefined ? null : decoded.get(href);
|
|
593
|
+
if (href !== undefined && source === undefined) {
|
|
594
|
+
charge(Math.ceil(href.length / 16));
|
|
595
|
+
source = svgDataImage(href) ?? null;
|
|
596
|
+
decoded.set(href, source);
|
|
597
|
+
}
|
|
598
|
+
if (!source) {
|
|
599
|
+
diagnose("IMAGE_SKIPPED", "image");
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
const a = (name, reference) => length(declared.get(name) ?? attribute(node, name), context.fontSize, reference);
|
|
603
|
+
const x = a("x", context.userWidth) ?? 0, y = a("y", context.userHeight) ?? 0;
|
|
604
|
+
const declaredWidth = a("width", context.userWidth), declaredHeight = a("height", context.userHeight);
|
|
605
|
+
const width = declaredWidth ?? (declaredHeight === undefined ? source.width : declaredHeight * source.width / source.height);
|
|
606
|
+
const height = declaredHeight ?? (declaredWidth === undefined ? source.height : declaredWidth * source.height / source.width);
|
|
607
|
+
if (!(width > 0 && height > 0))
|
|
608
|
+
return;
|
|
609
|
+
let placed = { x, y, width, height }, crop;
|
|
610
|
+
const aspect = attribute(node, "preserveAspectRatio")?.trim().replace(/^defer(?:\s+|$)/, "");
|
|
611
|
+
const [align = "xMidYMid", fit = "meet"] = (aspect || "xMidYMid meet").split(/\s+/);
|
|
612
|
+
if (align !== "none") {
|
|
613
|
+
const sx = width / source.width, sy = height / source.height, scale = fit === "slice" ? Math.max(sx, sy) : Math.min(sx, sy);
|
|
614
|
+
const fittedWidth = source.width * scale, fittedHeight = source.height * scale;
|
|
615
|
+
placed = { x: x + (width - fittedWidth) * (align.includes("xMid") ? .5 : align.includes("xMax") ? 1 : 0),
|
|
616
|
+
y: y + (height - fittedHeight) * (align.includes("YMid") ? .5 : align.includes("YMax") ? 1 : 0), width: fittedWidth, height: fittedHeight };
|
|
617
|
+
if (fit === "slice" && !context.overflowVisible) {
|
|
618
|
+
crop = rectangularClip(m, x, y, width, height);
|
|
619
|
+
if (crop === null)
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
const [left, top] = apply(m, placed.x, placed.y), [right, bottom] = apply(m, placed.x + placed.width, placed.y + placed.height);
|
|
624
|
+
const element = { kind: "image", image: source.image, x: left, y: top, width: right - left, height: bottom - top,
|
|
625
|
+
...(context.opacity < 1 ? { opacity: context.opacity } : {}) };
|
|
626
|
+
if (![left, top, element.width, element.height].every(value => Number.isFinite(value) && Math.abs(value) <= 1000000) || !(element.width > 0 && element.height > 0)) {
|
|
627
|
+
diagnose("IMAGE_SKIPPED", "image");
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
if (crop && context.clipDepth + 1 < pdfDrawingMaxDepth) {
|
|
631
|
+
const group = [];
|
|
632
|
+
addElement(group, element);
|
|
633
|
+
addElement(target, { kind: "group", clip: crop, elements: group });
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
if (crop)
|
|
637
|
+
diagnose("CLIP_DEPTH_IGNORED", "image");
|
|
638
|
+
addElement(target, element);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
// A group with one image can use that image's opacity, even through nested rectangular clips.
|
|
642
|
+
function singleImage(elements, start) {
|
|
643
|
+
if (elements.length !== start + 1)
|
|
644
|
+
return false;
|
|
645
|
+
const element = elements[start];
|
|
646
|
+
return element.kind === "image" || (element.kind === "group" && singleImage(element.elements, 0));
|
|
647
|
+
}
|
|
648
|
+
const active = new Set();
|
|
649
|
+
function render(node, parent, target, depth, useSize) {
|
|
650
|
+
charge();
|
|
651
|
+
if (skipped.has(node.local) && !(node.local === "symbol" && useSize))
|
|
652
|
+
return;
|
|
653
|
+
if (depth > 512)
|
|
654
|
+
throw new NotSupportedError("Chart SVG nesting is too deep.");
|
|
655
|
+
if (node.local === "image" && version === 1) {
|
|
656
|
+
diagnose("IMAGE_SKIPPED", "image");
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
if (node.local === "foreignObject") {
|
|
660
|
+
diagnose("FOREIGN_OBJECT_SKIPPED", "foreignObject");
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
if (node.local === "style") {
|
|
664
|
+
if (hasText(node))
|
|
665
|
+
diagnose("STYLESHEET_IGNORED", "style");
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
const cascaded = cascade(node, parent);
|
|
669
|
+
if (!cascaded || cascaded.context.opacity <= 0)
|
|
670
|
+
return;
|
|
671
|
+
let { context } = cascaded;
|
|
672
|
+
const values = cascaded.own;
|
|
673
|
+
if (values.get("mask") && values.get("mask") !== "none")
|
|
674
|
+
diagnose("MASK_IGNORED", node.local);
|
|
675
|
+
if (values.get("filter") && values.get("filter") !== "none")
|
|
676
|
+
diagnose("FILTER_IGNORED", node.local);
|
|
677
|
+
let viewportClip;
|
|
678
|
+
if ((node.local === "svg" || node.local === "symbol") && node !== root) {
|
|
679
|
+
const nested = viewport(node, context.fontSize, context.userWidth, context.userHeight, true, useSize);
|
|
680
|
+
if (!(nested.width > 0 && nested.height > 0))
|
|
681
|
+
return;
|
|
682
|
+
const x = length(attribute(node, "x"), context.fontSize, context.userWidth) ?? 0, y = length(attribute(node, "y"), context.fontSize, context.userHeight) ?? 0;
|
|
683
|
+
const placed = multiply(context.matrix, [1, 0, 0, 1, x, y]);
|
|
684
|
+
if (!context.overflowVisible) {
|
|
685
|
+
const clip = rectangularClip(placed, 0, 0, nested.width, nested.height);
|
|
686
|
+
if (clip === null)
|
|
687
|
+
return;
|
|
688
|
+
viewportClip = clip;
|
|
689
|
+
if (!viewportClip)
|
|
690
|
+
diagnose("CLIP_IGNORED", node.local);
|
|
691
|
+
}
|
|
692
|
+
context = { ...context, userWidth: nested.userWidth, userHeight: nested.userHeight, viewportOriginZero: nested.originZero,
|
|
693
|
+
matrix: multiply(placed, nested.matrix) };
|
|
694
|
+
}
|
|
695
|
+
let clip = clipFor(values.get("clip-path"), context, node.local);
|
|
696
|
+
if (clip === null)
|
|
697
|
+
return;
|
|
698
|
+
if (clip && viewportClip) {
|
|
699
|
+
const x = Math.max(clip.x, viewportClip.x), y = Math.max(clip.y, viewportClip.y);
|
|
700
|
+
const width = Math.min(clip.x + clip.width, viewportClip.x + viewportClip.width) - x;
|
|
701
|
+
const height = Math.min(clip.y + clip.height, viewportClip.y + viewportClip.height) - y;
|
|
702
|
+
if (width <= 0 || height <= 0)
|
|
703
|
+
return;
|
|
704
|
+
clip = { x, y, width, height };
|
|
705
|
+
}
|
|
706
|
+
else
|
|
707
|
+
clip ??= viewportClip;
|
|
708
|
+
if (clip) {
|
|
709
|
+
if (context.clipDepth + 1 >= pdfDrawingMaxDepth) {
|
|
710
|
+
diagnose("CLIP_DEPTH_IGNORED", node.local);
|
|
711
|
+
clip = undefined;
|
|
712
|
+
}
|
|
713
|
+
else
|
|
714
|
+
context = { ...context, clipDepth: context.clipDepth + 1 };
|
|
715
|
+
}
|
|
716
|
+
const output = clip ? [] : target, start = output.length;
|
|
717
|
+
const visible = context.style.get("visibility") !== "hidden" && context.style.get("visibility") !== "collapse";
|
|
718
|
+
if (shapes.has(node.local)) {
|
|
719
|
+
if (visible)
|
|
720
|
+
shape(node, context, output, values);
|
|
721
|
+
}
|
|
722
|
+
else if (node.local === "text")
|
|
723
|
+
textRuns(node, context, output);
|
|
724
|
+
else if (node.local === "image") {
|
|
725
|
+
if (visible)
|
|
726
|
+
placeImage(node, context, cascaded.declared, output);
|
|
727
|
+
}
|
|
728
|
+
else if (node.local === "use") {
|
|
729
|
+
const href = (attribute(node, "href") ?? attribute(node, "xlink:href"))?.replace(/^#/, ""), referenced = href === undefined ? undefined : ids.get(href);
|
|
730
|
+
if (!referenced || active.has(referenced))
|
|
731
|
+
diagnose("USE_UNRESOLVED", "use");
|
|
732
|
+
else {
|
|
733
|
+
const x = length(attribute(node, "x"), context.fontSize, context.userWidth) ?? 0, y = length(attribute(node, "y"), context.fontSize, context.userHeight) ?? 0;
|
|
734
|
+
const placed = { ...context, matrix: multiply(context.matrix, [1, 0, 0, 1, x, y]) };
|
|
735
|
+
active.add(referenced);
|
|
736
|
+
const useSize = { width: length(attribute(node, "width"), context.fontSize, context.userWidth),
|
|
737
|
+
height: length(attribute(node, "height"), context.fontSize, context.userHeight) };
|
|
738
|
+
render(referenced, placed, output, depth + 1, useSize);
|
|
739
|
+
active.delete(referenced);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
else if (node.local === "switch") {
|
|
743
|
+
for (const child of node.children) {
|
|
744
|
+
charge();
|
|
745
|
+
if (typeof child === "string")
|
|
746
|
+
continue;
|
|
747
|
+
if (["systemLanguage", "requiredExtensions", "requiredFeatures"].some(name => child.attributes.has(name))) {
|
|
748
|
+
diagnose("SWITCH_CONDITION_IGNORED", child.local);
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
render(child, context, output, depth + 1);
|
|
752
|
+
break;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
else if (containers.has(node.local)) {
|
|
756
|
+
for (const child of node.children) {
|
|
757
|
+
if (typeof child === "string")
|
|
758
|
+
charge();
|
|
759
|
+
else
|
|
760
|
+
render(child, context, output, depth + 1);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
else
|
|
764
|
+
diagnose("ELEMENT_SKIPPED", node.local);
|
|
765
|
+
if (cascaded.groupOpacity && output.length > start && !singleImage(output, start))
|
|
766
|
+
diagnose("OPACITY_FLATTENED", node.local);
|
|
767
|
+
if (clip && output.length)
|
|
768
|
+
addElement(target, { kind: "group", clip, elements: output });
|
|
769
|
+
}
|
|
770
|
+
render(root, { matrix: [unit * outer.matrix[0], 0, 0, unit * outer.matrix[3], unit * outer.matrix[4], unit * outer.matrix[5]],
|
|
771
|
+
userWidth: outer.userWidth, userHeight: outer.userHeight, viewportOriginZero: outer.originZero, clipDepth: 0,
|
|
772
|
+
overflowVisible: true, imageTransformValid: true, style: initial, fontSize: 16, opacity: 1, localOpacity: 1 }, elements, 0);
|
|
773
|
+
// Image elements can only be added when rendering version 2.
|
|
774
|
+
return { version, width: drawingWidth, height: drawingHeight, elements, standardFonts: true };
|
|
775
|
+
}
|