Skip to content

Commit 7278d88

Browse files
saeckilaurmaedje
andauthored
Fix bounding box computation for lines in curves (typst#6647)
Co-authored-by: Laurenz <[email protected]>
1 parent 7835542 commit 7278d88

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

crates/typst-library/src/visualize/curve.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -476,26 +476,18 @@ impl Curve {
476476

477477
/// Computes the size of the bounding box of this curve.
478478
pub fn bbox_size(&self) -> Size {
479-
let mut min_x = Abs::inf();
480-
let mut min_y = Abs::inf();
481-
let mut max_x = -Abs::inf();
482-
let mut max_y = -Abs::inf();
479+
let mut min = Point::splat(Abs::inf());
480+
let mut max = Point::splat(-Abs::inf());
483481

484482
let mut cursor = Point::zero();
485483
for item in self.0.iter() {
486484
match item {
487485
CurveItem::Move(to) => {
488-
min_x = min_x.min(cursor.x);
489-
min_y = min_y.min(cursor.y);
490-
max_x = max_x.max(cursor.x);
491-
max_y = max_y.max(cursor.y);
492486
cursor = *to;
493487
}
494488
CurveItem::Line(to) => {
495-
min_x = min_x.min(cursor.x);
496-
min_y = min_y.min(cursor.y);
497-
max_x = max_x.max(cursor.x);
498-
max_y = max_y.max(cursor.y);
489+
min = min.min(cursor).min(*to);
490+
max = max.max(cursor).max(*to);
499491
cursor = *to;
500492
}
501493
CurveItem::Cubic(c0, c1, end) => {
@@ -507,17 +499,17 @@ impl Curve {
507499
);
508500

509501
let bbox = cubic.bounding_box();
510-
min_x = min_x.min(Abs::pt(bbox.x0)).min(Abs::pt(bbox.x1));
511-
min_y = min_y.min(Abs::pt(bbox.y0)).min(Abs::pt(bbox.y1));
512-
max_x = max_x.max(Abs::pt(bbox.x0)).max(Abs::pt(bbox.x1));
513-
max_y = max_y.max(Abs::pt(bbox.y0)).max(Abs::pt(bbox.y1));
502+
min.x = min.x.min(Abs::pt(bbox.x0)).min(Abs::pt(bbox.x1));
503+
min.y = min.y.min(Abs::pt(bbox.y0)).min(Abs::pt(bbox.y1));
504+
max.x = max.x.max(Abs::pt(bbox.x0)).max(Abs::pt(bbox.x1));
505+
max.y = max.y.max(Abs::pt(bbox.y0)).max(Abs::pt(bbox.y1));
514506
cursor = *end;
515507
}
516508
CurveItem::Close => (),
517509
}
518510
}
519511

520-
Size::new(max_x - min_x, max_y - min_y)
512+
Size::new(max.x - min.x, max.y - min.y)
521513
}
522514
}
523515

399 Bytes
Loading
231 Bytes
Loading

tests/suite/visualize/curve.typ

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@
130130
down, up, down, up, down,
131131
)
132132

133+
--- curve-stroke-gradient-sharp ---
134+
#set page(width: auto)
135+
#let down = curve.line((40pt, 40pt), relative: true)
136+
#let up = curve.line((40pt, -40pt), relative: true)
137+
138+
#curve(
139+
stroke: 4pt + gradient.linear(red, blue).sharp(3),
140+
down, up, down, up, down,
141+
)
142+
133143
--- curve-fill-rule ---
134144
#stack(
135145
dir: ltr,

0 commit comments

Comments
 (0)