@folijs/view 0.5.0 → 1.0.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 +194 -0
- package/README.md +1 -1
- package/dist/clipboard.d.ts +1 -49
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +65 -13
- package/dist/clipboard.js.map +1 -1
- package/dist/extension.d.ts +90 -0
- package/dist/extension.d.ts.map +1 -0
- package/dist/extension.js +152 -0
- package/dist/extension.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +1 -109
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +808 -103
- package/dist/layout/flow.js.map +1 -1
- package/dist/print.d.ts +6 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +35 -0
- package/dist/print.js.map +1 -0
- package/dist/render.d.ts +1 -24
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +4 -1
- package/dist/render.js.map +1 -1
- package/dist/view.d.ts +244 -64
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +1945 -215
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +0 -146
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +294 -59
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/columns.d.ts +2 -0
- package/dist/word/columns.d.ts.map +1 -0
- package/dist/word/columns.js +63 -0
- package/dist/word/columns.js.map +1 -0
- package/dist/word/controls.d.ts +2 -0
- package/dist/word/controls.d.ts.map +1 -0
- package/dist/word/controls.js +416 -0
- package/dist/word/controls.js.map +1 -0
- package/dist/word/edit.d.ts +1 -71
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +338 -29
- package/dist/word/edit.js.map +1 -1
- package/dist/word/matches.d.ts +2 -0
- package/dist/word/matches.d.ts.map +1 -0
- package/dist/word/matches.js +102 -0
- package/dist/word/matches.js.map +1 -0
- package/dist/word/notes.d.ts +2 -0
- package/dist/word/notes.d.ts.map +1 -0
- package/dist/word/notes.js +403 -0
- package/dist/word/notes.js.map +1 -0
- package/dist/word/pages.d.ts +30 -20
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +421 -229
- package/dist/word/pages.js.map +1 -1
- package/dist/word/regions.d.ts +2 -0
- package/dist/word/regions.d.ts.map +1 -0
- package/dist/word/regions.js +388 -0
- package/dist/word/regions.js.map +1 -0
- package/dist/word/table.d.ts +1 -78
- package/dist/word/table.d.ts.map +1 -1
- package/dist/word/table.js +13 -3
- package/dist/word/table.js.map +1 -1
- package/package.json +6 -3
- package/src/clipboard.ts +93 -20
- package/src/extension.ts +267 -0
- package/src/index.ts +10 -0
- package/src/layout/flow.ts +997 -116
- package/src/print.ts +55 -0
- package/src/render.ts +6 -1
- package/src/view.ts +2178 -245
- package/src/word/blocks.ts +339 -62
- package/src/word/columns.ts +81 -0
- package/src/word/controls.ts +457 -0
- package/src/word/edit.ts +381 -31
- package/src/word/matches.ts +104 -0
- package/src/word/notes.ts +464 -0
- package/src/word/pages.ts +518 -221
- package/src/word/regions.ts +488 -0
- package/src/word/table.ts +33 -8
package/dist/layout/flow.js
CHANGED
|
@@ -1,26 +1,46 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* CSS pixels per millimetre (CSS fixes 1in = 96px = 25.4mm).
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
2
5
|
export const PX_PER_MM = 96 / 25.4;
|
|
6
|
+
/** @internal */
|
|
3
7
|
export const DEFAULT_RULES = { orphans: 2, widows: 2 };
|
|
4
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Height of a page's content box (page height minus top and bottom margins), in CSS px.
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
5
12
|
export function contentHeight({ pageSize, margins }) {
|
|
6
13
|
return (pageSize.height - margins.top - margins.bottom) * PX_PER_MM;
|
|
7
14
|
}
|
|
8
15
|
// Half a pixel of slack: measured heights are fractional and rounding must not add a page.
|
|
9
16
|
const SLACK = 0.5;
|
|
10
17
|
/**
|
|
11
|
-
* Page flow: the fragments of each block, in order. Blocks fill a page in order; a
|
|
12
|
-
* does not fit is split at a line boundary, the lines that fit staying
|
|
13
|
-
* continuing at the top of the next
|
|
14
|
-
* line of the next block ends; an empty
|
|
18
|
+
* Page flow: the fragments of each block, in order. Blocks fill a page's columns in order; a
|
|
19
|
+
* block that does not fit is split at a line boundary, the lines that fit staying in the column,
|
|
20
|
+
* the rest continuing at the top of the next column or page (as many as it takes). A column with
|
|
21
|
+
* room for no line of the next block ends; an empty one always takes at least one line.
|
|
15
22
|
*
|
|
16
23
|
* Rules:
|
|
17
|
-
* - Explicit
|
|
24
|
+
* - Explicit breaks: the line holding a page break ends its page, a column break its column.
|
|
18
25
|
* - Widows and orphans: a split leaves at least `orphans` lines at the bottom of a page and
|
|
19
26
|
* carries at least `widows` lines to the next; a block too short for both moves whole. Not
|
|
20
27
|
* applied where only an empty page could hold the lines, nor across an explicit break.
|
|
21
|
-
* - Keep with next: a `keepWithNext` block (and a chain of them) moves to the next page
|
|
22
|
-
* the block after it would start there, unless it already starts its
|
|
23
|
-
* not bring them together (then the flow stays as it was).
|
|
28
|
+
* - Keep with next: a `keepWithNext` block (and a chain of them) moves to the next column or page
|
|
29
|
+
* when the block after it would start there, unless it already starts its column or the move
|
|
30
|
+
* would not bring them together (then the flow stays as it was).
|
|
31
|
+
* - Sections (`BlockBox.section`): a new one starts as its `start` says, an even or odd page
|
|
32
|
+
* section after a blank page when the page's number asks for one. A continuous one with the
|
|
33
|
+
* same page opens a band below the last; each band has its section's columns. A band of a
|
|
34
|
+
* `balanced` section ends with its columns level: its last page's columns take the shortest
|
|
35
|
+
* height that holds them.
|
|
36
|
+
*
|
|
37
|
+
* - Footnotes (ADR-0007, on Word's PDFs): a line with a reference goes on a page only if it, the
|
|
38
|
+
* separator and the note's first line fit. The note then takes the room down to the text, up to
|
|
39
|
+
* all of it, so a long one fills the page; the rest is carried to the foot of the next page,
|
|
40
|
+
* 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
|
|
41
|
+
* section with columns the notes are set in those columns across the foot of the band, whichever
|
|
42
|
+
* column refers to them, and every column's text ends above them.
|
|
43
|
+
* - Endnotes (`BlockBox.endnotes`) follow their block in the text, over as many pages as they need.
|
|
24
44
|
*
|
|
25
45
|
* Spacing follows CSS inside a padded page: the first block keeps its top margin, adjacent
|
|
26
46
|
* margins collapse to the larger, and the last block's bottom margin stays inside the page
|
|
@@ -29,72 +49,504 @@ const SLACK = 0.5;
|
|
|
29
49
|
* no top margin.
|
|
30
50
|
*
|
|
31
51
|
* shortcut: positive margins only (negative margins collapse differently).
|
|
52
|
+
* @internal
|
|
32
53
|
*/
|
|
33
54
|
export function flow(boxes, pageHeight, rules = DEFAULT_RULES) {
|
|
34
55
|
return flowFrom(boxes, pageHeight, rules).fragments;
|
|
35
56
|
}
|
|
57
|
+
/** A fragment's place in reading order: its page, band and column. */
|
|
58
|
+
const slot = (f) => f ? f.page * 1_000_000 + (f.band ?? 0) * 1000 + (f.column ?? 0) : 0;
|
|
36
59
|
/**
|
|
37
60
|
* `flow`, reusing a previous one: it starts at `reuse.from` (backed up over a chain of
|
|
38
|
-
* keep-with-next blocks, whose placement depends on what follows
|
|
39
|
-
*
|
|
40
|
-
*
|
|
61
|
+
* keep-with-next blocks, whose placement depends on what follows, and to the start of a balanced
|
|
62
|
+
* section, whose columns depend on all of it) with the previous state there, and once the state
|
|
63
|
+
* before an unchanged block equals the previous state before it, the rest is the previous flow's.
|
|
64
|
+
* The result is the same as a full flow.
|
|
65
|
+
* @internal
|
|
41
66
|
*/
|
|
42
67
|
export function flowFrom(boxes, flowHeight, rules = DEFAULT_RULES, reuse) {
|
|
43
68
|
let start = reuse ? Math.min(reuse.from, boxes.length, reuse.previous.fragments.length) : 0;
|
|
44
|
-
|
|
45
|
-
|
|
69
|
+
const balancedAt = (i) => boxes[i]?.section?.balanced === true;
|
|
70
|
+
const sameSection = (i) => boxes[i]?.section === boxes[i - 1]?.section;
|
|
71
|
+
for (;;) {
|
|
72
|
+
if (start > 0 && boxes[start - 1]?.keepWithNext)
|
|
73
|
+
start--;
|
|
74
|
+
else if (start > 0 && start < boxes.length && balancedAt(start) && sameSection(start))
|
|
75
|
+
start--;
|
|
76
|
+
else
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
46
79
|
const result = reuse ? reuse.previous.fragments.slice(0, start) : [];
|
|
47
80
|
const snapshots = reuse ? reuse.previous.states.slice(0, start) : [];
|
|
48
|
-
|
|
81
|
+
const gaps = reuse ? reuse.previous.gaps.slice(0, start) : [];
|
|
82
|
+
let state = (reuse?.previous.states[start]
|
|
49
83
|
? { ...reuse.previous.states[start] }
|
|
50
|
-
: {
|
|
51
|
-
|
|
84
|
+
: {
|
|
85
|
+
page: 0,
|
|
86
|
+
number: boxes[0]?.section?.pageStart ?? 1,
|
|
87
|
+
sectionPage: 0,
|
|
88
|
+
band: 0,
|
|
89
|
+
bandTop: 0,
|
|
90
|
+
lead: 0,
|
|
91
|
+
column: 0,
|
|
92
|
+
used: 0,
|
|
93
|
+
bandBottom: 0,
|
|
94
|
+
previousMargin: 0,
|
|
95
|
+
onPage: 0,
|
|
96
|
+
breakPending: false,
|
|
97
|
+
columnPending: false,
|
|
98
|
+
forcedPage: true,
|
|
99
|
+
owner: null,
|
|
100
|
+
notes: 0,
|
|
101
|
+
noteParts: [],
|
|
102
|
+
pageNotes: 0,
|
|
103
|
+
carry: [],
|
|
104
|
+
reserves: {},
|
|
105
|
+
});
|
|
106
|
+
/** Blocks that start a column because the keep-with-next rule moved them. */
|
|
52
107
|
const breakBefore = new Set();
|
|
53
108
|
const word = rules.spacing === "word";
|
|
109
|
+
/** While a section is balanced: the page whose columns are limited, and to what height. */
|
|
110
|
+
let balancing = null;
|
|
111
|
+
/** Note pieces placed and not yet held by a fragment. */
|
|
112
|
+
let pending = [];
|
|
113
|
+
/** Asked for by a later column: the band is flowed again with this kept at its foot. */
|
|
114
|
+
let retry = null;
|
|
115
|
+
const bandKey = () => `${state.page}|${state.band}`;
|
|
116
|
+
const reserved = () => state.reserves[bandKey()] ?? 0;
|
|
54
117
|
const newPage = (forced = false) => {
|
|
55
118
|
state.page++;
|
|
119
|
+
state.number++;
|
|
120
|
+
state.band = 0;
|
|
121
|
+
state.bandTop = 0;
|
|
122
|
+
state.lead = 0;
|
|
123
|
+
state.column = 0;
|
|
124
|
+
state.used = 0;
|
|
125
|
+
state.bandBottom = 0;
|
|
126
|
+
state.onPage = 0;
|
|
127
|
+
state.forcedPage = forced;
|
|
128
|
+
state.owner = null;
|
|
129
|
+
state.notes = 0;
|
|
130
|
+
state.noteParts = [];
|
|
131
|
+
state.pageNotes = 0;
|
|
132
|
+
// What was kept on earlier pages is done with.
|
|
133
|
+
if (Object.keys(state.reserves).length)
|
|
134
|
+
state.reserves = Object.fromEntries(Object.entries(state.reserves).filter(([key]) => Number.parseInt(key, 10) >= state.page));
|
|
135
|
+
};
|
|
136
|
+
/** The next column of the band, or the next page after its last. */
|
|
137
|
+
const nextColumn = (box, forced = false) => {
|
|
138
|
+
if (state.column + 1 >= (box.section?.columns ?? 1)) {
|
|
139
|
+
newPage(forced);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
state.bandBottom = Math.max(state.bandBottom, state.used);
|
|
143
|
+
state.column++;
|
|
56
144
|
state.used = 0;
|
|
145
|
+
state.lead = 0;
|
|
57
146
|
state.onPage = 0;
|
|
58
147
|
state.forcedPage = forced;
|
|
59
148
|
};
|
|
149
|
+
/** The height a block may reach in the column, from the band's top. */
|
|
150
|
+
const room = (box) => pageRoom(box) - Math.max(state.notes, reserved());
|
|
151
|
+
/** The height a block may reach in the column, whatever notes take. */
|
|
152
|
+
const pageRoom = (box) => {
|
|
153
|
+
const section = state.owner?.section ?? box.section;
|
|
154
|
+
const page = (section
|
|
155
|
+
? section.box({
|
|
156
|
+
index: state.page,
|
|
157
|
+
number: state.number,
|
|
158
|
+
first: state.owner?.first ?? state.page === state.sectionPage,
|
|
159
|
+
}).height
|
|
160
|
+
: (box.pageHeight ?? flowHeight)) -
|
|
161
|
+
state.bandTop -
|
|
162
|
+
state.pageNotes;
|
|
163
|
+
return balancing?.page === state.page ? Math.min(page, balancing.height) : page;
|
|
164
|
+
};
|
|
165
|
+
const piece = (note, fromLine, toLine) => ({
|
|
166
|
+
id: note.id,
|
|
167
|
+
page: state.page,
|
|
168
|
+
...(state.band ? { band: state.band } : {}),
|
|
169
|
+
...(state.column ? { column: state.column } : {}),
|
|
170
|
+
fromLine,
|
|
171
|
+
toLine,
|
|
172
|
+
});
|
|
173
|
+
/** A piece of the notes at a band's foot: they are the band's, not one column's. */
|
|
174
|
+
const footPiece = (note, fromLine, toLine) => ({
|
|
175
|
+
id: note.id,
|
|
176
|
+
page: state.page,
|
|
177
|
+
...(state.band ? { band: state.band } : {}),
|
|
178
|
+
fromLine,
|
|
179
|
+
toLine,
|
|
180
|
+
});
|
|
181
|
+
/** How much of an endnote, from line `from`, fits in `free`. */
|
|
182
|
+
const linesIn = (note, from, free) => {
|
|
183
|
+
const lines = note.lines(state.column);
|
|
184
|
+
const top = from ? lines[from - 1] : 0;
|
|
185
|
+
let to = from;
|
|
186
|
+
while (to < lines.length && lines[to] - top <= free + SLACK)
|
|
187
|
+
to++;
|
|
188
|
+
return { to, height: to > from ? lines[to - 1] - top : 0, all: lines.length };
|
|
189
|
+
};
|
|
190
|
+
/** How much of a footnote fits in `free` beside the notes `parts` of the band's foot. */
|
|
191
|
+
const fitNote = (note, from, free, parts, columns) => {
|
|
192
|
+
const lines = note.lines(0);
|
|
193
|
+
const top = from ? lines[from - 1] : 0;
|
|
194
|
+
const beside = columns > 1 ? 0 : notesHeight(parts, 1);
|
|
195
|
+
const height = (to) => lines[to] - top;
|
|
196
|
+
let to = from;
|
|
197
|
+
while (to < lines.length &&
|
|
198
|
+
(columns > 1 ? notesHeight([...parts, height(to)], columns) : beside + height(to)) <=
|
|
199
|
+
free + SLACK)
|
|
200
|
+
to++;
|
|
201
|
+
return { to, height: to > from ? height(to - 1) : 0, all: lines.length };
|
|
202
|
+
};
|
|
203
|
+
const noteHeight = (note) => note.lines(0).at(-1) ?? 0;
|
|
204
|
+
const separatorPiece = (note) => footPiece(note, 0, note.lines(0).length);
|
|
205
|
+
/**
|
|
206
|
+
* On a page or band with no notes yet: what is carried over goes to its foot first, as much as
|
|
207
|
+
* fits, before any text (Word's PDFs: a long note takes whole pages). `force`: a page with
|
|
208
|
+
* nothing else on it takes at least a line.
|
|
209
|
+
*/
|
|
210
|
+
const placeCarried = (box, force = false) => {
|
|
211
|
+
if (!state.carry.length || state.notes || state.onPage || !rules.notes)
|
|
212
|
+
return;
|
|
213
|
+
const { continuation } = rules.notes;
|
|
214
|
+
const columns = box.section?.columns ?? 1;
|
|
215
|
+
const above = noteHeight(continuation);
|
|
216
|
+
const free = pageRoom(box) - state.used - above;
|
|
217
|
+
const placed = [];
|
|
218
|
+
const left = [];
|
|
219
|
+
let parts = [];
|
|
220
|
+
for (const item of state.carry) {
|
|
221
|
+
if (left.length) {
|
|
222
|
+
left.push(item);
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
let fit = fitNote(item.note, item.from, free, parts, columns);
|
|
226
|
+
if (fit.to === item.from && force && !placed.length)
|
|
227
|
+
fit = fitNote(item.note, item.from, lineHeightOf(item), parts, 1);
|
|
228
|
+
if (fit.to > item.from) {
|
|
229
|
+
placed.push(footPiece(item.note, item.from, fit.to));
|
|
230
|
+
parts = [...parts, fit.height];
|
|
231
|
+
}
|
|
232
|
+
if (fit.to < fit.all)
|
|
233
|
+
left.push({ note: item.note, from: fit.to });
|
|
234
|
+
}
|
|
235
|
+
if (!placed.length)
|
|
236
|
+
return;
|
|
237
|
+
pending.push(separatorPiece(continuation), ...placed);
|
|
238
|
+
state.notes = above + notesHeight(parts, columns);
|
|
239
|
+
state.noteParts = parts;
|
|
240
|
+
state.carry = left;
|
|
241
|
+
};
|
|
242
|
+
/** The height of the next line of a carried note. */
|
|
243
|
+
const lineHeightOf = ({ note, from }) => {
|
|
244
|
+
const lines = note.lines(0);
|
|
245
|
+
return lines[from] - (from ? lines[from - 1] : 0);
|
|
246
|
+
};
|
|
247
|
+
/** Whether what does not fit here can go elsewhere: not from an empty last column at the top. */
|
|
248
|
+
const canMove = (box) => state.onPage > 0 ||
|
|
249
|
+
state.notes > 0 ||
|
|
250
|
+
state.bandTop > 0 ||
|
|
251
|
+
state.column + 1 < (box.section?.columns ?? 1);
|
|
252
|
+
/** Moves to where `section` starts; the space above a new band when it opens one. */
|
|
253
|
+
const enterSection = (section, previous) => {
|
|
254
|
+
const pageBroken = state.breakPending;
|
|
255
|
+
state.breakPending = false;
|
|
256
|
+
state.columnPending = false;
|
|
257
|
+
// Word's PDFs: notes on the page and a continuous break into other columns start a page.
|
|
258
|
+
const notesAbove = (state.notes > 0 || state.pageNotes > 0) && section.columns !== previous?.columns;
|
|
259
|
+
const samePage = !previous || (section.samePage(previous) && !notesAbove);
|
|
260
|
+
if (!pageBroken && samePage && section.start === "continuous") {
|
|
261
|
+
state.owner ??= {
|
|
262
|
+
section: previous ?? section,
|
|
263
|
+
first: state.page === state.sectionPage,
|
|
264
|
+
};
|
|
265
|
+
const lead = state.previousMargin;
|
|
266
|
+
state.bandTop += Math.max(state.bandBottom, state.used) + lead;
|
|
267
|
+
state.lead = lead;
|
|
268
|
+
state.band++;
|
|
269
|
+
state.column = 0;
|
|
270
|
+
state.used = 0;
|
|
271
|
+
state.bandBottom = 0;
|
|
272
|
+
state.onPage = 0;
|
|
273
|
+
state.forcedPage = true;
|
|
274
|
+
state.sectionPage = state.page;
|
|
275
|
+
state.pageNotes += state.notes;
|
|
276
|
+
state.notes = 0;
|
|
277
|
+
state.noteParts = [];
|
|
278
|
+
return lead;
|
|
279
|
+
}
|
|
280
|
+
if (!pageBroken &&
|
|
281
|
+
samePage &&
|
|
282
|
+
section.start === "nextColumn" &&
|
|
283
|
+
section.columns === previous?.columns &&
|
|
284
|
+
state.column + 1 < section.columns) {
|
|
285
|
+
nextColumn({ section }, true);
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
if (state.onPage || state.used || state.column || state.band)
|
|
289
|
+
newPage(true);
|
|
290
|
+
else
|
|
291
|
+
state.forcedPage = true;
|
|
292
|
+
const wrongKind = (number) => (section.start === "oddPage" && number % 2 === 0) ||
|
|
293
|
+
(section.start === "evenPage" && number % 2 === 1);
|
|
294
|
+
// Word's even and odd page sections (its PDFs, every kind, start and page before): a blank
|
|
295
|
+
// page when the number the pages have reached is the wrong kind, unless the section restarts
|
|
296
|
+
// its numbers in a document without even and odd headers. A restart then takes its start, or
|
|
297
|
+
// the next number when that is the wrong kind.
|
|
298
|
+
if (wrongKind(state.number) && (section.pageStart === undefined || rules.evenAndOddHeaders))
|
|
299
|
+
newPage(true);
|
|
300
|
+
if (section.pageStart !== undefined)
|
|
301
|
+
state.number = section.pageStart + (wrongKind(section.pageStart) ? 1 : 0);
|
|
302
|
+
state.sectionPage = state.page;
|
|
303
|
+
return undefined;
|
|
304
|
+
};
|
|
60
305
|
const place = (box, index) => {
|
|
61
|
-
const
|
|
62
|
-
if (
|
|
63
|
-
|
|
306
|
+
const fragments = placeBlock(box, index);
|
|
307
|
+
if (box.endnotes?.length)
|
|
308
|
+
placeEndnotes(box, fragments);
|
|
309
|
+
return fragments;
|
|
310
|
+
};
|
|
311
|
+
/** A block's endnotes, after it in the text; lines that do not fit go on to the next column. */
|
|
312
|
+
const placeEndnotes = (box, fragments) => {
|
|
313
|
+
let lead = state.onPage ? state.previousMargin : 0;
|
|
314
|
+
for (const note of box.endnotes ?? []) {
|
|
315
|
+
let from = 0;
|
|
316
|
+
for (;;) {
|
|
317
|
+
const lines = note.lines(state.column);
|
|
318
|
+
if (from >= lines.length)
|
|
319
|
+
break;
|
|
320
|
+
const top = from ? lines[from - 1] : 0;
|
|
321
|
+
placeCarried(box);
|
|
322
|
+
const gap = state.onPage ? (lead ?? 0) : 0;
|
|
323
|
+
let fit = linesIn(note, from, room(box) - state.used - gap);
|
|
324
|
+
if (fit.to === from) {
|
|
325
|
+
if (canMove(box)) {
|
|
326
|
+
nextColumn(box);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
fit = { ...fit, to: from + 1, height: lines[from] - top };
|
|
330
|
+
}
|
|
331
|
+
const part = piece(note, from, fit.to);
|
|
332
|
+
part.end = true;
|
|
333
|
+
if (gap)
|
|
334
|
+
part.lead = gap;
|
|
335
|
+
pending.push(part);
|
|
336
|
+
lead = undefined;
|
|
337
|
+
state.used += gap + fit.height;
|
|
338
|
+
state.onPage++;
|
|
339
|
+
from = fit.to;
|
|
340
|
+
if (from < lines.length)
|
|
341
|
+
nextColumn(box);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
state.previousMargin = 0;
|
|
345
|
+
const last = fragments[fragments.length - 1];
|
|
346
|
+
if (last && pending.length)
|
|
347
|
+
last.notes = [...(last.notes ?? []), ...pending];
|
|
348
|
+
if (last)
|
|
349
|
+
pending = [];
|
|
350
|
+
};
|
|
351
|
+
const placeBlock = (box, index) => {
|
|
352
|
+
const previous = boxes[index - 1];
|
|
353
|
+
let lead;
|
|
354
|
+
if (index > 0 && box.section && previous?.section !== box.section)
|
|
355
|
+
lead = enterSection(box.section, previous?.section);
|
|
356
|
+
else if (state.breakPending && state.onPage)
|
|
357
|
+
newPage(true);
|
|
358
|
+
else if (state.columnPending)
|
|
359
|
+
nextColumn(box, true);
|
|
360
|
+
else if (breakBefore.has(index) && state.onPage)
|
|
361
|
+
nextColumn(box);
|
|
64
362
|
state.breakPending = false;
|
|
363
|
+
state.columnPending = false;
|
|
364
|
+
const leadPage = state.page;
|
|
365
|
+
const at = (fromLine, toLine) => {
|
|
366
|
+
const fragment = { page: state.page, fromLine, toLine };
|
|
367
|
+
if (state.band)
|
|
368
|
+
fragment.band = state.band;
|
|
369
|
+
if (state.column)
|
|
370
|
+
fragment.column = state.column;
|
|
371
|
+
// Moved on to the next page, the block opens no band below anything.
|
|
372
|
+
if (lead !== undefined && state.page === leadPage)
|
|
373
|
+
fragment.lead = lead;
|
|
374
|
+
lead = undefined;
|
|
375
|
+
if (pending.length) {
|
|
376
|
+
fragment.notes = pending;
|
|
377
|
+
pending = [];
|
|
378
|
+
}
|
|
379
|
+
return fragment;
|
|
380
|
+
};
|
|
381
|
+
if (box.sectionMark && state.onPage) {
|
|
382
|
+
gaps[index] = 0;
|
|
383
|
+
return [{ ...at(0, Infinity), mark: true }];
|
|
384
|
+
}
|
|
65
385
|
const above = state.previousMargin;
|
|
66
386
|
state.previousMargin = box.marginBottom;
|
|
387
|
+
const adding = word && rules.addSpacing === true;
|
|
67
388
|
const gap = () => {
|
|
68
|
-
if (!state.onPage)
|
|
69
|
-
|
|
70
|
-
|
|
389
|
+
if (!state.onPage) {
|
|
390
|
+
if (word && !state.forcedPage)
|
|
391
|
+
return 0;
|
|
392
|
+
return adding ? box.marginTop : Math.max(0, box.marginTop - state.lead);
|
|
393
|
+
}
|
|
394
|
+
return adding ? above + box.marginTop : Math.max(above, box.marginTop);
|
|
71
395
|
};
|
|
72
396
|
// The bottom margin that must fit on the page (Word lets the space after hang).
|
|
73
397
|
const below = word ? 0 : box.marginBottom;
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
398
|
+
const pageBreaks = box.breaksAfter?.(state.column) ?? [];
|
|
399
|
+
const columnBreaks = box.columnBreaksAfter?.(state.column) ?? [];
|
|
400
|
+
const forced = pageBreaks.length + columnBreaks.length > 0;
|
|
401
|
+
let measured = box.inColumn?.(state.column) ?? box;
|
|
402
|
+
/** The notes referred to on each line, in the column it is in now. */
|
|
403
|
+
const referred = () => {
|
|
404
|
+
const byLine = new Map();
|
|
405
|
+
for (const { line, note } of box.notes?.(state.column) ?? [])
|
|
406
|
+
byLine.set(line, [...(byLine.get(line) ?? []), note]);
|
|
407
|
+
return byLine;
|
|
408
|
+
};
|
|
409
|
+
let notesOf = referred();
|
|
410
|
+
if (state.carry.length && !state.onPage)
|
|
411
|
+
placeCarried(box);
|
|
412
|
+
if (!forced &&
|
|
413
|
+
!notesOf.size &&
|
|
414
|
+
state.used + gap() + measured.height + below <= room(box) + SLACK) {
|
|
415
|
+
const space = gap();
|
|
416
|
+
gaps[index] = space;
|
|
417
|
+
state.used += space + measured.height;
|
|
77
418
|
state.onPage++;
|
|
78
|
-
return [
|
|
419
|
+
return [at(0, Infinity)];
|
|
79
420
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const
|
|
421
|
+
let lines = measured.lines();
|
|
422
|
+
let count = lines.length;
|
|
423
|
+
const inRange = (list) => new Set(list.filter((at) => at >= 0 && at < count));
|
|
424
|
+
let pageAfter = inRange(pageBreaks);
|
|
425
|
+
let columnAfter = inRange(columnBreaks);
|
|
83
426
|
const fragments = [];
|
|
84
427
|
let line = 0;
|
|
428
|
+
/** The explicit breaks passed so far. */
|
|
429
|
+
let passed = 0;
|
|
430
|
+
/** After a move: `measured` is the new column's, and `line` where the text has got to. */
|
|
431
|
+
const remeasure = (broken = false) => {
|
|
432
|
+
if (!box.inColumn)
|
|
433
|
+
return;
|
|
434
|
+
const next = box.inColumn(state.column);
|
|
435
|
+
if (next === measured)
|
|
436
|
+
return;
|
|
437
|
+
const nextLines = next.lines();
|
|
438
|
+
// shortcut: the same share of the text is taken to start the same line in another
|
|
439
|
+
// width (a line either way); a word-exact tail needs the text in the flow's terms.
|
|
440
|
+
const share = line ? lines[line - 1] / (measured.height || 1) : 0;
|
|
441
|
+
measured = next;
|
|
442
|
+
lines = nextLines;
|
|
443
|
+
count = lines.length;
|
|
444
|
+
// Breaks are lines of this width.
|
|
445
|
+
pageAfter = inRange(box.breaksAfter?.(state.column) ?? []);
|
|
446
|
+
columnAfter = inRange(box.columnBreaksAfter?.(state.column) ?? []);
|
|
447
|
+
if (line > 0) {
|
|
448
|
+
// After an explicit break the text goes on right after the same break.
|
|
449
|
+
const after = [...pageAfter, ...columnAfter].sort((a, b) => a - b)[passed - 1];
|
|
450
|
+
let mapped = lines.findIndex((bottom) => bottom / (next.height || 1) > share + 1e-6);
|
|
451
|
+
if (mapped < 0)
|
|
452
|
+
mapped = count - 1;
|
|
453
|
+
line = broken && after !== undefined ? after + 1 : Math.max(mapped, 1);
|
|
454
|
+
}
|
|
455
|
+
// Nothing left in this width (all of it fits one wider line): the last fragment ends it.
|
|
456
|
+
if (line >= count) {
|
|
457
|
+
const last = fragments.at(-1);
|
|
458
|
+
if (last)
|
|
459
|
+
last.toLine = Infinity;
|
|
460
|
+
}
|
|
461
|
+
};
|
|
85
462
|
while (line < count) {
|
|
86
463
|
// The lines up to the next explicit break (or the end) can be split freely.
|
|
87
464
|
let segmentEnd = line;
|
|
88
|
-
while (segmentEnd < count && !
|
|
465
|
+
while (segmentEnd < count && !pageAfter.has(segmentEnd) && !columnAfter.has(segmentEnd))
|
|
89
466
|
segmentEnd++;
|
|
90
467
|
segmentEnd = Math.min(segmentEnd + 1, count);
|
|
91
468
|
const top = line ? lines[line - 1] : 0;
|
|
92
469
|
// Only the block's first fragment has the gap above it; the others repeat its header.
|
|
93
470
|
const spacing = line === 0 ? gap() : (box.repeat ?? 0);
|
|
471
|
+
notesOf = referred();
|
|
472
|
+
placeCarried(box);
|
|
473
|
+
const limit = pageRoom(box);
|
|
474
|
+
/**
|
|
475
|
+
* The footnotes of lines `line` to `to - 1` at the band's foot, each taking the room down
|
|
476
|
+
* to the text after its line; null when one's first line does not fit (`force`: it goes in).
|
|
477
|
+
* With columns the notes share the foot of them all: above them goes the tallest column.
|
|
478
|
+
*/
|
|
479
|
+
// A block kept whole is one line to the flow already.
|
|
480
|
+
const keepAll = count === 1;
|
|
481
|
+
const planNotes = (to, force = false) => {
|
|
482
|
+
const plan = {
|
|
483
|
+
notes: state.notes,
|
|
484
|
+
parts: state.noteParts,
|
|
485
|
+
carry: state.carry,
|
|
486
|
+
pieces: [],
|
|
487
|
+
};
|
|
488
|
+
if (!notesOf.size || !rules.notes)
|
|
489
|
+
return plan;
|
|
490
|
+
const columns = box.section?.columns ?? 1;
|
|
491
|
+
for (let at = line; at < to; at++)
|
|
492
|
+
for (const note of notesOf.get(at) ?? []) {
|
|
493
|
+
// Behind a note already cut, it waits its turn.
|
|
494
|
+
if (plan.carry.length) {
|
|
495
|
+
plan.carry = [...plan.carry, { note, from: 0 }];
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
const { separator } = rules.notes;
|
|
499
|
+
const first = !plan.notes;
|
|
500
|
+
const above = first
|
|
501
|
+
? noteHeight(separator)
|
|
502
|
+
: plan.notes - notesHeight(plan.parts, columns);
|
|
503
|
+
// Word's PDFs: the lines that the widow and orphan rules keep with this one stay above
|
|
504
|
+
// the note, and the paragraph's space after stays between its last line and the notes.
|
|
505
|
+
let kept = at + 1;
|
|
506
|
+
if (!box.anyLine && !keepAll) {
|
|
507
|
+
kept = Math.max(kept, line + rules.orphans);
|
|
508
|
+
if (count - kept < rules.widows)
|
|
509
|
+
kept = count;
|
|
510
|
+
kept = Math.min(kept, segmentEnd);
|
|
511
|
+
}
|
|
512
|
+
const after = kept === count ? box.marginBottom : 0;
|
|
513
|
+
const own = state.used + spacing + lines[kept - 1] - top + after;
|
|
514
|
+
const text = Math.max(own, columns > 1 && state.column ? state.bandBottom : 0);
|
|
515
|
+
let fit = fitNote(note, 0, limit - text - above, plan.parts, columns);
|
|
516
|
+
if (columns > 1 &&
|
|
517
|
+
state.column &&
|
|
518
|
+
!force &&
|
|
519
|
+
(!fit.to || (fit.to < fit.all && text > own))) {
|
|
520
|
+
// A later column's note, cut short by what the earlier columns hold: in Word they
|
|
521
|
+
// end above it. They are flowed again with its room kept, whole if the page can.
|
|
522
|
+
const lines = note.lines(0);
|
|
523
|
+
const whole = above + notesHeight([...plan.parts, lines.at(-1)], columns);
|
|
524
|
+
const one = above + notesHeight([...plan.parts, lines[0]], columns);
|
|
525
|
+
const height = whole < limit / 2 ? whole : one;
|
|
526
|
+
if (height > reserved() + SLACK && height < limit)
|
|
527
|
+
retry = { key: bandKey(), height };
|
|
528
|
+
}
|
|
529
|
+
if (!fit.to) {
|
|
530
|
+
if (!force)
|
|
531
|
+
return null;
|
|
532
|
+
fit = fitNote(note, 0, lineHeightOf({ note, from: 0 }), [], 1);
|
|
533
|
+
}
|
|
534
|
+
if (first)
|
|
535
|
+
plan.pieces.push(separatorPiece(separator));
|
|
536
|
+
plan.pieces.push(footPiece(note, 0, fit.to));
|
|
537
|
+
plan.parts = [...plan.parts, fit.height];
|
|
538
|
+
plan.notes = above + notesHeight(plan.parts, columns);
|
|
539
|
+
if (fit.to < fit.all)
|
|
540
|
+
plan.carry = [{ note, from: fit.to }];
|
|
541
|
+
}
|
|
542
|
+
return plan;
|
|
543
|
+
};
|
|
94
544
|
let end = line;
|
|
95
545
|
while (end < segmentEnd) {
|
|
96
546
|
const bottom = lines[end] - top + (end + 1 === count ? below : 0);
|
|
97
|
-
|
|
547
|
+
const plan = planNotes(end + 1);
|
|
548
|
+
if (!plan ||
|
|
549
|
+
state.used + spacing + bottom + Math.max(plan.notes, reserved()) > limit + SLACK)
|
|
98
550
|
break;
|
|
99
551
|
end++;
|
|
100
552
|
}
|
|
@@ -103,107 +555,360 @@ export function flowFrom(boxes, flowHeight, rules = DEFAULT_RULES, reuse) {
|
|
|
103
555
|
const kept = Math.min(end, segmentEnd - rules.widows);
|
|
104
556
|
if (kept - line >= rules.orphans)
|
|
105
557
|
end = kept;
|
|
106
|
-
else if (
|
|
558
|
+
else if (canMove(box))
|
|
107
559
|
end = line;
|
|
108
560
|
}
|
|
109
561
|
if (end === line) {
|
|
110
|
-
if (
|
|
111
|
-
|
|
562
|
+
if (canMove(box)) {
|
|
563
|
+
nextColumn(box);
|
|
564
|
+
remeasure();
|
|
112
565
|
continue;
|
|
113
566
|
}
|
|
114
567
|
end = line + 1; // a line taller than a page is placed anyway
|
|
115
568
|
}
|
|
116
|
-
|
|
569
|
+
// What the lines kept refer to: lines taken back take their notes with them.
|
|
570
|
+
const kept = planNotes(end, true);
|
|
571
|
+
if (kept) {
|
|
572
|
+
state.notes = kept.notes;
|
|
573
|
+
state.noteParts = kept.parts;
|
|
574
|
+
state.carry = kept.carry;
|
|
575
|
+
pending.push(...kept.pieces);
|
|
576
|
+
}
|
|
577
|
+
if (line === 0)
|
|
578
|
+
gaps[index] = spacing;
|
|
579
|
+
fragments.push(at(line, end));
|
|
117
580
|
state.used += spacing + lines[end - 1] - top;
|
|
118
581
|
state.onPage++;
|
|
119
582
|
line = end;
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
583
|
+
const pageBroken = pageAfter.has(end - 1);
|
|
584
|
+
const columnBroken = columnAfter.has(end - 1);
|
|
585
|
+
if (line < count) {
|
|
586
|
+
if (pageBroken || columnBroken)
|
|
587
|
+
passed++;
|
|
588
|
+
if (pageBroken)
|
|
589
|
+
newPage();
|
|
590
|
+
else
|
|
591
|
+
nextColumn(box, columnBroken);
|
|
592
|
+
remeasure(pageBroken || columnBroken);
|
|
593
|
+
}
|
|
594
|
+
else if (pageBroken)
|
|
124
595
|
state.breakPending = true;
|
|
596
|
+
else if (columnBroken)
|
|
597
|
+
state.columnPending = true;
|
|
125
598
|
}
|
|
126
599
|
// Moved whole to a later page: not split.
|
|
127
600
|
const [only] = fragments;
|
|
128
|
-
if (fragments.length === 1 && only && !forced
|
|
601
|
+
if (fragments.length === 1 && only && !forced)
|
|
129
602
|
only.toLine = Infinity;
|
|
130
603
|
return fragments;
|
|
131
604
|
};
|
|
132
605
|
/** The flow as it was before a keep-with-next move, by the block that triggered it. */
|
|
133
606
|
const attempts = new Map();
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
:
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
result.splice(attempt.first, result.length - attempt.first, ...attempt.result);
|
|
168
|
-
snapshots.splice(attempt.first, snapshots.length - attempt.first, ...attempt.snapshots);
|
|
169
|
-
state = attempt.state;
|
|
170
|
-
index++;
|
|
171
|
-
continue;
|
|
607
|
+
/** Flows blocks `from` to `to`; a result when the rest could be taken from `reuse`. */
|
|
608
|
+
const run = (from, to) => {
|
|
609
|
+
let index = from;
|
|
610
|
+
while (index < to) {
|
|
611
|
+
// Realigned with the previous flow: the rest is unchanged (no keep-with-next decision
|
|
612
|
+
// about this block, made with the block before it, is pending). The block before it must
|
|
613
|
+
// be unchanged too: had it been keep-with-next before the edit, the previous flow from
|
|
614
|
+
// here holds a decision about a chain that no longer exists. Inside a balanced section the
|
|
615
|
+
// columns depend on the whole section: only its start realigns.
|
|
616
|
+
const was = reuse &&
|
|
617
|
+
!balancing &&
|
|
618
|
+
index > start &&
|
|
619
|
+
!attempts.size &&
|
|
620
|
+
reuse.previousIndex(index - 1) >= 0
|
|
621
|
+
? reuse.previousIndex(index)
|
|
622
|
+
: -1;
|
|
623
|
+
const before = was >= 0 ? reuse?.previous.states[was] : undefined;
|
|
624
|
+
if (before &&
|
|
625
|
+
!boxes[index - 1]?.keepWithNext &&
|
|
626
|
+
!(balancedAt(index) && sameSection(index)) &&
|
|
627
|
+
sameState(before, state)) {
|
|
628
|
+
const previous = reuse.previous;
|
|
629
|
+
result.length = index;
|
|
630
|
+
snapshots.length = index;
|
|
631
|
+
gaps.length = index;
|
|
632
|
+
result.push(...previous.fragments.slice(was));
|
|
633
|
+
snapshots.push(...previous.states.slice(was, -1));
|
|
634
|
+
gaps.push(...previous.gaps.slice(was));
|
|
635
|
+
return {
|
|
636
|
+
fragments: result,
|
|
637
|
+
states: [...snapshots, { ...previous.states.at(-1) }],
|
|
638
|
+
gaps,
|
|
639
|
+
};
|
|
172
640
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
first
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
result: result.slice(first),
|
|
187
|
-
snapshots: snapshots.slice(first),
|
|
188
|
-
state: { ...state },
|
|
189
|
-
});
|
|
190
|
-
breakBefore.add(first);
|
|
191
|
-
state = { ...beforeFirst };
|
|
641
|
+
snapshots[index] = { ...state };
|
|
642
|
+
const fragments = place(boxes[index], index);
|
|
643
|
+
if (retry) {
|
|
644
|
+
const { key, height } = retry;
|
|
645
|
+
retry = null;
|
|
646
|
+
const [page, band] = key.split("|").map(Number);
|
|
647
|
+
// Back to the first block on that band: it may have started on an earlier page.
|
|
648
|
+
let first = index;
|
|
649
|
+
while (first > from &&
|
|
650
|
+
result[first - 1]?.some((f) => f.page === page && (f.band ?? 0) === band))
|
|
651
|
+
first--;
|
|
652
|
+
const before = snapshots[first];
|
|
653
|
+
state = { ...before, reserves: { ...before.reserves, [key]: height } };
|
|
192
654
|
result.length = first;
|
|
655
|
+
snapshots.length = first;
|
|
656
|
+
gaps.length = first;
|
|
657
|
+
attempts.clear();
|
|
658
|
+
for (const moved of breakBefore)
|
|
659
|
+
if (moved >= first)
|
|
660
|
+
breakBefore.delete(moved);
|
|
661
|
+
pending = [];
|
|
193
662
|
index = first;
|
|
194
663
|
continue;
|
|
195
664
|
}
|
|
665
|
+
result[index] = fragments;
|
|
666
|
+
const previous = result[index - 1];
|
|
667
|
+
const startsLater = previous !== undefined && slot(fragments[0]) > slot(previous[previous.length - 1]);
|
|
668
|
+
const attempt = attempts.get(index);
|
|
669
|
+
if (attempt) {
|
|
670
|
+
attempts.delete(index);
|
|
671
|
+
// The move did not bring them together (the block does not fit after the heading on a
|
|
672
|
+
// page of its own either): keep the flow as it was.
|
|
673
|
+
if (startsLater) {
|
|
674
|
+
result.splice(attempt.first, result.length - attempt.first, ...attempt.result);
|
|
675
|
+
snapshots.splice(attempt.first, snapshots.length - attempt.first, ...attempt.snapshots);
|
|
676
|
+
gaps.splice(attempt.first, gaps.length - attempt.first, ...attempt.gaps);
|
|
677
|
+
state = attempt.state;
|
|
678
|
+
index++;
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
if (startsLater && boxes[index - 1]?.keepWithNext && sameSection(index)) {
|
|
683
|
+
// The chain of keep-with-next blocks before this one, each whole in one column (keeping
|
|
684
|
+
// reaches no further back than the section: it starts where its `start` says).
|
|
685
|
+
let first = index - 1;
|
|
686
|
+
while (first > 0 &&
|
|
687
|
+
sameSection(first) &&
|
|
688
|
+
boxes[first - 1]?.keepWithNext &&
|
|
689
|
+
result[first - 1]?.length === 1 &&
|
|
690
|
+
slot(result[first - 1]?.[0]) === slot(result[first]?.[0]))
|
|
691
|
+
first--;
|
|
692
|
+
const beforeFirst = snapshots[first];
|
|
693
|
+
if (!breakBefore.has(first) && beforeFirst.onPage > 0 && result[first]?.length === 1) {
|
|
694
|
+
attempts.set(index, {
|
|
695
|
+
first,
|
|
696
|
+
result: result.slice(first),
|
|
697
|
+
snapshots: snapshots.slice(first),
|
|
698
|
+
gaps: gaps.slice(first),
|
|
699
|
+
state: { ...state },
|
|
700
|
+
});
|
|
701
|
+
breakBefore.add(first);
|
|
702
|
+
state = { ...beforeFirst };
|
|
703
|
+
result.length = first;
|
|
704
|
+
index = first;
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
index++;
|
|
709
|
+
const section = boxes[index - 1]?.section;
|
|
710
|
+
if (!balancing &&
|
|
711
|
+
section?.balanced &&
|
|
712
|
+
section.columns > 1 &&
|
|
713
|
+
boxes[index]?.section !== section)
|
|
714
|
+
balance(index);
|
|
715
|
+
}
|
|
716
|
+
return null;
|
|
717
|
+
};
|
|
718
|
+
/**
|
|
719
|
+
* Levels the columns of the balanced section ending before block `end`: flows it again with
|
|
720
|
+
* its last page's columns limited, looking for the shortest height that keeps it on that page.
|
|
721
|
+
*/
|
|
722
|
+
const balance = (end) => {
|
|
723
|
+
const section = boxes[end - 1]?.section;
|
|
724
|
+
let first = end - 1;
|
|
725
|
+
while (first > 0 && boxes[first - 1]?.section === section)
|
|
726
|
+
first--;
|
|
727
|
+
const from = snapshots[first];
|
|
728
|
+
const { page } = state;
|
|
729
|
+
let high = Math.max(state.bandBottom, state.used);
|
|
730
|
+
let low = high / section.columns - 1;
|
|
731
|
+
/** The section flowed with its last page's columns `height` tall: does it stay there? */
|
|
732
|
+
const attempt = (height) => {
|
|
733
|
+
balancing = { page, height };
|
|
734
|
+
state = { ...from };
|
|
735
|
+
result.length = first;
|
|
736
|
+
snapshots.length = first;
|
|
737
|
+
attempts.clear();
|
|
738
|
+
for (const moved of breakBefore)
|
|
739
|
+
if (moved >= first)
|
|
740
|
+
breakBefore.delete(moved);
|
|
741
|
+
run(first, end);
|
|
742
|
+
// The last column takes what it is given: a column over the limit is a failure too.
|
|
743
|
+
return state.page === page && Math.max(state.used, state.bandBottom) <= height + SLACK;
|
|
744
|
+
};
|
|
745
|
+
while (high - low > 0.5) {
|
|
746
|
+
const middle = (low + high) / 2;
|
|
747
|
+
if (attempt(middle))
|
|
748
|
+
high = middle;
|
|
749
|
+
else
|
|
750
|
+
low = middle;
|
|
751
|
+
}
|
|
752
|
+
if (!attempt(high))
|
|
753
|
+
attempt(Number.POSITIVE_INFINITY);
|
|
754
|
+
balancing = null;
|
|
755
|
+
};
|
|
756
|
+
/** What is still carried after the last block goes on pages of its own. */
|
|
757
|
+
const finishNotes = () => {
|
|
758
|
+
const held = result[result.length - 1];
|
|
759
|
+
const last = held?.[held.length - 1];
|
|
760
|
+
const box = boxes[boxes.length - 1];
|
|
761
|
+
if (!held || !last || !box || (!state.carry.length && !last.notes?.some((p) => p.tail)))
|
|
762
|
+
return;
|
|
763
|
+
const end = state;
|
|
764
|
+
state = { ...state };
|
|
765
|
+
while (state.carry.length) {
|
|
766
|
+
newPage();
|
|
767
|
+
placeCarried(box, true);
|
|
768
|
+
}
|
|
769
|
+
const notes = [
|
|
770
|
+
...(last.notes ?? []).filter((p) => !p.tail),
|
|
771
|
+
...pending.map((p) => ({ ...p, tail: true })),
|
|
772
|
+
];
|
|
773
|
+
pending = [];
|
|
774
|
+
state = end;
|
|
775
|
+
result[result.length - 1] = [...held.slice(0, -1), { ...last, notes }];
|
|
776
|
+
};
|
|
777
|
+
const reused = run(start, boxes.length);
|
|
778
|
+
if (reused)
|
|
779
|
+
return reused;
|
|
780
|
+
finishNotes();
|
|
781
|
+
return {
|
|
782
|
+
fragments: result,
|
|
783
|
+
states: [...snapshots.slice(0, boxes.length), { ...state }],
|
|
784
|
+
gaps: gaps.slice(0, boxes.length),
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
/**
|
|
788
|
+
* Each page of a flow: its number, its first section and whether that section starts on it. A page
|
|
789
|
+
* nothing is on (the blank page before an even or odd page section) has the next page's section.
|
|
790
|
+
* @internal
|
|
791
|
+
*/
|
|
792
|
+
export function pagesOf(boxes, result) {
|
|
793
|
+
const pages = [];
|
|
794
|
+
const owners = [];
|
|
795
|
+
const seen = new Set();
|
|
796
|
+
const onPages = new Map();
|
|
797
|
+
result.fragments.forEach((fragments, i) => {
|
|
798
|
+
const section = boxes[i]?.section;
|
|
799
|
+
const after = result.states[i + 1];
|
|
800
|
+
const own = onPages.get(section) ?? new Set();
|
|
801
|
+
onPages.set(section, own);
|
|
802
|
+
const places = [...fragments, ...fragments.flatMap((fragment) => fragment.notes ?? [])];
|
|
803
|
+
for (const { page } of places) {
|
|
804
|
+
own.add(page);
|
|
805
|
+
if (!pages[page]) {
|
|
806
|
+
owners[page] = section;
|
|
807
|
+
pages[page] = {
|
|
808
|
+
index: page,
|
|
809
|
+
// Pages count on from a block's first to its last: the number where it ended tells.
|
|
810
|
+
number: after.number - (after.page - page),
|
|
811
|
+
first: !seen.has(section),
|
|
812
|
+
section: section.index ?? 0,
|
|
813
|
+
sectionPages: 0,
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
seen.add(section);
|
|
817
|
+
}
|
|
818
|
+
});
|
|
819
|
+
pages.forEach((page, p) => {
|
|
820
|
+
if (page)
|
|
821
|
+
page.sectionPages = onPages.get(owners[p])?.size ?? 0;
|
|
822
|
+
});
|
|
823
|
+
for (let p = pages.length - 1; p >= 0; p--) {
|
|
824
|
+
const next = pages[p + 1];
|
|
825
|
+
pages[p] ??= { ...next, index: p, number: next.number - 1, first: false, sectionPages: 0 };
|
|
826
|
+
}
|
|
827
|
+
return pages;
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* Notes of heights `parts` set in `columns` columns, as Word sets those of a section with columns
|
|
831
|
+
* (its PDFs): whole, in order, each column as full as the height the set needs. That height and
|
|
832
|
+
* the column of each. One column: their sum.
|
|
833
|
+
* @internal
|
|
834
|
+
*/
|
|
835
|
+
export function arrangeNotes(parts, columns) {
|
|
836
|
+
const total = parts.reduce((sum, part) => sum + part, 0);
|
|
837
|
+
if (columns <= 1 || parts.length <= 1)
|
|
838
|
+
return { height: total, columnOf: parts.map(() => 0) };
|
|
839
|
+
const pack = (height) => {
|
|
840
|
+
const columnOf = [];
|
|
841
|
+
let column = 0;
|
|
842
|
+
let used = 0;
|
|
843
|
+
let tallest = 0;
|
|
844
|
+
for (const part of parts) {
|
|
845
|
+
if (used && used + part > height + 0.01) {
|
|
846
|
+
column++;
|
|
847
|
+
used = 0;
|
|
848
|
+
}
|
|
849
|
+
used += part;
|
|
850
|
+
tallest = Math.max(tallest, used);
|
|
851
|
+
columnOf.push(column);
|
|
852
|
+
}
|
|
853
|
+
return column < columns ? { height: tallest, columnOf } : null;
|
|
854
|
+
};
|
|
855
|
+
let low = Math.max(...parts, total / columns);
|
|
856
|
+
let best = pack(low);
|
|
857
|
+
if (best)
|
|
858
|
+
return best;
|
|
859
|
+
let high = total;
|
|
860
|
+
best = pack(high);
|
|
861
|
+
while (high - low > 0.25) {
|
|
862
|
+
const middle = (low + high) / 2;
|
|
863
|
+
const tried = pack(middle);
|
|
864
|
+
if (tried) {
|
|
865
|
+
best = tried;
|
|
866
|
+
high = middle;
|
|
196
867
|
}
|
|
197
|
-
|
|
868
|
+
else
|
|
869
|
+
low = middle;
|
|
198
870
|
}
|
|
199
|
-
return
|
|
871
|
+
return best;
|
|
200
872
|
}
|
|
873
|
+
const notesHeight = (parts, columns) => arrangeNotes(parts, columns).height;
|
|
874
|
+
/**
|
|
875
|
+
* The space after the last block of a column of a page, as flowed (what ends its text).
|
|
876
|
+
* @internal
|
|
877
|
+
*/
|
|
878
|
+
export function spaceAfterText(boxes, result, page, band, column) {
|
|
879
|
+
for (let i = result.fragments.length - 1; i >= 0; i--)
|
|
880
|
+
for (const f of result.fragments[i] ?? [])
|
|
881
|
+
if (f.page === page && (f.band ?? 0) === band && (f.column ?? 0) === column)
|
|
882
|
+
return boxes[i]?.marginBottom ?? 0;
|
|
883
|
+
return 0;
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Every note piece of a flow, in order.
|
|
887
|
+
* @internal
|
|
888
|
+
*/
|
|
889
|
+
export const notePieces = (result) => result.fragments.flatMap((fragments) => fragments.flatMap((f) => f.notes ?? []));
|
|
201
890
|
function sameState(a, b) {
|
|
202
|
-
return (a.
|
|
891
|
+
return (a.notes === b.notes &&
|
|
892
|
+
JSON.stringify(a.reserves) === JSON.stringify(b.reserves) &&
|
|
893
|
+
a.noteParts.length === b.noteParts.length &&
|
|
894
|
+
a.noteParts.every((part, i) => part === b.noteParts[i]) &&
|
|
895
|
+
a.pageNotes === b.pageNotes &&
|
|
896
|
+
a.carry.length === b.carry.length &&
|
|
897
|
+
a.carry.every((item, i) => item.note.id === b.carry[i]?.note.id && item.from === b.carry[i]?.from) &&
|
|
898
|
+
a.page === b.page &&
|
|
899
|
+
a.number === b.number &&
|
|
900
|
+
a.sectionPage === b.sectionPage &&
|
|
901
|
+
a.band === b.band &&
|
|
902
|
+
a.bandTop === b.bandTop &&
|
|
903
|
+
a.lead === b.lead &&
|
|
904
|
+
a.column === b.column &&
|
|
203
905
|
a.used === b.used &&
|
|
906
|
+
a.bandBottom === b.bandBottom &&
|
|
204
907
|
a.previousMargin === b.previousMargin &&
|
|
205
908
|
a.onPage === b.onPage &&
|
|
206
909
|
a.breakPending === b.breakPending &&
|
|
207
|
-
a.
|
|
910
|
+
a.columnPending === b.columnPending &&
|
|
911
|
+
a.forcedPage === b.forcedPage &&
|
|
912
|
+
a.owner === b.owner);
|
|
208
913
|
}
|
|
209
914
|
//# sourceMappingURL=flow.js.map
|