fix(forms): name detected check boxes from the nearer neighbouring label (#2943) (#2953)

* fix(forms): name detected check boxes from the nearer neighbouring label (#2943)

* M6: bound rectangle work during form detection (#2953)

Limit drawn and existing rectangle candidates before pairwise comb and checkbox work, preserve every widget overlap exclusion, and avoid cubic default-name lookup. Document partial proposals on unusually complex pages.

Verified: 143 forms unit tests, strict forms clippy, wasm check, layers, assets, and format. The count-based regression fails when the pre-comb cap is removed.

* Bound existing-widget overlap work during field detection

Index every existing widget spatially and cap aggregate query work, rejecting unchecked proposals if adversarial geometry exhausts the budget. Retain exclusions beyond the proposal/label budget.

Add synthetic 100k-widget, inverted-geometry and linear-equivalence regressions. The original linear scan fails the new work-bound negative control. All 147 forms tests, strict Clippy, formatting, wasm, layers and assets checks pass.

---------

Co-authored-by: skylar taylor-barrick <hi@skylar.cc>
This commit is contained in:
jonathanwxh-cellandskylar taylor-barrick authored and GitHub committed 2026-11-18 00:15:31 -06:00
1 parent 2251a75cc8
commit 7895411b03
4 files changed
+519 -62

No files matched your search

+27 -9
View File
@@ -7442,6 +7442,17 @@ fn detecting_fields_on_a_printed_form() {
let f = &d.form[0];
assert!(f.widgets[0].rect[0] > 72.0 + 20.0, "{:?}", f.widgets[0].rect);
assert!(s.detect_fields(id, &[]).is_empty(), "nothing left to detect");
// #2943: a "label, box, label, box" line names each check box after the label before it.
let labels_pdf = paper_form_with(
595,
842,
"BT /F1 10 Tf 100 534 Td (Monsieur) Tj ET 146 532 11 10 re S BT /F1 10 Tf 180 534 Td (Madame) Tj ET 222 532 11 10 re S",
);
let labels_id = s.open("labels.pdf", None, Arc::new(labels_pdf), None).unwrap();
let found = s.detect_fields(labels_id, &[]);
let names: Vec<&str> = found.iter().map(|(_, c)| c.name.as_str()).collect();
assert_eq!(names, ["Monsieur", "Madame"], "{found:?}");
}
/// #294, #2831: a detected field sits on a line or box the page already draws, so it gets no border
@@ -7466,18 +7477,11 @@ fn detected_fields_have_no_border_or_fill() {
assert!(look.border.is_some() && look.fill.is_some(), "{look:?}");
}
/// A printed form, 420 × 300 pt: a title, a label with a rule to write on, a box with a small
/// label in its corner, an empty box with its label above, and two check boxes.
fn paper_form() -> Vec<u8> {
let content = "BT /F1 14 Tf 30 270 Td (Gewerbe-Ummeldung) Tj ET \
BT /F1 10 Tf 30 230 Td (Name:) Tj ET 0.75 w 80 228 m 380 228 l S \
1 w 30 150 350 50 re S BT /F1 7 Tf 34 191 Td (Anschrift) Tj ET \
BT /F1 8 Tf 30 124 Td (Telefon) Tj ET 30 90 350 30 re S \
30 50 10 10 re S BT /F1 9 Tf 46 51 Td (Haupt) Tj ET 150 50 10 10 re S BT /F1 9 Tf 166 51 Td (Zweig) Tj ET";
fn paper_form_with(w: u32, h: u32, content: &str) -> Vec<u8> {
let objs = [
b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(),
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 420 300] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>".to_vec(),
format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {w} {h}] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>").into_bytes(),
format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()).into_bytes(),
b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".to_vec(),
];
@@ -7498,6 +7502,20 @@ fn paper_form() -> Vec<u8> {
out
}
/// A printed form, 420 × 300 pt: a title, a label with a rule to write on, a box with a small
/// label in its corner, an empty box with its label above, and two check boxes.
fn paper_form() -> Vec<u8> {
paper_form_with(
420,
300,
"BT /F1 14 Tf 30 270 Td (Gewerbe-Ummeldung) Tj ET \
BT /F1 10 Tf 30 230 Td (Name:) Tj ET 0.75 w 80 228 m 380 228 l S \
1 w 30 150 350 50 re S BT /F1 7 Tf 34 191 Td (Anschrift) Tj ET \
BT /F1 8 Tf 30 124 Td (Telefon) Tj ET 30 90 350 30 re S \
30 50 10 10 re S BT /F1 9 Tf 46 51 Td (Haupt) Tj ET 150 50 10 10 re S BT /F1 9 Tf 166 51 Td (Zweig) Tj ET",
)
}
/// How the paper form was scanned.
#[derive(Clone, Copy, Debug)]
enum Scan {
+8
View File
@@ -45,6 +45,14 @@ let cells: Vec<table::Cell> = grid.cells_for(names)?; // "Table1_1", "Table
several steps (filling, recalculating, the XFA data, the view), most with nothing changed in
between, so it walks a form once (twice on a laid-out XFA form, whose data is written in
between); `form_reads_on_this_thread` counts the walks, for tests.
- Automatic field detection considers the first 2,000 drawn rectangles in input order for
combs, check boxes and empty text boxes. Check-box label detection considers at most 2,000
drawn/existing widget rectangles combined, before filtering or deduplication. Unusually
complex pages may therefore have partial proposals, returned in reading order; existing
widgets always exclude overlapping proposals, even beyond the label budget. A balanced
spatial index avoids repeated full scans, with a shared 128,000-step query budget. If
adversarial geometry exhausts it, unverified proposals are omitted, not accepted. As the input
supplies widget geometry only, any existing small square widget can reserve a nearby label.
- Inheritable attributes (`/FT`, `/Ff`, `/V`, `/DV`, `/DA`, `/Q`, `/MaxLen`) are resolved down the
field tree; the form's `/DA` and `/Q` are the last fallback.
- Text and choice fields get new appearance streams (`/Tx BMC … EMC`): auto-size (`0 Tf`), comb
+358 -53
View File
@@ -49,6 +49,11 @@ pub struct Candidate {
/// points along its sides (#2942: a cell drawn with a midpoint on each vertical side).
const MAX_RECT_POINTS: usize = 16;
/// Maximum rectangle inputs examined for combs and boxes, and maximum combined drawn/existing
/// rectangle inputs examined for check-box labels. Count inputs before filtering or deduplication:
/// repeated or irrelevant rectangles must not defeat the bound on pairwise comparisons.
pub const MAX_DETECTION_BOXES: usize = 2000;
/// Graphics states saved (`q`) and kept at once while detecting; deeper saves are counted, not kept.
const MAX_SAVES: usize = 256;
@@ -335,30 +340,13 @@ fn clean(label: &str) -> String {
s.chars().take(40).collect::<String>().trim().to_string()
}
/// The label for a field at `rect`: the words to its left on the same line, else the words
/// The label for a text field at `rect`: the words to its left on the same line, else the words
/// just above it.
fn label_for(words: &[Word], rect: [f64; 4], right_of: bool) -> String {
fn label_for(words: &[Word], rect: [f64; 4]) -> String {
let h = (rect[3] - rect[1]).max(8.0);
let same_line = |w: &Word| (mid(w.rect) - mid(rect)).abs() < h.max(10.0) * 0.7;
let mut left: Vec<&Word> = if right_of {
words.iter().filter(|w| same_line(w) && w.rect[0] >= rect[2] - 1.0 && w.rect[0] - rect[2] < 200.0).collect()
} else {
words.iter().filter(|w| same_line(w) && w.rect[2] <= rect[0] + 1.0 && rect[0] - w.rect[2] < 250.0).collect()
};
let mut left: Vec<&Word> = words.iter().filter(|w| same_line(w) && w.rect[2] <= rect[0] + 1.0 && rect[0] - w.rect[2] < 250.0).collect();
left.sort_by(|a, b| a.rect[0].total_cmp(&b.rect[0]));
if right_of {
// The words that follow the box, up to the next gap wider than a few spaces.
let mut picked = Vec::new();
let mut x = rect[2];
for w in left {
if w.rect[0] - x > 24.0 {
break;
}
picked.push(w.text.as_str());
x = w.rect[2];
}
return clean(&picked.join(" "));
}
// The words that end nearest the field, back to a wide gap.
let mut picked: Vec<&str> = Vec::new();
let mut x = rect[0];
@@ -387,6 +375,119 @@ fn label_for(words: &[Word], rect: [f64; 4], right_of: bool) -> String {
clean(&on.iter().map(|w| w.text.as_str()).collect::<Vec<_>>().join(" "))
}
/// Sort items into reading order: top to bottom (rows within 8 pt of the row's first), left to right.
fn sort_reading_order<T>(mut items: Vec<T>, rect_of: impl Fn(&T) -> [f64; 4]) -> Vec<T> {
items.sort_by(|a, b| mid(rect_of(b)).total_cmp(&mid(rect_of(a))));
let mut rows = Vec::with_capacity(items.len());
let (mut row, mut start) = (0usize, f64::INFINITY);
for item in &items {
if start - mid(rect_of(item)) > 8.0 {
row += 1;
start = mid(rect_of(item));
}
rows.push(row);
}
let mut keyed: Vec<(usize, T)> = rows.into_iter().zip(items).collect();
keyed.sort_by(|a, b| a.0.cmp(&b.0).then(rect_of(&a.1)[0].total_cmp(&rect_of(&b.1)[0])));
keyed.into_iter().map(|(_, item)| item).collect()
}
/// The label for a check box at `rect`: the nearer unclaimed phrase on the same line (to its
/// right or to its left), without crossing or stealing a phrase that sits closer to an adjacent
/// check box on the line.
fn checkbox_label(words: &[Word], rect: [f64; 4], boxes: &[[f64; 4]], used: &mut std::collections::HashSet<usize>) -> String {
let h = (rect[3] - rect[1]).max(8.0);
let same_line_word = |r: [f64; 4]| (mid(r) - mid(rect)).abs() < h.max(10.0) * 0.7;
let same_line_box = |b: [f64; 4]| {
#[cfg(test)]
work_bound_tests::record_comparison();
(mid(b) - mid(rect)).abs() < h.max(b[3] - b[1]).max(10.0) * 0.7
};
let has_content = |w: &Word| !clean(&w.text).is_empty();
let prev_box_x2 =
boxes.iter().copied().filter(|b| *b != rect && same_line_box(*b) && b[2] <= rect[0] + 1.0).map(|b| b[2]).fold(f64::NEG_INFINITY, f64::max);
let next_box_x0 =
boxes.iter().copied().filter(|b| *b != rect && same_line_box(*b) && b[0] >= rect[2] - 1.0).map(|b| b[0]).fold(f64::INFINITY, f64::min);
let mut left_words: Vec<(usize, &Word)> = words
.iter()
.enumerate()
.filter(|(i, w)| {
!used.contains(i) && same_line_word(w.rect) && w.rect[2] <= rect[0] + 1.0 && rect[0] - w.rect[2] < 200.0 && w.rect[0] >= prev_box_x2 - 1.0
})
.collect();
left_words.sort_by(|a, b| a.1.rect[0].total_cmp(&b.1.rect[0]));
let mut left_picked: Vec<(usize, &Word)> = Vec::new();
let mut x = rect[0];
let mut has_word = false;
for (i, w) in left_words.into_iter().rev() {
if x - w.rect[2] > 24.0 || w.text.chars().all(|c| c == '_') || (has_word && w.text.trim_end().ends_with(':')) {
break;
}
has_word |= has_content(w);
left_picked.push((i, w));
x = w.rect[0];
}
left_picked.reverse();
let left_candidate = match (left_picked.iter().find(|(_, w)| has_content(w)), left_picked.iter().rev().find(|(_, w)| has_content(w))) {
(Some((_, first)), Some((_, last))) => {
let text = clean(&left_picked.iter().map(|(_, w)| w.text.as_str()).collect::<Vec<_>>().join(" "));
let gap = (rect[0] - last.rect[2]).max(0.0);
let prev_gap = (first.rect[0] - prev_box_x2).max(0.0);
let colon = left_picked.last().is_some_and(|(_, w)| w.text.trim_end().ends_with(':')) || last.text.trim_end().ends_with(':');
(!text.is_empty() && gap <= prev_gap).then_some((text, gap, colon, left_picked))
}
_ => None,
};
let mut right_words: Vec<(usize, &Word)> = words
.iter()
.enumerate()
.filter(|(i, w)| {
!used.contains(i) && same_line_word(w.rect) && w.rect[0] >= rect[2] - 1.0 && w.rect[0] - rect[2] < 200.0 && w.rect[2] <= next_box_x0 + 1.0
})
.collect();
right_words.sort_by(|a, b| a.1.rect[0].total_cmp(&b.1.rect[0]));
let mut right_picked: Vec<(usize, &Word)> = Vec::new();
let mut x = rect[2];
let mut has_word = false;
for (i, w) in right_words {
if w.rect[0] - x > 24.0 || w.text.chars().all(|c| c == '_') || (has_word && (next_box_x0 - w.rect[2]).max(0.0) < w.rect[0] - x) {
break;
}
has_word |= has_content(w);
right_picked.push((i, w));
x = w.rect[2];
}
let right_candidate = match (right_picked.iter().find(|(_, w)| has_content(w)), right_picked.iter().rev().find(|(_, w)| has_content(w))) {
(Some((_, first)), Some((_, last))) => {
let text = clean(&right_picked.iter().map(|(_, w)| w.text.as_str()).collect::<Vec<_>>().join(" "));
let gap = (first.rect[0] - rect[2]).max(0.0);
let next_gap = (next_box_x0 - last.rect[2]).max(0.0);
(!text.is_empty() && gap <= next_gap).then_some((text, gap, next_gap, right_picked))
}
_ => None,
};
let chosen = match (left_candidate, right_candidate) {
(Some((lt, l_gap, colon, l_words)), Some((rt, r_gap, next_gap, r_words))) => {
let pick_left = !colon && (l_gap < r_gap || (next_gap <= 24.0 && l_gap <= r_gap));
if pick_left { Some((lt, l_words)) } else { Some((rt, r_words)) }
}
(Some((lt, _, _, l_words)), None) => Some((lt, l_words)),
(None, Some((rt, _, _, r_words))) => Some((rt, r_words)),
(None, None) => None,
};
if let Some((text, picked)) = chosen {
for (i, _) in picked {
used.insert(i);
}
text
} else {
String::new()
}
}
/// Rows of two or more touching cells of one size, as (the row's rect, cell count). Two is
/// enough: dates split into day and month groups of two cells. Cells are square or taller
/// than wide; an empty table of wider cells is not a comb.
@@ -404,6 +505,8 @@ fn combs(boxes: &[[f64; 4]]) -> Vec<([f64; 4], usize)> {
.collect();
// `b` comes right after `a` in a row: same size, its left edge on a's right edge.
let follows = |a: &[f64; 4], b: &[f64; 4]| {
#[cfg(test)]
work_bound_tests::record_comparison();
(b[0] - a[2]).abs() <= 1.0 && (b[1] - a[1]).abs() <= 0.5 && (b[3] - a[3]).abs() <= 0.5 && ((b[2] - b[0]) - (a[2] - a[0])).abs() <= 0.5
};
let mut out = Vec::new();
@@ -426,13 +529,95 @@ fn combs(boxes: &[[f64; 4]]) -> Vec<([f64; 4], usize)> {
out
}
/// A balanced bounding-volume tree indexes *all* existing widgets, independently of the
/// proposal/label budget. Far-away widgets can be excluded together instead of rescanning
/// the entire page for each proposal. Construction is O(n log n), with logarithmic depth.
struct WidgetIndex {
bounds: [f64; 4],
contents: WidgetContents,
}
enum WidgetContents {
Leaf(Box<[[f64; 4]]>),
Branch(Box<WidgetIndex>, Box<WidgetIndex>),
}
fn rectangles_overlap(a: [f64; 4], b: [f64; 4]) -> bool {
#[cfg(test)]
work_bound_tests::record_comparison();
a[0] < b[2] - 1.0 && b[0] < a[2] - 1.0 && a[1] < b[3] - 1.0 && b[1] < a[3] - 1.0
}
impl WidgetIndex {
fn new(rects: &[[f64; 4]]) -> Option<Self> {
// A NaN makes the exact overlap predicate false. Infinities and inverted boxes are
// retained: pruning uses normalized bounds, but leaves preserve original geometry.
let mut rects: Vec<_> = rects.iter().copied().filter(|r| !r.iter().any(|x| x.is_nan())).collect();
Self::build(&mut rects, 0)
}
fn build(rects: &mut [[f64; 4]], depth: usize) -> Option<Self> {
let first = rects.first()?;
let mut bounds = [first[0].min(first[2]), first[1].min(first[3]), first[0].max(first[2]), first[1].max(first[3])];
for r in rects.iter().skip(1) {
bounds = [bounds[0].min(r[0]).min(r[2]), bounds[1].min(r[1]).min(r[3]), bounds[2].max(r[0]).max(r[2]), bounds[3].max(r[1]).max(r[3])];
}
let contents = if rects.len() <= 8 {
WidgetContents::Leaf(rects.to_vec().into_boxed_slice())
} else {
let axis = depth % 2;
let mid = rects.len() / 2;
rects.select_nth_unstable_by(mid, |a, b| a[axis].total_cmp(&b[axis]));
let (left, right) = rects.split_at_mut(mid);
WidgetContents::Branch(Box::new(Self::build(left, depth + 1)?), Box::new(Self::build(right, depth + 1)?))
};
Some(Self { bounds, contents })
}
fn overlaps(&self, rect: [f64; 4], remaining: &mut usize) -> bool {
// Spatial indexes can degenerate on adversarial geometry. Exhaustion suppresses
// the unverified proposal, rather than overlooking any existing widget exclusion.
if *remaining == 0 {
return true;
}
*remaining -= 1;
#[cfg(test)]
work_bound_tests::record_index_visit();
if !rectangles_overlap(self.bounds, rect) {
return false;
}
match &self.contents {
WidgetContents::Leaf(rects) => rects.iter().any(|r| {
if *remaining == 0 {
return true;
}
*remaining -= 1;
rectangles_overlap(*r, rect)
}),
WidgetContents::Branch(left, right) => left.overlaps(rect, remaining) || right.overlaps(rect, remaining),
}
}
}
/// Propose fields for a page. `existing` are the page's widget rectangles (no candidate
/// overlaps them); names are unique against `taken`.
///
/// Box detection considers only the first [`MAX_DETECTION_BOXES`] drawn rectangles in input
/// order, and check-box labelling considers at most that many drawn/existing rectangles combined.
/// Limits apply before pairwise comb/overlap/label work, not just to accepted candidates. This
/// heuristic can return a partial set on unusually complex pages, like raster shape detection;
/// the retained candidates are still returned in reading order. Every existing widget remains
/// an overlap exclusion, even when it falls outside the label-detection budget.
pub fn detect(words: &[Word], shapes: &Shapes, existing: &[[f64; 4]], taken: &[String]) -> Vec<Candidate> {
// Bound the comb pass too: small squares qualify as comb cells, so limiting only the new
// check-box loop would still leave quadratic work on all of a hostile page's rectangles.
let boxes = shapes.boxes.get(..shapes.boxes.len().min(MAX_DETECTION_BOXES)).unwrap_or_default();
let mut out: Vec<Candidate> = Vec::new();
let overlaps = |a: [f64; 4], b: [f64; 4]| a[0] < b[2] - 1.0 && b[0] < a[2] - 1.0 && a[1] < b[3] - 1.0 && b[1] < a[3] - 1.0;
let push = |kind: Kind, rect: [f64; 4], label: String, out: &mut Vec<Candidate>| {
if existing.iter().chain(out.iter().map(|c| &c.rect)).any(|e| overlaps(*e, rect)) {
let overlaps = rectangles_overlap;
let existing_index = WidgetIndex::new(existing);
let mut existing_work = 64 * MAX_DETECTION_BOXES;
let mut push = |kind: Kind, rect: [f64; 4], label: String, out: &mut Vec<Candidate>| {
if existing_index.as_ref().is_some_and(|index| index.overlaps(rect, &mut existing_work)) || out.iter().any(|c| overlaps(c.rect, rect)) {
return;
}
out.push(Candidate { kind, rect, name: label });
@@ -443,33 +628,45 @@ pub fn detect(words: &[Word], shapes: &Shapes, existing: &[[f64; 4]], taken: &[S
for b in blanks {
let h = (b.rect[3] - b.rect[1]).max(10.0);
let rect = [b.rect[0], b.rect[1], b.rect[2], b.rect[1] + (h * 1.4).clamp(12.0, 22.0)];
let l = label_for(&labels, rect, false);
let l = label_for(&labels, rect);
push(Kind::Text, rect, l, &mut out);
}
// Comb boxes (postcodes, dates, IBANs): one text field per row of cells, found before
// check boxes so the cells don't each become one.
for (rect, cells) in combs(&shapes.boxes) {
for (rect, cells) in combs(boxes) {
if labels.iter().any(|x| overlaps(x.rect, rect)) {
continue;
}
let l = label_for(&labels, rect, false);
let l = label_for(&labels, rect);
push(Kind::Comb { cells }, rect, l, &mut out);
}
// Small squares: check boxes, labelled by the text to their right.
for r in &shapes.boxes {
// Small squares: check boxes, labelled by the nearer text to their right or left.
// `existing` contains geometry only: a small square widget of any type reserves its label.
// Cap examined inputs, not accepted boxes, so duplicates cannot keep this pass running.
let mut checkboxes: Vec<[f64; 4]> = Vec::new();
for r in boxes.iter().chain(existing.iter()).take(MAX_DETECTION_BOXES) {
let (w, h) = (r[2] - r[0], r[3] - r[1]);
if (5.0..=20.0).contains(&w) && (5.0..=20.0).contains(&h) && (w / h - 1.0).abs() < 0.25 {
let l = label_for(&labels, *r, true);
push(Kind::CheckBox, *r, l, &mut out);
if (5.0..=20.0).contains(&w)
&& (5.0..=20.0).contains(&h)
&& (w / h - 1.0).abs() < 0.25
&& !out.iter().map(|c| &c.rect).chain(checkboxes.iter()).any(|e| overlaps(*e, *r))
{
checkboxes.push(*r);
}
}
let checkboxes = sort_reading_order(checkboxes, |r| *r);
let mut used_words = std::collections::HashSet::new();
for r in &checkboxes {
let l = checkbox_label(&labels, *r, &checkboxes, &mut used_words);
push(Kind::CheckBox, *r, l, &mut out);
}
// Empty boxes big enough to write in: text fields inside them.
for r in &shapes.boxes {
for r in boxes {
let (w, h) = (r[2] - r[0], r[3] - r[1]);
let has_text = labels.iter().any(|x| overlaps(x.rect, *r));
if w >= 40.0 && (12.0..=60.0).contains(&h) && !has_text {
let inner = [r[0] + 1.0, r[1] + 1.0, r[2] - 1.0, r[3] - 1.0];
let l = label_for(&labels, *r, false);
let l = label_for(&labels, *r);
push(Kind::Text, inner, l, &mut out);
}
}
@@ -484,26 +681,14 @@ pub fn detect(words: &[Word], shapes: &Shapes, existing: &[[f64; 4]], taken: &[S
if labels.iter().any(|x| overlaps(x.rect, rect)) {
continue;
}
let l = label_for(&labels, rect, false);
let l = label_for(&labels, rect);
push(Kind::Text, rect, l, &mut out);
}
// Reading order: top to bottom, left to right.
// Rows: fields whose middles are within 8 pt of the row's first.
out.sort_by(|a, b| mid(b.rect).total_cmp(&mid(a.rect)));
let mut rows = Vec::with_capacity(out.len());
let (mut row, mut start) = (0usize, f64::INFINITY);
for c in &out {
if start - mid(c.rect) > 8.0 {
row += 1;
start = mid(c.rect);
}
rows.push(row);
}
let mut keyed: Vec<(usize, Candidate)> = rows.into_iter().zip(out).collect();
keyed.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.rect[0].total_cmp(&b.1.rect[0])));
let mut out: Vec<Candidate> = keyed.into_iter().map(|(_, c)| c).collect();
let mut out = sort_reading_order(out, |c| c.rect);
// Unique names (Acrobat numbers repeats); unlabelled fields get Text1, Check Box1, …
let mut used: Vec<String> = taken.to_vec();
// A linear membership scan inside each suffix retry makes a page of unnamed squares
// cubic even after bounding the geometric passes.
let mut used: std::collections::HashSet<String> = taken.iter().cloned().collect();
for c in &mut out {
let base = if c.name.is_empty() {
match c.kind {
@@ -519,15 +704,135 @@ pub fn detect(words: &[Word], shapes: &Shapes, existing: &[[f64; 4]], taken: &[S
n += 1;
name = format!("{base}{n}");
}
used.push(name.clone());
used.insert(name.clone());
c.name = name;
}
out
}
#[cfg(test)]
mod comb_tests {
use super::combs;
mod work_bound_tests {
use super::*;
use std::cell::Cell;
// Per-thread so the other detection tests can run concurrently. Count work rather than
// wall time: appending more hostile rectangles must not add pairwise comparisons.
thread_local! {
static COMPARISONS: Cell<usize> = const { Cell::new(0) };
static INDEX_VISITS: Cell<usize> = const { Cell::new(0) };
}
pub(super) fn record_comparison() {
COMPARISONS.set(COMPARISONS.get() + 1);
}
pub(super) fn record_index_visit() {
INDEX_VISITS.set(INDEX_VISITS.get() + 1);
}
#[test]
fn rectangle_budget_bounds_comparisons_before_filtering() {
for duplicates in [false, true] {
for existing_only in [false, true] {
let boxes = (0..MAX_DETECTION_BOXES)
.map(|i| {
let x = if duplicates { 0.0 } else { i as f64 * 30.0 };
[x, 0.0, x + 10.0, 10.0]
})
.collect();
let mut shapes = Shapes { boxes, rules: Vec::new() };
let run = |shapes: &Shapes| {
if existing_only { detect(&[], &Shapes::default(), &shapes.boxes, &[]) } else { detect(&[], shapes, &[], &[]) }
};
COMPARISONS.set(0);
let expected = run(&shapes);
let bounded = COMPARISONS.get();
assert!(bounded > 0 && bounded <= 5 * MAX_DETECTION_BOXES * MAX_DETECTION_BOXES);
shapes.boxes.extend_from_within(..);
COMPARISONS.set(0);
assert_eq!(run(&shapes), expected);
if existing_only {
// Existing widgets are never discarded: doubling them may add tree levels,
// but must not restore the full pairwise scan.
assert!(COMPARISONS.get() <= bounded + 16 * MAX_DETECTION_BOXES);
} else {
assert_eq!(COMPARISONS.get(), bounded, "discarded inputs add no pairwise work");
}
}
}
}
#[test]
fn many_existing_widgets_do_not_multiply_proposal_work() {
let mut existing: Vec<_> = (0..100_000)
.map(|i| {
let x = i as f64 * 30.0;
[x, 100.0, x + 10.0, 110.0]
})
.collect();
let shapes = Shapes {
boxes: (0..MAX_DETECTION_BOXES)
.map(|i| {
let x = i as f64 * 30.0;
[x, 0.0, x + 10.0, 10.0]
})
.collect(),
rules: Vec::new(),
};
// An exclusion beyond both budgets must still suppress the last drawn candidate.
let exclusion = [59_970.0, 0.0, 59_980.0, 10.0];
existing.push(exclusion);
COMPARISONS.set(0);
let expected = detect(&[], &shapes, &[exclusion], &[]);
let baseline_work = COMPARISONS.get();
COMPARISONS.set(0);
INDEX_VISITS.set(0);
let candidates = detect(&[], &shapes, &existing, &[]);
assert_eq!(candidates.len(), MAX_DETECTION_BOXES - 1);
assert_eq!(candidates, expected);
assert!(!candidates.iter().any(|c| c.rect == [59_970.0, 0.0, 59_980.0, 10.0]));
assert!(COMPARISONS.get() <= baseline_work + 64 * MAX_DETECTION_BOXES, "existing-widget comparisons must not be candidates times widgets");
assert!(INDEX_VISITS.get() < 100 * MAX_DETECTION_BOXES);
}
#[test]
fn widget_index_matches_linear_overlap_semantics() {
let mut rects =
vec![[0.0, 0.0, 10.0, 10.0], [30.0, 20.0, 20.0, 10.0], [f64::NEG_INFINITY, 50.0, f64::INFINITY, 60.0], [f64::NAN, 0.0, 10.0, 10.0]];
rects.extend((0..500).map(|i| {
let x = (i % 25) as f64 * 3.0;
let y = (i / 25) as f64 * 4.0;
[x, y, x + 2.0, y + 2.0]
}));
let index = WidgetIndex::new(&rects).expect("nonempty index");
for x in -5..40 {
for y in -5..70 {
let query = [x as f64, y as f64, x as f64 + 12.0, y as f64 + 12.0];
let mut remaining = usize::MAX;
assert_eq!(index.overlaps(query, &mut remaining), rects.iter().any(|r| rectangles_overlap(*r, query)));
}
}
}
#[test]
fn adversarial_widget_geometry_exhausts_budget_without_accepting_unchecked_fields() {
let existing = vec![[1e9, 1e9, -1e9, -1e9]; 100_000];
let shapes = Shapes {
boxes: (0..MAX_DETECTION_BOXES)
.map(|i| {
let x = i as f64 * 30.0;
[x, 0.0, x + 10.0, 10.0]
})
.collect(),
rules: Vec::new(),
};
COMPARISONS.set(0);
INDEX_VISITS.set(0);
let candidates = detect(&[], &shapes, &existing, &[]);
assert!(candidates.is_empty(), "an exhausted search cannot verify any proposal");
assert!(INDEX_VISITS.get() <= 64 * MAX_DETECTION_BOXES);
assert!(COMPARISONS.get() < 5 * MAX_DETECTION_BOXES * MAX_DETECTION_BOXES);
}
#[test]
fn repeated_boxes_do_not_change_or_multiply_a_comb() {
+126
View File
@@ -4316,6 +4316,132 @@ fn document_scripts_read_each_name_tree_node_once() {
assert_eq!(document_scripts_named(&doc), [("init".to_string(), "var ready = true;".to_string())]);
}
#[test]
fn detection_names_check_boxes_from_the_nearer_neighbouring_label() {
use crate::detect::*;
let w = |t: &str, x0: f64, y0: f64, x1: f64| Word { text: t.into(), rect: [x0, y0, x1, y0 + 10.0] };
let names_of = |words: &[Word], boxes: &[[f64; 4]]| -> Vec<String> {
detect(words, &Shapes { boxes: boxes.to_vec(), rules: Vec::new() }, &[], &[])
.into_iter()
.map(|c| {
assert_eq!(c.kind, Kind::CheckBox);
c.name
})
.collect()
};
// #2943: "Monsieur [box] Madame [box]" — each label ends ~4-5 pt before its box, while
// the next label begins 23 pt after the first box. Drawing order does not change the names.
let french_words = [w("Monsieur", 100.0, 532.0, 141.0), w("Madame", 180.0, 532.0, 218.0)];
let french_boxes = [[146.0, 532.0, 157.0, 542.0], [222.0, 532.0, 233.0, 542.0]];
assert_eq!(names_of(&french_words, &french_boxes), ["Monsieur", "Madame"]);
assert_eq!(names_of(&french_words, &[french_boxes[1], french_boxes[0]]), ["Monsieur", "Madame"]);
// Two filers on the same form ("Monsieur [ ] Madame [ ]" repeated) number the second pair.
let two_filers_words =
[w("Monsieur", 100.0, 532.0, 141.0), w("Madame", 180.0, 532.0, 218.0), w("Monsieur", 320.0, 532.0, 361.0), w("Madame", 400.0, 532.0, 438.0)];
let two_filers_boxes = [[146.0, 532.0, 157.0, 542.0], [222.0, 532.0, 233.0, 542.0], [366.0, 532.0, 377.0, 542.0], [442.0, 532.0, 453.0, 542.0]];
assert_eq!(names_of(&two_filers_words, &two_filers_boxes), ["Monsieur", "Madame", "Monsieur2", "Madame2"]);
// Even with equal 5 pt gaps ("Label [ ] Label [ ]" vs "[ ] Label [ ] Label"), each box pairs
// with its own neighbour rather than leaving an outer box or label orphaned.
let tight_left_words = [w("Monsieur", 100.0, 400.0, 140.0), w("Madame", 160.0, 400.0, 200.0)];
let tight_left_boxes = [[145.0, 400.0, 155.0, 410.0], [205.0, 400.0, 215.0, 410.0]];
assert_eq!(names_of(&tight_left_words, &tight_left_boxes), ["Monsieur", "Madame"]);
let tight_right_words = [w("Monsieur", 115.0, 400.0, 155.0), w("Madame", 175.0, 400.0, 215.0)];
let tight_right_boxes = [[100.0, 400.0, 110.0, 410.0], [160.0, 400.0, 170.0, 410.0]];
assert_eq!(names_of(&tight_right_words, &tight_right_boxes), ["Monsieur", "Madame"]);
// An unlabelled box does not steal a label that sits closer to the adjacent box.
assert_eq!(names_of(&[w("Madame", 180.0, 532.0, 218.0)], &french_boxes), ["Check Box1", "Madame"]);
assert_eq!(
names_of(&[w("Monsieur", 115.0, 400.0, 155.0)], &[[100.0, 400.0, 110.0, 410.0], [178.0, 400.0, 188.0, 410.0]]),
["Monsieur", "Check Box1"]
);
// Deliberate ambiguity: "[ ] 4pt Yes 3pt [ ] No" gives Yes to the closer box.
assert_eq!(
names_of(&[w("Yes", 114.0, 400.0, 134.0), w("No", 151.0, 400.0, 166.0)], &[[100.0, 400.0, 110.0, 410.0], [137.0, 400.0, 147.0, 410.0]]),
["Check Box1", "Yes"]
);
// A row heading ending with a colon ("Status: [ ] Single [ ] Married" or French
// "Civilité : Monsieur [ ] Madame [ ]") does not beat or merge into the option label,
// while a standalone "Married: [ ]" or "Date of birth : [ ]" still uses its prompt.
let prompt_words = [w("Status:", 50.0, 300.0, 96.0), w("Single", 115.0, 300.0, 145.0), w("Married", 185.0, 300.0, 225.0)];
let prompt_boxes = [[100.0, 300.0, 110.0, 310.0], [170.0, 300.0, 180.0, 310.0]];
assert_eq!(names_of(&prompt_words, &prompt_boxes), ["Single", "Married"]);
let civilite_words =
[w("Civilité", 30.0, 532.0, 72.0), w(":", 75.0, 532.0, 78.0), w("Monsieur", 100.0, 532.0, 141.0), w("Madame", 180.0, 532.0, 218.0)];
assert_eq!(names_of(&civilite_words, &french_boxes), ["Monsieur", "Madame"]);
assert_eq!(names_of(&[w("Married:", 50.0, 300.0, 95.0)], &[[100.0, 300.0, 110.0, 310.0]]), ["Married"]);
assert_eq!(
names_of(
&[w("Date", 30.0, 300.0, 52.0), w("of", 55.0, 300.0, 65.0), w("birth", 68.0, 300.0, 90.0), w(":", 92.0, 300.0, 95.0)],
&[[100.0, 300.0, 110.0, 310.0]]
),
["Date of birth"]
);
// "[ ] Oui Non [ ]" with an 18 pt gap between "Oui" and "Non" splits the two labels
// between their respective boxes instead of merging "Oui Non".
let mixed_words = [w("Oui", 115.0, 300.0, 135.0), w("Non", 153.0, 300.0, 175.0)];
let mixed_boxes = [[100.0, 300.0, 110.0, 310.0], [180.0, 300.0, 190.0, 310.0]];
assert_eq!(names_of(&mixed_words, &mixed_boxes), ["Oui", "Non"]);
// An existing check-box widget still claims its own label so a neighbouring unlabelled box
// does not steal it.
let existing = [[100.0, 400.0, 110.0, 410.0]];
let remaining =
detect(&[w("Monsieur", 115.0, 400.0, 155.0)], &Shapes { boxes: vec![[178.0, 400.0, 188.0, 410.0]], rules: Vec::new() }, &existing, &[]);
assert_eq!(remaining.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(), ["Check Box1"]);
// A stray punctuation token right after the first box does not pull the next box's label in.
assert_eq!(names_of(&[w(".", 158.0, 532.0, 160.0), w("Madame", 180.0, 532.0, 218.0)], &french_boxes), ["Check Box1", "Madame"]);
}
/// A hostile vector page can contain arbitrarily many small squares; detection must not run
/// pairwise comb/overlap/neighbor work on all of them, nor cubic default-name lookup.
#[test]
fn detection_bounds_many_small_squares_before_pairwise_work() {
use crate::detect::*;
let boxes = (0..MAX_DETECTION_BOXES * 50)
.map(|i| {
let x = (i % 100) as f64 * 30.0;
let y = (i / 100) as f64 * 30.0;
[x, y, x + 10.0, y + 10.0]
})
.collect();
let shapes = Shapes { boxes, rules: Vec::new() };
let found = detect(&[], &shapes, &[], &["Check Box1".into(), "Check Box3".into()]);
assert_eq!(found.len(), MAX_DETECTION_BOXES);
assert!(found.iter().all(|c| c.kind == Kind::CheckBox));
assert_eq!(found.first().unwrap().name, "Check Box2");
assert_eq!(found.get(1).unwrap().name, "Check Box4");
assert_eq!(found.last().unwrap().name, format!("Check Box{}", MAX_DETECTION_BOXES + 2));
assert_eq!(found.iter().map(|c| &c.name).collect::<std::collections::HashSet<_>>().len(), found.len());
let prefix = Shapes { boxes: shapes.boxes.iter().take(MAX_DETECTION_BOXES).copied().collect(), rules: Vec::new() };
assert_eq!(found, detect(&[], &prefix, &[], &["Check Box1".into(), "Check Box3".into()]));
}
#[test]
fn detection_box_budget_counts_rejected_inputs_and_keeps_existing_exclusions() {
use crate::detect::*;
let first = [0.0, 0.0, 10.0, 10.0];
let last = [30.0, 0.0, 40.0, 10.0];
// The final input at the limit is included; the next is not, even if earlier inputs are
// duplicates. A later input higher on the page stays outside the budget despite reading first.
let mut boxes = vec![first; MAX_DETECTION_BOXES - 1];
boxes.push(last);
boxes.push([60.0, 30.0, 70.0, 40.0]);
let shapes = Shapes { boxes, rules: Vec::new() };
assert_eq!(detect(&[], &shapes, &[], &[]).iter().map(|c| c.rect).collect::<Vec<_>>(), [first, last]);
let mut existing = vec![[90.0, 0.0, 100.0, 10.0]; MAX_DETECTION_BOXES];
existing.push(last);
assert_eq!(detect(&[], &shapes, &existing, &[]).iter().map(|c| c.rect).collect::<Vec<_>>(), [first]);
}
#[test]
fn name_tree_nodes_sharing_one_long_kids_array_queue_it_a_bounded_number_of_times() {
// 10,000 nodes share one /Kids array of 210,001 entries: the leaf, every node, and the leaf