mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-19 10:35:59 +00:00
The phrase search collapsed runs of whitespace in the query but not in the page text it was matching against, so a document drawn with two space glyphs in a row like the issue example never matched the phrase. Single words still matched, which is why it looked like only some files were affected. Collapse whitespace while the text is flattened, the same way the query is normalised. The regression test fails on the old code because the phrase find returns an empty range, and it also covers tabs and single word queries. Fixes #814. Co-authored-by: Big Pickle <noreply@opencode.ai>
This commit is contained in:
1 parent
f9ed8b264a
commit
e0625cd5cf
1 file changed
+27
-1
@@ -88,7 +88,12 @@ impl PageText {
|
||||
chars.push((' ', i));
|
||||
}
|
||||
for c in fold(&g.text).chars() {
|
||||
chars.push((if c.is_whitespace() { ' ' } else { c }, i));
|
||||
let c = if c.is_whitespace() { ' ' } else { c };
|
||||
// The query collapses a run of whitespace to one space, so collapse the page too.
|
||||
if c == ' ' && chars.last().is_some_and(|k| k.0 == ' ') {
|
||||
continue;
|
||||
}
|
||||
chars.push((c, i));
|
||||
}
|
||||
}
|
||||
let word = |k: Option<&(char, usize)>| k.is_some_and(|c| c.0.is_alphanumeric());
|
||||
@@ -932,6 +937,27 @@ mod tests {
|
||||
assert_eq!(t.nearest(52.0, 14.0), Some(5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn phrase_search_ignores_repeated_spaces() {
|
||||
// "quick brown" drawn with two space glyphs in a row. Text extraction keeps both spaces,
|
||||
// but the phrase search has always collapsed runs of whitespace in the query, so the page
|
||||
// text has to be collapsed the same way or the phrase is never found (#814).
|
||||
let mut v = Vec::new();
|
||||
word(&mut v, "quick", 10.0, 10.0, 6.0);
|
||||
v.push(g(" ", 46.0, 10.0, 52.0));
|
||||
v.push(g(" ", 52.0, 10.0, 58.0));
|
||||
word(&mut v, "brown", 58.0, 10.0, 6.0);
|
||||
let n = v.len();
|
||||
let t = PageText { glyphs: v, line_of: vec![0; n], space_before: vec![false; n] };
|
||||
assert_eq!(t.plain_text(), "quick brown");
|
||||
assert_eq!(t.find("quick brown"), vec![0..12]);
|
||||
assert_eq!(t.find("quick brown"), vec![0..12], "the query's own run of spaces is collapsed too");
|
||||
assert_eq!(t.find("quick\tbrown"), vec![0..12]);
|
||||
assert_eq!(t.find("quick"), vec![0..5]);
|
||||
assert_eq!(t.find("brown"), vec![7..12]);
|
||||
assert!(t.find("quick absent").is_empty());
|
||||
}
|
||||
|
||||
fn word(v: &mut Vec<TextGlyph>, s: &str, x: f32, y: f32, advance: f32) {
|
||||
for (i, c) in s.chars().enumerate() {
|
||||
let x0 = x + i as f32 * advance;
|
||||
|
||||
Reference in new issue
Block a user