Skip to content

Commit

Permalink
Fix Path::compute_tight_bounds calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
RazrFalcon committed Feb 4, 2024
1 parent 4741251 commit a97f7ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
- `Path::compute_tight_bounds` calculation.
Thanks to [@qbcbyb](https://github.com/qbcbyb)

## [0.11.3] - 2023-12-03
### Added
Expand Down
6 changes: 4 additions & 2 deletions path/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl Path {
let mut min = self.points[0];
let mut max = self.points[0];
let mut iter = self.segments();
let mut last_point = Point::zero();
while let Some(segment) = iter.next() {
let mut count = 0;
match segment {
Expand All @@ -92,14 +93,15 @@ impl Path {
count = 1;
}
PathSegment::QuadTo(p0, p1) => {
count = compute_quad_extremas(iter.last_point, p0, p1, &mut extremas);
count = compute_quad_extremas(last_point, p0, p1, &mut extremas);
}
PathSegment::CubicTo(p0, p1, p2) => {
count = compute_cubic_extremas(iter.last_point, p0, p1, p2, &mut extremas);
count = compute_cubic_extremas(last_point, p0, p1, p2, &mut extremas);
}
PathSegment::Close => {}
}

last_point = iter.last_point;
for tmp in &extremas[0..count] {
min.x = min.x.min(tmp.x);
min.y = min.y.min(tmp.y);
Expand Down
20 changes: 18 additions & 2 deletions tests/integration/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@ fn tight_bounds_1() {
pb.quad_to(100.0, 45.0, 50.0, 85.0);
let path = pb.finish().unwrap();
let tight_bounds = path.compute_tight_bounds().unwrap();
assert_eq!(path.bounds(), Rect::from_ltrb(50.0, 45.0, 150.0, 135.0).unwrap());
assert_eq!(tight_bounds, Rect::from_ltrb(50.0, 65.0, 150.0, 135.0).unwrap());
assert_eq!(path.bounds(), Rect::from_xywh(50.0, 45.0, 100.0, 90.0).unwrap());
assert_eq!(tight_bounds, Rect::from_xywh(50.0, 72.692307, 100.0, 62.307693).unwrap());
}

#[test]
fn tight_bounds_2() {
let mut pb = PathBuilder::new();
pb.move_to(-19.309214, 72.11173);
pb.cubic_to(-24.832062, 67.477516, -20.490944, 62.16584, -9.61306, 60.247776);
pb.cubic_to(1.2648277, 58.329712, 14.560249, 60.53159, 20.083096, 65.16581);
pb.cubic_to(14.560249, 60.53159, 18.901363, 55.219913, 29.779247, 53.30185);
pb.cubic_to(40.65713, 51.383785, 53.952557, 53.585663, 59.475407, 58.21988);
pb.quad_to(74.4754, 70.80637, 50.083096, 90.3388);
pb.quad_to(-4.3092155, 84.69823, -19.309214, 72.11173);
pb.close();
let path = pb.finish().unwrap();
let tight_bounds = path.compute_tight_bounds().unwrap();
assert_eq!(tight_bounds, Rect::from_xywh(-21.707121, 52.609154, 86.894302, 37.729645).unwrap());
}

0 comments on commit a97f7ff

Please sign in to comment.