🐛 Add fix for percent number on style attrs on parsing svg

This commit is contained in:
Andrey Antukh 2024-01-29 23:47:54 +01:00
parent 03eca0d9a2
commit c4ee88dc66
2 changed files with 32 additions and 15 deletions

View file

@ -895,9 +895,10 @@
(defn map-nodes [mapfn node] (defn map-nodes [mapfn node]
(let [update-content (let [update-content
(fn [content] (cond->> content (fn [content]
(vector? content) (cond->> content
(mapv (partial map-nodes mapfn))))] (vector? content)
(mapv (partial map-nodes mapfn))))]
(cond-> node (cond-> node
(map? node) (map? node)
@ -922,7 +923,8 @@
value))) value)))
(defn fix-default-values (defn fix-default-values
"Gives values to some SVG elements which defaults won't work when imported into the platform" "Gives values to some SVG elements which defaults won't work when
imported into the platform"
[svg-data] [svg-data]
(let [add-defaults (let [add-defaults
(fn [{:keys [tag attrs] :as node}] (fn [{:keys [tag attrs] :as node}]
@ -984,29 +986,43 @@
(fix-percent-attrs-viewbox [attrs] (fix-percent-attrs-viewbox [attrs]
(d/mapm fix-percent-attr-viewbox attrs)) (d/mapm fix-percent-attr-viewbox attrs))
(fix-percent-attr-numeric [_ attr-val] (fix-percent-attr-numeric-val [val]
(let [is-percent? (str/ends-with? attr-val "%")] (let [val (d/parse-double (str/rtrim val "%"))]
(if is-percent? (str (/ val 100))))
(str (let [attr-num (d/parse-double (str/rtrim attr-val "%"))]
(/ attr-num 100)))
attr-val)))
(fix-percent-attrs-numeric [attrs] (fix-percent-attr-numeric [attrs key val]
(d/mapm fix-percent-attr-numeric attrs)) (cond
(= key :style)
(let [val (->> (str/split val ";")
(map (fn [val]
(if (str/ends-with? val "%")
(let [[k v] (str/split val ":" 2)
v (fix-percent-attr-numeric-val v)]
(str k ":" v))
val)))
(str/join ";"))]
(assoc attrs key val))
(str/ends-with? val "%")
(assoc attrs key (fix-percent-attr-numeric-val val))
:else
attrs))
(fix-percent-values [node] (fix-percent-values [node]
(let [units (or (get-in node [:attrs :filterUnits]) (let [units (or (get-in node [:attrs :filterUnits])
(get-in node [:attrs :gradientUnits]) (get-in node [:attrs :gradientUnits])
(get-in node [:attrs :patternUnits]) (get-in node [:attrs :patternUnits])
(get-in node [:attrs :clipUnits]))] (get-in node [:attrs :clipUnits]))]
(cond-> node (cond-> node
(or (= "objectBoundingBox" units) (nil? units)) (or (= "objectBoundingBox" units) (nil? units))
(update :attrs fix-percent-attrs-numeric) (update :attrs #(reduce-kv fix-percent-attr-numeric % %))
(not= "objectBoundingBox" units) (not= "objectBoundingBox" units)
(update :attrs fix-percent-attrs-viewbox))))] (update :attrs fix-percent-attrs-viewbox))))]
(->> svg-data (map-nodes fix-percent-values))))) (map-nodes fix-percent-values svg-data))))
(defn collect-images [svg-data] (defn collect-images [svg-data]
(let [redfn (fn [acc {:keys [tag attrs]}] (let [redfn (fn [acc {:keys [tag attrs]}]

View file

@ -193,7 +193,8 @@
(defn create-group (defn create-group
[name frame-id {:keys [x y width height offset-x offset-y] :as svg-data} {:keys [attrs]}] [name frame-id {:keys [x y width height offset-x offset-y] :as svg-data} {:keys [attrs]}]
(let [transform (csvg/parse-transform (:transform attrs)) (let [transform (csvg/parse-transform (:transform attrs))
attrs (-> (d/without-keys attrs csvg/inheritable-props) attrs (-> attrs
(d/without-keys csvg/inheritable-props)
(csvg/attrs->props)) (csvg/attrs->props))
vbox (grc/make-rect offset-x offset-y width height)] vbox (grc/make-rect offset-x offset-y width height)]
(cts/setup-shape (cts/setup-shape