Skip to content

Commit

Permalink
fix: panic when restore from scale(0,0) state (#944)
Browse files Browse the repository at this point in the history
- Close #848
  • Loading branch information
Brooooooklyn authored Nov 18, 2024
1 parent 4df6b01 commit 973adab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 10 additions & 0 deletions __test__/regression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,13 @@ test('isPointInPath with translate', (t) => {
t.true(ctx.isPointInPath(100, 100))
t.true(ctx.isPointInPath(110, 110))
})

test('restore from scale(0, 0)', (t) => {
const canvas = createCanvas(1200, 700)
const ctx = canvas.getContext('2d')
t.notThrows(() => {
ctx.scale(0, 0)
ctx.save()
ctx.restore()
})
})
12 changes: 8 additions & 4 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ impl Context {
if let Some(s) = self.states.pop() {
self.path.transform_self(&self.state.transform);
self.surface.canvas.restore();
self.path.transform_self(&s.transform.invert().unwrap());
if let Some(inverse) = s.transform.invert() {
self.path.transform_self(&inverse);
}
self.state = s;
}
}
Expand Down Expand Up @@ -275,9 +277,11 @@ impl Context {

pub fn scale(&mut self, x: f32, y: f32) {
let s = &mut self.state;
let mut inverse = Matrix::identity();
inverse.pre_scale(1f32 / x, 1f32 / y);
self.path.transform_self(&inverse);
if x != 0.0 && y != 0.0 {
let mut inverse = Matrix::identity();
inverse.pre_scale(1f32 / x, 1f32 / y);
self.path.transform_self(&inverse);
}
s.transform.pre_scale(x, y);
self.surface.canvas.set_transform(&s.transform);
}
Expand Down

0 comments on commit 973adab

Please sign in to comment.