diff --git a/crates/behavior/src/quantity.rs b/crates/behavior/src/quantity.rs index 782cbb7b6..00a256338 100644 --- a/crates/behavior/src/quantity.rs +++ b/crates/behavior/src/quantity.rs @@ -30,12 +30,15 @@ struct Unit { factor: f64, /// What the caller named this unit by, handed back by [`Reader::read_tagged`]. tag: Option, + /// The factor of a number written right after the spelling ([`Reader::split_unit`]). + rest: Option, } /// Reads numbers, expressions and unit quantities written in one locale. /// /// Grammar: `expr = term (('+' | '-') term)*`, `term = factor (('*' | '/' | 'x' | 'X' | '×') factor)*`, -/// `factor = ('-' | '+')? (number unit? | '(' expr ')')`. Text that starts with `+`, `*` or `/` is relative: it +/// `factor = ('-' | '+')? (number unit? | '(' expr ')')`. A factor after `*`, `/`, `x` or `×` may be a percentage +/// (`3cm*50%` is 1.5 cm), whatever `%` means elsewhere. Text that starts with `+`, `*` or `/` is relative: it /// applies to the start value (`+5` adds 5, `*2` doubles). A leading `-` is a negative number. A unit is the longest /// registered spelling at the start of the letters after a number, so `2inx3` is `2in` times 3 and `2px` stays a /// quantity when `px` is registered. A group separator counts only before exactly three digits. @@ -87,14 +90,22 @@ impl Reader { /// Spaces between the number and the spelling are skipped, so "12 %" reads like "12%". #[must_use] pub fn unit(mut self, spelling: &str, factor: f64) -> Self { - self.units.push(Unit { spelling: spelling.to_lowercase(), factor, tag: None }); + self.units.push(Unit { spelling: spelling.to_lowercase(), factor, tag: None, rest: None }); self } /// Like [`Reader::unit`], with a `tag` that [`Reader::read_tagged`] returns when the text names this spelling. #[must_use] pub fn unit_tagged(mut self, spelling: &str, factor: f64, tag: u32) -> Self { - self.units.push(Unit { spelling: spelling.to_lowercase(), factor, tag: Some(tag) }); + self.units.push(Unit { spelling: spelling.to_lowercase(), factor, tag: Some(tag), rest: None }); + self + } + + /// Like [`Reader::unit_tagged`], where a number written right after the spelling adds that many of `rest`: picas + /// and points read as `2p6`, two picas and six points. + #[must_use] + pub fn split_unit(mut self, spelling: &str, factor: f64, rest: f64, tag: u32) -> Self { + self.units.push(Unit { spelling: spelling.to_lowercase(), factor, tag: Some(tag), rest: Some(rest) }); self } @@ -170,7 +181,8 @@ impl Parse<'_> { Ok(left) } - /// A factor; `scaled` says a number with no unit counts as the reader's bare factor. + /// A factor; `scaled` says it starts a term, where a number with no unit counts as the reader's bare factor. + /// Otherwise it is a multiplier, which may be a percentage. fn factor(&mut self, scaled: bool) -> Result { match self.peek() { Some('-') => { @@ -196,6 +208,10 @@ impl Parse<'_> { fn quantity(&mut self, scaled: bool) -> Result { let number = self.number()?; self.tag = None; + if !scaled && self.peek() == Some('%') { + self.at += 1; + return Ok(number / 100.0); + } let reader = self.reader; let run: Vec = self.chars[self.at..].iter().copied().take_while(|c| is_unit_char(*c)).collect(); let unit = (1..=run.len()).rev().find_map(|len| { @@ -206,7 +222,11 @@ impl Parse<'_> { Some((len, unit)) => { self.at += len; self.tag = unit.tag; - Ok(number * unit.factor) + let rest = match unit.rest { + Some(rest) if self.peek().is_some_and(|c| digit(c).is_some() || c == reader.decimal) => rest * self.number()?, + _ => 0.0, + }; + Ok(number * unit.factor + rest) } None if scaled => Ok(number * reader.bare), None => Ok(number), @@ -285,6 +305,17 @@ mod tests { near(&picas, "1pc+12", 2.0); near(&picas, "12*2", 2.0); + let split = Reader::new().bare(1.0 / 12.0).split_unit("p", 1.0, 1.0 / 12.0, 0); + near(&split, "2p6", 2.5); + near(&split, "-2p6", -2.5); + near(&split, "2p", 2.0); + + let percent = Reader::new().unit("cm", 1.0).unit("%", 7.0); + near(&percent, "3cm*50%", 1.5); + near(&percent, "3cm/50%", 6.0); + near(&percent, "2%", 14.0); + assert_eq!(percent.read("*50%", Some(4.0)), Ok(2.0), "a relative share"); + let german = Reader::new().decimal(',').group('.'); near(&german, "1.234,5", 1234.5); near(&german, "1.234.567", 1_234_567.0); diff --git a/crates/ui-contract/src/runtime/env/units.rs b/crates/ui-contract/src/runtime/env/units.rs index cc10c9a55..30cefec0a 100644 --- a/crates/ui-contract/src/runtime/env/units.rs +++ b/crates/ui-contract/src/runtime/env/units.rs @@ -620,6 +620,7 @@ impl Units { reader = reader.group(group); } let english = Units::new(self.format.clone(), english_words(), Arc::new(Words::english()), self.point_size); + let points = factor(UnitKind::Points); for (tag, other) in UnitKind::ALL.into_iter().enumerate() { let Some(factor) = factor(other) else { continue }; for spelling in [self.symbol(other), english.symbol(other)] { @@ -627,6 +628,12 @@ impl Units { reader = reader.unit_tagged(&spelling, factor, tag as u32); } } + if other == UnitKind::Picas + && let Some(points) = points + { + // Picas and points as print apps read them: `2p6` is two picas and six points. + reader = reader.split_unit("p", factor, points, tag as u32); + } } reader } diff --git a/crates/ui-widgets/tests/it/text/number_length.rs b/crates/ui-widgets/tests/it/text/number_length.rs index 66efb8c54..4550c8944 100644 --- a/crates/ui-widgets/tests/it/text/number_length.rs +++ b/crates/ui-widgets/tests/it/text/number_length.rs @@ -77,3 +77,16 @@ fn a_bare_number_reads_in_the_lengths_bare_unit() { let picas_plain = in_lengths(picas, START, type_and_commit("1")); assert!((picas_spelled - picas_plain).abs() < SAME, "a spelled pica is still a pica"); } + +/// Any length field reads picas and points written together (`2p6`) as their sum, and a percentage after `*` as a +/// share of the quantity before it. +#[trial] +fn picas_with_points_and_a_percentage_factor_read_as_print_apps_write_them() { + let mm = LengthUnits::new(UnitKind::Points, UnitKind::Millimetres, START); + let together = in_lengths(mm, START, type_and_commit("2p6")); + let summed = in_lengths(mm, START, type_and_commit("2 pica + 6 pt")); + assert!((together - summed).abs() < SAME && together != START, "2p6 is two picas and six points: {together} against {summed}"); + let share = in_lengths(mm, START, type_and_commit("3cm*50%")); + let half = in_lengths(mm, START, type_and_commit("1.5cm")); + assert!((share - half).abs() < SAME && share != START, "3cm*50% is half of 3 cm: {share} against {half}"); +}