Bound text-object joins within the import comparison budget

This commit is contained in:
Codex
2026-11-30 13:25:54 +01:00
parent bc9e7c9124
commit aeae489395
+22 -12
View File
@@ -350,35 +350,45 @@ fn see_page_type(content: &mut [Content], shown: &[Vec<Arc<Node>>]) -> Option<us
let mut objects: Vec<(Vec<Rect>, Vec<String>, bool)> =
shown.iter().map(|o| (texts(o).iter().filter_map(|n| n.visual_bounds()).collect(), object_words(o), false)).collect();
let pieces: usize = objects.iter().map(|o| o.0.len()).sum();
if content.len().saturating_mul(pieces) > MAX_PAIRS {
return None;
}
let mut budget = MAX_PAIRS.checked_sub(content.len().checked_mul(pieces)?)?;
let holds = |drawn: Rect, r: &Rect| drawn.inflate(OUTLINE_REACH, OUTLINE_REACH).intersect(*r).area() >= 0.5 * r.area().max(1e-9);
// The made type holding each object, in order: where it draws and its words.
let mut holders: Vec<Vec<Holder>> = vec![vec![]; objects.len()];
for c in content {
let Content::Made(node, region, seen) = c else { continue };
let mut holders: Vec<Vec<Arc<Holder>>> = vec![vec![]; objects.len()];
let mut seen_slots = Vec::new();
for (slot, c) in content.iter().enumerate() {
let Content::Made(node, region, _) = c else { continue };
// Hidden made type has no region: where it draws, shown, is its own box.
let Some(drawn) = region.or_else(|| node.visual_bounds()) else { continue };
let (spelled, holder) = match &node.kind {
NodeKind::Text(t) => {
let text = t.plain_text();
let [a, b, ..] = t.xf.as_coeffs();
(spelling(&text), Some((drawn.center(), (a, b), word_pieces(&text).map(spelling).filter(|w| !w.is_empty()).collect())))
(spelling(&text), Some(Arc::new((drawn.center(), (a, b), word_pieces(&text).map(spelling).filter(|w| !w.is_empty()).collect()))))
}
_ => (String::new(), None),
};
for ((rects, words, held), by) in objects.iter_mut().zip(&mut holders) {
if rects.iter().any(|r| holds(drawn, r)) {
*held = true;
*seen |= region.is_some();
if region.is_some() && seen_slots.last() != Some(&slot) {
seen_slots.push(slot);
}
words.retain(|w| !spelled.contains(w.as_str()));
by.extend(holder.clone());
}
}
}
for ((_, words, _), by) in objects.iter_mut().zip(&holders) {
words.retain(|w| !joined_by_two(w, by));
if !words.is_empty() && !by.is_empty() {
// Each unmatched word can visit every ordered pair of holders, plus the first-holder scan.
budget = budget.checked_sub(words.len().checked_mul(by.len())?.checked_mul(by.len().checked_add(1)?)?)?;
words.retain(|w| !joined_by_two(w, by));
}
}
for slot in seen_slots {
if let Content::Made(_, _, seen) = &mut content[slot] {
*seen = true;
}
}
Some(objects.iter().filter(|(_, words, held)| !held || !words.is_empty()).count())
}
@@ -390,11 +400,11 @@ type Holder = (Point, (f64, f64), Vec<String>);
/// Is `word` (a [`spelling`]) the last word of one of `by` joined to the first word of another of
/// them that draws after it, along the first one's lines? Each part is a whole word of its text
/// object, so a piece is never made up of bits of words.
fn joined_by_two(word: &str, by: &[Holder]) -> bool {
by.iter().any(|(at, (a, b), first)| {
fn joined_by_two(word: &str, by: &[Arc<Holder>]) -> bool {
by.iter().map(Arc::as_ref).any(|(at, (a, b), first)| {
let Some(head) = first.last().filter(|h| word.len() > h.len() && word.starts_with(h.as_str())) else { return false };
let tail = &word[head.len()..];
by.iter().any(|(next, _, second)| (next.x - at.x) * a + (next.y - at.y) * b > 0.0 && second.first().is_some_and(|w| w == tail))
by.iter().map(Arc::as_ref).any(|(next, _, second)| (next.x - at.x) * a + (next.y - at.y) * b > 0.0 && second.first().is_some_and(|w| w == tail))
})
}