summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-11-18 15:10:11 +0100
committerLaurenz <laurmaedje@gmail.com>2019-11-18 15:10:11 +0100
commit1a6fb48bc5e95d0a9ef243ab62517557189c0eea (patch)
tree06f704fe638d5ae25f4976874500c5da75316348 /src/library
parent1eb25f86dd6763c4f2d7e60b6d09af60ada50af6 (diff)
Page style modification functions 📜
- `page.size` - `page.margins`
Diffstat (limited to 'src/library')
-rw-r--r--src/library/mod.rs5
-rw-r--r--src/library/page.rs79
-rw-r--r--src/library/spacing.rs12
-rw-r--r--src/library/style.rs8
4 files changed, 88 insertions, 16 deletions
diff --git a/src/library/mod.rs b/src/library/mod.rs
index d795d488..5fa326c8 100644
--- a/src/library/mod.rs
+++ b/src/library/mod.rs
@@ -6,6 +6,7 @@ pub_use_mod!(boxed);
pub_use_mod!(axes);
pub_use_mod!(spacing);
pub_use_mod!(style);
+pub_use_mod!(page);
/// Create a scope with all standard functions.
pub fn std() -> Scope {
@@ -19,7 +20,6 @@ pub fn std() -> Scope {
std.add::<LineBreak>("line.break");
std.add::<ParagraphBreak>("paragraph.break");
std.add::<PageBreak>("page.break");
-
std.add::<HorizontalSpace>("h");
std.add::<VerticalSpace>("v");
@@ -27,5 +27,8 @@ pub fn std() -> Scope {
std.add::<Italic>("italic");
std.add::<Monospace>("mono");
+ std.add::<PageSize>("page.size");
+ std.add::<PageMargins>("page.margins");
+
std
}
diff --git a/src/library/page.rs b/src/library/page.rs
new file mode 100644
index 00000000..4efcbea0
--- /dev/null
+++ b/src/library/page.rs
@@ -0,0 +1,79 @@
+use crate::func::prelude::*;
+
+/// `page.break`: Ends the current page.
+#[derive(Debug, PartialEq)]
+pub struct PageBreak;
+
+function! {
+ data: PageBreak,
+ parse: plain,
+ layout(_, _) { Ok(commands![FinishLayout]) }
+}
+
+/// `page.size`: Set the size of pages.
+#[derive(Debug, PartialEq)]
+pub struct PageSize {
+ width: Option<Size>,
+ height: Option<Size>,
+}
+
+function! {
+ data: PageSize,
+
+ parse(args, body, _ctx) {
+ parse!(forbidden: body);
+ Ok(PageSize {
+ width: args.get_key_opt::<ArgSize>("width")?.map(|a| a.val),
+ height: args.get_key_opt::<ArgSize>("height")?.map(|a| a.val),
+ })
+ }
+
+ layout(this, ctx) {
+ let mut style = ctx.page_style;
+
+ if let Some(width) = this.width { style.dimensions.x = width; }
+ if let Some(height) = this.height { style.dimensions.y = height; }
+
+ Ok(commands![SetPageStyle(style)])
+ }
+}
+
+/// `page.margins`: Set the margins of pages.
+#[derive(Debug, PartialEq)]
+pub struct PageMargins {
+ left: Option<Size>,
+ top: Option<Size>,
+ right: Option<Size>,
+ bottom: Option<Size>,
+}
+
+function! {
+ data: PageMargins,
+
+ parse(args, body, _ctx) {
+ parse!(forbidden: body);
+ let default = args.get_pos_opt::<ArgSize>()?;
+ let mut get = |which| {
+ args.get_key_opt::<ArgSize>(which)
+ .map(|size| size.or(default).map(|a| a.val))
+ };
+
+ Ok(PageMargins {
+ left: get("left")?,
+ top: get("top")?,
+ right: get("right")?,
+ bottom: get("bottom")?,
+ })
+ }
+
+ layout(this, ctx) {
+ let mut style = ctx.page_style;
+
+ if let Some(left) = this.left { style.margins.left = left; }
+ if let Some(top) = this.top { style.margins.top = top; }
+ if let Some(right) = this.right { style.margins.right = right; }
+ if let Some(bottom) = this.bottom { style.margins.bottom = bottom; }
+
+ Ok(commands![SetPageStyle(style)])
+ }
+}
diff --git a/src/library/spacing.rs b/src/library/spacing.rs
index afd26d80..f83f333d 100644
--- a/src/library/spacing.rs
+++ b/src/library/spacing.rs
@@ -22,16 +22,6 @@ function! {
layout(_, _) { Ok(commands![FinishBox]) }
}
-/// `page.break`: Ends the current page.
-#[derive(Debug, PartialEq)]
-pub struct PageBreak;
-
-function! {
- data: PageBreak,
- parse: plain,
- layout(_, _) { Ok(commands![FinishLayout]) }
-}
-
macro_rules! space_func {
($ident:ident, $doc:expr, $var:ident => $command:expr) => (
#[doc = $doc]
@@ -57,7 +47,7 @@ macro_rules! space_func {
layout(this, ctx) {
let $var = match this.0 {
Spacing::Absolute(s) => s,
- Spacing::Relative(f) => f * ctx.style.font_size,
+ Spacing::Relative(f) => f * ctx.text_style.font_size,
};
Ok(commands![$command])
diff --git a/src/library/style.rs b/src/library/style.rs
index 0615c0e7..a63166cf 100644
--- a/src/library/style.rs
+++ b/src/library/style.rs
@@ -18,16 +18,16 @@ macro_rules! stylefunc {
}
layout(this, ctx) {
- let mut style = ctx.style.clone();
+ let mut style = ctx.text_style.clone();
style.toggle_class(FontClass::$ident);
Ok(match &this.body {
Some(body) => commands![
- SetStyle(style),
+ SetTextStyle(style),
LayoutTree(body),
- SetStyle(ctx.style.clone()),
+ SetTextStyle(ctx.text_style.clone()),
],
- None => commands![SetStyle(style)]
+ None => commands![SetTextStyle(style)]
})
}
}