diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-02 14:48:51 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-02 14:48:51 +0100 |
| commit | 56342bd972a13ffe21beaf2b87ab7eb1597704b4 (patch) | |
| tree | 78f9549141e753dde4a938670c54f3fe8695a058 /src/library/mod.rs | |
| parent | 37ac5d966ebaf97ac79c507028cd5b742b510b89 (diff) | |
Move layout traits into library
Diffstat (limited to 'src/library/mod.rs')
| -rw-r--r-- | src/library/mod.rs | 200 |
1 files changed, 11 insertions, 189 deletions
diff --git a/src/library/mod.rs b/src/library/mod.rs index d75e2459..184c515e 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -11,7 +11,14 @@ pub mod structure; pub mod text; pub mod utility; -use prelude::*; +mod ext; +mod raw; + +pub use raw::*; + +use crate::geom::{Align, Color, Dir}; +use crate::model::{Node, Scope}; +use crate::LangItems; /// Construct a scope containing all standard library definitions. pub fn scope() -> Scope { @@ -156,10 +163,10 @@ pub fn items() -> LangItems { strong: |body| text::StrongNode(body).pack(), emph: |body| text::EmphNode(body).pack(), raw: |text, lang, block| { - let node = text::RawNode { text, block }.pack(); + let content = text::RawNode { text, block }.pack(); match lang { - Some(_) => node.styled(text::RawNode::LANG, lang), - None => node, + Some(_) => content.styled(text::RawNode::LANG, lang), + None => content, } }, link: |url| text::LinkNode::from_url(url).pack(), @@ -174,188 +181,3 @@ pub fn items() -> LangItems { }, } } - -/// Additional methods on content. -pub trait ContentExt { - /// Make this content strong. - fn strong(self) -> Self; - - /// Make this content emphasized. - fn emph(self) -> Self; - - /// Underline this content. - fn underlined(self) -> Self; - - /// Add weak vertical spacing above and below the content. - fn spaced(self, above: Option<Abs>, below: Option<Abs>) -> Self; - - /// Force a size for this node. - fn boxed(self, sizing: Axes<Option<Rel<Length>>>) -> Self; - - /// Set alignments for this node. - fn aligned(self, aligns: Axes<Option<RawAlign>>) -> Self; - - /// Pad this node at the sides. - fn padded(self, padding: Sides<Rel<Length>>) -> Self; - - /// Transform this node's contents without affecting layout. - fn moved(self, delta: Axes<Rel<Length>>) -> Self; - - /// Fill the frames resulting from a node. - fn filled(self, fill: Paint) -> Self; - - /// Stroke the frames resulting from a node. - fn stroked(self, stroke: Stroke) -> Self; -} - -impl ContentExt for Content { - fn strong(self) -> Self { - text::StrongNode(self).pack() - } - - fn emph(self) -> Self { - text::EmphNode(self).pack() - } - - fn underlined(self) -> Self { - text::DecoNode::<{ text::UNDERLINE }>(self).pack() - } - - fn spaced(self, above: Option<Abs>, below: Option<Abs>) -> Self { - if above.is_none() && below.is_none() { - return self; - } - - let mut seq = vec![]; - if let Some(above) = above { - seq.push( - layout::VNode { - amount: above.into(), - weak: true, - generated: true, - } - .pack(), - ); - } - - seq.push(self); - if let Some(below) = below { - seq.push( - layout::VNode { - amount: below.into(), - weak: true, - generated: true, - } - .pack(), - ); - } - - Self::sequence(seq) - } - - fn boxed(self, sizing: Axes<Option<Rel<Length>>>) -> Self { - layout::BoxNode { sizing, child: self }.pack() - } - - fn aligned(self, aligns: Axes<Option<RawAlign>>) -> Self { - layout::AlignNode { aligns, child: self }.pack() - } - - fn padded(self, padding: Sides<Rel<Length>>) -> Self { - layout::PadNode { padding, child: self }.pack() - } - - fn moved(self, delta: Axes<Rel<Length>>) -> Self { - layout::MoveNode { delta, child: self }.pack() - } - - fn filled(self, fill: Paint) -> Self { - FillNode { fill, child: self }.pack() - } - - fn stroked(self, stroke: Stroke) -> Self { - StrokeNode { stroke, child: self }.pack() - } -} - -/// Additional methods for the style chain. -pub trait StyleMapExt { - /// Set a font family composed of a preferred family and existing families - /// from a style chain. - fn set_family(&mut self, preferred: text::FontFamily, existing: StyleChain); -} - -impl StyleMapExt for StyleMap { - fn set_family(&mut self, preferred: text::FontFamily, existing: StyleChain) { - self.set( - text::TextNode::FAMILY, - std::iter::once(preferred) - .chain(existing.get(text::TextNode::FAMILY).iter().cloned()) - .collect(), - ); - } -} - -/// Fill the frames resulting from a node. -#[derive(Debug, Hash)] -struct FillNode { - /// How to fill the frames resulting from the `child`. - fill: Paint, - /// The node whose frames should be filled. - child: Content, -} - -#[node(Layout)] -impl FillNode {} - -impl Layout for FillNode { - fn layout( - &self, - world: Tracked<dyn World>, - regions: &Regions, - styles: StyleChain, - ) -> SourceResult<Vec<Frame>> { - let mut frames = self.child.layout_block(world, regions, styles)?; - for frame in &mut frames { - let shape = Geometry::Rect(frame.size()).filled(self.fill); - frame.prepend(Point::zero(), Element::Shape(shape)); - } - Ok(frames) - } - - fn level(&self) -> Level { - Level::Block - } -} - -/// Stroke the frames resulting from a node. -#[derive(Debug, Hash)] -struct StrokeNode { - /// How to stroke the frames resulting from the `child`. - stroke: Stroke, - /// The node whose frames should be stroked. - child: Content, -} - -#[node(Layout)] -impl StrokeNode {} - -impl Layout for StrokeNode { - fn layout( - &self, - world: Tracked<dyn World>, - regions: &Regions, - styles: StyleChain, - ) -> SourceResult<Vec<Frame>> { - let mut frames = self.child.layout_block(world, regions, styles)?; - for frame in &mut frames { - let shape = Geometry::Rect(frame.size()).stroked(self.stroke); - frame.prepend(Point::zero(), Element::Shape(shape)); - } - Ok(frames) - } - - fn level(&self) -> Level { - Level::Block - } -} |
