odf.js 1.12.0 → 1.13.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.
Files changed (178) hide show
  1. package/README.md +39 -0
  2. package/dist/codec.cjs +28 -0
  3. package/dist/codec.d.cts +72 -0
  4. package/dist/codec.d.ts +72 -0
  5. package/dist/codec.js +24 -0
  6. package/dist/image/sniff.cjs +28 -0
  7. package/dist/image/sniff.d.cts +5 -0
  8. package/dist/image/sniff.d.ts +5 -0
  9. package/dist/image/sniff.js +27 -0
  10. package/dist/index.cjs +156 -3242
  11. package/dist/index.d.cts +42 -679
  12. package/dist/index.d.ts +42 -679
  13. package/dist/index.js +42 -3129
  14. package/dist/manifest.cjs +252 -0
  15. package/dist/manifest.d.cts +41 -0
  16. package/dist/manifest.d.ts +41 -0
  17. package/dist/manifest.js +242 -0
  18. package/dist/media-type.cjs +27 -0
  19. package/dist/media-type.d.cts +20 -0
  20. package/dist/media-type.d.ts +20 -0
  21. package/dist/media-type.js +25 -0
  22. package/dist/mimetype.cjs +19 -0
  23. package/dist/mimetype.d.cts +7 -0
  24. package/dist/mimetype.d.ts +7 -0
  25. package/dist/mimetype.js +16 -0
  26. package/dist/model/node.cjs +67 -0
  27. package/dist/model/node.d.cts +2 -0
  28. package/dist/model/node.d.ts +2 -0
  29. package/dist/model/node.js +58 -0
  30. package/dist/model/package.cjs +19 -0
  31. package/dist/model/package.d.cts +114 -0
  32. package/dist/model/package.d.ts +114 -0
  33. package/dist/model/package.js +15 -0
  34. package/dist/node-CkBWRjNR.d.cts +83 -0
  35. package/dist/node-CkBWRjNR.d.ts +83 -0
  36. package/dist/ns.cjs +37 -0
  37. package/dist/ns.d.cts +32 -0
  38. package/dist/ns.d.ts +32 -0
  39. package/dist/ns.js +35 -0
  40. package/dist/package-io/read.cjs +35 -0
  41. package/dist/package-io/read.d.cts +5 -0
  42. package/dist/package-io/read.d.ts +5 -0
  43. package/dist/package-io/read.js +34 -0
  44. package/dist/package-io/write.cjs +36 -0
  45. package/dist/package-io/write.d.cts +7 -0
  46. package/dist/package-io/write.d.ts +7 -0
  47. package/dist/package-io/write.js +33 -0
  48. package/dist/styles/properties.cjs +286 -0
  49. package/dist/styles/properties.d.cts +45 -0
  50. package/dist/styles/properties.d.ts +45 -0
  51. package/dist/styles/properties.js +277 -0
  52. package/dist/styles/registry.cjs +212 -0
  53. package/dist/styles/registry.d.cts +37 -0
  54. package/dist/styles/registry.d.ts +37 -0
  55. package/dist/styles/registry.js +209 -0
  56. package/dist/styles/serialize.cjs +23 -0
  57. package/dist/styles/serialize.d.cts +7 -0
  58. package/dist/styles/serialize.d.ts +7 -0
  59. package/dist/styles/serialize.js +21 -0
  60. package/dist/styles/span.cjs +114 -0
  61. package/dist/styles/span.d.cts +5 -0
  62. package/dist/styles/span.d.ts +5 -0
  63. package/dist/styles/span.js +113 -0
  64. package/dist/typed/draw/shapes.cjs +314 -0
  65. package/dist/typed/draw/shapes.d.cts +14 -0
  66. package/dist/typed/draw/shapes.d.ts +14 -0
  67. package/dist/typed/draw/shapes.js +311 -0
  68. package/dist/typed/formula/read.cjs +48 -0
  69. package/dist/typed/formula/read.d.cts +12 -0
  70. package/dist/typed/formula/read.d.ts +12 -0
  71. package/dist/typed/formula/read.js +47 -0
  72. package/dist/typed/odb/read.cjs +112 -0
  73. package/dist/typed/odb/read.d.cts +17 -0
  74. package/dist/typed/odb/read.d.ts +17 -0
  75. package/dist/typed/odb/read.js +111 -0
  76. package/dist/typed/odg/read.cjs +30 -0
  77. package/dist/typed/odg/read.d.cts +10 -0
  78. package/dist/typed/odg/read.d.ts +10 -0
  79. package/dist/typed/odg/read.js +29 -0
  80. package/dist/typed/odm/read.cjs +37 -0
  81. package/dist/typed/odm/read.d.cts +15 -0
  82. package/dist/typed/odm/read.d.ts +15 -0
  83. package/dist/typed/odm/read.js +36 -0
  84. package/dist/typed/odp/read.cjs +39 -0
  85. package/dist/typed/odp/read.d.cts +10 -0
  86. package/dist/typed/odp/read.d.ts +10 -0
  87. package/dist/typed/odp/read.js +38 -0
  88. package/dist/typed/ods/read.cjs +318 -0
  89. package/dist/typed/ods/read.d.cts +10 -0
  90. package/dist/typed/ods/read.d.ts +10 -0
  91. package/dist/typed/ods/read.js +317 -0
  92. package/dist/typed/odt/read.cjs +112 -0
  93. package/dist/typed/odt/read.d.cts +10 -0
  94. package/dist/typed/odt/read.d.ts +10 -0
  95. package/dist/typed/odt/read.js +111 -0
  96. package/dist/typed/shared/a1.cjs +68 -0
  97. package/dist/typed/shared/a1.d.cts +18 -0
  98. package/dist/typed/shared/a1.d.ts +18 -0
  99. package/dist/typed/shared/a1.js +63 -0
  100. package/dist/typed/shared/cascade.cjs +102 -0
  101. package/dist/typed/shared/cascade.d.cts +22 -0
  102. package/dist/typed/shared/cascade.d.ts +22 -0
  103. package/dist/typed/shared/cascade.js +99 -0
  104. package/dist/typed/shared/color.cjs +14 -0
  105. package/dist/typed/shared/color.d.cts +6 -0
  106. package/dist/typed/shared/color.d.ts +6 -0
  107. package/dist/typed/shared/color.js +12 -0
  108. package/dist/typed/shared/geometry.cjs +79 -0
  109. package/dist/typed/shared/geometry.d.cts +12 -0
  110. package/dist/typed/shared/geometry.d.ts +12 -0
  111. package/dist/typed/shared/geometry.js +75 -0
  112. package/dist/typed/shared/masterpage.cjs +39 -0
  113. package/dist/typed/shared/masterpage.d.cts +8 -0
  114. package/dist/typed/shared/masterpage.d.ts +8 -0
  115. package/dist/typed/shared/masterpage.js +37 -0
  116. package/dist/typed/shared/metadata.cjs +42 -0
  117. package/dist/typed/shared/metadata.d.cts +7 -0
  118. package/dist/typed/shared/metadata.d.ts +7 -0
  119. package/dist/typed/shared/metadata.js +40 -0
  120. package/dist/typed/shared/paragraph.cjs +57 -0
  121. package/dist/typed/shared/paragraph.d.cts +7 -0
  122. package/dist/typed/shared/paragraph.d.ts +7 -0
  123. package/dist/typed/shared/paragraph.js +56 -0
  124. package/dist/typed/shared/path.cjs +261 -0
  125. package/dist/typed/shared/path.d.cts +34 -0
  126. package/dist/typed/shared/path.d.ts +34 -0
  127. package/dist/typed/shared/path.js +255 -0
  128. package/dist/typed/shared/style.cjs +8 -0
  129. package/dist/typed/shared/style.d.cts +2 -0
  130. package/dist/typed/shared/style.d.ts +2 -0
  131. package/dist/typed/shared/style.js +2 -0
  132. package/dist/typed/shared/table.cjs +84 -0
  133. package/dist/typed/shared/table.d.cts +7 -0
  134. package/dist/typed/shared/table.d.ts +7 -0
  135. package/dist/typed/shared/table.js +83 -0
  136. package/dist/typed/shared/text.cjs +43 -0
  137. package/dist/typed/shared/text.d.cts +8 -0
  138. package/dist/typed/shared/text.d.ts +8 -0
  139. package/dist/typed/shared/text.js +39 -0
  140. package/dist/typed/shared/transform.cjs +112 -0
  141. package/dist/typed/shared/transform.d.cts +26 -0
  142. package/dist/typed/shared/transform.d.ts +26 -0
  143. package/dist/typed/shared/transform.js +107 -0
  144. package/dist/typed/shared/units.cjs +35 -0
  145. package/dist/typed/shared/units.d.cts +2 -0
  146. package/dist/typed/shared/units.d.ts +2 -0
  147. package/dist/typed/shared/units.js +33 -0
  148. package/dist/units-7pCiECuP.d.cts +6 -0
  149. package/dist/units-7pCiECuP.d.ts +6 -0
  150. package/dist/util/base64.cjs +48 -0
  151. package/dist/util/base64.d.cts +5 -0
  152. package/dist/util/base64.d.ts +5 -0
  153. package/dist/util/base64.js +46 -0
  154. package/dist/xml/build.cjs +46 -0
  155. package/dist/xml/build.d.cts +5 -0
  156. package/dist/xml/build.d.ts +5 -0
  157. package/dist/xml/build.js +46 -0
  158. package/dist/xml/entities.cjs +19 -0
  159. package/dist/xml/entities.d.cts +5 -0
  160. package/dist/xml/entities.d.ts +5 -0
  161. package/dist/xml/entities.js +17 -0
  162. package/dist/xml/fragment.cjs +22 -0
  163. package/dist/xml/fragment.d.cts +6 -0
  164. package/dist/xml/fragment.d.ts +6 -0
  165. package/dist/xml/fragment.js +20 -0
  166. package/dist/xml/parse.cjs +88 -0
  167. package/dist/xml/parse.d.cts +5 -0
  168. package/dist/xml/parse.d.ts +5 -0
  169. package/dist/xml/parse.js +88 -0
  170. package/dist/xml/query.cjs +33 -0
  171. package/dist/xml/query.d.cts +9 -0
  172. package/dist/xml/query.d.ts +9 -0
  173. package/dist/xml/query.js +28 -0
  174. package/dist/zip.cjs +15 -0
  175. package/dist/zip.d.cts +9 -0
  176. package/dist/zip.d.ts +9 -0
  177. package/dist/zip.js +13 -0
  178. package/package.json +9 -1
