6ef13b2cc8a6
numeral.rs
//! Numeral display modes (Word › File › Options › Advanced › Numeral): Western digits,
//! Hindi digits, or context-following digits — display only, the text is untouched.
use crate::*;
use wordcraft_doc::props::ParaProps;
use wordcraft_doc::{Pos, StoryRef};
fn lay_opts(text: &str, rtl_para: bool, numeral: NumeralMode) -> DocLayout {
let mut d = Document::from_text(text);
if rtl_para {
let at = Pos::body(0, 0);
d.format_paragraphs(&at, &at, &|p: &mut ParaProps| p.bidi = Some(true)).unwrap();
}
let mut c = LayoutCache::new();
layout(&d, &mut c, &LayoutOptions { numeral, ..LayoutOptions::default() })
}
/// Glyph ids of the first line's clusters covering `text`, in cluster order.
fn line_gids(l: &DocLayout, text: &str) -> Vec<Vec<u32>> {
for p in &l.pages {
for it in &p.items {
if let Placed::Lines { story: StoryRef::Body, para, .. } = it {
let line = ¶.lines[0];
let mut out = Vec::new();
for k in line.c0..line.c1 {
let cl = ¶.clusters[k];
assert!(cl.start <= cl.end && cl.end <= text.len(), "cluster indexes the original text");
let gids: Vec<u32> = para.glyphs[cl.g0 as usize..cl.g1 as usize].iter().map(|g| g.gid).collect();
out.push(gids);
}
return out;
}
}
}
panic!("body paragraph not laid out");
}
/// The face of each cluster on the first line, as [`line_gids`] walks them.
fn line_faces(l: &DocLayout) -> Vec<u32> {
for p in &l.pages {
for it in &p.items {
if let Placed::Lines { story: StoryRef::Body, para, .. } = it {
let line = ¶.lines[0];
return para.clusters[line.c0..line.c1].iter().map(|cl| para.styles[cl.style as usize].face.id()).collect();
}
}
}
panic!("body paragraph not laid out");
}
/// [`line_gids`] of `text` laid out as `a` and then as `b` (right-to-left paragraph, numeral
/// mode). The tests running alongside share the font database: one loading a font between the
/// two layouts can change the face a character falls back to (U+06DD in Segoe UI, then in Noto
/// Sans Arabic), so the pair is made again until both draw with the same faces, as two layouts
/// with the same fonts loaded do.
fn gids_pair(text: &str, a: (bool, NumeralMode), b: (bool, NumeralMode)) -> (Vec<Vec<u32>>, Vec<Vec<u32>>) {
for _ in 0..5 {
let (la, lb) = (lay_opts(text, a.0, a.1), lay_opts(text, b.0, b.1));
if line_faces(&la) == line_faces(&lb) {
return (line_gids(&la, text), line_gids(&lb, text));
}
}
panic!("{text:?} draws with other faces in {b:?} than in {a:?}");
}
#[test]
fn parse_accepts_the_four_modes_any_case() {
crate::test_fonts();
assert_eq!(NumeralMode::parse("arabic"), Some(NumeralMode::Arabic));
assert_eq!(NumeralMode::parse("hindi"), Some(NumeralMode::Hindi));
assert_eq!(NumeralMode::parse("context"), Some(NumeralMode::Context));
assert_eq!(NumeralMode::parse("system"), Some(NumeralMode::System));
assert_eq!(NumeralMode::parse("Hindi"), Some(NumeralMode::Hindi));
assert_eq!(NumeralMode::parse("CONTEXT"), Some(NumeralMode::Context));
assert_eq!(NumeralMode::parse("western"), None);
assert_eq!(NumeralMode::parse(""), None);
assert_eq!(NumeralMode::parse("hindi "), None);
}
#[test]
fn map_substitutes_ascii_digits_only() {
crate::test_fonts();
for d in '0'..='9' {
let hindi = char::from_u32(0x660 + (d as u32 - '0' as u32)).unwrap();
assert_eq!(NumeralMode::Hindi.map(d, false), hindi);
assert_eq!(NumeralMode::Hindi.map(d, true), hindi, "Hindi mode ignores direction");
assert_eq!(NumeralMode::Arabic.map(hindi, false), hindi);
assert_eq!(NumeralMode::Arabic.map(hindi, true), hindi, "Arabic mode keeps typed Hindi digits");
assert_eq!(NumeralMode::Arabic.map(d, false), d);
assert_eq!(NumeralMode::Context.map(d, true), hindi);
assert_eq!(NumeralMode::Context.map(d, false), d);
assert_eq!(NumeralMode::Context.map(hindi, true), hindi);
assert_eq!(NumeralMode::Context.map(hindi, false), hindi);
}
// Letters, punctuation and Persian digits are never touched.
for c in ['A', 'ب', '.', ' ', '(', '۰', '۹'] {
for m in [NumeralMode::Arabic, NumeralMode::Hindi, NumeralMode::Context] {
assert_eq!(m.map(c, false), c, "{m:?} leaves {c:?} alone (ltr)");
assert_eq!(m.map(c, true), c, "{m:?} leaves {c:?} alone (rtl)");
}
}
}
#[test]
fn resolve_uses_the_given_system_locale() {
crate::test_fonts();
for m in [NumeralMode::Arabic, NumeralMode::Hindi, NumeralMode::Context] {
assert_eq!(m.resolve(Some("ar-EG")), m, "only System is resolved");
}
for tag in ["ar", "ar-EG", "fa_IR.UTF-8", "ur-PK", "ckb-IQ", "AR-sa"] {
assert_eq!(NumeralMode::System.resolve(Some(tag)), NumeralMode::Hindi, "{tag}");
}
for tag in ["en-US", "C", "", "tr_TR", "ary"] {
assert_eq!(NumeralMode::System.resolve(Some(tag)), NumeralMode::Arabic, "{tag}");
}
assert_eq!(NumeralMode::System.resolve(None), NumeralMode::Arabic, "unknown locale: Western digits");
}
#[test]
#[cfg_attr(coverage, ignore = "coverage instrumentation skews the measured time")]
fn context_mode_on_a_very_long_digit_run_stays_linear() {
crate::test_fonts();
// Every digit used to scan back over all the digits and spaces before it (O(n²)).
let text = format!("سنة {}", "1 ".repeat(100_000));
let start = std::time::Instant::now();
let l = lay_opts(&text, false, NumeralMode::Context);
assert!(!l.pages.is_empty());
assert!(start.elapsed() < std::time::Duration::from_secs(60), "took {:?}", start.elapsed());
// The digits follow the Arabic word before them.
let short = "سنة 1 2 3";
let (ctx, hin) = gids_pair(short, (false, NumeralMode::Context), (false, NumeralMode::Hindi));
assert_eq!(ctx, hin);
}
#[test]
fn hindi_mode_reshapes_digits_but_keeps_offsets() {
crate::test_fonts();
let text = "Year 2024!";
let latin = lay_opts(text, false, NumeralMode::Arabic);
let hindi = lay_opts(text, false, NumeralMode::Hindi);
let a = line_gids(&latin, text);
let h = line_gids(&hindi, text);
assert_eq!(a.len(), h.len(), "same clusters in both modes");
assert!(!a.is_empty());
// Letter clusters draw identical glyphs; digit clusters draw different ones.
for (n, (ga, gh)) in a.iter().zip(h.iter()).enumerate() {
let ch = text.chars().nth(n).unwrap();
if ch.is_ascii_digit() {
assert_ne!(ga, gh, "digit {ch:?} reshaped in Hindi mode");
assert!(!gh.is_empty(), "Hindi digit draws something");
} else {
assert_eq!(ga, gh, "{ch:?} untouched by Hindi mode");
}
}
}
#[test]
fn arabic_mode_keeps_typed_hindi_digits() {
crate::test_fonts();
let text = "عام ٢٠٢٤";
let (hindi, arabic) = gids_pair(text, (true, NumeralMode::Hindi), (true, NumeralMode::Arabic));
assert_eq!(hindi, arabic, "typed Hindi digits display as typed in Arabic mode");
}
#[test]
fn context_mode_follows_run_direction() {
crate::test_fonts();
// Right-to-left paragraph: digits behave like Hindi mode.
let rtl = "سنة 2024";
let (ctx, hin) = gids_pair(rtl, (true, NumeralMode::Context), (true, NumeralMode::Hindi));
assert_eq!(ctx, hin, "digits in RTL text show Hindi in Context mode");
// Left-to-right paragraph: digits behave like Arabic mode.
let ltr = "Year 2024";
let (ctx, ara) = gids_pair(ltr, (false, NumeralMode::Context), (false, NumeralMode::Arabic));
assert_eq!(ctx, ara, "digits in LTR text show Western in Context mode");
// Mixed paragraph: digits next to Arabic show Hindi even in a left-to-right paragraph.
let mixed = "Year سنة 2024";
let (ctx, hin) = gids_pair(mixed, (false, NumeralMode::Context), (false, NumeralMode::Hindi));
assert_eq!(ctx, hin, "digits by Arabic text show Hindi in Context mode");
}
#[test]
fn remap_range_maps_display_bytes_to_original() {
crate::test_fonts();
use crate::para::remap_range;
// "a5ب": '5' (1 byte) displays as '٥' (2 bytes), so display offsets shift after it.
let orig = "a5ب";
let disp = "a٥ب";
let map_starts: Vec<usize> = disp.char_indices().map(|(i, _)| i).collect();
let orig_of: Vec<usize> = orig.char_indices().map(|(i, _)| i).collect();
let mut orig_of = orig_of;
orig_of.push(orig.len());
// Display "a" [0,1) → original [0,1); "٥" [1,3) → original [1,2); "ب" [3,5) → [2,4).
assert_eq!(remap_range(&map_starts, &orig_of, orig.len(), 0, 1), (0, 1));
assert_eq!(remap_range(&map_starts, &orig_of, orig.len(), 1, 3), (1, 2));
assert_eq!(remap_range(&map_starts, &orig_of, orig.len(), 3, 5), (2, 4));
assert_eq!(remap_range(&map_starts, &orig_of, orig.len(), 0, 5), (0, 4));
// Hostile inputs never panic and stay in range.
assert_eq!(remap_range(&[], &[], 0, 0, 0), (0, 0));
assert_eq!(remap_range(&map_starts, &orig_of, orig.len(), 99, 200), (4, 4));
assert_eq!(remap_range(&map_starts, &orig_of, orig.len(), 3, 1), (1, 2));
}
#[test]
fn arabic_mode_keeps_arabic_indic_digits_as_typed() {
// Issue #3259: the end-of-ayah mark (U+06DD) ligates with the real Arabic-Indic digits that
// follow it, so those codepoints must reach the shaper unchanged in the default mode.
for n in 0..10u32 {
let hindi = char::from_u32(0x660 + n).unwrap();
let ascii = char::from_u32('0' as u32 + n).unwrap();
for m in [NumeralMode::default(), NumeralMode::Arabic, NumeralMode::System] {
assert_eq!(m.map(hindi, false), hindi, "{m:?} keeps {hindi:?} (ltr)");
assert_eq!(m.map(hindi, true), hindi, "{m:?} keeps {hindi:?} (rtl)");
}
let typed = format!("\u{6dd}{hindi}");
let western = format!("\u{6dd}{ascii}");
let (t, h) = gids_pair(&typed, (true, NumeralMode::default()), (true, NumeralMode::Hindi));
let w = line_gids(&lay_opts(&western, true, NumeralMode::default()), &western);
assert_ne!(t, w, "U+{:04X} is not shaped as ASCII {ascii:?} in the default mode", 0x660 + n);
assert_eq!(t, h, "U+{:04X} shapes the same as in Hindi mode", 0x660 + n);
}
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.