summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-08-02 11:06:45 +0200
committerLaurenz <laurmaedje@gmail.com>2020-08-02 11:06:45 +0200
commitefb78831a7a9fe8c807c326a1bfa338b50579938 (patch)
tree7bbba4130ac47809c72efeac796b2ca71b72e659 /src/library
parent659248d52ff9e6be4dad7c4555bd62899671ad55 (diff)
Unify font and page functions 💕
- Removes font weight and width warnings for now, will be added again later - Adds a bit hacky get_first function for tuples, will be refactored soon anyway
Diffstat (limited to 'src/library')
-rw-r--r--src/library/font.rs162
-rw-r--r--src/library/mod.rs9
-rw-r--r--src/library/page.rs29
3 files changed, 44 insertions, 156 deletions
diff --git a/src/library/font.rs b/src/library/font.rs
index a78f9124..be9b8c47 100644
--- a/src/library/font.rs
+++ b/src/library/font.rs
@@ -3,24 +3,33 @@ use crate::length::ScaleLength;
use super::*;
function! {
- /// `font.family`: Set the font family.
+ /// `font`: Configure the font.
#[derive(Debug, Clone, PartialEq)]
- pub struct FontFamilyFunc {
+ pub struct FontFunc {
body: Option<SyntaxModel>,
+ size: Option<ScaleLength>,
+ style: Option<FontStyle>,
+ weight: Option<FontWeight>,
+ width: Option<FontWidth>,
list: Vec<String>,
classes: Vec<(String, Vec<String>)>,
}
parse(header, body, ctx, f) {
+ let size = header.args.pos.get_first::<ScaleLength>(&mut f.diagnostics);
+
+ let style = header.args.key.get::<FontStyle>(&mut f.diagnostics, "style");
+ let weight = header.args.key.get::<FontWeight>(&mut f.diagnostics, "weight");
+ let width = header.args.key.get::<FontWidth>(&mut f.diagnostics, "width");
+
let list = header.args.pos.get_all::<StringLike>(&mut f.diagnostics)
.map(|s| s.0.to_lowercase())
.collect();
- let tuples: Vec<_> = header.args.key
+ let classes = header.args.key
.get_all::<String, Tuple>(&mut f.diagnostics)
- .collect();
-
- let classes = tuples.into_iter()
+ .collect::<Vec<_>>()
+ .into_iter()
.map(|(class, mut tuple)| {
let fallback = tuple.get_all::<StringLike>(&mut f.diagnostics)
.map(|s| s.0.to_lowercase())
@@ -29,140 +38,41 @@ function! {
})
.collect();
- FontFamilyFunc {
+ FontFunc {
body: body!(opt: body, ctx, f),
+ size,
list,
classes,
+ style,
+ weight,
+ width,
}
}
layout(self, ctx, f) {
styled(&self.body, ctx, Some(()),
- |s, _| {
+ |t, _| {
+ self.size.with(|s| match s {
+ ScaleLength::Absolute(length) => {
+ t.base_font_size = length.as_raw();
+ t.font_scale = 1.0;
+ }
+ ScaleLength::Scaled(scale) => t.font_scale = scale,
+ });
+
+ self.style.with(|s| t.variant.style = s);
+ self.weight.with(|w| t.variant.weight = w);
+ self.width.with(|w| t.variant.width = w);
+
if !self.list.is_empty() {
- *s.fallback.list_mut() = self.list.clone();
+ *t.fallback.list_mut() = self.list.clone();
}
for (class, fallback) in &self.classes {
- s.fallback.set_class_list(class.clone(), fallback.clone());
- }
-
- s.fallback.flatten();
- })
- }
-}
-
-function! {
- /// `font.style`: Set the font style (normal / italic).
- #[derive(Debug, Clone, PartialEq)]
- pub struct FontStyleFunc {
- body: Option<SyntaxModel>,
- style: Option<FontStyle>,
- }
-
- parse(header, body, ctx, f) {
- FontStyleFunc {
- body: body!(opt: body, ctx, f),
- style: header.args.pos.get::<FontStyle>(&mut f.diagnostics)
- .or_missing(&mut f.diagnostics, header.name.span, "style"),
- }
- }
-
- layout(self, ctx, f) {
- styled(&self.body, ctx, self.style, |t, s| t.variant.style = s)
- }
-}
-
-function! {
- /// `font.weight`: Set text with a given weight.
- #[derive(Debug, Clone, PartialEq)]
- pub struct FontWeightFunc {
- body: Option<SyntaxModel>,
- weight: Option<FontWeight>,
- }
-
- parse(header, body, ctx, f) {
- let body = body!(opt: body, ctx, f);
- let weight = header.args.pos.get::<Spanned<(FontWeight, bool)>>(&mut f.diagnostics)
- .map(|Spanned { v: (weight, is_clamped), span }| {
- if is_clamped {
- warning!(
- @f, span,
- "weight should be between 100 and 900, clamped to {}",
- weight.0,
- );
+ t.fallback.set_class_list(class.clone(), fallback.clone());
}
- weight
+ t.fallback.flatten();
})
- .or_missing(&mut f.diagnostics, header.name.span, "weight");
-
- FontWeightFunc { body, weight }
- }
-
- layout(self, ctx, f) {
- styled(&self.body, ctx, self.weight, |t, w| t.variant.weight = w)
- }
-}
-
-
-function! {
- /// `font.width`: Set text with a given width.
- #[derive(Debug, Clone, PartialEq)]
- pub struct FontWidthFunc {
- body: Option<SyntaxModel>,
- width: Option<FontWidth>,
- }
-
- parse(header, body, ctx, f) {
- let body = body!(opt: body, ctx, f);
- let width = header.args.pos.get::<Spanned<(FontWidth, bool)>>(&mut f.diagnostics)
- .map(|Spanned { v: (width, is_clamped), span }| {
- if is_clamped {
- warning!(
- @f, span,
- "width should be between 1 and 9, clamped to {}",
- width.to_number(),
- );
- }
-
- width
- })
- .or_missing(&mut f.diagnostics, header.name.span, "width");
-
- FontWidthFunc { body, width }
- }
-
- layout(self, ctx, f) {
- styled(&self.body, ctx, self.width, |t, w| t.variant.width = w)
- }
-}
-
-function! {
- /// `font.size`: Sets the font size.
- #[derive(Debug, Clone, PartialEq)]
- pub struct FontSizeFunc {
- body: Option<SyntaxModel>,
- size: Option<ScaleLength>,
- }
-
- parse(header, body, ctx, f) {
- FontSizeFunc {
- body: body!(opt: body, ctx, f),
- size: header.args.pos.get::<ScaleLength>(&mut f.diagnostics)
- .or_missing(&mut f.diagnostics, header.name.span, "size")
- }
- }
-
- layout(self, ctx, f) {
- styled(&self.body, ctx, self.size, |t, s| {
- match s {
- ScaleLength::Absolute(length) => {
- t.base_font_size = length.as_raw();
- t.font_scale = 1.0;
- }
- ScaleLength::Scaled(scale) => t.font_scale = scale,
- }
- })
}
}
diff --git a/src/library/mod.rs b/src/library/mod.rs
index 6e84362b..eac45567 100644
--- a/src/library/mod.rs
+++ b/src/library/mod.rs
@@ -16,11 +16,7 @@ pub fn std() -> Scope {
std.add::<ValFunc>("val");
// Font setup
- std.add::<FontFamilyFunc>("font.family");
- std.add::<FontStyleFunc>("font.style");
- std.add::<FontWeightFunc>("font.weight");
- std.add::<FontWidthFunc>("font.width");
- std.add::<FontSizeFunc>("font.size");
+ std.add::<FontFunc>("font");
std.add_with_meta::<ContentSpacingFunc>("word.spacing", ContentKind::Word);
// Layout
@@ -40,8 +36,7 @@ pub fn std() -> Scope {
std.add_with_meta::<SpacingFunc>("v", Some(Vertical));
// Page setup
- std.add::<PageSizeFunc>("page.size");
- std.add::<PageMarginsFunc>("page.margins");
+ std.add::<PageFunc>("page");
std
}
diff --git a/src/library/page.rs b/src/library/page.rs
index 43f916b3..85a061e9 100644
--- a/src/library/page.rs
+++ b/src/library/page.rs
@@ -3,19 +3,21 @@ use crate::paper::{Paper, PaperClass};
use super::*;
function! {
- /// `page.size`: Set the size of pages.
+ /// `page`: Configure pages.
#[derive(Debug, Clone, PartialEq)]
- pub struct PageSizeFunc {
+ pub struct PageFunc {
paper: Option<Paper>,
extents: AxisMap<Length>,
+ padding: PaddingMap,
flip: bool,
}
parse(header, body, state, f) {
body!(nope: body, f);
- PageSizeFunc {
+ PageFunc {
paper: header.args.pos.get::<Paper>(&mut f.diagnostics),
extents: AxisMap::parse::<ExtentKey>(&mut f.diagnostics, &mut header.args.key),
+ padding: PaddingMap::parse(&mut f.diagnostics, &mut header.args),
flip: header.args.key.get::<bool>(&mut f.diagnostics, "flip").unwrap_or(false),
}
}
@@ -38,27 +40,8 @@ function! {
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, state, f) {
- body!(nope: body, f);
- PageMarginsFunc {
- padding: PaddingMap::parse(&mut f.diagnostics, &mut header.args),
- }
- }
-
- layout(self, ctx, f) {
- let mut style = ctx.style.page;
self.padding.apply(&mut f.diagnostics, ctx.axes, &mut style.margins);
+
vec![SetPageStyle(style)]
}
}