From d327a20efd9c2f3d740c8d06ad44704012f0e22a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 06:54:48 -0800 Subject: [PATCH] Locale: raster resolution and Export for Screens sizes read Unicode digits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit document.rasterEffectsSettings' text resolution ("300 ppi") and ScreenSize::parse ("2x", "100w", "144ppi") read their numbers with str::parse and an ASCII-only check, so "३०० ppi" and "२x" were refused while number fields read them. Both now convert digits with vectorcraft_doc::ascii_digits, now public, before their own grammar and range checks; one number mixing digit sets is still refused. --- crates/doc/src/lib.rs | 2 +- crates/doc/src/units.rs | 2 +- crates/engine/src/cmd/fileio/screens.rs | 5 +++-- crates/engine/src/cmd/fileio/tests_screens.rs | 11 +++++++++++ crates/engine/src/cmd/rasterfx.rs | 5 ++++- crates/engine/src/tests/rastersettings.rs | 13 +++++++++++++ 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index 7bddc2c482..7772056668 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -179,7 +179,7 @@ use limits::MAX_NEXT_ID; pub use limits::{DocError, FORMAT_VERSION, MAX_COORD}; pub use records::{ImageBlob, MaskEdit, NewArtIds, SavedSelection, SavedView, SvgFilter, Symbol, SymbolEdit}; pub use stored_art::{StoredPlace, StoredRoot}; -pub use units::{ColorMode, Unit, arithmetic_signs, decimal_point, finite_in_every_unit, parse_number}; +pub use units::{ColorMode, Unit, arithmetic_signs, ascii_digits, decimal_point, finite_in_every_unit, parse_number}; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Document { diff --git a/crates/doc/src/units.rs b/crates/doc/src/units.rs index c114ceb75f..cc4ab253de 100644 --- a/crates/doc/src/units.rs +++ b/crates/doc/src/units.rs @@ -432,7 +432,7 @@ pub fn decimal_point(text: &str) -> Cow<'_, str> { /// Arabic-Indic, full-width, …) written as its ASCII digit, so `१२.५ mm` reads as `12.5 mm`. A /// number here is a run of digits, `.` and `,`, and must use one digit set: `1२` and `૧२` are /// `None`. Operators, unit suffixes and other characters are left as they are. -fn ascii_digits(text: &str) -> Option> { +pub fn ascii_digits(text: &str) -> Option> { craft_i18n::ascii_digits(text, |c| matches!(c, '.' | ',')).ok() } diff --git a/crates/engine/src/cmd/fileio/screens.rs b/crates/engine/src/cmd/fileio/screens.rs index 109dd1c59e..8e499f289d 100644 --- a/crates/engine/src/cmd/fileio/screens.rs +++ b/crates/engine/src/cmd/fileio/screens.rs @@ -46,9 +46,10 @@ impl ScreenSize { /// `2x` (or `2`), `100w`, `100h` or `72ppi`, in any case; positive numbers only. pub fn parse(s: &str) -> Option { let s = s.trim().to_ascii_lowercase(); - // Digits and `.` only: `str::parse` would also take `+2`, `1e2`, `inf` and `nan`. + // Digits and `.` only: `str::parse` would also take `+2`, `1e2`, `inf` and `nan`. Digits of + // other scripts read as ASCII digits do (`"२x"` is `2x`), as number fields read them. let num = |t: &str| { - let t = t.trim(); + let t = vectorcraft_doc::ascii_digits(t.trim())?; if !t.bytes().all(|b| b.is_ascii_digit() || b == b'.') { return None; } diff --git a/crates/engine/src/cmd/fileio/tests_screens.rs b/crates/engine/src/cmd/fileio/tests_screens.rs index 93c4d7e6b1..491fca372e 100644 --- a/crates/engine/src/cmd/fileio/tests_screens.rs +++ b/crates/engine/src/cmd/fileio/tests_screens.rs @@ -72,6 +72,17 @@ fn sizes_parse_as_factor_width_height_or_resolution() { ); } +#[test] +fn sizes_read_digits_of_other_scripts() { + assert_eq!(ScreenSize::parse("२x"), Some(ScreenSize::Scale(2.0))); + assert_eq!(ScreenSize::parse("૧૦૦W"), Some(ScreenSize::Width(100.0))); + assert_eq!(ScreenSize::parse("१४४ppi"), Some(ScreenSize::Ppi(144.0))); + // One digit set per number, and the same grammar as ASCII digits. + for bad in ["1२x", "१.5x", "-२x", "०w"] { + assert_eq!(ScreenSize::parse(bad), None, "{bad}"); + } +} + #[test] fn a_width_or_height_row_gives_that_many_pixels() { let mut s = session(1); diff --git a/crates/engine/src/cmd/rasterfx.rs b/crates/engine/src/cmd/rasterfx.rs index 97dd890457..b567c0f3a0 100644 --- a/crates/engine/src/cmd/rasterfx.rs +++ b/crates/engine/src/cmd/rasterfx.rs @@ -71,7 +71,10 @@ fn resolution(v: &Value) -> Result { "screen" => 72.0, "medium" => 150.0, "high" => 300.0, - other => other.trim_end_matches("ppi").trim().parse::().map_err(|_| bad(C, format!("unknown resolution `{v}`")))?, + // Digits of other scripts read as ASCII digits do (`"३००"` is 300), as number fields read them. + other => vectorcraft_doc::ascii_digits(other.trim_end_matches("ppi").trim()) + .and_then(|n| n.parse::().ok()) + .ok_or_else(|| bad(C, format!("unknown resolution `{v}`")))?, }, v => v.as_f64().ok_or_else(|| bad(C, "resolution must be a number or screen|medium|high"))?, }; diff --git a/crates/engine/src/tests/rastersettings.rs b/crates/engine/src/tests/rastersettings.rs index 6e3ef16476..fd88793cc9 100644 --- a/crates/engine/src/tests/rastersettings.rs +++ b/crates/engine/src/tests/rastersettings.rs @@ -91,6 +91,19 @@ fn settings_report_change_in_one_step_and_reject_junk() { assert_eq!(s.execute(C, &json!({"colorModel": "cmyk"})).unwrap()["colorModel"], "cmyk"); } +#[test] +fn a_resolution_given_as_text_reads_digits_of_other_scripts() { + let mut s = session(); + let r = s.execute(C, &json!({"resolution": "३०० ppi"})).unwrap(); + assert_eq!(r["resolution"].as_f64(), Some(300.0)); + assert_eq!(s.doc().unwrap().doc.raster_effects_ppi, 300.0); + // One digit set per number, and the same 1 to 2400 ppi range as ASCII digits. + for bad in ["1५0", "५०००"] { + assert!(s.execute(C, &json!({"resolution": bad})).is_err(), "{bad}"); + } + assert_eq!(s.doc().unwrap().history.undo.len(), 1); +} + #[test] fn a_white_background_makes_effect_images_opaque() { let mut s = session();