diff options
| author | Max <me@mkor.je> | 2024-12-20 12:35:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-20 12:35:57 +0000 |
| commit | bb38a01d0625d5bdab0af50daf479e60c774d90e (patch) | |
| tree | b588b09ce44b698e7602d84ec3e43dd86a47b552 /crates/typst-library/src | |
| parent | 39706fe42f269b3a283eeff9b595480631737159 (diff) | |
Fix math size resolving (#5596)
Diffstat (limited to 'crates/typst-library/src')
| -rw-r--r-- | crates/typst-library/src/layout/rel.rs | 6 | ||||
| -rw-r--r-- | crates/typst-library/src/math/accent.rs | 8 | ||||
| -rw-r--r-- | crates/typst-library/src/math/attach.rs | 6 | ||||
| -rw-r--r-- | crates/typst-library/src/math/cancel.rs | 1 | ||||
| -rw-r--r-- | crates/typst-library/src/math/equation.rs | 7 | ||||
| -rw-r--r-- | crates/typst-library/src/math/lr.rs | 18 | ||||
| -rw-r--r-- | crates/typst-library/src/math/matrix.rs | 4 | ||||
| -rw-r--r-- | crates/typst-library/src/text/mod.rs | 10 |
8 files changed, 40 insertions, 20 deletions
diff --git a/crates/typst-library/src/layout/rel.rs b/crates/typst-library/src/layout/rel.rs index 30441a42..76d73678 100644 --- a/crates/typst-library/src/layout/rel.rs +++ b/crates/typst-library/src/layout/rel.rs @@ -86,12 +86,6 @@ impl Rel<Length> { None } } - - /// Convert to a relative length with the absolute part resolved at the - /// given font size. - pub fn at(self, font_size: Abs) -> Rel<Abs> { - self.map(|abs| abs.at(font_size)) - } } impl<T: Numeric + Debug> Debug for Rel<T> { diff --git a/crates/typst-library/src/math/accent.rs b/crates/typst-library/src/math/accent.rs index a1c70b4a..fee705ee 100644 --- a/crates/typst-library/src/math/accent.rs +++ b/crates/typst-library/src/math/accent.rs @@ -1,5 +1,5 @@ use crate::diag::bail; -use crate::foundations::{cast, elem, func, Content, NativeElement, Smart, Value}; +use crate::foundations::{cast, elem, func, Content, NativeElement, Value}; use crate::layout::{Length, Rel}; use crate::math::Mathy; use crate::text::TextElem; @@ -52,7 +52,9 @@ pub struct AccentElem { pub accent: Accent, /// The size of the accent, relative to the width of the base. - pub size: Smart<Rel<Length>>, + #[resolve] + #[default(Rel::one())] + pub size: Rel<Length>, } /// An accent character. @@ -101,7 +103,7 @@ macro_rules! accents { base: Content, /// The size of the accent, relative to the width of the base. #[named] - size: Option<Smart<Rel<Length>>>, + size: Option<Rel<Length>>, ) -> Content { let mut accent = AccentElem::new(base, Accent::new($primary)); if let Some(size) = size { diff --git a/crates/typst-library/src/math/attach.rs b/crates/typst-library/src/math/attach.rs index f3c7ea99..e1f57727 100644 --- a/crates/typst-library/src/math/attach.rs +++ b/crates/typst-library/src/math/attach.rs @@ -1,4 +1,4 @@ -use crate::foundations::{elem, Content, Packed, Smart}; +use crate::foundations::{elem, Content, Packed}; use crate::layout::{Length, Rel}; use crate::math::{EquationElem, Mathy}; @@ -152,5 +152,7 @@ pub struct StretchElem { /// The size to stretch to, relative to the maximum size of the glyph and /// its attachments. - pub size: Smart<Rel<Length>>, + #[resolve] + #[default(Rel::one())] + pub size: Rel<Length>, } diff --git a/crates/typst-library/src/math/cancel.rs b/crates/typst-library/src/math/cancel.rs index a72505c0..17f4dcfb 100644 --- a/crates/typst-library/src/math/cancel.rs +++ b/crates/typst-library/src/math/cancel.rs @@ -29,6 +29,7 @@ pub struct CancelElem { /// $ a + cancel(x, length: #200%) /// - cancel(x, length: #200%) $ /// ``` + #[resolve] #[default(Rel::new(Ratio::one(), Abs::pt(3.0).into()))] pub length: Rel<Length>, diff --git a/crates/typst-library/src/math/equation.rs b/crates/typst-library/src/math/equation.rs index 4edafe5e..a9173c43 100644 --- a/crates/typst-library/src/math/equation.rs +++ b/crates/typst-library/src/math/equation.rs @@ -135,6 +135,13 @@ pub struct EquationElem { #[internal] #[ghost] pub class: Option<MathClass>, + + /// Values of `scriptPercentScaleDown` and `scriptScriptPercentScaleDown` + /// respectively in the current font's MathConstants table. + #[internal] + #[default((70, 50))] + #[ghost] + pub script_scale: (i16, i16), } impl Synthesize for Packed<EquationElem> { diff --git a/crates/typst-library/src/math/lr.rs b/crates/typst-library/src/math/lr.rs index 07ab0dd4..965f5351 100644 --- a/crates/typst-library/src/math/lr.rs +++ b/crates/typst-library/src/math/lr.rs @@ -1,4 +1,4 @@ -use crate::foundations::{elem, func, Content, NativeElement, Smart}; +use crate::foundations::{elem, func, Content, NativeElement}; use crate::layout::{Length, Rel}; use crate::math::Mathy; use crate::text::TextElem; @@ -10,7 +10,9 @@ use crate::text::TextElem; #[elem(title = "Left/Right", Mathy)] pub struct LrElem { /// The size of the brackets, relative to the height of the wrapped content. - pub size: Smart<Rel<Length>>, + #[resolve] + #[default(Rel::one())] + pub size: Rel<Length>, /// The delimited content, including the delimiters. #[required] @@ -44,7 +46,7 @@ pub struct MidElem { pub fn floor( /// The size of the brackets, relative to the height of the wrapped content. #[named] - size: Option<Smart<Rel<Length>>>, + size: Option<Rel<Length>>, /// The expression to floor. body: Content, ) -> Content { @@ -60,7 +62,7 @@ pub fn floor( pub fn ceil( /// The size of the brackets, relative to the height of the wrapped content. #[named] - size: Option<Smart<Rel<Length>>>, + size: Option<Rel<Length>>, /// The expression to ceil. body: Content, ) -> Content { @@ -76,7 +78,7 @@ pub fn ceil( pub fn round( /// The size of the brackets, relative to the height of the wrapped content. #[named] - size: Option<Smart<Rel<Length>>>, + size: Option<Rel<Length>>, /// The expression to round. body: Content, ) -> Content { @@ -92,7 +94,7 @@ pub fn round( pub fn abs( /// The size of the brackets, relative to the height of the wrapped content. #[named] - size: Option<Smart<Rel<Length>>>, + size: Option<Rel<Length>>, /// The expression to take the absolute value of. body: Content, ) -> Content { @@ -108,7 +110,7 @@ pub fn abs( pub fn norm( /// The size of the brackets, relative to the height of the wrapped content. #[named] - size: Option<Smart<Rel<Length>>>, + size: Option<Rel<Length>>, /// The expression to take the norm of. body: Content, ) -> Content { @@ -119,7 +121,7 @@ fn delimited( body: Content, left: char, right: char, - size: Option<Smart<Rel<Length>>>, + size: Option<Rel<Length>>, ) -> Content { let span = body.span(); let mut elem = LrElem::new(Content::sequence([ diff --git a/crates/typst-library/src/math/matrix.rs b/crates/typst-library/src/math/matrix.rs index 1c788995..c74eb8fa 100644 --- a/crates/typst-library/src/math/matrix.rs +++ b/crates/typst-library/src/math/matrix.rs @@ -56,6 +56,7 @@ pub struct VecElem { /// #set math.vec(gap: 1em) /// $ vec(1, 2) $ /// ``` + #[resolve] #[default(DEFAULT_ROW_GAP.into())] pub gap: Rel<Length>, @@ -161,6 +162,7 @@ pub struct MatElem { /// #set math.mat(row-gap: 1em) /// $ mat(1, 2; 3, 4) $ /// ``` + #[resolve] #[parse( let gap = args.named("gap")?; args.named("row-gap")?.or(gap) @@ -174,6 +176,7 @@ pub struct MatElem { /// #set math.mat(column-gap: 1em) /// $ mat(1, 2; 3, 4) $ /// ``` + #[resolve] #[parse(args.named("column-gap")?.or(gap))] #[default(DEFAULT_COL_GAP.into())] pub column_gap: Rel<Length>, @@ -256,6 +259,7 @@ pub struct CasesElem { /// #set math.cases(gap: 1em) /// $ x = cases(1, 2) $ /// ``` + #[resolve] #[default(DEFAULT_ROW_GAP.into())] pub gap: Rel<Length>, diff --git a/crates/typst-library/src/text/mod.rs b/crates/typst-library/src/text/mod.rs index ee81e3f2..25ed009e 100644 --- a/crates/typst-library/src/text/mod.rs +++ b/crates/typst-library/src/text/mod.rs @@ -50,6 +50,7 @@ use crate::foundations::{ Resolve, Scope, Set, Smart, StyleChain, }; use crate::layout::{Abs, Axis, Dir, Em, Length, Ratio, Rel}; +use crate::math::{EquationElem, MathSize}; use crate::model::ParElem; use crate::visualize::{Color, Paint, RelativeTo, Stroke}; use crate::World; @@ -981,7 +982,14 @@ impl Resolve for TextSize { type Output = Abs; fn resolve(self, styles: StyleChain) -> Self::Output { - self.0.resolve(styles) + let factor = match EquationElem::size_in(styles) { + MathSize::Display | MathSize::Text => 1.0, + MathSize::Script => EquationElem::script_scale_in(styles).0 as f64 / 100.0, + MathSize::ScriptScript => { + EquationElem::script_scale_in(styles).1 as f64 / 100.0 + } + }; + factor * self.0.resolve(styles) } } |
