summaryrefslogtreecommitdiff
path: root/src/library/page.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-12-05 12:54:03 +0100
committerLaurenz <laurmaedje@gmail.com>2021-12-05 12:54:03 +0100
commit26bdc1f0f6fe8113d7fcfb4d5aca46aa5238ccd8 (patch)
tree4c12a187032501735d858648a64fe66603f106a6 /src/library/page.rs
parent738ff7e1f573bef678932b313be9969a17af8d22 (diff)
Set Rules Episode I: The Phantom Style
Diffstat (limited to 'src/library/page.rs')
-rw-r--r--src/library/page.rs99
1 files changed, 46 insertions, 53 deletions
diff --git a/src/library/page.rs b/src/library/page.rs
index 0d29ddb6..9def5400 100644
--- a/src/library/page.rs
+++ b/src/library/page.rs
@@ -1,4 +1,5 @@
use super::prelude::*;
+use super::PadNode;
use crate::style::{Paper, PaperClass};
/// `page`: Configure pages.
@@ -20,90 +21,82 @@ pub fn page(ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let bottom = args.named("bottom")?;
let fill = args.named("fill")?;
- ctx.template.modify(move |style| {
- let page = style.page_mut();
+ let page = ctx.style.page_mut();
- if let Some(paper) = paper {
- page.class = paper.class();
- page.size = paper.size();
- }
-
- if let Some(width) = width {
- page.class = PaperClass::Custom;
- page.size.x = width.unwrap_or(Length::inf());
- }
+ if let Some(paper) = paper {
+ page.class = paper.class();
+ page.size = paper.size();
+ }
- if let Some(height) = height {
- page.class = PaperClass::Custom;
- page.size.y = height.unwrap_or(Length::inf());
- }
+ if let Some(width) = width {
+ page.class = PaperClass::Custom;
+ page.size.x = width.unwrap_or(Length::inf());
+ }
- if flip.unwrap_or(false) {
- std::mem::swap(&mut page.size.x, &mut page.size.y);
- }
+ if let Some(height) = height {
+ page.class = PaperClass::Custom;
+ page.size.y = height.unwrap_or(Length::inf());
+ }
- if let Some(margins) = margins {
- page.margins = Sides::splat(margins);
- }
+ if flip.unwrap_or(false) {
+ std::mem::swap(&mut page.size.x, &mut page.size.y);
+ }
- if let Some(left) = left {
- page.margins.left = left;
- }
+ if let Some(margins) = margins {
+ page.margins = Sides::splat(margins);
+ }
- if let Some(top) = top {
- page.margins.top = top;
- }
+ if let Some(left) = left {
+ page.margins.left = left;
+ }
- if let Some(right) = right {
- page.margins.right = right;
- }
+ if let Some(top) = top {
+ page.margins.top = top;
+ }
- if let Some(bottom) = bottom {
- page.margins.bottom = bottom;
- }
+ if let Some(right) = right {
+ page.margins.right = right;
+ }
- if let Some(fill) = fill {
- page.fill = fill;
- }
- });
+ if let Some(bottom) = bottom {
+ page.margins.bottom = bottom;
+ }
- ctx.template.pagebreak(false);
+ if let Some(fill) = fill {
+ page.fill = fill;
+ }
Ok(Value::None)
}
/// `pagebreak`: Start a new page.
pub fn pagebreak(_: &mut EvalContext, _: &mut Args) -> TypResult<Value> {
- let mut template = Template::new();
- template.pagebreak(true);
- Ok(Value::Template(template))
+ Ok(Value::Node(Node::Pagebreak))
}
/// Layouts its children onto one or multiple pages.
#[derive(Debug, Hash)]
-pub struct PageNode {
- /// The size of the page.
- pub size: Size,
- /// The background fill.
- pub fill: Option<Paint>,
- /// The node that produces the actual pages.
- pub child: PackedNode,
-}
+pub struct PageNode(pub PackedNode);
impl PageNode {
/// Layout the page run into a sequence of frames, one per page.
pub fn layout(&self, ctx: &mut LayoutContext) -> Vec<Rc<Frame>> {
+ // TODO(set): Get style from styles.
+ let style = crate::style::PageStyle::default();
+
// When one of the lengths is infinite the page fits its content along
// that axis.
- let expand = self.size.map(Length::is_finite);
- let regions = Regions::repeat(self.size, self.size, expand);
+ let expand = style.size.map(Length::is_finite);
+ let regions = Regions::repeat(style.size, style.size, expand);
// Layout the child.
+ let padding = style.margins();
+ let padded = PadNode { child: self.0.clone(), padding }.pack();
let mut frames: Vec<_> =
- self.child.layout(ctx, &regions).into_iter().map(|c| c.item).collect();
+ padded.layout(ctx, &regions).into_iter().map(|c| c.item).collect();
// Add background fill if requested.
- if let Some(fill) = self.fill {
+ if let Some(fill) = style.fill {
for frame in &mut frames {
let shape = Shape::filled(Geometry::Rect(frame.size), fill);
Rc::make_mut(frame).prepend(Point::zero(), Element::Shape(shape));