summaryrefslogtreecommitdiff
path: root/crates/typst-layout
diff options
context:
space:
mode:
authorAndrew Voynov <37143421+Andrew15-5@users.noreply.github.com>2024-12-16 15:18:34 +0300
committerGitHub <noreply@github.com>2024-12-16 12:18:34 +0000
commit7f139517b9ba8cf4f51083d5387971571d1aa950 (patch)
tree1ac01c07b30aaadbaa644f2e4282f32e6835ee15 /crates/typst-layout
parenta3ad0a0bba3ce3abb1d8ed84656f54cc2d74be25 (diff)
Derivation comment for calculation in `repeat` (#5575)
Diffstat (limited to 'crates/typst-layout')
-rw-r--r--crates/typst-layout/src/repeat.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/crates/typst-layout/src/repeat.rs b/crates/typst-layout/src/repeat.rs
index b761438c..bfc7b32c 100644
--- a/crates/typst-layout/src/repeat.rs
+++ b/crates/typst-layout/src/repeat.rs
@@ -33,8 +33,17 @@ pub fn layout_repeat(
let fill = region.size.x;
let width = piece.width();
- // count * width + (count - 1) * gap = fill, but count is an integer so
- // we need to round down and get the remainder.
+ // We need to fit the body N times, but the number of gaps is (N - 1):
+ // N * w + (N - 1) * g ≤ F
+ // where N - body count (count)
+ // w - body width (width)
+ // g - gap width (gap)
+ // F - available space to fill (fill)
+ //
+ // N * w + N * g - g ≤ F
+ // N * (w + g) ≤ F + g
+ // N ≤ (F + g) / (w + g)
+ // N = ⌊(F + g) / (w + g)⌋
let count = ((fill + gap) / (width + gap)).floor();
let remaining = (fill + gap) % (width + gap);
@@ -52,7 +61,7 @@ pub fn layout_repeat(
if width > Abs::zero() {
for _ in 0..(count as usize).min(1000) {
frame.push_frame(Point::with_x(offset), piece.clone());
- offset += piece.width() + gap;
+ offset += width + gap;
}
}