mirror of
https://github.com/typst/typst
synced 2025-05-25 06:25:28 +08:00
Add basic paragraph function 📑
Allows to change: - (paragraph) spacing - leading - word-spacing
This commit is contained in:
parent
54a9ccb1a5
commit
ca3df70e2a
@ -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);
|
||||
|
41
src/library/par.rs
Normal file
41
src/library/par.rs
Normal 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;
|
||||
}
|
||||
})
|
||||
}
|
BIN
tests/ref/library/paragraph.png
Normal file
BIN
tests/ref/library/paragraph.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.1 KiB |
13
tests/typ/library/paragraph.typ
Normal file
13
tests/typ/library/paragraph.typ
Normal 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 !
|
Loading…
x
Reference in New Issue
Block a user