@@ -0,0 +1,317 @@
1
+ import { parseOdfLength } from "../shared/units.js";
2
+ import { attrValue, childrenWithTag, findChildElement, rootElement } from "../../xml/query.js";
3
+ import { TableCursor, parseCellReference } from "../shared/a1.js";
4
+ import { parseMargins, parsePageSize } from "../shared/geometry.js";
5
+ import { findStyleElement } from "../shared/cascade.js";
6
+ import { readOdfMetadata } from "../shared/metadata.js";
7
+ import { readOdfParagraph } from "../shared/paragraph.js";
8
+ import { resolvePageLayoutProperties } from "../shared/masterpage.js";
9
+ import { PAGE_SIZE_A4 } from "document-schema.js";
10
+ //#region src/typed/ods/read.ts
11
+ const CONTENT_PART = "content.xml";
12
+ function parseKnownOdfLength(value) {
13
+ const parsed = parseOdfLength(value);
14
+ if (parsed === void 0) throw new Error(`readOds: internal error -- "${value}" is not a valid ODF length literal`);
15
+ return parsed;
16
+ }
17
+ const DEFAULT_MARGIN_PT = parseKnownOdfLength("2cm");
18
+ const DEFAULT_MARGINS = {
19
+ topPt: DEFAULT_MARGIN_PT,
20
+ rightPt: DEFAULT_MARGIN_PT,
21
+ bottomPt: DEFAULT_MARGIN_PT,
22
+ leftPt: DEFAULT_MARGIN_PT
23
+ };
24
+ function readRepeatCount(element, attrName) {
25
+ const raw = attrValue(element, attrName);
26
+ if (raw === void 0) return 1;
27
+ const parsed = Number.parseInt(raw, 10);
28
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : 1;
29
+ }
30
+ function isHidden(element) {
31
+ return attrValue(element, "table:visibility") === "collapse";
32
+ }
33
+ function readColumnLayout(columnElement, pkg) {
34
+ const styleName = attrValue(columnElement, "table:style-name");
35
+ const styleElement = styleName === void 0 ? void 0 : findStyleElement(styleName, "table-column", pkg);
36
+ const properties = styleElement === void 0 ? void 0 : childrenWithTag(styleElement, "style:table-column-properties")[0];
37
+ const widthValue = properties === void 0 ? void 0 : attrValue(properties, "style:column-width");
38
+ return {
39
+ widthPt: widthValue === void 0 ? 0 : parseOdfLength(widthValue) ?? 0,
40
+ manualBreak: (properties === void 0 ? void 0 : attrValue(properties, "fo:break-before")) === "page"
41
+ };
42
+ }
43
+ function readRowLayout(rowElement, pkg) {
44
+ const styleName = attrValue(rowElement, "table:style-name");
45
+ const styleElement = styleName === void 0 ? void 0 : findStyleElement(styleName, "table-row", pkg);
46
+ const properties = styleElement === void 0 ? void 0 : childrenWithTag(styleElement, "style:table-row-properties")[0];
47
+ const heightValue = properties === void 0 ? void 0 : attrValue(properties, "style:row-height");
48
+ return {
49
+ heightPt: heightValue === void 0 ? 0 : parseOdfLength(heightValue) ?? 0,
50
+ manualBreak: (properties === void 0 ? void 0 : attrValue(properties, "fo:break-before")) === "page"
51
+ };
52
+ }
53
+ function readCellText(cellElement, pkg) {
54
+ const paragraphs = childrenWithTag(cellElement, "text:p");
55
+ const runs = [];
56
+ paragraphs.forEach((paragraph, index) => {
57
+ if (index > 0) runs.push({ text: "\n" });
58
+ runs.push(...readOdfParagraph(paragraph, pkg).runs);
59
+ });
60
+ return {
61
+ runs,
62
+ displayText: runs.map((run) => run.text).join("")
63
+ };
64
+ }
65
+ function readCellValue(cellElement, displayText) {
66
+ const valueType = attrValue(cellElement, "office:value-type");
67
+ const stringFallback = {
68
+ kind: "string",
69
+ value: displayText
70
+ };
71
+ switch (valueType) {
72
+ case "float": {
73
+ const value = parseRequiredNumber(attrValue(cellElement, "office:value"));
74
+ return value === void 0 ? stringFallback : {
75
+ kind: "number",
76
+ value
77
+ };
78
+ }
79
+ case "percentage": {
80
+ const value = parseRequiredNumber(attrValue(cellElement, "office:value"));
81
+ return value === void 0 ? stringFallback : {
82
+ kind: "percentage",
83
+ value
84
+ };
85
+ }
86
+ case "currency": {
87
+ const value = parseRequiredNumber(attrValue(cellElement, "office:value"));
88
+ if (value === void 0) return stringFallback;
89
+ const currency = attrValue(cellElement, "office:currency");
90
+ return currency === void 0 ? {
91
+ kind: "currency",
92
+ value
93
+ } : {
94
+ kind: "currency",
95
+ value,
96
+ currency
97
+ };
98
+ }
99
+ case "boolean": {
100
+ const raw = attrValue(cellElement, "office:boolean-value");
101
+ return raw === void 0 ? stringFallback : {
102
+ kind: "boolean",
103
+ value: raw === "true"
104
+ };
105
+ }
106
+ case "date": return {
107
+ kind: "date",
108
+ value: attrValue(cellElement, "office:date-value") ?? displayText
109
+ };
110
+ case "time": return {
111
+ kind: "time",
112
+ value: attrValue(cellElement, "office:time-value") ?? displayText
113
+ };
114
+ case "string": return {
115
+ kind: "string",
116
+ value: attrValue(cellElement, "office:string-value") ?? displayText
117
+ };
118
+ default: return { kind: "empty" };
119
+ }
120
+ }
121
+ function parseRequiredNumber(raw) {
122
+ if (raw === void 0) return;
123
+ const value = Number(raw);
124
+ return Number.isNaN(value) ? void 0 : value;
125
+ }
126
+ function parsePrintRanges(value) {
127
+ const first = value.split(" ").find((part) => part.length > 0);
128
+ if (first === void 0) return;
129
+ const separatorIndex = first.indexOf(":");
130
+ if (separatorIndex === -1) return;
131
+ const start = parseA1WithOptionalSheetPrefix(first.slice(0, separatorIndex));
132
+ const end = parseA1WithOptionalSheetPrefix(first.slice(separatorIndex + 1));
133
+ if (start === void 0 || end === void 0) return;
134
+ return {
135
+ startRow: start.row,
136
+ startColumn: start.column,
137
+ endRow: end.row,
138
+ endColumn: end.column
139
+ };
140
+ }
141
+ function parseA1WithOptionalSheetPrefix(cellPart) {
142
+ const dotIndex = cellPart.lastIndexOf(".");
143
+ const bareReference = dotIndex === -1 ? cellPart : cellPart.slice(dotIndex + 1);
144
+ return parseCellReference(bareReference);
145
+ }
146
+ function parseScalePercentage(value) {
147
+ const match = /^(\d+(?:\.\d+)?)%$/.exec(value);
148
+ if (match === null) return;
149
+ const numeric = match[1];
150
+ return numeric === void 0 ? void 0 : Number(numeric);
151
+ }
152
+ function parseNonNegativeInteger(value) {
153
+ if (value === void 0) return;
154
+ const parsed = Number.parseInt(value, 10);
155
+ return Number.isInteger(parsed) && parsed >= 0 ? parsed : void 0;
156
+ }
157
+ function readTable(tableElement, pkg) {
158
+ const columns = [];
159
+ const rows = [];
160
+ const cells = [];
161
+ const manualBreakRows = [];
162
+ const manualBreakColumns = [];
163
+ let repeatColumns;
164
+ let repeatRows;
165
+ let columnCursor = 0;
166
+ const cursor = new TableCursor();
167
+ function processColumn(columnElement) {
168
+ const { widthPt, manualBreak } = readColumnLayout(columnElement, pkg);
169
+ columns.push({
170
+ index: columnCursor,
171
+ widthPt,
172
+ hidden: isHidden(columnElement) ? true : void 0
173
+ });
174
+ if (manualBreak) manualBreakColumns.push(columnCursor);
175
+ columnCursor += readRepeatCount(columnElement, "table:number-columns-repeated");
176
+ }
177
+ function processRowCells(rowElement) {
178
+ for (const child of rowElement.children) {
179
+ if (child.type !== "element") continue;
180
+ if (child.tag === "table:covered-table-cell") cursor.nextCell(readRepeatCount(child, "table:number-columns-repeated"));
181
+ else if (child.tag === "table:table-cell") {
182
+ const columnIndex = cursor.columnIndex;
183
+ const rowIndex = cursor.rowIndex;
184
+ cursor.nextCell(readRepeatCount(child, "table:number-columns-repeated"));
185
+ const formula = attrValue(child, "table:formula");
186
+ const { runs, displayText } = readCellText(child, pkg);
187
+ if (!(attrValue(child, "office:value-type") !== void 0) && formula === void 0 && displayText.length === 0) continue;
188
+ const value = readCellValue(child, displayText);
189
+ const colSpan = parseNonNegativeInteger(attrValue(child, "table:number-columns-spanned"));
190
+ const rowSpan = parseNonNegativeInteger(attrValue(child, "table:number-rows-spanned"));
191
+ const cell = {
192
+ row: rowIndex,
193
+ column: columnIndex,
194
+ value,
195
+ displayText
196
+ };
197
+ if (formula !== void 0) cell.formula = formula;
198
+ if (runs.length > 0) cell.runs = runs;
199
+ if (colSpan !== void 0) cell.colSpan = colSpan;
200
+ if (rowSpan !== void 0) cell.rowSpan = rowSpan;
201
+ cells.push(cell);
202
+ }
203
+ }
204
+ }
205
+ function processRow(rowElement) {
206
+ const startIndex = cursor.rowIndex;
207
+ processRowCells(rowElement);
208
+ const { heightPt, manualBreak } = readRowLayout(rowElement, pkg);
209
+ rows.push({
210
+ index: startIndex,
211
+ heightPt,
212
+ hidden: isHidden(rowElement) ? true : void 0
213
+ });
214
+ if (manualBreak) manualBreakRows.push(startIndex);
215
+ cursor.nextRow(readRepeatCount(rowElement, "table:number-rows-repeated"));
216
+ }
217
+ for (const child of tableElement.children) {
218
+ if (child.type !== "element") continue;
219
+ if (child.tag === "table:table-column") processColumn(child);
220
+ else if (child.tag === "table:table-header-columns") {
221
+ const startIndex = columnCursor;
222
+ for (const headerChild of child.children) if (headerChild.type === "element" && headerChild.tag === "table:table-column") processColumn(headerChild);
223
+ if (columnCursor > startIndex) repeatColumns = {
224
+ start: startIndex,
225
+ end: columnCursor - 1
226
+ };
227
+ } else if (child.tag === "table:table-row") processRow(child);
228
+ else if (child.tag === "table:table-header-rows") {
229
+ const startIndex = cursor.rowIndex;
230
+ for (const headerChild of child.children) if (headerChild.type === "element" && headerChild.tag === "table:table-row") processRow(headerChild);
231
+ if (cursor.rowIndex > startIndex) repeatRows = {
232
+ start: startIndex,
233
+ end: cursor.rowIndex - 1
234
+ };
235
+ }
236
+ }
237
+ return {
238
+ columns,
239
+ rows,
240
+ cells,
241
+ repeatColumns,
242
+ repeatRows,
243
+ manualBreakRows,
244
+ manualBreakColumns
245
+ };
246
+ }
247
+ function readPrintSettings(tableElement, pkg, repeatColumns, repeatRows, manualBreakRows, manualBreakColumns) {
248
+ const tableStyleName = attrValue(tableElement, "table:style-name");
249
+ const tableStyleElement = tableStyleName === void 0 ? void 0 : findStyleElement(tableStyleName, "table", pkg);
250
+ const masterPageName = tableStyleElement === void 0 ? void 0 : attrValue(tableStyleElement, "style:master-page-name");
251
+ const layoutProperties = resolvePageLayoutProperties(pkg, masterPageName);
252
+ const pageSize = layoutProperties === void 0 ? void 0 : parsePageSize(layoutProperties);
253
+ const margins = layoutProperties === void 0 ? void 0 : parseMargins(layoutProperties);
254
+ const printTokens = new Set((layoutProperties === void 0 ? void 0 : attrValue(layoutProperties, "style:print"))?.split(" ").filter((token) => token.length > 0));
255
+ const pageOrder = (layoutProperties === void 0 ? void 0 : attrValue(layoutProperties, "style:print-page-order")) === "ltr" ? "overThenDown" : "downThenOver";
256
+ const scaleToRaw = layoutProperties === void 0 ? void 0 : attrValue(layoutProperties, "style:scale-to");
257
+ const scale = scaleToRaw === void 0 ? void 0 : parseScalePercentage(scaleToRaw);
258
+ const scaleToXRaw = layoutProperties === void 0 ? void 0 : attrValue(layoutProperties, "style:scale-to-X");
259
+ const scaleToYRaw = layoutProperties === void 0 ? void 0 : attrValue(layoutProperties, "style:scale-to-Y");
260
+ const fitWidth = parseNonNegativeInteger(scaleToXRaw);
261
+ const fitHeight = parseNonNegativeInteger(scaleToYRaw);
262
+ const fitToPages = fitWidth === void 0 || fitHeight === void 0 ? void 0 : {
263
+ width: fitWidth,
264
+ height: fitHeight
265
+ };
266
+ const printRangesRaw = attrValue(tableElement, "table:print-ranges");
267
+ const printRange = printRangesRaw === void 0 ? void 0 : parsePrintRanges(printRangesRaw);
268
+ const manualBreaks = manualBreakRows.length > 0 || manualBreakColumns.length > 0 ? {
269
+ rows: manualBreakRows,
270
+ columns: manualBreakColumns
271
+ } : void 0;
272
+ const settings = {
273
+ pageSize: pageSize ?? PAGE_SIZE_A4,
274
+ margins: margins ?? DEFAULT_MARGINS,
275
+ gridlines: printTokens.has("grid"),
276
+ headers: printTokens.has("headers"),
277
+ pageOrder
278
+ };
279
+ if (printRange !== void 0) settings.printRange = printRange;
280
+ if (scale !== void 0) settings.scale = scale;
281
+ if (fitToPages !== void 0) settings.fitToPages = fitToPages;
282
+ if (repeatRows !== void 0) settings.repeatRows = repeatRows;
283
+ if (repeatColumns !== void 0) settings.repeatColumns = repeatColumns;
284
+ if (manualBreaks !== void 0) settings.manualBreaks = manualBreaks;
285
+ return settings;
286
+ }
287
+ function readSheet(tableElement, pkg) {
288
+ const name = attrValue(tableElement, "table:name");
289
+ if (name === void 0) return;
290
+ const { columns, rows, cells, repeatColumns, repeatRows, manualBreakRows, manualBreakColumns } = readTable(tableElement, pkg);
291
+ return {
292
+ name,
293
+ cells,
294
+ columns,
295
+ rows,
296
+ images: [],
297
+ printSettings: readPrintSettings(tableElement, pkg, repeatColumns, repeatRows, manualBreakRows, manualBreakColumns)
298
+ };
299
+ }
300
+ function readOds(pkg) {
301
+ const contentPart = pkg.parts[CONTENT_PART];
302
+ const root = contentPart?.kind === "xml" ? rootElement(contentPart.nodes) : void 0;
303
+ const body = root === void 0 ? void 0 : findChildElement(root.children, "office:body");
304
+ const spreadsheet = body === void 0 ? void 0 : findChildElement(body.children, "office:spreadsheet");
305
+ const tables = spreadsheet === void 0 ? [] : childrenWithTag(spreadsheet, "table:table");
306
+ const sheets = [];
307
+ for (const table of tables) {
308
+ const sheet = readSheet(table, pkg);
309
+ if (sheet !== void 0) sheets.push(sheet);
310
+ }
311
+ return {
312
+ metadata: readOdfMetadata(pkg),
313
+ sheets
314
+ };
315
+ }
316
+ //#endregion
317
+ export { readOds };
@@ -0,0 +1,112 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_typed_shared_units = require("../shared/units.cjs");
3
+ const require_xml_query = require("../../xml/query.cjs");
4
+ const require_typed_shared_geometry = require("../shared/geometry.cjs");
5
+ const require_typed_shared_metadata = require("../shared/metadata.cjs");
6
+ const require_typed_shared_paragraph = require("../shared/paragraph.cjs");
7
+ const require_typed_shared_table = require("../shared/table.cjs");
8
+ let document_schema_js = require("document-schema.js");
9
+ //#region src/typed/odt/read.ts
10
+ const CONTENT_PART = "content.xml";
11
+ const STYLES_PART = "styles.xml";
12
+ const AUTOMATIC_STYLE_PARTS = [CONTENT_PART, STYLES_PART];
13
+ function readOutlineLevel(headingElement) {
14
+ const raw = require_xml_query.attrValue(headingElement, "text:outline-level");
15
+ if (raw === void 0) return 1;
16
+ const parsed = Number.parseInt(raw, 10);
17
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : 1;
18
+ }
19
+ function readParagraphOrHeading(element, pkg, list) {
20
+ const paragraph = require_typed_shared_paragraph.readOdfParagraph(element, pkg);
21
+ if (element.tag === "text:h") paragraph.styleId = `Heading${readOutlineLevel(element)}`;
22
+ if (list !== void 0) paragraph.list = list;
23
+ return paragraph;
24
+ }
25
+ function readListItems(listElement, context, pkg, blocks) {
26
+ for (const item of listElement.children) {
27
+ if (item.type !== "element" || item.tag !== "text:list-item") continue;
28
+ for (const itemChild of item.children) {
29
+ if (itemChild.type !== "element") continue;
30
+ if (itemChild.tag === "text:p" || itemChild.tag === "text:h") blocks.push(readParagraphOrHeading(itemChild, pkg, context));
31
+ else if (itemChild.tag === "text:list") readListItems(itemChild, {
32
+ numId: context.numId,
33
+ level: context.level + 1
34
+ }, pkg, blocks);
35
+ }
36
+ }
37
+ }
38
+ function readBlocks(nodes, pkg, listIdState) {
39
+ const blocks = [];
40
+ for (const node of nodes) {
41
+ if (node.type !== "element") continue;
42
+ if (node.tag === "text:p" || node.tag === "text:h") blocks.push(readParagraphOrHeading(node, pkg, void 0));
43
+ else if (node.tag === "text:list") {
44
+ const numId = `list${listIdState.next}`;
45
+ listIdState.next += 1;
46
+ readListItems(node, {
47
+ numId,
48
+ level: 0
49
+ }, pkg, blocks);
50
+ } else if (node.tag === "table:table") blocks.push(require_typed_shared_table.readOdfTable(node, pkg));
51
+ else if (node.tag === "text:section") blocks.push(...readBlocks(node.children, pkg, listIdState));
52
+ }
53
+ return blocks;
54
+ }
55
+ function parseKnownOdfLength(value) {
56
+ const parsed = require_typed_shared_units.parseOdfLength(value);
57
+ if (parsed === void 0) throw new Error(`readOdt: internal error -- "${value}" is not a valid ODF length literal`);
58
+ return parsed;
59
+ }
60
+ const DEFAULT_MARGIN_PT = parseKnownOdfLength("2cm");
61
+ const DEFAULT_MARGINS = {
62
+ topPt: DEFAULT_MARGIN_PT,
63
+ rightPt: DEFAULT_MARGIN_PT,
64
+ bottomPt: DEFAULT_MARGIN_PT,
65
+ leftPt: DEFAULT_MARGIN_PT
66
+ };
67
+ function findPageLayoutElement(pkg, pageLayoutName) {
68
+ if (pageLayoutName === void 0) return;
69
+ for (const partPath of AUTOMATIC_STYLE_PARTS) {
70
+ const part = pkg.parts[partPath];
71
+ if (part?.kind !== "xml") continue;
72
+ const root = require_xml_query.rootElement(part.nodes);
73
+ const automaticStyles = root === void 0 ? void 0 : require_xml_query.findChildElement(root.children, "office:automatic-styles");
74
+ if (automaticStyles === void 0) continue;
75
+ const found = require_xml_query.childrenWithTag(automaticStyles, "style:page-layout").find((element) => require_xml_query.attrValue(element, "style:name") === pageLayoutName);
76
+ if (found !== void 0) return found;
77
+ }
78
+ }
79
+ function readFirstMasterPageGeometry(pkg) {
80
+ const stylesPart = pkg.parts[STYLES_PART];
81
+ const stylesRoot = stylesPart?.kind === "xml" ? require_xml_query.rootElement(stylesPart.nodes) : void 0;
82
+ const masterStyles = stylesRoot === void 0 ? void 0 : require_xml_query.findChildElement(stylesRoot.children, "office:master-styles");
83
+ const masterPage = masterStyles === void 0 ? void 0 : require_xml_query.findChildElement(masterStyles.children, "style:master-page");
84
+ const layout = findPageLayoutElement(pkg, masterPage === void 0 ? void 0 : require_xml_query.attrValue(masterPage, "style:page-layout-name"));
85
+ const properties = layout === void 0 ? void 0 : require_xml_query.findChildElement(layout.children, "style:page-layout-properties");
86
+ const pageSize = properties === void 0 ? void 0 : require_typed_shared_geometry.parsePageSize(properties);
87
+ const margins = properties === void 0 ? void 0 : require_typed_shared_geometry.parseMargins(properties);
88
+ return {
89
+ pageSize: pageSize ?? document_schema_js.PAGE_SIZE_A4,
90
+ margins: margins ?? DEFAULT_MARGINS
91
+ };
92
+ }
93
+ function readOdt(pkg) {
94
+ const contentPart = pkg.parts[CONTENT_PART];
95
+ if (contentPart?.kind !== "xml") throw new Error(`readOdt: package has no ${CONTENT_PART} part`);
96
+ const contentRoot = require_xml_query.rootElement(contentPart.nodes);
97
+ const body = contentRoot === void 0 ? void 0 : require_xml_query.findChildElement(contentRoot.children, "office:body");
98
+ const textElement = body === void 0 ? void 0 : require_xml_query.findChildElement(body.children, "office:text");
99
+ if (textElement === void 0) throw new Error(`readOdt: ${CONTENT_PART} has no office:body/office:text element`);
100
+ const metadata = require_typed_shared_metadata.readOdfMetadata(pkg);
101
+ const { pageSize, margins } = readFirstMasterPageGeometry(pkg);
102
+ return {
103
+ metadata,
104
+ sections: [{
105
+ pageSize,
106
+ margins,
107
+ blocks: readBlocks(textElement.children, pkg, { next: 1 })
108
+ }]
109
+ };
110
+ }
111
+ //#endregion
112
+ exports.readOdt = readOdt;
@@ -0,0 +1,10 @@
1
+ import { Package } from "../../model/package.cjs";
2
+ import { ContentSection, LayoutMetadata } from "document-schema.js";
3
+ //#region src/typed/odt/read.d.ts
4
+ interface OdtDocument {
5
+ metadata: LayoutMetadata;
6
+ sections: ContentSection[];
7
+ }
8
+ declare function readOdt(pkg: Package): OdtDocument;
9
+ //#endregion
10
+ export { OdtDocument, readOdt };
@@ -0,0 +1,10 @@
1
+ import { Package } from "../../model/package.js";
2
+ import { ContentSection, LayoutMetadata } from "document-schema.js";
3
+ //#region src/typed/odt/read.d.ts
4
+ interface OdtDocument {
5
+ metadata: LayoutMetadata;
6
+ sections: ContentSection[];
7
+ }
8
+ declare function readOdt(pkg: Package): OdtDocument;
9
+ //#endregion
10
+ export { OdtDocument, readOdt };
@@ -0,0 +1,111 @@
1
+ import { parseOdfLength } from "../shared/units.js";
2
+ import { attrValue, childrenWithTag, findChildElement, rootElement } from "../../xml/query.js";
3
+ import { parseMargins, parsePageSize } from "../shared/geometry.js";
4
+ import { readOdfMetadata } from "../shared/metadata.js";
5
+ import { readOdfParagraph } from "../shared/paragraph.js";
6
+ import { readOdfTable } from "../shared/table.js";
7
+ import { PAGE_SIZE_A4 } from "document-schema.js";
8
+ //#region src/typed/odt/read.ts
9
+ const CONTENT_PART = "content.xml";
10
+ const STYLES_PART = "styles.xml";
11
+ const AUTOMATIC_STYLE_PARTS = [CONTENT_PART, STYLES_PART];
12
+ function readOutlineLevel(headingElement) {
13
+ const raw = attrValue(headingElement, "text:outline-level");
14
+ if (raw === void 0) return 1;
15
+ const parsed = Number.parseInt(raw, 10);
16
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : 1;
17
+ }
18
+ function readParagraphOrHeading(element, pkg, list) {
19
+ const paragraph = readOdfParagraph(element, pkg);
20
+ if (element.tag === "text:h") paragraph.styleId = `Heading${readOutlineLevel(element)}`;
21
+ if (list !== void 0) paragraph.list = list;
22
+ return paragraph;
23
+ }
24
+ function readListItems(listElement, context, pkg, blocks) {
25
+ for (const item of listElement.children) {
26
+ if (item.type !== "element" || item.tag !== "text:list-item") continue;
27
+ for (const itemChild of item.children) {
28
+ if (itemChild.type !== "element") continue;
29
+ if (itemChild.tag === "text:p" || itemChild.tag === "text:h") blocks.push(readParagraphOrHeading(itemChild, pkg, context));
30
+ else if (itemChild.tag === "text:list") readListItems(itemChild, {
31
+ numId: context.numId,
32
+ level: context.level + 1
33
+ }, pkg, blocks);
34
+ }
35
+ }
36
+ }
37
+ function readBlocks(nodes, pkg, listIdState) {
38
+ const blocks = [];
39
+ for (const node of nodes) {
40
+ if (node.type !== "element") continue;
41
+ if (node.tag === "text:p" || node.tag === "text:h") blocks.push(readParagraphOrHeading(node, pkg, void 0));
42
+ else if (node.tag === "text:list") {
43
+ const numId = `list${listIdState.next}`;
44
+ listIdState.next += 1;
45
+ readListItems(node, {
46
+ numId,
47
+ level: 0
48
+ }, pkg, blocks);
49
+ } else if (node.tag === "table:table") blocks.push(readOdfTable(node, pkg));
50
+ else if (node.tag === "text:section") blocks.push(...readBlocks(node.children, pkg, listIdState));
51
+ }
52
+ return blocks;
53
+ }
54
+ function parseKnownOdfLength(value) {
55
+ const parsed = parseOdfLength(value);
56
+ if (parsed === void 0) throw new Error(`readOdt: internal error -- "${value}" is not a valid ODF length literal`);
57
+ return parsed;
58
+ }
59
+ const DEFAULT_MARGIN_PT = parseKnownOdfLength("2cm");
60
+ const DEFAULT_MARGINS = {
61
+ topPt: DEFAULT_MARGIN_PT,
62
+ rightPt: DEFAULT_MARGIN_PT,
63
+ bottomPt: DEFAULT_MARGIN_PT,
64
+ leftPt: DEFAULT_MARGIN_PT
65
+ };
66
+ function findPageLayoutElement(pkg, pageLayoutName) {
67
+ if (pageLayoutName === void 0) return;
68
+ for (const partPath of AUTOMATIC_STYLE_PARTS) {
69
+ const part = pkg.parts[partPath];
70
+ if (part?.kind !== "xml") continue;
71
+ const root = rootElement(part.nodes);
72
+ const automaticStyles = root === void 0 ? void 0 : findChildElement(root.children, "office:automatic-styles");
73
+ if (automaticStyles === void 0) continue;
74
+ const found = childrenWithTag(automaticStyles, "style:page-layout").find((element) => attrValue(element, "style:name") === pageLayoutName);
75
+ if (found !== void 0) return found;
76
+ }
77
+ }
78
+ function readFirstMasterPageGeometry(pkg) {
79
+ const stylesPart = pkg.parts[STYLES_PART];
80
+ const stylesRoot = stylesPart?.kind === "xml" ? rootElement(stylesPart.nodes) : void 0;
81
+ const masterStyles = stylesRoot === void 0 ? void 0 : findChildElement(stylesRoot.children, "office:master-styles");
82
+ const masterPage = masterStyles === void 0 ? void 0 : findChildElement(masterStyles.children, "style:master-page");
83
+ const layout = findPageLayoutElement(pkg, masterPage === void 0 ? void 0 : attrValue(masterPage, "style:page-layout-name"));
84
+ const properties = layout === void 0 ? void 0 : findChildElement(layout.children, "style:page-layout-properties");
85
+ const pageSize = properties === void 0 ? void 0 : parsePageSize(properties);
86
+ const margins = properties === void 0 ? void 0 : parseMargins(properties);
87
+ return {
88
+ pageSize: pageSize ?? PAGE_SIZE_A4,
89
+ margins: margins ?? DEFAULT_MARGINS
90
+ };
91
+ }
92
+ function readOdt(pkg) {
93
+ const contentPart = pkg.parts[CONTENT_PART];
94
+ if (contentPart?.kind !== "xml") throw new Error(`readOdt: package has no ${CONTENT_PART} part`);
95
+ const contentRoot = rootElement(contentPart.nodes);
96
+ const body = contentRoot === void 0 ? void 0 : findChildElement(contentRoot.children, "office:body");
97
+ const textElement = body === void 0 ? void 0 : findChildElement(body.children, "office:text");
98
+ if (textElement === void 0) throw new Error(`readOdt: ${CONTENT_PART} has no office:body/office:text element`);
99
+ const metadata = readOdfMetadata(pkg);
100
+ const { pageSize, margins } = readFirstMasterPageGeometry(pkg);
101
+ return {
102
+ metadata,
103
+ sections: [{
104
+ pageSize,
105
+ margins,
106
+ blocks: readBlocks(textElement.children, pkg, { next: 1 })
107
+ }]
108
+ };
109
+ }
110
+ //#endregion
111
+ export { readOdt };
@@ -0,0 +1,68 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/typed/shared/a1.ts
3
+ const ALPHABET_SIZE = 26;
4
+ const ALPHABET_START_CODE = "A".charCodeAt(0);
5
+ function columnIndexToLetters(index) {
6
+ if (!Number.isInteger(index) || index < 0) throw new Error(`columnIndexToLetters: index must be a non-negative integer, got ${index}`);
7
+ let remaining = index + 1;
8
+ let letters = "";
9
+ while (remaining > 0) {
10
+ const digit = (remaining - 1) % ALPHABET_SIZE;
11
+ letters = String.fromCharCode(ALPHABET_START_CODE + digit) + letters;
12
+ remaining = Math.floor((remaining - 1) / ALPHABET_SIZE);
13
+ }
14
+ return letters;
15
+ }
16
+ function cellReference(columnIndex, rowIndex) {
17
+ if (!Number.isInteger(rowIndex) || rowIndex < 0) throw new Error(`cellReference: rowIndex must be a non-negative integer, got ${rowIndex}`);
18
+ return `${columnIndexToLetters(columnIndex)}${rowIndex + 1}`;
19
+ }
20
+ function columnLettersToIndex(letters) {
21
+ if (!/^[A-Z]+$/.test(letters)) return;
22
+ let index = 0;
23
+ for (const char of letters) index = index * ALPHABET_SIZE + (char.charCodeAt(0) - ALPHABET_START_CODE + 1);
24
+ return index - 1;
25
+ }
26
+ function parseCellReference(reference) {
27
+ const match = /^([A-Z]+)(\d+)$/.exec(reference);
28
+ if (match === null) return;
29
+ const [, letters, digits] = match;
30
+ if (letters === void 0 || digits === void 0) return;
31
+ const column = columnLettersToIndex(letters);
32
+ const row = Number.parseInt(digits, 10) - 1;
33
+ if (column === void 0 || row < 0) return;
34
+ return {
35
+ column,
36
+ row
37
+ };
38
+ }
39
+ function validateRepeatCount(repeatCount, caller) {
40
+ if (!Number.isInteger(repeatCount) || repeatCount < 1) throw new Error(`${caller}: repeatCount must be a positive integer, got ${repeatCount}`);
41
+ }
42
+ var TableCursor = class {
43
+ columnCursor = 0;
44
+ rowCursor = 0;
45
+ get columnIndex() {
46
+ return this.columnCursor;
47
+ }
48
+ get rowIndex() {
49
+ return this.rowCursor;
50
+ }
51
+ nextCell(repeatCount = 1) {
52
+ validateRepeatCount(repeatCount, "TableCursor.nextCell");
53
+ const reference = cellReference(this.columnCursor, this.rowCursor);
54
+ this.columnCursor += repeatCount;
55
+ return reference;
56
+ }
57
+ nextRow(repeatCount = 1) {
58
+ validateRepeatCount(repeatCount, "TableCursor.nextRow");
59
+ this.rowCursor += repeatCount;
60
+ this.columnCursor = 0;
61
+ }
62
+ };
63
+ //#endregion
64
+ exports.TableCursor = TableCursor;
65
+ exports.cellReference = cellReference;
66
+ exports.columnIndexToLetters = columnIndexToLetters;
67
+ exports.columnLettersToIndex = columnLettersToIndex;
68
+ exports.parseCellReference = parseCellReference;
@@ -0,0 +1,18 @@
1
+ //#region src/typed/shared/a1.d.ts
2
+ declare function columnIndexToLetters(index: number): string;
3
+ declare function cellReference(columnIndex: number, rowIndex: number): string;
4
+ declare function columnLettersToIndex(letters: string): number | undefined;
5
+ declare function parseCellReference(reference: string): {
6
+ column: number;
7
+ row: number;
8
+ } | undefined;
9
+ declare class TableCursor {
10
+ private columnCursor;
11
+ private rowCursor;
12
+ get columnIndex(): number;
13
+ get rowIndex(): number;
14
+ nextCell(repeatCount?: number): string;
15
+ nextRow(repeatCount?: number): void;
16
+ }
17
+ //#endregion
18
+ export { TableCursor, cellReference, columnIndexToLetters, columnLettersToIndex, parseCellReference };
@@ -0,0 +1,18 @@
1
+ //#region src/typed/shared/a1.d.ts
2
+ declare function columnIndexToLetters(index: number): string;
3
+ declare function cellReference(columnIndex: number, rowIndex: number): string;
4
+ declare function columnLettersToIndex(letters: string): number | undefined;
5
+ declare function parseCellReference(reference: string): {
6
+ column: number;
7
+ row: number;
8
+ } | undefined;
9
+ declare class TableCursor {
10
+ private columnCursor;
11
+ private rowCursor;
12
+ get columnIndex(): number;
13
+ get rowIndex(): number;
14
+ nextCell(repeatCount?: number): string;
15
+ nextRow(repeatCount?: number): void;
16
+ }
17
+ //#endregion
18
+ export { TableCursor, cellReference, columnIndexToLetters, columnLettersToIndex, parseCellReference };