rtf-codec 0.0.0 → 1.1.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 (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +226 -0
  3. package/dist/base64.cjs +66 -0
  4. package/dist/base64.d.cts +7 -0
  5. package/dist/base64.d.ts +7 -0
  6. package/dist/base64.js +62 -0
  7. package/dist/bytes.cjs +25 -0
  8. package/dist/bytes.d.cts +6 -0
  9. package/dist/bytes.d.ts +6 -0
  10. package/dist/bytes.js +22 -0
  11. package/dist/cell-format.cjs +142 -0
  12. package/dist/cell-format.d.cts +25 -0
  13. package/dist/cell-format.d.ts +25 -0
  14. package/dist/cell-format.js +137 -0
  15. package/dist/codec.cjs +29 -0
  16. package/dist/codec.d.cts +2344 -0
  17. package/dist/codec.d.ts +2344 -0
  18. package/dist/codec.js +26 -0
  19. package/dist/codepage.cjs +98 -0
  20. package/dist/codepage.d.cts +10 -0
  21. package/dist/codepage.d.ts +10 -0
  22. package/dist/codepage.js +92 -0
  23. package/dist/constructs.cjs +131 -0
  24. package/dist/constructs.d.cts +31 -0
  25. package/dist/constructs.d.ts +31 -0
  26. package/dist/constructs.js +121 -0
  27. package/dist/diagnostics-BgG_KAiN.d.cts +52 -0
  28. package/dist/diagnostics-BgG_KAiN.d.ts +52 -0
  29. package/dist/diagnostics.cjs +76 -0
  30. package/dist/diagnostics.d.cts +2 -0
  31. package/dist/diagnostics.d.ts +2 -0
  32. package/dist/diagnostics.js +68 -0
  33. package/dist/group.cjs +40 -0
  34. package/dist/group.d.cts +11 -0
  35. package/dist/group.d.ts +11 -0
  36. package/dist/group.js +38 -0
  37. package/dist/header.cjs +433 -0
  38. package/dist/header.d.cts +44 -0
  39. package/dist/header.d.ts +44 -0
  40. package/dist/header.js +431 -0
  41. package/dist/index.cjs +28 -0
  42. package/dist/index.d.cts +8 -0
  43. package/dist/index.d.ts +8 -0
  44. package/dist/index.js +8 -0
  45. package/dist/list-id.cjs +30 -0
  46. package/dist/list-id.d.cts +16 -0
  47. package/dist/list-id.d.ts +16 -0
  48. package/dist/list-id.js +27 -0
  49. package/dist/options.cjs +7 -0
  50. package/dist/options.d.cts +18 -0
  51. package/dist/options.d.ts +18 -0
  52. package/dist/options.js +5 -0
  53. package/dist/read.cjs +1211 -0
  54. package/dist/read.d.cts +16 -0
  55. package/dist/read.d.ts +16 -0
  56. package/dist/read.js +1209 -0
  57. package/dist/tokenize.cjs +155 -0
  58. package/dist/tokenize.d.cts +25 -0
  59. package/dist/tokenize.d.ts +25 -0
  60. package/dist/tokenize.js +154 -0
  61. package/dist/units.cjs +43 -0
  62. package/dist/units.d.cts +17 -0
  63. package/dist/units.d.ts +17 -0
  64. package/dist/units.js +29 -0
  65. package/dist/write.cjs +532 -0
  66. package/dist/write.d.cts +7 -0
  67. package/dist/write.d.ts +7 -0
  68. package/dist/write.js +530 -0
  69. package/package.json +95 -2
package/dist/read.cjs ADDED
@@ -0,0 +1,1211 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_base64 = require("./base64.cjs");
3
+ const require_diagnostics = require("./diagnostics.cjs");
4
+ const require_bytes = require("./bytes.cjs");
5
+ const require_units = require("./units.cjs");
6
+ const require_cell_format = require("./cell-format.cjs");
7
+ const require_constructs = require("./constructs.cjs");
8
+ const require_codepage = require("./codepage.cjs");
9
+ const require_group = require("./group.cjs");
10
+ const require_header = require("./header.cjs");
11
+ const require_list_id = require("./list-id.cjs");
12
+ require("./options.cjs");
13
+ const require_tokenize = require("./tokenize.cjs");
14
+ let document_schema_js = require("document-schema.js");
15
+ //#region src/read.ts
16
+ const DESTINATION_KINDS = /* @__PURE__ */ new Map([
17
+ ["fldrslt", "body"],
18
+ ["ud", "body"],
19
+ ["shppict", "body"],
20
+ ["field", "body"],
21
+ ["pict", "picture"],
22
+ ["fldinst", "fieldInstruction"],
23
+ ["listtext", "listText"],
24
+ ["pntext", "listText"],
25
+ ["upr", "unicodeWrapper"],
26
+ ["footnote", "skip"],
27
+ ["header", "skip"],
28
+ ["headerl", "skip"],
29
+ ["headerr", "skip"],
30
+ ["headerf", "skip"],
31
+ ["footer", "skip"],
32
+ ["footerl", "skip"],
33
+ ["footerr", "skip"],
34
+ ["footerf", "skip"],
35
+ ["ftnsep", "skip"],
36
+ ["ftnsepc", "skip"],
37
+ ["ftncn", "skip"],
38
+ ["aftnsep", "skip"],
39
+ ["aftnsepc", "skip"],
40
+ ["aftncn", "skip"],
41
+ ["annotation", "skip"],
42
+ ["atnid", "skip"],
43
+ ["atnauthor", "skip"],
44
+ ["atnref", "skip"],
45
+ ["atndate", "skip"],
46
+ ["atnparent", "skip"],
47
+ ["atnicn", "skip"],
48
+ ["bkmkstart", "bookmarkStart"],
49
+ ["bkmkend", "bookmarkEnd"],
50
+ ["object", "skip"],
51
+ ["objdata", "skip"],
52
+ ["objclass", "skip"],
53
+ ["objname", "skip"],
54
+ ["do", "skip"],
55
+ ["shp", "skip"],
56
+ ["shptxt", "skip"],
57
+ ["shpinst", "skip"],
58
+ ["nonshppict", "skip"],
59
+ ["xe", "skip"],
60
+ ["tc", "skip"],
61
+ ["tcn", "skip"],
62
+ ["pn", "skip"],
63
+ ["pnseclvl", "skip"],
64
+ ["template", "skip"],
65
+ ["comment", "skip"],
66
+ ["falt", "skip"],
67
+ ["panose", "skip"],
68
+ ["fname", "skip"]
69
+ ]);
70
+ const SILENT_SKIP_DESTINATIONS = /* @__PURE__ */ new Set([
71
+ "pn",
72
+ "pnseclvl",
73
+ "nonshppict",
74
+ "falt",
75
+ "panose",
76
+ "fname",
77
+ "atnid",
78
+ "atnauthor",
79
+ "atnref",
80
+ "atndate",
81
+ "atnparent",
82
+ "atnicn",
83
+ "objclass",
84
+ "objname",
85
+ "objdata",
86
+ "shpinst",
87
+ "shptxt",
88
+ "ftnsep",
89
+ "ftnsepc",
90
+ "ftncn",
91
+ "aftnsep",
92
+ "aftnsepc",
93
+ "aftncn",
94
+ "template",
95
+ "comment",
96
+ "xe",
97
+ "tc",
98
+ "tcn"
99
+ ]);
100
+ const SPECIAL_CHARACTER_TEXT = /* @__PURE__ */ new Map([
101
+ ["tab", " "],
102
+ ["line", "\n"],
103
+ ["softline", "\n"],
104
+ ["emdash", "—"],
105
+ ["endash", "–"],
106
+ ["bullet", "•"],
107
+ ["lquote", "‘"],
108
+ ["rquote", "’"],
109
+ ["ldblquote", "“"],
110
+ ["rdblquote", "”"],
111
+ ["emspace", " "],
112
+ ["enspace", " "],
113
+ ["qmspace", " "],
114
+ ["zwj", "‍"],
115
+ ["zwnj", "‌"],
116
+ ["ltrmark", "‎"],
117
+ ["rtlmark", "‏"]
118
+ ]);
119
+ const SPECIAL_SYMBOL_TEXT = /* @__PURE__ */ new Map([
120
+ ["~", "\xA0"],
121
+ ["-", "­"],
122
+ ["_", "‑"],
123
+ ["\\", "\\"],
124
+ ["{", "{"],
125
+ ["}", "}"]
126
+ ]);
127
+ const ALIGNMENTS = /* @__PURE__ */ new Map([
128
+ ["ql", "left"],
129
+ ["qc", "center"],
130
+ ["qr", "right"],
131
+ ["qj", "justify"]
132
+ ]);
133
+ const PICTURE_FORMATS = /* @__PURE__ */ new Map([["pngblip", "png"], ["jpegblip", "jpeg"]]);
134
+ const SECTION_BREAK_TYPES = /* @__PURE__ */ new Map([
135
+ ["sbknone", "continuous"],
136
+ ["sbkpage", void 0],
137
+ ["sbkeven", "evenPage"],
138
+ ["sbkodd", "oddPage"]
139
+ ]);
140
+ function defaultCharacterState() {
141
+ return {
142
+ bold: false,
143
+ italic: false,
144
+ underline: false,
145
+ strike: false,
146
+ hidden: false,
147
+ fontIndex: void 0,
148
+ sizeHalfPoints: 24,
149
+ colorIndex: void 0,
150
+ revision: require_constructs.NO_REVISION
151
+ };
152
+ }
153
+ function defaultParagraphState() {
154
+ return {
155
+ alignment: void 0,
156
+ indentLeftTwips: 0,
157
+ indentFirstLineTwips: 0,
158
+ spaceBeforeTwips: 0,
159
+ spaceAfterTwips: 0,
160
+ lineSpacingTwips: void 0,
161
+ lineSpacingIsMultiple: false,
162
+ styleIndex: void 0,
163
+ outlineLevel: void 0,
164
+ listOverrideIndex: void 0,
165
+ listLevel: 0,
166
+ inTable: false,
167
+ pageBreakBefore: false
168
+ };
169
+ }
170
+ function cloneGroupState(state) {
171
+ return {
172
+ destination: state.destination,
173
+ uc: state.uc,
174
+ char: { ...state.char },
175
+ para: { ...state.para },
176
+ field: state.field,
177
+ picture: state.picture,
178
+ bookmark: state.bookmark,
179
+ inUnicodeWrapper: state.inUnicodeWrapper
180
+ };
181
+ }
182
+ function runKey(char, fontName, color, hyperlink) {
183
+ return [
184
+ char.bold ? "b" : "",
185
+ char.italic ? "i" : "",
186
+ char.underline ? "u" : "",
187
+ char.strike ? "s" : "",
188
+ fontName ?? "",
189
+ String(char.sizeHalfPoints),
190
+ color === void 0 ? "" : `${String(color.r)},${String(color.g)},${String(color.b)}`,
191
+ hyperlink ?? "",
192
+ JSON.stringify(char.revision)
193
+ ].join("|");
194
+ }
195
+ const HYPERLINK_TARGET = /HYPERLINK\s+"([^"]*)"/i;
196
+ const HYPERLINK_BARE_TARGET = /HYPERLINK\s+([^\s"\\]\S*)/i;
197
+ const HYPERLINK_ANCHOR = /\\l\s+"([^"]*)"/i;
198
+ function hyperlinkFromInstruction(instruction) {
199
+ const quoted = HYPERLINK_TARGET.exec(instruction);
200
+ const anchor = HYPERLINK_ANCHOR.exec(instruction);
201
+ const target = quoted?.[1] ?? HYPERLINK_BARE_TARGET.exec(instruction)?.[1];
202
+ if (target === void 0) return anchor?.[1] === void 0 ? void 0 : `#${anchor[1]}`;
203
+ return anchor?.[1] === void 0 ? target : `${target}#${anchor[1]}`;
204
+ }
205
+ function skipUnicodeFallback(tokens, from, count) {
206
+ let { index, textOffset } = from;
207
+ let remaining = count;
208
+ while (remaining > 0 && index < tokens.length) {
209
+ const token = tokens[index];
210
+ if (token === void 0) break;
211
+ if (token.kind === "groupStart" || token.kind === "groupEnd") break;
212
+ if (token.kind === "text") {
213
+ const available = token.bytes.length - textOffset;
214
+ const consumed = Math.min(available, remaining);
215
+ remaining -= consumed;
216
+ textOffset += consumed;
217
+ if (textOffset >= token.bytes.length) {
218
+ index += 1;
219
+ textOffset = 0;
220
+ }
221
+ continue;
222
+ }
223
+ remaining -= 1;
224
+ index += 1;
225
+ textOffset = 0;
226
+ }
227
+ return {
228
+ index,
229
+ textOffset
230
+ };
231
+ }
232
+ function insertConstructMarkers(blocks, extents) {
233
+ if (extents.length === 0) return [...blocks];
234
+ const ordered = [...extents].sort((left, right) => left.startIndex - right.startIndex || right.endIndex - left.endIndex);
235
+ const closing = [...ordered].reverse();
236
+ const out = [];
237
+ for (let index = 0; index <= blocks.length; index += 1) {
238
+ for (const extent of closing) if (extent.endIndex === index) out.push({ kind: "constructEnd" });
239
+ for (const extent of ordered) if (extent.startIndex === index) out.push({
240
+ kind: "constructStart",
241
+ descriptor: extent.descriptor
242
+ });
243
+ const block = blocks[index];
244
+ if (block !== void 0) out.push(block);
245
+ }
246
+ return out;
247
+ }
248
+ function horizontalSpanAt(definitions, index) {
249
+ if (definitions[index]?.horizontalMergeFirst !== true) return 1;
250
+ let span = 1;
251
+ while (definitions[index + span]?.horizontalMergeContinuation === true) span += 1;
252
+ return span;
253
+ }
254
+ var ContentBuilder = class {
255
+ header;
256
+ sink;
257
+ sections = [];
258
+ blocks = [];
259
+ runs = [];
260
+ pendingRunKey;
261
+ pendingRunText = "";
262
+ pendingRunFields = {};
263
+ runProvenance = [];
264
+ pendingRunProvenance = [];
265
+ tableRows = [];
266
+ tableColumnRights = [];
267
+ rowCells = [];
268
+ cellBlocks = [];
269
+ pendingCellRights = [];
270
+ pendingCellDefinitions = [];
271
+ pendingCell = require_cell_format.newPendingCell();
272
+ rowLeftTwips = 0;
273
+ paragraphSerial = 0;
274
+ openBookmarks = /* @__PURE__ */ new Map();
275
+ sectionBlockExtents = [];
276
+ cellBlockExtents = [];
277
+ pendingRunConstructs = [];
278
+ closingBookmarks = [];
279
+ constructor(header, sink) {
280
+ this.header = header;
281
+ this.sink = sink;
282
+ }
283
+ startBookmark(bookmark, para) {
284
+ this.flushRun();
285
+ const name = bookmark.name;
286
+ if (name.length === 0) return;
287
+ this.openBookmarks.set(name, {
288
+ descriptor: require_constructs.bookmarkAnchorDescriptor(name, bookmark.columnFirst === void 0 && bookmark.columnLast === void 0 ? void 0 : {
289
+ first: bookmark.columnFirst,
290
+ last: bookmark.columnLast
291
+ }),
292
+ paragraphSerial: this.paragraphSerial,
293
+ runIndex: this.runs.length,
294
+ inTable: para.inTable,
295
+ blockIndex: void 0
296
+ });
297
+ }
298
+ endBookmark(name) {
299
+ this.flushRun();
300
+ const open = this.openBookmarks.get(name);
301
+ if (open === void 0) {
302
+ this.sink({
303
+ code: require_diagnostics.RtfDiagnosticCodes.BOOKMARK_UNPAIRED,
304
+ severity: "warning",
305
+ message: `a \\bkmkend named '${name}' has no matching \\bkmkstart, so no anchor construct is produced for it`
306
+ });
307
+ return;
308
+ }
309
+ this.openBookmarks.delete(name);
310
+ if (open.paragraphSerial === this.paragraphSerial) {
311
+ this.pendingRunConstructs.push({
312
+ descriptor: open.descriptor,
313
+ startRun: open.runIndex,
314
+ endRun: this.runs.length
315
+ });
316
+ return;
317
+ }
318
+ this.closingBookmarks.push(open);
319
+ }
320
+ appendText(text, char, hyperlink) {
321
+ if (text.length === 0 || char.hidden) return;
322
+ const fontName = char.fontIndex === void 0 ? void 0 : this.header.fonts.get(char.fontIndex)?.name;
323
+ const color = char.colorIndex === void 0 ? void 0 : this.header.colors[char.colorIndex];
324
+ const key = runKey(char, fontName, color, hyperlink);
325
+ if (this.pendingRunKey !== key) {
326
+ this.flushRun();
327
+ this.pendingRunKey = key;
328
+ this.pendingRunFields = buildRunFields(char, fontName, color, hyperlink);
329
+ this.pendingRunProvenance = require_constructs.provenanceDescriptors(char.revision, this.header.revisionAuthors);
330
+ }
331
+ this.pendingRunText += text;
332
+ }
333
+ flushRun() {
334
+ if (this.pendingRunText.length > 0) {
335
+ this.runs.push({
336
+ ...this.pendingRunFields,
337
+ text: this.pendingRunText
338
+ });
339
+ this.runProvenance.push(this.pendingRunProvenance);
340
+ }
341
+ this.pendingRunText = "";
342
+ this.pendingRunKey = void 0;
343
+ this.pendingRunFields = {};
344
+ this.pendingRunProvenance = [];
345
+ }
346
+ endParagraph(para, force) {
347
+ this.flushRun();
348
+ if (!force && this.runs.length === 0) return;
349
+ const target = para.inTable ? this.cellBlocks : this.blocks;
350
+ if (!para.inTable) this.closeTable();
351
+ const blockIndex = target.length;
352
+ target.push(this.buildParagraph(para));
353
+ this.runs = [];
354
+ this.runProvenance = [];
355
+ this.resolveBookmarkPositions(para, blockIndex);
356
+ this.paragraphSerial += 1;
357
+ }
358
+ resolveBookmarkPositions(para, blockIndex) {
359
+ for (const open of this.openBookmarks.values()) if (open.blockIndex === void 0 && open.paragraphSerial === this.paragraphSerial) open.blockIndex = blockIndex;
360
+ this.flushClosingBookmarks(para.inTable, blockIndex + 1);
361
+ }
362
+ flushClosingBookmarks(inTable, endIndex) {
363
+ if (this.closingBookmarks.length === 0) return;
364
+ const target = inTable ? this.cellBlockExtents : this.sectionBlockExtents;
365
+ for (const closing of this.closingBookmarks) {
366
+ if (closing.inTable !== inTable) {
367
+ this.sink({
368
+ code: require_diagnostics.RtfDiagnosticCodes.BOOKMARK_UNPAIRED,
369
+ severity: "warning",
370
+ message: `the bookmark '${closing.descriptor.name}' spans a table cell boundary; a construct extent cannot straddle two block lists, so no anchor construct is produced for it`
371
+ });
372
+ continue;
373
+ }
374
+ target.push({
375
+ descriptor: closing.descriptor,
376
+ startIndex: closing.blockIndex ?? Math.max(0, endIndex - 1),
377
+ endIndex
378
+ });
379
+ }
380
+ this.closingBookmarks = [];
381
+ }
382
+ takeRunConstructs() {
383
+ const extents = [...this.pendingRunConstructs, ...require_constructs.coalesceRunConstructs(this.runProvenance)].filter((extent) => extent.endRun <= this.runs.length);
384
+ this.pendingRunConstructs = [];
385
+ this.runProvenance = [];
386
+ return extents.sort((left, right) => left.startRun - right.startRun || left.endRun - right.endRun);
387
+ }
388
+ buildParagraph(para) {
389
+ const style = para.styleIndex === void 0 ? void 0 : this.header.styles.get(para.styleIndex);
390
+ const headingLevel = para.outlineLevel === void 0 ? style?.headingLevel : para.outlineLevel + 1;
391
+ const constructs = this.takeRunConstructs();
392
+ const paragraph = {
393
+ kind: "paragraph",
394
+ runs: this.runs,
395
+ ...constructs.length === 0 ? {} : { constructs }
396
+ };
397
+ const withStyle = style?.name === void 0 || style.name.length === 0 ? paragraph : {
398
+ ...paragraph,
399
+ styleId: style.name
400
+ };
401
+ return {
402
+ ...headingLevel === void 0 ? withStyle : {
403
+ ...withStyle,
404
+ headingLevel
405
+ },
406
+ ...para.alignment === void 0 ? {} : { alignment: para.alignment },
407
+ ...para.indentLeftTwips === 0 ? {} : { indentLeftPt: require_units.twipsToPoints(para.indentLeftTwips) },
408
+ ...para.indentFirstLineTwips === 0 ? {} : { indentFirstLinePt: require_units.twipsToPoints(para.indentFirstLineTwips) },
409
+ ...para.spaceBeforeTwips === 0 ? {} : { spacingBeforePt: require_units.twipsToPoints(para.spaceBeforeTwips) },
410
+ ...para.spaceAfterTwips === 0 ? {} : { spacingAfterPt: require_units.twipsToPoints(para.spaceAfterTwips) },
411
+ ...this.lineSpacingFields(para),
412
+ ...para.pageBreakBefore ? { pageBreakBefore: true } : {},
413
+ ...this.listFields(para)
414
+ };
415
+ }
416
+ lineSpacingFields(para) {
417
+ const value = para.lineSpacingTwips;
418
+ if (value === void 0 || value === 0 || !para.lineSpacingIsMultiple) return {};
419
+ const multiple = Math.abs(value) / 240;
420
+ return multiple > 0 ? { lineSpacing: multiple } : {};
421
+ }
422
+ listFields(para) {
423
+ const overrideIndex = para.listOverrideIndex;
424
+ if (overrideIndex === void 0 || overrideIndex === 0) return {};
425
+ const list = this.header.lists.get(overrideIndex);
426
+ const level = list?.levels[para.listLevel] ?? list?.levels[0];
427
+ const type = level?.numberFormat === 23 ? "bullet" : "ordered";
428
+ return { list: {
429
+ numId: require_list_id.mintRtfListNumId({
430
+ listOverrideIndex: overrideIndex,
431
+ type,
432
+ ...level?.startAt === void 0 ? {} : { start: level.startAt }
433
+ }),
434
+ level: para.listLevel
435
+ } };
436
+ }
437
+ startRowDefinition() {
438
+ this.pendingCellRights = [];
439
+ this.pendingCellDefinitions = [];
440
+ this.pendingCell = require_cell_format.newPendingCell();
441
+ this.rowLeftTwips = 0;
442
+ }
443
+ setRowLeft(twips) {
444
+ this.rowLeftTwips = twips;
445
+ }
446
+ applyCellDefinition(name, param) {
447
+ return require_cell_format.applyCellDefinitionControlWord(name, param, this.pendingCell);
448
+ }
449
+ addCellBoundary(rightTwips) {
450
+ this.pendingCellRights.push(rightTwips);
451
+ this.pendingCellDefinitions.push(this.pendingCell);
452
+ this.pendingCell = require_cell_format.newPendingCell();
453
+ }
454
+ endCell(para) {
455
+ this.endParagraph(para, false);
456
+ this.flushClosingBookmarks(true, this.cellBlocks.length);
457
+ this.rowCells.push({ blocks: insertConstructMarkers(this.cellBlocks, this.cellBlockExtents) });
458
+ this.cellBlocks = [];
459
+ this.cellBlockExtents = [];
460
+ }
461
+ endRow(para) {
462
+ if (this.cellBlocks.length > 0 || this.pendingRunText.length > 0) this.endCell(para);
463
+ if (this.rowCells.length === 0) {
464
+ this.sink({
465
+ code: require_diagnostics.RtfDiagnosticCodes.TABLE_ROW_WITHOUT_DEFINITION,
466
+ severity: "warning",
467
+ message: "a \\row closed a table row that contained no \\cell marks"
468
+ });
469
+ return;
470
+ }
471
+ this.tableRows.push({
472
+ cells: this.rowCells,
473
+ definitions: this.pendingCellDefinitions
474
+ });
475
+ if (this.tableColumnRights.length === 0) this.tableColumnRights = [...this.pendingCellRights];
476
+ this.rowCells = [];
477
+ }
478
+ closeTable() {
479
+ if (this.tableRows.length === 0) return;
480
+ const rows = this.resolveRows();
481
+ const columnCount = Math.max(this.tableColumnRights.length, ...rows.map((row) => row.cells.reduce((total, cell) => total + (cell.colSpan ?? 1), 0)));
482
+ this.blocks.push({
483
+ kind: "table",
484
+ rows,
485
+ columnWidthsPt: this.columnWidths(columnCount)
486
+ });
487
+ this.tableRows = [];
488
+ this.tableColumnRights = [];
489
+ }
490
+ resolveRows() {
491
+ const rows = this.tableRows;
492
+ const columnIndices = rows.map((row) => {
493
+ const indices = [];
494
+ let column = 0;
495
+ for (const [index] of row.cells.entries()) {
496
+ indices.push(column);
497
+ column += horizontalSpanAt(row.definitions, index);
498
+ }
499
+ return indices;
500
+ });
501
+ return rows.map((row, rowIndex) => ({ cells: row.cells.map((cell, cellIndex) => ({
502
+ cell,
503
+ cellIndex
504
+ })).filter(({ cellIndex }) => row.definitions[cellIndex]?.horizontalMergeContinuation !== true).map(({ cell, cellIndex }) => {
505
+ const definition = row.definitions[cellIndex];
506
+ if (definition?.verticalMergeContinuation === true) return { blocks: [] };
507
+ const colSpan = horizontalSpanAt(row.definitions, cellIndex);
508
+ const column = columnIndices[rowIndex]?.[cellIndex];
509
+ let rowSpan = 1;
510
+ if (definition?.verticalMergeFirst === true && column !== void 0) for (let next = rowIndex + 1; next < rows.length; next += 1) {
511
+ const matchIndex = columnIndices[next]?.indexOf(column) ?? -1;
512
+ if ((matchIndex === -1 ? void 0 : rows[next]?.definitions[matchIndex])?.verticalMergeContinuation !== true) break;
513
+ rowSpan += 1;
514
+ }
515
+ const borders = this.resolveBorders(definition);
516
+ const background = definition?.backgroundIndex === void 0 ? void 0 : this.header.colors[definition.backgroundIndex];
517
+ return {
518
+ blocks: cell.blocks,
519
+ ...colSpan > 1 ? { colSpan } : {},
520
+ ...rowSpan > 1 ? { rowSpan } : {},
521
+ ...background === void 0 ? {} : { background },
522
+ ...borders === void 0 ? {} : { borders }
523
+ };
524
+ }) }));
525
+ }
526
+ resolveBorders(definition) {
527
+ if (definition === void 0) return;
528
+ const colorAt = (index) => this.header.colors[index];
529
+ const present = [
530
+ "top",
531
+ "left",
532
+ "bottom",
533
+ "right"
534
+ ].map((side) => {
535
+ const pending = definition.borders[side];
536
+ return [side, pending === void 0 ? void 0 : require_cell_format.resolveBorder(pending, colorAt)];
537
+ }).filter((entry) => entry[1] !== void 0);
538
+ return present.length === 0 ? void 0 : Object.fromEntries(present);
539
+ }
540
+ columnWidths(columnCount) {
541
+ const rights = this.tableColumnRights;
542
+ const widths = [];
543
+ let previous = this.rowLeftTwips;
544
+ for (const right of rights.slice(0, columnCount)) {
545
+ widths.push(require_units.twipsToPoints(right - previous));
546
+ previous = right;
547
+ }
548
+ const usable = require_units.twipsToPoints(this.header.page.paperWidthTwips - this.header.page.marginLeftTwips - this.header.page.marginRightTwips);
549
+ if (widths.length !== columnCount || widths.some((width) => width <= 0)) {
550
+ this.sink({
551
+ code: require_diagnostics.RtfDiagnosticCodes.TABLE_COLUMN_WIDTH_INVALID,
552
+ severity: "warning",
553
+ message: "the row's \\cellxN boundaries do not describe increasing column widths for every column; falling back to an even split of the page's text width"
554
+ });
555
+ return Array.from({ length: columnCount }, () => usable / columnCount);
556
+ }
557
+ return widths;
558
+ }
559
+ addBlock(block) {
560
+ this.flushRun();
561
+ this.blocks.push(block);
562
+ }
563
+ endSection(section, para) {
564
+ this.endParagraph(para, false);
565
+ this.closeTable();
566
+ this.flushClosingBookmarks(false, this.blocks.length);
567
+ this.reportUnclosedBookmarks();
568
+ const blocks = insertConstructMarkers(this.blocks, this.sectionBlockExtents);
569
+ this.sectionBlockExtents = [];
570
+ if (blocks.length === 0 && this.sections.length > 0) {
571
+ this.blocks = [];
572
+ return;
573
+ }
574
+ this.sections.push({
575
+ ...sectionGeometry(section),
576
+ ...section.breakType === void 0 ? {} : { breakType: section.breakType },
577
+ blocks
578
+ });
579
+ this.blocks = [];
580
+ }
581
+ reportUnclosedBookmarks() {
582
+ for (const open of this.openBookmarks.values()) this.sink({
583
+ code: require_diagnostics.RtfDiagnosticCodes.BOOKMARK_UNPAIRED,
584
+ severity: "warning",
585
+ message: `the bookmark '${open.descriptor.name}' has no matching \\bkmkend within its own block flow, so no anchor construct is produced for it`
586
+ });
587
+ this.openBookmarks.clear();
588
+ }
589
+ finish(metadata, section, para) {
590
+ this.endSection(section, para);
591
+ if (this.sections.length === 0) this.sections.push({
592
+ ...sectionGeometry(section),
593
+ blocks: []
594
+ });
595
+ return {
596
+ kind: "wordprocessing",
597
+ metadata,
598
+ sections: this.sections
599
+ };
600
+ }
601
+ };
602
+ function sectionGeometry(section) {
603
+ return {
604
+ pageSize: {
605
+ widthPt: require_units.twipsToPoints(section.paperWidthTwips),
606
+ heightPt: require_units.twipsToPoints(section.paperHeightTwips)
607
+ },
608
+ margins: {
609
+ topPt: require_units.twipsToPoints(section.marginTopTwips),
610
+ rightPt: require_units.twipsToPoints(section.marginRightTwips),
611
+ bottomPt: require_units.twipsToPoints(section.marginBottomTwips),
612
+ leftPt: require_units.twipsToPoints(section.marginLeftTwips)
613
+ }
614
+ };
615
+ }
616
+ function defaultSectionState(header) {
617
+ return {
618
+ paperWidthTwips: header.page.paperWidthTwips,
619
+ paperHeightTwips: header.page.paperHeightTwips,
620
+ marginLeftTwips: header.page.marginLeftTwips,
621
+ marginRightTwips: header.page.marginRightTwips,
622
+ marginTopTwips: header.page.marginTopTwips,
623
+ marginBottomTwips: header.page.marginBottomTwips,
624
+ breakType: void 0
625
+ };
626
+ }
627
+ function buildRunFields(char, fontName, color, hyperlink) {
628
+ return {
629
+ ...char.bold ? { bold: true } : {},
630
+ ...char.italic ? { italic: true } : {},
631
+ ...char.underline ? { underline: true } : {},
632
+ ...char.strike ? { strike: true } : {},
633
+ ...fontName === void 0 || fontName.length === 0 ? {} : { fontFamily: fontName },
634
+ sizePt: require_units.halfPointsToPoints(char.sizeHalfPoints),
635
+ ...color === void 0 ? {} : { color },
636
+ ...hyperlink === void 0 ? {} : { hyperlink }
637
+ };
638
+ }
639
+ function buildPicture(picture, sink) {
640
+ if (picture.format === void 0) {
641
+ sink({
642
+ code: require_diagnostics.RtfDiagnosticCodes.UNSUPPORTED_PICTURE_FORMAT,
643
+ severity: "warning",
644
+ message: `a \\pict destination declared ${picture.unsupportedFormat ?? "no"} picture format; ContentImageBlock carries PNG and JPEG only, so this picture is dropped`
645
+ });
646
+ return;
647
+ }
648
+ const bytes = picture.binary.length > 0 ? Uint8Array.from(picture.binary) : require_base64.hexToBytes(picture.hex);
649
+ if (bytes.length === 0) {
650
+ sink({
651
+ code: require_diagnostics.RtfDiagnosticCodes.UNSUPPORTED_PICTURE_FORMAT,
652
+ severity: "warning",
653
+ message: "a \\pict destination carried no picture payload"
654
+ });
655
+ return;
656
+ }
657
+ const scaleX = picture.scaleXPercent / 100;
658
+ const scaleY = picture.scaleYPercent / 100;
659
+ const widthPt = picture.widthGoalTwips === void 0 ? picture.widthPixels === void 0 ? void 0 : require_units.pixelsToPoints(picture.widthPixels) : require_units.twipsToPoints(picture.widthGoalTwips);
660
+ const heightPt = picture.heightGoalTwips === void 0 ? picture.heightPixels === void 0 ? void 0 : require_units.pixelsToPoints(picture.heightPixels) : require_units.twipsToPoints(picture.heightGoalTwips);
661
+ if (widthPt === void 0 || heightPt === void 0) {
662
+ sink({
663
+ code: require_diagnostics.RtfDiagnosticCodes.PICTURE_SIZE_UNSTATED,
664
+ severity: "warning",
665
+ message: "a \\pict destination stated neither \\picwgoalN/\\pichgoalN nor \\picwN/\\pichN, so its rendered size is unknown and the picture is dropped; decoding the payload's own intrinsic size would need an image decoder this package deliberately does not carry"
666
+ });
667
+ return;
668
+ }
669
+ const scaledWidth = widthPt * scaleX;
670
+ const scaledHeight = heightPt * scaleY;
671
+ if (scaledWidth <= 0 || scaledHeight <= 0) {
672
+ sink({
673
+ code: require_diagnostics.RtfDiagnosticCodes.PICTURE_SIZE_UNSTATED,
674
+ severity: "warning",
675
+ message: "a \\pict destination's stated size scaled to zero or less, which ContentImageBlock cannot express"
676
+ });
677
+ return;
678
+ }
679
+ return {
680
+ kind: "image",
681
+ format: picture.format,
682
+ base64: require_base64.bytesToBase64(bytes),
683
+ widthPt: scaledWidth,
684
+ heightPt: scaledHeight
685
+ };
686
+ }
687
+ function defaultPictureState() {
688
+ return {
689
+ format: void 0,
690
+ unsupportedFormat: void 0,
691
+ widthGoalTwips: void 0,
692
+ heightGoalTwips: void 0,
693
+ widthPixels: void 0,
694
+ heightPixels: void 0,
695
+ scaleXPercent: 100,
696
+ scaleYPercent: 100,
697
+ hex: "",
698
+ binary: []
699
+ };
700
+ }
701
+ function toggleValue(param) {
702
+ return param === void 0 || param !== 0;
703
+ }
704
+ function readRtfDetail(input, options) {
705
+ options.signal?.throwIfAborted();
706
+ const maxInputBytes = options.maxInputBytes ?? 67108864;
707
+ if (input.length > maxInputBytes) throw new require_diagnostics.RtfInputTooLargeError(input.length, maxInputBytes);
708
+ const maxGroupDepth = options.maxGroupDepth ?? 256;
709
+ const diagnostics = [];
710
+ const callerSink = options.sink;
711
+ const sink = (diagnostic) => {
712
+ diagnostics.push(diagnostic);
713
+ callerSink?.(diagnostic);
714
+ };
715
+ const tokens = require_tokenize.tokenizeRtf(input);
716
+ assertRtfHeaderPresent(tokens);
717
+ const header = require_header.readRtfHeader(tokens, sink);
718
+ const builder = new ContentBuilder(header, sink);
719
+ const section = defaultSectionState(header);
720
+ const root = {
721
+ destination: "body",
722
+ uc: 1,
723
+ char: defaultCharacterState(),
724
+ para: defaultParagraphState(),
725
+ field: void 0,
726
+ picture: void 0,
727
+ bookmark: void 0,
728
+ inUnicodeWrapper: false
729
+ };
730
+ const stack = [root];
731
+ let state = root;
732
+ let pendingBytes = [];
733
+ const activeCodepage = () => {
734
+ return (state.char.fontIndex === void 0 ? void 0 : header.fonts.get(state.char.fontIndex)?.codepage) ?? header.codepage;
735
+ };
736
+ const flushBytes = () => {
737
+ if (pendingBytes.length === 0) return;
738
+ const text = require_codepage.decodeCodepageBytes(Uint8Array.from(pendingBytes), activeCodepage(), sink);
739
+ pendingBytes = [];
740
+ emitText(text);
741
+ };
742
+ const emitText = (text) => {
743
+ if (state.destination === "body") {
744
+ const hyperlink = state.field === void 0 ? void 0 : hyperlinkFromInstruction(state.field.instruction);
745
+ builder.appendText(text, state.char, hyperlink);
746
+ return;
747
+ }
748
+ if (state.destination === "fieldInstruction" && state.field !== void 0) {
749
+ state.field.instruction += text;
750
+ return;
751
+ }
752
+ if ((state.destination === "bookmarkStart" || state.destination === "bookmarkEnd") && state.bookmark !== void 0) {
753
+ state.bookmark.name += text;
754
+ return;
755
+ }
756
+ };
757
+ let index = 0;
758
+ let textOffset = 0;
759
+ while (index < tokens.length) {
760
+ const token = tokens[index];
761
+ if (token === void 0) break;
762
+ if (token.kind === "groupStart") {
763
+ flushBytes();
764
+ const head = require_group.groupHead(tokens, index);
765
+ const known = head.destination === void 0 ? void 0 : DESTINATION_KINDS.get(head.destination);
766
+ const isHeaderTable = head.destination !== void 0 && require_header.HEADER_DESTINATIONS.has(head.destination);
767
+ const wrapperChild = state.inUnicodeWrapper;
768
+ const kind = isHeaderTable || wrapperChild && head.destination !== "ud" ? "skip" : known ?? (head.ignorable ? "skip" : state.destination);
769
+ if (head.destination !== void 0 && !isHeaderTable) {
770
+ if (head.ignorable && known === void 0) sink({
771
+ code: require_diagnostics.RtfDiagnosticCodes.UNKNOWN_DESTINATION_SKIPPED,
772
+ severity: "info",
773
+ message: `the ignorable destination \\${head.destination} is not recognised and its content is discarded, as the specification requires`
774
+ });
775
+ else if (known === "skip" && !SILENT_SKIP_DESTINATIONS.has(head.destination)) sink({
776
+ code: require_diagnostics.RtfDiagnosticCodes.CONTENT_DESTINATION_SKIPPED,
777
+ severity: "warning",
778
+ message: `the \\${head.destination} destination's content is discarded: no ContentDocument position carries it`
779
+ });
780
+ }
781
+ if (kind === "skip") {
782
+ index = require_group.matchingGroupEnd(tokens, index) + 1;
783
+ textOffset = 0;
784
+ continue;
785
+ }
786
+ if (stack.length >= maxGroupDepth) throw new require_diagnostics.RtfNestingLimitExceededError(maxGroupDepth);
787
+ const child = cloneGroupState(state);
788
+ child.inUnicodeWrapper = kind === "unicodeWrapper";
789
+ if (known !== void 0) {
790
+ child.destination = kind;
791
+ if (head.destination === "field") child.field = { instruction: "" };
792
+ if (kind === "picture") child.picture = defaultPictureState();
793
+ if (kind === "bookmarkStart" || kind === "bookmarkEnd") child.bookmark = {
794
+ name: "",
795
+ columnFirst: void 0,
796
+ columnLast: void 0
797
+ };
798
+ index = head.contentStart;
799
+ } else index += 1;
800
+ stack.push(child);
801
+ state = child;
802
+ textOffset = 0;
803
+ continue;
804
+ }
805
+ if (token.kind === "groupEnd") {
806
+ flushBytes();
807
+ if (state.destination === "picture" && state.picture !== void 0) {
808
+ const image = buildPicture(state.picture, sink);
809
+ if (image !== void 0) builder.addBlock(image);
810
+ }
811
+ if (state.bookmark !== void 0) {
812
+ const bookmark = {
813
+ ...state.bookmark,
814
+ name: state.bookmark.name.trim()
815
+ };
816
+ if (state.destination === "bookmarkStart") builder.startBookmark(bookmark, state.para);
817
+ else if (state.destination === "bookmarkEnd") builder.endBookmark(bookmark.name);
818
+ }
819
+ if (stack.length > 1) {
820
+ stack.pop();
821
+ const parent = stack[stack.length - 1];
822
+ if (parent !== void 0) state = parent;
823
+ } else sink({
824
+ code: require_diagnostics.RtfDiagnosticCodes.UNBALANCED_GROUP,
825
+ severity: "warning",
826
+ message: "a closing brace appeared with no group open; the extra brace is ignored",
827
+ tokenIndex: index
828
+ });
829
+ index += 1;
830
+ textOffset = 0;
831
+ continue;
832
+ }
833
+ if (token.kind === "text") {
834
+ const slice = textOffset === 0 ? token.bytes : token.bytes.subarray(textOffset);
835
+ if (state.destination === "picture" && state.picture !== void 0) state.picture.hex += require_bytes.asciiStringFromBytes(slice);
836
+ else require_bytes.appendBytes(pendingBytes, slice);
837
+ index += 1;
838
+ textOffset = 0;
839
+ continue;
840
+ }
841
+ if (token.kind === "binary") {
842
+ if (state.destination === "picture" && state.picture !== void 0) require_bytes.appendBytes(state.picture.binary, token.bytes);
843
+ index += 1;
844
+ continue;
845
+ }
846
+ if (token.kind === "hex") {
847
+ if (state.destination === "picture" && state.picture !== void 0) state.picture.binary.push(token.byte);
848
+ else pendingBytes.push(token.byte);
849
+ index += 1;
850
+ continue;
851
+ }
852
+ if (token.kind === "controlSymbol") {
853
+ const text = SPECIAL_SYMBOL_TEXT.get(token.symbol);
854
+ if (text !== void 0) {
855
+ flushBytes();
856
+ emitText(text);
857
+ }
858
+ index += 1;
859
+ continue;
860
+ }
861
+ if (token.name === "u") {
862
+ flushBytes();
863
+ const code = token.param;
864
+ if (code !== void 0) emitText(String.fromCharCode(code < 0 ? code + 65536 : code));
865
+ const skipped = skipUnicodeFallback(tokens, {
866
+ index: index + 1,
867
+ textOffset: 0
868
+ }, state.uc);
869
+ index = skipped.index;
870
+ textOffset = skipped.textOffset;
871
+ continue;
872
+ }
873
+ const special = SPECIAL_CHARACTER_TEXT.get(token.name);
874
+ if (special !== void 0) {
875
+ flushBytes();
876
+ emitText(special);
877
+ index += 1;
878
+ continue;
879
+ }
880
+ flushBytes();
881
+ applyControlWord(token.name, token.param, state, builder, header, section, sink);
882
+ index += 1;
883
+ }
884
+ flushBytes();
885
+ if (stack.length > 1) sink({
886
+ code: require_diagnostics.RtfDiagnosticCodes.UNBALANCED_GROUP,
887
+ severity: "warning",
888
+ message: `${String(stack.length - 1)} group(s) were still open at the end of the input; each is treated as closing there`
889
+ });
890
+ return {
891
+ document: builder.finish(header.metadata, section, root.para),
892
+ diagnostics
893
+ };
894
+ }
895
+ function assertRtfHeaderPresent(tokens) {
896
+ const first = tokens[0];
897
+ const second = tokens[1];
898
+ if (first?.kind !== "groupStart" || second?.kind !== "controlWord" || second.name !== "rtf") throw new require_diagnostics.RtfNotAnRtfDocumentError();
899
+ }
900
+ function applyPictureControlWord(name, param, picture) {
901
+ const format = PICTURE_FORMATS.get(name);
902
+ if (format !== void 0) {
903
+ picture.format = format;
904
+ return;
905
+ }
906
+ switch (name) {
907
+ case "emfblip":
908
+ case "macpict":
909
+ case "wmetafile":
910
+ case "pmmetafile":
911
+ case "dibitmap":
912
+ case "wbitmap":
913
+ picture.unsupportedFormat = `\\${name}`;
914
+ return;
915
+ case "picwgoal":
916
+ picture.widthGoalTwips = param;
917
+ return;
918
+ case "pichgoal":
919
+ picture.heightGoalTwips = param;
920
+ return;
921
+ case "picw":
922
+ picture.widthPixels = param;
923
+ return;
924
+ case "pich":
925
+ picture.heightPixels = param;
926
+ return;
927
+ case "picscalex":
928
+ if (param !== void 0) picture.scaleXPercent = param;
929
+ return;
930
+ case "picscaley":
931
+ if (param !== void 0) picture.scaleYPercent = param;
932
+ return;
933
+ default: return;
934
+ }
935
+ }
936
+ function applyCharacterControlWord(name, param, state, header) {
937
+ switch (name) {
938
+ case "plain":
939
+ state.char = defaultCharacterState();
940
+ return true;
941
+ case "b":
942
+ state.char.bold = toggleValue(param);
943
+ return true;
944
+ case "i":
945
+ state.char.italic = toggleValue(param);
946
+ return true;
947
+ case "strike":
948
+ state.char.strike = toggleValue(param);
949
+ return true;
950
+ case "v":
951
+ state.char.hidden = toggleValue(param);
952
+ return true;
953
+ case "ulnone":
954
+ state.char.underline = false;
955
+ return true;
956
+ case "f":
957
+ state.char.fontIndex = param ?? header.defaultFontIndex;
958
+ return true;
959
+ case "fs":
960
+ state.char.sizeHalfPoints = param ?? 24;
961
+ return true;
962
+ case "cf":
963
+ state.char.colorIndex = param;
964
+ return true;
965
+ case "uc":
966
+ if (param !== void 0 && param >= 0) state.uc = param;
967
+ return true;
968
+ case "revised":
969
+ state.char.revision = {
970
+ ...state.char.revision,
971
+ revised: toggleValue(param)
972
+ };
973
+ return true;
974
+ case "revauth":
975
+ state.char.revision = {
976
+ ...state.char.revision,
977
+ revisedAuthor: param
978
+ };
979
+ return true;
980
+ case "revdttm":
981
+ state.char.revision = {
982
+ ...state.char.revision,
983
+ revisedDateTime: param
984
+ };
985
+ return true;
986
+ case "deleted":
987
+ state.char.revision = {
988
+ ...state.char.revision,
989
+ deleted: toggleValue(param)
990
+ };
991
+ return true;
992
+ case "revauthdel":
993
+ state.char.revision = {
994
+ ...state.char.revision,
995
+ deletedAuthor: param
996
+ };
997
+ return true;
998
+ case "revdttmdel":
999
+ state.char.revision = {
1000
+ ...state.char.revision,
1001
+ deletedDateTime: param
1002
+ };
1003
+ return true;
1004
+ case "mvf":
1005
+ state.char.revision = {
1006
+ ...state.char.revision,
1007
+ moved: toggleValue(param) ? "moveFrom" : void 0
1008
+ };
1009
+ return true;
1010
+ case "mvt":
1011
+ state.char.revision = {
1012
+ ...state.char.revision,
1013
+ moved: toggleValue(param) ? "moveTo" : void 0
1014
+ };
1015
+ return true;
1016
+ case "mvauth":
1017
+ state.char.revision = {
1018
+ ...state.char.revision,
1019
+ movedAuthor: param
1020
+ };
1021
+ return true;
1022
+ case "mvdate":
1023
+ state.char.revision = {
1024
+ ...state.char.revision,
1025
+ movedDateTime: param
1026
+ };
1027
+ return true;
1028
+ case "crauth":
1029
+ state.char.revision = {
1030
+ ...state.char.revision,
1031
+ formatAuthor: param
1032
+ };
1033
+ return true;
1034
+ case "crdate":
1035
+ state.char.revision = {
1036
+ ...state.char.revision,
1037
+ formatDateTime: param
1038
+ };
1039
+ return true;
1040
+ default:
1041
+ if (name.startsWith("ul") && name !== "ulc") {
1042
+ state.char.underline = toggleValue(param);
1043
+ return true;
1044
+ }
1045
+ return false;
1046
+ }
1047
+ }
1048
+ function applyParagraphControlWord(name, param, state) {
1049
+ const alignment = ALIGNMENTS.get(name);
1050
+ if (alignment !== void 0) {
1051
+ state.para.alignment = alignment;
1052
+ return true;
1053
+ }
1054
+ switch (name) {
1055
+ case "pard":
1056
+ state.para = defaultParagraphState();
1057
+ return true;
1058
+ case "s":
1059
+ state.para.styleIndex = param;
1060
+ return true;
1061
+ case "outlinelevel":
1062
+ state.para.outlineLevel = param === void 0 || param > 8 ? void 0 : param;
1063
+ return true;
1064
+ case "li":
1065
+ case "lin":
1066
+ state.para.indentLeftTwips = param ?? 0;
1067
+ return true;
1068
+ case "fi":
1069
+ state.para.indentFirstLineTwips = param ?? 0;
1070
+ return true;
1071
+ case "sb":
1072
+ state.para.spaceBeforeTwips = param ?? 0;
1073
+ return true;
1074
+ case "sa":
1075
+ state.para.spaceAfterTwips = param ?? 0;
1076
+ return true;
1077
+ case "sl":
1078
+ state.para.lineSpacingTwips = param;
1079
+ return true;
1080
+ case "slmult":
1081
+ state.para.lineSpacingIsMultiple = toggleValue(param);
1082
+ return true;
1083
+ case "pagebb":
1084
+ state.para.pageBreakBefore = true;
1085
+ return true;
1086
+ case "ls":
1087
+ state.para.listOverrideIndex = param;
1088
+ return true;
1089
+ case "ilvl":
1090
+ state.para.listLevel = param ?? 0;
1091
+ return true;
1092
+ case "intbl":
1093
+ state.para.inTable = true;
1094
+ return true;
1095
+ default: return false;
1096
+ }
1097
+ }
1098
+ function applySectionControlWord(name, param, section, header, sink) {
1099
+ if (name === "sectd") {
1100
+ Object.assign(section, defaultSectionState(header));
1101
+ return true;
1102
+ }
1103
+ if (name === "sbkcol") {
1104
+ sink({
1105
+ code: require_diagnostics.RtfDiagnosticCodes.SECTION_BREAK_UNREPRESENTED,
1106
+ severity: "info",
1107
+ message: "\\sbkcol starts the section at a new column; ContentSection.breakType names page-level breaks only, so the break kind is dropped and the section itself is kept"
1108
+ });
1109
+ section.breakType = void 0;
1110
+ return true;
1111
+ }
1112
+ if (SECTION_BREAK_TYPES.has(name)) {
1113
+ section.breakType = SECTION_BREAK_TYPES.get(name);
1114
+ return true;
1115
+ }
1116
+ if (param === void 0) return false;
1117
+ switch (name) {
1118
+ case "pgwsxn":
1119
+ section.paperWidthTwips = param;
1120
+ return true;
1121
+ case "pghsxn":
1122
+ section.paperHeightTwips = param;
1123
+ return true;
1124
+ case "marglsxn":
1125
+ section.marginLeftTwips = param;
1126
+ return true;
1127
+ case "margrsxn":
1128
+ section.marginRightTwips = param;
1129
+ return true;
1130
+ case "margtsxn":
1131
+ section.marginTopTwips = param;
1132
+ return true;
1133
+ case "margbsxn":
1134
+ section.marginBottomTwips = param;
1135
+ return true;
1136
+ default: return false;
1137
+ }
1138
+ }
1139
+ function applyStructureControlWord(name, param, state, builder, section, sink) {
1140
+ switch (name) {
1141
+ case "par":
1142
+ builder.endParagraph(state.para, true);
1143
+ return true;
1144
+ case "trowd":
1145
+ state.para.inTable = true;
1146
+ builder.startRowDefinition();
1147
+ return true;
1148
+ case "trleft":
1149
+ builder.setRowLeft(param ?? 0);
1150
+ return true;
1151
+ case "cellx":
1152
+ if (param !== void 0) builder.addCellBoundary(param);
1153
+ return true;
1154
+ case "cell":
1155
+ builder.endCell(state.para);
1156
+ return true;
1157
+ case "row":
1158
+ builder.endRow(state.para);
1159
+ state.para.inTable = false;
1160
+ return true;
1161
+ case "nestcell":
1162
+ case "nestrow":
1163
+ sink({
1164
+ code: require_diagnostics.RtfDiagnosticCodes.NESTED_TABLE_FLATTENED,
1165
+ severity: "warning",
1166
+ message: "a nested table's cell/row marks are read as ordinary cell content; the inner table's own structure is not reconstructed"
1167
+ });
1168
+ return true;
1169
+ case "page":
1170
+ builder.endParagraph(state.para, false);
1171
+ builder.addBlock({ kind: "pageBreak" });
1172
+ return true;
1173
+ case "sect":
1174
+ builder.endParagraph(state.para, true);
1175
+ builder.endSection(section, state.para);
1176
+ return true;
1177
+ default: return false;
1178
+ }
1179
+ }
1180
+ function applyControlWord(name, param, state, builder, header, section, sink) {
1181
+ const picture = state.picture;
1182
+ if (state.destination === "picture" && picture !== void 0) {
1183
+ applyPictureControlWord(name, param, picture);
1184
+ return;
1185
+ }
1186
+ const bookmark = state.bookmark;
1187
+ if (bookmark !== void 0 && state.destination === "bookmarkStart") {
1188
+ if (name === "bkmkcolf") bookmark.columnFirst = param;
1189
+ else if (name === "bkmkcoll") bookmark.columnLast = param;
1190
+ return;
1191
+ }
1192
+ if (state.destination === "bookmarkEnd") return;
1193
+ if (applyCharacterControlWord(name, param, state, header)) return;
1194
+ if (builder.applyCellDefinition(name, param)) return;
1195
+ if (applyParagraphControlWord(name, param, state)) return;
1196
+ if (applySectionControlWord(name, param, section, header, sink)) return;
1197
+ applyStructureControlWord(name, param, state, builder, section, sink);
1198
+ }
1199
+ function readRtfContent(input, options = {}) {
1200
+ return readRtfDetail(typeof input === "string" ? require_bytes.rtfBytesFromLatin1(input) : input, options);
1201
+ }
1202
+ function readRtf(input, options = {}) {
1203
+ const { document, diagnostics } = readRtfContent(input, options);
1204
+ return {
1205
+ documentPackage: (0, document_schema_js.assembleTree)(document),
1206
+ diagnostics
1207
+ };
1208
+ }
1209
+ //#endregion
1210
+ exports.readRtf = readRtf;
1211
+ exports.readRtfContent = readRtfContent;