diff options
Diffstat (limited to 'crates/typst-library/src/layout')
| -rw-r--r-- | crates/typst-library/src/layout/align.rs | 16 | ||||
| -rw-r--r-- | crates/typst-library/src/layout/columns.rs | 16 | ||||
| -rw-r--r-- | crates/typst-library/src/layout/grid/mod.rs | 44 | ||||
| -rw-r--r-- | crates/typst-library/src/layout/hide.rs | 13 | ||||
| -rw-r--r-- | crates/typst-library/src/layout/layout.rs | 42 | ||||
| -rw-r--r-- | crates/typst-library/src/layout/pad.rs | 16 | ||||
| -rw-r--r-- | crates/typst-library/src/layout/repeat.rs | 16 | ||||
| -rw-r--r-- | crates/typst-library/src/layout/stack.rs | 16 | ||||
| -rw-r--r-- | crates/typst-library/src/layout/transform.rs | 50 |
9 files changed, 32 insertions, 197 deletions
diff --git a/crates/typst-library/src/layout/align.rs b/crates/typst-library/src/layout/align.rs index e5ceddf6..447648f0 100644 --- a/crates/typst-library/src/layout/align.rs +++ b/crates/typst-library/src/layout/align.rs @@ -2,11 +2,10 @@ use std::ops::Add; use ecow::{eco_format, EcoString}; -use crate::diag::{bail, HintedStrResult, SourceResult, StrResult}; -use crate::engine::Engine; +use crate::diag::{bail, HintedStrResult, StrResult}; use crate::foundations::{ - cast, elem, func, scope, ty, CastInfo, Content, Fold, FromValue, IntoValue, Packed, - Reflect, Repr, Resolve, Show, StyleChain, Value, + cast, elem, func, scope, ty, CastInfo, Content, Fold, FromValue, IntoValue, Reflect, + Repr, Resolve, StyleChain, Value, }; use crate::layout::{Abs, Axes, Axis, Dir, Side}; use crate::text::TextElem; @@ -73,7 +72,7 @@ use crate::text::TextElem; /// ```example /// Start #h(1fr) End /// ``` -#[elem(Show)] +#[elem] pub struct AlignElem { /// The [alignment] along both axes. /// @@ -97,13 +96,6 @@ pub struct AlignElem { pub body: Content, } -impl Show for Packed<AlignElem> { - #[typst_macros::time(name = "align", span = self.span())] - fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> { - Ok(self.body.clone().aligned(self.alignment.get(styles))) - } -} - /// Where to align something along an axis. /// /// Possible values are: diff --git a/crates/typst-library/src/layout/columns.rs b/crates/typst-library/src/layout/columns.rs index 1cea5275..e7bce393 100644 --- a/crates/typst-library/src/layout/columns.rs +++ b/crates/typst-library/src/layout/columns.rs @@ -1,9 +1,7 @@ use std::num::NonZeroUsize; -use crate::diag::SourceResult; -use crate::engine::Engine; -use crate::foundations::{elem, Content, NativeElement, Packed, Show, StyleChain}; -use crate::layout::{BlockElem, Length, Ratio, Rel}; +use crate::foundations::{elem, Content}; +use crate::layout::{Length, Ratio, Rel}; /// Separates a region into multiple equally sized columns. /// @@ -41,7 +39,7 @@ use crate::layout::{BlockElem, Length, Ratio, Rel}; /// /// #lorem(40) /// ``` -#[elem(Show)] +#[elem] pub struct ColumnsElem { /// The number of columns. #[positional] @@ -57,14 +55,6 @@ pub struct ColumnsElem { pub body: Content, } -impl Show for Packed<ColumnsElem> { - fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(BlockElem::multi_layouter(self.clone(), engine.routines.layout_columns) - .pack() - .spanned(self.span())) - } -} - /// Forces a column break. /// /// The function will behave like a [page break]($pagebreak) when used in a diff --git a/crates/typst-library/src/layout/grid/mod.rs b/crates/typst-library/src/layout/grid/mod.rs index 64e7464b..658523ec 100644 --- a/crates/typst-library/src/layout/grid/mod.rs +++ b/crates/typst-library/src/layout/grid/mod.rs @@ -11,10 +11,10 @@ use crate::diag::{bail, At, HintedStrResult, HintedString, SourceResult}; use crate::engine::Engine; use crate::foundations::{ cast, elem, scope, Array, CastInfo, Content, Context, Fold, FromValue, Func, - IntoValue, NativeElement, Packed, Reflect, Resolve, Show, Smart, StyleChain, Value, + IntoValue, Packed, Reflect, Resolve, Smart, StyleChain, Value, }; use crate::layout::{ - Alignment, BlockElem, Length, OuterHAlignment, OuterVAlignment, Rel, Sides, Sizing, + Alignment, Length, OuterHAlignment, OuterVAlignment, Rel, Sides, Sizing, }; use crate::model::{TableCell, TableFooter, TableHLine, TableHeader, TableVLine}; use crate::visualize::{Paint, Stroke}; @@ -136,7 +136,7 @@ use crate::visualize::{Paint, Stroke}; /// /// Furthermore, strokes of a repeated grid header or footer will take /// precedence over regular cell strokes. -#[elem(scope, Show)] +#[elem(scope)] pub struct GridElem { /// The column sizes. /// @@ -320,14 +320,6 @@ impl GridElem { type GridFooter; } -impl Show for Packed<GridElem> { - fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(BlockElem::multi_layouter(self.clone(), engine.routines.layout_grid) - .pack() - .spanned(self.span())) - } -} - /// Track sizing definitions. #[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] pub struct TrackSizings(pub SmallVec<[Sizing; 4]>); @@ -648,7 +640,7 @@ pub struct GridVLine { /// which allows you, for example, to apply styles based on a cell's position. /// Refer to the examples of the [`table.cell`]($table.cell) element to learn /// more about this. -#[elem(name = "cell", title = "Grid Cell", Show)] +#[elem(name = "cell", title = "Grid Cell")] pub struct GridCell { /// The cell's body. #[required] @@ -748,12 +740,6 @@ cast! { v: Content => v.into(), } -impl Show for Packed<GridCell> { - fn show(&self, _engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> { - show_grid_cell(self.body.clone(), self.inset.get(styles), self.align.get(styles)) - } -} - impl Default for Packed<GridCell> { fn default() -> Self { Packed::new( @@ -774,28 +760,6 @@ impl From<Content> for GridCell { } } -/// Function with common code to display a grid cell or table cell. -pub(crate) fn show_grid_cell( - mut body: Content, - inset: Smart<Sides<Option<Rel<Length>>>>, - align: Smart<Alignment>, -) -> SourceResult<Content> { - let inset = inset.unwrap_or_default().map(Option::unwrap_or_default); - - if inset != Sides::default() { - // Only pad if some inset is not 0pt. - // Avoids a bug where using .padded() in any way inside Show causes - // alignment in align(...) to break. - body = body.padded(inset); - } - - if let Smart::Custom(alignment) = align { - body = body.aligned(alignment); - } - - Ok(body) -} - /// A value that can be configured per cell. #[derive(Debug, Clone, PartialEq, Hash)] pub enum Celled<T> { diff --git a/crates/typst-library/src/layout/hide.rs b/crates/typst-library/src/layout/hide.rs index 5f3a5a2d..bb40447d 100644 --- a/crates/typst-library/src/layout/hide.rs +++ b/crates/typst-library/src/layout/hide.rs @@ -1,6 +1,4 @@ -use crate::diag::SourceResult; -use crate::engine::Engine; -use crate::foundations::{elem, Content, Packed, Show, StyleChain}; +use crate::foundations::{elem, Content}; /// Hides content without affecting layout. /// @@ -14,7 +12,7 @@ use crate::foundations::{elem, Content, Packed, Show, StyleChain}; /// Hello Jane \ /// #hide[Hello] Joe /// ``` -#[elem(Show)] +#[elem] pub struct HideElem { /// The content to hide. #[required] @@ -25,10 +23,3 @@ pub struct HideElem { #[ghost] pub hidden: bool, } - -impl Show for Packed<HideElem> { - #[typst_macros::time(name = "hide", span = self.span())] - fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(self.body.clone().set(HideElem::hidden, true)) - } -} diff --git a/crates/typst-library/src/layout/layout.rs b/crates/typst-library/src/layout/layout.rs index 46271ff2..00897bcf 100644 --- a/crates/typst-library/src/layout/layout.rs +++ b/crates/typst-library/src/layout/layout.rs @@ -1,13 +1,7 @@ -use comemo::Track; use typst_syntax::Span; -use crate::diag::SourceResult; -use crate::engine::Engine; -use crate::foundations::{ - dict, elem, func, Content, Context, Func, NativeElement, Packed, Show, StyleChain, -}; +use crate::foundations::{elem, func, Content, Func, NativeElement}; use crate::introspection::Locatable; -use crate::layout::{BlockElem, Size}; /// Provides access to the current outer container's (or page's, if none) /// dimensions (width and height). @@ -86,37 +80,9 @@ pub fn layout( } /// Executes a `layout` call. -#[elem(Locatable, Show)] -struct LayoutElem { +#[elem(Locatable)] +pub struct LayoutElem { /// The function to call with the outer container's (or page's) size. #[required] - func: Func, -} - -impl Show for Packed<LayoutElem> { - fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(BlockElem::multi_layouter( - self.clone(), - |elem, engine, locator, styles, regions| { - // Gets the current region's base size, which will be the size of the - // outer container, or of the page if there is no such container. - let Size { x, y } = regions.base(); - let loc = elem.location().unwrap(); - let context = Context::new(Some(loc), Some(styles)); - let result = elem - .func - .call( - engine, - context.track(), - [dict! { "width" => x, "height" => y }], - )? - .display(); - (engine.routines.layout_fragment)( - engine, &result, locator, styles, regions, - ) - }, - ) - .pack() - .spanned(self.span())) - } + pub func: Func, } diff --git a/crates/typst-library/src/layout/pad.rs b/crates/typst-library/src/layout/pad.rs index 1dc6d131..d533df35 100644 --- a/crates/typst-library/src/layout/pad.rs +++ b/crates/typst-library/src/layout/pad.rs @@ -1,7 +1,5 @@ -use crate::diag::SourceResult; -use crate::engine::Engine; -use crate::foundations::{elem, Content, NativeElement, Packed, Show, StyleChain}; -use crate::layout::{BlockElem, Length, Rel}; +use crate::foundations::{elem, Content}; +use crate::layout::{Length, Rel}; /// Adds spacing around content. /// @@ -16,7 +14,7 @@ use crate::layout::{BlockElem, Length, Rel}; /// _Typing speeds can be /// measured in words per minute._ /// ``` -#[elem(title = "Padding", Show)] +#[elem(title = "Padding")] pub struct PadElem { /// The padding at the left side. #[parse( @@ -55,11 +53,3 @@ pub struct PadElem { #[required] pub body: Content, } - -impl Show for Packed<PadElem> { - fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(BlockElem::multi_layouter(self.clone(), engine.routines.layout_pad) - .pack() - .spanned(self.span())) - } -} diff --git a/crates/typst-library/src/layout/repeat.rs b/crates/typst-library/src/layout/repeat.rs index 9579f185..a38d5f89 100644 --- a/crates/typst-library/src/layout/repeat.rs +++ b/crates/typst-library/src/layout/repeat.rs @@ -1,7 +1,5 @@ -use crate::diag::SourceResult; -use crate::engine::Engine; -use crate::foundations::{elem, Content, NativeElement, Packed, Show, StyleChain}; -use crate::layout::{BlockElem, Length}; +use crate::foundations::{elem, Content}; +use crate::layout::Length; /// Repeats content to the available space. /// @@ -24,7 +22,7 @@ use crate::layout::{BlockElem, Length}; /// Berlin, the 22nd of December, 2022 /// ] /// ``` -#[elem(Show)] +#[elem] pub struct RepeatElem { /// The content to repeat. #[required] @@ -39,11 +37,3 @@ pub struct RepeatElem { #[default(true)] pub justify: bool, } - -impl Show for Packed<RepeatElem> { - fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_repeat) - .pack() - .spanned(self.span())) - } -} diff --git a/crates/typst-library/src/layout/stack.rs b/crates/typst-library/src/layout/stack.rs index 5fc78480..fca1ecb8 100644 --- a/crates/typst-library/src/layout/stack.rs +++ b/crates/typst-library/src/layout/stack.rs @@ -1,9 +1,7 @@ use std::fmt::{self, Debug, Formatter}; -use crate::diag::SourceResult; -use crate::engine::Engine; -use crate::foundations::{cast, elem, Content, NativeElement, Packed, Show, StyleChain}; -use crate::layout::{BlockElem, Dir, Spacing}; +use crate::foundations::{cast, elem, Content}; +use crate::layout::{Dir, Spacing}; /// Arranges content and spacing horizontally or vertically. /// @@ -19,7 +17,7 @@ use crate::layout::{BlockElem, Dir, Spacing}; /// rect(width: 90pt), /// ) /// ``` -#[elem(Show)] +#[elem] pub struct StackElem { /// The direction along which the items are stacked. Possible values are: /// @@ -47,14 +45,6 @@ pub struct StackElem { pub children: Vec<StackChild>, } -impl Show for Packed<StackElem> { - fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(BlockElem::multi_layouter(self.clone(), engine.routines.layout_stack) - .pack() - .spanned(self.span())) - } -} - /// A child of a stack element. #[derive(Clone, PartialEq, Hash)] pub enum StackChild { diff --git a/crates/typst-library/src/layout/transform.rs b/crates/typst-library/src/layout/transform.rs index d153d97d..c2d9a21c 100644 --- a/crates/typst-library/src/layout/transform.rs +++ b/crates/typst-library/src/layout/transform.rs @@ -1,11 +1,5 @@ -use crate::diag::SourceResult; -use crate::engine::Engine; -use crate::foundations::{ - cast, elem, Content, NativeElement, Packed, Show, Smart, StyleChain, -}; -use crate::layout::{ - Abs, Alignment, Angle, BlockElem, HAlignment, Length, Ratio, Rel, VAlignment, -}; +use crate::foundations::{cast, elem, Content, Smart}; +use crate::layout::{Abs, Alignment, Angle, HAlignment, Length, Ratio, Rel, VAlignment}; /// Moves content without affecting layout. /// @@ -25,7 +19,7 @@ use crate::layout::{ /// ) /// )) /// ``` -#[elem(Show)] +#[elem] pub struct MoveElem { /// The horizontal displacement of the content. pub dx: Rel<Length>, @@ -38,14 +32,6 @@ pub struct MoveElem { pub body: Content, } -impl Show for Packed<MoveElem> { - fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_move) - .pack() - .spanned(self.span())) - } -} - /// Rotates content without affecting layout. /// /// Rotates an element by a given angle. The layout will act as if the element @@ -60,7 +46,7 @@ impl Show for Packed<MoveElem> { /// .map(i => rotate(24deg * i)[X]), /// ) /// ``` -#[elem(Show)] +#[elem] pub struct RotateElem { /// The amount of rotation. /// @@ -107,14 +93,6 @@ pub struct RotateElem { pub body: Content, } -impl Show for Packed<RotateElem> { - fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_rotate) - .pack() - .spanned(self.span())) - } -} - /// Scales content without affecting layout. /// /// Lets you mirror content by specifying a negative scale on a single axis. @@ -125,7 +103,7 @@ impl Show for Packed<RotateElem> { /// #scale(x: -100%)[This is mirrored.] /// #scale(x: -100%, reflow: true)[This is mirrored.] /// ``` -#[elem(Show)] +#[elem] pub struct ScaleElem { /// The scaling factor for both axes, as a positional argument. This is just /// an optional shorthand notation for setting `x` and `y` to the same @@ -179,14 +157,6 @@ pub struct ScaleElem { pub body: Content, } -impl Show for Packed<ScaleElem> { - fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_scale) - .pack() - .spanned(self.span())) - } -} - /// To what size something shall be scaled. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum ScaleAmount { @@ -215,7 +185,7 @@ cast! { /// This is some fake italic text. /// ] /// ``` -#[elem(Show)] +#[elem] pub struct SkewElem { /// The horizontal skewing angle. /// @@ -265,14 +235,6 @@ pub struct SkewElem { pub body: Content, } -impl Show for Packed<SkewElem> { - fn show(&self, engine: &mut Engine, _: StyleChain) -> SourceResult<Content> { - Ok(BlockElem::single_layouter(self.clone(), engine.routines.layout_skew) - .pack() - .spanned(self.span())) - } -} - /// A scale-skew-translate transformation. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub struct Transform { |
