Configurable paragraph alignment

This commit is contained in:
Laurenz 2021-11-17 23:54:43 +01:00
parent 095fa52be5
commit d9c529347d
3 changed files with 31 additions and 9 deletions

View File

@ -15,17 +15,26 @@ pub fn par(ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let spacing = args.named("spacing")?;
let leading = args.named("leading")?;
let mut dir = args.named::<EcoString>("lang")?.map(|iso| {
match iso.to_ascii_lowercase().as_str() {
"ar" | "he" | "fa" | "ur" | "ps" | "yi" => Dir::RTL,
"en" | "fr" | "de" => Dir::LTR,
_ => Dir::LTR,
}
});
let mut dir =
args.named("lang")?
.map(|iso: EcoString| match iso.to_lowercase().as_str() {
"ar" | "he" | "fa" | "ur" | "ps" | "yi" => Dir::RTL,
"en" | "fr" | "de" => Dir::LTR,
_ => Dir::LTR,
});
if let Some(Spanned { v, span }) = args.named::<Spanned<Dir>>("dir")? {
if v.axis() == SpecAxis::Horizontal {
dir = Some(v)
dir = Some(v);
} else {
bail!(span, "must be horizontal");
}
}
let mut align = None;
if let Some(Spanned { v, span }) = args.named::<Spanned<Align>>("align")? {
if matches!(v.axis(), None | Some(SpecAxis::Horizontal)) {
align = Some(v);
} else {
bail!(span, "must be horizontal");
}
@ -39,6 +48,10 @@ pub fn par(ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
par.align = if dir == Dir::LTR { Align::Left } else { Align::Right };
}
if let Some(align) = align {
par.align = align;
}
if let Some(leading) = leading {
par.leading = leading;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

View File

@ -1,8 +1,17 @@
// Test configuring paragraph properties.
---
#par(spacing: 100%, leading: 0pt)
// Test ragged-left.
#par(align: right)
To the right! Where the sunlight peeks behind the mountain.
---
// Test weird metrics.
#par(spacing: 100%, leading: 0pt)
But, soft! what light through yonder window breaks?
It is the east, and Juliet is the sun.
---
// Error: 13-16 must be horizontal
#par(align: top)