From dbfb3d2ced91e56314dfabbb4df9a338926c0a7a Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 3 Aug 2020 16:01:23 +0200 Subject: =?UTF-8?q?Formatting,=20documentation=20and=20small=20improvement?= =?UTF-8?q?s=20=F0=9F=A7=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/library/font.rs | 57 ++++++++++++++++++++++++++++---------------------- src/library/layout.rs | 10 ++++----- src/library/mod.rs | 50 ++++++++++++++----------------------------- src/library/page.rs | 4 +--- src/library/spacing.rs | 10 ++++----- 5 files changed, 57 insertions(+), 74 deletions(-) (limited to 'src/library') diff --git a/src/library/font.rs b/src/library/font.rs index 21ac14c7..6e711021 100644 --- a/src/library/font.rs +++ b/src/library/font.rs @@ -1,6 +1,5 @@ -//! Font configuration. - use fontdock::{FontStyle, FontWeight, FontWidth}; + use crate::length::ScaleLength; use super::*; @@ -19,7 +18,6 @@ function! { parse(header, body, state, f) { let size = header.args.pos.get::(); - let style = header.args.key.get::("style", f); let weight = header.args.key.get::("weight", f); let width = header.args.key.get::("width", f); @@ -40,7 +38,7 @@ function! { }) .collect(); - FontFunc { + Self { body: parse_maybe_body(body, state, f), size, style, @@ -52,30 +50,39 @@ function! { } layout(self, ctx, f) { - styled(&self.body, ctx, Some(()), |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, - }); + let mut text = ctx.style.text.clone(); - 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() { - *t.fallback.list_mut() = self.list.iter() - .map(|s| s.to_lowercase()) - .collect(); + self.size.with(|s| match s { + ScaleLength::Absolute(length) => { + text.base_font_size = length.as_raw(); + text.font_scale = 1.0; } + ScaleLength::Scaled(scale) => text.font_scale = scale, + }); - for (class, fallback) in &self.classes { - t.fallback.set_class_list(class.clone(), fallback.clone()); - } + self.style.with(|s| text.variant.style = s); + self.weight.with(|w| text.variant.weight = w); + self.width.with(|w| text.variant.width = w); - t.fallback.flatten(); - }) + if !self.list.is_empty() { + *text.fallback.list_mut() = self.list.iter() + .map(|s| s.to_lowercase()) + .collect(); + } + + for (class, fallback) in &self.classes { + text.fallback.set_class_list(class.clone(), fallback.clone()); + } + + text.fallback.flatten(); + + match &self.body { + Some(tree) => vec![ + SetTextStyle(text), + LayoutSyntaxTree(tree), + SetTextStyle(ctx.style.text.clone()), + ], + None => vec![SetTextStyle(text)], + } } } diff --git a/src/library/layout.rs b/src/library/layout.rs index d6d02436..f3ddaadf 100644 --- a/src/library/layout.rs +++ b/src/library/layout.rs @@ -1,5 +1,3 @@ -//! Layout building blocks. - use crate::length::ScaleLength; use super::*; @@ -13,7 +11,7 @@ function! { } parse(header, body, state, f) { - BoxFunc { + Self { body: parse_maybe_body(body, state, f).unwrap_or(SyntaxTree::new()), width: header.args.key.get::("width", f), height: header.args.key.get::("height", f), @@ -21,8 +19,8 @@ function! { } layout(self, ctx, f) { - ctx.repeat = false; ctx.spaces.truncate(1); + ctx.repeat = false; self.width.with(|v| { let length = v.raw_scaled(ctx.base.x); @@ -51,13 +49,13 @@ function! { #[derive(Debug, Clone, PartialEq)] pub struct AlignFunc { body: Option, - aligns: Vec>, + aligns: SpanVec, h: Option>, v: Option>, } parse(header, body, state, f) { - AlignFunc { + Self { body: parse_maybe_body(body, state, f), aligns: header.args.pos.all::>().collect(), h: header.args.key.get::>("horizontal", f), diff --git a/src/library/mod.rs b/src/library/mod.rs index 7240e42b..0f61b901 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -1,16 +1,19 @@ //! The standard library. +mod font; +mod layout; +mod page; +mod spacing; + +pub use font::*; +pub use layout::*; +pub use page::*; +pub use spacing::*; + use crate::func::prelude::*; -use crate::layout::{LayoutContext, Commands}; use crate::syntax::scope::Scope; -macro_rules! lib { ($name:ident) => { mod $name; pub use $name::*; }} -lib!(font); -lib!(layout); -lib!(page); -lib!(spacing); - -/// Create a scope with all standard functions. +/// Create a scope with all standard library functions. pub fn std() -> Scope { let mut std = Scope::new::(); @@ -28,7 +31,10 @@ pub fn std() -> Scope { } function! { - /// `val`: Layouts the body with no special effect. + /// `val`: Ignores all arguments and layouts the body flatly. + /// + /// This is also the fallback function, which is used when a function name + /// could not be resolved. #[derive(Debug, Clone, PartialEq)] pub struct ValFunc { body: Option, @@ -37,7 +43,7 @@ function! { parse(header, body, state, f) { header.args.pos.0.clear(); header.args.key.0.clear(); - ValFunc { body: parse_maybe_body(body, state, f), } + Self { body: parse_maybe_body(body, state, f), } } layout(self, ctx, f) { @@ -47,27 +53,3 @@ function! { } } } - -/// Layout an optional body with a change of the text style. -fn styled<'a, T, F>( - body: &'a Option, - ctx: LayoutContext<'_>, - data: Option, - f: F, -) -> Commands<'a> where F: FnOnce(&mut TextStyle, T) { - if let Some(data) = data { - let mut style = ctx.style.text.clone(); - f(&mut style, data); - - match body { - Some(tree) => vec![ - SetTextStyle(style), - LayoutSyntaxTree(tree), - SetTextStyle(ctx.style.text.clone()), - ], - None => vec![SetTextStyle(style)], - } - } else { - vec![] - } -} diff --git a/src/library/page.rs b/src/library/page.rs index faf08ee0..1cc76d7f 100644 --- a/src/library/page.rs +++ b/src/library/page.rs @@ -1,5 +1,3 @@ -//! Page setup. - use crate::length::{Length, ScaleLength}; use crate::paper::{Paper, PaperClass}; use super::*; @@ -21,7 +19,7 @@ function! { parse(header, body, state, f) { expect_no_body(body, f); - PageFunc { + Self { paper: header.args.pos.get::(), width: header.args.key.get::("width", f), height: header.args.key.get::("height", f), diff --git a/src/library/spacing.rs b/src/library/spacing.rs index d8263694..545f3910 100644 --- a/src/library/spacing.rs +++ b/src/library/spacing.rs @@ -1,13 +1,11 @@ -//! Spacing. - -use crate::length::ScaleLength; use crate::layout::SpacingKind; +use crate::length::ScaleLength; use super::*; function! { /// `parbreak`: Ends the current paragraph. /// - /// self has the same effect as two subsequent newlines. + /// This has the same effect as two subsequent newlines. #[derive(Debug, Default, Clone, PartialEq)] pub struct ParBreakFunc; @@ -25,7 +23,7 @@ function! { } function! { - /// `h` and `v`: Add spacing along an axis. + /// `h` and `v`: Add horizontal or vertical spacing. #[derive(Debug, Clone, PartialEq)] pub struct SpacingFunc { spacing: Option<(SpecAxis, ScaleLength)>, @@ -35,7 +33,7 @@ function! { parse(header, body, state, f, meta) { expect_no_body(body, f); - SpacingFunc { + Self { spacing: header.args.pos.expect::(f) .map(|s| (meta, s)) .or_missing(header.name.span, "spacing", f), -- cgit v1.2.3