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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//! Hyphenation in the paragraph builder's second pass: `tex.web` §891-§918.
//!
//! When the first pass finds no feasible breaks, §866 tries to hyphenate the
//! word after every glue it passes. §894-§899 find the word -- its letters by
//! `\lccode`, in one font, between something that stops it -- §923's
//! patterns (here `crate::linebreak::Hyphenator`) say where it may break,
//! and §903-§918 rebuild it with `reconstitute`, the main loop's ligature and
//! kern machinery run again over the letters, so that a discretionary's
//! pre-break text ends in the font's hyphen character ligatured and kerned as
//! tex sets it, and the post-break text starts as a new line would.
use super::font::Font;
use super::Stomach;
use crate::node::{CharNode, DiscNode, Ligature, Node};
/// §549's `non_char`.
const NON_CHAR: u16 = 256;
const STOP_FLAG: u8 = 128;
const KERN_FLAG: u8 = 128;
/// One `lig_stack` item of §907: a character to be set, holding (when it
/// replaced one) the character node `hu[j+1]` it stood for.
#[derive(Clone, Copy)]
struct Item {
c: u16,
lig_ptr: Option<u16>,
}
/// §905-§907's state for one word.
struct Word<'a> {
font: &'a Font,
f: usize,
/// `hu[0..=hn]`: the word's characters, `hu[0]` the one before it.
hu: Vec<u16>,
/// `hyf[0..=hn]`: odd where a hyphen may go after letter `j`.
hyf: Vec<u8>,
/// `init_list`, `init_lig`, `init_lft` (§903).
init_list: Vec<u16>,
init_lig: bool,
init_lft: bool,
/// `link(hold_head)`: what `reconstitute` built.
hold: Vec<Node>,
hyphen_passed: usize,
}
impl Word<'_> {
fn char_node(&self, c: u16) -> Node {
let ch = char::from(c as u8);
let m = self.font.metrics(ch).unwrap_or_default();
Node::Char(CharNode {
font: self.f,
character: ch,
width: m.width,
height: m.height,
depth: m.depth,
})
}
/// `reconstitute(j,n,bchar,hchar)` (§906-§911): translate `hu[j..]` as far
/// as the ligature program goes in one cursor sweep, into `hold`, and
/// return the index of the last character consumed.
fn reconstitute(&mut self, mut j: usize, n: usize, mut bchar: u16, mut hchar: u16) -> usize {
self.hyphen_passed = 0;
self.hold.clear();
let mut w: crate::node::Scaled = 0;
let mut ligature_present;
let mut lft_hit = false;
let mut rt_hit = false;
let mut lig_stack: Vec<Item> = Vec::new();
// §908.
let mut cur_l = self.hu[j];
let mut cur_q = 0usize;
if j == 0 {
ligature_present = self.init_lig;
if ligature_present {
lft_hit = self.init_lft;
}
for c in self.init_list.clone() {
let node = self.char_node(c);
self.hold.push(node);
}
} else {
ligature_present = false;
if cur_l < NON_CHAR {
let node = self.char_node(cur_l);
self.hold.push(node);
}
}
let set_cur_r = |w: &Word, j: usize, bchar: u16, hchar: u16| -> (u16, u16) {
let r = if j < n { w.hu[j + 1] } else { bchar };
let rh = if w.hyf[j] % 2 == 1 { hchar } else { NON_CHAR };
(r, rh)
};
let (mut cur_r, mut cur_rh) = set_cur_r(self, j, bchar, hchar);
// §910's wrap_lig: the characters linked since `cur_q` become a
// ligature node when one is being built.
let wrap = |s: &mut Word,
cur_q: usize,
cur_l: u16,
lp: &mut bool,
lft: &mut bool,
rt: &mut bool,
right: bool,
stack_empty: bool| {
if !*lp {
return;
}
let components: Vec<char> = s
.hold
.drain(cur_q..)
.filter_map(|n| match n {
Node::Char(c) => Some(c.character),
_ => None,
})
.collect();
let mut lig = Ligature {
components,
left_boundary: false,
right_boundary: false,
};
if *lft {
lig.left_boundary = true;
*lft = false;
}
if right && stack_empty {
lig.right_boundary = true;
*rt = false;
}
let Node::Char(c) = s.char_node(cur_l) else {
return;
};
s.hold.push(Node::Ligature(c, lig));
*lp = false;
};
let mut budget = 64 * (self.hu.len() + 8);
'continue_: loop {
budget -= 1;
if budget == 0 {
return j;
}
// §909: the lig/kern program of `cur_l`, looking for `test_char`.
'done: {
let start = match cur_l == NON_CHAR {
true => self.font.lig_start(NON_CHAR),
false => self.font.lig_start(cur_l),
};
let Some(mut k) = start else {
break 'done;
};
let test_char = if cur_rh < NON_CHAR { cur_rh } else { cur_r };
loop {
let Some([skip, next, op, rem]) = self.font.lig_word(k) else {
break 'done;
};
if u16::from(next) == test_char && skip <= STOP_FLAG {
if cur_rh < NON_CHAR {
self.hyphen_passed = j;
hchar = NON_CHAR;
cur_rh = NON_CHAR;
continue 'continue_;
}
if hchar < NON_CHAR && self.hyf[j] % 2 == 1 {
self.hyphen_passed = j;
hchar = NON_CHAR;
}
if op < KERN_FLAG {
// §911.
if cur_l == NON_CHAR {
lft_hit = true;
}
if j == n && lig_stack.is_empty() {
rt_hit = true;
}
let rem = u16::from(rem);
match op {
1 | 5 => {
cur_l = rem;
ligature_present = true;
}
2 | 6 => {
cur_r = rem;
match lig_stack.last_mut() {
Some(top) => top.c = rem,
None => {
let lig_ptr = match j == n {
true => {
bchar = NON_CHAR;
None
}
false => Some(self.hu[j + 1]),
};
lig_stack.push(Item { c: rem, lig_ptr });
}
}
}
3 => {
cur_r = rem;
lig_stack.push(Item {
c: rem,
lig_ptr: None,
});
}
7 | 11 => {
let empty = lig_stack.is_empty();
wrap(
self,
cur_q,
cur_l,
&mut ligature_present,
&mut lft_hit,
&mut rt_hit,
false,
empty,
);
cur_q = self.hold.len();
cur_l = rem;
ligature_present = true;
}
_ => {
cur_l = rem;
ligature_present = true;
if let Some(top) = lig_stack.pop() {
// pop_lig_stack
if let Some(c) = top.lig_ptr {
let node = self.char_node(c);
self.hold.push(node);
j += 1;
}
match lig_stack.last() {
None => {
(cur_r, cur_rh) = set_cur_r(self, j, bchar, hchar)
}
Some(t) => cur_r = t.c,
}
} else if j == n {
break 'done;
} else {
let node = self.char_node(cur_r);
self.hold.push(node);
j += 1;
(cur_r, cur_rh) = set_cur_r(self, j, bchar, hchar);
}
}
}
if op > 4 && op != 7 {
break 'done;
}
continue 'continue_;
}
w = self.font.kern(op, rem);
break 'done;
}
if skip >= STOP_FLAG {
if cur_rh == NON_CHAR {
break 'done;
}
cur_rh = NON_CHAR;
continue 'continue_;
}
k += usize::from(skip) + 1;
}
}
// §910.
let empty = lig_stack.is_empty();
let right = rt_hit;
wrap(
self,
cur_q,
cur_l,
&mut ligature_present,
&mut lft_hit,
&mut rt_hit,
right,
empty,
);
if w != 0 {
self.hold.push(Node::Kern {
width: w,
explicit: false,
});
w = 0;
}
if let Some(top) = lig_stack.pop() {
cur_q = self.hold.len();
cur_l = top.c;
ligature_present = true;
if let Some(c) = top.lig_ptr {
let node = self.char_node(c);
self.hold.push(node);
j += 1;
}
match lig_stack.last() {
None => (cur_r, cur_rh) = set_cur_r(self, j, bchar, hchar),
Some(t) => cur_r = t.c,
}
continue 'continue_;
}
return j;
}
}
}
/// `set_lc_code` (§897): the `\lccode` of `c`; INITEX's for the letters
/// unless the document has said otherwise.
fn lc_code(s: &Stomach, c: char) -> u16 {
match s.lc_codes.get(&c) {
Some(v) => *v as u16,
None if c.is_ascii_lowercase() => c as u16,
None if c.is_ascii_uppercase() => c.to_ascii_lowercase() as u16,
None => 0,
}
}
impl Stomach {
/// §894: "Try to hyphenate the following word" after the glue at
/// `cur_p`, replacing its nodes by the reconstituted word with
/// discretionaries in it.
pub fn hyphenate_after(&mut self, list: &mut Vec<Node>, cur_p: usize) {
let Some(hyph) = self.hyphenator() else {
return;
};
let l_hyf = self.int_par("lefthyphenmin").clamp(1, 63) as usize;
let r_hyf = self.int_par("righthyphenmin").clamp(1, 63) as usize;
if l_hyf + r_hyf > 63 {
return;
}
let uc_hyph = self.int_par("uchyph");
// §896: skip to node `ha`.
let mut prev_s = cur_p;
let mut s = cur_p + 1;
let (hf, first) = loop {
let Some(node) = list.get(s) else {
return;
};
let (c, f) = match node {
Node::Char(c) => (c.character, c.font),
Node::Ligature(c, lig) => match lig.components.first() {
None => {
prev_s = s;
s += 1;
continue;
}
Some(&k) => (k, c.font),
},
Node::Kern {
explicit: false, ..
}
| Node::Whatsit(_)
| Node::Out(_) => {
prev_s = s;
s += 1;
continue;
}
_ => return,
};
let lc = lc_code(self, c);
if lc != 0 {
if lc == c as u16 || uc_hyph > 0 {
break (f, s);
}
return;
}
prev_s = s;
s += 1;
};
let font = self.font(hf).clone();
let hyf_char = font.hyphen_char;
if !(0..=255).contains(&hyf_char) {
return;
}
let ha = prev_s;
// §897: the letters, into `hu` and `hc`.
let mut hu: Vec<u16> = vec![0];
let mut hc: Vec<u8> = Vec::new();
let mut hb = first;
let mut hyf_bchar = NON_CHAR;
s = first;
loop {
match list.get(s) {
Some(Node::Char(c)) => {
if c.font != hf {
break;
}
// `hyf_bchar:=character(s)` BEFORE the letter test: when
// this character ends the word, it is the right
// neighbour the word's last letter is reconstituted
// against (§903's `bchar`), so the kern or ligature
// between them -- cmr10's `y.` -- comes back.
hyf_bchar = c.character as u16;
let lc = lc_code(self, c.character);
if lc == 0 || hu.len() > 63 {
break;
}
hb = s;
hu.push(c.character as u16);
hc.push(lc as u8);
hyf_bchar = NON_CHAR;
}
// §898.
Some(Node::Ligature(c, lig)) => {
if c.font != hf {
break;
}
let mut letters = Vec::new();
let mut ok = true;
for &k in &lig.components {
let lc = lc_code(self, k);
if lc == 0 || hu.len() + letters.len() > 63 {
ok = false;
break;
}
letters.push((k as u16, lc as u8));
}
if !ok {
break;
}
for (k, lc) in letters {
hu.push(k);
hc.push(lc);
}
hb = s;
hyf_bchar = match lig.right_boundary {
true => font.bchar_code(),
false => NON_CHAR,
};
}
Some(Node::Kern {
explicit: false, ..
}) => {
hb = s;
hyf_bchar = font.bchar_code();
}
_ => break,
}
s += 1;
}
let hn = hu.len() - 1;
// §899: what follows the word must permit hyphenation.
if hn < l_hyf + r_hyf {
return;
}
let mut t = s;
loop {
match list.get(t) {
None => break,
Some(Node::Char(_)) | Some(Node::Ligature(..)) => {}
Some(Node::Kern {
explicit: false, ..
}) => {}
Some(Node::Kern { .. }) => break,
Some(
Node::Whatsit(_)
| Node::Out(_)
| Node::Glue(_)
| Node::Penalty(_)
| Node::Ins(_)
| Node::Adjust(_)
| Node::Mark(_),
) => break,
Some(_) => return,
}
t += 1;
}
// §923: the patterns (and exceptions) say where.
let word: String = hc.iter().map(|&b| b as char).collect();
let mut hyf = vec![0u8; hn + 2];
for at in hyph.points(&word) {
if at >= l_hyf && at <= hn.saturating_sub(r_hyf) {
hyf[at] = 1;
}
}
// §902.
if !(l_hyf..=hn.saturating_sub(r_hyf)).any(|j| hyf[j] % 2 == 1) {
return;
}
self.reconstitute_word(
list,
cur_p,
ha,
hb,
hf,
&font,
hu,
hyf,
hyf_char as u16,
hyf_bchar,
);
}
/// §903 and §913-§918: replace `ha..hb` by the word rebuilt, with the
/// discretionary hyphens in it.
#[allow(clippy::too_many_arguments)]
fn reconstitute_word(
&mut self,
list: &mut Vec<Node>,
cur_p: usize,
ha: usize,
hb: usize,
hf: usize,
font: &Font,
mut hu: Vec<u16>,
hyf: Vec<u8>,
hyf_char: u16,
hyf_bchar: u16,
) {
let hn = hu.len() - 1;
let bchar = hyf_bchar;
let mut w = Word {
font,
f: hf,
hu: Vec::new(),
hyf,
init_list: Vec::new(),
init_lig: false,
init_lft: false,
hold: Vec::new(),
hyphen_passed: 0,
};
// §903: where the rebuilt nodes go (`s`), and where they start (`j`).
let found2 = |hu: &mut Vec<u16>| -> (usize, usize) {
hu[0] = NON_CHAR;
(ha, 0)
};
let (keep_through, mut j) = match &list[ha] {
Node::Char(c) if c.font == hf => {
w.init_list = vec![c.character as u16];
hu[0] = c.character as u16;
(ha - 1, 0)
}
Node::Char(_) => found2(&mut hu),
Node::Ligature(c, lig) if c.font == hf => {
w.init_list = lig.components.iter().map(|&k| k as u16).collect();
w.init_lig = true;
w.init_lft = lig.left_boundary;
hu[0] = c.character as u16;
if w.init_list.is_empty() && w.init_lft {
hu[0] = NON_CHAR;
w.init_lig = false;
}
(ha - 1, 0)
}
Node::Ligature(..) => found2(&mut hu),
_ => {
// No punctuation found; look for the left boundary.
if let Some(Node::Ligature(_, lig)) = list.get(ha + 1) {
if lig.left_boundary {
let (k, j0) = found2(&mut hu);
let _ = cur_p;
return self.finish_word(list, k, hb, w, hu, j0, hn, bchar, hyf_char);
}
}
(ha, 1)
}
};
let _ = &mut j;
self.finish_word(list, keep_through, hb, w, hu, j, hn, bchar, hyf_char);
}
/// §913-§918 proper, splicing the result in after `keep_through`.
#[allow(clippy::too_many_arguments)]
fn finish_word(
&mut self,
list: &mut Vec<Node>,
keep_through: usize,
hb: usize,
mut w: Word,
hu: Vec<u16>,
mut j: usize,
hn: usize,
bchar: u16,
hyf_char: u16,
) {
w.hu = hu;
let font = w.font;
let mut out: Vec<Node> = Vec::new();
loop {
// §913.
let mut l = j;
j = w.reconstitute(j, hn, bchar, hyf_char) + 1;
if w.hyphen_passed == 0 {
out.append(&mut w.hold);
if w.hyf[j - 1] % 2 == 1 {
l = j;
w.hyphen_passed = j - 1;
w.hold.clear();
}
}
if w.hyphen_passed > 0 {
// §914.
loop {
let mut major: Vec<Node> = std::mem::take(&mut w.hold);
let mut i = w.hyphen_passed;
w.hyf[i] = 0;
// §915: the pre-break text, `hu[l..i]` and a hyphen.
let mut pre: Vec<Node> = Vec::new();
let hyf_node = font.metrics(char::from(hyf_char as u8)).is_some();
let mut saved = 0u16;
if hyf_node {
i += 1;
saved = w.hu[i];
w.hu[i] = hyf_char;
}
while l <= i {
l = w.reconstitute(l, i, font.bchar_code(), NON_CHAR) + 1;
pre.append(&mut w.hold);
}
if hyf_node {
w.hu[i] = saved;
l = i;
i -= 1;
}
let _ = i;
// §916: the post-break text, and the replacement grown
// until the two branches agree again.
let mut post: Vec<Node> = Vec::new();
let mut c_loc = 0usize;
let mut c = 0u16;
if font.lig_start(NON_CHAR).is_some() {
l -= 1;
c = w.hu[l];
c_loc = l;
w.hu[l] = NON_CHAR;
}
while l < j {
loop {
l = w.reconstitute(l, hn, bchar, NON_CHAR) + 1;
if c_loc > 0 {
w.hu[c_loc] = c;
c_loc = 0;
}
post.append(&mut w.hold);
if l >= j {
break;
}
}
while l > j {
// §917.
j = w.reconstitute(j, hn, bchar, NON_CHAR) + 1;
major.append(&mut w.hold);
}
}
// §918.
let r_count = major.len();
if r_count <= 127 {
out.push(Node::Disc(DiscNode {
pre_break: pre,
post_break: post,
replace_count: r_count,
}));
}
out.append(&mut major);
w.hyphen_passed = j - 1;
w.hold.clear();
if w.hyf[j - 1].is_multiple_of(2) {
break;
}
}
}
if j > hn {
break;
}
}
// `link(s):=q`.
let tail = list.split_off(hb + 1);
list.truncate(keep_through + 1);
list.extend(out);
list.extend(tail);
}
/// The patterns as a hyphenator, built the first time a paragraph needs
/// one.
fn hyphenator(&mut self) -> Option<std::rc::Rc<crate::linebreak::Hyphenator>> {
if self.hyphenation_source.is_empty() {
return None;
}
if self.hyphenator.is_none() {
let h = crate::linebreak::Hyphenator::from_source(&self.hyphenation_source);
self.hyphenator = Some(std::rc::Rc::new(h));
}
self.hyphenator.clone()
}
}
impl Stomach {
/// Every word after a glue node of the paragraph, hyphenated (§866's
/// "Try to hyphenate the following word", for the whole pass): after
/// glue where `auto_breaking` holds, which is not inside a formula.
pub fn hyphenate_all(&mut self, list: &mut Vec<Node>) {
let mut auto_breaking = true;
let mut i = 0;
while i < list.len() {
match list[i] {
Node::Math(_) => auto_breaking = false,
Node::MathOff(_) => auto_breaking = true,
Node::Glue(_) if auto_breaking => self.hyphenate_after(list, i),
_ => {}
}
i += 1;
}
}
}