diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-03-09 14:17:24 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-03-09 14:42:14 +0100 |
| commit | c38d72383d2068361d635d6c1c78ba97aa917801 (patch) | |
| tree | e758418a2d704d69dee88faf4a9a9c69b25b47ca /library/src/math | |
| parent | d7a65fa26d131179d9d82226e5ee1b562084e48a (diff) | |
Make all optional fields settable
Diffstat (limited to 'library/src/math')
| -rw-r--r-- | library/src/math/attach.rs | 14 | ||||
| -rw-r--r-- | library/src/math/delimited.rs | 26 | ||||
| -rw-r--r-- | library/src/math/matrix.rs | 54 | ||||
| -rw-r--r-- | library/src/math/mod.rs | 17 | ||||
| -rw-r--r-- | library/src/math/op.rs | 3 | ||||
| -rw-r--r-- | library/src/math/underover.rs | 12 |
6 files changed, 55 insertions, 71 deletions
diff --git a/library/src/math/attach.rs b/library/src/math/attach.rs index 7ba1484a..c2d7703d 100644 --- a/library/src/math/attach.rs +++ b/library/src/math/attach.rs @@ -21,13 +21,9 @@ pub struct AttachNode { pub base: Content, /// The top attachment. - #[named] - #[default] pub top: Option<Content>, /// The bottom attachment. - #[named] - #[default] pub bottom: Option<Content>, } @@ -40,11 +36,17 @@ impl LayoutMath for AttachNode { let base = ctx.layout_fragment(&base)?; ctx.style(ctx.style.for_subscript()); - let top = self.top().map(|node| ctx.layout_fragment(&node)).transpose()?; + let top = self + .top(ctx.styles()) + .map(|node| ctx.layout_fragment(&node)) + .transpose()?; ctx.unstyle(); ctx.style(ctx.style.for_superscript()); - let bottom = self.bottom().map(|node| ctx.layout_fragment(&node)).transpose()?; + let bottom = self + .bottom(ctx.styles()) + .map(|node| ctx.layout_fragment(&node)) + .transpose()?; ctx.unstyle(); let display_limits = display_limits diff --git a/library/src/math/delimited.rs b/library/src/math/delimited.rs index 63cb916c..f9d22c43 100644 --- a/library/src/math/delimited.rs +++ b/library/src/math/delimited.rs @@ -16,23 +16,17 @@ pub(super) const DELIM_SHORT_FALL: Em = Em::new(0.1); /// /// Display: Left/Right /// Category: math -#[node(Construct, LayoutMath)] +#[node(LayoutMath)] pub struct LrNode { - /// The delimited content, including the delimiters. - #[positional] - #[required] - pub body: Content, - /// The size of the brackets, relative to the height of the wrapped content. /// /// Defaults to `{100%}`. - #[named] - #[default] pub size: Smart<Rel<Length>>, -} -impl Construct for LrNode { - fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> { + /// The delimited content, including the delimiters. + #[positional] + #[required] + #[parse( let mut body = Content::empty(); for (i, arg) in args.all::<Content>()?.into_iter().enumerate() { if i > 0 { @@ -40,16 +34,16 @@ impl Construct for LrNode { } body += arg; } - let size = args.named::<Smart<Rel<Length>>>("size")?.unwrap_or_default(); - Ok(Self::new(body).with_size(size).pack()) - } + body + )] + pub body: Content, } impl LayoutMath for LrNode { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { let mut body = self.body(); if let Some(node) = body.to::<LrNode>() { - if node.size().is_auto() { + if node.size(ctx.styles()).is_auto() { body = node.body(); } } @@ -63,7 +57,7 @@ impl LayoutMath for LrNode { .unwrap_or_default(); let height = self - .size() + .size(ctx.styles()) .unwrap_or(Rel::one()) .resolve(ctx.styles()) .relative_to(2.0 * max_extent); diff --git a/library/src/math/matrix.rs b/library/src/math/matrix.rs index d4bf52f3..6dec645c 100644 --- a/library/src/math/matrix.rs +++ b/library/src/math/matrix.rs @@ -18,24 +18,23 @@ const VERTICAL_PADDING: Ratio = Ratio::new(0.1); /// Category: math #[node(LayoutMath)] pub struct VecNode { - /// The elements of the vector. - #[variadic] - pub children: Vec<Content>, - /// The delimiter to use. /// /// ```example /// #set math.vec(delim: "[") /// $ vec(1, 2) $ /// ``` - #[settable] #[default(Delimiter::Paren)] pub delim: Delimiter, + + /// The elements of the vector. + #[variadic] + pub children: Vec<Content>, } impl LayoutMath for VecNode { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { - let delim = Self::delim_in(ctx.styles()); + let delim = self.delim(ctx.styles()); let frame = layout_vec_body(ctx, &self.children(), Align::Center)?; layout_delimiters(ctx, frame, Some(delim.open()), Some(delim.close())) } @@ -63,31 +62,26 @@ impl LayoutMath for VecNode { /// /// Display: Matrix /// Category: math -#[node(Construct, LayoutMath)] +#[node(LayoutMath)] pub struct MatNode { - /// An array of arrays with the rows of the matrix. - /// - /// ```example - /// #let data = ((1, 2, 3), (4, 5, 6)) - /// #let matrix = math.mat(..data) - /// $ v := matrix $ - /// ``` - #[variadic] - pub rows: Vec<Vec<Content>>, - /// The delimiter to use. /// /// ```example /// #set math.mat(delim: "[") /// $ mat(1, 2; 3, 4) $ /// ``` - #[settable] #[default(Delimiter::Paren)] pub delim: Delimiter, -} -impl Construct for MatNode { - fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> { + /// An array of arrays with the rows of the matrix. + /// + /// ```example + /// #let data = ((1, 2, 3), (4, 5, 6)) + /// #let matrix = math.mat(..data) + /// $ v := matrix $ + /// ``` + #[variadic] + #[parse( let mut rows = vec![]; let mut width = 0; @@ -109,13 +103,14 @@ impl Construct for MatNode { } } - Ok(Self::new(rows).pack()) - } + rows + )] + pub rows: Vec<Vec<Content>>, } impl LayoutMath for MatNode { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { - let delim = Self::delim_in(ctx.styles()); + let delim = self.delim(ctx.styles()); let frame = layout_mat_body(ctx, &self.rows())?; layout_delimiters(ctx, frame, Some(delim.open()), Some(delim.close())) } @@ -139,24 +134,23 @@ impl LayoutMath for MatNode { /// Category: math #[node(LayoutMath)] pub struct CasesNode { - /// The branches of the case distinction. - #[variadic] - pub children: Vec<Content>, - /// The delimiter to use. /// /// ```example /// #set math.cases(delim: "[") /// $ x = cases(1, 2) $ /// ``` - #[settable] #[default(Delimiter::Brace)] pub delim: Delimiter, + + /// The branches of the case distinction. + #[variadic] + pub children: Vec<Content>, } impl LayoutMath for CasesNode { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { - let delim = Self::delim_in(ctx.styles()); + let delim = self.delim(ctx.styles()); let frame = layout_vec_body(ctx, &self.children(), Align::Left)?; layout_delimiters(ctx, frame, Some(delim.open()), None) } diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index 3f0b0607..0c9dc338 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -134,21 +134,20 @@ pub fn module() -> Module { /// Category: math #[node(Show, Finalize, Layout, LayoutMath)] pub struct FormulaNode { + /// Whether the formula is displayed as a separate block. + #[default(false)] + pub block: bool, + /// The content of the formula. #[positional] #[required] pub body: Content, - - /// Whether the formula is displayed as a separate block. - #[named] - #[default(false)] - pub block: bool, } impl Show for FormulaNode { - fn show(&self, _: &mut Vt, _: &Content, _: StyleChain) -> SourceResult<Content> { + fn show(&self, _: &mut Vt, _: &Content, styles: StyleChain) -> SourceResult<Content> { let mut realized = self.clone().pack().guarded(Guard::Base(NodeId::of::<Self>())); - if self.block() { + if self.block(styles) { realized = realized.aligned(Axes::with_x(Some(Align::Center.into()))) } Ok(realized) @@ -156,7 +155,7 @@ impl Show for FormulaNode { } impl Finalize for FormulaNode { - fn finalize(&self, realized: Content) -> Content { + fn finalize(&self, realized: Content, _: StyleChain) -> Content { realized .styled(TextNode::set_weight(FontWeight::from_number(450))) .styled(TextNode::set_font(FontList(vec![FontFamily::new( @@ -172,7 +171,7 @@ impl Layout for FormulaNode { styles: StyleChain, regions: Regions, ) -> SourceResult<Fragment> { - let block = self.block(); + let block = self.block(styles); // Find a math font. let variant = variant(styles); diff --git a/library/src/math/op.rs b/library/src/math/op.rs index c855cd92..aa2e4cf7 100644 --- a/library/src/math/op.rs +++ b/library/src/math/op.rs @@ -30,7 +30,6 @@ pub struct OpNode { /// Whether the operator should force attachments to display as limits. /// /// Defaults to `{false}`. - #[named] #[default(false)] pub limits: bool, } @@ -41,7 +40,7 @@ impl LayoutMath for OpNode { ctx.push( FrameFragment::new(ctx, frame) .with_class(MathClass::Large) - .with_limits(self.limits()), + .with_limits(self.limits(ctx.styles())), ); Ok(()) } diff --git a/library/src/math/underover.rs b/library/src/math/underover.rs index 87f30c0f..2aabf132 100644 --- a/library/src/math/underover.rs +++ b/library/src/math/underover.rs @@ -68,13 +68,12 @@ pub struct UnderbraceNode { /// The optional content below the brace. #[positional] - #[default] pub annotation: Option<Content>, } impl LayoutMath for UnderbraceNode { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { - layout(ctx, &self.body(), &self.annotation(), '⏟', BRACE_GAP, false) + layout(ctx, &self.body(), &self.annotation(ctx.styles()), '⏟', BRACE_GAP, false) } } @@ -96,13 +95,12 @@ pub struct OverbraceNode { /// The optional content above the brace. #[positional] - #[default] pub annotation: Option<Content>, } impl LayoutMath for OverbraceNode { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { - layout(ctx, &self.body(), &self.annotation(), '⏞', BRACE_GAP, true) + layout(ctx, &self.body(), &self.annotation(ctx.styles()), '⏞', BRACE_GAP, true) } } @@ -124,13 +122,12 @@ pub struct UnderbracketNode { /// The optional content below the bracket. #[positional] - #[default] pub annotation: Option<Content>, } impl LayoutMath for UnderbracketNode { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { - layout(ctx, &self.body(), &self.annotation(), '⎵', BRACKET_GAP, false) + layout(ctx, &self.body(), &self.annotation(ctx.styles()), '⎵', BRACKET_GAP, false) } } @@ -152,13 +149,12 @@ pub struct OverbracketNode { /// The optional content above the bracket. #[positional] - #[default] pub annotation: Option<Content>, } impl LayoutMath for OverbracketNode { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { - layout(ctx, &self.body(), &self.annotation(), '⎴', BRACKET_GAP, true) + layout(ctx, &self.body(), &self.annotation(ctx.styles()), '⎴', BRACKET_GAP, true) } } |
