Add word|line|par.spacing functions 💶

This commit is contained in:
Laurenz 2020-01-01 17:15:21 +01:00
parent 63b8ccba95
commit 7de9219321
2 changed files with 56 additions and 3 deletions

View File

@ -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");
@ -69,11 +74,49 @@ function! {
layout() { vec![BreakPage] }
}
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) => {

View File

@ -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 {