summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-01-01 17:15:21 +0100
committerLaurenz <laurmaedje@gmail.com>2020-01-01 17:15:21 +0100
commit7de92193212b655dca01217bd798b6566a438249 (patch)
treec4a3c64f3f65e264ec62ed0f35ec3d8eead63726
parent63b8ccba955c1d1e274f377c6db9c65dd719ae62 (diff)
Add word|line|par.spacing functions 💶
-rw-r--r--src/library/mod.rs54
-rw-r--r--src/size.rs5
2 files changed, 56 insertions, 3 deletions
diff --git a/src/library/mod.rs b/src/library/mod.rs
index c5a5a796..9c7cc7ce 100644
--- a/src/library/mod.rs
+++ b/src/library/mod.rs
@@ -24,6 +24,11 @@ pub fn std() -> Scope {
std.add::<LineBreak>("line.break");
std.add::<ParBreak>("par.break");
std.add::<PageBreak>("page.break");
+
+ std.add_with_metadata::<ContentSpacing>("word.spacing", ContentKind::Word);
+ std.add_with_metadata::<ContentSpacing>("line.spacing", ContentKind::Line);
+ std.add_with_metadata::<ContentSpacing>("par.spacing", ContentKind::Paragraph);
+
std.add::<PageSize>("page.size");
std.add::<PageMargins>("page.margins");
@@ -70,10 +75,48 @@ function! {
}
function! {
+ /// `word.spacing`, `line.spacing`, `par.spacing`: The spacing between
+ /// words, lines or paragraphs as a multiple of the font size.
+ #[derive(Debug, PartialEq)]
+ pub struct ContentSpacing {
+ spacing: f32,
+ content: ContentKind,
+ }
+
+ type Meta = ContentKind;
+
+ parse(args, body, _, meta) {
+ parse!(forbidden: body);
+ ContentSpacing {
+ spacing: args.get_pos::<f64>()? as f32,
+ content: meta
+ }
+ }
+
+ layout(self, mut ctx) {
+ let mut style = ctx.style.text.clone();
+ match self.content {
+ ContentKind::Word => style.word_spacing_scale = self.spacing,
+ ContentKind::Line => style.line_spacing_scale = self.spacing,
+ ContentKind::Paragraph => style.paragraph_spacing_scale = self.spacing,
+ }
+ vec![SetTextStyle(style)]
+ }
+}
+
+/// The different kinds of content that can be spaced.
+#[derive(Debug, Copy, Clone, Eq, PartialEq)]
+pub enum ContentKind {
+ Word,
+ Line,
+ Paragraph,
+}
+
+function! {
/// `page.size`: Set the size of pages.
#[derive(Debug, PartialEq)]
pub enum PageSize {
- Paper(Paper),
+ Paper(Paper, bool),
Custom(ExtentMap<PSize>),
}
@@ -81,7 +124,9 @@ function! {
parse!(forbidden: body);
if let Some(name) = args.get_pos_opt::<Ident>()? {
- PageSize::Paper(Paper::from_name(name.as_str())?)
+ let landscape = args.get_key_opt::<bool>("landscape")?
+ .unwrap_or(false);
+ PageSize::Paper(Paper::from_name(name.as_str())?, landscape)
} else {
PageSize::Custom(ExtentMap::new(&mut args, true)?)
}
@@ -91,9 +136,12 @@ function! {
let mut style = ctx.style.page;
match self {
- PageSize::Paper(paper) => {
+ PageSize::Paper(paper, landscape) => {
style.class = paper.class;
style.dimensions = paper.dimensions;
+ if *landscape {
+ style.dimensions.swap();
+ }
}
PageSize::Custom(map) => {
diff --git a/src/size.rs b/src/size.rs
index a3380f34..04eabf39 100644
--- a/src/size.rs
+++ b/src/size.rs
@@ -218,6 +218,11 @@ impl<T: Copy> Value2D<T> {
// at the call site, we still have this second function.
self.generalized(axes)
}
+
+ /// Swap the `x` and `y` values.
+ pub fn swap(&mut self) {
+ std::mem::swap(&mut self.x, &mut self.y);
+ }
}
impl<T: Copy> Display for Value2D<T> where T: Display {