summaryrefslogtreecommitdiff
path: root/src/library/page.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-01-24 12:44:04 +0100
committerLaurenz <laurmaedje@gmail.com>2020-01-24 12:44:04 +0100
commit03fddaf3aea778057aedd74dbcb27bae971ec22f (patch)
tree37e3136e29e0e5d69ec8f56e43d156739d2931ab /src/library/page.rs
parent78da2bdd5d77d1b8572e5e9da119bfa68127a3fa (diff)
Non-fatal argument parsing 🌋
Diffstat (limited to 'src/library/page.rs')
-rw-r--r--src/library/page.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/library/page.rs b/src/library/page.rs
new file mode 100644
index 00000000..25f81bc1
--- /dev/null
+++ b/src/library/page.rs
@@ -0,0 +1,64 @@
+use crate::style::{Paper, PaperClass};
+use super::*;
+
+
+function! {
+ /// `page.size`: Set the size of pages.
+ #[derive(Debug, Clone, PartialEq)]
+ pub struct PageSizeFunc {
+ paper: Option<Paper>,
+ extents: AxisMap<Size>,
+ flip: bool,
+ }
+
+ parse(header, body, ctx, errors, decos) {
+ body!(nope: body, errors);
+ PageSizeFunc {
+ paper: header.args.pos.get::<Paper>(errors),
+ extents: AxisMap::parse::<ExtentKey, Size>(errors, &mut header.args.key),
+ flip: header.args.key.get::<bool>(errors, "flip").unwrap_or(false),
+ }
+ }
+
+ layout(self, ctx, errors) {
+ let mut style = ctx.style.page;
+
+ if let Some(paper) = self.paper {
+ style.class = paper.class;
+ style.dimensions = paper.dimensions;
+ } else {
+ style.class = PaperClass::Custom;
+ }
+
+ let map = self.extents.dedup(errors, ctx.axes);
+ map.with(Horizontal, |&width| style.dimensions.x = width);
+ map.with(Vertical, |&height| style.dimensions.y = height);
+
+ if self.flip {
+ style.dimensions.swap();
+ }
+
+ vec![SetPageStyle(style)]
+ }
+}
+
+function! {
+ /// `page.margins`: Sets the page margins.
+ #[derive(Debug, Clone, PartialEq)]
+ pub struct PageMarginsFunc {
+ padding: PaddingMap,
+ }
+
+ parse(header, body, ctx, errors, decos) {
+ body!(nope: body, errors);
+ PageMarginsFunc {
+ padding: PaddingMap::parse(errors, &mut header.args),
+ }
+ }
+
+ layout(self, ctx, errors) {
+ let mut style = ctx.style.page;
+ self.padding.apply(errors, ctx.axes, &mut style.margins);
+ vec![SetPageStyle(style)]
+ }
+}