diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index 18974db32a..ddea80c5f6 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -168,7 +168,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, decimal_point, finite_in_every_unit, parse_number}; +pub use units::{ColorMode, Unit, arithmetic_signs, decimal_point, finite_in_every_unit, parse_number}; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Document { diff --git a/crates/doc/src/tests/units.rs b/crates/doc/src/tests/units.rs index 9696bf54d0..fd8690a8ff 100644 --- a/crates/doc/src/tests/units.rs +++ b/crates/doc/src/tests/units.rs @@ -116,6 +116,11 @@ fn x_is_a_multiplication_sign() { (Unit::Points, "2px x 3", 6.0), (Unit::Points, "2pxx3", 6.0), (Unit::Millimeters, "4x3 px", 12.0), + // Only a lowercase x after a p is the px suffix. + (Unit::Points, "2pX3", 72.0), + (Unit::Points, "2p×3", 72.0), + // Text that used to be refused now reads as a product. + (Unit::Points, "8.5x11 in", 93.5 * 72.0), ] { let got = u.parse(s).unwrap_or_else(|| panic!("{u:?} can't read `{s}`")); assert!((got - want).abs() < 1e-9, "{u:?}: `{s}` → {got}, want {want}"); @@ -132,6 +137,8 @@ fn x_is_a_multiplication_sign() { assert_eq!(Unit::Points.parse(&format!("1{}", "x1".repeat(128))), Some(1.0)); assert_eq!(Unit::Points.parse(&format!("1{}", "x1".repeat(129))), None); assert_eq!(crate::parse_number(&format!("1{}", "×1".repeat(129))), None); + assert_eq!(crate::arithmetic_signs("1+2x3X4×5*6/7-8"), 7); + assert_eq!(crate::arithmetic_signs("2px x 3"), 1); } proptest! { diff --git a/crates/doc/src/units.rs b/crates/doc/src/units.rs index 45af1d172b..c896ee5ee8 100644 --- a/crates/doc/src/units.rs +++ b/crates/doc/src/units.rs @@ -153,26 +153,17 @@ impl Unit { /// /// `x`, `X` and `×` multiply as `*` does (`10x2` is 20, `2x3 mm` is 6 mm), except a lowercase /// `x` right after a `p`, which is the `px` suffix (`2px x 3` is 6 px, and `2px3` reads as - /// nothing; write `2p x 3` for picas). + /// nothing; write `2p x 3` for picas). `X` and `×` after a `p` multiply (`2pX3` is 6 picas). /// - /// A unit after a `*` or `/` operand measures the whole expression (`1080/2 px` is 540 px), so - /// math typed before a field's unit suffix works. After `*` or `/`, a percentage scales the term - /// before it (`3cm*50%` is 1.5 cm, `3cm/50%` is 6 cm). After `+` or `-`, it adds or takes away - /// that share of everything before it (`50pt+25%` is 62.5 pt, `100pt+20pt-50%` is 60 pt). A - /// percentage on its own (`50%`) reads as nothing. + /// A unit after a `*` or `/` operand measures the whole expression (`1080/2 px` is 540 px), + /// so math typed before a field's unit suffix works. After `*` or `/`, a percentage scales the + /// term before it (`3cm*50%` is 1.5 cm, `3cm/50%` is 6 cm). After `+` or `-`, it adds or takes + /// away that share of everything before it (`50pt+25%` is 62.5 pt, `100pt+20pt-50%` is 60 + /// pt). A percentage on its own (`50%`) reads as nothing. /// - /// Text with more than 128 `+ - * /` signs (a multiplication sign counts as `*`) reads as nothing. + /// Text with more than 128 [`arithmetic_signs`] reads as nothing. pub fn parse(self, s: &str) -> Option { - let mut prev = None; - let operators = s - .chars() - .filter(|&c| { - let operator = matches!(c, '+' | '-' | '*' | '/') || is_times(c, prev); - prev = Some(c); - operator - }) - .count(); - if operators > MAX_MEASURE_OPERATORS { + if arithmetic_signs(s) > MAX_MEASURE_OPERATORS { return None; } parse_measure(×_as_asterisks(s), self).filter(|v| v.is_finite()) @@ -228,6 +219,19 @@ impl Unit { /// The most `+ - * /` signs [`Unit::parse`] reads in one text. const MAX_MEASURE_OPERATORS: usize = 128; +/// The number of `+ - * /` signs in `s`, each multiplication sign typed as `x`, `X` or `×` +/// ([`Unit::parse`]) counted as a `*`. +pub fn arithmetic_signs(s: &str) -> usize { + let mut prev = None; + s.chars() + .filter(|&c| { + let sign = matches!(c, '+' | '-' | '*' | '/') || is_times(c, prev); + prev = Some(c); + sign + }) + .count() +} + /// Is `c`, after `prev`, a multiplication sign typed as `x`, `X` or `×`? A lowercase `x` right /// after a `p` is the `px` suffix (`2px x 3`), also where a number follows it (`2px3`). fn is_times(c: char, prev: Option) -> bool { diff --git a/crates/engine/src/cmd/addons/units.rs b/crates/engine/src/cmd/addons/units.rs index b5d844709a..4da4f68b79 100644 --- a/crates/engine/src/cmd/addons/units.rs +++ b/crates/engine/src/cmd/addons/units.rs @@ -10,7 +10,7 @@ mod tests; use serde_json::{Value, json}; -use vectorcraft_doc::{OrientedBox, Unit, decimal_point, finite_in_every_unit}; +use vectorcraft_doc::{OrientedBox, Unit, arithmetic_signs, decimal_point, finite_in_every_unit}; use vectorcraft_render::effects; use super::super::{CommandSpec, always, bad, cmd, has_selection}; @@ -22,8 +22,8 @@ const SELECTION_SIZE: &str = "addon.units.selectionSize"; const SHOWN_CHARS: usize = 64; /// The longest `value` or `unit` text `addon.units.convert` reads, in bytes. const MAX_TEXT_LEN: usize = 1024; -/// The most `+ - * /` signs `addon.units.convert` reads in one `value`: [`Unit::parse`] takes one -/// level of recursion per sign. +/// The most `+ - * /` signs `addon.units.convert` reads in one `value`, a multiplication sign +/// counted as a `*`: [`Unit::parse`] takes one level of recursion per sign. const MAX_OPERATORS: usize = 128; pub fn specs() -> Vec { @@ -33,7 +33,7 @@ pub fn specs() -> Vec { "Convert Units", [], None, - "{value: a length (a number, or text as a number field reads it, with a unit suffix and + - * / arithmetic: \"10mm\", \"1in/2\", \"2p6\", \"3cm*50%\"; a decimal comma reads as a point: \"0,5 mm\"; at most 1024 bytes and 128 operators), unit?: the unit of a number without a suffix (a unit's name, suffix or key: points, picas, inches, millimeters, centimeters, pixels, feetInches, meters, yards, feet, q, ha; default: points)} the length in every unit of Preferences › Units › East Asian Type, written as the number fields write it (rounded to 3 decimals in pt, px, picas, Q and Ha and to 4 in the others; pixels are points). Nothing in the document changes → {points, units: [{unit (its key), label, number (no suffix), text (with the suffix)}]}", + "{value: a length (a number, or text as a number field reads it, with a unit suffix and + - * / arithmetic, x, X or × multiplying as * does: \"10mm\", \"1in/2\", \"2p6\", \"3cm*50%\", \"2x3 mm\"; a decimal comma reads as a point: \"0,5 mm\"; at most 1024 bytes and 128 operators, each multiplication sign counted), unit?: the unit of a number without a suffix (a unit's name, suffix or key: points, picas, inches, millimeters, centimeters, pixels, feetInches, meters, yards, feet, q, ha; default: points)} the length in every unit of Preferences › Units › East Asian Type, written as the number fields write it (rounded to 3 decimals in pt, px, picas, Q and Ha and to 4 in the others; pixels are points). Nothing in the document changes → {points, units: [{unit (its key), label, number (no suffix), text (with the suffix)}]}", always, convert ), @@ -88,9 +88,9 @@ fn convert(_: &mut Session, p: &Value) -> Result { } /// Is `s` short enough to read: at most [`MAX_TEXT_LEN`] bytes and [`MAX_OPERATORS`] `+ - * /` -/// signs? +/// signs, a multiplication sign typed as `x`, `X` or `×` counted as a `*` ([`arithmetic_signs`])? fn readable(s: &str) -> bool { - s.len() <= MAX_TEXT_LEN && s.chars().filter(|c| matches!(c, '+' | '-' | '*' | '/')).count() <= MAX_OPERATORS + s.len() <= MAX_TEXT_LEN && arithmetic_signs(s) <= MAX_OPERATORS } /// `v` as an error message shows it: text cut at [`SHOWN_CHARS`] characters with "…", as JSON, diff --git a/crates/engine/src/cmd/addons/units/tests.rs b/crates/engine/src/cmd/addons/units/tests.rs index f914c53092..802fb5dfdf 100644 --- a/crates/engine/src/cmd/addons/units/tests.rs +++ b/crates/engine/src/cmd/addons/units/tests.rs @@ -76,6 +76,12 @@ fn values_read_as_number_fields_read_them() { assert!(near(pt(&mut s, json!({"value": "1,500+2", "unit": "mm"})), 3.5 * 72.0 / 25.4)); // A sum of 128 signs still reads. assert!(near(pt(&mut s, json!({"value": format!("1{}", "+1".repeat(128))})), 129.0)); + // x, X and × multiply as * does, and each counts toward the 128 signs. + assert!(near(pt(&mut s, json!({"value": "2x3 mm"})), 6.0 * 72.0 / 25.4)); + assert!(near(pt(&mut s, json!({"value": "10X2"})), 20.0)); + assert!(near(pt(&mut s, json!({"value": "0,5×4 in"})), 144.0)); + assert!(near(pt(&mut s, json!({"value": format!("1{}", "x1".repeat(128))})), 1.0)); + assert!(s.execute("addon.units.convert", &json!({"value": format!("1{}", "x1".repeat(129))})).is_err()); } #[test] diff --git a/crates/ui-egui/src/tests/numfields.rs b/crates/ui-egui/src/tests/numfields.rs index 3b7c2d157e..585c16ba2a 100644 --- a/crates/ui-egui/src/tests/numfields.rs +++ b/crates/ui-egui/src/tests/numfields.rs @@ -779,7 +779,7 @@ fn bare_numbers_in_picas_fields_are_points_when_the_preference_says_so() { assert_eq!(typed(&mut f, pc, "1in"), Some(72.0), "a typed unit wins"); assert_eq!(typed(&mut f, pc, "10+5"), Some(15.0), "arithmetic in points too"); assert_eq!(typed(&mut f, pc, "10x2"), Some(20.0), "x multiplies (#8435)"); - assert_eq!(typed(&mut f, mm, "2 x 3"), Some(mm.to_pt(6.0)), "x multiplies in millimetres"); + assert_eq!(typed(&mut f, mm, "2 x 3"), Some(mm.to_pt(6.0)), "x multiplies in millimeters"); assert_eq!(typed(&mut f, mm, "12"), Some(mm.to_pt(12.0)), "a millimetre field keeps millimetres"); widgets::set_bare_numbers_are_points(&f.ctx, false); assert_eq!(typed(&mut f, pc, "12"), Some(pc.to_pt(12.0)), "off: picas"); diff --git a/docs/mcp.md b/docs/mcp.md index 014636167e..13b1d089fb 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -7592,7 +7592,8 @@ Commands take and return lengths in points (the distances of `object.path.offset `object.path.splitIntoGrid` and `text.setStyle` `size`/`leading` also take a string with a unit, `"5 mm"`). A length string takes the same arithmetic as the desktop app's fields, where `%` is a share of the value before it: `"200mm*50%"` is 100 mm and `"50pt+25%"` is 62.5 pt. `x`, `X` and `×` multiply as `*` does (`"2x3 mm"` is 6 mm), -except a lowercase `x` right after a `p`, which is the `px` suffix (`"2px3"` is not read; `"2p x 3"` is 6 picas). +except a lowercase `x` right after a `p`, which is the `px` suffix (`"2px3"` is not read; `"2p x 3"` and `"2pX3"` +are 6 picas). A string such as `"8.5x11 in"` reads as one length, 93.5 in. A length string with more than 128 `+ - * /` signs (each multiplication sign counts) is not read as a length. The units only change what the UI shows and how it reads typed numbers. Preferences ▸ Units has four: General (rulers, positions and sizes, the Info panel, dialog distances, canvas measurement labels), Stroke @@ -14025,9 +14026,10 @@ Unit Converter is a built-in add-on that shows one length in every unit at once Converter and in its panel (`window.panel {panel: "unitConverter"}`). Neither command changes the document. `addon.units.convert {value, unit?}` reads `value` as a number field reads it: a number, or text with a unit suffix and -`+ - * /` arithmetic (`"10mm"`, `"1in/2"`, `"2p6"`, `"1080/2 px"`, `"3cm*50%"`, `"13q"`), a decimal comma read as a point -(`"0,5 mm"`) and a single number with a thousands comma (`"1,000 mm"`) refused, in at most 1024 bytes with at most 128 `+ - * /` signs. A -number without a suffix is in `unit` (a unit's name, suffix or key: `points`, `picas`, `inches`, `millimeters`, +`+ - * /` arithmetic, where `x`, `X` and `×` multiply as `*` does (`"10mm"`, `"1in/2"`, `"2p6"`, `"1080/2 px"`, +`"3cm*50%"`, `"2x3 mm"`, `"13q"`), a decimal comma read as a point (`"0,5 mm"`) and a single number with a thousands +comma (`"1,000 mm"`) refused, in at most 1024 bytes with at most 128 `+ - * /` signs (each multiplication sign counts). +A number without a suffix is in `unit` (a unit's name, suffix or key: `points`, `picas`, `inches`, `millimeters`, `centimeters`, `pixels`, `feetInches`, `meters`, `yards`, `feet`, `q`, `ha`; default `points`); a suffix in the text takes precedence over `unit`. It works without an open document. The command fails when `value` is missing, does not read, or is not finite in every unit, and when `unit` is not a unit. Result: `{points, units: [{unit, label, number, diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 47233ebac4..c7721291c0 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -309,7 +309,7 @@ tools, never checked side by side) isn't captured row by row. | Fields commit what was typed on a click away or a move to another field, as Enter does; Escape discards it | done | #1787 | | Number fields: Up/Down step, Shift ×10, Cmd/Ctrl ÷10, wheel over a hovered field without a click; steps, the wheel and label scrubs stop at the field's bounds (a stroke weight at 0, a miter limit at 1) | done | Shift+arrow in the Stroke weight field doesn't step by 10 (#991); bounds #3370. The field takes the wheel only when the pointer was moved onto it after the field last moved or was covered by a window or overlay, and the wheel stream began there; a stream that began elsewhere stays there until 0.5 s pass with no wheel input or the pointer is pressed on a field, so a panel scrolled under a still pointer keeps scrolling past the field. This rule and its 0.5 s are VectorCraft's choices. | | Numeric spinner arrows step once on a click and keep repeating while held with the mouse | done | #6307; headless test `a_held_spinner_arrow_repeats` holds past egui's click timeout and checks release. | -| Arithmetic in fields (`+ - * /`, `x`, `X` or `×` for `*`, and `%` of the value before it: `3cm*50%`, `50pt+25%`) and unit suffixes (`3 mm`, `1in`, `2p6`) | done | `Unit::parse`; mixed units in one expression not verified | +| Arithmetic in fields (`+ - * /`, `x`, `X` or `×` for `*`, and `%` of the value before it: `3cm*50%`, `50pt+25%`) and unit suffixes (`3 mm`, `1in`, `2p6`) | done | `Unit::parse` and `parse_number`; mixed units in one expression not verified | | Numeric fields: focus click selects the whole value; dragging selects from the pressed character | partial | #1326; select-all waits for click release; px/pt/plain/optional fields, both directions and replacement text tested; native API input, 17 px partial replacement, Undo/Redo, saved-file round trip and screenshots verified; Tab, double-click, arrows and wheel preserved; a release more than 1 pt from the press keeps the caret, a tap within 1 pt selects all (#6659) | | Units: pt, pc, in, mm, cm, px, ft-in, m, yd, ft; General/Stroke/Type units | done | units preference reverting to points on SVG output reported (#864). A file that records no units (PDF, EPS or `.ai` without ruler units, raster images, metafiles, Affinity files) opens and reverts in General units; native, SVG and DXF files and a `.ai` or EPS with ruler units keep theirs, as a [Community Expert's answer](https://community.adobe.com/questions-652/change-units-from-points-to-inches-won-t-stick-765372) describes (#5820) | | Units › East Asian Type (Q and Ha) | done | #394: `unitsAsianType` offers every unit and Q and Ha, a quarter of a millimetre each ([Scripting Guide's Measurement Units](https://ai-scripting.docsforadobe.dev/scripting/measurementUnits/): 0.709 pt per Q; [InDesign's unit notation](https://helpx.adobe.com/in/indesign/desktop/layout-and-grid-tools/rulers-and-measure-tools/change-ruler-measurement-units.html): `q`, `h`); only it lists Q and Ha. The public pages do not say which fields use it, so no field uses it yet and that waits for a source. Tests `q_and_ha_are_a_quarter_millimetre`, `east_asian_type_unit_*` |