diff options
Diffstat (limited to 'library/src/layout')
| -rw-r--r-- | library/src/layout/align.rs | 7 | ||||
| -rw-r--r-- | library/src/layout/columns.rs | 14 | ||||
| -rw-r--r-- | library/src/layout/container.rs | 14 | ||||
| -rw-r--r-- | library/src/layout/grid.rs | 7 | ||||
| -rw-r--r-- | library/src/layout/hide.rs | 7 | ||||
| -rw-r--r-- | library/src/layout/pad.rs | 7 | ||||
| -rw-r--r-- | library/src/layout/page.rs | 14 | ||||
| -rw-r--r-- | library/src/layout/par.rs | 12 | ||||
| -rw-r--r-- | library/src/layout/place.rs | 7 | ||||
| -rw-r--r-- | library/src/layout/repeat.rs | 7 | ||||
| -rw-r--r-- | library/src/layout/spacing.rs | 14 | ||||
| -rw-r--r-- | library/src/layout/stack.rs | 7 | ||||
| -rw-r--r-- | library/src/layout/transform.rs | 129 |
13 files changed, 149 insertions, 97 deletions
diff --git a/library/src/layout/align.rs b/library/src/layout/align.rs index 5426f5e2..9fcb216f 100644 --- a/library/src/layout/align.rs +++ b/library/src/layout/align.rs @@ -1,16 +1,17 @@ use crate::prelude::*; +/// # Align /// Align content horizontally and vertically. /// -/// # Parameters +/// ## Parameters /// - body: Content (positional, required) /// The content to align. /// /// - alignment: Axes<Option<GenAlign>> (positional, settable) /// The alignment along both axes. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable] #[derive(Debug, Hash)] diff --git a/library/src/layout/columns.rs b/library/src/layout/columns.rs index 6154cd14..e8ae5f35 100644 --- a/library/src/layout/columns.rs +++ b/library/src/layout/columns.rs @@ -1,17 +1,18 @@ use crate::prelude::*; use crate::text::TextNode; +/// # Columns /// Separate a region into multiple equally sized columns. /// -/// # Parameters +/// ## Parameters /// - count: usize (positional, required) /// The number of columns. /// /// - body: Content (positional, required) /// The content that should be layouted into the columns. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout)] #[derive(Debug, Hash)] @@ -112,15 +113,16 @@ impl Layout for ColumnsNode { } } +/// # Column Break /// A column break. /// -/// # Parameters +/// ## Parameters /// - weak: bool (named) /// If true, the column break is skipped if the current column is already /// empty. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Behave)] #[derive(Debug, Hash)] diff --git a/library/src/layout/container.rs b/library/src/layout/container.rs index 5df6b2dc..933deeba 100644 --- a/library/src/layout/container.rs +++ b/library/src/layout/container.rs @@ -2,9 +2,10 @@ use super::VNode; use crate::layout::Spacing; use crate::prelude::*; +/// # Box /// An inline-level container that sizes content. /// -/// # Parameters +/// ## Parameters /// - body: Content (positional) /// The contents of the box. /// @@ -14,8 +15,8 @@ use crate::prelude::*; /// - height: Rel<Length> (named) /// The height of the box. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout, Inline)] #[derive(Debug, Hash)] @@ -75,9 +76,10 @@ impl Layout for BoxNode { impl Inline for BoxNode {} +/// # Block /// A block-level container that places content into a separate flow. /// -/// # Parameters +/// ## Parameters /// - body: Content (positional) /// The contents of the block. /// @@ -92,8 +94,8 @@ impl Inline for BoxNode {} /// The spacing between this block and the following one. Takes precedence /// over `spacing`. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout)] #[derive(Debug, Hash)] diff --git a/library/src/layout/grid.rs b/library/src/layout/grid.rs index 1cf95001..eb45cfbe 100644 --- a/library/src/layout/grid.rs +++ b/library/src/layout/grid.rs @@ -2,9 +2,10 @@ use crate::prelude::*; use super::Spacing; +/// # Grid /// Arrange content in a grid. /// -/// # Parameters +/// ## Parameters /// - cells: Content (positional, variadic) /// The contents of the table cells. /// @@ -23,8 +24,8 @@ use super::Spacing; /// - row-gutter: TrackSizings (named) /// Defines the gaps between rows. Takes precedence over `gutter`. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout)] #[derive(Debug, Hash)] diff --git a/library/src/layout/hide.rs b/library/src/layout/hide.rs index fc9150c1..f7f19549 100644 --- a/library/src/layout/hide.rs +++ b/library/src/layout/hide.rs @@ -1,13 +1,14 @@ use crate::prelude::*; +/// # Hide /// Hide content without affecting layout. /// -/// # Parameters +/// ## Parameters /// - body: Content (positional, required) /// The content to hide. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout, Inline)] #[derive(Debug, Hash)] diff --git a/library/src/layout/pad.rs b/library/src/layout/pad.rs index 1326ae22..7825d133 100644 --- a/library/src/layout/pad.rs +++ b/library/src/layout/pad.rs @@ -1,8 +1,9 @@ use crate::prelude::*; +/// # Padding /// Pad content at the sides. /// -/// # Parameters +/// ## Parameters /// - body: Content (positional, required) /// The content to pad at the sides. /// @@ -27,8 +28,8 @@ use crate::prelude::*; /// - rest: Rel<Length> (named) /// The padding for all sides. All other parameters take precedence over this. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout)] #[derive(Debug, Hash)] diff --git a/library/src/layout/page.rs b/library/src/layout/page.rs index a2548c02..dfaaae23 100644 --- a/library/src/layout/page.rs +++ b/library/src/layout/page.rs @@ -4,17 +4,18 @@ use super::ColumnsNode; use crate::prelude::*; use crate::text::TextNode; +/// # Page /// Layouts its child onto one or multiple pages. /// -/// # Parameters +/// ## Parameters /// - body: Content (positional, required) /// The contents of the page(s). /// /// - paper: Paper (positional, settable) /// The paper size. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable] #[derive(Clone, Hash)] @@ -154,14 +155,15 @@ impl Debug for PageNode { } } +/// # Page Break /// A page break. /// -/// # Parameters +/// ## Parameters /// - weak: bool (named) /// If true, the page break is skipped if the current page is already empty. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable] #[derive(Debug, Copy, Clone, Hash)] diff --git a/library/src/layout/par.rs b/library/src/layout/par.rs index f9731ff2..bdd8c9d5 100644 --- a/library/src/layout/par.rs +++ b/library/src/layout/par.rs @@ -11,14 +11,15 @@ use crate::text::{ shape, LinebreakNode, Quoter, Quotes, ShapedText, SmartQuoteNode, SpaceNode, TextNode, }; +/// # Paragraph /// Arrange text, spacing and inline-level nodes into a paragraph. /// -/// # Parameters +/// ## Parameters /// - body: Content (positional, required) /// The contents of the paragraph. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable] #[derive(Hash)] @@ -147,10 +148,11 @@ castable! { "optimized" => Self::Optimized, } +/// # Paragraph Break /// A paragraph break. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Unlabellable)] #[derive(Debug, Hash)] diff --git a/library/src/layout/place.rs b/library/src/layout/place.rs index 8baf69af..ed3d71bf 100644 --- a/library/src/layout/place.rs +++ b/library/src/layout/place.rs @@ -1,8 +1,9 @@ use crate::prelude::*; +/// # Place /// Place content at an absolute position. /// -/// # Parameters +/// ## Parameters /// - alignment: Axes<Option<GenAlign>> (positional) /// Relative to which position in the parent container to place the content. /// @@ -15,8 +16,8 @@ use crate::prelude::*; /// - dy: Rel<Length> (named) /// The vertical displacement of the placed content. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout, Behave)] #[derive(Debug, Hash)] diff --git a/library/src/layout/repeat.rs b/library/src/layout/repeat.rs index fa87d922..864454c4 100644 --- a/library/src/layout/repeat.rs +++ b/library/src/layout/repeat.rs @@ -1,13 +1,14 @@ use crate::prelude::*; +/// # Repeat /// Repeats content to fill a line. /// -/// # Parameters +/// ## Parameters /// - body: Content (positional, required) /// The content to repeat. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout, Inline)] #[derive(Debug, Hash)] diff --git a/library/src/layout/spacing.rs b/library/src/layout/spacing.rs index 092d2c0a..f306d47c 100644 --- a/library/src/layout/spacing.rs +++ b/library/src/layout/spacing.rs @@ -2,9 +2,10 @@ use std::cmp::Ordering; use crate::prelude::*; +/// # Spacing (H) /// Horizontal spacing in a paragraph. /// -/// # Parameters +/// ## Parameters /// - amount: Spacing (positional, required) /// How much spacing to insert. /// @@ -13,8 +14,8 @@ use crate::prelude::*; /// Moreover, from multiple adjacent weak spacings all but the largest one /// collapse. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Behave)] #[derive(Debug, Copy, Clone, Hash)] @@ -63,9 +64,10 @@ impl Behave for HNode { } } +/// # Spacing (V) /// Vertical spacing. /// -/// # Parameters +/// ## Parameters /// - amount: Spacing (positional, required) /// How much spacing to insert. /// @@ -74,8 +76,8 @@ impl Behave for HNode { /// Moreover, from multiple adjacent weak spacings all but the largest one /// collapse. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Behave)] #[derive(Debug, Copy, Clone, Hash, PartialEq, PartialOrd)] diff --git a/library/src/layout/stack.rs b/library/src/layout/stack.rs index 2683175a..f83f4e41 100644 --- a/library/src/layout/stack.rs +++ b/library/src/layout/stack.rs @@ -3,9 +3,10 @@ use typst::model::StyledNode; use super::{AlignNode, Spacing}; use crate::prelude::*; +/// # Stack /// Arrange content and spacing along an axis. /// -/// # Parameters +/// ## Parameters /// - items: StackChild (positional, variadic) /// The items to stack along an axis. /// @@ -15,8 +16,8 @@ use crate::prelude::*; /// - spacing: Spacing (named) /// Spacing to insert between items where no explicit spacing was provided. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout)] #[derive(Debug, Hash)] diff --git a/library/src/layout/transform.rs b/library/src/layout/transform.rs index b509607d..6d2caf1d 100644 --- a/library/src/layout/transform.rs +++ b/library/src/layout/transform.rs @@ -2,9 +2,10 @@ use typst::geom::Transform; use crate::prelude::*; +/// # Move /// Move content without affecting layout. /// -/// # Parameters +/// ## Parameters /// - body: Content (positional, required) /// The content to move. /// @@ -14,8 +15,8 @@ use crate::prelude::*; /// - dy: Rel<Length> (named) /// The vertical displacement of the content. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout, Inline)] #[derive(Debug, Hash)] @@ -58,64 +59,109 @@ impl Layout for MoveNode { impl Inline for MoveNode {} -/// Transform content without affecting layout. +/// # Rotate +/// Rotate content with affecting layout. /// -/// # Parameters +/// ## Parameters /// - body: Content (positional, required) -/// The content to transform. +/// The content to rotate. /// /// - angle: Angle (named) /// The amount of rotation. /// +/// ## Category +/// layout +#[func] +#[capable(Layout, Inline)] +#[derive(Debug, Hash)] +pub struct RotateNode { + /// The angle by which to rotate the node. + pub angle: Angle, + /// The content that should be rotated. + pub body: Content, +} + +#[node] +impl RotateNode { + /// The origin of the rotation. + #[property(resolve)] + pub const ORIGIN: Axes<Option<GenAlign>> = Axes::default(); + + fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> { + Ok(Self { + angle: args.named_or_find("angle")?.unwrap_or_default(), + body: args.expect("body")?, + } + .pack()) + } +} + +impl Layout for RotateNode { + fn layout( + &self, + vt: &mut Vt, + styles: StyleChain, + regions: Regions, + ) -> SourceResult<Fragment> { + let mut fragment = self.body.layout(vt, styles, regions)?; + for frame in &mut fragment { + let origin = styles.get(Self::ORIGIN).unwrap_or(Align::CENTER_HORIZON); + let Axes { x, y } = origin.zip(frame.size()).map(|(o, s)| o.position(s)); + let transform = Transform::translate(x, y) + .pre_concat(Transform::rotate(self.angle)) + .pre_concat(Transform::translate(-x, -y)); + frame.transform(transform); + } + Ok(fragment) + } +} + +impl Inline for RotateNode {} + +/// # Scale +/// Scale content without affecting layout. +/// +/// ## Parameters +/// - body: Content (positional, required) +/// The content to scale. +/// /// - x: Ratio (named) /// The horizontal scaling factor. /// /// - y: Ratio (named) /// The vertical scaling factor. /// -/// # Tags -/// - layout +/// ## Category +/// layout #[func] #[capable(Layout, Inline)] #[derive(Debug, Hash)] -pub struct TransformNode<const T: TransformKind> { - /// Transformation to apply to the content. - pub transform: Transform, - /// The content that should be transformed. +pub struct ScaleNode { + /// Scaling factor. + pub factor: Axes<Ratio>, + /// The content that should be scaled. pub body: Content, } -/// Rotate content without affecting layout. -pub type RotateNode = TransformNode<ROTATE>; - -/// Scale content without affecting layout. -pub type ScaleNode = TransformNode<SCALE>; - #[node] -impl<const T: TransformKind> TransformNode<T> { +impl ScaleNode { /// The origin of the transformation. #[property(resolve)] pub const ORIGIN: Axes<Option<GenAlign>> = Axes::default(); fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> { - let transform = match T { - ROTATE => { - let angle = args.named_or_find("angle")?.unwrap_or_default(); - Transform::rotate(angle) - } - SCALE | _ => { - let all = args.find()?; - let sx = args.named("x")?.or(all).unwrap_or(Ratio::one()); - let sy = args.named("y")?.or(all).unwrap_or(Ratio::one()); - Transform::scale(sx, sy) - } - }; - - Ok(Self { transform, body: args.expect("body")? }.pack()) + let all = args.find()?; + let x = args.named("x")?.or(all).unwrap_or(Ratio::one()); + let y = args.named("y")?.or(all).unwrap_or(Ratio::one()); + Ok(Self { + factor: Axes::new(x, y), + body: args.expect("body")?, + } + .pack()) } } -impl<const T: TransformKind> Layout for TransformNode<T> { +impl Layout for ScaleNode { fn layout( &self, vt: &mut Vt, @@ -127,7 +173,7 @@ impl<const T: TransformKind> Layout for TransformNode<T> { let origin = styles.get(Self::ORIGIN).unwrap_or(Align::CENTER_HORIZON); let Axes { x, y } = origin.zip(frame.size()).map(|(o, s)| o.position(s)); let transform = Transform::translate(x, y) - .pre_concat(self.transform) + .pre_concat(Transform::scale(self.factor.x, self.factor.y)) .pre_concat(Transform::translate(-x, -y)); frame.transform(transform); } @@ -135,15 +181,4 @@ impl<const T: TransformKind> Layout for TransformNode<T> { } } -impl<const T: TransformKind> Inline for TransformNode<T> {} - -/// Kinds of transformations. -/// -/// The move transformation is handled separately. -pub type TransformKind = usize; - -/// A rotational transformation. -const ROTATE: TransformKind = 1; - -/// A scale transformation. -const SCALE: TransformKind = 2; +impl Inline for ScaleNode {} |
