Files
gridcraft/crates/ui-egui/src/text_direction.rs
T
e9addf52fe Arabic bidi layout and cell reading direction in the grid and editors (#947)
* M12.AR4: preserve Arabic logical text in bidi display and editing

* M12.AR4: drop the stale "no bidi" note from the RTL sheet status

* M12.AR4: open the Arabic editors and place the caret through key and pointer events in tests

* M12.AR4: skip the conditional-format lookup in the formula bar unless a rule sets reading order

* M12.AR4: test that the caret after typed Arabic is drawn at the text's left end (#1070)

* M12.AR4: pass the theme body font in the reading-order style test

* M12.AR4: adapt the Arabic UI tests to egui 0.37 (row direction, editor ids, Ctrl+End to the logical end)

* M12.AR4: read the active sheet through Workbook::active_sheet() in the formula bar

main made `Workbook::active_sheet` an `ActiveSheet` (shared between windows on one
workbook); the formula bar's reading-order lookup takes the index from the accessor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* M12.AR4: keep explicit Left on wrapped right-to-left cells and trailing spaces in the editor

Wrapped cell text lays out every row from the left when the painted alignment is Left, instead of aligning right-to-left rows to their start. The cell and formula bar editor keep trailing whitespace in their layout so a right-to-left entry ending in a space keeps the caret inside the text.

* M12.AR4: keep Right and General on right-to-left sheets aligned right in wrapped cells

* M12.AR4: keep right-aligned Arabic with a marked first letter inside its cell

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
Co-authored-by: FredApps <FredApps@users.noreply.github.com>
2026-11-09 15:55:43 +01:00

33 lines
1.4 KiB
Rust

//! Cell reading direction follows OOXML context rules, independently of sheet geometry.
use egui::epaint::text::TextDirection;
use gridcraft_engine::model::ReadingOrder;
pub(crate) fn cell_direction(order: Option<ReadingOrder>, text: &str) -> TextDirection {
match order.unwrap_or_default() {
ReadingOrder::LeftToRight => TextDirection::LeftToRight,
ReadingOrder::RightToLeft => TextDirection::RightToLeft,
ReadingOrder::Context => match text.chars().find(|ch| !ch.is_whitespace()).map(unicode_bidi::bidi_class) {
Some(unicode_bidi::BidiClass::R | unicode_bidi::BidiClass::AL) => TextDirection::RightToLeft,
_ => TextDirection::LeftToRight,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn context_uses_the_first_non_whitespace_character() {
for text in ["مرحبا 123", " עברית", "\n\tالعربية"] {
assert_eq!(cell_direction(None, text), TextDirection::RightToLeft);
}
for text in ["123 مرحبا", "(مرحبا)", "ABC العربية", "", " "] {
assert_eq!(cell_direction(None, text), TextDirection::LeftToRight);
}
assert_eq!(cell_direction(Some(ReadingOrder::LeftToRight), "مرحبا"), TextDirection::LeftToRight);
assert_eq!(cell_direction(Some(ReadingOrder::RightToLeft), "123 ABC"), TextDirection::RightToLeft);
}
}