#490: Fix for "incomplete" paths with implicit line back to start.

This commit is contained in:
Harald Kuhr 2020-01-17 16:44:42 +01:00
parent d2b58ed20e
commit 420f78be88
2 changed files with 49 additions and 6 deletions

View File

@ -131,9 +131,10 @@ public final class AdobePathWriter {
// Replace initial point. // Replace initial point.
AdobePathSegment initial = subpath.get(0); AdobePathSegment initial = subpath.get(0);
if (initial.apx != prev.apx || initial.apy != prev.apy) { if (initial.apx != prev.apx || initial.apy != prev.apy) {
// TODO: Line back to initial if last anchor point does not equal initial anchor? // Line back to initial if last anchor point does not equal initial anchor
// subpath.add(new AdobePathSegment(CLOSED_SUBPATH_BEZIER_LINKED, prev.cppy, prev.cppx, prev.apy, prev.apx, 0, 0)); collinear = isCollinear(prev.cppx, prev.cppy, initial.apx, initial.apy, initial.apx, initial.apy);
throw new AssertionError("Not a closed path"); subpath.add(new AdobePathSegment(collinear ? CLOSED_SUBPATH_BEZIER_LINKED : CLOSED_SUBPATH_BEZIER_UNLINKED, prev.cppy, prev.cppx, prev.apy, prev.apx, initial.apy, initial.apx));
prev = new AdobePathSegment(CLOSED_SUBPATH_BEZIER_LINKED, initial.apy, initial.apx, initial.apy, initial.apx, 0, 0);
} }
collinear = isCollinear(prev.cppx, prev.cppy, initial.apx, initial.apy, initial.cplx, initial.cply); collinear = isCollinear(prev.cppx, prev.cppy, initial.apx, initial.apy, initial.cplx, initial.cply);
@ -151,6 +152,9 @@ public final class AdobePathWriter {
pathIterator.next(); pathIterator.next();
} }
// TODO: If subpath is not empty at this point, there was no close segment...
// Either wrap up (if coordinates match), or throw exception (otherwise)
return segments; return segments;
} }

View File

@ -44,8 +44,7 @@ import java.util.Arrays;
import static com.twelvemonkeys.imageio.path.AdobePathSegment.*; import static com.twelvemonkeys.imageio.path.AdobePathSegment.*;
import static com.twelvemonkeys.imageio.path.PathsTest.assertPathEquals; import static com.twelvemonkeys.imageio.path.PathsTest.assertPathEquals;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
/** /**
* AdobePathWriterTest. * AdobePathWriterTest.
@ -93,6 +92,46 @@ public class AdobePathWriterTest {
new AdobePathWriter(path); new AdobePathWriter(path);
} }
@Test
public void testCreateClosed() {
GeneralPath path = new GeneralPath(Path2D.WIND_EVEN_ODD);
path.moveTo(.5, .5);
path.lineTo(1, .5);
path.curveTo(1, 1, 1, 1, .5, 1);
path.closePath();
new AdobePathWriter(path).writePath();
fail("Test that we have 4 segments");
}
@Test
public void testCreateImplicitClosed() {
GeneralPath path = new GeneralPath(Path2D.WIND_EVEN_ODD);
path.moveTo(.5, .5);
path.lineTo(1, .5);
path.curveTo(1, 1, 1, 1, .5, 1);
path.lineTo(.5, .5);
new AdobePathWriter(path).writePath(); // TODO: Should we allow this?
fail("Test that we have 4 segments, and that it is equal to the one above");
}
@Test
public void testCreateDoubleClosed() {
GeneralPath path = new GeneralPath(Path2D.WIND_EVEN_ODD);
path.moveTo(.5, .5);
path.lineTo(1, .5);
path.curveTo(1, 1, 1, 1, .5, 1);
path.lineTo(.5, .5);
path.closePath();
new AdobePathWriter(path).writePath();
fail("Test that we have 4 segments, and that it is equal to the one above");
}
@Test @Test
public void testWriteToStream() throws IOException { public void testWriteToStream() throws IOException {
Path2D path = new GeneralPath(Path2D.WIND_EVEN_ODD); Path2D path = new GeneralPath(Path2D.WIND_EVEN_ODD);
@ -168,7 +207,7 @@ public class AdobePathWriterTest {
AdobePathWriter pathCreator = new AdobePathWriter(path); AdobePathWriter pathCreator = new AdobePathWriter(path);
byte[] bytes = pathCreator.writePath(); byte[] bytes = pathCreator.writePath();
System.err.println(Arrays.toString(bytes)); // System.err.println(Arrays.toString(bytes));
assertEquals(12 * 26, bytes.length); assertEquals(12 * 26, bytes.length);