0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-13 16:21:57 -05:00

🐛 Fix svg import making it more resilient

This commit is contained in:
Alejandro Alonso 2023-05-12 10:01:45 +02:00 committed by Alonso Torres
parent 00d625ee33
commit cb5ae99e1d

View file

@ -8,6 +8,7 @@
(:require
[app.common.colors :as clr]
[app.common.data :as d]
[app.common.exceptions :as ex]
[app.common.geom.matrix :as gmt]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
@ -30,6 +31,7 @@
[app.util.svg :as usvg]
[app.util.webapi :as wapi]
[beicon.core :as rx]
[clojure.spec.alpha :as s]
[cuerdas.core :as str]
[potok.core :as ptk]))
@ -38,11 +40,12 @@
(defonce default-image {:x 0 :y 0 :width 1 :height 1 :rx 0 :ry 0})
(defn- assert-valid-num [attr num]
(us/verify!
:expr (and (d/num? num)
(when-not (and (d/num? num)
(<= num max-safe-int)
(>= num min-safe-int))
:hint (str/ffmt "%1 attribute has invalid value: %2" (d/name attr) num))
(ex/raise :type :assertion
:code :expr-validation
:hint (str/ffmt "%1 attribute has invalid value: %2" (d/name attr) num)))
;; If the number is between 0-1 we round to 1 (same in negative form
(cond
@ -52,11 +55,24 @@
(defn- assert-valid-pos-num
[attr num]
(us/verify!
:expr (pos? num)
:hint (str/ffmt "%1 attribute should be positive" (d/name attr)))
(when-not (pos? num)
(ex/raise :type :assertion
:code :expr-validation
:hint (str/ffmt "%1 attribute should be positive" (d/name attr))))
num)
(defn- assert-valid-blend-mode
[mode]
(let [clean-value (-> mode
str/trim
str/lower
keyword)]
(when-not (s/valid? ::cts/blend-mode clean-value)
(ex/raise :type :assertion
:code :expr-validation
:hint (str/ffmt "%1 is not a valid blend mode" clean-value)))
clean-value))
(defn- svg-dimensions [data]
(let [width (get-in data [:attrs :width] 100)
height (get-in data [:attrs :height] 100)
@ -133,12 +149,12 @@
(get-in shape [:svg-attrs :stroke-opacity])
(-> (update :svg-attrs dissoc :stroke-opacity)
(assoc-in [:strokes 0 :stroke-opacity] (-> (get-in shape [:svg-attrs :stroke-opacity])
(d/parse-double))))
(d/parse-double 1))))
(get-in shape [:svg-attrs :style :stroke-opacity])
(-> (update-in [:svg-attrs :style] dissoc :stroke-opacity)
(assoc-in [:strokes 0 :stroke-opacity] (-> (get-in shape [:svg-attrs :style :stroke-opacity])
(d/parse-double))))
(d/parse-double 1))))
(get-in shape [:svg-attrs :stroke-width])
(-> (update :svg-attrs dissoc :stroke-width)
@ -165,21 +181,21 @@
(get-in shape [:svg-attrs :opacity])
(-> (update :svg-attrs dissoc :opacity)
(assoc :opacity (-> (get-in shape [:svg-attrs :opacity])
(d/parse-double))))
(d/parse-double 1))))
(get-in shape [:svg-attrs :style :opacity])
(-> (update-in [:svg-attrs :style] dissoc :opacity)
(assoc :opacity (-> (get-in shape [:svg-attrs :style :opacity])
(d/parse-double))))
(d/parse-double 1))))
(get-in shape [:svg-attrs :mix-blend-mode])
(-> (update :svg-attrs dissoc :mix-blend-mode)
(assoc :blend-mode (-> (get-in shape [:svg-attrs :mix-blend-mode]) keyword)))
(assoc :blend-mode (-> (get-in shape [:svg-attrs :mix-blend-mode]) assert-valid-blend-mode)))
(get-in shape [:svg-attrs :style :mix-blend-mode])
(-> (update-in [:svg-attrs :style] dissoc :mix-blend-mode)
(assoc :blend-mode (-> (get-in shape [:svg-attrs :style :mix-blend-mode]) keyword)))))
(assoc :blend-mode (-> (get-in shape [:svg-attrs :style :mix-blend-mode]) assert-valid-blend-mode)))))
(defn create-raw-svg [name frame-id svg-data {:keys [tag attrs] :as data}]
(let [{:keys [x y width height offset-x offset-y]} svg-data]