From be27ce4914f7ca576b467c6def2e45259c0ae476 Mon Sep 17 00:00:00 2001
From: "alonso.torres" <alonso.torres@kaleidos.net>
Date: Wed, 29 Mar 2023 16:00:07 +0200
Subject: [PATCH] :bug: Fix problem with invalid SVG shape

---
 common/src/app/common/geom/shapes/common.cljc    | 15 ++++++++++++++-
 common/src/app/common/geom/shapes/modifiers.cljc |  6 +++++-
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/common/src/app/common/geom/shapes/common.cljc b/common/src/app/common/geom/shapes/common.cljc
index 710c15e78..a0fcdb4b5 100644
--- a/common/src/app/common/geom/shapes/common.cljc
+++ b/common/src/app/common/geom/shapes/common.cljc
@@ -9,7 +9,8 @@
    [app.common.data :as d]
    [app.common.geom.matrix :as gmt]
    [app.common.geom.point :as gpt]
-   [app.common.geom.shapes.rect :as gpr]))
+   [app.common.geom.shapes.rect :as gpr]
+   [app.common.math :as mth]))
 
 (defn center-rect
   [{:keys [x y width height]}]
@@ -71,3 +72,15 @@
   [{:keys [x1 y1 x2 y2] :as sr} matrix]
   (let [[c1 c2] (transform-points [(gpt/point x1 y1) (gpt/point x2 y2)] matrix)]
     (gpr/corners->selrect c1 c2)))
+
+(defn invalid-geometry?
+  [{:keys [points selrect]}]
+
+  (or (mth/nan? (:x selrect))
+      (mth/nan? (:y selrect))
+      (mth/nan? (:width selrect))
+      (mth/nan? (:height selrect))
+      (some (fn [p]
+              (or (mth/nan? (:x p))
+                  (mth/nan? (:y p))))
+            points)))
diff --git a/common/src/app/common/geom/shapes/modifiers.cljc b/common/src/app/common/geom/shapes/modifiers.cljc
index 582d2a404..17879b765 100644
--- a/common/src/app/common/geom/shapes/modifiers.cljc
+++ b/common/src/app/common/geom/shapes/modifiers.cljc
@@ -9,6 +9,7 @@
    [app.common.data :as d]
    [app.common.data.macros :as dm]
    [app.common.geom.point :as gpt]
+   [app.common.geom.shapes.common :as gco]
    [app.common.geom.shapes.constraints :as gct]
    [app.common.geom.shapes.flex-layout :as gcfl]
    [app.common.geom.shapes.grid-layout :as gcgl]
@@ -180,6 +181,7 @@
     (let [children     (->> children
                             (map (d/getf objects))
                             (remove :hidden)
+                            (remove gco/invalid-geometry?)
                             (map apply-modifiers))
           layout-data  (gcfl/calc-layout-data parent children @transformed-parent-bounds)
           children     (into [] (cond-> children (not (:reverse? layout-data)) reverse))
@@ -215,6 +217,7 @@
               modif-tree))]
     (let [children     (->> (cph/get-immediate-children objects (:id parent))
                             (remove :hidden)
+                            (remove gco/invalid-geometry?)
                             (map apply-modifiers))
           grid-data    (gcgl/calc-layout-data parent children @transformed-parent-bounds)]
       (loop [modif-tree modif-tree
@@ -249,7 +252,8 @@
                 (ctm/resize (gpt/point 1 scale-height) origin (:transform parent) (:transform-inverse parent)))))
 
         children (->> (cph/get-immediate-children objects parent-id)
-                      (remove :hidden))
+                      (remove :hidden)
+                      (remove gco/invalid-geometry?))
 
         content-bounds
         (when (and (d/not-empty? children) (or (ctl/auto-height? parent) (ctl/auto-width? parent)))