fixed an issue with the polygon parser, see #218

This commit is contained in:
hneemann 2018-12-02 09:50:33 +01:00
parent 34ee212bf4
commit 2a1e4f9361
2 changed files with 41 additions and 3 deletions

View File

@ -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);
}

View File

@ -327,6 +327,19 @@ public class SvgImporterTest extends TestCase {
.check();
}
public void testInkscape4() throws IOException, SvgException, PolygonParser.ParserException, PinException {
CustomShapeDescription custom = new SvgImporter(
in("<svg viewBox=\"0 0 200 100\" xmlns=\"http://www.w3.org/2000/svg\">\n" +
"<path fill=\"none\" stroke=\"black\" stroke-width=\"3\"\n" +
" d=\"M 10,10 V 40 H 40 V 10 Z m 10, 10 h 10 v 10 H 20 v -10 z\"" +
" /> \n" +
"</svg>")).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<VectorInterface> 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;
}
}
}