diff --git a/src/library/mod.rs b/src/library/mod.rs index 453eab26b..e813a138d 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -9,6 +9,7 @@ mod font; mod image; mod pad; mod page; +mod par; mod shapes; mod spacing; @@ -18,6 +19,7 @@ pub use base::*; pub use font::*; pub use pad::*; pub use page::*; +pub use par::*; pub use shapes::*; pub use spacing::*; @@ -50,6 +52,7 @@ pub fn new() -> Scope { 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); diff --git a/src/library/par.rs b/src/library/par.rs new file mode 100644 index 000000000..8242bfdcf --- /dev/null +++ b/src/library/par.rs @@ -0,0 +1,41 @@ +use super::*; + +/// `paragraph`: Configure paragraphs. +/// +/// # Named parameters +/// - Paragraph spacing: `spacing`, of type `linear` relative to current font size. +/// - Line leading: `leading`, of type `linear` relative to current font size. +/// - Word spacing: `word-spacing`, of type `linear` relative to current font size. +/// +/// # 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 { + let spacing = args.get(ctx, "spacing"); + let leading = args.get(ctx, "leading"); + let word_spacing = args.get(ctx, "word-spacing"); + let body = args.find::(ctx); + + Value::template("paragraph", move |ctx| { + let snapshot = ctx.state.clone(); + + if let Some(spacing) = spacing { + ctx.state.par.spacing = spacing; + } + + if let Some(leading) = leading { + ctx.state.par.leading = leading; + } + + if let Some(word_spacing) = word_spacing { + ctx.state.par.word_spacing = word_spacing; + } + + ctx.push_parbreak(); + + if let Some(body) = &body { + body.exec(ctx); + ctx.state = snapshot; + } + }) +} diff --git a/tests/ref/library/paragraph.png b/tests/ref/library/paragraph.png new file mode 100644 index 000000000..bf38bdf8c Binary files /dev/null and b/tests/ref/library/paragraph.png differ diff --git a/tests/typ/library/paragraph.typ b/tests/typ/library/paragraph.typ new file mode 100644 index 000000000..3ce946bde --- /dev/null +++ b/tests/typ/library/paragraph.typ @@ -0,0 +1,13 @@ +// Test the `paragraph` function. + +--- +// Test configuring paragraph properties. + +#paragraph(spacing: 10pt, leading: 25%, word-spacing: 1pt) + +But, soft! what light through yonder window breaks? It is the east, and Juliet +is the sun. + +--- +// Test that it finishes an existing paragraph. +Hello #paragraph(word-spacing: 0pt) t h e r e !