From 8493e510705130d07285953d454e15b3e8ee98cc Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 26 Jul 2021 11:54:23 +0200 Subject: [PATCH] :bug: Fix problem with svg's viewbox --- .../app/main/data/workspace/svg_upload.cljs | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/frontend/src/app/main/data/workspace/svg_upload.cljs b/frontend/src/app/main/data/workspace/svg_upload.cljs index 048e4bb96..49942812a 100644 --- a/frontend/src/app/main/data/workspace/svg_upload.cljs +++ b/frontend/src/app/main/data/workspace/svg_upload.cljs @@ -7,10 +7,13 @@ (ns app.main.data.workspace.svg-upload (:require [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] + [app.common.math :as mth] [app.common.pages :as cp] + [app.common.spec :refer [max-safe-int min-safe-int]] [app.common.uuid :as uuid] [app.main.data.workspace.changes :as dch] [app.main.data.workspace.common :as dwc] @@ -28,13 +31,36 @@ (defonce default-circle {:r 0 :cx 0 :cy 0}) (defonce default-image {:x 0 :y 0 :width 1 :height 1}) +(defn- assert-valid-num [attr num] + (when (or (nil? num) + (mth/nan? num) + (not (mth/finite? num)) + (>= num max-safe-int ) + (<= num min-safe-int)) + (ex/raise (str (d/name attr) " attribute invalid: " num))) + + ;; If the number is between 0-1 we round to 1 (same in negative form + (cond + (and (> num 0) (< num 1)) 1 + (and (< num 0) (> num -1)) -1 + :else num)) + +(defn- assert-valid-pos-num [attr num] + (let [num (assert-valid-num attr num)] + (when (< num 0) + (ex/raise (str (d/name attr) " attribute invalid: " num))) + num)) + (defn- svg-dimensions [data] (let [width (get-in data [:attrs :width] 100) height (get-in data [:attrs :height] 100) viewbox (get-in data [:attrs :viewBox] (str "0 0 " width " " height)) - [x y width height] (->> (str/split viewbox " ") + [x y width height] (->> (str/split viewbox #"\s+") (map d/parse-double))] - [x y width height])) + [(assert-valid-num :x x) + (assert-valid-num :y y) + (assert-valid-pos-num :width width) + (assert-valid-pos-num :height height)])) (defn tag->name "Given a tag returns its layer name" @@ -467,4 +493,6 @@ (dwc/select-shapes (d/ordered-set root-id)))) (catch :default e - (.error js/console "Error upload" e)))))) + (.error js/console "Error SVG" e) + (rx/throw {:type :svg-parser + :data e}))))))