Add basic paragraph function 📑

Allows to change:
- (paragraph) spacing
- leading
- word-spacing
This commit is contained in:
Laurenz 2021-03-19 13:32:12 +01:00
parent 54a9ccb1a5
commit ca3df70e2a
4 changed files with 57 additions and 0 deletions

View File

@ -9,6 +9,7 @@ mod font;
mod image; mod image;
mod pad; mod pad;
mod page; mod page;
mod par;
mod shapes; mod shapes;
mod spacing; mod spacing;
@ -18,6 +19,7 @@ pub use base::*;
pub use font::*; pub use font::*;
pub use pad::*; pub use pad::*;
pub use page::*; pub use page::*;
pub use par::*;
pub use shapes::*; pub use shapes::*;
pub use spacing::*; pub use spacing::*;
@ -50,6 +52,7 @@ pub fn new() -> Scope {
set!(func: "pad", pad); set!(func: "pad", pad);
set!(func: "page", page); set!(func: "page", page);
set!(func: "pagebreak", pagebreak); set!(func: "pagebreak", pagebreak);
set!(func: "paragraph", paragraph);
set!(func: "rect", rect); set!(func: "rect", rect);
set!(func: "repr", repr); set!(func: "repr", repr);
set!(func: "rgb", rgb); set!(func: "rgb", rgb);

41
src/library/par.rs Normal file
View File

@ -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::<ValueTemplate>(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;
}
})
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

View File

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