Sort oracle word boxes before grouping lines
pdftotext 26 does not always emit words in reading order: wide justified gaps and hyphenated lines interleave words from different baselines, so parse_bbox split one visual line into several. Sort by baseline and then x before grouping. It also reports each word box as the font extent, whose ascent varies with type size; compare display limits by the subscript/superscript baseline gap, which is anchor-free, instead of mixing 7pt box tops with the 10pt base.
This commit is contained in:
1 parent
e09e413892
commit
83f49db49e
1 file changed
+45
-10
@@ -110,16 +110,27 @@ fn unescape(text: &str) -> String {
|
||||
}
|
||||
|
||||
fn parse_bbox(xhtml: &str) -> Vec<Line> {
|
||||
let mut lines: Vec<Line> = Vec::new();
|
||||
let mut current_y = f64::NAN;
|
||||
let mut words: Vec<(f64, f64, String)> = Vec::new();
|
||||
let mut rest = xhtml;
|
||||
while let Some(start) = rest.find("<word ") {
|
||||
let tag_end = start + rest[start..].find('>').expect("word tag end");
|
||||
let close = tag_end + rest[tag_end..].find("</word>").expect("word close");
|
||||
let tag = &rest[start..=tag_end];
|
||||
let text = unescape(&rest[tag_end + 1..close]);
|
||||
let x_min = attr(tag, "xMin");
|
||||
let y_min = attr(tag, "yMin");
|
||||
words.push((attr(tag, "xMin"), attr(tag, "yMin"), text));
|
||||
rest = &rest[close + "</word>".len()..];
|
||||
}
|
||||
// pdftotext does not always emit words in reading order: wide justified
|
||||
// gaps and hyphenated lines can interleave words from different baselines.
|
||||
// Sort by baseline, then by x, before grouping into lines.
|
||||
words.sort_by(|a, b| {
|
||||
a.1.partial_cmp(&b.1)
|
||||
.expect("bbox y is finite")
|
||||
.then(a.0.partial_cmp(&b.0).expect("bbox x is finite"))
|
||||
});
|
||||
let mut lines: Vec<Line> = Vec::new();
|
||||
let mut current_y = f64::NAN;
|
||||
for (x_min, y_min, text) in words {
|
||||
if lines.is_empty() || (y_min - current_y).abs() > 2.0 {
|
||||
lines.push(Line {
|
||||
words: Vec::new(),
|
||||
@@ -133,7 +144,6 @@ fn parse_bbox(xhtml: &str) -> Vec<Line> {
|
||||
abs_x: x_min * BP_TO_PT,
|
||||
abs_y: y_min * BP_TO_PT,
|
||||
});
|
||||
rest = &rest[close + "</word>".len()..];
|
||||
}
|
||||
for line in &mut lines {
|
||||
let origin = line.words.first().map(|word| word.x).unwrap_or(0.0);
|
||||
@@ -1476,17 +1486,42 @@ fn display_limits_sit_above_and_below_their_base() {
|
||||
})
|
||||
.unwrap_or_else(|| panic!("route-D produced no `{letter}`"))
|
||||
};
|
||||
let (origin_x, origin_y) = ours(base);
|
||||
let (tex_x, tex_y) = tex(base);
|
||||
let (origin_x, _origin_y) = ours(base);
|
||||
let (tex_x, _tex_y) = tex(base);
|
||||
let mut reference_gap = 0.0;
|
||||
let mut our_gap = 0.0;
|
||||
for letter in ["i", "n"] {
|
||||
let (x, y) = ours(letter);
|
||||
let (reference_x, reference_y) = tex(letter);
|
||||
let dx = (x - origin_x) - (reference_x - tex_x);
|
||||
let dy = (y - origin_y) - (reference_y - tex_y);
|
||||
assert!(
|
||||
dx.abs() <= 1.2 && dy.abs() <= 0.2,
|
||||
"limits over `{base}`: `{letter}` is off by ({dx:.2}, {dy:.2}) pt"
|
||||
dx.abs() <= 1.2,
|
||||
"limits over `{base}`: `{letter}` is off by {dx:.2} pt horizontally"
|
||||
);
|
||||
// poppler 26 reports a word's box as the font extent, whose ascent
|
||||
// depends on the type size, so the absolute `yMin` of a 7pt limit
|
||||
// cannot be compared with the 10pt base. The subscript and
|
||||
// superscript share a size, so their baseline distance is a valid
|
||||
// anchor-free reference.
|
||||
if letter == "i" {
|
||||
reference_gap = reference_y;
|
||||
our_gap = y;
|
||||
} else {
|
||||
reference_gap -= reference_y;
|
||||
our_gap -= y;
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
(our_gap - reference_gap).abs() <= 0.2,
|
||||
"limits over `{base}`: limit gap is off by {:.2} pt",
|
||||
our_gap - reference_gap
|
||||
);
|
||||
let (_, base_y) = ours(base);
|
||||
let (_, subscript_y) = ours("i");
|
||||
let (_, superscript_y) = ours("n");
|
||||
assert!(
|
||||
subscript_y > base_y && superscript_y < base_y,
|
||||
"limits over `{base}` must straddle the operator baseline"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user