Skip to content

Commit 4668f65

Browse files
authored
libra:0.1.0 (#2861)
1 parent e0c9849 commit 4668f65

File tree

7 files changed

+2092
-0
lines changed

7 files changed

+2092
-0
lines changed

packages/preview/libra/0.1.0/LICENSE

Lines changed: 619 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The `libra` Package
2+
<div align="center">Version 0.1.0</div>
3+
4+
Offers a single function, `balance`, which balances the lines of a given text.
5+
It is most useful for balancing especially laid-out text such as headings or captions.
6+
7+
<picture>
8+
<source media="(prefers-color-scheme: dark)" srcset="./thumbnail-dark.svg">
9+
<img src="./thumbnail-light.svg">
10+
</picture>
11+
12+
## Getting Started
13+
14+
These instructions will get you a copy of the project up and running on the typst web app.
15+
For more details, check out the [manual](docs/manual.pdf).
16+
17+
```typst
18+
#import "@preview/libra:0.1.0": balance
19+
20+
#balance(lorem(20))
21+
```
22.4 KB
Binary file not shown.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/// Balances text, useful for headings, captions, centered text, or others.
2+
3+
/// Balances lines of text while keeping the minimum amount of lines possible.
4+
/// Works by shrinking the width of the text until it can't be shrunk anymore.
5+
///
6+
/// Unbalanced text:
7+
/// #example(```
8+
/// #rect(width: 25em, lorem(10))
9+
/// ```)
10+
///
11+
/// Balanced text:
12+
/// #example(```
13+
/// #rect(width: 25em, balance(lorem(10)))
14+
/// ```)
15+
///
16+
/// -> content
17+
#let balance(
18+
/// The text to balance. -> content
19+
body,
20+
/// Maximum number of iterations to perform. -> int
21+
max-iterations: 20,
22+
/// The precision to which to balance. -> length
23+
precision: 0.1em,
24+
) = context layout(size => {
25+
set text(hyphenate: par.justify) if text.hyphenate == auto
26+
let lead = par.leading.to-absolute()
27+
let line-height = measure(body).height + lead
28+
let initial-size = measure(width: size.width, body)
29+
let initial-lines = (initial-size.height + lead) / line-height
30+
31+
let high = initial-size.width
32+
let low = high * (1 - (1 / initial-lines)) / 2
33+
34+
let extra-lines = initial-lines
35+
for i in range(0, max-iterations) {
36+
let candidate = high - (high - low) / (extra-lines + 1)
37+
set par(justify: false)
38+
let (height, width) = measure(width: candidate, body)
39+
if height > initial-size.height {
40+
low = candidate
41+
extra-lines = (height - initial-size.height) / line-height
42+
} else {
43+
high = width
44+
if measure(width: width - precision, body).height > initial-size.height {
45+
break
46+
}
47+
high -= precision.to-absolute()
48+
}
49+
if high - low < precision.to-absolute() {
50+
break
51+
}
52+
}
53+
54+
box(width: high, body + if par.justify { linebreak(justify: true) })
55+
})

packages/preview/libra/0.1.0/thumbnail-dark.svg

Lines changed: 686 additions & 0 deletions
Loading

packages/preview/libra/0.1.0/thumbnail-light.svg

Lines changed: 686 additions & 0 deletions
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# for a description of available keys, see https://github.com/typst/packages/?tab=readme-ov-file#package-format
2+
3+
[package]
4+
name = "libra"
5+
version = "0.1.0"
6+
entrypoint = "src/lib.typ"
7+
authors = [
8+
"Luiz Georg <https://github.com/mentonin>",
9+
]
10+
license = "GPL-3.0-or-later"
11+
description = "Balance your lines."
12+
repository = "https://github.com/mentonin/libra"
13+
keywords = ["balanced"]
14+
categories = ["layout"]
15+
16+
exclude = [
17+
".github",
18+
"docs",
19+
"scripts",
20+
"tests",
21+
".typstignore",
22+
"Justfile",
23+
"thumbnail-dark.svg",
24+
"thumbnail-light.svg",
25+
]

0 commit comments

Comments
 (0)