summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-11-17 23:54:43 +0100
committerLaurenz <laurmaedje@gmail.com>2021-11-17 23:54:43 +0100
commitd9c529347d7f46eb2f4698d256b1906c1ced6b76 (patch)
treef9eb0bfcb58c46fa2d7cbdbb7166e3a5509e7fdb
parent095fa52be5d7ed135f39553359e0253cfea6b71b (diff)
Configurable paragraph alignment
-rw-r--r--src/library/par.rs29
-rw-r--r--tests/ref/text/par.pngbin3432 -> 5732 bytes
-rw-r--r--tests/typ/text/par.typ11
3 files changed, 31 insertions, 9 deletions
diff --git a/src/library/par.rs b/src/library/par.rs
index 67c2969b..9d48ef05 100644
--- a/src/library/par.rs
+++ b/src/library/par.rs
@@ -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;
}
diff --git a/tests/ref/text/par.png b/tests/ref/text/par.png
index fb493bd4..47f0ed95 100644
--- a/tests/ref/text/par.png
+++ b/tests/ref/text/par.png
Binary files differ
diff --git a/tests/typ/text/par.typ b/tests/typ/text/par.typ
index 4b223684..9955b993 100644
--- a/tests/typ/text/par.typ
+++ b/tests/typ/text/par.typ
@@ -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)