diff --git a/crates/edit/src/added/layout.rs b/crates/edit/src/added/layout.rs index 0520ada0ec..62f24c5c4c 100644 --- a/crates/edit/src/added/layout.rs +++ b/crates/edit/src/added/layout.rs @@ -102,13 +102,14 @@ fn thai_lines(t: &AddedText, para: &str, width: f64) -> Result, Edit for word in words { // The word's own width: the spaces after it may hang past the row's end. let ink: f64 = word.iter().filter(|(s, _)| !blank(s)).map(|(_, w)| w).sum(); - if !line.is_empty() && used + ink > width { + // A row holding only the spaces that open the paragraph has nothing to end yet. + if !blank(&line) && used + ink > width { end_row(&mut lines, &mut line); used = 0.0; } - let too_wide = line.is_empty() && ink > width; + let too_wide = blank(&line) && ink > width; for (text, advance) in word { - if too_wide && !line.is_empty() && !blank(&text) && used + advance > width { + if too_wide && !blank(&line) && !blank(&text) && used + advance > width { end_row(&mut lines, &mut line); used = 0.0; } diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs index ff91fef668..d8ba0a8aeb 100644 --- a/crates/edit/src/tests.rs +++ b/crates/edit/src/tests.rs @@ -5815,8 +5815,17 @@ fn added_thai_text_wraps_between_dictionary_words() { let rows = super::added::lines(&spaced); assert!(rows.iter().all(|r| !r.starts_with(' ') && !r.ends_with(' ')), "{rows:?}"); assert_eq!(rows.concat(), "น้ำประปาสะอาดปลอดภัย"); - let narrow = AddedText { rect: [0.0, 0.0, 16.0, 200.0], text: "กิ่กิ่กิ่".into(), ..t }; + let narrow = AddedText { rect: [0.0, 0.0, 16.0, 200.0], text: "กิ่กิ่กิ่".into(), ..t.clone() }; assert_eq!(super::added::lines(&narrow), ["กิ่", "กิ่", "กิ่"]); + // Spaces that open a paragraph are kept, but they are not a row of their own: a first word + // too wide to fit after them stays on the first row (or breaks between clusters there). + for width in [16.0, 40.0, 60.0, 90.0] { + let indented = AddedText { rect: [0.0, 0.0, width, 400.0], text: " ประปาสะอาด".into(), ..t.clone() }; + let rows = super::added::lines(&indented); + assert!(rows.iter().all(|r| !r.trim().is_empty()), "an empty row at width {width}: {rows:?}"); + assert!(rows[0].starts_with(" "), "the opening spaces are kept at width {width}: {rows:?}"); + assert_eq!(rows.concat().trim_start(), "ประปาสะอาด"); + } } #[test]