From c55c9900b7af2bd69b2a46a1db8fade8cdedd4ba Mon Sep 17 00:00:00 2001 From: Eva Marco Date: Wed, 25 Nov 2026 15:53:03 +0100 Subject: [PATCH] :bug: Keep a valid transform when a shape is squashed far from the origin (#13325) * :bug: Keep a valid transform when a shape is squashed far from the origin A path squashed almost flat tens of millions of pixels from the origin got an inverse transform with values beyond the safe range, for example :f -1.04E10. The backend rejected the shape as invalid and the file stopped saving. apply-transform already kept the previous transform pair when the new one had no inverse. It now does the same when either matrix is not a valid matrix. AI-assisted-by: claude-opus-5-5 * :bug: Check the squashed geometry in the transform overflow test From the review: once the fallback keeps the identity pair, the product of the transform and its inverse is the identity by construction. Also check that the selrect and the points still follow the squash, and point the test comment to #13324. AI-assisted-by: claude-opus-5-5 --- .../app/common/geom/shapes/transforms.cljc | 5 +++- .../test/common_tests/geom_shapes_test.cljc | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/common/src/app/common/geom/shapes/transforms.cljc b/common/src/app/common/geom/shapes/transforms.cljc index 7ba56f2197..db74581994 100644 --- a/common/src/app/common/geom/shapes/transforms.cljc +++ b/common/src/app/common/geom/shapes/transforms.cljc @@ -347,10 +347,13 @@ center (gco/points->center points) selrect (calculate-selrect points center) + ;; Keep the previous pair when the new one is not a valid matrix: + ;; a shape squashed almost flat far from the origin has an inverse + ;; whose values overflow the range the backend accepts. [transform inverse] (let [transform (calculate-transform points center selrect) inverse (when (some? transform) (gmt/inverse transform))] - (if (and (some? transform) (some? inverse)) + (if (and (gmt/valid-matrix? transform) (gmt/valid-matrix? inverse)) [transform inverse] [(:transform shape (gmt/matrix)) (:transform-inverse shape (gmt/matrix))])) diff --git a/common/test/common_tests/geom_shapes_test.cljc b/common/test/common_tests/geom_shapes_test.cljc index e60ded0fe7..36d23e2618 100644 --- a/common/test/common_tests/geom_shapes_test.cljc +++ b/common/test/common_tests/geom_shapes_test.cljc @@ -271,3 +271,26 @@ (t/is (true? (:flip-y result))) ;; rotation must not be negated when both axes are flipped (t/is (mth/close? 30 (:rotation result)))))) + +(t/deftest squashed-shape-far-from-origin-keeps-a-valid-transform + ;; Regression for https://github.com/penpot/penpot/issues/13324. + ;; Squashing a path almost flat far from the origin lost precision in + ;; the transform, its inverse went beyond the safe range, and the backend + ;; rejected the shape. + (let [x -34647600 + y -46672500 + shape (create-test-shape :path {:content [{:command :move-to :params {:x x :y y}} + {:command :line-to :params {:x (+ x 10) :y (+ y 10)}}]}) + squash (gmt/scale-matrix (gpt/point 1 0.000001) (gpt/point x y)) + result (gsh/apply-transform shape squash) + mtx (gmt/multiply (:transform result) (:transform-inverse result))] + (t/is (gmt/valid-matrix? (:transform result))) + (t/is (gmt/valid-matrix? (:transform-inverse result))) + (t/is (gmt/close? (gmt/matrix) mtx)) + + (t/testing "the rest of the geometry still follows the squash" + (t/is (close? 0.01 (get-in result [:selrect :height]))) + (t/is (every? true? (map gpt/close? + (gsh/transform-points (:points shape) squash) + (:points result))))))) +