🐛 Keep a valid transform when a shape is squashed far from the origin (#13325)

* 🐛 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

* 🐛 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
This commit is contained in:
Eva Marco 2026-11-25 15:53:03 +01:00 committed by GitHub
parent aab5a4752f
commit c55c9900b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -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))]))

View File

@ -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)))))))