docx 9.8.0 → 9.9.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.
@@ -0,0 +1,385 @@
1
+ import type { Document as Document_2 } from 'docx';
2
+ import type { PageNumberEstimator } from 'docx';
3
+ import type { TemplatePageNumberEstimator } from 'docx';
4
+
5
+ /**
6
+ * A paragraph or table on a page.
7
+ *
8
+ * @publicApi
9
+ */
10
+ export declare type BlockLayout = ParagraphLayout | TableLayout;
11
+
12
+ /**
13
+ * A document's pages, as {@link layoutDocument} lays them out.
14
+ *
15
+ * @publicApi
16
+ */
17
+ export declare type DocumentLayout = {
18
+ /**
19
+ * Its pages, in order. When the layout stopped, those up to where it stopped, the last of them with what was laid out
20
+ * on it
21
+ */
22
+ readonly pages: readonly PageLayout[];
23
+ /**
24
+ * What the layout stopped at, when it couldn't lay out all of the document, such as `"a text box"`. What comes after
25
+ * it isn't laid out
26
+ */
27
+ readonly stoppedAt?: string;
28
+ };
29
+
30
+ /**
31
+ * Works out the page each bookmark of a document starts on, and how many pages the document and each of its sections
32
+ * have, by laying out its pages as Word does, so the page numbers of its tables of contents and page references, and its
33
+ * numbers of pages, are written with it. Give it to a document as its `pageNumbers`, or to `patchDocument` as its
34
+ * `pageNumbers` to write a template's once it is patched:
35
+ *
36
+ * ```ts
37
+ * new Document({ pageNumbers: estimatePageNumbers, sections: [...] });
38
+ * await patchDocument({ outputType: "nodebuffer", data, patches, pageNumbers: estimatePageNumbers });
39
+ * ```
40
+ *
41
+ * The pages are laid out with the widths and heights of the fonts Word documents use most, such as Calibri, Cambria,
42
+ * Arial and Times New Roman, and of those made as wide, such as Carlito, and text in the fonts the document embeds is
43
+ * measured from their files, but for fonts Office also offers. To measure text in other fonts, such as Aptos, from their files, use
44
+ * {@link estimatePageNumbersWith}. It follows paragraphs' spacing, indents, line spacing, tab stops and keep settings, widow
45
+ * and orphan control, lists, pictures in the line, tables, whose rows break across pages, footnotes and endnotes, page,
46
+ * column and section breaks, and each section's page size, margins, columns, headers, footers and page numbering.
47
+ *
48
+ * It stops at the first thing it can't lay out yet: a drawing or table that text flows around, a text box or frame, an
49
+ * equation, a footnote that continues on the next page, columns evened out before a continuous section break, a line in
50
+ * a table cell that is taller than a page, text in a font that isn't in the width tables and isn't embedded, such as
51
+ * Garamond, text in a font the document embeds that Office also offers, such as Pacifico, which Word for Mac draws in
52
+ * Office's own copy of it, a character whose width in its font isn't known, such as a mathematical symbol in Calibri, which Word draws in
53
+ * Cambria Math, a word Word may hyphenate, in a document that hyphenates its words, as which parts Word breaks it into
54
+ * is in its own dictionaries, or a date in the text, which Word writes when it opens the document. The page references
55
+ * to bookmarks after it are left blank, for Word to fill in when it updates the fields. A document in compatibility
56
+ * mode, which Word lays out as an older version of Word did, isn't laid out at all, nor is one with a compatibility
57
+ * setting that changes Word's lines in a way not yet followed: `suppressTopSpacing` or `useFELayout` turned on, or one
58
+ * of Word's own the layout doesn't know, on or off. The schema's other settings, which ask for an older Word's or
59
+ * another application's layout, Word lays out lines with as without them, and settings for other applications are left
60
+ * to them. When laying the pages out again with the page numbers it worked out still changes them after three passes,
61
+ * as when a table of contents wraps one way with a number and the other way without it, all of them are left blank. To
62
+ * lay out past what it stops at with the best guess it has instead, give the document
63
+ * `estimatePageNumbersWith({ guess: true })`.
64
+ *
65
+ * Page references are written as Word writes them, with `\p` ("above", "below" or "on page 4") and in formats of their
66
+ * own, such as `\* roman`, and so are numbers of pages. Page references, tables of contents and SEQ fields (caption
67
+ * numbers, which are counted without laying out the pages) are written clean, so Word shows the numbers as they are
68
+ * written, and the numbers left blank stay blank, without asking to update the fields, unless the document has
69
+ * `updateFields` on.
70
+ *
71
+ * @publicApi
72
+ */
73
+ export declare const estimatePageNumbers: PageNumberEstimator & TemplatePageNumberEstimator;
74
+
75
+ /**
76
+ * How {@link estimatePageNumbersWith} lays out the pages.
77
+ *
78
+ * @publicApi
79
+ */
80
+ export declare type EstimatePageNumbersOptions = {
81
+ /**
82
+ * Font files to measure text in, with their own widths, kerning, ligatures and line heights, as Word measures it. Give
83
+ * a file for each of a font's faces the document uses, such as Aptos, Aptos Bold and Aptos Italic: text in fonts
84
+ * without files is measured as it is without them, and so is bold text, or text that isn't bold, in a font without a
85
+ * file for it, which stops the layout when the font isn't in the width tables. Italic text in a font without an italic
86
+ * file is measured with the upright one. The layout stops at a character a font's file has no glyph for, as Word draws
87
+ * it in another font. A font the document embeds is measured from the document's own file. Text in a font Office also
88
+ * offers, such as Pacifico, stops the layout, as Word for Mac draws it in Office's own copy of the font, but for
89
+ * Office's fonts of the width tables, such as Aptos
90
+ */
91
+ readonly fonts?: readonly FontFile[];
92
+ /**
93
+ * Measures how wide text is, such as {@link measureWithPretext}, which measures it with the fonts a browser has.
94
+ * Default is the widths of the fonts Word documents use most, which {@link estimatePageNumbers} uses. Lines still
95
+ * break, and tabs move to their stops, as Word lays them out, and lines are as tall as Word makes them, from the
96
+ * width tables, so the layout stops at text in a font that isn't in them and isn't given as a file or embedded.
97
+ */
98
+ readonly measureWidth?: MeasureWidth;
99
+ /**
100
+ * Whether to lay out past what the layout can't lay out as Word does yet with the best guess it has, rather than
101
+ * leave the page numbers after it blank. Text in a font that isn't in the width tables, and isn't given as a file or
102
+ * embedded, is measured as the most similar font that is, such as Roboto as Arial, text in a font given as a file or
103
+ * embedded that Office also offers is measured from the file, and a character whose width isn't
104
+ * known as an average letter of its font. A date is measured as it is written, and a setting or formatting the layout
105
+ * doesn't follow, such as hyphenation or a compatibility setting, is left as if it weren't there. What it can't read,
106
+ * such as a drawing that text flows around or an equation, is left out, and a page laid out as the rule it follows
107
+ * nearest to Word's lays it out. It still stops where it has no guess, such as at an imported document.
108
+ *
109
+ * The page numbers may then not be Word's, so `guesses`, in what the estimator gives, says where it guessed. Default
110
+ * is off, so the numbers written are Word's, as near as the layout knows, or blank.
111
+ */
112
+ readonly guess?: boolean;
113
+ };
114
+
115
+ /**
116
+ * Works out the page each bookmark of a document starts on, as {@link estimatePageNumbers} does, measuring text as the
117
+ * options say. Give what it returns to a document as its `pageNumbers`, or to `patchDocument` as its `pageNumbers`:
118
+ *
119
+ * ```ts
120
+ * new Document({ pageNumbers: estimatePageNumbersWith({ measureWidth: measureWithPretext(pretext) }), sections: [...] });
121
+ * const fonts = [{ data: await readFile("Aptos.ttf") }, { data: await readFile("Aptos-Bold.ttf") }];
122
+ * new Document({ pageNumbers: estimatePageNumbersWith({ fonts }), sections: [...] });
123
+ * new Document({ pageNumbers: estimatePageNumbersWith({ guess: true }), sections: [...] });
124
+ * ```
125
+ *
126
+ * It throws when a font file isn't a TrueType or OpenType font.
127
+ *
128
+ * @publicApi
129
+ */
130
+ export declare const estimatePageNumbersWith: ({ measureWidth, fonts, guess, }: EstimatePageNumbersOptions) => PageNumberEstimator & TemplatePageNumberEstimator;
131
+
132
+ /**
133
+ * A font file's bytes: a TrueType or OpenType font (`.ttf` or `.otf`), or a collection of them (`.ttc`).
134
+ */
135
+ declare type FontData = Uint8Array | ArrayBuffer;
136
+
137
+ /**
138
+ * A font file to measure text in.
139
+ *
140
+ * @publicApi
141
+ */
142
+ export declare type FontFile = {
143
+ /**
144
+ * The file's bytes: a TrueType or OpenType font (`.ttf` or `.otf`), or a collection of them (`.ttc`). Web fonts
145
+ * (`.woff` and `.woff2`) are compressed, and can't be read
146
+ */
147
+ readonly data: FontData;
148
+ /**
149
+ * The name documents give the font, when it isn't the name in the file, such as `"Calibri"` for a file of Carlito,
150
+ * which is as wide. Each font in the file has the name it gives itself by default, such as `"Aptos"`
151
+ */
152
+ readonly name?: string;
153
+ };
154
+
155
+ /**
156
+ * The font to measure text in.
157
+ *
158
+ * @publicApi
159
+ */
160
+ export declare type FontToMeasure = {
161
+ /** The font's name, as the document gives it, such as `"Calibri"` */
162
+ readonly name: string;
163
+ /** Size in points */
164
+ readonly size: number;
165
+ readonly bold: boolean;
166
+ readonly italic: boolean;
167
+ };
168
+
169
+ /**
170
+ * Lays out a document's pages as Word does, and gives what is on each page: the lines of its paragraphs, with their text
171
+ * and where they are, the rows of its tables, its footnotes and endnotes, and which of its section's headers and footers
172
+ * it shows.
173
+ *
174
+ * ```ts
175
+ * const { pages } = layoutDocument(new Document({ sections: [...] }));
176
+ * ```
177
+ *
178
+ * The pages are laid out as `estimatePageNumbers` lays them out, with the same fonts and rules, and the page numbers of
179
+ * tables of contents and page references are laid out as it writes them. Where it can't lay out something yet, such as a
180
+ * text box, it stops, and gives the pages up to there, with why in `stoppedAt`, unless it is asked to guess.
181
+ *
182
+ * @param document - The document to lay out. It is laid out as it would be written
183
+ * @param options - Whether to guess past what it can't lay out as Word does yet
184
+ *
185
+ * @publicApi
186
+ */
187
+ export declare const layoutDocument: (document: Document_2, { guess }?: LayoutDocumentOptions) => DocumentLayout;
188
+
189
+ /**
190
+ * How {@link layoutDocument} lays out the pages.
191
+ *
192
+ * @publicApi
193
+ */
194
+ export declare type LayoutDocumentOptions = {
195
+ /**
196
+ * Whether to lay out past what the layout can't lay out as Word does yet with the best guess it has, as
197
+ * `estimatePageNumbersWith({ guess: true })` does, rather than stop there. Each page says what was guessed at on it,
198
+ * in `guesses`, and `stoppedAt` says where it stopped all the same, where it has no guess. Default is off
199
+ */
200
+ readonly guess?: boolean;
201
+ };
202
+
203
+ /**
204
+ * A line of a paragraph on a page. Lengths are in pixels, 96 to the inch, from the top left corner of the page.
205
+ *
206
+ * @publicApi
207
+ */
208
+ export declare type LineLayout = {
209
+ /**
210
+ * The text on the line, with the spaces where it wraps and a tab as `\t`. A picture or a break adds nothing to it,
211
+ * so the texts of a paragraph's lines, one after the other, are its text
212
+ */
213
+ readonly text: string;
214
+ /** Where the room for the line starts across the page: beside a drawing that text flows around, where that room starts */
215
+ readonly x: number;
216
+ /** Where the top of the line is down the page */
217
+ readonly y: number;
218
+ /**
219
+ * How wide the room for the line is: its column, or the page's text, less the paragraph's indents, or the room beside a
220
+ * drawing that text flows around. Its text is lined up in it as the paragraph's alignment says, but for an equation alone
221
+ * in its paragraph, which Word lines up as the equation's own justification says (`m:jc`, or the document's
222
+ * `m:defJc`), centred unless that says otherwise. A line with text on both sides of a drawing is two lines with the same
223
+ * `y`, the left first
224
+ */
225
+ readonly width: number;
226
+ /** How tall the line is, with the paragraph's line spacing */
227
+ readonly height: number;
228
+ /** How far its text goes from where the line starts, without the spaces at its end */
229
+ readonly textWidth: number;
230
+ };
231
+
232
+ /**
233
+ * Measures how wide text is in a font, in points. The text is a word, part of one, or the spaces between words, with no
234
+ * tabs or line breaks: the layout places those itself, at the paragraph's tab stops, as Word does. It adds the space
235
+ * between characters and the width of scaled text, which the document's formatting gives, to what this measures.
236
+ *
237
+ * @publicApi
238
+ */
239
+ export declare type MeasureWidth = (text: string, font: FontToMeasure) => number;
240
+
241
+ /**
242
+ * Measures text with Pretext, in the fonts the page has, for laying out a document's pages in a browser. Pretext needs
243
+ * a canvas to measure with, an `OffscreenCanvas` or a page's, so it doesn't work in Node.
244
+ *
245
+ * Load the fonts first, such as with `document.fonts.load("11pt Calibri")`: until a font has loaded, the browser
246
+ * measures text in another, and Pretext keeps the widths it measured.
247
+ *
248
+ * ```ts
249
+ * import * as pretext from "@chenglou/pretext";
250
+ * import { estimatePageNumbersWith, measureWithPretext } from "docx/layout";
251
+ *
252
+ * new Document({ pageNumbers: estimatePageNumbersWith({ measureWidth: measureWithPretext(pretext) }), sections: [...] });
253
+ * ```
254
+ *
255
+ * @param pretext - Pretext's module, or the two functions of it that are used
256
+ * @publicApi
257
+ */
258
+ export declare const measureWithPretext: <Prepared>({ prepareWithSegments, measureNaturalWidth }: Pretext<Prepared>, { fontFamilies }?: PretextOptions) => MeasureWidth;
259
+
260
+ /**
261
+ * A footnote or endnote, or the part of one, on a page.
262
+ *
263
+ * @publicApi
264
+ */
265
+ export declare type NoteLayout = {
266
+ /** The note's number, as its reference shows it, such as `"1"` or `"iv"` */
267
+ readonly noteNumber: string;
268
+ /** Its paragraphs and tables on the page */
269
+ readonly content: readonly BlockLayout[];
270
+ };
271
+
272
+ /**
273
+ * A page of a document, and what is on it. Lengths are in pixels.
274
+ *
275
+ * @publicApi
276
+ */
277
+ export declare type PageLayout = {
278
+ /**
279
+ * The page's number, as the page shows it, such as `"3"` or `"iv"`. None when Word's isn't known: when its section's
280
+ * page numbers start with a chapter number, or are in a format the layout doesn't write
281
+ */
282
+ readonly pageNumber?: string;
283
+ /** The section the page starts in, counted from 0 */
284
+ readonly section: number;
285
+ /** The size of the page */
286
+ readonly width: number;
287
+ readonly height: number;
288
+ /**
289
+ * When its text runs down the page, as in Chinese and Japanese, whether its lines go across it from the right or from
290
+ * the left. Each line is then a line down the page: its `x` and `y` are the top left of its room, its `width` how far
291
+ * across the page the line is, and its `height` how far down it the room is, its text going `textWidth` down from its
292
+ * top
293
+ */
294
+ readonly textRunsDown?: "fromRight" | "fromLeft";
295
+ /**
296
+ * Which of its section's headers the page shows, as a section's `headers` names them: the first page's, the even
297
+ * pages', or the default. A section that gives none of a kind shows the one of the section before. None when the
298
+ * page has no header
299
+ */
300
+ readonly header?: "default" | "first" | "even";
301
+ /** Which of its section's footers the page shows, in the same way */
302
+ readonly footer?: "default" | "first" | "even";
303
+ /** The paragraphs and tables of the body on the page, or the parts of them on it, in order */
304
+ readonly body: readonly BlockLayout[];
305
+ /** The footnotes at the bottom of the page, in order: the rest of one that goes on from the page before comes first */
306
+ readonly footnotes: readonly NoteLayout[];
307
+ /** The endnotes on the page, which follow the body */
308
+ readonly endnotes: readonly NoteLayout[];
309
+ /**
310
+ * What the layout guessed at on the page, when it was asked to guess: why it would have stopped there, such as
311
+ * `"a font not in the width tables"`, once each. From there on, the lines may not be where Word puts them
312
+ */
313
+ readonly guesses?: readonly string[];
314
+ };
315
+
316
+ /**
317
+ * A paragraph, or the part of one, on a page.
318
+ *
319
+ * @publicApi
320
+ */
321
+ export declare type ParagraphLayout = {
322
+ readonly type: "paragraph";
323
+ /**
324
+ * Where the paragraph is among the paragraphs and tables of the body, or of its footnote or endnote, counted from 0.
325
+ * A paragraph on more than one page has the same index on each
326
+ */
327
+ readonly index: number;
328
+ /** Its lines on the page, in order */
329
+ readonly lines: readonly LineLayout[];
330
+ };
331
+
332
+ /**
333
+ * The functions of Pretext (`@chenglou/pretext`) that {@link measureWithPretext} uses. The module itself is one:
334
+ * `import * as pretext from "@chenglou/pretext"`.
335
+ *
336
+ * @publicApi
337
+ */
338
+ export declare type Pretext<Prepared> = {
339
+ readonly prepareWithSegments: (text: string, font: string, options: {
340
+ readonly whiteSpace: "pre-wrap";
341
+ }) => Prepared;
342
+ readonly measureNaturalWidth: (prepared: Prepared) => number;
343
+ };
344
+
345
+ /**
346
+ * @publicApi
347
+ */
348
+ export declare type PretextOptions = {
349
+ /**
350
+ * The CSS font families to measure each of the document's fonts with, by the name the document gives the font, such
351
+ * as `{ Calibri: "Carlito, sans-serif" }` for a page that has Carlito, which is as wide as Calibri, but not Calibri.
352
+ * A font not in it is measured with the font of its own name, or the browser's default font if the page doesn't
353
+ * have it.
354
+ */
355
+ readonly fontFamilies?: Readonly<Record<string, string>>;
356
+ };
357
+
358
+ /**
359
+ * A row of a table, or the part of one, on a page. Lengths are in pixels, from the top of the page.
360
+ *
361
+ * @publicApi
362
+ */
363
+ export declare type RowLayout = {
364
+ /** Which of the table's rows it is, counted from 0 */
365
+ readonly index: number;
366
+ /** Where the top of the row is, above its top border */
367
+ readonly y: number;
368
+ /** How tall the row is, or its part on the page, with its borders */
369
+ readonly height: number;
370
+ };
371
+
372
+ /**
373
+ * A table, or the part of one, on a page.
374
+ *
375
+ * @publicApi
376
+ */
377
+ export declare type TableLayout = {
378
+ readonly type: "table";
379
+ /** Where the table is among the paragraphs and tables of the body, or of its note, counted from 0 */
380
+ readonly index: number;
381
+ /** Its rows on the page, or the parts of them on it, in order, with its header rows repeated at the top of each page */
382
+ readonly rows: readonly RowLayout[];
383
+ };
384
+
385
+ export { }