From e0625cd5cfc53f7f69583619942034fd6fd92204 Mon Sep 17 00:00:00 2001 From: muraa-p <85794781+muraa-p@users.noreply.github.com> Date: Sun, 11 Oct 2026 03:06:43 +0800 Subject: [PATCH] fix(text): match phrases across repeated whitespace (#814) (#831) 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 --- crates/render/src/text.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/render/src/text.rs b/crates/render/src/text.rs index 5db3fa47cd..34c3d94d64 100644 --- a/crates/render/src/text.rs +++ b/crates/render/src/text.rs @@ -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, s: &str, x: f32, y: f32, advance: f32) { for (i, c) in s.chars().enumerate() { let x0 = x + i as f32 * advance;