Skip to content

Commit e3cf5b4

Browse files
committed
A line that closes a sentence before its formula is prose, so the formula hole after it is bridged
1 parent 1b7ec55 commit e3cf5b4

1 file changed

Lines changed: 58 additions & 8 deletions

File tree

‎crates/rpt-core/src/segment.rs‎

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6591,11 +6591,55 @@ pub(crate) fn display_equation_glyphs(glyphs: &[&Glyph]) -> bool {
65916591
if variance_clause(glyphs) {
65926592
return false;
65936593
}
6594+
// `another. Let C = (X, Y` ends a sentence before the formula starts.
6595+
// A displayed equation has no prose word closed by a period.
6596+
if closes_a_sentence(glyphs) {
6597+
return false;
6598+
}
65946599
// `Attention(Q, K, V) = softmax(...)` has no function word. `where head_i
65956600
// = Attention(Q W^Q_i, ...)` is still that equation: one lead-in word.
65966601
function_words == 0 || (function_words <= 1 && content_words <= 3)
65976602
}
65986603

6604+
/// A lowercase word of four letters or more, set in the text face, then a
6605+
/// period and a capitalized word: `another. Let`.
6606+
fn closes_a_sentence(glyphs: &[&Glyph]) -> bool {
6607+
let mut ordered = glyphs.to_vec();
6608+
ordered.sort_by(|left, right| left.matrix[4].total_cmp(&right.matrix[4]));
6609+
let mut run = 0usize;
6610+
let mut after_period = false;
6611+
for glyph in &ordered {
6612+
let text = glyph.unicode.trim();
6613+
if after_period {
6614+
if text.is_empty() {
6615+
continue;
6616+
}
6617+
if !math_font_glyph(glyph)
6618+
&& text
6619+
.chars()
6620+
.next()
6621+
.is_some_and(|ch| ch.is_ascii_uppercase())
6622+
{
6623+
return true;
6624+
}
6625+
after_period = false;
6626+
}
6627+
if math_font_glyph(glyph) {
6628+
run = 0;
6629+
continue;
6630+
}
6631+
if text == "." {
6632+
after_period = run >= 4;
6633+
run = 0;
6634+
} else if !text.is_empty() && text.chars().all(|ch| ch.is_ascii_lowercase()) {
6635+
run += text.chars().count();
6636+
} else {
6637+
run = 0;
6638+
}
6639+
}
6640+
false
6641+
}
6642+
65996643
/// `has mean 0 and variance d_k`. The words are the sentence; the product
66006644
/// is the formula in front of them.
66016645
fn variance_clause(glyphs: &[&Glyph]) -> bool {
@@ -6957,14 +7001,20 @@ fn next_formula_shard(lines: &[VisualLine<'_>], index: usize) -> Option<usize> {
69577001
if !left.glyphs.iter().any(|glyph| body_glyph(glyph)) {
69587002
return None;
69597003
}
6960-
let next = (index + 1..lines.len().min(index + 8)).find(|&other| {
6961-
let right = &lines[other];
6962-
right.page == left.page
6963-
&& !right.glyphs.is_empty()
6964-
&& right.glyphs.iter().any(|glyph| body_glyph(glyph))
6965-
&& (right.y - left.y).abs() <= size * 0.45
6966-
&& right.left >= left.right - 1.0
6967-
})?;
7004+
// The shard is the nearest piece to the right on the baseline. Lines
7005+
// are not kept in reading order, so `) denote …` can sit many entries
7006+
// away from `Let C = (X, Y`.
7007+
let next = (0..lines.len())
7008+
.filter(|&other| {
7009+
let right = &lines[other];
7010+
other != index
7011+
&& right.page == left.page
7012+
&& !right.glyphs.is_empty()
7013+
&& right.glyphs.iter().any(|glyph| body_glyph(glyph))
7014+
&& (right.y - left.y).abs() <= size * 0.45
7015+
&& right.left >= left.right - 1.0
7016+
})
7017+
.min_by(|a, b| lines[*a].left.total_cmp(&lines[*b].left).then(a.cmp(b)))?;
69687018
let right = &lines[next];
69697019
// Equation (1) is already a display equation. Bridging its fraction
69707020
// hole pulls `)V` and the transpose into the segment, and the formula

0 commit comments

Comments
 (0)