summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
Diffstat (limited to 'src/library')
-rw-r--r--src/library/align.rs4
-rw-r--r--src/library/base.rs6
-rw-r--r--src/library/font.rs5
-rw-r--r--src/library/image.rs12
-rw-r--r--src/library/mod.rs110
-rw-r--r--src/library/pad.rs9
-rw-r--r--src/library/page.rs6
-rw-r--r--src/library/par.rs7
-rw-r--r--src/library/shapes.rs9
-rw-r--r--src/library/spacing.rs9
10 files changed, 93 insertions, 84 deletions
diff --git a/src/library/align.rs b/src/library/align.rs
index a516ff4c..93c6db0d 100644
--- a/src/library/align.rs
+++ b/src/library/align.rs
@@ -26,12 +26,12 @@ use super::*;
/// - `top`
/// - `bottom`
/// - `center`
-pub fn align(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn align(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
let first = args.find(ctx);
let second = args.find(ctx);
let hor = args.get(ctx, "horizontal");
let ver = args.get(ctx, "vertical");
- let body = args.find::<ValueTemplate>(ctx);
+ let body = args.find::<TemplateValue>(ctx);
Value::template("align", move |ctx| {
let snapshot = ctx.state.clone();
diff --git a/src/library/base.rs b/src/library/base.rs
index 4d36b878..22adb1f4 100644
--- a/src/library/base.rs
+++ b/src/library/base.rs
@@ -10,7 +10,7 @@ use super::*;
///
/// # Return value
/// The string representation of the value.
-pub fn repr(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn repr(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
match args.require::<Value>(ctx, "value") {
Some(value) => pretty(&value).into(),
None => Value::Error,
@@ -27,7 +27,7 @@ pub fn repr(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
///
/// # Return value
/// The color with the given components.
-pub fn rgb(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn rgb(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
let r = args.require(ctx, "red component");
let g = args.require(ctx, "green component");
let b = args.require(ctx, "blue component");
@@ -57,7 +57,7 @@ pub fn rgb(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
///
/// # Return value
/// The name of the value's type as a string.
-pub fn type_(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn type_(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
match args.require::<Value>(ctx, "value") {
Some(value) => value.type_name().into(),
None => Value::Error,
diff --git a/src/library/font.rs b/src/library/font.rs
index ecc15d96..00fd0e81 100644
--- a/src/library/font.rs
+++ b/src/library/font.rs
@@ -1,7 +1,6 @@
use fontdock::{FontStretch, FontStyle, FontWeight};
use super::*;
-use crate::shaping::VerticalFontMetric;
/// `font`: Configure the font.
///
@@ -55,7 +54,7 @@ use crate::shaping::VerticalFontMetric;
/// - `x-height`
/// - `baseline`
/// - `descender`
-pub fn font(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn font(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
let size = args.find::<Linear>(ctx);
let list: Vec<_> = args.filter::<FontFamily>(ctx).map(|f| f.to_string()).collect();
let style = args.get(ctx, "style");
@@ -66,7 +65,7 @@ pub fn font(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
let serif = args.get(ctx, "serif");
let sans_serif = args.get(ctx, "sans-serif");
let monospace = args.get(ctx, "monospace");
- let body = args.find::<ValueTemplate>(ctx);
+ let body = args.find::<TemplateValue>(ctx);
Value::template("font", move |ctx| {
let snapshot = ctx.state.clone();
diff --git a/src/library/image.rs b/src/library/image.rs
index c3200e93..8cb09463 100644
--- a/src/library/image.rs
+++ b/src/library/image.rs
@@ -2,7 +2,9 @@ use ::image::GenericImageView;
use super::*;
use crate::env::{ImageResource, ResourceId};
-use crate::layout::*;
+use crate::layout::{
+ AnyNode, Areas, Element, Fragment, Frame, Image, Layout, LayoutContext,
+};
/// `image`: Insert an image.
///
@@ -13,7 +15,7 @@ use crate::layout::*;
///
/// # Return value
/// A template that inserts an image.
-pub fn image(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn image(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
let path = args.require::<Spanned<String>>(ctx, "path to image file");
let width = args.get(ctx, "width");
let height = args.get(ctx, "height");
@@ -53,7 +55,7 @@ struct NodeImage {
}
impl Layout for NodeImage {
- fn layout(&self, _: &mut LayoutContext, areas: &Areas) -> Layouted {
+ fn layout(&self, _: &mut LayoutContext, areas: &Areas) -> Fragment {
let Areas { current, full, .. } = areas;
let pixel_width = self.dimensions.0 as f64;
@@ -84,11 +86,11 @@ impl Layout for NodeImage {
let mut frame = Frame::new(size);
frame.push(Point::ZERO, Element::Image(Image { res: self.res, size }));
- Layouted::Frame(frame, self.aligns)
+ Fragment::Frame(frame, self.aligns)
}
}
-impl From<NodeImage> for NodeAny {
+impl From<NodeImage> for AnyNode {
fn from(image: NodeImage) -> Self {
Self::new(image)
}
diff --git a/src/library/mod.rs b/src/library/mod.rs
index e813a138..d0920cf1 100644
--- a/src/library/mod.rs
+++ b/src/library/mod.rs
@@ -27,68 +27,72 @@ use std::fmt::{self, Display, Formatter};
use fontdock::{FontStyle, FontWeight};
-use crate::eval::{Scope, ValueAny, ValueFunc};
-use crate::layout::*;
-use crate::prelude::*;
-use crate::shaping::VerticalFontMetric;
+use crate::eval::{AnyValue, FuncValue, Scope};
+use crate::eval::{EvalContext, FuncArgs, TemplateValue, Value};
+use crate::exec::{Exec, ExecContext};
+use crate::geom::*;
+use crate::layout::VerticalFontMetric;
+use crate::syntax::Spanned;
/// Construct a scope containing all standard library definitions.
pub fn new() -> Scope {
let mut std = Scope::new();
- macro_rules! set {
- (func: $name:expr, $func:expr) => {
- std.def_const($name, ValueFunc::new(Some($name.into()), $func))
+
+ macro_rules! func {
+ ($name:expr, $func:expr) => {
+ std.def_const($name, FuncValue::new(Some($name.into()), $func))
};
- (any: $var:expr, $any:expr) => {
- std.def_const($var, ValueAny::new($any))
+ }
+
+ macro_rules! constant {
+ ($var:expr, $any:expr) => {
+ std.def_const($var, AnyValue::new($any))
};
}
- // Functions.
- set!(func: "align", align);
- set!(func: "font", font);
- set!(func: "h", h);
- set!(func: "image", image);
- set!(func: "pad", pad);
- set!(func: "page", page);
- set!(func: "pagebreak", pagebreak);
- set!(func: "paragraph", paragraph);
- set!(func: "rect", rect);
- set!(func: "repr", repr);
- set!(func: "rgb", rgb);
- set!(func: "type", type_);
- set!(func: "v", v);
+ func!("align", align);
+ func!("font", font);
+ func!("h", h);
+ func!("image", image);
+ func!("pad", pad);
+ func!("page", page);
+ func!("pagebreak", pagebreak);
+ func!("paragraph", par);
+ func!("rect", rect);
+ func!("repr", repr);
+ func!("rgb", rgb);
+ func!("type", type_);
+ func!("v", v);
- // Constants.
- set!(any: "left", AlignValue::Left);
- set!(any: "center", AlignValue::Center);
- set!(any: "right", AlignValue::Right);
- set!(any: "top", AlignValue::Top);
- set!(any: "bottom", AlignValue::Bottom);
- set!(any: "ltr", Dir::LTR);
- set!(any: "rtl", Dir::RTL);
- set!(any: "ttb", Dir::TTB);
- set!(any: "btt", Dir::BTT);
- set!(any: "serif", FontFamily::Serif);
- set!(any: "sans-serif", FontFamily::SansSerif);
- set!(any: "monospace", FontFamily::Monospace);
- set!(any: "normal", FontStyle::Normal);
- set!(any: "italic", FontStyle::Italic);
- set!(any: "oblique", FontStyle::Oblique);
- set!(any: "thin", FontWeight::THIN);
- set!(any: "extralight", FontWeight::EXTRALIGHT);
- set!(any: "light", FontWeight::LIGHT);
- set!(any: "regular", FontWeight::REGULAR);
- set!(any: "medium", FontWeight::MEDIUM);
- set!(any: "semibold", FontWeight::SEMIBOLD);
- set!(any: "bold", FontWeight::BOLD);
- set!(any: "extrabold", FontWeight::EXTRABOLD);
- set!(any: "black", FontWeight::BLACK);
- set!(any: "ascender", VerticalFontMetric::Ascender);
- set!(any: "cap-height", VerticalFontMetric::CapHeight);
- set!(any: "x-height", VerticalFontMetric::XHeight);
- set!(any: "baseline", VerticalFontMetric::Baseline);
- set!(any: "descender", VerticalFontMetric::Descender);
+ constant!("left", AlignValue::Left);
+ constant!("center", AlignValue::Center);
+ constant!("right", AlignValue::Right);
+ constant!("top", AlignValue::Top);
+ constant!("bottom", AlignValue::Bottom);
+ constant!("ltr", Dir::LTR);
+ constant!("rtl", Dir::RTL);
+ constant!("ttb", Dir::TTB);
+ constant!("btt", Dir::BTT);
+ constant!("serif", FontFamily::Serif);
+ constant!("sans-serif", FontFamily::SansSerif);
+ constant!("monospace", FontFamily::Monospace);
+ constant!("normal", FontStyle::Normal);
+ constant!("italic", FontStyle::Italic);
+ constant!("oblique", FontStyle::Oblique);
+ constant!("thin", FontWeight::THIN);
+ constant!("extralight", FontWeight::EXTRALIGHT);
+ constant!("light", FontWeight::LIGHT);
+ constant!("regular", FontWeight::REGULAR);
+ constant!("medium", FontWeight::MEDIUM);
+ constant!("semibold", FontWeight::SEMIBOLD);
+ constant!("bold", FontWeight::BOLD);
+ constant!("extrabold", FontWeight::EXTRABOLD);
+ constant!("black", FontWeight::BLACK);
+ constant!("ascender", VerticalFontMetric::Ascender);
+ constant!("cap-height", VerticalFontMetric::CapHeight);
+ constant!("x-height", VerticalFontMetric::XHeight);
+ constant!("baseline", VerticalFontMetric::Baseline);
+ constant!("descender", VerticalFontMetric::Descender);
std
}
diff --git a/src/library/pad.rs b/src/library/pad.rs
index 5d59f2b3..9f05f7ce 100644
--- a/src/library/pad.rs
+++ b/src/library/pad.rs
@@ -1,4 +1,5 @@
use super::*;
+use crate::layout::PadNode;
/// `pad`: Pad content at the sides.
///
@@ -14,13 +15,13 @@ use super::*;
///
/// # Return value
/// A template that pads the body at the sides.
-pub fn pad(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn pad(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
let all = args.find(ctx);
let left = args.get(ctx, "left");
let top = args.get(ctx, "top");
let right = args.get(ctx, "right");
let bottom = args.get(ctx, "bottom");
- let body = args.require::<ValueTemplate>(ctx, "body").unwrap_or_default();
+ let body = args.require::<TemplateValue>(ctx, "body").unwrap_or_default();
let padding = Sides::new(
left.or(all).unwrap_or_default(),
@@ -31,10 +32,8 @@ pub fn pad(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
Value::template("pad", move |ctx| {
let snapshot = ctx.state.clone();
-
let child = ctx.exec(&body).into();
- ctx.push(NodePad { padding, child });
-
+ ctx.push(PadNode { padding, child });
ctx.state = snapshot;
})
}
diff --git a/src/library/page.rs b/src/library/page.rs
index f7d76eaf..067258f5 100644
--- a/src/library/page.rs
+++ b/src/library/page.rs
@@ -30,7 +30,7 @@ use crate::paper::{Paper, PaperClass};
/// - `rtl` (right to left)
/// - `ttb` (top to bottom)
/// - `btt` (bottom to top)
-pub fn page(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn page(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
let paper = args.find::<Spanned<String>>(ctx).and_then(|name| {
Paper::from_name(&name.v).or_else(|| {
ctx.diag(error!(name.span, "invalid paper name"));
@@ -48,7 +48,7 @@ pub fn page(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
let flip = args.get(ctx, "flip");
let main = args.get(ctx, "main-dir");
let cross = args.get(ctx, "cross-dir");
- let body = args.find::<ValueTemplate>(ctx);
+ let body = args.find::<TemplateValue>(ctx);
let span = args.span;
Value::template("page", move |ctx| {
@@ -110,7 +110,7 @@ pub fn page(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
///
/// # Return value
/// A template that starts a new page.
-pub fn pagebreak(_: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn pagebreak(_: &mut EvalContext, args: &mut FuncArgs) -> Value {
let span = args.span;
Value::template("pagebreak", move |ctx| {
ctx.finish_page(true, true, span);
diff --git a/src/library/par.rs b/src/library/par.rs
index 8242bfdc..a7db46de 100644
--- a/src/library/par.rs
+++ b/src/library/par.rs
@@ -2,6 +2,9 @@ use super::*;
/// `paragraph`: Configure paragraphs.
///
+/// # Positional parameters
+/// - Body: optional, of type `template`.
+///
/// # Named parameters
/// - Paragraph spacing: `spacing`, of type `linear` relative to current font size.
/// - Line leading: `leading`, of type `linear` relative to current font size.
@@ -10,11 +13,11 @@ use super::*;
/// # Return value
/// A template that configures paragraph properties. The effect is scoped to the
/// body if present.
-pub fn paragraph(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn par(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
let spacing = args.get(ctx, "spacing");
let leading = args.get(ctx, "leading");
let word_spacing = args.get(ctx, "word-spacing");
- let body = args.find::<ValueTemplate>(ctx);
+ let body = args.find::<TemplateValue>(ctx);
Value::template("paragraph", move |ctx| {
let snapshot = ctx.state.clone();
diff --git a/src/library/shapes.rs b/src/library/shapes.rs
index 48bc7ebd..211a4f2e 100644
--- a/src/library/shapes.rs
+++ b/src/library/shapes.rs
@@ -1,4 +1,5 @@
use super::*;
+use crate::layout::{BackgroundNode, Fill, FixedNode};
/// `rect`: Create a rectangular box.
///
@@ -21,13 +22,13 @@ use super::*;
/// - `rtl` (right to left)
/// - `ttb` (top to bottom)
/// - `btt` (bottom to top)
-pub fn rect(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn rect(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
let width = args.get(ctx, "width");
let height = args.get(ctx, "height");
let main = args.get(ctx, "main-dir");
let cross = args.get(ctx, "cross-dir");
let fill = args.get(ctx, "fill");
- let body = args.find::<ValueTemplate>(ctx).unwrap_or_default();
+ let body = args.find::<TemplateValue>(ctx).unwrap_or_default();
Value::template("box", move |ctx| {
let snapshot = ctx.state.clone();
@@ -35,9 +36,9 @@ pub fn rect(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
ctx.set_dirs(Gen::new(main, cross));
let child = ctx.exec(&body).into();
- let fixed = NodeFixed { width, height, child };
+ let fixed = FixedNode { width, height, child };
if let Some(color) = fill {
- ctx.push(NodeBackground {
+ ctx.push(BackgroundNode {
fill: Fill::Color(color),
child: fixed.into(),
});
diff --git a/src/library/spacing.rs b/src/library/spacing.rs
index 4965a220..fee802fa 100644
--- a/src/library/spacing.rs
+++ b/src/library/spacing.rs
@@ -1,4 +1,5 @@
use super::*;
+use crate::layout::SpacingNode;
/// `h`: Add horizontal spacing.
///
@@ -7,7 +8,7 @@ use super::*;
///
/// # Return value
/// A template that adds horizontal spacing.
-pub fn h(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn h(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
spacing(ctx, args, SpecAxis::Horizontal)
}
@@ -18,17 +19,17 @@ pub fn h(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
///
/// # Return value
/// A template that adds vertical spacing.
-pub fn v(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
+pub fn v(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
spacing(ctx, args, SpecAxis::Vertical)
}
/// Apply spacing along a specific axis.
-fn spacing(ctx: &mut EvalContext, args: &mut ValueArgs, axis: SpecAxis) -> Value {
+fn spacing(ctx: &mut EvalContext, args: &mut FuncArgs, axis: SpecAxis) -> Value {
let spacing: Option<Linear> = args.require(ctx, "spacing");
Value::template("spacing", move |ctx| {
if let Some(linear) = spacing {
let amount = linear.resolve(ctx.state.font.font_size());
- let spacing = NodeSpacing { amount, softness: 0 };
+ let spacing = SpacingNode { amount, softness: 0 };
if axis == ctx.state.dirs.main.axis() {
ctx.push_into_stack(spacing);
} else {