mirror of
https://github.com/penpot/penpot.git
synced 2025-07-19 01:47:17 +02:00
🐛 Fix rasterizer not setting intrinsic size
This commit is contained in:
parent
42b68a786e
commit
611b90f5fb
1 changed files with 32 additions and 11 deletions
|
@ -54,18 +54,34 @@
|
||||||
(obj/set! image "onerror" nil)
|
(obj/set! image "onerror" nil)
|
||||||
(obj/set! image "onabort" nil))))))
|
(obj/set! image "onabort" nil))))))
|
||||||
|
|
||||||
(defn- svg-get-size
|
(defn- svg-get-adjusted-size
|
||||||
|
"Returns the adjusted size of an SVG."
|
||||||
|
[width height max]
|
||||||
|
(let [ratio (/ width height)]
|
||||||
|
(if (> width height)
|
||||||
|
[max (* max (/ 1 ratio))]
|
||||||
|
[(* max ratio) max])))
|
||||||
|
|
||||||
|
(defn- svg-get-size-from-viewbox
|
||||||
|
"Returns the size of an SVG from its viewbox."
|
||||||
[svg max]
|
[svg max]
|
||||||
(let [doc (get-document-element svg)
|
(let [doc (get-document-element svg)
|
||||||
vbox (dom/get-attribute doc "viewBox")]
|
vbox (dom/get-attribute doc "viewBox")]
|
||||||
(when (string? vbox)
|
(when (string? vbox)
|
||||||
(let [[_ _ width height] (str/split vbox #"\s+")
|
(let [[_ _ width height] (str/split vbox #"\s+")
|
||||||
width (d/parse-integer width 0)
|
width (d/parse-integer width 0)
|
||||||
height (d/parse-integer height 0)
|
height (d/parse-integer height 0)]
|
||||||
ratio (/ width height)]
|
(svg-get-adjusted-size width height max)))))
|
||||||
(if (> width height)
|
|
||||||
[max (* max (/ 1 ratio))]
|
(defn- svg-get-size-from-intrinsic-size
|
||||||
[(* max ratio) max])))))
|
"Returns the size of an SVG from its intrinsic size."
|
||||||
|
[svg max]
|
||||||
|
(let [doc (get-document-element svg)
|
||||||
|
width (dom/get-attribute doc "width")
|
||||||
|
height (dom/get-attribute doc "height")
|
||||||
|
width (d/parse-integer width 0)
|
||||||
|
height (d/parse-integer height 0)]
|
||||||
|
(svg-get-adjusted-size width height max)))
|
||||||
|
|
||||||
(defn- svg-has-intrinsic-size?
|
(defn- svg-has-intrinsic-size?
|
||||||
"Returns true if the SVG has an intrinsic size."
|
"Returns true if the SVG has an intrinsic size."
|
||||||
|
@ -75,14 +91,19 @@
|
||||||
height (dom/get-attribute doc "height")]
|
height (dom/get-attribute doc "height")]
|
||||||
(d/num? width height)))
|
(d/num? width height)))
|
||||||
|
|
||||||
|
(defn- svg-get-size
|
||||||
|
[svg max]
|
||||||
|
(if (svg-has-intrinsic-size? svg)
|
||||||
|
(svg-get-size-from-intrinsic-size svg max)
|
||||||
|
(svg-get-size-from-viewbox svg max)))
|
||||||
|
|
||||||
(defn- svg-set-intrinsic-size!
|
(defn- svg-set-intrinsic-size!
|
||||||
"Sets the intrinsic size of an SVG to the given max size."
|
"Sets the intrinsic size of an SVG to the given max size."
|
||||||
[^js svg max]
|
[^js svg max]
|
||||||
(when-not (svg-has-intrinsic-size? svg)
|
|
||||||
(let [doc (get-document-element svg)
|
(let [doc (get-document-element svg)
|
||||||
[w h] (svg-get-size svg max)]
|
[w h] (svg-get-size svg max)]
|
||||||
(dom/set-attribute! doc "width" (dm/str w))
|
(dom/set-attribute! doc "width" (dm/str w))
|
||||||
(dom/set-attribute! doc "height" (dm/str h))))
|
(dom/set-attribute! doc "height" (dm/str h)))
|
||||||
svg)
|
svg)
|
||||||
|
|
||||||
(defn- fetch-as-data-uri
|
(defn- fetch-as-data-uri
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue