From 84b3e14da70db07ae589950e7203c362c1259be8 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 18 Nov 2024 23:37:27 +0800 Subject: [PATCH] fix: panic when restore from scale(0,0) state --- __test__/regression.spec.ts | 10 ++++++++++ src/ctx.rs | 12 ++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/__test__/regression.spec.ts b/__test__/regression.spec.ts index f88077c6..16faf32e 100644 --- a/__test__/regression.spec.ts +++ b/__test__/regression.spec.ts @@ -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() + }) +}) diff --git a/src/ctx.rs b/src/ctx.rs index a8d7dcad..4e7d8344 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -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; } } @@ -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); }