From 2a1e4f9361f69b18ee7bdf0d0d0dcbe231cbf400 Mon Sep 17 00:00:00 2001 From: hneemann Date: Sun, 2 Dec 2018 09:50:33 +0100 Subject: [PATCH] fixed an issue with the polygon parser, see #218 --- .../digital/draw/graphics/PolygonParser.java | 15 ++++++++-- .../draw/shapes/custom/SvgImporterTest.java | 29 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/neemann/digital/draw/graphics/PolygonParser.java b/src/main/java/de/neemann/digital/draw/graphics/PolygonParser.java index 35f187e8d..97679f882 100644 --- a/src/main/java/de/neemann/digital/draw/graphics/PolygonParser.java +++ b/src/main/java/de/neemann/digital/draw/graphics/PolygonParser.java @@ -9,6 +9,7 @@ package de.neemann.digital.draw.graphics; * Creates a polygon from a path */ public class PolygonParser { + enum Token {EOF, COMMAND, NUMBER} private final String path; @@ -18,6 +19,7 @@ public class PolygonParser { private float value; private float x; private float y; + private VectorFloat polyStart; private VectorInterface lastQuadraticControlPoint; private VectorInterface lastCubicControlPoint; @@ -132,7 +134,7 @@ public class PolygonParser { closedPending = false; p.addClosePath(); } - p.addMoveTo(nextVector()); + p.addMoveTo(setPolyStart(nextVector())); clearControl(); break; case 'm': @@ -140,7 +142,7 @@ public class PolygonParser { closedPending = false; p.addClosePath(); } - p.addMoveTo(nextVectorInc()); + p.addMoveTo(setPolyStart(nextVectorInc())); clearControl(); break; case 'V': @@ -206,6 +208,10 @@ public class PolygonParser { case 'Z': case 'z': closedPending = true; + if (polyStart != null) { + x = polyStart.getXFloat(); + y = polyStart.getYFloat(); + } clearControl(); break; default: @@ -217,6 +223,11 @@ public class PolygonParser { return p; } + private VectorFloat setPolyStart(VectorFloat v) { + polyStart = v; + return v; + } + private VectorInterface getCurrent() { return new VectorFloat(x, y); } diff --git a/src/test/java/de/neemann/digital/draw/shapes/custom/SvgImporterTest.java b/src/test/java/de/neemann/digital/draw/shapes/custom/SvgImporterTest.java index eccae3f37..e2f689adf 100644 --- a/src/test/java/de/neemann/digital/draw/shapes/custom/SvgImporterTest.java +++ b/src/test/java/de/neemann/digital/draw/shapes/custom/SvgImporterTest.java @@ -327,6 +327,19 @@ public class SvgImporterTest extends TestCase { .check(); } + public void testInkscape4() throws IOException, SvgException, PolygonParser.ParserException, PinException { + CustomShapeDescription custom = new SvgImporter( + in("\n" + + " \n" + + "")).create(); + + new CSDChecker(custom) + .checkPolygon("M 10,10 L 10,40 L 40,40 L 40,10 Z M 20,20 L 30,20 L 30,30 L 20,30 L 20,20 Z") + .check(); + } + //***************************************************************************************************** @@ -373,6 +386,11 @@ public class SvgImporterTest extends TestCase { fail("not enough elements found in the csd"); } + private CSDChecker checkPolygonDebug(String s) throws PolygonParser.ParserException { + checker.add(new CheckPolygon(new PolygonParser(s).create()).setDebug(true)); + return this; + } + private CSDChecker checkPolygon(String s) throws PolygonParser.ParserException { checker.add(new CheckPolygon(new PolygonParser(s).create())); return this; @@ -425,6 +443,7 @@ public class SvgImporterTest extends TestCase { private static class CheckPolygon implements Checker { private final Polygon should; + private boolean debug; private CheckPolygon(Polygon should) { this.should = should; @@ -441,7 +460,10 @@ public class SvgImporterTest extends TestCase { ArrayList isPoints = new ArrayList<>(); polygon.traverse(isPoints::add); - //System.out.println(polygon); + if (debug) { + System.out.println("should: " + should); + System.out.println("was : " + polygon); + } assertEquals("not the correct polygon size", shouldPoints.size(), isPoints.size()); for (int i = 0; i < shouldPoints.size(); i++) { @@ -451,6 +473,11 @@ public class SvgImporterTest extends TestCase { assertEquals("y coordinate " + i, sh.getYFloat(), is.getYFloat(), 1e-4); } } + + private Checker setDebug(boolean debug) { + this.debug = true; + return this; + } } } \ No newline at end of file