@folijs/view 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/dist/clipboard.d.ts +3 -1
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +195 -20
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +790 -102
- package/dist/layout/flow.js.map +1 -1
- package/dist/print.d.ts +22 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +31 -0
- package/dist/print.js.map +1 -0
- package/dist/view.d.ts +407 -8
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +2956 -263
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +99 -2
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +535 -31
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/columns.d.ts +23 -0
- package/dist/word/columns.d.ts.map +1 -0
- package/dist/word/columns.js +51 -0
- package/dist/word/columns.js.map +1 -0
- package/dist/word/controls.d.ts +63 -0
- package/dist/word/controls.d.ts.map +1 -0
- package/dist/word/controls.js +411 -0
- package/dist/word/controls.js.map +1 -0
- package/dist/word/edit.d.ts +32 -11
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +215 -33
- package/dist/word/edit.js.map +1 -1
- package/dist/word/matches.d.ts +23 -0
- package/dist/word/matches.d.ts.map +1 -0
- package/dist/word/matches.js +97 -0
- package/dist/word/matches.js.map +1 -0
- package/dist/word/notes.d.ts +105 -0
- package/dist/word/notes.d.ts.map +1 -0
- package/dist/word/notes.js +392 -0
- package/dist/word/notes.js.map +1 -0
- package/dist/word/pages.d.ts +26 -13
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +439 -203
- package/dist/word/pages.js.map +1 -1
- package/dist/word/regions.d.ts +113 -0
- package/dist/word/regions.d.ts.map +1 -0
- package/dist/word/regions.js +374 -0
- package/dist/word/regions.js.map +1 -0
- package/dist/word/table.d.ts +79 -0
- package/dist/word/table.d.ts.map +1 -0
- package/dist/word/table.js +210 -0
- package/dist/word/table.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +3 -0
- package/src/index.ts +8 -1
- package/src/layout/flow.ts +948 -111
- package/src/print.ts +48 -0
- package/src/view.ts +3291 -273
- package/src/word/blocks.ts +611 -28
- package/src/word/columns.ts +66 -0
- package/src/word/controls.ts +447 -0
- package/src/word/edit.ts +253 -30
- package/src/word/matches.ts +99 -0
- package/src/word/notes.ts +451 -0
- package/src/word/pages.ts +524 -192
- package/src/word/regions.ts +469 -0
- package/src/word/table.ts +267 -0
package/src/layout/flow.ts
CHANGED
|
@@ -17,22 +17,116 @@ export interface BlockBox {
|
|
|
17
17
|
/** Keep on the same page as the start of the next block (headings). */
|
|
18
18
|
keepWithNext?: boolean;
|
|
19
19
|
/** The lines that end with an explicit page break (indices into `lines()`), if any. */
|
|
20
|
-
breaksAfter?(): readonly number[];
|
|
20
|
+
breaksAfter?(column?: number): readonly number[];
|
|
21
21
|
/** Height shown again at the top of each fragment after the first (a table's header rows). */
|
|
22
22
|
repeat?: number;
|
|
23
|
+
/** Splits at any of its line ends, without widow and orphan control (a table's rows). */
|
|
24
|
+
anyLine?: boolean;
|
|
23
25
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
|
|
26
|
+
* The lines that end with an explicit column break, if any: indices into the lines of column
|
|
27
|
+
* `column` (`inColumn`), as for `breaksAfter`.
|
|
28
|
+
*/
|
|
29
|
+
columnBreaksAfter?(column?: number): readonly number[];
|
|
30
|
+
/**
|
|
31
|
+
* Content height of the pages this block goes on, when not the flow's. A block with another
|
|
32
|
+
* page height than the one before it must start a page (an explicit break before it).
|
|
33
|
+
* `section`, when given, says it instead.
|
|
27
34
|
*/
|
|
28
35
|
pageHeight?: number;
|
|
36
|
+
/** The section it is in: its blocks are consecutive and share this object. */
|
|
37
|
+
section?: FlowSection;
|
|
38
|
+
/**
|
|
39
|
+
* An empty paragraph that only holds a section's break: Word gives it no room after text
|
|
40
|
+
* (not its line, not its spacing). Alone at the top of a column it is an ordinary line.
|
|
41
|
+
*/
|
|
42
|
+
sectionMark?: boolean;
|
|
43
|
+
/** Its height and lines in column `column` of its section, when the columns' widths differ. */
|
|
44
|
+
inColumn?: ((column: number) => Pick<BlockBox, "height" | "lines">) | undefined;
|
|
45
|
+
/** The footnotes referred to, with the line (of `column`'s lines) holding each reference. */
|
|
46
|
+
notes?: ((column: number) => readonly { line: number; note: FlowNote }[]) | undefined;
|
|
47
|
+
/** What follows this block in the text: the endnotes ending its section or the document. */
|
|
48
|
+
endnotes?: readonly FlowNote[] | undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A note, or a separator, as the flow sees it: lines, split between pages like a block's. */
|
|
52
|
+
export interface FlowNote {
|
|
53
|
+
/** Its story. */
|
|
54
|
+
id: string;
|
|
55
|
+
/** The bottom of each of its lines from its top, in column `column`'s width. */
|
|
56
|
+
lines(column: number): readonly number[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Lines `[fromLine, toLine)` of a note on a page, at the foot or (`end`) after the text. */
|
|
60
|
+
export interface NotePiece {
|
|
61
|
+
id: string;
|
|
62
|
+
page: number;
|
|
63
|
+
band?: number;
|
|
64
|
+
column?: number;
|
|
65
|
+
fromLine: number;
|
|
66
|
+
toLine: number;
|
|
67
|
+
/** An endnote, in the text's flow: the space above it when it starts there. */
|
|
68
|
+
end?: boolean;
|
|
69
|
+
lead?: number;
|
|
70
|
+
/** On a page after the last block's, for what was still to come. */
|
|
71
|
+
tail?: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** What a page's layout depends on: first page, odd or even, by displayed number. */
|
|
75
|
+
export interface FlowPage {
|
|
76
|
+
/** The page's place in the document, from 0. */
|
|
77
|
+
index: number;
|
|
78
|
+
/** Its displayed number (`w:pgNumType` restarts count). */
|
|
79
|
+
number: number;
|
|
80
|
+
/** Whether its section starts on it. */
|
|
81
|
+
first: boolean;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A page as flowed: what decides its header and footer (its first section's). */
|
|
85
|
+
export interface PageFacts extends FlowPage {
|
|
86
|
+
/** The first section on it, by its `index` (a page nothing is on: the next page's). */
|
|
87
|
+
section: number;
|
|
88
|
+
/** How many pages that section has text on; 0 for a page nothing is on. */
|
|
89
|
+
sectionPages: number;
|
|
29
90
|
}
|
|
30
91
|
|
|
31
|
-
/**
|
|
92
|
+
/** A section as the flow sees it. */
|
|
93
|
+
export interface FlowSection {
|
|
94
|
+
/** Its place among the document's sections, from 0. */
|
|
95
|
+
index?: number;
|
|
96
|
+
/** How it starts (Word's `w:type`). */
|
|
97
|
+
start: "nextPage" | "continuous" | "evenPage" | "oddPage" | "nextColumn";
|
|
98
|
+
columns: number;
|
|
99
|
+
/** A continuous section follows: its columns end level on its last page. */
|
|
100
|
+
balanced?: boolean;
|
|
101
|
+
/** The displayed number of its first page, when it restarts them. */
|
|
102
|
+
pageStart?: number | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* The content box of a page that starts with this section. A section continuing on another's
|
|
105
|
+
* page has that page's box, less what is above its band.
|
|
106
|
+
*/
|
|
107
|
+
box(page: FlowPage): { height: number };
|
|
108
|
+
/** Whether a page of this section is a page of `other` (a continuous one can share it). */
|
|
109
|
+
samePage(other: FlowSection): boolean;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The lines `[fromLine, toLine)` of a block, placed on one page (`toLine` Infinity: to the end).
|
|
114
|
+
* Line numbers are those of the column it is in, when columns' widths differ.
|
|
115
|
+
*/
|
|
32
116
|
export interface Fragment {
|
|
33
117
|
page: number;
|
|
34
118
|
fromLine: number;
|
|
35
119
|
toLine: number;
|
|
120
|
+
/** Its band on the page (one per section on it), when not the first. */
|
|
121
|
+
band?: number;
|
|
122
|
+
/** Its column in the band, when not the first. */
|
|
123
|
+
column?: number;
|
|
124
|
+
/** The first fragment of a band after the first: the space between the two bands. */
|
|
125
|
+
lead?: number;
|
|
126
|
+
/** A `sectionMark` block given no room. */
|
|
127
|
+
mark?: boolean;
|
|
128
|
+
/** The notes this fragment brought onto their pages: those it refers to, what was carried. */
|
|
129
|
+
notes?: NotePiece[];
|
|
36
130
|
}
|
|
37
131
|
|
|
38
132
|
export interface FlowRules {
|
|
@@ -42,11 +136,21 @@ export interface FlowRules {
|
|
|
42
136
|
widows: number;
|
|
43
137
|
/**
|
|
44
138
|
* How margins (paragraph spacing) combine. `css` (the default): as CSS in a padded page.
|
|
45
|
-
* `word`: as Word spaces paragraphs
|
|
46
|
-
* A page that a block flowed onto (not one an
|
|
47
|
-
* the space before its first block. The space
|
|
139
|
+
* `word`: as Word spaces paragraphs: the larger of the space after one and the space before
|
|
140
|
+
* the next (Word's PDFs of the corpus). A page or column that a block flowed onto (not one an
|
|
141
|
+
* explicit break started, nor the first) drops the space before its first block. The space
|
|
142
|
+
* after the last block on a page need not fit.
|
|
48
143
|
*/
|
|
49
144
|
spacing?: "css" | "word";
|
|
145
|
+
/**
|
|
146
|
+
* With `word` spacing: the two spaces add up instead (a document with Word's
|
|
147
|
+
* `w:doNotUseHTMLParagraphAutoSpacing`, in every compatibility mode).
|
|
148
|
+
*/
|
|
149
|
+
addSpacing?: boolean;
|
|
150
|
+
/** The document has even and odd headers: a section restarting its numbers still keeps sides. */
|
|
151
|
+
evenAndOddHeaders?: boolean;
|
|
152
|
+
/** What goes above footnotes: on a page they start on, and on one they continue on. */
|
|
153
|
+
notes?: { separator: FlowNote; continuation: FlowNote } | undefined;
|
|
50
154
|
}
|
|
51
155
|
|
|
52
156
|
export const DEFAULT_RULES: FlowRules = { orphans: 2, widows: 2 };
|
|
@@ -62,29 +166,70 @@ const SLACK = 0.5;
|
|
|
62
166
|
/** Where the flow stands before a block: shared by a full and an incremental flow. */
|
|
63
167
|
export interface FlowState {
|
|
64
168
|
page: number;
|
|
169
|
+
/** The page's displayed number. */
|
|
170
|
+
number: number;
|
|
171
|
+
/** The page the current section started on. */
|
|
172
|
+
sectionPage: number;
|
|
173
|
+
band: number;
|
|
174
|
+
/** Where the band starts on its page, the space above it included. */
|
|
175
|
+
bandTop: number;
|
|
176
|
+
/** The space above the band: its first block's space before counts from there. */
|
|
177
|
+
lead: number;
|
|
178
|
+
column: number;
|
|
179
|
+
/** The height used in the column, from the band's top. */
|
|
65
180
|
used: number;
|
|
181
|
+
/** The tallest of the band's earlier columns. */
|
|
182
|
+
bandBottom: number;
|
|
66
183
|
previousMargin: number;
|
|
184
|
+
/** Blocks in the column. */
|
|
67
185
|
onPage: number;
|
|
68
186
|
/** An explicit page break ended the previous block: the next one starts a new page. */
|
|
69
187
|
breakPending: boolean;
|
|
70
|
-
/**
|
|
188
|
+
/** An explicit column break ended it: the next one starts a new column. */
|
|
189
|
+
columnPending: boolean;
|
|
190
|
+
/** The column is the first or an explicit break started it (Word keeps its first space before). */
|
|
71
191
|
forcedPage: boolean;
|
|
192
|
+
/** On a page shared by sections: the first of them, which gives the page its box. */
|
|
193
|
+
owner: { section: FlowSection; first: boolean } | null;
|
|
194
|
+
/** The height footnotes take at the foot of the band, their separator included. */
|
|
195
|
+
notes: number;
|
|
196
|
+
/** The height of each of those notes. */
|
|
197
|
+
noteParts: readonly number[];
|
|
198
|
+
/** What the notes of the page's earlier bands take. */
|
|
199
|
+
pageNotes: number;
|
|
200
|
+
/** Footnotes still to be placed, from the line each has got to. */
|
|
201
|
+
carry: readonly { note: FlowNote; from: number }[];
|
|
202
|
+
/** Room kept at the foot of a band (by page and band) for notes its later columns refer to. */
|
|
203
|
+
reserves: Readonly<Record<string, number>>;
|
|
72
204
|
}
|
|
73
205
|
|
|
74
206
|
/**
|
|
75
|
-
* Page flow: the fragments of each block, in order. Blocks fill a page in order; a
|
|
76
|
-
* does not fit is split at a line boundary, the lines that fit staying
|
|
77
|
-
* continuing at the top of the next
|
|
78
|
-
* line of the next block ends; an empty
|
|
207
|
+
* Page flow: the fragments of each block, in order. Blocks fill a page's columns in order; a
|
|
208
|
+
* block that does not fit is split at a line boundary, the lines that fit staying in the column,
|
|
209
|
+
* the rest continuing at the top of the next column or page (as many as it takes). A column with
|
|
210
|
+
* room for no line of the next block ends; an empty one always takes at least one line.
|
|
79
211
|
*
|
|
80
212
|
* Rules:
|
|
81
|
-
* - Explicit
|
|
213
|
+
* - Explicit breaks: the line holding a page break ends its page, a column break its column.
|
|
82
214
|
* - Widows and orphans: a split leaves at least `orphans` lines at the bottom of a page and
|
|
83
215
|
* carries at least `widows` lines to the next; a block too short for both moves whole. Not
|
|
84
216
|
* applied where only an empty page could hold the lines, nor across an explicit break.
|
|
85
|
-
* - Keep with next: a `keepWithNext` block (and a chain of them) moves to the next page
|
|
86
|
-
* the block after it would start there, unless it already starts its
|
|
87
|
-
* not bring them together (then the flow stays as it was).
|
|
217
|
+
* - Keep with next: a `keepWithNext` block (and a chain of them) moves to the next column or page
|
|
218
|
+
* when the block after it would start there, unless it already starts its column or the move
|
|
219
|
+
* would not bring them together (then the flow stays as it was).
|
|
220
|
+
* - Sections (`BlockBox.section`): a new one starts as its `start` says, an even or odd page
|
|
221
|
+
* section after a blank page when the page's number asks for one. A continuous one with the
|
|
222
|
+
* same page opens a band below the last; each band has its section's columns. A band of a
|
|
223
|
+
* `balanced` section ends with its columns level: its last page's columns take the shortest
|
|
224
|
+
* height that holds them.
|
|
225
|
+
*
|
|
226
|
+
* - Footnotes (ADR-0007, on Word's PDFs): a line with a reference goes on a page only if it, the
|
|
227
|
+
* separator and the note's first line fit. The note then takes the room down to the text, up to
|
|
228
|
+
* all of it, so a long one fills the page; the rest is carried to the foot of the next page,
|
|
229
|
+
* under the continuation separator, before any text is placed there, as much as fits. Lines a widow, orphan or keep rule takes back take their notes with them. In a
|
|
230
|
+
* section with columns the notes are set in those columns across the foot of the band, whichever
|
|
231
|
+
* column refers to them, and every column's text ends above them.
|
|
232
|
+
* - Endnotes (`BlockBox.endnotes`) follow their block in the text, over as many pages as they need.
|
|
88
233
|
*
|
|
89
234
|
* Spacing follows CSS inside a padded page: the first block keeps its top margin, adjacent
|
|
90
235
|
* margins collapse to the larger, and the last block's bottom margin stays inside the page
|
|
@@ -106,6 +251,8 @@ export function flow(
|
|
|
106
251
|
export interface FlowResult {
|
|
107
252
|
fragments: Fragment[][];
|
|
108
253
|
states: FlowState[];
|
|
254
|
+
/** The space above each block's first fragment (what the renderers write as its margin). */
|
|
255
|
+
gaps: number[];
|
|
109
256
|
}
|
|
110
257
|
|
|
111
258
|
/** What an incremental flow may reuse from the previous one. */
|
|
@@ -120,11 +267,16 @@ export interface Reuse {
|
|
|
120
267
|
previousIndex(i: number): number;
|
|
121
268
|
}
|
|
122
269
|
|
|
270
|
+
/** A fragment's place in reading order: its page, band and column. */
|
|
271
|
+
const slot = (f: Fragment | undefined) =>
|
|
272
|
+
f ? f.page * 1_000_000 + (f.band ?? 0) * 1000 + (f.column ?? 0) : 0;
|
|
273
|
+
|
|
123
274
|
/**
|
|
124
275
|
* `flow`, reusing a previous one: it starts at `reuse.from` (backed up over a chain of
|
|
125
|
-
* keep-with-next blocks, whose placement depends on what follows
|
|
126
|
-
*
|
|
127
|
-
*
|
|
276
|
+
* keep-with-next blocks, whose placement depends on what follows, and to the start of a balanced
|
|
277
|
+
* section, whose columns depend on all of it) with the previous state there, and once the state
|
|
278
|
+
* before an unchanged block equals the previous state before it, the rest is the previous flow's.
|
|
279
|
+
* The result is the same as a full flow.
|
|
128
280
|
*/
|
|
129
281
|
export function flowFrom(
|
|
130
282
|
boxes: readonly BlockBox[],
|
|
@@ -133,172 +285,857 @@ export function flowFrom(
|
|
|
133
285
|
reuse?: Reuse,
|
|
134
286
|
): FlowResult {
|
|
135
287
|
let start = reuse ? Math.min(reuse.from, boxes.length, reuse.previous.fragments.length) : 0;
|
|
136
|
-
|
|
288
|
+
const balancedAt = (i: number) => boxes[i]?.section?.balanced === true;
|
|
289
|
+
const sameSection = (i: number) => boxes[i]?.section === boxes[i - 1]?.section;
|
|
290
|
+
for (;;) {
|
|
291
|
+
if (start > 0 && boxes[start - 1]?.keepWithNext) start--;
|
|
292
|
+
else if (start > 0 && start < boxes.length && balancedAt(start) && sameSection(start)) start--;
|
|
293
|
+
else break;
|
|
294
|
+
}
|
|
137
295
|
const result: Fragment[][] = reuse ? reuse.previous.fragments.slice(0, start) : [];
|
|
138
296
|
const snapshots: FlowState[] = reuse ? reuse.previous.states.slice(0, start) : [];
|
|
297
|
+
const gaps: number[] = reuse ? reuse.previous.gaps.slice(0, start) : [];
|
|
139
298
|
let state: FlowState = (
|
|
140
|
-
reuse
|
|
299
|
+
reuse?.previous.states[start]
|
|
141
300
|
? { ...reuse.previous.states[start] }
|
|
142
|
-
: {
|
|
301
|
+
: {
|
|
302
|
+
page: 0,
|
|
303
|
+
number: boxes[0]?.section?.pageStart ?? 1,
|
|
304
|
+
sectionPage: 0,
|
|
305
|
+
band: 0,
|
|
306
|
+
bandTop: 0,
|
|
307
|
+
lead: 0,
|
|
308
|
+
column: 0,
|
|
309
|
+
used: 0,
|
|
310
|
+
bandBottom: 0,
|
|
311
|
+
previousMargin: 0,
|
|
312
|
+
onPage: 0,
|
|
313
|
+
breakPending: false,
|
|
314
|
+
columnPending: false,
|
|
315
|
+
forcedPage: true,
|
|
316
|
+
owner: null,
|
|
317
|
+
notes: 0,
|
|
318
|
+
noteParts: [],
|
|
319
|
+
pageNotes: 0,
|
|
320
|
+
carry: [],
|
|
321
|
+
reserves: {},
|
|
322
|
+
}
|
|
143
323
|
) as FlowState;
|
|
144
|
-
/** Blocks that start a
|
|
324
|
+
/** Blocks that start a column because the keep-with-next rule moved them. */
|
|
145
325
|
const breakBefore = new Set<number>();
|
|
146
326
|
const word = rules.spacing === "word";
|
|
327
|
+
/** While a section is balanced: the page whose columns are limited, and to what height. */
|
|
328
|
+
let balancing: { page: number; height: number } | null = null;
|
|
329
|
+
/** Note pieces placed and not yet held by a fragment. */
|
|
330
|
+
let pending: NotePiece[] = [];
|
|
331
|
+
/** Asked for by a later column: the band is flowed again with this kept at its foot. */
|
|
332
|
+
let retry: { key: string; height: number } | null = null;
|
|
333
|
+
const bandKey = () => `${state.page}|${state.band}`;
|
|
334
|
+
const reserved = () => state.reserves[bandKey()] ?? 0;
|
|
335
|
+
|
|
147
336
|
const newPage = (forced = false) => {
|
|
148
337
|
state.page++;
|
|
338
|
+
state.number++;
|
|
339
|
+
state.band = 0;
|
|
340
|
+
state.bandTop = 0;
|
|
341
|
+
state.lead = 0;
|
|
342
|
+
state.column = 0;
|
|
149
343
|
state.used = 0;
|
|
344
|
+
state.bandBottom = 0;
|
|
150
345
|
state.onPage = 0;
|
|
151
346
|
state.forcedPage = forced;
|
|
347
|
+
state.owner = null;
|
|
348
|
+
state.notes = 0;
|
|
349
|
+
state.noteParts = [];
|
|
350
|
+
state.pageNotes = 0;
|
|
351
|
+
// What was kept on earlier pages is done with.
|
|
352
|
+
if (Object.keys(state.reserves).length)
|
|
353
|
+
state.reserves = Object.fromEntries(
|
|
354
|
+
Object.entries(state.reserves).filter(([key]) => Number.parseInt(key, 10) >= state.page),
|
|
355
|
+
);
|
|
356
|
+
};
|
|
357
|
+
/** The next column of the band, or the next page after its last. */
|
|
358
|
+
const nextColumn = (box: BlockBox, forced = false) => {
|
|
359
|
+
if (state.column + 1 >= (box.section?.columns ?? 1)) {
|
|
360
|
+
newPage(forced);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
state.bandBottom = Math.max(state.bandBottom, state.used);
|
|
364
|
+
state.column++;
|
|
365
|
+
state.used = 0;
|
|
366
|
+
state.lead = 0;
|
|
367
|
+
state.onPage = 0;
|
|
368
|
+
state.forcedPage = forced;
|
|
369
|
+
};
|
|
370
|
+
/** The height a block may reach in the column, from the band's top. */
|
|
371
|
+
const room = (box: BlockBox) => pageRoom(box) - Math.max(state.notes, reserved());
|
|
372
|
+
/** The height a block may reach in the column, whatever notes take. */
|
|
373
|
+
const pageRoom = (box: BlockBox) => {
|
|
374
|
+
const section = state.owner?.section ?? box.section;
|
|
375
|
+
const page =
|
|
376
|
+
(section
|
|
377
|
+
? section.box({
|
|
378
|
+
index: state.page,
|
|
379
|
+
number: state.number,
|
|
380
|
+
first: state.owner?.first ?? state.page === state.sectionPage,
|
|
381
|
+
}).height
|
|
382
|
+
: (box.pageHeight ?? flowHeight)) -
|
|
383
|
+
state.bandTop -
|
|
384
|
+
state.pageNotes;
|
|
385
|
+
return balancing?.page === state.page ? Math.min(page, balancing.height) : page;
|
|
386
|
+
};
|
|
387
|
+
const piece = (note: FlowNote, fromLine: number, toLine: number): NotePiece => ({
|
|
388
|
+
id: note.id,
|
|
389
|
+
page: state.page,
|
|
390
|
+
...(state.band ? { band: state.band } : {}),
|
|
391
|
+
...(state.column ? { column: state.column } : {}),
|
|
392
|
+
fromLine,
|
|
393
|
+
toLine,
|
|
394
|
+
});
|
|
395
|
+
/** A piece of the notes at a band's foot: they are the band's, not one column's. */
|
|
396
|
+
const footPiece = (note: FlowNote, fromLine: number, toLine: number): NotePiece => ({
|
|
397
|
+
id: note.id,
|
|
398
|
+
page: state.page,
|
|
399
|
+
...(state.band ? { band: state.band } : {}),
|
|
400
|
+
fromLine,
|
|
401
|
+
toLine,
|
|
402
|
+
});
|
|
403
|
+
/** How much of an endnote, from line `from`, fits in `free`. */
|
|
404
|
+
const linesIn = (note: FlowNote, from: number, free: number) => {
|
|
405
|
+
const lines = note.lines(state.column);
|
|
406
|
+
const top = from ? (lines[from - 1] as number) : 0;
|
|
407
|
+
let to = from;
|
|
408
|
+
while (to < lines.length && (lines[to] as number) - top <= free + SLACK) to++;
|
|
409
|
+
return { to, height: to > from ? (lines[to - 1] as number) - top : 0, all: lines.length };
|
|
410
|
+
};
|
|
411
|
+
/** How much of a footnote fits in `free` beside the notes `parts` of the band's foot. */
|
|
412
|
+
const fitNote = (
|
|
413
|
+
note: FlowNote,
|
|
414
|
+
from: number,
|
|
415
|
+
free: number,
|
|
416
|
+
parts: readonly number[],
|
|
417
|
+
columns: number,
|
|
418
|
+
) => {
|
|
419
|
+
const lines = note.lines(0);
|
|
420
|
+
const top = from ? (lines[from - 1] as number) : 0;
|
|
421
|
+
const beside = columns > 1 ? 0 : notesHeight(parts, 1);
|
|
422
|
+
const height = (to: number) => (lines[to] as number) - top;
|
|
423
|
+
let to = from;
|
|
424
|
+
while (
|
|
425
|
+
to < lines.length &&
|
|
426
|
+
(columns > 1 ? notesHeight([...parts, height(to)], columns) : beside + height(to)) <=
|
|
427
|
+
free + SLACK
|
|
428
|
+
)
|
|
429
|
+
to++;
|
|
430
|
+
return { to, height: to > from ? height(to - 1) : 0, all: lines.length };
|
|
431
|
+
};
|
|
432
|
+
const noteHeight = (note: FlowNote) => note.lines(0).at(-1) ?? 0;
|
|
433
|
+
const separatorPiece = (note: FlowNote) => footPiece(note, 0, note.lines(0).length);
|
|
434
|
+
/**
|
|
435
|
+
* On a page or band with no notes yet: what is carried over goes to its foot first, as much as
|
|
436
|
+
* fits, before any text (Word's PDFs: a long note takes whole pages). `force`: a page with
|
|
437
|
+
* nothing else on it takes at least a line.
|
|
438
|
+
*/
|
|
439
|
+
const placeCarried = (box: BlockBox, force = false) => {
|
|
440
|
+
if (!state.carry.length || state.notes || state.onPage || !rules.notes) return;
|
|
441
|
+
const { continuation } = rules.notes;
|
|
442
|
+
const columns = box.section?.columns ?? 1;
|
|
443
|
+
const above = noteHeight(continuation);
|
|
444
|
+
const free = pageRoom(box) - state.used - above;
|
|
445
|
+
const placed: NotePiece[] = [];
|
|
446
|
+
const left: FlowState["carry"][number][] = [];
|
|
447
|
+
let parts: readonly number[] = [];
|
|
448
|
+
for (const item of state.carry) {
|
|
449
|
+
if (left.length) {
|
|
450
|
+
left.push(item);
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
let fit = fitNote(item.note, item.from, free, parts, columns);
|
|
454
|
+
if (fit.to === item.from && force && !placed.length)
|
|
455
|
+
fit = fitNote(item.note, item.from, lineHeightOf(item), parts, 1);
|
|
456
|
+
if (fit.to > item.from) {
|
|
457
|
+
placed.push(footPiece(item.note, item.from, fit.to));
|
|
458
|
+
parts = [...parts, fit.height];
|
|
459
|
+
}
|
|
460
|
+
if (fit.to < fit.all) left.push({ note: item.note, from: fit.to });
|
|
461
|
+
}
|
|
462
|
+
if (!placed.length) return;
|
|
463
|
+
pending.push(separatorPiece(continuation), ...placed);
|
|
464
|
+
state.notes = above + notesHeight(parts, columns);
|
|
465
|
+
state.noteParts = parts;
|
|
466
|
+
state.carry = left;
|
|
467
|
+
};
|
|
468
|
+
/** The height of the next line of a carried note. */
|
|
469
|
+
const lineHeightOf = ({ note, from }: FlowState["carry"][number]) => {
|
|
470
|
+
const lines = note.lines(0);
|
|
471
|
+
return (lines[from] as number) - (from ? (lines[from - 1] as number) : 0);
|
|
472
|
+
};
|
|
473
|
+
/** Whether what does not fit here can go elsewhere: not from an empty last column at the top. */
|
|
474
|
+
const canMove = (box: BlockBox) =>
|
|
475
|
+
state.onPage > 0 ||
|
|
476
|
+
state.notes > 0 ||
|
|
477
|
+
state.bandTop > 0 ||
|
|
478
|
+
state.column + 1 < (box.section?.columns ?? 1);
|
|
479
|
+
|
|
480
|
+
/** Moves to where `section` starts; the space above a new band when it opens one. */
|
|
481
|
+
const enterSection = (section: FlowSection, previous: FlowSection | undefined) => {
|
|
482
|
+
const pageBroken = state.breakPending;
|
|
483
|
+
state.breakPending = false;
|
|
484
|
+
state.columnPending = false;
|
|
485
|
+
// Word's PDFs: notes on the page and a continuous break into other columns start a page.
|
|
486
|
+
const notesAbove =
|
|
487
|
+
(state.notes > 0 || state.pageNotes > 0) && section.columns !== previous?.columns;
|
|
488
|
+
const samePage = !previous || (section.samePage(previous) && !notesAbove);
|
|
489
|
+
if (!pageBroken && samePage && section.start === "continuous") {
|
|
490
|
+
state.owner ??= {
|
|
491
|
+
section: previous ?? section,
|
|
492
|
+
first: state.page === state.sectionPage,
|
|
493
|
+
};
|
|
494
|
+
const lead = state.previousMargin;
|
|
495
|
+
state.bandTop += Math.max(state.bandBottom, state.used) + lead;
|
|
496
|
+
state.lead = lead;
|
|
497
|
+
state.band++;
|
|
498
|
+
state.column = 0;
|
|
499
|
+
state.used = 0;
|
|
500
|
+
state.bandBottom = 0;
|
|
501
|
+
state.onPage = 0;
|
|
502
|
+
state.forcedPage = true;
|
|
503
|
+
state.sectionPage = state.page;
|
|
504
|
+
state.pageNotes += state.notes;
|
|
505
|
+
state.notes = 0;
|
|
506
|
+
state.noteParts = [];
|
|
507
|
+
return lead;
|
|
508
|
+
}
|
|
509
|
+
if (
|
|
510
|
+
!pageBroken &&
|
|
511
|
+
samePage &&
|
|
512
|
+
section.start === "nextColumn" &&
|
|
513
|
+
section.columns === previous?.columns &&
|
|
514
|
+
state.column + 1 < section.columns
|
|
515
|
+
) {
|
|
516
|
+
nextColumn({ section } as BlockBox, true);
|
|
517
|
+
return undefined;
|
|
518
|
+
}
|
|
519
|
+
if (state.onPage || state.used || state.column || state.band) newPage(true);
|
|
520
|
+
else state.forcedPage = true;
|
|
521
|
+
const wrongKind = (number: number) =>
|
|
522
|
+
(section.start === "oddPage" && number % 2 === 0) ||
|
|
523
|
+
(section.start === "evenPage" && number % 2 === 1);
|
|
524
|
+
// Word's even and odd page sections (its PDFs, every kind, start and page before): a blank
|
|
525
|
+
// page when the number the pages have reached is the wrong kind, unless the section restarts
|
|
526
|
+
// its numbers in a document without even and odd headers. A restart then takes its start, or
|
|
527
|
+
// the next number when that is the wrong kind.
|
|
528
|
+
if (wrongKind(state.number) && (section.pageStart === undefined || rules.evenAndOddHeaders))
|
|
529
|
+
newPage(true);
|
|
530
|
+
if (section.pageStart !== undefined)
|
|
531
|
+
state.number = section.pageStart + (wrongKind(section.pageStart) ? 1 : 0);
|
|
532
|
+
state.sectionPage = state.page;
|
|
533
|
+
return undefined;
|
|
152
534
|
};
|
|
153
535
|
|
|
154
536
|
const place = (box: BlockBox, index: number): Fragment[] => {
|
|
155
|
-
const
|
|
156
|
-
if (
|
|
537
|
+
const fragments = placeBlock(box, index);
|
|
538
|
+
if (box.endnotes?.length) placeEndnotes(box, fragments);
|
|
539
|
+
return fragments;
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
/** A block's endnotes, after it in the text; lines that do not fit go on to the next column. */
|
|
543
|
+
const placeEndnotes = (box: BlockBox, fragments: Fragment[]) => {
|
|
544
|
+
let lead: number | undefined = state.onPage ? state.previousMargin : 0;
|
|
545
|
+
for (const note of box.endnotes ?? []) {
|
|
546
|
+
let from = 0;
|
|
547
|
+
for (;;) {
|
|
548
|
+
const lines = note.lines(state.column);
|
|
549
|
+
if (from >= lines.length) break;
|
|
550
|
+
const top = from ? (lines[from - 1] as number) : 0;
|
|
551
|
+
placeCarried(box);
|
|
552
|
+
const gap = state.onPage ? (lead ?? 0) : 0;
|
|
553
|
+
let fit = linesIn(note, from, room(box) - state.used - gap);
|
|
554
|
+
if (fit.to === from) {
|
|
555
|
+
if (canMove(box)) {
|
|
556
|
+
nextColumn(box);
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
fit = { ...fit, to: from + 1, height: (lines[from] as number) - top };
|
|
560
|
+
}
|
|
561
|
+
const part = piece(note, from, fit.to);
|
|
562
|
+
part.end = true;
|
|
563
|
+
if (gap) part.lead = gap;
|
|
564
|
+
pending.push(part);
|
|
565
|
+
lead = undefined;
|
|
566
|
+
state.used += gap + fit.height;
|
|
567
|
+
state.onPage++;
|
|
568
|
+
from = fit.to;
|
|
569
|
+
if (from < lines.length) nextColumn(box);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
state.previousMargin = 0;
|
|
573
|
+
const last = fragments[fragments.length - 1];
|
|
574
|
+
if (last && pending.length) last.notes = [...(last.notes ?? []), ...pending];
|
|
575
|
+
if (last) pending = [];
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
const placeBlock = (box: BlockBox, index: number): Fragment[] => {
|
|
579
|
+
const previous = boxes[index - 1];
|
|
580
|
+
let lead: number | undefined;
|
|
581
|
+
if (index > 0 && box.section && previous?.section !== box.section)
|
|
582
|
+
lead = enterSection(box.section, previous?.section);
|
|
583
|
+
else if (state.breakPending && state.onPage) newPage(true);
|
|
584
|
+
else if (state.columnPending) nextColumn(box, true);
|
|
585
|
+
else if (breakBefore.has(index) && state.onPage) nextColumn(box);
|
|
157
586
|
state.breakPending = false;
|
|
587
|
+
state.columnPending = false;
|
|
588
|
+
const leadPage = state.page;
|
|
589
|
+
const at = (fromLine: number, toLine: number): Fragment => {
|
|
590
|
+
const fragment: Fragment = { page: state.page, fromLine, toLine };
|
|
591
|
+
if (state.band) fragment.band = state.band;
|
|
592
|
+
if (state.column) fragment.column = state.column;
|
|
593
|
+
// Moved on to the next page, the block opens no band below anything.
|
|
594
|
+
if (lead !== undefined && state.page === leadPage) fragment.lead = lead;
|
|
595
|
+
lead = undefined;
|
|
596
|
+
if (pending.length) {
|
|
597
|
+
fragment.notes = pending;
|
|
598
|
+
pending = [];
|
|
599
|
+
}
|
|
600
|
+
return fragment;
|
|
601
|
+
};
|
|
602
|
+
if (box.sectionMark && state.onPage) {
|
|
603
|
+
gaps[index] = 0;
|
|
604
|
+
return [{ ...at(0, Infinity), mark: true }];
|
|
605
|
+
}
|
|
158
606
|
const above = state.previousMargin;
|
|
159
607
|
state.previousMargin = box.marginBottom;
|
|
608
|
+
const adding = word && rules.addSpacing === true;
|
|
160
609
|
const gap = () => {
|
|
161
|
-
if (!state.onPage)
|
|
162
|
-
|
|
610
|
+
if (!state.onPage) {
|
|
611
|
+
if (word && !state.forcedPage) return 0;
|
|
612
|
+
return adding ? box.marginTop : Math.max(0, box.marginTop - state.lead);
|
|
613
|
+
}
|
|
614
|
+
return adding ? above + box.marginTop : Math.max(above, box.marginTop);
|
|
163
615
|
};
|
|
164
616
|
// The bottom margin that must fit on the page (Word lets the space after hang).
|
|
165
617
|
const below = word ? 0 : box.marginBottom;
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
618
|
+
const pageBreaks = box.breaksAfter?.(state.column) ?? [];
|
|
619
|
+
const columnBreaks = box.columnBreaksAfter?.(state.column) ?? [];
|
|
620
|
+
const forced = pageBreaks.length + columnBreaks.length > 0;
|
|
621
|
+
let measured: Pick<BlockBox, "height" | "lines"> = box.inColumn?.(state.column) ?? box;
|
|
622
|
+
/** The notes referred to on each line, in the column it is in now. */
|
|
623
|
+
const referred = () => {
|
|
624
|
+
const byLine = new Map<number, FlowNote[]>();
|
|
625
|
+
for (const { line, note } of box.notes?.(state.column) ?? [])
|
|
626
|
+
byLine.set(line, [...(byLine.get(line) ?? []), note]);
|
|
627
|
+
return byLine;
|
|
628
|
+
};
|
|
629
|
+
let notesOf = referred();
|
|
630
|
+
if (state.carry.length && !state.onPage) placeCarried(box);
|
|
631
|
+
if (
|
|
632
|
+
!forced &&
|
|
633
|
+
!notesOf.size &&
|
|
634
|
+
state.used + gap() + measured.height + below <= room(box) + SLACK
|
|
635
|
+
) {
|
|
636
|
+
const space = gap();
|
|
637
|
+
gaps[index] = space;
|
|
638
|
+
state.used += space + measured.height;
|
|
169
639
|
state.onPage++;
|
|
170
|
-
return [
|
|
640
|
+
return [at(0, Infinity)];
|
|
171
641
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const
|
|
642
|
+
let lines = measured.lines();
|
|
643
|
+
let count = lines.length;
|
|
644
|
+
const inRange = (list: readonly number[]) =>
|
|
645
|
+
new Set(list.filter((at) => at >= 0 && at < count));
|
|
646
|
+
let pageAfter = inRange(pageBreaks);
|
|
647
|
+
let columnAfter = inRange(columnBreaks);
|
|
175
648
|
const fragments: Fragment[] = [];
|
|
176
649
|
let line = 0;
|
|
650
|
+
/** The explicit breaks passed so far. */
|
|
651
|
+
let passed = 0;
|
|
652
|
+
/** After a move: `measured` is the new column's, and `line` where the text has got to. */
|
|
653
|
+
const remeasure = (broken = false) => {
|
|
654
|
+
if (!box.inColumn) return;
|
|
655
|
+
const next = box.inColumn(state.column);
|
|
656
|
+
if (next === measured) return;
|
|
657
|
+
const nextLines = next.lines();
|
|
658
|
+
// shortcut: the same share of the text is taken to start the same line in another
|
|
659
|
+
// width (a line either way); a word-exact tail needs the text in the flow's terms.
|
|
660
|
+
const share = line ? (lines[line - 1] as number) / (measured.height || 1) : 0;
|
|
661
|
+
measured = next;
|
|
662
|
+
lines = nextLines;
|
|
663
|
+
count = lines.length;
|
|
664
|
+
// Breaks are lines of this width.
|
|
665
|
+
pageAfter = inRange(box.breaksAfter?.(state.column) ?? []);
|
|
666
|
+
columnAfter = inRange(box.columnBreaksAfter?.(state.column) ?? []);
|
|
667
|
+
if (line > 0) {
|
|
668
|
+
// After an explicit break the text goes on right after the same break.
|
|
669
|
+
const after = [...pageAfter, ...columnAfter].sort((a, b) => a - b)[passed - 1];
|
|
670
|
+
let mapped = lines.findIndex((bottom) => bottom / (next.height || 1) > share + 1e-6);
|
|
671
|
+
if (mapped < 0) mapped = count - 1;
|
|
672
|
+
line = broken && after !== undefined ? after + 1 : Math.max(mapped, 1);
|
|
673
|
+
}
|
|
674
|
+
// Nothing left in this width (all of it fits one wider line): the last fragment ends it.
|
|
675
|
+
if (line >= count) {
|
|
676
|
+
const last = fragments.at(-1);
|
|
677
|
+
if (last) last.toLine = Infinity;
|
|
678
|
+
}
|
|
679
|
+
};
|
|
177
680
|
while (line < count) {
|
|
178
681
|
// The lines up to the next explicit break (or the end) can be split freely.
|
|
179
682
|
let segmentEnd = line;
|
|
180
|
-
while (segmentEnd < count && !
|
|
683
|
+
while (segmentEnd < count && !pageAfter.has(segmentEnd) && !columnAfter.has(segmentEnd))
|
|
684
|
+
segmentEnd++;
|
|
181
685
|
segmentEnd = Math.min(segmentEnd + 1, count);
|
|
182
686
|
const top = line ? (lines[line - 1] as number) : 0;
|
|
183
687
|
// Only the block's first fragment has the gap above it; the others repeat its header.
|
|
184
688
|
const spacing = line === 0 ? gap() : (box.repeat ?? 0);
|
|
689
|
+
notesOf = referred();
|
|
690
|
+
placeCarried(box);
|
|
691
|
+
const limit = pageRoom(box);
|
|
692
|
+
/**
|
|
693
|
+
* The footnotes of lines `line` to `to - 1` at the band's foot, each taking the room down
|
|
694
|
+
* to the text after its line; null when one's first line does not fit (`force`: it goes in).
|
|
695
|
+
* With columns the notes share the foot of them all: above them goes the tallest column.
|
|
696
|
+
*/
|
|
697
|
+
// A block kept whole is one line to the flow already.
|
|
698
|
+
const keepAll = count === 1;
|
|
699
|
+
const planNotes = (to: number, force = false) => {
|
|
700
|
+
const plan = {
|
|
701
|
+
notes: state.notes,
|
|
702
|
+
parts: state.noteParts,
|
|
703
|
+
carry: state.carry,
|
|
704
|
+
pieces: [] as NotePiece[],
|
|
705
|
+
};
|
|
706
|
+
if (!notesOf.size || !rules.notes) return plan;
|
|
707
|
+
const columns = box.section?.columns ?? 1;
|
|
708
|
+
for (let at = line; at < to; at++)
|
|
709
|
+
for (const note of notesOf.get(at) ?? []) {
|
|
710
|
+
// Behind a note already cut, it waits its turn.
|
|
711
|
+
if (plan.carry.length) {
|
|
712
|
+
plan.carry = [...plan.carry, { note, from: 0 }];
|
|
713
|
+
continue;
|
|
714
|
+
}
|
|
715
|
+
const { separator } = rules.notes;
|
|
716
|
+
const first = !plan.notes;
|
|
717
|
+
const above = first
|
|
718
|
+
? noteHeight(separator)
|
|
719
|
+
: plan.notes - notesHeight(plan.parts, columns);
|
|
720
|
+
// Word's PDFs: the lines that the widow and orphan rules keep with this one stay above
|
|
721
|
+
// the note, and the paragraph's space after stays between its last line and the notes.
|
|
722
|
+
let kept = at + 1;
|
|
723
|
+
if (!box.anyLine && !keepAll) {
|
|
724
|
+
kept = Math.max(kept, line + rules.orphans);
|
|
725
|
+
if (count - kept < rules.widows) kept = count;
|
|
726
|
+
kept = Math.min(kept, segmentEnd);
|
|
727
|
+
}
|
|
728
|
+
const after = kept === count ? box.marginBottom : 0;
|
|
729
|
+
const own = state.used + spacing + (lines[kept - 1] as number) - top + after;
|
|
730
|
+
const text = Math.max(own, columns > 1 && state.column ? state.bandBottom : 0);
|
|
731
|
+
let fit = fitNote(note, 0, limit - text - above, plan.parts, columns);
|
|
732
|
+
if (
|
|
733
|
+
columns > 1 &&
|
|
734
|
+
state.column &&
|
|
735
|
+
!force &&
|
|
736
|
+
(!fit.to || (fit.to < fit.all && text > own))
|
|
737
|
+
) {
|
|
738
|
+
// A later column's note, cut short by what the earlier columns hold: in Word they
|
|
739
|
+
// end above it. They are flowed again with its room kept, whole if the page can.
|
|
740
|
+
const lines = note.lines(0);
|
|
741
|
+
const whole = above + notesHeight([...plan.parts, lines.at(-1) as number], columns);
|
|
742
|
+
const one = above + notesHeight([...plan.parts, lines[0] as number], columns);
|
|
743
|
+
const height = whole < limit / 2 ? whole : one;
|
|
744
|
+
if (height > reserved() + SLACK && height < limit) retry = { key: bandKey(), height };
|
|
745
|
+
}
|
|
746
|
+
if (!fit.to) {
|
|
747
|
+
if (!force) return null;
|
|
748
|
+
fit = fitNote(note, 0, lineHeightOf({ note, from: 0 }), [], 1);
|
|
749
|
+
}
|
|
750
|
+
if (first) plan.pieces.push(separatorPiece(separator));
|
|
751
|
+
plan.pieces.push(footPiece(note, 0, fit.to));
|
|
752
|
+
plan.parts = [...plan.parts, fit.height];
|
|
753
|
+
plan.notes = above + notesHeight(plan.parts, columns);
|
|
754
|
+
if (fit.to < fit.all) plan.carry = [{ note, from: fit.to }];
|
|
755
|
+
}
|
|
756
|
+
return plan;
|
|
757
|
+
};
|
|
185
758
|
let end = line;
|
|
186
759
|
while (end < segmentEnd) {
|
|
187
760
|
const bottom = (lines[end] as number) - top + (end + 1 === count ? below : 0);
|
|
188
|
-
|
|
761
|
+
const plan = planNotes(end + 1);
|
|
762
|
+
if (
|
|
763
|
+
!plan ||
|
|
764
|
+
state.used + spacing + bottom + Math.max(plan.notes, reserved()) > limit + SLACK
|
|
765
|
+
)
|
|
766
|
+
break;
|
|
189
767
|
end++;
|
|
190
768
|
}
|
|
191
|
-
if (end < segmentEnd && end > line) {
|
|
769
|
+
if (end < segmentEnd && end > line && !box.anyLine) {
|
|
192
770
|
// A natural split: keep `widows` lines for the next page and `orphans` on this one.
|
|
193
771
|
const kept = Math.min(end, segmentEnd - rules.widows);
|
|
194
772
|
if (kept - line >= rules.orphans) end = kept;
|
|
195
|
-
else if (
|
|
773
|
+
else if (canMove(box)) end = line;
|
|
196
774
|
}
|
|
197
775
|
if (end === line) {
|
|
198
|
-
if (
|
|
199
|
-
|
|
776
|
+
if (canMove(box)) {
|
|
777
|
+
nextColumn(box);
|
|
778
|
+
remeasure();
|
|
200
779
|
continue;
|
|
201
780
|
}
|
|
202
781
|
end = line + 1; // a line taller than a page is placed anyway
|
|
203
782
|
}
|
|
204
|
-
|
|
783
|
+
// What the lines kept refer to: lines taken back take their notes with them.
|
|
784
|
+
const kept = planNotes(end, true);
|
|
785
|
+
if (kept) {
|
|
786
|
+
state.notes = kept.notes;
|
|
787
|
+
state.noteParts = kept.parts;
|
|
788
|
+
state.carry = kept.carry;
|
|
789
|
+
pending.push(...kept.pieces);
|
|
790
|
+
}
|
|
791
|
+
if (line === 0) gaps[index] = spacing;
|
|
792
|
+
fragments.push(at(line, end));
|
|
205
793
|
state.used += spacing + (lines[end - 1] as number) - top;
|
|
206
794
|
state.onPage++;
|
|
207
795
|
line = end;
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
796
|
+
const pageBroken = pageAfter.has(end - 1);
|
|
797
|
+
const columnBroken = columnAfter.has(end - 1);
|
|
798
|
+
if (line < count) {
|
|
799
|
+
if (pageBroken || columnBroken) passed++;
|
|
800
|
+
if (pageBroken) newPage();
|
|
801
|
+
else nextColumn(box, columnBroken);
|
|
802
|
+
remeasure(pageBroken || columnBroken);
|
|
803
|
+
} else if (pageBroken) state.breakPending = true;
|
|
804
|
+
else if (columnBroken) state.columnPending = true;
|
|
211
805
|
}
|
|
212
806
|
// Moved whole to a later page: not split.
|
|
213
807
|
const [only] = fragments;
|
|
214
|
-
if (fragments.length === 1 && only && !forced
|
|
808
|
+
if (fragments.length === 1 && only && !forced) only.toLine = Infinity;
|
|
215
809
|
return fragments;
|
|
216
810
|
};
|
|
217
811
|
|
|
218
812
|
/** The flow as it was before a keep-with-next move, by the block that triggered it. */
|
|
219
813
|
const attempts = new Map<
|
|
220
814
|
number,
|
|
221
|
-
{
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
// be unchanged too: had it been keep-with-next before the edit, the previous flow from
|
|
228
|
-
// here holds a decision about a chain that no longer exists.
|
|
229
|
-
const was =
|
|
230
|
-
reuse && index > start && !attempts.size && reuse.previousIndex(index - 1) >= 0
|
|
231
|
-
? reuse.previousIndex(index)
|
|
232
|
-
: -1;
|
|
233
|
-
const before = was >= 0 ? reuse?.previous.states[was] : undefined;
|
|
234
|
-
if (before && !boxes[index - 1]?.keepWithNext && sameState(before, state)) {
|
|
235
|
-
const previous = reuse as Reuse;
|
|
236
|
-
result.length = index;
|
|
237
|
-
snapshots.length = index;
|
|
238
|
-
result.push(...previous.previous.fragments.slice(was));
|
|
239
|
-
snapshots.push(...previous.previous.states.slice(was, -1));
|
|
240
|
-
return {
|
|
241
|
-
fragments: result,
|
|
242
|
-
states: [...snapshots, { ...(previous.previous.states.at(-1) as FlowState) }],
|
|
243
|
-
};
|
|
815
|
+
{
|
|
816
|
+
first: number;
|
|
817
|
+
result: Fragment[][];
|
|
818
|
+
snapshots: FlowState[];
|
|
819
|
+
gaps: number[];
|
|
820
|
+
state: FlowState;
|
|
244
821
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
//
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
index
|
|
262
|
-
|
|
822
|
+
>();
|
|
823
|
+
|
|
824
|
+
/** Flows blocks `from` to `to`; a result when the rest could be taken from `reuse`. */
|
|
825
|
+
const run = (from: number, to: number): FlowResult | null => {
|
|
826
|
+
let index = from;
|
|
827
|
+
while (index < to) {
|
|
828
|
+
// Realigned with the previous flow: the rest is unchanged (no keep-with-next decision
|
|
829
|
+
// about this block, made with the block before it, is pending). The block before it must
|
|
830
|
+
// be unchanged too: had it been keep-with-next before the edit, the previous flow from
|
|
831
|
+
// here holds a decision about a chain that no longer exists. Inside a balanced section the
|
|
832
|
+
// columns depend on the whole section: only its start realigns.
|
|
833
|
+
const was =
|
|
834
|
+
reuse &&
|
|
835
|
+
!balancing &&
|
|
836
|
+
index > start &&
|
|
837
|
+
!attempts.size &&
|
|
838
|
+
reuse.previousIndex(index - 1) >= 0
|
|
839
|
+
? reuse.previousIndex(index)
|
|
840
|
+
: -1;
|
|
841
|
+
const before = was >= 0 ? reuse?.previous.states[was] : undefined;
|
|
842
|
+
if (
|
|
843
|
+
before &&
|
|
844
|
+
!boxes[index - 1]?.keepWithNext &&
|
|
845
|
+
!(balancedAt(index) && sameSection(index)) &&
|
|
846
|
+
sameState(before, state)
|
|
847
|
+
) {
|
|
848
|
+
const previous = (reuse as Reuse).previous;
|
|
849
|
+
result.length = index;
|
|
850
|
+
snapshots.length = index;
|
|
851
|
+
gaps.length = index;
|
|
852
|
+
result.push(...previous.fragments.slice(was));
|
|
853
|
+
snapshots.push(...previous.states.slice(was, -1));
|
|
854
|
+
gaps.push(...previous.gaps.slice(was));
|
|
855
|
+
return {
|
|
856
|
+
fragments: result,
|
|
857
|
+
states: [...snapshots, { ...(previous.states.at(-1) as FlowState) }],
|
|
858
|
+
gaps,
|
|
859
|
+
};
|
|
263
860
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
result: result.slice(first),
|
|
280
|
-
snapshots: snapshots.slice(first),
|
|
281
|
-
state: { ...state },
|
|
282
|
-
});
|
|
283
|
-
breakBefore.add(first);
|
|
284
|
-
state = { ...beforeFirst };
|
|
861
|
+
snapshots[index] = { ...state };
|
|
862
|
+
const fragments = place(boxes[index] as BlockBox, index);
|
|
863
|
+
if (retry) {
|
|
864
|
+
const { key, height } = retry;
|
|
865
|
+
retry = null;
|
|
866
|
+
const [page, band] = key.split("|").map(Number) as [number, number];
|
|
867
|
+
// Back to the first block on that band: it may have started on an earlier page.
|
|
868
|
+
let first = index;
|
|
869
|
+
while (
|
|
870
|
+
first > from &&
|
|
871
|
+
result[first - 1]?.some((f) => f.page === page && (f.band ?? 0) === band)
|
|
872
|
+
)
|
|
873
|
+
first--;
|
|
874
|
+
const before = snapshots[first] as FlowState;
|
|
875
|
+
state = { ...before, reserves: { ...before.reserves, [key]: height } };
|
|
285
876
|
result.length = first;
|
|
877
|
+
snapshots.length = first;
|
|
878
|
+
gaps.length = first;
|
|
879
|
+
attempts.clear();
|
|
880
|
+
for (const moved of breakBefore) if (moved >= first) breakBefore.delete(moved);
|
|
881
|
+
pending = [];
|
|
286
882
|
index = first;
|
|
287
883
|
continue;
|
|
288
884
|
}
|
|
885
|
+
result[index] = fragments;
|
|
886
|
+
const previous = result[index - 1];
|
|
887
|
+
const startsLater =
|
|
888
|
+
previous !== undefined && slot(fragments[0]) > slot(previous[previous.length - 1]);
|
|
889
|
+
const attempt = attempts.get(index);
|
|
890
|
+
if (attempt) {
|
|
891
|
+
attempts.delete(index);
|
|
892
|
+
// The move did not bring them together (the block does not fit after the heading on a
|
|
893
|
+
// page of its own either): keep the flow as it was.
|
|
894
|
+
if (startsLater) {
|
|
895
|
+
result.splice(attempt.first, result.length - attempt.first, ...attempt.result);
|
|
896
|
+
snapshots.splice(attempt.first, snapshots.length - attempt.first, ...attempt.snapshots);
|
|
897
|
+
gaps.splice(attempt.first, gaps.length - attempt.first, ...attempt.gaps);
|
|
898
|
+
state = attempt.state;
|
|
899
|
+
index++;
|
|
900
|
+
continue;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
if (startsLater && boxes[index - 1]?.keepWithNext && sameSection(index)) {
|
|
904
|
+
// The chain of keep-with-next blocks before this one, each whole in one column (keeping
|
|
905
|
+
// reaches no further back than the section: it starts where its `start` says).
|
|
906
|
+
let first = index - 1;
|
|
907
|
+
while (
|
|
908
|
+
first > 0 &&
|
|
909
|
+
sameSection(first) &&
|
|
910
|
+
boxes[first - 1]?.keepWithNext &&
|
|
911
|
+
result[first - 1]?.length === 1 &&
|
|
912
|
+
slot(result[first - 1]?.[0]) === slot(result[first]?.[0])
|
|
913
|
+
)
|
|
914
|
+
first--;
|
|
915
|
+
const beforeFirst = snapshots[first] as FlowState;
|
|
916
|
+
if (!breakBefore.has(first) && beforeFirst.onPage > 0 && result[first]?.length === 1) {
|
|
917
|
+
attempts.set(index, {
|
|
918
|
+
first,
|
|
919
|
+
result: result.slice(first),
|
|
920
|
+
snapshots: snapshots.slice(first),
|
|
921
|
+
gaps: gaps.slice(first),
|
|
922
|
+
state: { ...state },
|
|
923
|
+
});
|
|
924
|
+
breakBefore.add(first);
|
|
925
|
+
state = { ...beforeFirst };
|
|
926
|
+
result.length = first;
|
|
927
|
+
index = first;
|
|
928
|
+
continue;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
index++;
|
|
932
|
+
const section = boxes[index - 1]?.section;
|
|
933
|
+
if (
|
|
934
|
+
!balancing &&
|
|
935
|
+
section?.balanced &&
|
|
936
|
+
section.columns > 1 &&
|
|
937
|
+
boxes[index]?.section !== section
|
|
938
|
+
)
|
|
939
|
+
balance(index);
|
|
940
|
+
}
|
|
941
|
+
return null;
|
|
942
|
+
};
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* Levels the columns of the balanced section ending before block `end`: flows it again with
|
|
946
|
+
* its last page's columns limited, looking for the shortest height that keeps it on that page.
|
|
947
|
+
*/
|
|
948
|
+
const balance = (end: number) => {
|
|
949
|
+
const section = boxes[end - 1]?.section as FlowSection;
|
|
950
|
+
let first = end - 1;
|
|
951
|
+
while (first > 0 && boxes[first - 1]?.section === section) first--;
|
|
952
|
+
const from = snapshots[first] as FlowState;
|
|
953
|
+
const { page } = state;
|
|
954
|
+
let high = Math.max(state.bandBottom, state.used);
|
|
955
|
+
let low = high / section.columns - 1;
|
|
956
|
+
/** The section flowed with its last page's columns `height` tall: does it stay there? */
|
|
957
|
+
const attempt = (height: number) => {
|
|
958
|
+
balancing = { page, height };
|
|
959
|
+
state = { ...from };
|
|
960
|
+
result.length = first;
|
|
961
|
+
snapshots.length = first;
|
|
962
|
+
attempts.clear();
|
|
963
|
+
for (const moved of breakBefore) if (moved >= first) breakBefore.delete(moved);
|
|
964
|
+
run(first, end);
|
|
965
|
+
// The last column takes what it is given: a column over the limit is a failure too.
|
|
966
|
+
return state.page === page && Math.max(state.used, state.bandBottom) <= height + SLACK;
|
|
967
|
+
};
|
|
968
|
+
while (high - low > 0.5) {
|
|
969
|
+
const middle = (low + high) / 2;
|
|
970
|
+
if (attempt(middle)) high = middle;
|
|
971
|
+
else low = middle;
|
|
972
|
+
}
|
|
973
|
+
if (!attempt(high)) attempt(Number.POSITIVE_INFINITY);
|
|
974
|
+
balancing = null;
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
/** What is still carried after the last block goes on pages of its own. */
|
|
978
|
+
const finishNotes = () => {
|
|
979
|
+
const held = result[result.length - 1];
|
|
980
|
+
const last = held?.[held.length - 1];
|
|
981
|
+
const box = boxes[boxes.length - 1];
|
|
982
|
+
if (!held || !last || !box || (!state.carry.length && !last.notes?.some((p) => p.tail))) return;
|
|
983
|
+
const end = state;
|
|
984
|
+
state = { ...state };
|
|
985
|
+
while (state.carry.length) {
|
|
986
|
+
newPage();
|
|
987
|
+
placeCarried(box, true);
|
|
988
|
+
}
|
|
989
|
+
const notes = [
|
|
990
|
+
...(last.notes ?? []).filter((p) => !p.tail),
|
|
991
|
+
...pending.map((p) => ({ ...p, tail: true })),
|
|
992
|
+
];
|
|
993
|
+
pending = [];
|
|
994
|
+
state = end;
|
|
995
|
+
result[result.length - 1] = [...held.slice(0, -1), { ...last, notes }];
|
|
996
|
+
};
|
|
997
|
+
|
|
998
|
+
const reused = run(start, boxes.length);
|
|
999
|
+
if (reused) return reused;
|
|
1000
|
+
finishNotes();
|
|
1001
|
+
return {
|
|
1002
|
+
fragments: result,
|
|
1003
|
+
states: [...snapshots.slice(0, boxes.length), { ...state }],
|
|
1004
|
+
gaps: gaps.slice(0, boxes.length),
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
/**
|
|
1009
|
+
* Each page of a flow: its number, its first section and whether that section starts on it. A page
|
|
1010
|
+
* nothing is on (the blank page before an even or odd page section) has the next page's section.
|
|
1011
|
+
*/
|
|
1012
|
+
export function pagesOf(boxes: readonly BlockBox[], result: FlowResult): PageFacts[] {
|
|
1013
|
+
const pages: (PageFacts | undefined)[] = [];
|
|
1014
|
+
const owners: FlowSection[] = [];
|
|
1015
|
+
const seen = new Set<FlowSection>();
|
|
1016
|
+
const onPages = new Map<FlowSection, Set<number>>();
|
|
1017
|
+
result.fragments.forEach((fragments, i) => {
|
|
1018
|
+
const section = boxes[i]?.section as FlowSection;
|
|
1019
|
+
const after = result.states[i + 1] as FlowState;
|
|
1020
|
+
const own = onPages.get(section) ?? new Set<number>();
|
|
1021
|
+
onPages.set(section, own);
|
|
1022
|
+
const places = [...fragments, ...fragments.flatMap((fragment) => fragment.notes ?? [])];
|
|
1023
|
+
for (const { page } of places) {
|
|
1024
|
+
own.add(page);
|
|
1025
|
+
if (!pages[page]) {
|
|
1026
|
+
owners[page] = section;
|
|
1027
|
+
pages[page] = {
|
|
1028
|
+
index: page,
|
|
1029
|
+
// Pages count on from a block's first to its last: the number where it ended tells.
|
|
1030
|
+
number: after.number - (after.page - page),
|
|
1031
|
+
first: !seen.has(section),
|
|
1032
|
+
section: section.index ?? 0,
|
|
1033
|
+
sectionPages: 0,
|
|
1034
|
+
};
|
|
1035
|
+
}
|
|
1036
|
+
seen.add(section);
|
|
1037
|
+
}
|
|
1038
|
+
});
|
|
1039
|
+
pages.forEach((page, p) => {
|
|
1040
|
+
if (page) page.sectionPages = onPages.get(owners[p] as FlowSection)?.size ?? 0;
|
|
1041
|
+
});
|
|
1042
|
+
for (let p = pages.length - 1; p >= 0; p--) {
|
|
1043
|
+
const next = pages[p + 1] as PageFacts;
|
|
1044
|
+
pages[p] ??= { ...next, index: p, number: next.number - 1, first: false, sectionPages: 0 };
|
|
1045
|
+
}
|
|
1046
|
+
return pages as PageFacts[];
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* Notes of heights `parts` set in `columns` columns, as Word sets those of a section with columns
|
|
1051
|
+
* (its PDFs): whole, in order, each column as full as the height the set needs. That height and
|
|
1052
|
+
* the column of each. One column: their sum.
|
|
1053
|
+
*/
|
|
1054
|
+
export function arrangeNotes(
|
|
1055
|
+
parts: readonly number[],
|
|
1056
|
+
columns: number,
|
|
1057
|
+
): { height: number; columnOf: number[] } {
|
|
1058
|
+
const total = parts.reduce((sum, part) => sum + part, 0);
|
|
1059
|
+
if (columns <= 1 || parts.length <= 1) return { height: total, columnOf: parts.map(() => 0) };
|
|
1060
|
+
const pack = (height: number) => {
|
|
1061
|
+
const columnOf: number[] = [];
|
|
1062
|
+
let column = 0;
|
|
1063
|
+
let used = 0;
|
|
1064
|
+
let tallest = 0;
|
|
1065
|
+
for (const part of parts) {
|
|
1066
|
+
if (used && used + part > height + 0.01) {
|
|
1067
|
+
column++;
|
|
1068
|
+
used = 0;
|
|
1069
|
+
}
|
|
1070
|
+
used += part;
|
|
1071
|
+
tallest = Math.max(tallest, used);
|
|
1072
|
+
columnOf.push(column);
|
|
289
1073
|
}
|
|
290
|
-
|
|
1074
|
+
return column < columns ? { height: tallest, columnOf } : null;
|
|
1075
|
+
};
|
|
1076
|
+
let low = Math.max(...parts, total / columns);
|
|
1077
|
+
let best = pack(low);
|
|
1078
|
+
if (best) return best;
|
|
1079
|
+
let high = total;
|
|
1080
|
+
best = pack(high) as NonNullable<ReturnType<typeof pack>>;
|
|
1081
|
+
while (high - low > 0.25) {
|
|
1082
|
+
const middle = (low + high) / 2;
|
|
1083
|
+
const tried = pack(middle);
|
|
1084
|
+
if (tried) {
|
|
1085
|
+
best = tried;
|
|
1086
|
+
high = middle;
|
|
1087
|
+
} else low = middle;
|
|
291
1088
|
}
|
|
292
|
-
return
|
|
1089
|
+
return best;
|
|
293
1090
|
}
|
|
294
1091
|
|
|
1092
|
+
const notesHeight = (parts: readonly number[], columns: number) =>
|
|
1093
|
+
arrangeNotes(parts, columns).height;
|
|
1094
|
+
|
|
1095
|
+
/** The space after the last block of a column of a page, as flowed (what ends its text). */
|
|
1096
|
+
export function spaceAfterText(
|
|
1097
|
+
boxes: readonly BlockBox[],
|
|
1098
|
+
result: Pick<FlowResult, "fragments">,
|
|
1099
|
+
page: number,
|
|
1100
|
+
band: number,
|
|
1101
|
+
column: number,
|
|
1102
|
+
): number {
|
|
1103
|
+
for (let i = result.fragments.length - 1; i >= 0; i--)
|
|
1104
|
+
for (const f of result.fragments[i] ?? [])
|
|
1105
|
+
if (f.page === page && (f.band ?? 0) === band && (f.column ?? 0) === column)
|
|
1106
|
+
return boxes[i]?.marginBottom ?? 0;
|
|
1107
|
+
return 0;
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
/** Every note piece of a flow, in order. */
|
|
1111
|
+
export const notePieces = (result: Pick<FlowResult, "fragments">): NotePiece[] =>
|
|
1112
|
+
result.fragments.flatMap((fragments) => fragments.flatMap((f) => f.notes ?? []));
|
|
1113
|
+
|
|
295
1114
|
function sameState(a: FlowState, b: FlowState): boolean {
|
|
296
1115
|
return (
|
|
1116
|
+
a.notes === b.notes &&
|
|
1117
|
+
JSON.stringify(a.reserves) === JSON.stringify(b.reserves) &&
|
|
1118
|
+
a.noteParts.length === b.noteParts.length &&
|
|
1119
|
+
a.noteParts.every((part, i) => part === b.noteParts[i]) &&
|
|
1120
|
+
a.pageNotes === b.pageNotes &&
|
|
1121
|
+
a.carry.length === b.carry.length &&
|
|
1122
|
+
a.carry.every(
|
|
1123
|
+
(item, i) => item.note.id === b.carry[i]?.note.id && item.from === b.carry[i]?.from,
|
|
1124
|
+
) &&
|
|
297
1125
|
a.page === b.page &&
|
|
1126
|
+
a.number === b.number &&
|
|
1127
|
+
a.sectionPage === b.sectionPage &&
|
|
1128
|
+
a.band === b.band &&
|
|
1129
|
+
a.bandTop === b.bandTop &&
|
|
1130
|
+
a.lead === b.lead &&
|
|
1131
|
+
a.column === b.column &&
|
|
298
1132
|
a.used === b.used &&
|
|
1133
|
+
a.bandBottom === b.bandBottom &&
|
|
299
1134
|
a.previousMargin === b.previousMargin &&
|
|
300
1135
|
a.onPage === b.onPage &&
|
|
301
1136
|
a.breakPending === b.breakPending &&
|
|
302
|
-
a.
|
|
1137
|
+
a.columnPending === b.columnPending &&
|
|
1138
|
+
a.forcedPage === b.forcedPage &&
|
|
1139
|
+
a.owner === b.owner
|
|
303
1140
|
);
|
|
304
1141
|
}
|