@evotecit/officeimo 0.1.21 → 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 +39 -8
- package/bundles/officeimo-canopyx-adapter.js +56 -36
- package/bundles/officeimo-canopyx-adapter.min.js +1 -1
- package/bundles/officeimo-canopyx-adapter.min.mjs +1 -1
- package/bundles/officeimo-canopyx-adapter.mjs +42 -22
- package/bundles/officeimo-canopyx.js +420 -177
- package/bundles/officeimo-canopyx.min.js +1 -1
- package/bundles/officeimo-canopyx.min.mjs +1 -1
- package/bundles/officeimo-canopyx.mjs +346 -103
- 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 +375 -152
- package/bundles/officeimo-datatables.min.js +1 -1
- package/bundles/officeimo-datatables.min.mjs +1 -1
- package/bundles/officeimo-datatables.mjs +311 -88
- 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 +93 -31
- package/bundles/officeimo-xlsx.min.js +1 -1
- package/bundles/officeimo-xlsx.min.mjs +1 -1
- package/bundles/officeimo-xlsx.mjs +75 -13
- package/bundles/officeimo.js +373 -150
- package/bundles/officeimo.min.js +1 -1
- package/bundles/officeimo.min.mjs +1 -1
- package/bundles/officeimo.mjs +312 -89
- package/dist/core/progress.js +7 -2
- package/dist/index.d.ts +1 -1
- package/dist/integrations/canopyx/index.js +9 -2
- package/dist/integrations/canopyx/source.js +26 -13
- 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 +1 -1
- package/dist/xlsx/types.d.ts +8 -0
- package/dist/xlsx/workbook.js +6 -0
- package/dist/xlsx/worksheet.d.ts +13 -1
- package/dist/xlsx/worksheet.js +60 -9
- package/package.json +9 -2
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
const named = {
|
|
2
|
+
black: "000000", white: "ffffff", red: "ff0000", green: "008000", blue: "0000ff", yellow: "ffff00", orange: "ffa500", purple: "800080",
|
|
3
|
+
gray: "808080", grey: "808080", silver: "c0c0c0", maroon: "800000", navy: "000080", teal: "008080", olive: "808000", lime: "00ff00",
|
|
4
|
+
aqua: "00ffff", cyan: "00ffff", fuchsia: "ff00ff", magenta: "ff00ff", darkgray: "a9a9a9", darkgrey: "a9a9a9", lightgray: "d3d3d3",
|
|
5
|
+
lightgrey: "d3d3d3", dimgray: "696969", dimgrey: "696969", gainsboro: "dcdcdc", whitesmoke: "f5f5f5", steelblue: "4682b4",
|
|
6
|
+
slategray: "708090", slategrey: "708090", crimson: "dc143c", tomato: "ff6347", gold: "ffd700", indigo: "4b0082", brown: "a52a2a",
|
|
7
|
+
pink: "ffc0cb", coral: "ff7f50", salmon: "fa8072", seagreen: "2e8b57", forestgreen: "228b22", royalblue: "4169e1", darkblue: "00008b",
|
|
8
|
+
darkred: "8b0000", darkgreen: "006400", darkorange: "ff8c00", lightblue: "add8e6", skyblue: "87ceeb", tan: "d2b48c", violet: "ee82ee"
|
|
9
|
+
};
|
|
10
|
+
const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
|
|
11
|
+
function channel(text, scale) {
|
|
12
|
+
const value = parseFloat(text);
|
|
13
|
+
return text.trim().endsWith("%") ? value / 100 * scale : value;
|
|
14
|
+
}
|
|
15
|
+
export function alpha(text) {
|
|
16
|
+
if (text === undefined)
|
|
17
|
+
return 1;
|
|
18
|
+
if (text.trim() === "none")
|
|
19
|
+
return 0;
|
|
20
|
+
if (!/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?%?$/i.test(text.trim()))
|
|
21
|
+
return undefined;
|
|
22
|
+
return clamp(channel(text, 1), 0, 1);
|
|
23
|
+
}
|
|
24
|
+
function functionArguments(body, legacyAlpha) {
|
|
25
|
+
const [main, slash] = body.split("/");
|
|
26
|
+
const values = main.includes(",") ? main.split(",").map(s => s.trim()).filter(Boolean) : main.trim().split(/\s+/);
|
|
27
|
+
// Legacy rgba(r, g, b, a) and hsla carry alpha as a fourth argument; color(space c1 c2 c3) does not.
|
|
28
|
+
if (legacyAlpha && slash === undefined && values.length === 4)
|
|
29
|
+
return { values: values.slice(0, 3), alpha: values[3] };
|
|
30
|
+
return { values, alpha: slash?.trim() };
|
|
31
|
+
}
|
|
32
|
+
function linearToSrgb(value) {
|
|
33
|
+
const v = clamp(value, 0, 1);
|
|
34
|
+
return 255 * (v <= 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055);
|
|
35
|
+
}
|
|
36
|
+
function fromOklab(l, a, b, opacity) {
|
|
37
|
+
const l1 = Math.pow(l + 0.3963377774 * a + 0.2158037573 * b, 3), m1 = Math.pow(l - 0.1055613458 * a - 0.0638541728 * b, 3);
|
|
38
|
+
const s1 = Math.pow(l - 0.0894841775 * a - 1.2914855480 * b, 3);
|
|
39
|
+
const r = 4.0767416621 * l1 - 3.3077115913 * m1 + 0.2309699292 * s1;
|
|
40
|
+
const g = -1.2684380046 * l1 + 2.6097574011 * m1 - 0.3413193965 * s1;
|
|
41
|
+
const blue = -0.0041960863 * l1 - 0.7034186147 * m1 + 1.7076147010 * s1;
|
|
42
|
+
if (![r, g, blue].every(Number.isFinite))
|
|
43
|
+
return undefined;
|
|
44
|
+
return { r: linearToSrgb(r), g: linearToSrgb(g), b: linearToSrgb(blue), a: opacity };
|
|
45
|
+
}
|
|
46
|
+
function hue(text) {
|
|
47
|
+
const value = parseFloat(text);
|
|
48
|
+
return text.endsWith("turn") ? value * 360 : text.endsWith("rad") ? value * 180 / Math.PI : value;
|
|
49
|
+
}
|
|
50
|
+
/** Parse the CSS color forms a browser's computed style and SVG presentation attributes produce. */
|
|
51
|
+
export function parseColor(input) {
|
|
52
|
+
const text = input.trim().toLowerCase();
|
|
53
|
+
if (!text)
|
|
54
|
+
return undefined;
|
|
55
|
+
if (text === "transparent")
|
|
56
|
+
return { r: 0, g: 0, b: 0, a: 0 };
|
|
57
|
+
const hex = /^#([0-9a-f]{3,8})$/.exec(text)?.[1];
|
|
58
|
+
if (hex) {
|
|
59
|
+
if (hex.length === 3 || hex.length === 4) {
|
|
60
|
+
const [r, g, b, a] = [...hex].map(c => parseInt(c + c, 16));
|
|
61
|
+
return { r: r, g: g, b: b, a: a === undefined ? 1 : a / 255 };
|
|
62
|
+
}
|
|
63
|
+
if (hex.length === 6 || hex.length === 8)
|
|
64
|
+
return { r: parseInt(hex.slice(0, 2), 16), g: parseInt(hex.slice(2, 4), 16), b: parseInt(hex.slice(4, 6), 16), a: hex.length === 8 ? parseInt(hex.slice(6), 16) / 255 : 1 };
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const known = named[text];
|
|
68
|
+
if (known)
|
|
69
|
+
return parseColor("#" + known);
|
|
70
|
+
const call = /^([a-z]+)\((.*)\)$/.exec(text);
|
|
71
|
+
if (!call)
|
|
72
|
+
return undefined;
|
|
73
|
+
const name = call[1], { values, alpha: opacity } = functionArguments(call[2], name !== "color");
|
|
74
|
+
if (values.some(value => !/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?(%|deg|rad|turn)?$/.test(value) && value !== "none")) {
|
|
75
|
+
if (name !== "color")
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const a = alpha(opacity);
|
|
79
|
+
if (a === undefined)
|
|
80
|
+
return undefined;
|
|
81
|
+
const v = values.map(value => value === "none" ? "0" : value);
|
|
82
|
+
if ((name === "rgb" || name === "rgba") && v.length === 3)
|
|
83
|
+
return { r: clamp(channel(v[0], 255), 0, 255), g: clamp(channel(v[1], 255), 0, 255), b: clamp(channel(v[2], 255), 0, 255), a };
|
|
84
|
+
if ((name === "hsl" || name === "hsla") && v.length === 3) {
|
|
85
|
+
const h = ((hue(v[0]) % 360) + 360) % 360 / 360, s = clamp(parseFloat(v[1]) / 100, 0, 1), l = clamp(parseFloat(v[2]) / 100, 0, 1);
|
|
86
|
+
const q = l < .5 ? l * (1 + s) : l + s - l * s, p = 2 * l - q;
|
|
87
|
+
const tone = (t) => { t = (t + 1) % 1; return 255 * (t < 1 / 6 ? p + (q - p) * 6 * t : t < .5 ? q : t < 2 / 3 ? p + (q - p) * (2 / 3 - t) * 6 : p); };
|
|
88
|
+
return { r: tone(h + 1 / 3), g: tone(h), b: tone(h - 1 / 3), a };
|
|
89
|
+
}
|
|
90
|
+
if (name === "oklab" && v.length === 3)
|
|
91
|
+
return fromOklab(channel(v[0], 1), channel(v[1], .4), channel(v[2], .4), a);
|
|
92
|
+
if (name === "oklch" && v.length === 3) {
|
|
93
|
+
const l = channel(v[0], 1), c = channel(v[1], .4), h = hue(v[2]) * Math.PI / 180;
|
|
94
|
+
return fromOklab(l, c * Math.cos(h), c * Math.sin(h), a);
|
|
95
|
+
}
|
|
96
|
+
if (name === "color" && v.length === 4 && (v[0] === "srgb" || v[0] === "srgb-linear")) {
|
|
97
|
+
const parts = v.slice(1).map(value => channel(value, 1));
|
|
98
|
+
const convert = v[0] === "srgb" ? (x) => clamp(x, 0, 1) * 255 : linearToSrgb;
|
|
99
|
+
if (!parts.every(Number.isFinite))
|
|
100
|
+
return undefined;
|
|
101
|
+
return { r: convert(parts[0]), g: convert(parts[1]), b: convert(parts[2]), a };
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
/** Composite over white, the page colour, and return six-digit hex for the PDF drawing contract. */
|
|
106
|
+
export function flatten(color, opacity) {
|
|
107
|
+
const a = clamp(color.a * opacity, 0, 1);
|
|
108
|
+
if (a < 0.002)
|
|
109
|
+
return undefined;
|
|
110
|
+
const mix = (value) => Math.round(clamp(value, 0, 255) * a + 255 * (1 - a)).toString(16).padStart(2, "0").toUpperCase();
|
|
111
|
+
return mix(color.r) + mix(color.g) + mix(color.b);
|
|
112
|
+
}
|
|
113
|
+
/** Inline `style` declarations as a map; later declarations win, `!important` is accepted and dropped. */
|
|
114
|
+
export function parseStyle(text) {
|
|
115
|
+
const result = new Map();
|
|
116
|
+
if (!text)
|
|
117
|
+
return result;
|
|
118
|
+
const important = new Set();
|
|
119
|
+
for (const declaration of text.split(";")) {
|
|
120
|
+
const colon = declaration.indexOf(":");
|
|
121
|
+
if (colon < 0)
|
|
122
|
+
continue;
|
|
123
|
+
const name = declaration.slice(0, colon).trim().toLowerCase(), raw = declaration.slice(colon + 1);
|
|
124
|
+
const priority = /!\s*important\s*$/i;
|
|
125
|
+
const value = raw.replace(priority, "").trim(), takesPriority = priority.test(raw);
|
|
126
|
+
if (!name || !value || important.has(name) && !takesPriority)
|
|
127
|
+
continue;
|
|
128
|
+
if (takesPriority)
|
|
129
|
+
important.add(name);
|
|
130
|
+
result.set(name, value);
|
|
131
|
+
}
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
/** A CSS/SVG length in user units. Percentages resolve against `reference`; `em` against the font size. */
|
|
135
|
+
export function length(text, fontSize, reference = 0) {
|
|
136
|
+
if (text === undefined)
|
|
137
|
+
return undefined;
|
|
138
|
+
const match = /^\s*([+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)\s*(px|pt|pc|mm|cm|in|em|rem|ex|%)?\s*$/.exec(text);
|
|
139
|
+
if (!match)
|
|
140
|
+
return undefined;
|
|
141
|
+
const value = Number(match[1]), unit = match[2];
|
|
142
|
+
const factor = unit === undefined || unit === "px" ? 1 : unit === "pt" ? 4 / 3 : unit === "pc" ? 16 : unit === "mm" ? 96 / 25.4 : unit === "cm" ? 96 / 2.54 :
|
|
143
|
+
unit === "in" ? 96 : unit === "em" ? fontSize : unit === "rem" ? 16 : unit === "ex" ? fontSize / 2 : reference / 100;
|
|
144
|
+
const result = value * factor;
|
|
145
|
+
return Number.isFinite(result) ? result : undefined;
|
|
146
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { ExportLimits, ExportProgress } from "../../core/index.js";
|
|
2
|
+
import type { XlsxOptions } from "../../xlsx/index.js";
|
|
3
|
+
import type { PdfDocumentOptions } from "../../pdf/index.js";
|
|
4
|
+
import type { PdfDrawingFont } from "../../pdf/drawing-types.js";
|
|
5
|
+
/** One observation. `x` matches the capture's x kind: an ISO 8601 string or Date, a finite number, or a category label. */
|
|
6
|
+
export interface ChartForgeXPoint {
|
|
7
|
+
readonly x: string | number | Date | null;
|
|
8
|
+
/** The x value as the chart displays it, for example `Mar 2026`. */
|
|
9
|
+
readonly xCaption?: string;
|
|
10
|
+
readonly y: number | null;
|
|
11
|
+
/** The value as the chart displays it, for example `1.2k ms`. */
|
|
12
|
+
readonly caption?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ChartForgeXSeries {
|
|
15
|
+
/** Stable series identity; names are display text and may repeat. */
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly name: string;
|
|
18
|
+
/** Unit appended to the value heading, for example `ms`. Overrides the y axis unit. */
|
|
19
|
+
readonly unit?: string;
|
|
20
|
+
/** Native Excel number format for this series' values. Overrides the y axis format. */
|
|
21
|
+
readonly format?: string;
|
|
22
|
+
readonly points: readonly ChartForgeXPoint[];
|
|
23
|
+
}
|
|
24
|
+
export interface ChartForgeXAxis {
|
|
25
|
+
readonly title?: string;
|
|
26
|
+
readonly unit?: string;
|
|
27
|
+
/** Native Excel number format, for example `0.0` or `yyyy-mm-dd`. */
|
|
28
|
+
readonly format?: string;
|
|
29
|
+
}
|
|
30
|
+
/** A plain chart capture, as ChartForgeX's browser runtime produces it. OfficeIMO never reads the live chart. */
|
|
31
|
+
export interface ChartForgeXCapture {
|
|
32
|
+
readonly version: 1;
|
|
33
|
+
readonly id?: string;
|
|
34
|
+
readonly title?: string;
|
|
35
|
+
readonly description?: string;
|
|
36
|
+
readonly x: ChartForgeXAxis & {
|
|
37
|
+
readonly kind: "date" | "number" | "category";
|
|
38
|
+
};
|
|
39
|
+
readonly y?: ChartForgeXAxis;
|
|
40
|
+
readonly series: readonly ChartForgeXSeries[];
|
|
41
|
+
/** Serialized SVG whose paint is resolved inline (style declarations or presentation attributes). Required for PDF. */
|
|
42
|
+
readonly svg?: string | {
|
|
43
|
+
readonly outerHTML: string;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export type ChartForgeXFormat = "xlsx" | "pdf";
|
|
47
|
+
export interface ChartForgeXDiagnostic {
|
|
48
|
+
/** For example `OPACITY_FLATTENED`, `GRADIENT_FLATTENED`, `CLIP_IGNORED`, `IMAGE_SKIPPED` or `TEXT_SUBSTITUTED`. */
|
|
49
|
+
readonly code: string;
|
|
50
|
+
/** The SVG element name the diagnostic applies to. */
|
|
51
|
+
readonly element?: string;
|
|
52
|
+
}
|
|
53
|
+
export interface ChartForgeXSvgOptions {
|
|
54
|
+
/** SVG features without a vector equivalent are skipped with a diagnostic by default; `reject` fails instead. */
|
|
55
|
+
readonly unsupported?: "skip" | "reject";
|
|
56
|
+
/** Drawing font slots the PDF can paint. Text in another slot falls back to `regular`. Default: all four. */
|
|
57
|
+
readonly fonts?: readonly PdfDrawingFont[];
|
|
58
|
+
/** Map text to the standard fonts' WinAnsi repertoire: other Latin letters lose their diacritics and anything else becomes `?`. Default true. */
|
|
59
|
+
readonly winAnsi?: boolean;
|
|
60
|
+
/** Override the PDF drawing ceilings used while converting SVG. */
|
|
61
|
+
readonly limits?: Pick<NonNullable<PdfDocumentOptions["limits"]>, "maxDrawingElements" | "maxVectorCommands">;
|
|
62
|
+
readonly onDiagnostic?: (diagnostic: ChartForgeXDiagnostic) => void;
|
|
63
|
+
}
|
|
64
|
+
export interface ChartForgeXWriteOptions {
|
|
65
|
+
/** `wide`: one row per x value and a column per series. `long`: one row per point. `auto` (default) picks wide unless
|
|
66
|
+
* a series repeats an x value, a point has no x, or series captions disagree for a shared x. */
|
|
67
|
+
readonly layout?: "auto" | "wide" | "long";
|
|
68
|
+
readonly signal?: AbortSignal;
|
|
69
|
+
readonly limits?: ExportLimits;
|
|
70
|
+
readonly onProgress?: (progress: ExportProgress) => void;
|
|
71
|
+
readonly onDiagnostic?: (diagnostic: ChartForgeXDiagnostic) => void;
|
|
72
|
+
readonly unsupported?: "skip" | "reject";
|
|
73
|
+
readonly xlsx?: Pick<XlsxOptions, "dateMode" | "compression" | "creator" | "created" | "modified"> & {
|
|
74
|
+
readonly sheetName?: string;
|
|
75
|
+
};
|
|
76
|
+
readonly pdf?: Pick<PdfDocumentOptions, "pageSize" | "orientation" | "margins" | "fonts" | "tagged" | "compression" | "pageNumbers" | "pageHeader" | "pageFooter" | "fontSize" | "limits">;
|
|
77
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Keep text within the standard PDF fonts' WinAnsi repertoire: typographic minus and spaces get their ASCII
|
|
2
|
+
* equivalents, invisible marks are dropped, other Latin letters lose their diacritics and any other character becomes
|
|
3
|
+
* `?`. Every changed letter or `?` is reported through `substituted`. */
|
|
4
|
+
export declare function winAnsiText(text: string, substituted: () => void): string;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { standardCode } from "../../pdf/font-support.js";
|
|
2
|
+
const replacements = new Map([[0x2212, "-"], [0x2010, "-"], [0x2011, "-"], [0x2012, "-"], [0x2009, " "], [0x200a, " "],
|
|
3
|
+
[0x202f, " "], [0x2007, " "], [0x2002, " "], [0x2003, " "], [0x200b, ""], [0x200e, ""], [0x200f, ""], [0x2060, ""], [0xfeff, ""]]);
|
|
4
|
+
// Latin letters whose stroke or form has no canonical decomposition to a base letter.
|
|
5
|
+
const baseLetters = new Map([[0x141, "L"], [0x142, "l"], [0x110, "D"], [0x111, "d"], [0x126, "H"], [0x127, "h"],
|
|
6
|
+
[0x131, "i"], [0x13f, "L"], [0x140, "l"], [0x166, "T"], [0x167, "t"], [0x180, "b"], [0x197, "I"], [0x1b5, "Z"], [0x1b6, "z"], [0x1e9e, "SS"]]);
|
|
7
|
+
/** A Latin letter outside WinAnsi without its diacritic, for example `Ł` → `L` and `ą` → `a`. */
|
|
8
|
+
function baseLetter(character) {
|
|
9
|
+
const base = baseLetters.get(character.codePointAt(0));
|
|
10
|
+
if (base !== undefined)
|
|
11
|
+
return base;
|
|
12
|
+
if (!/\p{Script=Latin}/u.test(character))
|
|
13
|
+
return undefined;
|
|
14
|
+
const stripped = character.normalize("NFD").replace(/\p{Mark}+/gu, "");
|
|
15
|
+
return stripped && stripped !== character && [...stripped].every(part => standardCode(part.codePointAt(0)) !== undefined) ? stripped : undefined;
|
|
16
|
+
}
|
|
17
|
+
/** Keep text within the standard PDF fonts' WinAnsi repertoire: typographic minus and spaces get their ASCII
|
|
18
|
+
* equivalents, invisible marks are dropped, other Latin letters lose their diacritics and any other character becomes
|
|
19
|
+
* `?`. Every changed letter or `?` is reported through `substituted`. */
|
|
20
|
+
export function winAnsiText(text, substituted) {
|
|
21
|
+
let result = "";
|
|
22
|
+
// Canonically equivalent Latin spellings should keep the same WinAnsi accents.
|
|
23
|
+
const composed = text.normalize("NFC").replace(/(\p{Script=Latin})\p{Mark}+/gu, (_sequence, letter) => {
|
|
24
|
+
substituted();
|
|
25
|
+
return letter;
|
|
26
|
+
});
|
|
27
|
+
for (const character of composed) {
|
|
28
|
+
const scalar = character.codePointAt(0);
|
|
29
|
+
if (scalar === 0xad)
|
|
30
|
+
continue; // A soft hyphen is invisible on an unbroken line.
|
|
31
|
+
if (standardCode(scalar) !== undefined) {
|
|
32
|
+
result += character;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
if (scalar === 10 || scalar === 13 || scalar === 9) {
|
|
36
|
+
result += character;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const replacement = replacements.get(scalar);
|
|
40
|
+
if (replacement !== undefined) {
|
|
41
|
+
result += replacement;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
substituted();
|
|
45
|
+
result += baseLetter(character) ?? "?";
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ExportLimits } from "../../core/limits.js";
|
|
2
|
+
import type { ColumnSettings, ExportValue } from "../../core/index.js";
|
|
3
|
+
import type { PreparedChart } from "./capture.js";
|
|
4
|
+
export type ChartColumn = ColumnSettings & {
|
|
5
|
+
readonly key: string;
|
|
6
|
+
};
|
|
7
|
+
export interface ChartTable {
|
|
8
|
+
readonly columns: readonly ChartColumn[];
|
|
9
|
+
readonly rows: Iterable<readonly ExportValue[]>;
|
|
10
|
+
}
|
|
11
|
+
/** Map the chart's series to typed columns in a stable order: x, its caption, then each series' value and caption. */
|
|
12
|
+
export declare function chartTable(chart: PreparedChart, layout?: "auto" | "wide" | "long", limits?: ExportLimits): ChartTable;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { ExportCell } from "../../core/presentation.js";
|
|
2
|
+
import { NumberFormats } from "../../xlsx/styles.js";
|
|
3
|
+
import { ExportBudget } from "../../core/limits.js";
|
|
4
|
+
const display = (header) => header + " (display)";
|
|
5
|
+
function dateFormat(chart) {
|
|
6
|
+
let time = false, seconds = false;
|
|
7
|
+
for (const series of chart.series)
|
|
8
|
+
for (const point of series.points) {
|
|
9
|
+
if (!(point.x instanceof Date))
|
|
10
|
+
continue;
|
|
11
|
+
const within = point.x.getTime() % 86400000;
|
|
12
|
+
if (within % 1000)
|
|
13
|
+
return "yyyy-mm-dd hh:mm:ss.000";
|
|
14
|
+
if (within)
|
|
15
|
+
time = true;
|
|
16
|
+
if (within % 60000)
|
|
17
|
+
seconds = true;
|
|
18
|
+
}
|
|
19
|
+
return seconds ? "yyyy-mm-dd hh:mm:ss" : time ? "yyyy-mm-dd hh:mm" : "yyyy-mm-dd";
|
|
20
|
+
}
|
|
21
|
+
function xColumn(chart) {
|
|
22
|
+
const header = chart.xTitle;
|
|
23
|
+
if (chart.kind === "date")
|
|
24
|
+
return { key: "x", header, type: "date", format: chart.xFormat ?? dateFormat(chart) };
|
|
25
|
+
if (chart.kind === "number")
|
|
26
|
+
return { key: "x", header, type: "number", ...(chart.xFormat === undefined ? {} : { format: chart.xFormat }) };
|
|
27
|
+
return { key: "x", header, type: "string" };
|
|
28
|
+
}
|
|
29
|
+
const withUnit = (header, unit) => unit ? header + " (" + unit + ")" : header;
|
|
30
|
+
/** Whether every series can share one row per x value. */
|
|
31
|
+
function canWiden(chart) {
|
|
32
|
+
const captions = new Map();
|
|
33
|
+
for (const series of chart.series) {
|
|
34
|
+
const seen = new Set();
|
|
35
|
+
for (const point of series.points) {
|
|
36
|
+
if (point.key === null)
|
|
37
|
+
return false;
|
|
38
|
+
if (seen.has(point.key))
|
|
39
|
+
return false;
|
|
40
|
+
seen.add(point.key);
|
|
41
|
+
if (point.xCaption !== undefined) {
|
|
42
|
+
const previous = captions.get(point.key);
|
|
43
|
+
if (previous !== undefined && previous !== point.xCaption)
|
|
44
|
+
return false;
|
|
45
|
+
captions.set(point.key, point.xCaption);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
/** Map the chart's series to typed columns in a stable order: x, its caption, then each series' value and caption. */
|
|
52
|
+
export function chartTable(chart, layout = "auto", limits) {
|
|
53
|
+
if (layout !== "auto" && layout !== "wide" && layout !== "long")
|
|
54
|
+
throw new TypeError("Chart layout must be auto, wide or long.");
|
|
55
|
+
const wide = layout !== "long" && canWiden(chart);
|
|
56
|
+
if (layout === "wide" && !wide)
|
|
57
|
+
throw new TypeError("A wide chart sheet needs unique x values in every series, consistent x captions, and no missing x values.");
|
|
58
|
+
const budget = new ExportBudget(limits);
|
|
59
|
+
const headingRows = chart.title ? 2 : 1;
|
|
60
|
+
function checkRows(count) {
|
|
61
|
+
budget.row(count);
|
|
62
|
+
if (count + headingRows > 1048576)
|
|
63
|
+
throw new RangeError("Excel supports at most 1,048,576 rows including headings.");
|
|
64
|
+
}
|
|
65
|
+
function checkCells(rows, columns) {
|
|
66
|
+
budget.check("maxCells", (rows + headingRows) * columns);
|
|
67
|
+
}
|
|
68
|
+
const x = xColumn(chart), hasXCaption = chart.series.some(series => series.points.some(point => point.xCaption !== undefined));
|
|
69
|
+
const value = (point, format, column) => point?.y === null || point === undefined ? null :
|
|
70
|
+
format !== undefined && format !== column ? new ExportCell(point.y, { presentation: { numberFormat: format || NumberFormats.General } }) : point.y;
|
|
71
|
+
if (!wide) {
|
|
72
|
+
const yFormat = chart.yFormat, hasCaption = chart.series.some(series => series.points.some(point => point.caption !== undefined));
|
|
73
|
+
const units = new Set(chart.series.filter(series => series.points.length > 0).map(series => (series.unit ?? chart.yUnit) || undefined));
|
|
74
|
+
const mixedUnits = units.size > 1;
|
|
75
|
+
const yHeader = withUnit(chart.yTitle, units.size === 0 ? chart.yUnit : mixedUnits ? undefined : units.values().next().value);
|
|
76
|
+
const columns = [{ key: "series", header: "Series", type: "string" }, { key: "seriesId", header: "Series ID", type: "string" }, x,
|
|
77
|
+
...(hasXCaption ? [{ key: "xCaption", header: display(x.header), type: "string" }] : []),
|
|
78
|
+
{ key: "y", header: yHeader, type: "number", ...(yFormat === undefined ? {} : { format: yFormat }) },
|
|
79
|
+
...(hasCaption ? [{ key: "caption", header: display(yHeader), type: "string" }] : []),
|
|
80
|
+
...(mixedUnits ? [{ key: "unit", header: "Unit", type: "string" }] : [])];
|
|
81
|
+
const rowCount = chart.series.reduce((count, series) => count + series.points.length, 0);
|
|
82
|
+
checkRows(rowCount);
|
|
83
|
+
checkCells(rowCount, columns.length);
|
|
84
|
+
function* rows() {
|
|
85
|
+
for (const series of chart.series)
|
|
86
|
+
for (const point of series.points)
|
|
87
|
+
yield [series.name, series.id, point.x, ...(hasXCaption ? [point.xCaption ?? null] : []), value(point, series.format ?? yFormat, yFormat),
|
|
88
|
+
...(hasCaption ? [point.caption ?? null] : []), ...(mixedUnits ? [series.unit ?? chart.yUnit ?? null] : [])];
|
|
89
|
+
}
|
|
90
|
+
return { columns, rows: rows() };
|
|
91
|
+
}
|
|
92
|
+
const captionColumns = chart.series.map(series => series.points.some(point => point.caption !== undefined));
|
|
93
|
+
const columnCount = 1 + Number(hasXCaption) + chart.series.length + captionColumns.filter(Boolean).length;
|
|
94
|
+
if (columnCount > 16384)
|
|
95
|
+
throw new RangeError("Excel supports at most 16,384 columns.");
|
|
96
|
+
const keys = [], captions = new Map(), values = new Map();
|
|
97
|
+
const known = new Set();
|
|
98
|
+
for (const series of chart.series)
|
|
99
|
+
for (const point of series.points) {
|
|
100
|
+
if (!known.has(point.key)) {
|
|
101
|
+
checkRows(keys.length + 1);
|
|
102
|
+
known.add(point.key);
|
|
103
|
+
keys.push(point.key);
|
|
104
|
+
values.set(point.key, point.x);
|
|
105
|
+
}
|
|
106
|
+
if (point.xCaption !== undefined && !captions.has(point.key))
|
|
107
|
+
captions.set(point.key, point.xCaption);
|
|
108
|
+
}
|
|
109
|
+
checkCells(keys.length, columnCount);
|
|
110
|
+
if (chart.kind !== "category")
|
|
111
|
+
keys.sort((a, b) => a - b);
|
|
112
|
+
const names = new Map();
|
|
113
|
+
for (const series of chart.series)
|
|
114
|
+
names.set(series.name, (names.get(series.name) ?? 0) + 1);
|
|
115
|
+
const columns = [x, ...(hasXCaption ? [{ key: "xCaption", header: display(x.header), type: "string" }] : [])];
|
|
116
|
+
const lookups = chart.series.map((series, index) => {
|
|
117
|
+
const format = series.format ?? chart.yFormat, header = withUnit(series.name + (names.get(series.name) > 1 ? " [" + series.id + "]" : ""), series.unit ?? chart.yUnit);
|
|
118
|
+
const caption = captionColumns[index];
|
|
119
|
+
columns.push({ key: "s" + index, header, type: "number", ...(format === undefined ? {} : { format }) });
|
|
120
|
+
if (caption)
|
|
121
|
+
columns.push({ key: "s" + index + "Caption", header: display(header), type: "string" });
|
|
122
|
+
return { caption, format, points: new Map(series.points.map(point => [point.key, point])) };
|
|
123
|
+
});
|
|
124
|
+
function* rows() {
|
|
125
|
+
for (const key of keys) {
|
|
126
|
+
const row = [values.get(key) ?? null, ...(hasXCaption ? [captions.get(key) ?? null] : [])];
|
|
127
|
+
for (const lookup of lookups) {
|
|
128
|
+
const point = lookup.points.get(key);
|
|
129
|
+
row.push(point?.y ?? null);
|
|
130
|
+
if (lookup.caption)
|
|
131
|
+
row.push(point?.caption ?? null);
|
|
132
|
+
}
|
|
133
|
+
yield row;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return { columns, rows: rows() };
|
|
137
|
+
}
|
|
@@ -12,4 +12,4 @@ export interface PreparedPdfDrawing extends PdfDrawingUsage {
|
|
|
12
12
|
}
|
|
13
13
|
export declare function checkDrawingBlock(value: unknown): void;
|
|
14
14
|
/** Preflight and paint owned records. The existing font owner retains glyph maps and subsets. */
|
|
15
|
-
export declare function prepareDrawing(drawing: PdfDrawing
|
|
15
|
+
export declare function prepareDrawing(drawing: PdfDrawing<1> | PdfDrawing<2>, settings: PdfSettings, fonts: PdfFontResources, used: PdfDrawingUsage, resources: PdfDrawingResources): Promise<PreparedPdfDrawing>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NotSupportedError, OfficeIMOError } from "../core/errors.js";
|
|
2
2
|
import { checkAbort, pause, taskYieldDue } from "../core/iteration.js";
|
|
3
|
-
import { color, positive } from "./settings.js";
|
|
3
|
+
import { color, pdfDrawingMaxDepth, positive } from "./settings.js";
|
|
4
4
|
import { pdfNumber as n } from "./objects.js";
|
|
5
5
|
/** Portable drawings are an exact schema: unsupported data must never be silently discarded. */
|
|
6
6
|
function record(value, keys, name) {
|
|
@@ -20,22 +20,50 @@ function clipRectangle(value) {
|
|
|
20
20
|
const clip = record(value, ["x", "y", "width", "height"], "drawing clip");
|
|
21
21
|
return { x: coordinate(clip.x), y: coordinate(clip.y), width: positive(clip.width, "Drawing clip width", 1000000), height: positive(clip.height, "Drawing clip height", 1000000) };
|
|
22
22
|
}
|
|
23
|
+
/** A PDF dash array: 1 through 16 nonnegative lengths, not all zero, and a finite phase. */
|
|
24
|
+
function dashPattern(value, offset) {
|
|
25
|
+
if (value === undefined) {
|
|
26
|
+
if (offset !== undefined)
|
|
27
|
+
throw new TypeError("Drawing dashOffset requires dash.");
|
|
28
|
+
return "";
|
|
29
|
+
}
|
|
30
|
+
if (!Array.isArray(value) || !value.length || value.length > 16)
|
|
31
|
+
throw new TypeError("Drawing dash must list 1 through 16 nonnegative lengths with at least one positive length.");
|
|
32
|
+
const lengths = Array.from(value, item => {
|
|
33
|
+
if (typeof item !== "number" || !Number.isFinite(item) || item < 0 || item > 14400)
|
|
34
|
+
throw new TypeError("Drawing dash lengths must be from 0 through 14,400 points.");
|
|
35
|
+
return n(item);
|
|
36
|
+
});
|
|
37
|
+
if (lengths.every(length => length === "0"))
|
|
38
|
+
throw new TypeError("Drawing dash needs a positive length after PDF rounding.");
|
|
39
|
+
let phase = coordinate(offset ?? 0);
|
|
40
|
+
if (phase < 0) {
|
|
41
|
+
// Work in serialized thousandths of a point. Odd arrays repeat twice before the paint state repeats.
|
|
42
|
+
const period = lengths.reduce((sum, length) => sum + Math.round(Number(length) * 1000), 0) * (lengths.length % 2 ? 2 : 1);
|
|
43
|
+
phase = ((Math.round(phase * 1000) % period + period) % period) / 1000;
|
|
44
|
+
}
|
|
45
|
+
return "[" + lengths.join(" ") + "] " + n(phase) + " d\n";
|
|
46
|
+
}
|
|
23
47
|
async function cooperate(settings) {
|
|
24
48
|
if (taskYieldDue())
|
|
25
49
|
await pause();
|
|
26
50
|
checkAbort(settings.options.signal);
|
|
27
51
|
}
|
|
28
52
|
/** Take ownership of one bounded schema before any cooperative or font work can run caller code. */
|
|
29
|
-
function captureDrawing(drawing, settings, used) {
|
|
53
|
+
function captureDrawing(drawing, settings, used, images) {
|
|
30
54
|
checkAbort(settings.options.signal);
|
|
31
|
-
const root = record(drawing, ["version", "width", "height", "elements"], "drawing");
|
|
32
|
-
|
|
33
|
-
|
|
55
|
+
const root = record(drawing, ["version", "width", "height", "elements", "standardFonts"], "drawing");
|
|
56
|
+
const standardFonts = root.standardFonts;
|
|
57
|
+
if (standardFonts !== undefined && typeof standardFonts !== "boolean")
|
|
58
|
+
throw new TypeError("Drawing standardFonts must be boolean.");
|
|
59
|
+
const version = root.version;
|
|
60
|
+
if (version !== 1 && version !== 2)
|
|
61
|
+
throw new NotSupportedError("Only portable drawing versions 1 and 2 are supported.");
|
|
34
62
|
const width = positive(root.width, "Drawing width"), height = positive(root.height, "Drawing height");
|
|
35
63
|
let commands = used.commands, elements = used.elements;
|
|
36
64
|
const ancestors = new Set();
|
|
37
65
|
function path(value) {
|
|
38
|
-
record(value, ["kind", "commands", "fill", "stroke", "strokeWidth", "fillRule", "lineCap", "lineJoin"], "drawing path");
|
|
66
|
+
record(value, ["kind", "commands", "fill", "stroke", "strokeWidth", "fillRule", "lineCap", "lineJoin", "miterLimit", "dash", "dashOffset"], "drawing path");
|
|
39
67
|
const source = value.commands;
|
|
40
68
|
if (!Array.isArray(source))
|
|
41
69
|
throw new TypeError("Drawing path commands must be an array.");
|
|
@@ -57,6 +85,10 @@ function captureDrawing(drawing, settings, used) {
|
|
|
57
85
|
throw new TypeError("Invalid drawing lineCap.");
|
|
58
86
|
if (lineJoin !== undefined && !["miter", "round", "bevel"].includes(lineJoin))
|
|
59
87
|
throw new TypeError("Invalid drawing lineJoin.");
|
|
88
|
+
const miterLimit = positive(value.miterLimit ?? 10, "Drawing miter limit", 1000000);
|
|
89
|
+
if (miterLimit < 1)
|
|
90
|
+
throw new RangeError("Drawing miter limit must be at least 1.");
|
|
91
|
+
const dash = dashPattern(value.dash, value.dashOffset);
|
|
60
92
|
for (let i = 0; i < snapshot.length; i++) {
|
|
61
93
|
const c = snapshot[i];
|
|
62
94
|
if (!c || typeof c !== "object")
|
|
@@ -89,11 +121,12 @@ function captureDrawing(drawing, settings, used) {
|
|
|
89
121
|
if (!started)
|
|
90
122
|
throw new TypeError("A drawing path needs at least one command.");
|
|
91
123
|
return { kind: "path", commands: copied, style: (fill === undefined ? "" : fill + " rg\n") + (stroke === undefined ? "" : stroke + " RG\n" + n(strokeWidth) + " w\n") +
|
|
92
|
-
String(["butt", "round", "square"].indexOf(lineCap ?? "butt")) + " J\n" + String(["miter", "round", "bevel"].indexOf(lineJoin ?? "miter")) +
|
|
124
|
+
String(["butt", "round", "square"].indexOf(lineCap ?? "butt")) + " J\n" + String(["miter", "round", "bevel"].indexOf(lineJoin ?? "miter")) +
|
|
125
|
+
" j\n" + n(miterLimit) + " M\n" + dash,
|
|
93
126
|
paint: (fill !== undefined && stroke !== undefined ? "B" : fill !== undefined ? "f" : "S") + (fill !== undefined && fillRule === "evenodd" ? "*" : "") + "\n" };
|
|
94
127
|
}
|
|
95
128
|
function text(value) {
|
|
96
|
-
record(value, ["kind", "text", "x", "y", "font", "fontSize", "color"], "drawing text");
|
|
129
|
+
record(value, ["kind", "text", "x", "y", "font", "fontSize", "color", "anchor", "rotation"], "drawing text");
|
|
97
130
|
const snapshot = value.text, slot = value.font;
|
|
98
131
|
if (typeof snapshot !== "string")
|
|
99
132
|
throw new TypeError("Drawing text must be a string.");
|
|
@@ -104,14 +137,33 @@ function captureDrawing(drawing, settings, used) {
|
|
|
104
137
|
if (!["regular", "bold", "italic", "boldItalic"].includes(slot))
|
|
105
138
|
throw new TypeError("Invalid drawing font slot.");
|
|
106
139
|
const x = coordinate(value.x), y = coordinate(value.y), size = positive(value.fontSize, "Drawing font size"), foreground = color(value.color);
|
|
140
|
+
const anchor = value.anchor, rotation = value.rotation ?? 0;
|
|
141
|
+
if (anchor !== undefined && !["start", "middle", "end"].includes(anchor))
|
|
142
|
+
throw new TypeError("Invalid drawing text anchor.");
|
|
143
|
+
if (typeof rotation !== "number" || !Number.isFinite(rotation) || Math.abs(rotation) > 360)
|
|
144
|
+
throw new RangeError("Drawing text rotation must be finite degrees within -360 through 360.");
|
|
107
145
|
settings.budget.cell(snapshot);
|
|
108
|
-
|
|
146
|
+
// Quarter turns stay exact so upright and vertical axis labels keep integral matrices.
|
|
147
|
+
const turn = rotation % 90 === 0 ? (((rotation / 90) % 4) + 4) % 4 : -1, radians = rotation * Math.PI / 180;
|
|
148
|
+
const cos = turn < 0 ? Math.cos(radians) : [1, 0, -1, 0][turn], sin = turn < 0 ? Math.sin(radians) : [0, 1, 0, -1][turn];
|
|
149
|
+
return { kind: "text", text: snapshot, x, y, size, color: foreground, font: slot, anchor: anchor === "middle" ? .5 : anchor === "end" ? 1 : 0, cos, sin };
|
|
150
|
+
}
|
|
151
|
+
function image(value) {
|
|
152
|
+
record(value, ["kind", "image", "x", "y", "width", "height", "opacity"], "drawing image");
|
|
153
|
+
const x = coordinate(value.x), y = coordinate(value.y), suppliedOpacity = value.opacity;
|
|
154
|
+
const opacity = suppliedOpacity === undefined ? 1 : suppliedOpacity;
|
|
155
|
+
const width = positive(value.width, "Drawing image width", 1000000), height = positive(value.height, "Drawing image height", 1000000);
|
|
156
|
+
if (typeof opacity !== "number" || !(opacity >= 0 && opacity <= 1))
|
|
157
|
+
throw new RangeError("Drawing image opacity must be from 0 through 1.");
|
|
158
|
+
const source = value.image;
|
|
159
|
+
images.snapshot(source);
|
|
160
|
+
return { kind: "image", image: source, x, y, width, height, opacity };
|
|
109
161
|
}
|
|
110
162
|
function children(input, depth) {
|
|
111
163
|
if (!Array.isArray(input))
|
|
112
164
|
throw new TypeError("Drawing elements must be an array.");
|
|
113
|
-
if (depth >
|
|
114
|
-
throw new RangeError("Drawing groups support at most
|
|
165
|
+
if (depth > pdfDrawingMaxDepth && input.length)
|
|
166
|
+
throw new RangeError("Drawing groups support at most " + pdfDrawingMaxDepth + " element levels.");
|
|
115
167
|
if (elements + input.length > settings.limits.maxDrawingElements)
|
|
116
168
|
throw new OfficeIMOError("RESOURCE_LIMIT", "maxDrawingElements exceeded.");
|
|
117
169
|
checkAbort(settings.options.signal);
|
|
@@ -129,6 +181,8 @@ function captureDrawing(drawing, settings, used) {
|
|
|
129
181
|
output.push(path(value));
|
|
130
182
|
else if (kind === "text")
|
|
131
183
|
output.push(text(value));
|
|
184
|
+
else if (kind === "image" && version === 2)
|
|
185
|
+
output.push(image(value));
|
|
132
186
|
else if (kind === "group") {
|
|
133
187
|
record(value, ["kind", "clip", "elements"], "drawing group");
|
|
134
188
|
const clipValue = value.clip, clip = clipValue === undefined ? undefined : clipRectangle(clipValue);
|
|
@@ -145,29 +199,35 @@ function captureDrawing(drawing, settings, used) {
|
|
|
145
199
|
}
|
|
146
200
|
const plan = children(root.elements, 1);
|
|
147
201
|
checkAbort(settings.options.signal);
|
|
148
|
-
return { width, height, plan, commands: commands - used.commands, elements: elements - used.elements };
|
|
202
|
+
return { width, height, plan, commands: commands - used.commands, elements: elements - used.elements, standardFonts: standardFonts === true };
|
|
149
203
|
}
|
|
150
204
|
/** Preflight and paint owned records. The existing font owner retains glyph maps and subsets. */
|
|
151
|
-
export async function prepareDrawing(drawing, settings, fonts, used) {
|
|
205
|
+
export async function prepareDrawing(drawing, settings, fonts, used, resources) {
|
|
152
206
|
// A drawing's bounded capture is synchronous; yielding here would leave later caller records borrowed.
|
|
153
|
-
const captured = captureDrawing(drawing, settings, used), drawingFonts = new Map();
|
|
207
|
+
const { images } = resources, captured = captureDrawing(drawing, settings, used, images), drawingFonts = new Map();
|
|
154
208
|
function fontFor(slot) {
|
|
155
209
|
const existing = drawingFonts.get(slot);
|
|
156
210
|
if (existing)
|
|
157
211
|
return existing;
|
|
158
|
-
const font = fonts.select(slot === "bold" || slot === "boldItalic", slot === "italic" || slot === "boldItalic");
|
|
159
|
-
if (
|
|
212
|
+
const font = fonts.select(slot === "bold" || slot === "boldItalic", slot === "italic" || slot === "boldItalic", undefined, captured.standardFonts);
|
|
213
|
+
if (font.syntheticBold || font.syntheticItalic || !font.program && !captured.standardFonts)
|
|
160
214
|
throw new NotSupportedError("Drawing text without an exact embedded font face at fonts." + slot);
|
|
161
215
|
drawingFonts.set(slot, font);
|
|
162
216
|
return font;
|
|
163
217
|
}
|
|
164
218
|
// The work budget belongs to the drawing, including short runs in separate sibling groups.
|
|
165
219
|
let preflightCharacters = 0, preflightElements = 0;
|
|
220
|
+
const imageNames = new Map();
|
|
166
221
|
async function preflight(nodes) {
|
|
167
222
|
for (let i = 0; i < nodes.length; i++) {
|
|
168
223
|
const node = nodes[i];
|
|
169
224
|
if (node.kind === "group")
|
|
170
225
|
await preflight(node.children);
|
|
226
|
+
else if (node.kind === "image" && !imageNames.has(node.image)) {
|
|
227
|
+
const resource = await images.prepare(node.image);
|
|
228
|
+
resources.registerImage(resource.name, resource.id);
|
|
229
|
+
imageNames.set(node.image, resource.name);
|
|
230
|
+
}
|
|
171
231
|
else if (node.kind === "text") {
|
|
172
232
|
const font = fontFor(node.font);
|
|
173
233
|
for (const character of node.text) {
|
|
@@ -210,9 +270,23 @@ export async function prepareDrawing(drawing, settings, fonts, used) {
|
|
|
210
270
|
}
|
|
211
271
|
add(node.paint);
|
|
212
272
|
}
|
|
273
|
+
else if (node.kind === "image") {
|
|
274
|
+
// Image space is a unit square with a bottom-left origin; the drawing's y axis points down.
|
|
275
|
+
if (node.opacity < 1)
|
|
276
|
+
add("/" + resources.opacity(node.opacity) + " gs\n");
|
|
277
|
+
add([node.width, 0, 0, -node.height, node.x, node.y + node.height].map(n).join(" ") + " cm /" + imageNames.get(node.image) + " Do\n");
|
|
278
|
+
}
|
|
213
279
|
else {
|
|
214
280
|
const font = fontFor(node.font);
|
|
215
|
-
|
|
281
|
+
let shift = 0;
|
|
282
|
+
if (node.anchor) {
|
|
283
|
+
for (const character of node.text)
|
|
284
|
+
shift += font.width(character.codePointAt(0));
|
|
285
|
+
shift *= node.size / 1000 * node.anchor;
|
|
286
|
+
}
|
|
287
|
+
// Text space is upright in the drawing's downward y axis; rotation turns the baseline clockwise about its anchor.
|
|
288
|
+
add(node.color + " rg\nBT /" + font.name + " " + n(node.size) + " Tf 0 Tr " +
|
|
289
|
+
[node.cos, node.sin, node.sin, -node.cos, node.x - shift * node.cos, node.y - shift * node.sin].map(n).join(" ") + " Tm <");
|
|
216
290
|
let chunk = "";
|
|
217
291
|
for (const character of node.text) {
|
|
218
292
|
chunk += character;
|