DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge #316: GREP: the dialect's classes and metacharacters (\l \u \h, ~ codes, lookaround) in GREP styles and Find/Change
aionggd86bbd8d6c38
diff --cc crates/compose/src/lib.rsindex e6af50ef3,59fc0aba3..1bb877504--- a/crates/compose/src/lib.rs+++ b/crates/compose/src/lib.rs@@@ -13,8 -13,8 +13,9 @@@mod bidi;pub mod breaker;mod cache;+ pub mod grep;pub mod hyphen;+mod mojikumi;mod notes;mod overlay;mod ruby;diff --cc crates/compose/src/overlay.rsindex f34ed2925,9b1fd453a..96341d4b2--- a/crates/compose/src/overlay.rs+++ b/crates/compose/src/overlay.rs@@@ -97,16 -94,19 +103,25 @@@ mod testsassert_eq!(&text[o[0].0.clone()], "Q:");assert_eq!(&text[o[1].0.clone()], " what is", "up to the end of the second word");// Digits by GREP.- let o = overlays(&text, 0..text.len(), &[], &[GrepStyle { style: "Num".into(), pattern: r"\d+".into() }]);+ let o = overlays(&text, 0..text.len(), &[], &[GrepStyle { style: "Num".into(), pattern: r"\d+".into() }], 0);let got: Vec<&str> = o.iter().map(|(r, _)| &text[r.clone()]).collect();assert_eq!(got, ["42", "7"]);+ // Through the drop cap (its first 2 bytes here); up to it is nothing; without one, nothing.+ let d = |through: bool| n("Cap", through, 1, NestedUntil::Dropcap);+ let o = overlays(&text, 0..text.len(), &[d(true), n("Next", true, 1, NestedUntil::Words)], &[], 2);+ assert_eq!((&text[o[0].0.clone()], &text[o[1].0.clone()]), ("Q:", " what "));+ assert_eq!(&text[overlays(&text, 0..text.len(), &[d(false), n("Next", true, 1, NestedUntil::Words)], &[], 2)[0].0.clone()], "Q: ");+ assert!(overlays(&text, 0..text.len(), &[d(true)], &[], 0).is_empty());// A bad pattern is ignored.- assert!(overlays(&text, 0..text.len(), &[], &[GrepStyle { style: "X".into(), pattern: "(".into() }]).is_empty());+ assert!(overlays(&text, 0..text.len(), &[], &[GrepStyle { style: "X".into(), pattern: "(".into() }], 0).is_empty());}++ #[test]+ fn grep_style_in_the_documents_dialect() {+ // Short Russian words kept with the next one: `\l` lowercase letter, `\h` horizontal space.+ let text = "Мы и он в доме.\n".to_string();- let o = overlays(&text, 0..text.len(), &[], &[GrepStyle { style: "nobreak".into(), pattern: r"\b\l{1,2}\h".into() }]);++ let o = overlays(&text, 0..text.len(), &[], &[GrepStyle { style: "nobreak".into(), pattern: r"\b\l{1,2}\h".into() }], 0);+ let got: Vec<&str> = o.iter().map(|(r, _)| &text[r.clone()]).collect();+ assert_eq!(got, ["и ", "он ", "в "]);+ }}diff --cc crates/compose/src/tests.rsindex 208d462fe,ba30d1e37..1c7fd67c8--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@@ -4599,72 -2772,30 +4599,100 @@@ fn empty_paragraphs_get_no_bullet_or_nu}}+#[test]+fn tight_word_spacing_does_not_isolate_a_word() {+ // A narrow word-spacing range (100–105%) with some letter spacing and glyph scaling, no+ // hyphenation: lines that need more stretch must open their word spaces, not letter-space a+ // single word across the measure.+ let para = ParaAttrs {+ align: Some(Align::LeftJustified),+ hyphenate: Some(false),+ first_line_indent: Some(9.0),+ word_space_min: Some(1.0),+ word_space_desired: Some(1.03),+ word_space_max: Some(1.05),+ letter_space_min: Some(-0.05),+ letter_space_max: Some(0.05),+ glyph_scale_min: Some(0.99),+ glyph_scale_max: Some(1.01),+ ..Default::default()+ };+ // Short words: any two fit on a line together.+ let text = CORPUS[..2].join(" ");+ for w in (200..=320).step_by(5) {+ let (d, sid, _) = doc_with(&text, Rect::new(0.0, 0.0, w as f64, 20000.0), para.clone());+ let cs = compose_story(&d, sid, &ComposeOptions::default());+ let lines = all_lines(&cs);+ for l in &lines[..lines.len() - 1] {+ let words = text[l.range.clone()].split_whitespace().count();+ assert!(words > 1, "width {w}: a single-word line {:?}", &text[l.range.clone()]);+ }+ }+}++#[test]+fn fixed_width_spaces_do_not_stretch_on_justified_lines() {+ // Long words in a narrow column: lines stretch past the maximum word spacing. Only the word+ // spaces take that stretch; em, en and thin spaces keep their width.+ let words = ["characteristically", "misunderstanding", "unquestionably", "responsibility", "administration", "photographer"];+ let seps = [" ", "\u{2003}", " ", "\u{2002}", " ", "\u{2009}"];+ let text: String = (0..60).map(|i| format!("{}{}", words[i % words.len()], seps[i % seps.len()])).collect();+ let text = text.trim_end().to_string();+ let fixed = |c: char| matches!(c, '\u{2003}' | '\u{2002}' | '\u{2009}');+ // (character, advance) of each fixed space, per line, and whether the line ends the paragraph.+ let spaces = |align: Align, w: f64| -> Vec<(Vec<(char, f64)>, bool)> {+ let para = ParaAttrs { align: Some(align), hyphenate: Some(false), ..Default::default() };+ let (d, sid, _) = doc_with(&text, Rect::new(0.0, 0.0, w, 20000.0), para);+ let cs = compose_story(&d, sid, &ComposeOptions::default());+ all_lines(&cs)+ .iter()+ .map(|l| {+ let found = l.glyphs.iter().filter_map(|g| text[g.byte..].chars().next().filter(|&c| fixed(c)).map(|c| (c, g.adv))).collect();+ (found, l.last_in_para)+ })+ .collect()+ };+ let mut checked = 0;+ for w in [190.0, 230.0, 270.0] {+ let natural: Vec<(char, f64)> = spaces(Align::Left, w).into_iter().flat_map(|l| l.0).collect();+ for (found, last) in spaces(Align::LeftJustified, w) {+ if last {+ continue;+ }+ for (c, a) in found {+ let Some(&(_, n)) = natural.iter().find(|x| x.0 == c) else { continue };+ assert!((a - n).abs() < 1e-6, "width {w}: U+{:04X} is {a:.2} wide, not {n:.2}", c as u32);+ checked += 1;+ }+ }+ }+ assert!(checked > 5, "{checked} fixed spaces on justified lines");+}+++ #[test]+ fn grep_style_in_the_documents_dialect_keeps_short_words_with_the_next() {+ // `\l` (lowercase letter) and `\h` (horizontal space) aren't Rust regex syntax; the style used+ // to be skipped silently.+ let text = "Мы и он в доме и в саду, а она у окна и с книгой в руках у стола.";+ let para = ParaAttrs {+ align: Some(Align::Left),+ grep_styles: Some(vec![designcraft_doc::GrepStyle { style: "nobreak".into(), pattern: r"\b\l{1,2}\h".into() }]),+ ..Default::default()+ };+ let (mut d, sid, _) = doc_with(text, Rect::new(0.0, 0.0, 70.0, 1000.0), para);+ let chars = designcraft_doc::CharAttrs { no_break: Some(true), ..Default::default() };+ std::sync::Arc::make_mut(&mut d.styles).character.push(designcraft_doc::CharacterStyle {+ name: "nobreak".into(),+ based_on: None,+ chars,+ shortcut: String::new(),+ });+ let cs = compose_story(&d, sid, &ComposeOptions::default());+ let lines = all_lines(&cs);+ assert!(lines.len() > 3);+ let short = |w: &str| w.chars().count() <= 2 && w.chars().all(char::is_lowercase);+ for l in &lines[..lines.len() - 1] {+ let last = text[l.range.clone()].split_whitespace().last().unwrap_or("");+ assert!(!short(last), "line ends with a short word: {:?}", &text[l.range.clone()]);+ }+ }