mirror of
https://github.com/penpot/penpot.git
synced 2026-11-09 11:30:54 +00:00
* 🐛 Normalize stored SVG attribute keys to kebab-case The v3 reader already emits kebab-case keys, but birth stored the camelCase React prop names, so import and export never converged: fill rules painted wrongly and filter defs lost their blur. Birth now normalizes every stored attribute key to kebab-case through the `svg-props` whitelist (`attrs->kebab-props`), applied on all element paths of the svg importer, on the file builder and on defs. svg-raw `:content` is kebab-ized in spelling only and keeps unknown keys, because the sidebar reads them. Non-spec attrs Penpot must keep live in `penpot-extra-attrs`, seeded with `:stroke-style`. Render boundaries translate back to camelCase for React and for the wasm props, and the shape schema declares the closed kebab contract derived from that same whitelist, so birth and validation cannot drift. Migration `0035-fix-shape-svg-attrs-and-defs` converts old files once with the birth transforms, and carries back the svg-viewbox rect normalization the withdrawn `0021` had. That old id leaves `available-migrations`, so files recording it migrate again through this one, and the binfile cleaner drops its own svg repair. Review fixes included here: `setup-opacity` and the blend mode read the stored keys, gradient stops accept `:style` as a CSS string or a map, `inherit-attributes` reads `:style` so a child's inline style beats the inherited group attr per CSS, and the svg-attrs menu shows spec spelling (`viewBox`, `class`) through `stored-attr-display-name`. Tests cover the migration, the closed svg-attr schemas and the import/export round trip; the svg-content schema test pins its sample seed, which dealt no svg-raw shape on unlucky runs. Closes #11947 AI-assisted-by: muse-spark-1.3-contributor * 🐛 Keep legacy svg node tags and transforms valid in migration 0035 The closed svg node schemas require keyword tags and map attrs, but stored files hold def and svg-raw content tags as strings, and some node `:attrs` as nil. Migration 0035 left them as they were, so the file data check that runs after migrating rejected those files and they could not be opened. `normalize-node-tree` now turns string tags into keywords and nil attrs into an empty map, at birth and in 0035. With keyword tags, the svg defs renderer applies the shape transform to user-space defs. Legacy `:svg-transform` values are plain maps, which multiply into NaN matrices. `gmt/coerce` turns them into matrices, in 0035 and in the binfile cleaner, because every v3 import decodes the transform as a plain map again. Legacy masks and gradients now render as designed in the svg renderer. The binfile test helper calls `export-files!` with the current config keys. AI-assisted-by: claude-opus-5-5 ---------
139 lines
4.4 KiB
Clojure
139 lines
4.4 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
;;
|
|
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
|
|
|
(ns common-tests.geom-test
|
|
(:require
|
|
[app.common.geom.matrix :as gmt]
|
|
[app.common.geom.point :as gpt]
|
|
[app.common.math :as mth]
|
|
[app.common.schema :as sm]
|
|
[clojure.test :as t]))
|
|
|
|
(t/deftest point-constructors-test
|
|
(t/testing "Create point with both coordinates"
|
|
(let [p (gpt/point 1 2)]
|
|
(t/is (= (:x p) 1))
|
|
(t/is (= (:y p) 2))))
|
|
|
|
(t/testing "Create point with single coordinate"
|
|
(let [p (gpt/point 1)]
|
|
(t/is (= (:x p) 1))
|
|
(t/is (= (:y p) 1))))
|
|
|
|
(t/testing "Create empty point"
|
|
(let [p (gpt/point)]
|
|
(t/is (= (:x p) 0))
|
|
(t/is (= (:y p) 0)))))
|
|
|
|
(t/deftest point-add-test
|
|
(t/testing "Adds two points together"
|
|
(let [p1 (gpt/point 1 1)
|
|
p2 (gpt/point 2 2)
|
|
p3 (gpt/add p1 p2)]
|
|
(t/is (= (:x p3) 3))
|
|
(t/is (= (:y p3) 3)))))
|
|
|
|
(t/deftest point-subtract-test
|
|
(t/testing "Point substraction"
|
|
(let [p1 (gpt/point 3 3)
|
|
p2 (gpt/point 2 2)
|
|
p3 (gpt/subtract p1 p2)]
|
|
(t/is (= (:x p3) 1))
|
|
(t/is (= (:y p3) 1)))))
|
|
|
|
(t/deftest point-distance-test
|
|
(let [p1 (gpt/point 0 0)
|
|
p2 (gpt/point 10 0)
|
|
d (gpt/distance p1 p2)]
|
|
(t/is (number? d))
|
|
(t/is (= d 10.0))))
|
|
|
|
(t/deftest point-length-test
|
|
(let [p1 (gpt/point 10 0)
|
|
ln (gpt/length p1)]
|
|
(t/is (number? ln))
|
|
(t/is (= ln 10.0))))
|
|
|
|
(t/deftest point-angle-test
|
|
(t/testing "Get angle a 90 degree angle"
|
|
(let [p1 (gpt/point 0 10)
|
|
angle (gpt/angle p1)]
|
|
(t/is (number? angle))
|
|
(t/is (= angle 90.0))))
|
|
|
|
(t/testing "Get 45 degree angle"
|
|
(let [p1 (gpt/point 0 10)
|
|
p2 (gpt/point 10 10)
|
|
angle (gpt/angle-with-other p1 p2)]
|
|
(t/is (number? angle))
|
|
(t/is (mth/close? angle 45.0)))))
|
|
|
|
(t/deftest matrix-constructors-test
|
|
(let [m (gmt/matrix)]
|
|
(t/is (= 1.0 (.-a m)))
|
|
(t/is (= 0.0 (.-b m)))
|
|
(t/is (= 0.0 (.-c m)))
|
|
(t/is (= 1.0 (.-d m)))
|
|
(t/is (= 0.0 (.-e m)))
|
|
(t/is (= 0.0 (.-f m))))
|
|
(let [m (gmt/matrix 1 1 1 2 2 2)]
|
|
(t/is (= 1.0 (.-a m)))
|
|
(t/is (= 1.0 (.-b m)))
|
|
(t/is (= 1.0 (.-c m)))
|
|
(t/is (= 2.0 (.-d m)))
|
|
(t/is (= 2.0 (.-e m)))
|
|
(t/is (= 2.0 (.-f m)))))
|
|
|
|
(t/deftest matrix-translate-test
|
|
(let [m (-> (gmt/matrix)
|
|
(gmt/translate (gpt/point 2 10)))]
|
|
(t/is (= m (gmt/matrix 1 0 0 1 2 10)))))
|
|
|
|
(t/deftest matrix-scale-test
|
|
(let [m (-> (gmt/matrix)
|
|
(gmt/scale (gpt/point 2)))]
|
|
(t/is (= m (gmt/matrix 2 0 0 2 0 0)))))
|
|
|
|
(t/deftest matrix-rotate-test
|
|
(let [m (-> (gmt/matrix)
|
|
(gmt/rotate 10))]
|
|
(t/is (= m (gmt/matrix 0.984807753012208 0.17364817766693033 -0.17364817766693033 0.984807753012208 0 0)))))
|
|
|
|
;; ---- matrix->str (no trailing comma) ----
|
|
|
|
(t/deftest matrix-str-roundtrip-test
|
|
(t/testing "Identity matrix encodes and decodes back to equal matrix"
|
|
(let [m (gmt/matrix)
|
|
enc (sm/encode gmt/schema:matrix m (sm/string-transformer))
|
|
dec (sm/decode gmt/schema:matrix enc (sm/string-transformer))]
|
|
(t/is (string? enc))
|
|
;; Must not end with a comma
|
|
(t/is (not= \, (last enc)))
|
|
(t/is (gmt/close? m dec))))
|
|
|
|
(t/testing "Arbitrary matrix encodes without trailing comma and round-trips"
|
|
(let [m (gmt/matrix 2 0.5 -0.5 3 10 20)
|
|
enc (sm/encode gmt/schema:matrix m (sm/string-transformer))
|
|
dec (sm/decode gmt/schema:matrix enc (sm/string-transformer))]
|
|
(t/is (string? enc))
|
|
(t/is (not= \, (last enc)))
|
|
(t/is (gmt/close? m dec))))
|
|
|
|
(t/testing "Encoded string contains exactly 5 commas (6 fields)"
|
|
(let [m (gmt/matrix 1 0 0 1 0 0)
|
|
enc (sm/encode gmt/schema:matrix m (sm/string-transformer))]
|
|
(t/is (= 5 (count (filter #(= \, %) enc)))))))
|
|
|
|
(t/deftest matrix-coerce-test
|
|
(t/is (= (gmt/matrix 1 0 0 1 10 20)
|
|
(gmt/coerce {:a 1.0 :b 0.0 :c 0.0 :d 1.0 :e 10.0 :f 20.0}))
|
|
"a plain map becomes a matrix")
|
|
(t/is (gmt/matrix? (gmt/coerce {:a 1 :b 0 :c 0 :d 1 :e 0 :f 0})))
|
|
(let [m (gmt/matrix 2 0 0 2 0 0)]
|
|
(t/is (identical? m (gmt/coerce m)) "matrices pass through"))
|
|
(t/is (= {:a 1 :b 0} (gmt/coerce {:a 1 :b 0})) "incomplete maps are left as they are")
|
|
(t/is (nil? (gmt/coerce nil))))
|