1use crate::pdfium_backend::{Glyph, TextCell};
15
16const MERGE: f64 = 1.0; const MERGE_WITH_SPACE: f64 = 0.33; const WORD_MERGE: f64 = 0.33; const WORD_MERGE_WITH_SPACE: f64 = 2.0 * WORD_MERGE;
28const H_TOL: f64 = 1.0; #[derive(Clone)]
31struct Cell {
32 text: String,
33 rx0: f64,
34 ry0: f64, rx1: f64,
36 ry1: f64, rx2: f64,
38 ry2: f64, rx3: f64,
40 ry3: f64, ltr: bool,
42 active: bool,
43 lig_carry: bool, font: u64, blank: bool, lig: bool, fb: bool, nchars: usize, b_l: f64, b_r: f64,
56 b_b: f64,
57 b_t: f64,
58 pen: Option<[f64; 3]>,
61 math_font: bool,
63}
64
65impl Cell {
66 fn length(&self) -> f64 {
68 ((self.rx1 - self.rx0).powi(2) + (self.ry1 - self.ry0).powi(2)).sqrt()
69 }
70
71 fn avg_char_width(&self) -> f64 {
73 if self.nchars > 0 {
74 self.length() / self.nchars as f64
75 } else {
76 0.0
77 }
78 }
79
80 fn gap(&self, other: &Cell) -> f64 {
82 ((self.rx1 - other.rx0).powi(2) + (self.ry1 - other.ry0).powi(2)).sqrt()
83 }
84
85 fn adjacent(&self, other: &Cell, eps0: f64, eps1: f64) -> bool {
89 let d0 = self.gap(other);
90 let d1 = ((self.rx2 - other.rx3).powi(2) + (self.ry2 - other.ry3).powi(2)).sqrt();
91 d0 < eps0 && d1 < eps1
92 }
93
94 fn same_orientation(&self, other: &Cell) -> bool {
96 self.ltr == other.ltr || is_punct_or_space(&self.text) || is_punct_or_space(&other.text)
97 }
98
99 fn merge_with(&mut self, other: &Cell, delta: f64, euclidean: bool) {
110 let gap = if euclidean {
111 self.gap(other)
112 } else {
113 other.rx0 - self.rx1
114 };
115 let space = delta < gap;
116 if !self.ltr || !other.ltr {
117 if space {
118 self.text.insert(0, ' ');
119 }
120 self.text = format!("{}{}", other.text, self.text);
121 self.ltr = false;
122 } else {
123 if space {
124 self.text.push(' ');
125 }
126 self.text.push_str(&other.text);
127 self.ltr = true;
128 }
129 self.rx1 = other.rx1;
131 self.ry1 = other.ry1;
132 self.rx2 = other.rx2;
133 self.ry2 = other.ry2;
134 self.blank = self.blank && other.blank;
141 self.nchars += other.nchars + usize::from(space);
142 self.fb = self.fb || other.fb;
143 self.lig = if self.text.len() <= 3 {
144 is_ligature(&self.text)
145 } else {
146 self.fb
147 };
148 let (l, r, b, t) = quad_bounds(self);
149 self.b_l = l;
150 self.b_r = r;
151 self.b_b = b;
152 self.b_t = t;
153 }
154}
155
156fn bounds(c: &Cell) -> (f64, f64, f64, f64) {
159 (c.b_l, c.b_r, c.b_b, c.b_t)
160}
161
162fn quad_bounds(c: &Cell) -> (f64, f64, f64, f64) {
164 let xs = [c.rx0, c.rx1, c.rx2, c.rx3];
165 let ys = [c.ry0, c.ry1, c.ry2, c.ry3];
166 let fold = |it: &[f64], f: fn(f64, f64) -> f64| it.iter().copied().reduce(f).unwrap();
167 (
168 fold(&xs, f64::min),
169 fold(&xs, f64::max),
170 fold(&ys, f64::min),
171 fold(&ys, f64::max),
172 )
173}
174
175fn gap_occupied(cells: &[Cell], i: usize, j: usize) -> bool {
184 let (al, ar, ab, at) = bounds(&cells[i]);
185 let (bl, br, bb, bt) = bounds(&cells[j]);
186 let (gl, gr) = if ar <= bl { (ar, bl) } else { (br, al) };
187 if gr - gl < 0.5 {
188 return false; }
190 let (band_b, band_t) = (ab.min(bb), at.max(bt));
191 cells.iter().enumerate().any(|(k, c)| {
192 if k == i || k == j || !c.active || c.blank {
193 return false;
194 }
195 let (cl, cr, cb, ct) = bounds(c);
196 let overlap = (ct.min(band_t) - cb.max(band_b)).max(0.0);
198 overlap > 0.5 * (ct - cb).max(f64::EPSILON)
199 && cr.min(gr) - cl.max(gl) > 0.1
201 })
202}
203
204fn applicable(a: &Cell, b: &Cell, parser: bool, block_spaces: bool) -> bool {
210 if !a.active || !b.active {
211 return false;
212 }
213 if block_spaces && (is_all_space(&a.text) || is_all_space(&b.text)) {
217 return false;
218 }
219 let lone_punct = |s: &str| {
225 let mut ch = s.chars();
226 matches!(ch.next(), Some(c) if c != ' ' && is_punct_or_space(&c.to_string()))
227 && ch.next().is_none()
228 };
229 let punct_bridge =
230 parser && ((lone_punct(&a.text) && !b.ltr) || (lone_punct(&b.text) && !a.ltr));
231 let font_neutral = a.lig || b.lig || punct_bridge;
232 if a.font != 0 && b.font != 0 && a.font != b.font && !font_neutral {
233 return false;
234 }
235 a.same_orientation(b)
236}
237
238fn pass_ltr(cells: &mut [Cell], allow_reverse: bool, euclidean: bool, p: Factors) {
240 for i in 0..cells.len() {
241 if !cells[i].active {
242 continue;
243 }
244 let mut j = i + 1;
245 while j < cells.len() {
246 if !applicable(&cells[i], &cells[j], euclidean, p.block_spaces) {
247 break;
248 }
249 let i_lig = cells[i].lig || cells[i].lig_carry;
250 let j_lig = cells[j].lig || cells[j].lig_carry;
251 let d0 = cells[i].avg_char_width() * p.merge;
252 let d1 = cells[i].avg_char_width() * p.merge_with_space;
253 let adj_d1 = d0 + if i_lig || j_lig { H_TOL } else { 0.0 };
254 if cells[i].adjacent(&cells[j], d0, adj_d1) && !gap_occupied(cells, i, j) {
255 let other = cells[j].clone();
256 cells[i].merge_with(&other, d1, euclidean);
257 cells[i].lig_carry = other.lig;
258 cells[j].active = false;
259 j += 1; } else if allow_reverse
261 && cells[j].adjacent(&cells[i], d0, adj_d1)
262 && !gap_occupied(cells, j, i)
263 {
264 let other = cells[i].clone();
265 cells[j].merge_with(&other, d1, euclidean);
266 cells[j].lig_carry = other.lig;
267 cells[i].active = false;
268 break; } else {
270 break;
271 }
272 }
273 }
274}
275
276fn pass_rtl(cells: &mut [Cell], euclidean: bool, p: Factors) {
279 let n = cells.len();
280 for k in 0..n {
281 let i = n - 1 - k;
282 if !cells[i].active || i == 0 {
283 continue;
284 }
285 let j = i - 1;
286 if !applicable(&cells[i], &cells[j], euclidean, p.block_spaces) {
287 continue;
288 }
289 let i_lig = cells[i].lig || cells[i].lig_carry;
290 let j_lig = cells[j].lig || cells[j].lig_carry;
291 let d0 = cells[i].avg_char_width() * p.merge;
292 let d1 = cells[i].avg_char_width() * p.merge_with_space;
293 let adj_d1 = d0 + if i_lig || j_lig { H_TOL } else { 0.0 };
294 if cells[j].adjacent(&cells[i], d0, adj_d1) && !gap_occupied(cells, j, i) {
295 let other = cells[i].clone();
296 cells[j].merge_with(&other, d1, euclidean);
297 cells[j].lig_carry = other.lig;
298 cells[i].active = false;
299 }
300 }
301}
302
303#[derive(Clone, Copy)]
306struct Factors {
307 merge: f64,
308 merge_with_space: f64,
309 block_spaces: bool,
310}
311
312const LINE_FACTORS: Factors = Factors {
313 merge: MERGE,
314 merge_with_space: MERGE_WITH_SPACE,
315 block_spaces: false,
316};
317const WORD_FACTORS: Factors = Factors {
318 merge: WORD_MERGE,
319 merge_with_space: WORD_MERGE_WITH_SPACE,
320 block_spaces: true,
321};
322
323fn is_all_space(s: &str) -> bool {
325 !s.is_empty() && s.chars().all(char::is_whitespace)
326}
327
328fn contract(cells: &mut Vec<Cell>, euclidean: bool, p: Factors) {
329 pass_ltr(cells, false, euclidean, p);
330 cells.retain(|c| c.active);
331 pass_rtl(cells, euclidean, p);
332 cells.retain(|c| c.active);
333 pass_ltr(cells, true, euclidean, p);
334 cells.retain(|c| c.active);
335}
336
337fn build_cells(glyphs: &[Glyph], euclidean: bool) -> Vec<Cell> {
340 let mut cells: Vec<Cell> = Vec::new();
341 for g in glyphs {
342 if !g.ll.is_finite() {
345 continue;
346 }
347 let q = g.quad.map(|q| q.map(f64::from)).unwrap_or_else(|| {
352 let (l, b, r, t) = (g.ll as f64, g.lb as f64, g.lr as f64, g.lt as f64);
353 [l, b, r, b, r, t, l, t]
354 });
355 if g.ch == ' ' && (q[2] - q[0]).hypot(q[3] - q[1]) < 0.5 {
362 continue;
363 }
364 if let Some(last) = cells.last_mut() {
374 let near =
375 |ax: f64, ay: f64, bx: f64, by: f64| (ax - bx).abs() < 0.5 && (ay - by).abs() < 0.5;
376 if near(last.rx0, last.ry0, q[0], q[1]) && near(last.rx1, last.ry1, q[2], q[3]) {
377 let offset = match g.quad {
385 None => (q[0] - last.rx0).abs(),
386 Some(_) => (q[0] - last.rx0).hypot(q[1] - last.ry0),
387 };
388 if euclidean && offset > 0.1 && last.text.ends_with(g.ch) {
389 continue;
390 }
391 last.text.push(g.ch);
392 last.ltr = !is_right_to_left(&last.text);
393 last.blank = last.blank && g.ch.is_whitespace();
394 last.nchars += 1;
395 last.fb = last.fb || (0xFB00..=0xFB06).contains(&(g.ch as u32));
397 last.lig = is_ligature(&last.text);
398 continue;
399 }
400 }
401 let text = g.ch.to_string();
402 let ltr = !is_right_to_left(&text);
403 let blank = g.ch.is_whitespace();
404 let lig = is_ligature(&text);
405 let fb = (0xFB00..=0xFB06).contains(&(g.ch as u32));
406 cells.push(Cell {
407 text,
408 rx0: q[0],
409 ry0: q[1],
410 rx1: q[2],
411 ry1: q[3],
412 rx2: q[4],
413 ry2: q[5],
414 rx3: q[6],
415 ry3: q[7],
416 ltr,
417 active: true,
418 lig_carry: false,
419 font: g.font,
420 blank,
421 lig,
422 fb,
423 nchars: 1,
424 b_l: (g.ll as f64).min(g.lr as f64),
425 b_r: (g.ll as f64).max(g.lr as f64),
426 b_b: (g.lb as f64).min(g.lt as f64),
427 b_t: (g.lb as f64).max(g.lt as f64),
428 pen: g.pen.map(|p| p.map(f64::from)),
429 math_font: g.math_font,
430 });
431 }
432 cells
433}
434
435pub(crate) fn line_cells(glyphs: &[Glyph], page_h: f32, euclidean: bool) -> Vec<TextCell> {
438 line_and_word_cells_with(glyphs, page_h, euclidean, WordGap::from_env()).0
439}
440
441pub(crate) fn word_cells(glyphs: &[Glyph], page_h: f32, euclidean: bool) -> Vec<TextCell> {
449 line_and_word_cells_with(glyphs, page_h, euclidean, WordGap::from_env()).1
450}
451
452#[cfg(test)]
459pub(crate) fn line_and_word_cells(
460 glyphs: &[Glyph],
461 page_h: f32,
462 euclidean: bool,
463) -> (Vec<TextCell>, Vec<TextCell>) {
464 line_and_word_cells_with(glyphs, page_h, euclidean, WordGap::Legacy)
465}
466
467pub(crate) fn line_and_word_cells_with(
470 glyphs: &[Glyph],
471 page_h: f32,
472 euclidean: bool,
473 gap: WordGap,
474) -> (Vec<TextCell>, Vec<TextCell>) {
475 let built = build_cells(glyphs, euclidean);
476 if gap != WordGap::Legacy {
477 return contract_runs(&built, gap, page_h);
478 }
479 let to_text_cell = |c: Cell| {
480 let l = c.rx0.min(c.rx1).min(c.rx2).min(c.rx3) as f32;
481 let r = c.rx0.max(c.rx1).max(c.rx2).max(c.rx3) as f32;
482 let top = c.ry0.max(c.ry1).max(c.ry2).max(c.ry3) as f32;
483 let bot = c.ry0.min(c.ry1).min(c.ry2).min(c.ry3) as f32;
484 TextCell {
485 text: c.text,
486 l,
487 t: page_h - top,
488 r,
489 b: page_h - bot,
490 }
491 };
492 let mut word_run: Vec<Cell> = built.clone();
501 let mut cells = built;
502 contract(&mut cells, euclidean, LINE_FACTORS);
503 let lines = to_text_cells(cells, to_text_cell);
504 contract(&mut word_run, euclidean, WORD_FACTORS);
505 word_run.retain(|c| !c.text.trim().is_empty());
506 let words = to_text_cells(word_run, to_text_cell);
507 (lines, words)
508}
509
510fn to_text_cells(cells: Vec<Cell>, f: impl Fn(Cell) -> TextCell) -> Vec<TextCell> {
520 let mut out = Vec::with_capacity(cells.len());
521 out.extend(cells.into_iter().map(f));
522 out
523}
524
525fn is_rtl_char(c: char) -> bool {
526 let ch = c as u32;
527 (0x0600..=0x06FF).contains(&ch)
528 || (0x0750..=0x077F).contains(&ch)
529 || (0x08A0..=0x08FF).contains(&ch)
530 || (0xFB50..=0xFDFF).contains(&ch)
531 || (0xFE70..=0xFEFF).contains(&ch)
532 || (0x0590..=0x05FF).contains(&ch)
533 || (0xFB1D..=0xFB4F).contains(&ch)
534 || (0x0700..=0x074F).contains(&ch)
535 || (0x0780..=0x07BF).contains(&ch)
536 || (0x07C0..=0x07FF).contains(&ch)
537}
538
539fn is_right_to_left(s: &str) -> bool {
541 !s.is_empty() && s.chars().all(is_rtl_char)
542}
543
544fn is_punct_or_space(s: &str) -> bool {
546 let mut chars = s.chars();
547 let (Some(c), None) = (chars.next(), chars.next()) else {
548 return false;
549 };
550 if matches!(
551 c,
552 ' ' | '\t'
553 | '\n'
554 | '\r'
555 | '\u{0c}'
556 | '\u{0b}'
557 | '.'
558 | ','
559 | ';'
560 | ':'
561 | '!'
562 | '?'
563 | '('
564 | ')'
565 | '['
566 | ']'
567 | '{'
568 | '}'
569 | '\''
570 | '"'
571 | '`'
572 | '\u{2018}'
573 | '\u{2019}'
574 | '\u{201c}'
575 | '\u{201d}'
576 | '-'
577 | '\u{2013}'
578 | '\u{2014}'
579 | '_'
580 | '/'
581 | '\\'
582 | '|'
583 | '@'
584 | '#'
585 | '%'
586 | '&'
587 | '*'
588 | '+'
589 | '='
590 | '<'
591 | '>'
592 ) {
593 return true;
594 }
595 let ch = c as u32;
596 (0x2000..=0x206F).contains(&ch)
597 || (0x3000..=0x303F).contains(&ch)
598 || (0xFE50..=0xFE6F).contains(&ch)
599 || (0xFF00..=0xFF0F).contains(&ch)
600 || (0xFF1A..=0xFF1F).contains(&ch)
601 || (0xFF3B..=0xFF5E).contains(&ch)
602}
603
604fn is_ligature(s: &str) -> bool {
606 matches!(s, "ff" | "fi" | "fl" | "ffi" | "ffl")
607 || s.chars().any(|c| (0xFB00..=0xFB06).contains(&(c as u32)))
608}
609
610#[derive(Clone, Copy, Debug, Default, PartialEq)]
633pub enum WordGap {
634 Legacy,
636 Factor(f64),
638 #[default]
642 Adaptive,
643}
644
645impl WordGap {
646 pub const ACCEPTED: &'static str = "legacy, adaptive, or a factor in (0, 4]";
648
649 pub fn parse(s: &str) -> Result<Self, String> {
652 let t = s.trim().to_ascii_lowercase();
653 match t.as_str() {
654 "legacy" => Ok(WordGap::Legacy),
655 "adaptive" | "auto" | "default" => Ok(WordGap::Adaptive),
656 _ => match t.parse::<f64>() {
657 Ok(f) if f.is_finite() && f > 0.0 && f <= 4.0 => Ok(WordGap::Factor(f)),
658 _ => Err(format!(
659 "pdf_word_gap must be {}, got {s:?}",
660 WordGap::ACCEPTED
661 )),
662 },
663 }
664 }
665
666 pub fn from_env() -> Self {
670 let Some(v) = docling_core::env::nonempty("DOCLING_RS_PDF_WORD_GAP") else {
671 return WordGap::default();
672 };
673 WordGap::parse(&v).unwrap_or_else(|e| {
674 static WARNED: std::sync::Once = std::sync::Once::new();
675 WARNED.call_once(|| {
676 eprintln!("docling-pdf: DOCLING_RS_PDF_WORD_GAP: {e}; using adaptive")
677 });
678 WordGap::default()
679 })
680 }
681}
682
683struct RunCell<'a> {
685 cell: &'a Cell,
686 ux: f64,
688 uy: f64,
689 advance: f64,
691 height: f64,
692 space: bool,
693}
694
695impl<'a> RunCell<'a> {
696 fn new(cell: &'a Cell) -> Self {
697 let (dx, dy) = (cell.rx1 - cell.rx0, cell.ry1 - cell.ry0);
698 let advance = dx.hypot(dy);
699 let (ux, uy) = if advance > 1e-9 {
700 (dx / advance, dy / advance)
701 } else {
702 (1.0, 0.0)
703 };
704 let h0 = (cell.rx3 - cell.rx0).hypot(cell.ry3 - cell.ry0);
705 let h1 = (cell.rx2 - cell.rx1).hypot(cell.ry2 - cell.ry1);
706 RunCell {
707 cell,
708 ux,
709 uy,
710 advance,
711 height: 0.5 * (h0 + h1),
712 space: cell.blank,
713 }
714 }
715
716 fn scale(&self) -> f64 {
719 if self.advance > 1e-6 {
720 self.advance
721 } else {
722 (0.5 * self.height).max(1e-6)
723 }
724 }
725}
726
727fn forward_gap(lhs: &RunCell, rhs: &RunCell) -> f64 {
736 let (l, r) = (lhs.cell, rhs.cell);
737 let (tx, ty) = (0.5 * (l.rx1 + l.rx2), 0.5 * (l.ry1 + l.ry2));
738 let (hx, hy) = (0.5 * (r.rx0 + r.rx3), 0.5 * (r.ry0 + r.ry3));
739 (hx - tx) * lhs.ux + (hy - ty) * lhs.uy
740}
741
742fn facing_edge_overlap(lhs: &RunCell, rhs: &RunCell) -> f64 {
745 let (l, r) = (lhs.cell, rhs.cell);
746 let (ux, uy) = if l.ltr {
747 (lhs.ux, lhs.uy)
748 } else {
749 (-lhs.ux, -lhs.uy)
750 };
751 let (nx, ny) = (-uy, ux);
752 let proj = |x: f64, y: f64| x * nx + y * ny;
753 let (a0, a1) = if l.ltr {
754 (proj(l.rx1, l.ry1), proj(l.rx2, l.ry2))
755 } else {
756 (proj(l.rx0, l.ry0), proj(l.rx3, l.ry3))
757 };
758 let (b0, b1) = if l.ltr {
759 (proj(r.rx0, r.ry0), proj(r.rx3, r.ry3))
760 } else {
761 (proj(r.rx1, r.ry1), proj(r.rx2, r.ry2))
762 };
763 a0.max(a1).min(b0.max(b1)) - a0.min(a1).max(b0.min(b1))
764}
765
766fn is_math_glyph(text: &str) -> bool {
770 const MATH: &str = "∑∏∫√∞∈∉≠≤≥⊂⊃⊆⊇⊥∪∩⎛⎜⎝⎞⎟⎠⎡⎢⎣⎤⎥⎦";
771 text.chars().any(|c| MATH.contains(c))
772}
773
774fn is_local_attachment(lhs: &RunCell, rhs: &RunCell) -> bool {
777 let scale = lhs.scale().max(rhs.scale());
778 if forward_gap(lhs, rhs) > 0.10 * scale {
779 return false;
780 }
781 let (off, (lh, rh)) = baseline_offset(lhs, rhs);
782 let math = |c: &RunCell| c.cell.math_font || is_math_glyph(&c.cell.text);
783 off <= 2.0 * lh.max(rh).max(1e-6) && (math(lhs) || math(rhs))
784}
785
786fn baseline_offset(lhs: &RunCell, rhs: &RunCell) -> (f64, (f64, f64)) {
794 let (nx, ny) = (-lhs.uy, lhs.ux);
795 match (lhs.cell.pen, rhs.cell.pen) {
796 (Some(a), Some(b)) => (
797 ((b[0] - a[0]) * nx + (b[1] - a[1]) * ny).abs(),
798 (a[2], b[2]),
799 ),
800 _ => (
801 ((rhs.cell.rx0 - lhs.cell.rx0) * nx + (rhs.cell.ry0 - lhs.cell.ry0) * ny).abs(),
802 (lhs.height, rhs.height),
803 ),
804 }
805}
806
807fn continues_line(lhs: &RunCell, rhs: &RunCell) -> bool {
811 if lhs.ux * rhs.ux + lhs.uy * rhs.uy < 0.985 {
812 return false;
813 }
814 let scale = lhs.scale().max(rhs.scale());
815 let gap = forward_gap(lhs, rhs);
816 if facing_edge_overlap(lhs, rhs) <= 1e-6 && !is_local_attachment(lhs, rhs) {
817 return false;
818 }
819 -scale <= gap && gap <= 4.0 * scale
820}
821
822fn baselines_compatible(lhs: &RunCell, rhs: &RunCell) -> bool {
826 let (off, (lh, rh)) = baseline_offset(lhs, rhs);
827 off <= 0.35 * lh.min(rh).max(1e-6) || is_local_attachment(lhs, rhs)
828}
829
830fn normalized_gap(lhs: &RunCell, rhs: &RunCell) -> f64 {
832 forward_gap(lhs, rhs).max(0.0) / (0.5 * (lhs.scale() + rhs.scale())).max(1e-6)
833}
834
835#[derive(Clone, Copy, PartialEq)]
837enum TrackClass {
838 Letter,
840 Dot,
842 Other,
843}
844
845fn track_class(text: &str) -> TrackClass {
846 if matches!(text, "." | "·" | "…") {
847 TrackClass::Dot
848 } else if !text.is_empty() && text.chars().all(char::is_alphanumeric) {
849 TrackClass::Letter
850 } else {
851 TrackClass::Other
852 }
853}
854
855fn tracking_tolerance(gap: f64) -> f64 {
859 (0.08 * gap).max(0.25)
860}
861
862fn tracked_gaps(run: &[RunCell]) -> Vec<bool> {
886 struct Gap {
887 left: usize,
888 right: usize,
889 pt: f64,
890 n: f64,
891 left_class: TrackClass,
892 right_class: TrackClass,
893 }
894 let mut gaps: Vec<Gap> = Vec::new();
895 let mut prev: Option<usize> = None;
896 let mut crossed_space = false;
897 for (i, c) in run.iter().enumerate() {
898 if c.space {
899 crossed_space = true;
900 continue;
901 }
902 if let (Some(p), false) = (prev, crossed_space) {
903 gaps.push(Gap {
904 left: p,
905 right: i,
906 pt: forward_gap(&run[p], c),
907 n: normalized_gap(&run[p], c),
908 left_class: track_class(&run[p].cell.text),
909 right_class: track_class(&c.cell.text),
910 });
911 }
912 prev = Some(i);
913 crossed_space = false;
914 }
915 let joined = |k: usize| gaps[k + 1].left == gaps[k].right;
917 let stretch = |k: usize, fits: &dyn Fn(&Gap) -> bool| {
919 let mut end = k;
920 while end < gaps.len() && fits(&gaps[end]) && (end == k || joined(end - 1)) {
921 end += 1;
922 }
923 end
924 };
925 let flanks_open = |k: usize, end: usize, floor: f64| {
927 let before = k > 0 && joined(k - 1) && gaps[k - 1].pt < floor;
928 let after = end < gaps.len() && joined(end - 1) && gaps[end].pt < floor;
929 !before && !after
930 };
931
932 let mut tracked = vec![false; run.len()];
933 let mut leader = vec![false; run.len()];
934 let mut letter_tracking: Vec<f64> = Vec::new();
935 let mut k = 0;
936 while k < gaps.len() {
937 let dots = |g: &Gap| {
938 g.left_class == TrackClass::Dot && g.right_class == TrackClass::Dot && g.n <= 1.0
939 };
940 let end = stretch(k, &dots);
941 if end - k >= 3 {
942 for g in &gaps[k..end] {
943 tracked[g.right] = true;
944 leader[g.left] = true;
945 leader[g.right] = true;
946 }
947 k = end;
948 continue;
949 }
950 let first = &gaps[k];
951 let letters = |g: &Gap| {
952 g.left_class == TrackClass::Letter
953 && g.right_class == TrackClass::Letter
954 && (0.1..=1.0).contains(&g.n)
955 };
956 if !letters(first) {
957 k += 1;
958 continue;
959 }
960 let (mut lo, mut hi) = (first.pt, first.pt);
961 let mut end = k + 1;
962 while end < gaps.len()
963 && joined(end - 1)
964 && letters(&gaps[end])
965 && hi.max(gaps[end].pt) - lo.min(gaps[end].pt) <= tracking_tolerance(lo)
966 {
967 lo = lo.min(gaps[end].pt);
968 hi = hi.max(gaps[end].pt);
969 end += 1;
970 }
971 let one_char = gaps[k..end]
974 .iter()
975 .all(|g| run[g.right].cell.text == run[first.left].cell.text);
976 if end - k >= 3 && !one_char && flanks_open(k, end, lo - tracking_tolerance(lo)) {
977 for g in &gaps[k..end] {
978 tracked[g.right] = true;
979 }
980 letter_tracking.push(0.5 * (lo + hi));
981 k = end;
982 } else {
983 k += 1;
984 }
985 }
986
987 for &t in &letter_tracking {
988 let tol = tracking_tolerance(t);
989 let same = |g: &Gap| {
990 (g.left_class == TrackClass::Letter || g.right_class == TrackClass::Letter)
991 && g.left_class != TrackClass::Dot
992 && g.right_class != TrackClass::Dot
993 && g.n <= 1.0
994 && (g.pt - t).abs() <= tol
995 };
996 let mut k = 0;
997 while k < gaps.len() {
998 let end = stretch(k, &same);
999 if end == k {
1000 k += 1;
1001 continue;
1002 }
1003 if flanks_open(k, end, t - tol) && !gaps[k..end].iter().any(|g| leader[g.left]) {
1004 for g in &gaps[k..end] {
1005 tracked[g.right] = true;
1006 }
1007 }
1008 k = end;
1009 }
1010 }
1011 tracked
1012}
1013
1014const GAP_CLUSTER_CLIP: f64 = 0.5;
1024
1025fn infer_word_gap(run: &[RunCell], tracked: &[bool]) -> f64 {
1033 let mut gaps = Vec::new();
1034 let mut prev: Option<&RunCell> = None;
1035 let mut crossed_space = false;
1036 for (i, c) in run.iter().enumerate() {
1037 if c.space {
1038 crossed_space = true;
1039 continue;
1040 }
1041 if let (Some(p), false, false) = (prev, crossed_space, tracked[i]) {
1042 gaps.push(normalized_gap(p, c).min(GAP_CLUSTER_CLIP));
1043 }
1044 prev = Some(c);
1045 crossed_space = false;
1046 }
1047 if gaps.len() < 2 {
1048 return 0.25;
1049 }
1050 let mut low = gaps.iter().copied().fold(f64::INFINITY, f64::min);
1051 let mut high = gaps.iter().copied().fold(f64::NEG_INFINITY, f64::max);
1052 if high < 0.18 || high - low < 0.08 {
1053 return 0.25;
1054 }
1055 for _ in 0..8 {
1056 let mid = 0.5 * (low + high);
1057 let (mut ls, mut ln, mut hs, mut hn) = (0.0, 0usize, 0.0, 0usize);
1058 for &g in &gaps {
1059 if g <= mid {
1060 ls += g;
1061 ln += 1;
1062 } else {
1063 hs += g;
1064 hn += 1;
1065 }
1066 }
1067 if ln == 0 || hn == 0 {
1068 return 0.25;
1069 }
1070 low = ls / ln as f64;
1071 high = hs / hn as f64;
1072 }
1073 if high < 0.18 || high - low < 0.08 {
1074 return 0.25;
1075 }
1076 (0.5 * (low + high)).max(0.18)
1077}
1078
1079struct Agg {
1081 text: String,
1082 ltr: bool,
1083 q: [f64; 8],
1084}
1085
1086impl Agg {
1087 fn of(c: &Cell) -> Self {
1088 Agg {
1089 text: c.text.clone(),
1090 ltr: c.ltr,
1091 q: [c.rx0, c.ry0, c.rx1, c.ry1, c.rx2, c.ry2, c.rx3, c.ry3],
1092 }
1093 }
1094
1095 fn append(&mut self, text: &str, ltr: bool, q: &[f64; 8], space: bool) {
1099 if self.ltr && ltr {
1100 if space {
1101 self.text.push(' ');
1102 }
1103 self.text.push_str(text);
1104 } else {
1105 let sep = if space { " " } else { "" };
1106 self.text = format!("{text}{sep}{}", self.text);
1107 self.ltr = false;
1108 }
1109 let (mut ux, mut uy) = (self.q[2] - self.q[0], self.q[3] - self.q[1]);
1110 if ux.hypot(uy) <= 1e-9 {
1111 (ux, uy) = (q[2] - q[0], q[3] - q[1]);
1112 }
1113 let n = ux.hypot(uy);
1114 if n <= 1e-9 {
1115 return;
1116 }
1117 let (ux, uy) = (ux / n, uy / n);
1118 let (nx, ny) = (-uy, ux);
1119 let (mut u0, mut u1, mut n0, mut n1) = (
1120 f64::INFINITY,
1121 f64::NEG_INFINITY,
1122 f64::INFINITY,
1123 f64::NEG_INFINITY,
1124 );
1125 for k in 0..4 {
1126 for (x, y) in [(self.q[2 * k], self.q[2 * k + 1]), (q[2 * k], q[2 * k + 1])] {
1127 let (u, v) = (x * ux + y * uy, x * nx + y * ny);
1128 u0 = u0.min(u);
1129 u1 = u1.max(u);
1130 n0 = n0.min(v);
1131 n1 = n1.max(v);
1132 }
1133 }
1134 let pt = |u: f64, v: f64| (u * ux + v * nx, u * uy + v * ny);
1135 let (a, b, c, d) = (pt(u0, n0), pt(u1, n0), pt(u1, n1), pt(u0, n1));
1136 self.q = [a.0, a.1, b.0, b.1, c.0, c.1, d.0, d.1];
1137 }
1138
1139 fn into_cell(self, page_h: f32) -> TextCell {
1140 let xs = [self.q[0], self.q[2], self.q[4], self.q[6]];
1141 let ys = [self.q[1], self.q[3], self.q[5], self.q[7]];
1142 let (l, r) = (
1143 xs.iter().copied().fold(f64::INFINITY, f64::min),
1144 xs.iter().copied().fold(f64::NEG_INFINITY, f64::max),
1145 );
1146 let (b, t) = (
1147 ys.iter().copied().fold(f64::INFINITY, f64::min),
1148 ys.iter().copied().fold(f64::NEG_INFINITY, f64::max),
1149 );
1150 TextCell {
1151 text: self.text,
1152 l: l as f32,
1153 t: page_h - t as f32,
1154 r: r as f32,
1155 b: page_h - b as f32,
1156 }
1157 }
1158}
1159
1160fn page_draws_spaces(runs: &[Vec<RunCell>]) -> bool {
1167 let long = |r: &&Vec<RunCell>| r.iter().filter(|c| !c.space).count() >= 8;
1168 let lines = runs.iter().filter(long).count();
1169 let spaced = runs
1170 .iter()
1171 .filter(long)
1172 .filter(|r| r.iter().any(|c| c.space))
1173 .count();
1174 lines > 0 && 2 * spaced >= lines
1175}
1176
1177fn contract_runs(cells: &[Cell], gap: WordGap, page_h: f32) -> (Vec<TextCell>, Vec<TextCell>) {
1181 let mut runs: Vec<Vec<RunCell>> = Vec::new();
1184 for c in cells.iter().filter(|c| !c.text.is_empty()) {
1185 let rc = RunCell::new(c);
1186 if rc.space {
1187 match runs.last_mut() {
1188 Some(run) => run.push(rc),
1189 None => runs.push(vec![rc]),
1190 }
1191 continue;
1192 }
1193 let starts = match runs.last().and_then(|r| r.iter().rev().find(|c| !c.space)) {
1194 Some(prev) => !continues_line(prev, &rc),
1195 None => runs.is_empty(),
1196 };
1197 if starts {
1198 runs.push(Vec::new());
1199 }
1200 runs.last_mut().expect("a run is open").push(rc);
1201 }
1202
1203 let (mut words_out, mut lines_out) = (Vec::new(), Vec::new());
1204 let page_spaced = page_draws_spaces(&runs);
1205 for run in &runs {
1206 let has_spaces = page_spaced || run.iter().any(|c| c.space);
1213 let tracked = tracked_gaps(run);
1214 let threshold = match gap {
1221 WordGap::Factor(f) if has_spaces => f.max(0.35),
1222 WordGap::Factor(f) => f,
1223 _ => (if has_spaces {
1224 1.0
1225 } else {
1226 infer_word_gap(run, &tracked)
1227 })
1228 .min(0.35),
1229 };
1230 let mut words: Vec<Agg> = Vec::new();
1231 let mut prev: Option<&RunCell> = None;
1232 let mut boundary = false;
1233 for (i, c) in run.iter().enumerate() {
1234 if c.space {
1235 boundary = true;
1236 continue;
1237 }
1238 let mut starts = words.is_empty() || boundary;
1239 if !starts
1242 && c.cell.text == "."
1243 && i + 4 < run.len()
1244 && run[i + 1].space
1245 && run[i + 2].cell.text == "."
1246 && run[i + 3].space
1247 && run[i + 4].cell.text == "."
1248 && prev.is_some_and(|p| p.cell.text != ".")
1249 {
1250 starts = true;
1251 }
1252 if let (false, Some(p)) = (starts, prev) {
1253 starts = !baselines_compatible(p, c)
1254 || (!tracked[i] && normalized_gap(p, c) > threshold);
1255 }
1256 if starts {
1257 words.push(Agg::of(c.cell));
1258 } else {
1259 let q = [
1260 c.cell.rx0, c.cell.ry0, c.cell.rx1, c.cell.ry1, c.cell.rx2, c.cell.ry2,
1261 c.cell.rx3, c.cell.ry3,
1262 ];
1263 words.last_mut().expect("a word is open").append(
1264 &c.cell.text,
1265 c.cell.ltr,
1266 &q,
1267 false,
1268 );
1269 }
1270 prev = Some(c);
1271 boundary = false;
1272 }
1273 let mut it = words.into_iter();
1274 let Some(first) = it.next() else {
1275 continue;
1276 };
1277 let mut line = Agg {
1278 text: first.text.clone(),
1279 ltr: first.ltr,
1280 q: first.q,
1281 };
1282 words_out.push(first.into_cell(page_h));
1283 for w in it {
1284 line.append(&w.text, w.ltr, &w.q, true);
1285 words_out.push(w.into_cell(page_h));
1286 }
1287 lines_out.push(line.into_cell(page_h));
1288 }
1289 (lines_out, words_out)
1290}
1291
1292#[cfg(test)]
1293mod tests {
1294 use super::*;
1295
1296 fn glyph(ch: char, l: f32, r: f32) -> Glyph {
1297 Glyph {
1298 ch,
1299 l,
1300 b: 100.0,
1301 r,
1302 t: 110.0,
1303 ll: l,
1304 lb: 98.0,
1305 lr: r,
1306 lt: 110.0,
1307 font: 0,
1308 quad: None,
1309 pen: None,
1310 math_font: false,
1311 }
1312 }
1313
1314 #[test]
1319 fn space_glyph_separates_tight_words() {
1320 let glyphs = [
1321 glyph('M', 137.66, 143.74),
1322 glyph('O', 143.74, 149.82),
1323 glyph('D', 149.82, 155.90),
1324 glyph('E', 155.90, 161.98),
1325 glyph(' ', 161.84, 164.09),
1326 glyph('t', 163.82, 166.60),
1327 glyph('o', 166.60, 171.60),
1328 ];
1329 let (lines, words) = line_and_word_cells(&glyphs, 792.0, true);
1330 let words: Vec<&str> = words.iter().map(|w| w.text.as_str()).collect();
1331 assert_eq!(words, ["MODE", "to"]);
1332 assert_eq!(lines.len(), 1);
1333 assert_eq!(lines[0].text, "MODE to");
1334 }
1335
1336 #[test]
1341 fn stacked_glyphs_at_one_x_stay_separate_cells() {
1342 let at = |ch: char, b: f32| Glyph {
1343 lb: b,
1344 lt: b + 10.0,
1345 b,
1346 t: b + 10.0,
1347 ..glyph(ch, 98.0, 103.0)
1348 };
1349 let ticks: Vec<Glyph> = "345678"
1350 .chars()
1351 .enumerate()
1352 .map(|(k, ch)| at(ch, 316.0 + 25.92 * k as f32))
1353 .collect();
1354 let lines = line_cells(&ticks, 792.0, true);
1355 let texts: Vec<&str> = lines.iter().map(|c| c.text.as_str()).collect();
1356 assert_eq!(texts, ["3", "4", "5", "6", "7", "8"]);
1357 assert!(lines.windows(2).all(|w| w[0].t > w[1].t));
1358
1359 let lig = [glyph('f', 10.0, 16.0), glyph('i', 10.0, 16.0)];
1360 let lines = line_cells(&lig, 792.0, true);
1361 assert_eq!(lines.len(), 1);
1362 assert_eq!(lines[0].text, "fi");
1363 }
1364
1365 #[test]
1370 fn cells_do_not_keep_the_glyph_buffer() {
1371 let glyphs: Vec<Glyph> = (0..400)
1373 .map(|i| {
1374 let x = (i % 40) as f32 * 6.0;
1375 let ch = if i % 4 == 3 { ' ' } else { '7' };
1376 let mut g = glyph(ch, x, x + 6.0);
1377 let row = (i / 40) as f32 * 14.0;
1378 (g.b, g.t, g.lb, g.lt) = (100.0 + row, 110.0 + row, 98.0 + row, 110.0 + row);
1379 g
1380 })
1381 .collect();
1382 let (lines, words) = line_and_word_cells(&glyphs, 792.0, true);
1383 assert_eq!((lines.len(), words.len()), (10, 100));
1384 assert_eq!(lines.capacity(), lines.len());
1385 assert_eq!(words.capacity(), words.len());
1386 }
1387
1388 fn und_dem_line() -> Vec<Glyph> {
1392 let at = |ch: char, l: f32, r: f32, font: u64| Glyph {
1393 ch,
1394 l,
1395 b: 682.056,
1396 r,
1397 t: 691.05,
1398 ll: l,
1399 lb: 682.056,
1400 lr: r,
1401 lt: 691.05,
1402 font,
1403 quad: None,
1404 pen: None,
1405 math_font: false,
1406 };
1407 let mut g = Vec::new();
1408 let mut run = |text: &str, edges: &[f32], font: u64| {
1409 for (i, ch) in text.chars().enumerate() {
1410 g.push(at(ch, edges[i], edges[i + 1], font));
1411 }
1412 };
1413 run(
1414 "schaft",
1415 &[
1416 328.976, 333.216, 338.572, 344.325, 350.486, 353.920, 357.248,
1417 ],
1418 1,
1419 );
1420 run("und", &[359.261, 365.121, 370.875, 377.035], 2);
1421 run("dem", &[378.990, 385.151, 390.701, 399.287], 2);
1422 run("Öst", &[401.214, 409.092, 413.332, 416.660], 1);
1423 g
1424 }
1425
1426 #[test]
1427 fn legacy_rule_keeps_the_borderline_und_dem_glued() {
1428 let (lines, words) = line_and_word_cells(&und_dem_line(), 792.0, true);
1432 assert!(lines.iter().any(|c| c.text == "unddem"), "{lines:?}");
1433 assert!(words.iter().any(|c| c.text == "unddem"), "{words:?}");
1434 }
1435
1436 #[test]
1437 fn line_local_word_gaps_split_und_dem() {
1438 for gap in [WordGap::Adaptive, WordGap::Factor(0.2)] {
1439 let (lines, words) = line_and_word_cells_with(&und_dem_line(), 792.0, true, gap);
1440 let texts: Vec<&str> = lines.iter().map(|c| c.text.as_str()).collect();
1441 assert_eq!(texts, ["schaft und dem Öst"], "{gap:?}");
1442 let texts: Vec<&str> = words.iter().map(|c| c.text.as_str()).collect();
1443 assert_eq!(texts, ["schaft", "und", "dem", "Öst"], "{gap:?}");
1444 assert!((words[1].l - 359.261).abs() < 1e-3 && (words[1].r - 377.035).abs() < 1e-3);
1446 }
1447 let (lines, _) =
1449 line_and_word_cells_with(&und_dem_line(), 792.0, true, WordGap::Factor(0.5));
1450 assert_eq!(lines[0].text, "schaftunddemÖst");
1451 }
1452
1453 #[test]
1454 fn a_superscript_attaches_by_pen_origin_and_math_face() {
1455 let at = |ch: char, l: f32, y: f32, size: f32, math: bool, pen: bool| Glyph {
1459 lb: y - 0.2 * size,
1460 lt: y + 0.8 * size,
1461 b: y - 0.2 * size,
1462 t: y + 0.8 * size,
1463 pen: pen.then_some([l, y, size]),
1464 math_font: math,
1465 ..glyph(ch, l, l + 0.5 * size)
1466 };
1467 let line = |rise: f32, math: bool, pen: bool| {
1468 let mut g: Vec<Glyph> = "Lysak"
1469 .chars()
1470 .enumerate()
1471 .map(|(k, ch)| at(ch, 100.0 + 5.0 * k as f32, 600.0, 10.0, math, pen))
1472 .collect();
1473 g.extend(
1474 "[0000"
1475 .chars()
1476 .enumerate()
1477 .map(|(k, ch)| at(ch, 125.0 + 3.5 * k as f32, 600.0 + rise, 7.0, math, pen)),
1478 );
1479 let (lines, _) = line_and_word_cells_with(&g, 792.0, true, WordGap::Adaptive);
1480 lines[0].text.clone()
1481 };
1482 assert_eq!(line(3.6, true, true), "Lysak[0000");
1485 assert_eq!(line(3.6, false, true), "Lysak [0000");
1486 assert_eq!(line(2.0, false, true), "Lysak[0000");
1489 assert_eq!(line(2.0, false, false), "Lysak [0000");
1490 }
1491
1492 #[test]
1493 fn a_factor_keeps_docling_parse_s_floor_on_lines_with_space_glyphs() {
1494 let mut g = Vec::new();
1499 let mut x = 100.0f32;
1500 let put = |g: &mut Vec<Glyph>, s: &str, x: &mut f32| {
1501 for ch in s.chars() {
1502 g.push(glyph(ch, *x, *x + 2.5));
1503 *x += 2.5;
1504 }
1505 };
1506 put(&mut g, "Die", &mut x);
1507 g.push(glyph(' ', x, x + 2.5));
1508 x += 2.5;
1509 put(&mut g, "Verpf", &mut x);
1510 x += 0.6;
1511 put(&mut g, "lichtungen", &mut x);
1512 let (lines, _) = line_and_word_cells_with(&g, 792.0, true, WordGap::Factor(0.2));
1513 assert_eq!(lines[0].text, "Die Verpflichtungen");
1514 let spaceless: Vec<Glyph> = g.into_iter().filter(|c| c.ch != ' ').collect();
1515 let (lines, _) = line_and_word_cells_with(&spaceless, 792.0, true, WordGap::Factor(0.2));
1516 assert_eq!(lines[0].text, "Die Verpf lichtungen");
1517 }
1518
1519 #[test]
1520 fn right_to_left_glyphs_in_visual_order_stay_one_line() {
1521 let mut g = Vec::new();
1526 let mut x = 100.0f32;
1527 for (k, word) in [
1528 "\u{0631}\u{0627}\u{0632}\u{0627}\u{0628}",
1529 "\u{0631}\u{062f}",
1530 ]
1531 .iter()
1532 .enumerate()
1533 {
1534 if k > 0 {
1535 x += 3.0;
1536 }
1537 for ch in word.chars() {
1538 g.push(glyph(ch, x, x + 5.0));
1539 x += 5.0;
1540 }
1541 }
1542 for gap in [WordGap::Adaptive, WordGap::Factor(0.2)] {
1543 let (lines, words) = line_and_word_cells_with(&g, 792.0, true, gap);
1544 assert_eq!(
1545 lines.len(),
1546 1,
1547 "{gap:?}: {:?}",
1548 lines.iter().map(|c| &c.text).collect::<Vec<_>>()
1549 );
1550 assert_eq!(words.len(), 2, "{gap:?}");
1551 }
1552 }
1553
1554 #[test]
1555 fn letter_spacing_stays_one_word_under_the_adaptive_rule() {
1556 let mut g = Vec::new();
1559 let mut x = 100.0f32;
1560 for (k, word) in ["Allgemeine", "Bestimmungen"].iter().enumerate() {
1561 if k > 0 {
1562 x += 6.0;
1563 }
1564 for ch in word.chars() {
1565 g.push(glyph(ch, x, x + 5.0));
1566 x += 5.0 + 1.5;
1567 }
1568 x -= 1.5;
1569 }
1570 let (lines, _) = line_and_word_cells_with(&g, 792.0, true, WordGap::Adaptive);
1571 assert_eq!(lines[0].text, "Allgemeine Bestimmungen");
1572 }
1573
1574 fn set_line(pieces: &[(&str, f32)], y: f32) -> Vec<Glyph> {
1577 let mut g = Vec::new();
1578 let mut x = 100.0f32;
1579 for (text, gap) in pieces {
1580 x += gap;
1581 for ch in text.chars() {
1582 g.push(Glyph {
1583 b: y,
1584 t: y + 10.0,
1585 lb: y - 2.0,
1586 lt: y + 10.0,
1587 ..glyph(ch, x, x + 2.5)
1588 });
1589 x += 2.5;
1590 }
1591 }
1592 g
1593 }
1594
1595 #[test]
1596 fn one_wide_gap_does_not_raise_the_line_s_word_gap() {
1597 let g = set_line(
1602 &[
1603 ("jedoch", 0.0),
1604 ("werden", 0.8),
1605 ("weiterlaufende", 0.8),
1606 ("Quartierkosten", 8.0),
1607 ],
1608 600.0,
1609 );
1610 let (_, words) = line_and_word_cells_with(&g, 792.0, true, WordGap::Adaptive);
1611 let texts: Vec<&str> = words.iter().map(|c| c.text.as_str()).collect();
1612 assert_eq!(
1613 texts,
1614 ["jedoch", "werden", "weiterlaufende", "Quartierkosten"]
1615 );
1616 }
1617
1618 #[test]
1619 fn a_lone_word_on_a_page_that_draws_spaces_keeps_its_kerning() {
1620 let lone = || set_line(&[("Offenlegungspf", 0.0), ("lichten", 0.8)], 500.0);
1625 let spaced = |y| {
1626 set_line(
1627 &[
1628 ("Die", 0.0),
1629 (" ", 0.0),
1630 ("Taxonomie", 0.0),
1631 (" ", 0.0),
1632 ("gilt", 0.0),
1633 ],
1634 y,
1635 )
1636 };
1637 let mut page = lone();
1638 page.extend(spaced(600.0));
1639 page.extend(spaced(620.0));
1640 let texts = |g: &[Glyph]| {
1641 let (lines, _) = line_and_word_cells_with(g, 792.0, true, WordGap::Adaptive);
1642 lines.into_iter().map(|c| c.text).collect::<Vec<_>>()
1643 };
1644 assert!(texts(&page).contains(&"Offenlegungspflichten".to_string()));
1645 assert_eq!(texts(&lone()), ["Offenlegungspf lichten"]);
1646 }
1647
1648 #[test]
1649 fn word_gap_spellings() {
1650 assert_eq!(WordGap::parse("adaptive"), Ok(WordGap::Adaptive));
1651 assert_eq!(WordGap::parse(" Legacy "), Ok(WordGap::Legacy));
1652 assert_eq!(WordGap::parse("0.2"), Ok(WordGap::Factor(0.2)));
1653 for bad in ["0", "-1", "5", "nan", "wide"] {
1654 assert!(WordGap::parse(bad).is_err(), "{bad}");
1655 }
1656 }
1657}