mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 05:53:08 -05:00
Locale: raster resolution and Export for Screens sizes read Unicode digits
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.
This commit is contained in:
1 parent
116049ca65
commit
d327a20efd
6 files changed
+33
-5
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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<Cow<'_, str>> {
|
||||
pub fn ascii_digits(text: &str) -> Option<Cow<'_, str>> {
|
||||
craft_i18n::ascii_digits(text, |c| matches!(c, '.' | ',')).ok()
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Self> {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -71,7 +71,10 @@ fn resolution(v: &Value) -> Result<f64> {
|
||||
"screen" => 72.0,
|
||||
"medium" => 150.0,
|
||||
"high" => 300.0,
|
||||
other => other.trim_end_matches("ppi").trim().parse::<f64>().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::<f64>().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"))?,
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user