odf.js 6.3.0 → 6.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +172 -18
- package/dist/{codec-BnzBd5_I.d.cts → codec-CFdlbFLJ.d.cts} +16 -10
- package/dist/{codec-Dg_HWuH1.d.ts → codec-DTAgph2r.d.ts} +16 -10
- package/dist/codec.d.cts +1 -1
- package/dist/codec.d.ts +1 -1
- package/dist/{embedded-BONPxvS3.js → embedded-BnavfkP-.js} +3 -3
- package/dist/{embedded-wrtHCJ68.cjs → embedded-Bxyss_ck.cjs} +14 -2
- package/dist/index.cjs +34 -1
- package/dist/index.d.cts +19 -13
- package/dist/index.d.ts +19 -13
- package/dist/index.js +12 -6
- package/dist/{list-vhXd0Jf6.d.ts → list-7FO0SDqZ.d.ts} +14 -1
- package/dist/{list-Bttwr1mk.d.cts → list-BorrTJBY.d.cts} +14 -1
- package/dist/{ns-C14w-Zhf.d.cts → ns-DrleaIZQ.d.cts} +3 -1
- package/dist/{ns-CTyKJZeF.d.ts → ns-E0TfX9tZ.d.ts} +3 -1
- package/dist/ooo1/ns.cjs +9 -0
- package/dist/ooo1/ns.d.cts +2 -2
- package/dist/ooo1/ns.d.ts +2 -2
- package/dist/ooo1/ns.js +8 -1
- package/dist/ooo1/properties.cjs +30 -0
- package/dist/ooo1/properties.d.cts +6 -2
- package/dist/ooo1/properties.d.ts +6 -2
- package/dist/ooo1/properties.js +30 -1
- package/dist/ooo1/read.cjs +1 -1
- package/dist/ooo1/read.d.cts +1 -1
- package/dist/ooo1/read.d.ts +1 -1
- package/dist/ooo1/read.js +1 -1
- package/dist/ooo1/transform.cjs +290 -0
- package/dist/ooo1/transform.d.cts +2 -2
- package/dist/ooo1/transform.d.ts +2 -2
- package/dist/ooo1/transform.js +293 -4
- package/dist/ooo1/write.cjs +40 -0
- package/dist/ooo1/write.d.cts +2 -0
- package/dist/ooo1/write.d.ts +2 -0
- package/dist/ooo1/write.js +32 -0
- package/dist/package-io/scaffold.cjs +2 -1
- package/dist/package-io/scaffold.js +2 -1
- package/dist/{paragraph-BdwsQisB.d.cts → paragraph-Bf6Iq5wC.d.cts} +1 -1
- package/dist/{paragraph-BoVy1LrN.d.ts → paragraph-DtC86tft.d.ts} +1 -1
- package/dist/{path-CK0dIPgx.d.cts → path-8HQS-J7y.d.cts} +3 -1
- package/dist/{path-CK0dIPgx.d.ts → path-8HQS-J7y.d.ts} +3 -1
- package/dist/{read-Bo1GBjSE.d.ts → read-A3CJkqVM.d.ts} +3 -1
- package/dist/{read-RFqq4ckj.d.cts → read-BgnLFuSi.d.cts} +1 -1
- package/dist/{read-3yy5EFV_.d.ts → read-CqaZ01kU.d.ts} +1 -1
- package/dist/{read-tpfiT_n4.d.cts → read-fphkvCpR.d.cts} +3 -1
- package/dist/{shapes-DUzrrf7b.d.ts → shapes-B6Ms7ibr.d.ts} +1 -1
- package/dist/{shapes-BQhXAk0D.d.cts → shapes-CbBc9t0y.d.cts} +1 -1
- package/dist/{table-ByhqRnSG.d.cts → table-CECMtPxZ.d.cts} +6 -2
- package/dist/{table-DElqGcT0.d.ts → table-XuniPr2I.d.ts} +6 -2
- package/dist/{transform-SDysw9wx.d.cts → transform-DFCPKX7e.d.cts} +2 -1
- package/dist/{transform-DRr76JEu.d.ts → transform-DPliWQuD.d.ts} +2 -1
- package/dist/typed/draw/embedded.cjs +1 -1
- package/dist/typed/draw/embedded.js +1 -1
- package/dist/typed/draw/shapes.cjs +7 -2
- package/dist/typed/draw/shapes.d.cts +1 -1
- package/dist/typed/draw/shapes.d.ts +1 -1
- package/dist/typed/draw/shapes.js +7 -2
- package/dist/typed/draw/write-shapes.cjs +210 -0
- package/dist/typed/draw/write-shapes.d.cts +2 -0
- package/dist/typed/draw/write-shapes.d.ts +2 -0
- package/dist/typed/draw/write-shapes.js +203 -0
- package/dist/typed/draw/write-vectors.cjs +132 -0
- package/dist/typed/draw/write-vectors.d.cts +2 -0
- package/dist/typed/draw/write-vectors.d.ts +2 -0
- package/dist/typed/draw/write-vectors.js +129 -0
- package/dist/typed/odb/form.cjs +1 -1
- package/dist/typed/odb/form.js +1 -1
- package/dist/typed/odg/write.cjs +80 -0
- package/dist/typed/odg/write.d.cts +2 -0
- package/dist/typed/odg/write.d.ts +2 -0
- package/dist/typed/odg/write.js +77 -0
- package/dist/typed/odp/write.cjs +108 -0
- package/dist/typed/odp/write.d.cts +2 -0
- package/dist/typed/odp/write.d.ts +2 -0
- package/dist/typed/odp/write.js +105 -0
- package/dist/typed/ods/read.cjs +3 -1
- package/dist/typed/ods/read.d.cts +2 -2
- package/dist/typed/ods/read.d.ts +2 -2
- package/dist/typed/ods/read.js +2 -2
- package/dist/typed/ods/write.cjs +578 -0
- package/dist/typed/ods/write.d.cts +2 -0
- package/dist/typed/ods/write.d.ts +2 -0
- package/dist/typed/ods/write.js +575 -0
- package/dist/typed/odt/read.cjs +1 -1
- package/dist/typed/odt/read.js +1 -1
- package/dist/typed/odt/write.cjs +9 -140
- package/dist/typed/odt/write.d.cts +1 -1
- package/dist/typed/odt/write.d.ts +1 -1
- package/dist/typed/odt/write.js +8 -139
- package/dist/typed/shared/canonicalise.cjs +126 -0
- package/dist/typed/shared/canonicalise.d.cts +10 -0
- package/dist/typed/shared/canonicalise.d.ts +10 -0
- package/dist/typed/shared/canonicalise.js +120 -0
- package/dist/typed/shared/list.cjs +32 -0
- package/dist/typed/shared/list.d.cts +2 -2
- package/dist/typed/shared/list.d.ts +2 -2
- package/dist/typed/shared/list.js +28 -1
- package/dist/typed/shared/paragraph.d.cts +1 -1
- package/dist/typed/shared/paragraph.d.ts +1 -1
- package/dist/typed/shared/path.cjs +19 -0
- package/dist/typed/shared/path.d.cts +2 -2
- package/dist/typed/shared/path.d.ts +2 -2
- package/dist/typed/shared/path.js +18 -1
- package/dist/typed/shared/table.cjs +3 -0
- package/dist/typed/shared/table.d.cts +2 -2
- package/dist/typed/shared/table.d.ts +2 -2
- package/dist/typed/shared/table.js +1 -1
- package/dist/typed/shared/units.cjs +16 -1
- package/dist/typed/shared/units.d.cts +2 -2
- package/dist/typed/shared/units.d.ts +2 -2
- package/dist/typed/shared/units.js +16 -2
- package/dist/{units-BQSNkTQZ.d.ts → units-DdyaQolR.d.cts} +2 -1
- package/dist/{units-BQSNkTQZ.d.cts → units-DdyaQolR.d.ts} +2 -1
- package/dist/write-4kMi-asu.d.ts +14 -0
- package/dist/write-BZXpT8F6.d.cts +17 -0
- package/dist/write-BhG3t3fJ.d.cts +14 -0
- package/dist/write-BuLQmwLD.d.cts +14 -0
- package/dist/write-C2c4xBXs.d.cts +14 -0
- package/dist/{write-7Uu_wlkL.d.cts → write-CPATqG4v.d.cts} +1 -0
- package/dist/write-CfTE2jY8.d.ts +14 -0
- package/dist/write-DttX1rTC.d.ts +14 -0
- package/dist/{write-DGcI9W7Z.d.ts → write-JERychL3.d.ts} +1 -0
- package/dist/write-SKnCTU4C.d.ts +17 -0
- package/dist/write-shapes-Co7BrmMZ.d.ts +36 -0
- package/dist/write-shapes-D4piGKf4.d.cts +36 -0
- package/dist/write-vectors-B-RVHAKI.d.cts +11 -0
- package/dist/write-vectors-CYjZoVBW.d.ts +11 -0
- package/package.json +2 -2
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { el, txt } from "../../xml/fragment.js";
|
|
2
|
+
import { encodeXmlText } from "../../xml/entities.js";
|
|
3
|
+
import { formatOdfLength, formatOdfNumber } from "../shared/units.js";
|
|
4
|
+
import { buildOdfListStyle, closeListPlan, listKindOf, planListMembership, writeOdfList } from "../shared/list.js";
|
|
5
|
+
import { writeOdfParagraph } from "../shared/paragraph.js";
|
|
6
|
+
import { writeOdfTable } from "../shared/table.js";
|
|
7
|
+
import { canonicalImage, canonicalParagraph, canonicalTable } from "../shared/canonicalise.js";
|
|
8
|
+
//#region src/typed/draw/write-shapes.ts
|
|
9
|
+
function unsupportedShapeContent(what) {
|
|
10
|
+
return /* @__PURE__ */ new Error(`writeDrawFrame: a shape carries ${what}, which this writer does not write yet -- refusing rather than producing a document that silently lost it. See ExaDev/documents.js for the tracked follow-up covering the fidelity constructs.`);
|
|
11
|
+
}
|
|
12
|
+
function planShapeContent(blocks, listState) {
|
|
13
|
+
closeListPlan(listState);
|
|
14
|
+
if (blocks.length === 1) {
|
|
15
|
+
const only = blocks[0];
|
|
16
|
+
if (only.kind === "table") return {
|
|
17
|
+
kind: "table",
|
|
18
|
+
table: only
|
|
19
|
+
};
|
|
20
|
+
if (only.kind === "image") return {
|
|
21
|
+
kind: "image",
|
|
22
|
+
image: only
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const paragraphs = [];
|
|
26
|
+
for (const block of blocks) {
|
|
27
|
+
if (block.kind === "table") throw unsupportedShapeContent("a table alongside other content (a draw:frame's own content is exactly one of table:table/draw:text-box/draw:image, never a mix)");
|
|
28
|
+
if (block.kind === "image") throw unsupportedShapeContent("an image alongside other content (same reason)");
|
|
29
|
+
if (block.kind === "pageBreak") throw unsupportedShapeContent("a page break (no ODF spelling inside a shape's own text)");
|
|
30
|
+
if (block.kind === "embeddedObject") throw unsupportedShapeContent("an embedded object");
|
|
31
|
+
if (block.kind === "constructStart" || block.kind === "constructEnd") throw unsupportedShapeContent("a construct boundary marker");
|
|
32
|
+
if (block.constructs !== void 0 && block.constructs.length > 0) throw unsupportedShapeContent("a run-level construct extent (a field, bookmark, note, annotation, or tracked change)");
|
|
33
|
+
if (block.headingLevel !== void 0) throw unsupportedShapeContent("a heading (a shape's own draw:text-box has no text:h reading path)");
|
|
34
|
+
const canonicalId = planListMembership(block.list, listState);
|
|
35
|
+
paragraphs.push(canonicalId === void 0 ? {
|
|
36
|
+
...block,
|
|
37
|
+
list: void 0
|
|
38
|
+
} : {
|
|
39
|
+
...block,
|
|
40
|
+
list: {
|
|
41
|
+
numId: canonicalId,
|
|
42
|
+
level: block.list.level
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
kind: "text",
|
|
48
|
+
paragraphs
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function createDrawShapeWriteState(pkg, registry, contentAutomaticStyles) {
|
|
52
|
+
return {
|
|
53
|
+
pkg,
|
|
54
|
+
registry,
|
|
55
|
+
contentAutomaticStyles,
|
|
56
|
+
nextImage: 1,
|
|
57
|
+
nextTable: 1,
|
|
58
|
+
nextListStyle: 1,
|
|
59
|
+
listStyleByKind: /* @__PURE__ */ new Map()
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function listStyleNameFor(kind, state) {
|
|
63
|
+
const existing = state.listStyleByKind.get(kind);
|
|
64
|
+
if (existing !== void 0) return existing;
|
|
65
|
+
const name = `SL${state.nextListStyle}`;
|
|
66
|
+
state.nextListStyle += 1;
|
|
67
|
+
state.listStyleByKind.set(kind, name);
|
|
68
|
+
state.contentAutomaticStyles.children.push(buildOdfListStyle(name, kind));
|
|
69
|
+
return name;
|
|
70
|
+
}
|
|
71
|
+
function frameGeometryAttrs(frame, rotationDeg) {
|
|
72
|
+
if (rotationDeg === void 0 || rotationDeg === 0) return {
|
|
73
|
+
"svg:x": formatOdfLength(frame.xPt),
|
|
74
|
+
"svg:y": formatOdfLength(frame.yPt),
|
|
75
|
+
"svg:width": formatOdfLength(frame.widthPt),
|
|
76
|
+
"svg:height": formatOdfLength(frame.heightPt)
|
|
77
|
+
};
|
|
78
|
+
const angleRad = -rotationDeg * Math.PI / 180;
|
|
79
|
+
const cos = Math.cos(angleRad);
|
|
80
|
+
const sin = Math.sin(angleRad);
|
|
81
|
+
const halfWidthPt = frame.widthPt / 2;
|
|
82
|
+
const halfHeightPt = frame.heightPt / 2;
|
|
83
|
+
const txPt = frame.xPt + halfWidthPt - halfWidthPt * cos - halfHeightPt * sin;
|
|
84
|
+
const tyPt = frame.yPt + halfHeightPt - halfHeightPt * cos + halfWidthPt * sin;
|
|
85
|
+
return {
|
|
86
|
+
"svg:width": formatOdfLength(frame.widthPt),
|
|
87
|
+
"svg:height": formatOdfLength(frame.heightPt),
|
|
88
|
+
"draw:transform": `rotate(${formatOdfNumber(angleRad)}) translate(${formatOdfLength(txPt)} ${formatOdfLength(tyPt)})`
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function shapeGraphicStyleName(shape, state) {
|
|
92
|
+
const properties = {
|
|
93
|
+
"draw:fill": "none",
|
|
94
|
+
"draw:stroke": "none"
|
|
95
|
+
};
|
|
96
|
+
if (shape.insetLeftPt !== 0 || shape.insetTopPt !== 0 || shape.insetRightPt !== 0 || shape.insetBottomPt !== 0) {
|
|
97
|
+
properties["fo:padding-left"] = formatOdfLength(shape.insetLeftPt);
|
|
98
|
+
properties["fo:padding-top"] = formatOdfLength(shape.insetTopPt);
|
|
99
|
+
properties["fo:padding-right"] = formatOdfLength(shape.insetRightPt);
|
|
100
|
+
properties["fo:padding-bottom"] = formatOdfLength(shape.insetBottomPt);
|
|
101
|
+
}
|
|
102
|
+
return state.registry.intern({
|
|
103
|
+
properties: {},
|
|
104
|
+
family: "graphic",
|
|
105
|
+
propertyElements: [el("style:graphic-properties", properties)]
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function writeShapeTextBox(paragraphs, state) {
|
|
109
|
+
const out = [];
|
|
110
|
+
let openList;
|
|
111
|
+
const closeList = () => {
|
|
112
|
+
if (openList === void 0) return;
|
|
113
|
+
const kind = listKindOf(openList.numId);
|
|
114
|
+
const built = writeOdfList(openList.entries, kind === void 0 ? void 0 : listStyleNameFor(kind, state));
|
|
115
|
+
openList.element.attributes = built.attributes;
|
|
116
|
+
openList.element.children = built.children;
|
|
117
|
+
openList = void 0;
|
|
118
|
+
};
|
|
119
|
+
for (const paragraph of paragraphs) {
|
|
120
|
+
const element = writeOdfParagraph(paragraph, state.registry);
|
|
121
|
+
const membership = paragraph.list;
|
|
122
|
+
if (membership?.numId === void 0) {
|
|
123
|
+
closeList();
|
|
124
|
+
out.push(element);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (openList !== void 0 && openList.numId !== membership.numId) closeList();
|
|
128
|
+
if (openList === void 0) {
|
|
129
|
+
const listElement = el("text:list");
|
|
130
|
+
openList = {
|
|
131
|
+
numId: membership.numId,
|
|
132
|
+
entries: [],
|
|
133
|
+
element: listElement
|
|
134
|
+
};
|
|
135
|
+
out.push(listElement);
|
|
136
|
+
}
|
|
137
|
+
openList.entries.push({
|
|
138
|
+
level: membership.level,
|
|
139
|
+
element
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
closeList();
|
|
143
|
+
return el("draw:text-box", {}, out);
|
|
144
|
+
}
|
|
145
|
+
const PICTURES_DIRECTORY = "Pictures";
|
|
146
|
+
function writeShapeImage(image, state) {
|
|
147
|
+
const extension = image.format === "png" ? "png" : "jpg";
|
|
148
|
+
const path = `${PICTURES_DIRECTORY}/image${state.nextImage}.${extension}`;
|
|
149
|
+
state.nextImage += 1;
|
|
150
|
+
state.pkg.parts[path] = {
|
|
151
|
+
kind: "binary",
|
|
152
|
+
base64: image.base64
|
|
153
|
+
};
|
|
154
|
+
const children = [el("draw:image", {
|
|
155
|
+
"xlink:href": encodeXmlText(path),
|
|
156
|
+
"xlink:type": "simple",
|
|
157
|
+
"xlink:show": "embed",
|
|
158
|
+
"xlink:actuate": "onLoad"
|
|
159
|
+
})];
|
|
160
|
+
if (image.altText !== void 0) children.push(el("svg:title", {}, [txt(encodeXmlText(image.altText))]));
|
|
161
|
+
return children;
|
|
162
|
+
}
|
|
163
|
+
function odfZIndexOf(paintOrder) {
|
|
164
|
+
if (paintOrder === void 0 || !Number.isSafeInteger(paintOrder) || paintOrder < 0) return;
|
|
165
|
+
return paintOrder;
|
|
166
|
+
}
|
|
167
|
+
function writeDrawFrame(shape, listState, state, documentIndex) {
|
|
168
|
+
const zIndex = odfZIndexOf(shape.paintOrder) ?? documentIndex;
|
|
169
|
+
const attributes = {
|
|
170
|
+
"draw:z-index": String(zIndex),
|
|
171
|
+
...frameGeometryAttrs(shape.frame, shape.rotationDeg)
|
|
172
|
+
};
|
|
173
|
+
if (shape.name !== void 0) attributes["draw:name"] = encodeXmlText(shape.name);
|
|
174
|
+
attributes["draw:style-name"] = encodeXmlText(shapeGraphicStyleName(shape, state));
|
|
175
|
+
const content = planShapeContent(shape.blocks, listState);
|
|
176
|
+
const children = content.kind === "table" ? [writeOdfTable(content.table, state.registry, `DrawTable${state.nextTable++}`)] : content.kind === "image" ? writeShapeImage(content.image, state) : [writeShapeTextBox(content.paragraphs, state)];
|
|
177
|
+
return el("draw:frame", attributes, children);
|
|
178
|
+
}
|
|
179
|
+
function canonicalDrawShape(shape, documentIndex, listState) {
|
|
180
|
+
const content = planShapeContent(shape.blocks, listState);
|
|
181
|
+
const blocks = content.kind === "table" ? [canonicalTable(content.table)] : content.kind === "image" ? [{
|
|
182
|
+
...canonicalImage(content.image),
|
|
183
|
+
widthPt: shape.frame.widthPt,
|
|
184
|
+
heightPt: shape.frame.heightPt
|
|
185
|
+
}] : content.paragraphs.map((paragraph) => canonicalParagraph(paragraph, paragraph.list?.numId));
|
|
186
|
+
const canonical = {
|
|
187
|
+
frame: shape.frame,
|
|
188
|
+
insetLeftPt: shape.insetLeftPt,
|
|
189
|
+
insetTopPt: shape.insetTopPt,
|
|
190
|
+
insetRightPt: shape.insetRightPt,
|
|
191
|
+
insetBottomPt: shape.insetBottomPt,
|
|
192
|
+
paintOrder: odfZIndexOf(shape.paintOrder) ?? documentIndex,
|
|
193
|
+
blocks
|
|
194
|
+
};
|
|
195
|
+
if (shape.name !== void 0) canonical.name = shape.name;
|
|
196
|
+
if (shape.rotationDeg !== void 0 && shape.rotationDeg !== 0) canonical.rotationDeg = shape.rotationDeg;
|
|
197
|
+
return canonical;
|
|
198
|
+
}
|
|
199
|
+
function writeDrawShapes(shapes, listState, state) {
|
|
200
|
+
return shapes.map((shape, index) => writeDrawFrame(shape, listState, state, index));
|
|
201
|
+
}
|
|
202
|
+
//#endregion
|
|
203
|
+
export { canonicalDrawShape, createDrawShapeWriteState, frameGeometryAttrs, odfZIndexOf, planShapeContent, writeDrawFrame, writeDrawShapes };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_xml_fragment = require("../../xml/fragment.cjs");
|
|
3
|
+
const require_xml_entities = require("../../xml/entities.cjs");
|
|
4
|
+
const require_typed_shared_units = require("../shared/units.cjs");
|
|
5
|
+
const require_typed_shared_color = require("../shared/color.cjs");
|
|
6
|
+
const require_typed_shared_path = require("../shared/path.cjs");
|
|
7
|
+
const require_typed_shared_canonicalise = require("../shared/canonicalise.cjs");
|
|
8
|
+
const require_typed_draw_write_shapes = require("./write-shapes.cjs");
|
|
9
|
+
//#region src/typed/draw/write-vectors.ts
|
|
10
|
+
function unsupportedVector(what) {
|
|
11
|
+
return /* @__PURE__ */ new Error(`writeDrawVector: a vector carries ${what}, which ODF has no spelling for -- refusing rather than producing a document that silently lost it or renders differently. See ExaDev/documents.js for the tracked follow-up.`);
|
|
12
|
+
}
|
|
13
|
+
function odfStrokeMode(style) {
|
|
14
|
+
if (style === void 0 || style === "solid") return "solid";
|
|
15
|
+
if (style === "dashed") return "dash";
|
|
16
|
+
throw unsupportedVector(`a '${style}' stroke style (draw:stroke is enumerated to exactly none/solid/dash -- ODF's vector-stroke model has no dotted or double spelling)`);
|
|
17
|
+
}
|
|
18
|
+
function vectorGraphicStyleName(paint, state) {
|
|
19
|
+
const properties = {};
|
|
20
|
+
if (paint.fill === void 0) properties["draw:fill"] = "none";
|
|
21
|
+
else {
|
|
22
|
+
properties["draw:fill"] = "solid";
|
|
23
|
+
properties["draw:fill-color"] = require_typed_shared_color.formatOdfColor(paint.fill);
|
|
24
|
+
}
|
|
25
|
+
if (paint.fillRule !== void 0) properties["svg:fill-rule"] = paint.fillRule;
|
|
26
|
+
if (paint.stroke === void 0) properties["draw:stroke"] = "none";
|
|
27
|
+
else {
|
|
28
|
+
if (!(paint.stroke.widthPt > 0)) throw unsupportedVector(`a stroke of width ${paint.stroke.widthPt}pt (svg:stroke-width must be positive for a stroke to exist at all)`);
|
|
29
|
+
properties["draw:stroke"] = odfStrokeMode(paint.stroke.style);
|
|
30
|
+
properties["svg:stroke-color"] = require_typed_shared_color.formatOdfColor(paint.stroke.color);
|
|
31
|
+
properties["svg:stroke-width"] = require_typed_shared_units.formatOdfLength(paint.stroke.widthPt);
|
|
32
|
+
}
|
|
33
|
+
return state.registry.intern({
|
|
34
|
+
properties: {},
|
|
35
|
+
family: "graphic",
|
|
36
|
+
propertyElements: [require_xml_fragment.el("style:graphic-properties", properties)]
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function zIndexAttrs(paintOrder, documentIndex) {
|
|
40
|
+
const zIndex = require_typed_draw_write_shapes.odfZIndexOf(paintOrder) ?? documentIndex;
|
|
41
|
+
return { "draw:z-index": String(zIndex) };
|
|
42
|
+
}
|
|
43
|
+
function writeDrawVector(vector, state, documentIndex) {
|
|
44
|
+
if (vector.kind === "line") {
|
|
45
|
+
const styleName = vectorGraphicStyleName({
|
|
46
|
+
fill: void 0,
|
|
47
|
+
fillRule: void 0,
|
|
48
|
+
stroke: vector.stroke
|
|
49
|
+
}, state);
|
|
50
|
+
return require_xml_fragment.el("draw:line", {
|
|
51
|
+
...zIndexAttrs(vector.paintOrder, documentIndex),
|
|
52
|
+
"svg:x1": require_typed_shared_units.formatOdfLength(vector.from.xPt),
|
|
53
|
+
"svg:y1": require_typed_shared_units.formatOdfLength(vector.from.yPt),
|
|
54
|
+
"svg:x2": require_typed_shared_units.formatOdfLength(vector.to.xPt),
|
|
55
|
+
"svg:y2": require_typed_shared_units.formatOdfLength(vector.to.yPt),
|
|
56
|
+
"draw:style-name": require_xml_entities.encodeXmlText(styleName)
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (vector.kind === "path") {
|
|
60
|
+
if (vector.subpaths.length === 0) throw unsupportedVector("no subpaths at all (an empty svg:d reads back as an element with no resolvable geometry, so the whole vector would vanish)");
|
|
61
|
+
if (!(vector.frame.widthPt > 0) || !(vector.frame.heightPt > 0)) throw unsupportedVector(`a frame of ${vector.frame.widthPt}pt x ${vector.frame.heightPt}pt (a path's svg:viewBox states the extent its own coordinates are scaled against, which cannot be zero or negative)`);
|
|
62
|
+
const styleName = vectorGraphicStyleName({
|
|
63
|
+
fill: vector.fill,
|
|
64
|
+
fillRule: vector.fillRule,
|
|
65
|
+
stroke: vector.stroke
|
|
66
|
+
}, state);
|
|
67
|
+
return require_xml_fragment.el("draw:path", {
|
|
68
|
+
...zIndexAttrs(vector.paintOrder, documentIndex),
|
|
69
|
+
...require_typed_draw_write_shapes.frameGeometryAttrs(vector.frame, vector.rotationDeg),
|
|
70
|
+
"svg:viewBox": require_typed_shared_path.formatOdfViewBox(vector.frame),
|
|
71
|
+
"svg:d": require_typed_shared_path.formatOdfPathData(vector.subpaths),
|
|
72
|
+
"draw:style-name": require_xml_entities.encodeXmlText(styleName)
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
const styleName = vectorGraphicStyleName({
|
|
76
|
+
fill: vector.fill,
|
|
77
|
+
fillRule: void 0,
|
|
78
|
+
stroke: vector.stroke
|
|
79
|
+
}, state);
|
|
80
|
+
return require_xml_fragment.el(vector.kind === "rect" ? "draw:rect" : "draw:ellipse", {
|
|
81
|
+
...zIndexAttrs(vector.paintOrder, documentIndex),
|
|
82
|
+
...require_typed_draw_write_shapes.frameGeometryAttrs(vector.frame, vector.rotationDeg),
|
|
83
|
+
"draw:style-name": require_xml_entities.encodeXmlText(styleName)
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function writeDrawVectors(vectors, state, baseIndex) {
|
|
87
|
+
return vectors.map((vector, index) => writeDrawVector(vector, state, baseIndex + index));
|
|
88
|
+
}
|
|
89
|
+
function canonicalDrawVector(vector, documentIndex) {
|
|
90
|
+
const paintOrder = require_typed_draw_write_shapes.odfZIndexOf(vector.paintOrder) ?? documentIndex;
|
|
91
|
+
if (vector.kind === "line") return {
|
|
92
|
+
kind: "line",
|
|
93
|
+
from: vector.from,
|
|
94
|
+
to: vector.to,
|
|
95
|
+
stroke: canonicalStroke(vector.stroke),
|
|
96
|
+
paintOrder
|
|
97
|
+
};
|
|
98
|
+
const paint = {
|
|
99
|
+
...vector.rotationDeg !== void 0 && vector.rotationDeg !== 0 ? { rotationDeg: vector.rotationDeg } : {},
|
|
100
|
+
...vector.fill !== void 0 ? { fill: require_typed_shared_canonicalise.canonicalColor(vector.fill) } : {},
|
|
101
|
+
...vector.stroke !== void 0 ? { stroke: canonicalStroke(vector.stroke) } : {}
|
|
102
|
+
};
|
|
103
|
+
if (vector.kind === "path") return {
|
|
104
|
+
kind: "path",
|
|
105
|
+
frame: vector.frame,
|
|
106
|
+
subpaths: vector.subpaths.map((subpath) => ({
|
|
107
|
+
start: subpath.start,
|
|
108
|
+
segments: [...subpath.segments],
|
|
109
|
+
closed: subpath.closed
|
|
110
|
+
})),
|
|
111
|
+
...vector.fillRule !== void 0 ? { fillRule: vector.fillRule } : {},
|
|
112
|
+
...paint,
|
|
113
|
+
paintOrder
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
kind: vector.kind,
|
|
117
|
+
frame: vector.frame,
|
|
118
|
+
...paint,
|
|
119
|
+
paintOrder
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function canonicalStroke(stroke) {
|
|
123
|
+
return {
|
|
124
|
+
color: require_typed_shared_canonicalise.canonicalColor(stroke.color),
|
|
125
|
+
widthPt: stroke.widthPt,
|
|
126
|
+
style: stroke.style ?? "solid"
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
exports.canonicalDrawVector = canonicalDrawVector;
|
|
131
|
+
exports.writeDrawVector = writeDrawVector;
|
|
132
|
+
exports.writeDrawVectors = writeDrawVectors;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { el } from "../../xml/fragment.js";
|
|
2
|
+
import { encodeXmlText } from "../../xml/entities.js";
|
|
3
|
+
import { formatOdfLength } from "../shared/units.js";
|
|
4
|
+
import { formatOdfColor } from "../shared/color.js";
|
|
5
|
+
import { formatOdfPathData, formatOdfViewBox } from "../shared/path.js";
|
|
6
|
+
import { canonicalColor } from "../shared/canonicalise.js";
|
|
7
|
+
import { frameGeometryAttrs, odfZIndexOf } from "./write-shapes.js";
|
|
8
|
+
//#region src/typed/draw/write-vectors.ts
|
|
9
|
+
function unsupportedVector(what) {
|
|
10
|
+
return /* @__PURE__ */ new Error(`writeDrawVector: a vector carries ${what}, which ODF has no spelling for -- refusing rather than producing a document that silently lost it or renders differently. See ExaDev/documents.js for the tracked follow-up.`);
|
|
11
|
+
}
|
|
12
|
+
function odfStrokeMode(style) {
|
|
13
|
+
if (style === void 0 || style === "solid") return "solid";
|
|
14
|
+
if (style === "dashed") return "dash";
|
|
15
|
+
throw unsupportedVector(`a '${style}' stroke style (draw:stroke is enumerated to exactly none/solid/dash -- ODF's vector-stroke model has no dotted or double spelling)`);
|
|
16
|
+
}
|
|
17
|
+
function vectorGraphicStyleName(paint, state) {
|
|
18
|
+
const properties = {};
|
|
19
|
+
if (paint.fill === void 0) properties["draw:fill"] = "none";
|
|
20
|
+
else {
|
|
21
|
+
properties["draw:fill"] = "solid";
|
|
22
|
+
properties["draw:fill-color"] = formatOdfColor(paint.fill);
|
|
23
|
+
}
|
|
24
|
+
if (paint.fillRule !== void 0) properties["svg:fill-rule"] = paint.fillRule;
|
|
25
|
+
if (paint.stroke === void 0) properties["draw:stroke"] = "none";
|
|
26
|
+
else {
|
|
27
|
+
if (!(paint.stroke.widthPt > 0)) throw unsupportedVector(`a stroke of width ${paint.stroke.widthPt}pt (svg:stroke-width must be positive for a stroke to exist at all)`);
|
|
28
|
+
properties["draw:stroke"] = odfStrokeMode(paint.stroke.style);
|
|
29
|
+
properties["svg:stroke-color"] = formatOdfColor(paint.stroke.color);
|
|
30
|
+
properties["svg:stroke-width"] = formatOdfLength(paint.stroke.widthPt);
|
|
31
|
+
}
|
|
32
|
+
return state.registry.intern({
|
|
33
|
+
properties: {},
|
|
34
|
+
family: "graphic",
|
|
35
|
+
propertyElements: [el("style:graphic-properties", properties)]
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function zIndexAttrs(paintOrder, documentIndex) {
|
|
39
|
+
const zIndex = odfZIndexOf(paintOrder) ?? documentIndex;
|
|
40
|
+
return { "draw:z-index": String(zIndex) };
|
|
41
|
+
}
|
|
42
|
+
function writeDrawVector(vector, state, documentIndex) {
|
|
43
|
+
if (vector.kind === "line") {
|
|
44
|
+
const styleName = vectorGraphicStyleName({
|
|
45
|
+
fill: void 0,
|
|
46
|
+
fillRule: void 0,
|
|
47
|
+
stroke: vector.stroke
|
|
48
|
+
}, state);
|
|
49
|
+
return el("draw:line", {
|
|
50
|
+
...zIndexAttrs(vector.paintOrder, documentIndex),
|
|
51
|
+
"svg:x1": formatOdfLength(vector.from.xPt),
|
|
52
|
+
"svg:y1": formatOdfLength(vector.from.yPt),
|
|
53
|
+
"svg:x2": formatOdfLength(vector.to.xPt),
|
|
54
|
+
"svg:y2": formatOdfLength(vector.to.yPt),
|
|
55
|
+
"draw:style-name": encodeXmlText(styleName)
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
if (vector.kind === "path") {
|
|
59
|
+
if (vector.subpaths.length === 0) throw unsupportedVector("no subpaths at all (an empty svg:d reads back as an element with no resolvable geometry, so the whole vector would vanish)");
|
|
60
|
+
if (!(vector.frame.widthPt > 0) || !(vector.frame.heightPt > 0)) throw unsupportedVector(`a frame of ${vector.frame.widthPt}pt x ${vector.frame.heightPt}pt (a path's svg:viewBox states the extent its own coordinates are scaled against, which cannot be zero or negative)`);
|
|
61
|
+
const styleName = vectorGraphicStyleName({
|
|
62
|
+
fill: vector.fill,
|
|
63
|
+
fillRule: vector.fillRule,
|
|
64
|
+
stroke: vector.stroke
|
|
65
|
+
}, state);
|
|
66
|
+
return el("draw:path", {
|
|
67
|
+
...zIndexAttrs(vector.paintOrder, documentIndex),
|
|
68
|
+
...frameGeometryAttrs(vector.frame, vector.rotationDeg),
|
|
69
|
+
"svg:viewBox": formatOdfViewBox(vector.frame),
|
|
70
|
+
"svg:d": formatOdfPathData(vector.subpaths),
|
|
71
|
+
"draw:style-name": encodeXmlText(styleName)
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const styleName = vectorGraphicStyleName({
|
|
75
|
+
fill: vector.fill,
|
|
76
|
+
fillRule: void 0,
|
|
77
|
+
stroke: vector.stroke
|
|
78
|
+
}, state);
|
|
79
|
+
return el(vector.kind === "rect" ? "draw:rect" : "draw:ellipse", {
|
|
80
|
+
...zIndexAttrs(vector.paintOrder, documentIndex),
|
|
81
|
+
...frameGeometryAttrs(vector.frame, vector.rotationDeg),
|
|
82
|
+
"draw:style-name": encodeXmlText(styleName)
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
function writeDrawVectors(vectors, state, baseIndex) {
|
|
86
|
+
return vectors.map((vector, index) => writeDrawVector(vector, state, baseIndex + index));
|
|
87
|
+
}
|
|
88
|
+
function canonicalDrawVector(vector, documentIndex) {
|
|
89
|
+
const paintOrder = odfZIndexOf(vector.paintOrder) ?? documentIndex;
|
|
90
|
+
if (vector.kind === "line") return {
|
|
91
|
+
kind: "line",
|
|
92
|
+
from: vector.from,
|
|
93
|
+
to: vector.to,
|
|
94
|
+
stroke: canonicalStroke(vector.stroke),
|
|
95
|
+
paintOrder
|
|
96
|
+
};
|
|
97
|
+
const paint = {
|
|
98
|
+
...vector.rotationDeg !== void 0 && vector.rotationDeg !== 0 ? { rotationDeg: vector.rotationDeg } : {},
|
|
99
|
+
...vector.fill !== void 0 ? { fill: canonicalColor(vector.fill) } : {},
|
|
100
|
+
...vector.stroke !== void 0 ? { stroke: canonicalStroke(vector.stroke) } : {}
|
|
101
|
+
};
|
|
102
|
+
if (vector.kind === "path") return {
|
|
103
|
+
kind: "path",
|
|
104
|
+
frame: vector.frame,
|
|
105
|
+
subpaths: vector.subpaths.map((subpath) => ({
|
|
106
|
+
start: subpath.start,
|
|
107
|
+
segments: [...subpath.segments],
|
|
108
|
+
closed: subpath.closed
|
|
109
|
+
})),
|
|
110
|
+
...vector.fillRule !== void 0 ? { fillRule: vector.fillRule } : {},
|
|
111
|
+
...paint,
|
|
112
|
+
paintOrder
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
kind: vector.kind,
|
|
116
|
+
frame: vector.frame,
|
|
117
|
+
...paint,
|
|
118
|
+
paintOrder
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function canonicalStroke(stroke) {
|
|
122
|
+
return {
|
|
123
|
+
color: canonicalColor(stroke.color),
|
|
124
|
+
widthPt: stroke.widthPt,
|
|
125
|
+
style: stroke.style ?? "solid"
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
export { canonicalDrawVector, writeDrawVector, writeDrawVectors };
|
package/dist/typed/odb/form.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_xml_query = require("../../xml/query.cjs");
|
|
3
3
|
const require_typed_odb_subdocument = require("./subdocument.cjs");
|
|
4
|
-
const require_embedded = require("../../embedded-
|
|
4
|
+
const require_embedded = require("../../embedded-Bxyss_ck.cjs");
|
|
5
5
|
const require_typed_shared_forms = require("../shared/forms.cjs");
|
|
6
6
|
const require_typed_odb_read = require("./read.cjs");
|
|
7
7
|
//#region src/typed/odb/form.ts
|
package/dist/typed/odb/form.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { findChildElement, rootElement } from "../../xml/query.js";
|
|
2
2
|
import { subDocumentPackage } from "./subdocument.js";
|
|
3
|
-
import { a as readOdtContent } from "../../embedded-
|
|
3
|
+
import { a as readOdtContent } from "../../embedded-BnavfkP-.js";
|
|
4
4
|
import { readOdfFormDefinitions } from "../shared/forms.js";
|
|
5
5
|
import { resolveOdbComponent } from "./read.js";
|
|
6
6
|
//#region src/typed/odb/form.ts
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_media_type = require("../../media-type.cjs");
|
|
3
|
+
const require_xml_fragment = require("../../xml/fragment.cjs");
|
|
4
|
+
const require_xml_entities = require("../../xml/entities.cjs");
|
|
5
|
+
const require_manifest = require("../../manifest.cjs");
|
|
6
|
+
const require_typed_shared_units = require("../shared/units.cjs");
|
|
7
|
+
const require_styles_registry = require("../../styles/registry.cjs");
|
|
8
|
+
const require_typed_shared_metadata = require("../shared/metadata.cjs");
|
|
9
|
+
const require_package_io_scaffold = require("../../package-io/scaffold.cjs");
|
|
10
|
+
const require_typed_shared_canonicalise = require("../shared/canonicalise.cjs");
|
|
11
|
+
const require_typed_draw_write_shapes = require("../draw/write-shapes.cjs");
|
|
12
|
+
const require_typed_draw_write_vectors = require("../draw/write-vectors.cjs");
|
|
13
|
+
let document_schema_js = require("document-schema.js");
|
|
14
|
+
//#region src/typed/odg/write.ts
|
|
15
|
+
const CONTENT_PART = "content.xml";
|
|
16
|
+
const STYLES_PART = "styles.xml";
|
|
17
|
+
function sortByPaintOrder(items) {
|
|
18
|
+
return items.slice().sort((a, b) => a.paintOrder - b.paintOrder);
|
|
19
|
+
}
|
|
20
|
+
function canonicalPage(page, listState) {
|
|
21
|
+
const shapes = page.shapes.map((shape, index) => require_typed_draw_write_shapes.canonicalDrawShape(shape, index, listState));
|
|
22
|
+
const vectors = page.vectors.map((vector, index) => require_typed_draw_write_vectors.canonicalDrawVector(vector, page.shapes.length + index));
|
|
23
|
+
return {
|
|
24
|
+
size: page.size,
|
|
25
|
+
shapes: sortByPaintOrder(shapes),
|
|
26
|
+
vectors: sortByPaintOrder(vectors)
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function normaliseOdgContent(document) {
|
|
30
|
+
if (document.kind !== "drawing") throw new Error(`normaliseOdgContent: expected a 'drawing' document, got '${document.kind}'`);
|
|
31
|
+
const listState = { next: 1 };
|
|
32
|
+
return {
|
|
33
|
+
kind: "drawing",
|
|
34
|
+
metadata: require_typed_shared_canonicalise.canonicalMetadata(document.metadata),
|
|
35
|
+
pages: document.pages.map((page) => canonicalPage(page, listState))
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function drawPageLayoutElement(name, pageSize) {
|
|
39
|
+
return require_xml_fragment.el("style:page-layout", { "style:name": require_xml_entities.encodeXmlText(name) }, [require_xml_fragment.el("style:page-layout-properties", {
|
|
40
|
+
"fo:page-width": require_typed_shared_units.formatOdfLength(pageSize.widthPt),
|
|
41
|
+
"fo:page-height": require_typed_shared_units.formatOdfLength(pageSize.heightPt),
|
|
42
|
+
"style:print-orientation": pageSize.widthPt > pageSize.heightPt ? "landscape" : "portrait"
|
|
43
|
+
})]);
|
|
44
|
+
}
|
|
45
|
+
function writeOdgContent(document, options = {}) {
|
|
46
|
+
if (document.kind !== "drawing") throw new Error(`writeOdgContent: expected a 'drawing' document, got '${document.kind}' -- odf.js writes .odg from the drawing arm only`);
|
|
47
|
+
const version = options.version ?? "1.3";
|
|
48
|
+
const drawingElement = require_xml_fragment.el("office:drawing");
|
|
49
|
+
const pkg = require_package_io_scaffold.createOdfPackage(options.template ? require_media_type.ODF_MEDIA_TYPES.otg : require_media_type.ODF_MEDIA_TYPES.odg, drawingElement, version);
|
|
50
|
+
const registry = require_styles_registry.StyleRegistry.forPart(pkg, CONTENT_PART, { otherPart: {
|
|
51
|
+
pkg,
|
|
52
|
+
partPath: STYLES_PART
|
|
53
|
+
} });
|
|
54
|
+
const contentAutomaticStyles = require_package_io_scaffold.odfPartContainer(pkg, CONTENT_PART, "office:automatic-styles");
|
|
55
|
+
const stylesAutomaticStyles = require_package_io_scaffold.odfPartContainer(pkg, STYLES_PART, "office:automatic-styles");
|
|
56
|
+
const masterStyles = require_package_io_scaffold.odfPartContainer(pkg, STYLES_PART, "office:master-styles");
|
|
57
|
+
const shapeState = require_typed_draw_write_shapes.createDrawShapeWriteState(pkg, registry, contentAutomaticStyles);
|
|
58
|
+
const listState = { next: 1 };
|
|
59
|
+
document.pages.forEach((page, index) => {
|
|
60
|
+
const masterPageName = `MP${index + 1}`;
|
|
61
|
+
const pageLayoutName = `PM${index + 1}`;
|
|
62
|
+
stylesAutomaticStyles.children.push(drawPageLayoutElement(pageLayoutName, page.size));
|
|
63
|
+
masterStyles.children.push(require_xml_fragment.el("style:master-page", {
|
|
64
|
+
"style:name": require_xml_entities.encodeXmlText(masterPageName),
|
|
65
|
+
"style:page-layout-name": require_xml_entities.encodeXmlText(pageLayoutName)
|
|
66
|
+
}));
|
|
67
|
+
const children = [...require_typed_draw_write_shapes.writeDrawShapes(page.shapes, listState, shapeState), ...require_typed_draw_write_vectors.writeDrawVectors(page.vectors, shapeState, page.shapes.length)];
|
|
68
|
+
drawingElement.children.push(require_xml_fragment.el("draw:page", { "draw:master-page-name": require_xml_entities.encodeXmlText(masterPageName) }, children));
|
|
69
|
+
});
|
|
70
|
+
require_typed_shared_metadata.writeOdfMetadata(pkg, document.metadata, version);
|
|
71
|
+
require_manifest.syncManifest(pkg, { version });
|
|
72
|
+
return pkg;
|
|
73
|
+
}
|
|
74
|
+
function writeOdg(document, options = {}) {
|
|
75
|
+
return writeOdgContent((0, document_schema_js.flattenTree)(document), options);
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
exports.normaliseOdgContent = normaliseOdgContent;
|
|
79
|
+
exports.writeOdg = writeOdg;
|
|
80
|
+
exports.writeOdgContent = writeOdgContent;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { ODF_MEDIA_TYPES } from "../../media-type.js";
|
|
2
|
+
import { el } from "../../xml/fragment.js";
|
|
3
|
+
import { encodeXmlText } from "../../xml/entities.js";
|
|
4
|
+
import { syncManifest } from "../../manifest.js";
|
|
5
|
+
import { formatOdfLength } from "../shared/units.js";
|
|
6
|
+
import { StyleRegistry } from "../../styles/registry.js";
|
|
7
|
+
import { writeOdfMetadata } from "../shared/metadata.js";
|
|
8
|
+
import { createOdfPackage, odfPartContainer } from "../../package-io/scaffold.js";
|
|
9
|
+
import { canonicalMetadata } from "../shared/canonicalise.js";
|
|
10
|
+
import { canonicalDrawShape, createDrawShapeWriteState, writeDrawShapes } from "../draw/write-shapes.js";
|
|
11
|
+
import { canonicalDrawVector, writeDrawVectors } from "../draw/write-vectors.js";
|
|
12
|
+
import { flattenTree } from "document-schema.js";
|
|
13
|
+
//#region src/typed/odg/write.ts
|
|
14
|
+
const CONTENT_PART = "content.xml";
|
|
15
|
+
const STYLES_PART = "styles.xml";
|
|
16
|
+
function sortByPaintOrder(items) {
|
|
17
|
+
return items.slice().sort((a, b) => a.paintOrder - b.paintOrder);
|
|
18
|
+
}
|
|
19
|
+
function canonicalPage(page, listState) {
|
|
20
|
+
const shapes = page.shapes.map((shape, index) => canonicalDrawShape(shape, index, listState));
|
|
21
|
+
const vectors = page.vectors.map((vector, index) => canonicalDrawVector(vector, page.shapes.length + index));
|
|
22
|
+
return {
|
|
23
|
+
size: page.size,
|
|
24
|
+
shapes: sortByPaintOrder(shapes),
|
|
25
|
+
vectors: sortByPaintOrder(vectors)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function normaliseOdgContent(document) {
|
|
29
|
+
if (document.kind !== "drawing") throw new Error(`normaliseOdgContent: expected a 'drawing' document, got '${document.kind}'`);
|
|
30
|
+
const listState = { next: 1 };
|
|
31
|
+
return {
|
|
32
|
+
kind: "drawing",
|
|
33
|
+
metadata: canonicalMetadata(document.metadata),
|
|
34
|
+
pages: document.pages.map((page) => canonicalPage(page, listState))
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function drawPageLayoutElement(name, pageSize) {
|
|
38
|
+
return el("style:page-layout", { "style:name": encodeXmlText(name) }, [el("style:page-layout-properties", {
|
|
39
|
+
"fo:page-width": formatOdfLength(pageSize.widthPt),
|
|
40
|
+
"fo:page-height": formatOdfLength(pageSize.heightPt),
|
|
41
|
+
"style:print-orientation": pageSize.widthPt > pageSize.heightPt ? "landscape" : "portrait"
|
|
42
|
+
})]);
|
|
43
|
+
}
|
|
44
|
+
function writeOdgContent(document, options = {}) {
|
|
45
|
+
if (document.kind !== "drawing") throw new Error(`writeOdgContent: expected a 'drawing' document, got '${document.kind}' -- odf.js writes .odg from the drawing arm only`);
|
|
46
|
+
const version = options.version ?? "1.3";
|
|
47
|
+
const drawingElement = el("office:drawing");
|
|
48
|
+
const pkg = createOdfPackage(options.template ? ODF_MEDIA_TYPES.otg : ODF_MEDIA_TYPES.odg, drawingElement, version);
|
|
49
|
+
const registry = StyleRegistry.forPart(pkg, CONTENT_PART, { otherPart: {
|
|
50
|
+
pkg,
|
|
51
|
+
partPath: STYLES_PART
|
|
52
|
+
} });
|
|
53
|
+
const contentAutomaticStyles = odfPartContainer(pkg, CONTENT_PART, "office:automatic-styles");
|
|
54
|
+
const stylesAutomaticStyles = odfPartContainer(pkg, STYLES_PART, "office:automatic-styles");
|
|
55
|
+
const masterStyles = odfPartContainer(pkg, STYLES_PART, "office:master-styles");
|
|
56
|
+
const shapeState = createDrawShapeWriteState(pkg, registry, contentAutomaticStyles);
|
|
57
|
+
const listState = { next: 1 };
|
|
58
|
+
document.pages.forEach((page, index) => {
|
|
59
|
+
const masterPageName = `MP${index + 1}`;
|
|
60
|
+
const pageLayoutName = `PM${index + 1}`;
|
|
61
|
+
stylesAutomaticStyles.children.push(drawPageLayoutElement(pageLayoutName, page.size));
|
|
62
|
+
masterStyles.children.push(el("style:master-page", {
|
|
63
|
+
"style:name": encodeXmlText(masterPageName),
|
|
64
|
+
"style:page-layout-name": encodeXmlText(pageLayoutName)
|
|
65
|
+
}));
|
|
66
|
+
const children = [...writeDrawShapes(page.shapes, listState, shapeState), ...writeDrawVectors(page.vectors, shapeState, page.shapes.length)];
|
|
67
|
+
drawingElement.children.push(el("draw:page", { "draw:master-page-name": encodeXmlText(masterPageName) }, children));
|
|
68
|
+
});
|
|
69
|
+
writeOdfMetadata(pkg, document.metadata, version);
|
|
70
|
+
syncManifest(pkg, { version });
|
|
71
|
+
return pkg;
|
|
72
|
+
}
|
|
73
|
+
function writeOdg(document, options = {}) {
|
|
74
|
+
return writeOdgContent(flattenTree(document), options);
|
|
75
|
+
}
|
|
76
|
+
//#endregion
|
|
77
|
+
export { normaliseOdgContent, writeOdg, writeOdgContent };
|