diff --git a/crates/data/src/display_marks.rs b/crates/data/src/display_marks.rs index 698768edc5..0680149c6b 100644 --- a/crates/data/src/display_marks.rs +++ b/crates/data/src/display_marks.rs @@ -161,6 +161,12 @@ fn letter(c: char) -> bool { c.is_alphabetic() && !rtl_script(c) } +/// A character the toolkit's direction guess reads as right to left: a letter of a right-to-left +/// script, or a Persian or Arabic digit. +fn rtl_strong(c: char) -> bool { + rtl_script(c) || matches!(c as u32, 0x0660..=0x0669 | 0x06F0..=0x06F9) +} + /// What the toolkit's direction guess makes of a character: `Some(true)` — of a /// right-to-left script, the line start that is laid out back to front (the digits /// of the Arabic block included, for their script is Arabic); `Some(false)` — of a @@ -168,7 +174,7 @@ fn letter(c: char) -> bool { /// its own (digits, punctuation, marks, controls), which the guess skips. #[must_use] pub fn strong(c: char) -> Option { - if rtl_script(c) || matches!(c as u32, 0x0660..=0x0669 | 0x06F0..=0x06F9) { + if rtl_strong(c) { Some(true) } else if c.is_alphabetic() { Some(false) @@ -185,9 +191,17 @@ fn closing(c: char) -> bool { matches!(c, ')' | ']' | '}' | '»' | '›') } -/// Whether any right-to-left text is in `s`: a cheap check before any work. +/// Whether any text the toolkit lays out right to left is in `s`: a letter of a right-to-left +/// script, or a Persian or Arabic digit (see [`strong`]). A cheap check before any work. #[must_use] pub fn has_rtl(s: &str) -> bool { + s.chars().any(rtl_strong) +} + +/// Whether `s` holds a letter of a right-to-left script: text that reads backwards in a line laid +/// out front to back. Persian and Arabic digits don't count: they read in logical order there. +#[must_use] +pub fn has_rtl_letter(s: &str) -> bool { s.chars().any(rtl_script) } @@ -217,7 +231,7 @@ pub fn visual(s: &str) -> String { /// One line of [`visual`]. Display forms put into it (a translated part of a message) are put /// back in logical order first, so the line is shaped as a whole. A line that starts /// left-to-right, or still holds a mark (its data's), comes back as it is. A line of Persian or -/// Arabic digits alone is back to front too, though [`has_rtl`] finds no letter in it. +/// Arabic digits alone is back to front too. #[must_use] pub fn visual_line(line: &str) -> String { let logical = unmark_line(line, true); diff --git a/crates/ui-egui/src/i18n/boundary_tests.rs b/crates/ui-egui/src/i18n/boundary_tests.rs index 25a11b64b6..c4a24cb89f 100644 --- a/crates/ui-egui/src/i18n/boundary_tests.rs +++ b/crates/ui-egui/src/i18n/boundary_tests.rs @@ -296,6 +296,12 @@ fn data_identical_to_a_display_form_is_read_as_one() { assert_eq!(logical(&rtl::visual(text)).as_deref(), Some(text)); } +/// A message of digits alone is rewritten for display, and text leaving the screen puts it back. +#[test] +fn a_message_of_digits_alone_comes_back_in_logical_order() { + assert_eq!(logical(&rtl::visual("۱۲۳")).as_deref(), Some("۱۲۳")); +} + /// A message put around a display form (a `tl!` or `fmt` result as an argument) is shaped as /// one made of the logical parts; beside data with marks, the line stays as it was made. #[test] diff --git a/crates/ui-egui/src/i18n/rtl.rs b/crates/ui-egui/src/i18n/rtl.rs index c808e132c7..4bc940ff89 100644 --- a/crates/ui-egui/src/i18n/rtl.rs +++ b/crates/ui-egui/src/i18n/rtl.rs @@ -34,7 +34,7 @@ //! the screen can be put back in logical order. use rustgis_engine::data::display_marks::{back_to_front, strong, visual_line}; -pub use rustgis_engine::data::display_marks::{has_rtl, visual}; +pub use rustgis_engine::data::display_marks::{has_rtl, has_rtl_letter, visual}; /// `s` broken into lines whose display form is no wider than `max_width`, so the /// toolkit's own wrapping never has to: it fills rows from the start of a @@ -76,7 +76,7 @@ fn fit_line(line: &str, max_width: f32, width: &dyn Fn(&str) -> f32, rows: &mut // back, and the right-to-left words in it would read backwards: the row // ends before the first word that holds right-to-left text. if words[start..end].iter().find_map(|w| w.chars().find_map(strong)) == Some(false) - && let Some(cut) = (start + 1..end).find(|&i| has_rtl(words[i])) + && let Some(cut) = (start + 1..end).find(|&i| has_rtl_letter(words[i])) { end = cut; } @@ -181,6 +181,27 @@ mod tests { assert_eq!(fit("hello world", 2.0, &chars), "hello world"); } + #[test] + fn a_message_of_digits_alone_is_put_in_display_form() { + let chars = |s: &str| s.chars().count() as f32; + // The toolkit lays out a line that starts with a Persian or Arabic digit back to front, + // so a message of those digits alone is rewritten the way one that holds a letter is. + assert_eq!(visual("۱۲۳"), "۳۲۱"); + assert_eq!(visual("١٢٣"), "٣٢١"); + assert_eq!(visual("۱۲۳\nabc"), "۳۲۱\nabc"); + assert_ne!(super::visual("۱۲۳"), "۱۲۳", "the form is marked, so it can be put back"); + assert_eq!(fit("۱۲۳ ۴۵۶", 30.0, &chars), "۶۵۴ ۳۲۱"); + // One digit reads the same either way; ASCII digits and left-to-right lines stay. + assert_eq!(visual("۱"), "۱"); + assert_eq!(visual("123"), "123"); + assert_eq!(visual("abc ۱۲۳"), "abc ۱۲۳"); + assert_eq!(visual(""), ""); + // A line that holds a mark of its data's own stays as it is. + assert_eq!(super::visual("\u{2063}۱۲۳"), "\u{2063}۱۲۳"); + assert!(super::has_rtl("۱۲۳") && super::has_rtl("١٢٣")); + assert!(!super::has_rtl("123") && !super::has_rtl("abc")); + } + #[test] fn a_row_of_digits_alone_is_put_in_display_form() { let chars = |s: &str| s.chars().count() as f32; @@ -189,6 +210,14 @@ mod tests { assert_eq!(fit("سلام ١٢٣", 4.0, &chars), "سلام\n٣٢١"); } + #[test] + fn a_latin_row_keeps_the_digits_that_fit_on_it() { + let chars = |s: &str| s.chars().count() as f32; + // Digits in a row laid out front to back read in logical order, so only a right-to-left + // letter ends such a row: the year stays on the row of "Natural Earth". + assert_eq!(fit("سلامسلامسلام Natural Earth ۲۰۲۴", 18.0, &chars), "سلامسلامسلام\nNatural Earth ۲۰۲۴"); + } + #[test] fn a_row_that_starts_with_a_latin_word_holds_no_rtl_words() { let chars = |s: &str| s.chars().count() as f32;