1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//! 縦の並びをページに割り付ける
//!
//! 上から順に積み、入らなくなったら直前の切れ目まで戻して切る。脚注はそれを載せる行と
//! 同じページに置き、そのぶん本文に使える高さが減る。
//!
//! 浮かせるものは、収まる最初のページの先頭に置く。`[h]` と書いてあって、その場に
//! 入るときだけはそこに置く。1 ページに使ってよい割合は [`Style::float_fraction`] で決める。
use crate::draw::{self, Letter, Mark, Page};
use crate::font::{self, Library};
use crate::layout::{Block, Note};
use crate::line;
use crate::number::Numbering;
use crate::pdf::Destination;
use crate::style::{self, Style};
/// まだ置いていない浮かせるもの
#[derive(Debug, Clone)]
struct Waiting {
mark: Vec<Mark>,
height: f64,
anchor: Vec<String>,
}
/// 縦の並びをページに割り付ける
pub fn paginate(
block: &[Block],
library: &Library,
style: &Style,
numbering: &mut Numbering,
) -> (Vec<Page>, Vec<Destination>) {
let goal = style::body_height(style);
let mut out: Vec<Page> = Vec::new();
let mut destination = Vec::new();
let mut waiting: Vec<Waiting> = Vec::new();
let mut at = 0;
while at < block.len() || !waiting.is_empty() {
let number = out.len();
let (top, float_height) = take_floats(&mut waiting, style, goal);
let room = goal - float_height;
// 浮かせたものだけでページが埋まったら、本文はこのページには入れない
let (placed, note, end) = if room < style.leading {
(Vec::new(), Vec::new(), at)
} else {
take_body(block, at, room, style, &mut waiting)
};
let mut mark = Vec::new();
let mut y = 0.0;
for one in &top {
let mut copy = one.mark.clone();
draw::shift(&mut copy, style.margin_left, style.margin_top + y);
mark.extend(copy);
remember(numbering, &one.anchor, number + 1);
y += one.height + style.float_gap;
}
for (index, offset) in &placed {
let at_y = style.margin_top + float_height + offset;
match &block[*index] {
Block::Content {mark: inner, anchor, heading, ..} => {
let mut copy = inner.clone();
draw::shift(&mut copy, style.margin_left, at_y);
mark.extend(copy);
remember(numbering, anchor, number + 1);
if let Some((level, title)) = heading {
destination.push(Destination {
level: *level,
title: title.clone(),
page: number,
});
}
}
Block::Float {mark: inner, anchor, ..} => {
let mut copy = inner.clone();
draw::shift(&mut copy, style.margin_left, at_y);
mark.extend(copy);
remember(numbering, anchor, number + 1);
}
_ => {}
}
}
if !note.is_empty() {
place_notes(&mut mark, ¬e, style, goal);
}
if style.page_number {
mark.extend(page_number(library, style, number + 1));
}
out.push(Page {
mark,
width: style.page_width,
height: style.page_height,
});
at = if end == at && top.is_empty() && at < block.len() {
at + 1
} else {
end
};
}
if out.is_empty() {
out.push(Page {
mark: Vec::new(),
width: style.page_width,
height: style.page_height,
});
}
(out, destination)
}
/// 名前とページ番号を結び付ける
fn remember(numbering: &mut Numbering, anchor: &[String], page: usize) {
for name in anchor {
numbering.page.insert(name.clone(), page);
}
}
/// このページの先頭に置く浮かせるものを取る
fn take_floats(waiting: &mut Vec<Waiting>, style: &Style, goal: f64) -> (Vec<Waiting>, f64) {
let budget = goal * style.float_fraction;
let mut top = Vec::new();
let mut height = 0.0;
while !waiting.is_empty() {
let want = waiting[0].height + style.float_gap;
if top.is_empty() && want > goal {
// 1 つでページに入らないものは、そのページを丸ごと使う
height += want;
top.push(waiting.remove(0));
break;
}
if height + want > budget {
break;
}
height += want;
top.push(waiting.remove(0));
}
(top, height)
}
/// このページに入れる本文を決める
fn take_body(
block: &[Block],
from: usize,
room: f64,
style: &Style,
waiting: &mut Vec<Waiting>,
) -> (Vec<(usize, f64)>, Vec<Note>, usize) {
let mut placed: Vec<(usize, f64)> = Vec::new();
let mut note: Vec<Note> = Vec::new();
let mut used = 0.0;
let mut mark: Option<(usize, usize, usize)> = None;
let mut at = from;
let mut forced = false;
while at < block.len() {
match &block[at] {
Block::NewPage => {
at += 1;
forced = true;
break;
}
Block::Break {penalty} => {
if *penalty < line::FORBIDDEN && !placed.is_empty() {
mark = Some((at, placed.len(), note.len()));
}
at += 1;
}
Block::Space {height, ..} => {
if !placed.is_empty() {
used += height;
}
at += 1;
}
Block::Float {placement, mark: inner, height, anchor} => {
let here = placement.contains('h') || placement.contains('H');
// その場に置くときも、本文とのあいだは空ける
let lead = if placed.is_empty() {0.0} else {style.float_gap};
let want = lead + height + style.float_gap + note_area(¬e, style);
if here && used + want <= room {
used += lead;
placed.push((at, used));
used += height + style.float_gap;
} else {
waiting.push(Waiting {
mark: inner.clone(),
height: *height,
anchor: anchor.clone(),
});
}
at += 1;
}
Block::Content {height, note: mine, ..} => {
let mut after = note.clone();
after.extend(mine.iter().cloned());
let total = used + height + note_area(&after, style);
if total > room && !placed.is_empty() {
break;
}
placed.push((at, used));
used += height;
note = after;
at += 1;
}
}
}
if at >= block.len() || forced {
return (placed, note, at);
}
match mark {
Some((index, keep, note_keep)) => {
placed.truncate(keep);
note.truncate(note_keep);
(placed, note, index + 1)
}
None => (placed, note, at),
}
}
/// 脚注の欄が取る高さ
fn note_area(note: &[Note], style: &Style) -> f64 {
if note.is_empty() {
return 0.0;
}
style.footnote_gap + note.iter().map(|one| one.height).sum::<f64>()
}
/// 脚注をページの下に置く
fn place_notes(mark: &mut Vec<Mark>, note: &[Note], style: &Style, goal: f64) {
let total: f64 = note.iter().map(|one| one.height).sum();
let base = style.margin_top + goal - total;
mark.push(Mark::Rule {
x: style.margin_left,
y: base - style.footnote_gap * 0.6,
width: style.footnote_rule,
height: style.rule_width,
});
let mut y = base;
for one in note {
let mut copy = one.mark.clone();
draw::shift(&mut copy, style.margin_left, y);
mark.extend(copy);
y += one.height;
}
}
/// ノンブルを置く
fn page_number(library: &Library, style: &Style, number: usize) -> Vec<Mark> {
let index = library.role.roman;
let face = &library.face[index];
let size = style.size * 0.9;
let mut letter = Vec::new();
let mut width = 0.0;
for character in number.to_string().chars() {
if let Some(glyph) = font::glyph(face, character) {
width += font::advance(face, glyph) * size;
letter.push(Letter {glyph, text: character.to_string()});
}
}
if letter.is_empty() {
return Vec::new();
}
vec![Mark::Text {
face: index,
size,
stretch: 1.0,
x: style.page_width / 2.0 - width / 2.0,
y: style.page_height - style.margin_bottom * 0.45,
letter,
}]
}