From 10485848b85335d4bca5c1cbd43f443e92bb6bd7 Mon Sep 17 00:00:00 2001
From: John Beard <john.j.beard@gmail.com>
Date: Tue, 31 Dec 2024 21:54:37 +0800
Subject: [PATCH] SVG import: import unclosed, filled shapes as a separate fill
 and stroke object

This means that validly unclosed paths can be imported correctly.

Fixes: https://gitlab.com/kicad/code/kicad/-/issues/18477
---
 common/import_gfx/svg_import_plugin.cpp | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/common/import_gfx/svg_import_plugin.cpp b/common/import_gfx/svg_import_plugin.cpp
index 2c6d56426f..173b68bf00 100644
--- a/common/import_gfx/svg_import_plugin.cpp
+++ b/common/import_gfx/svg_import_plugin.cpp
@@ -189,9 +189,22 @@ bool SVG_IMPORT_PLUGIN::Import()
 
         for( NSVGpath* path = shape->paths; path != nullptr; path = path->next )
         {
-            bool closed = path->closed || filled || rule == GRAPHICS_IMPORTER::PF_EVEN_ODD;
+            if( filled && !path->closed )
+            {
+                // KiCad doesn't support a single object representing a filled shape that is not closed
+                // so create a filled, closed shape for the fill, and an unfilled, open shape for the outline
+                IMPORTED_STROKE noStroke( 0, LINE_STYLE::SOLID, COLOR4D::UNSPECIFIED );
+                DrawPath( path->pts, path->npts, true, noStroke, true, fillColor );
+                DrawPath( path->pts, path->npts, false, stroke, false, COLOR4D::UNSPECIFIED );
+            }
+            else
+            {
+                // Either the shape has fill and no stroke, so we implicitly close it (for no difference),
+                // or it's really closed
+                const bool closed = path->closed || filled;
 
-            DrawPath( path->pts, path->npts, closed, stroke, filled, fillColor );
+                DrawPath( path->pts, path->npts, closed, stroke, filled, fillColor );
+            }
         }
     }