Skip to content

Commit 00aaebe

Browse files
committed
fix closepath relative problem in SVG (issue 1058)
1 parent eac610f commit 00aaebe

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

android/core/src/processing/core/PShapeSVG.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,9 @@ protected void parsePath() {
546546
// char prevCommand = '\0';
547547
boolean prevCurve = false;
548548
float ctrlX, ctrlY;
549+
// store values for closepath so that relative coords work properly
550+
float movetoX = 0;
551+
float movetoY = 0;
549552

550553
while (i < pathTokens.length) {
551554
char c = pathTokens[i].charAt(0);
@@ -560,6 +563,8 @@ protected void parsePath() {
560563
case 'M': // M - move to (absolute)
561564
cx = PApplet.parseFloat(pathTokens[i + 1]);
562565
cy = PApplet.parseFloat(pathTokens[i + 2]);
566+
movetoX = cx;
567+
movetoY = cy;
563568
parsePathMoveto(cx, cy);
564569
implicitCommand = 'L';
565570
i += 3;
@@ -798,6 +803,11 @@ protected void parsePath() {
798803

799804
case 'Z':
800805
case 'z':
806+
// since closing the path, the 'current' point needs
807+
// to return back to the last moveto location.
808+
// http://code.google.com/p/processing/issues/detail?id=1058
809+
cx = movetoX;
810+
cy = movetoY;
801811
close = true;
802812
i++;
803813
break;
@@ -811,7 +821,7 @@ protected void parsePath() {
811821
System.err.println("unparsed: " + unparsed);
812822
if (pathTokens[i].equals("a") || pathTokens[i].equals("A")) {
813823
String msg = "Sorry, elliptical arc support for SVG files " +
814-
"is not yet implemented (See issue #130 for updates)";
824+
"is not yet implemented (See issue 130 for updates)";
815825
throw new RuntimeException(msg);
816826
}
817827
throw new RuntimeException("shape command not handled: " + pathTokens[i]);

core/src/processing/core/PShapeSVG.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,9 @@ protected void parsePath() {
554554
// char prevCommand = '\0';
555555
boolean prevCurve = false;
556556
float ctrlX, ctrlY;
557+
// store values for closepath so that relative coords work properly
558+
float movetoX = 0;
559+
float movetoY = 0;
557560

558561
while (i < pathTokens.length) {
559562
char c = pathTokens[i].charAt(0);
@@ -568,6 +571,8 @@ protected void parsePath() {
568571
case 'M': // M - move to (absolute)
569572
cx = PApplet.parseFloat(pathTokens[i + 1]);
570573
cy = PApplet.parseFloat(pathTokens[i + 2]);
574+
movetoX = cx;
575+
movetoY = cy;
571576
parsePathMoveto(cx, cy);
572577
implicitCommand = 'L';
573578
i += 3;
@@ -654,15 +659,15 @@ protected void parsePath() {
654659
break;
655660

656661
// S - curve to shorthand (absolute)
657-
// Draws a cubic Bezier curve from the current point to (x,y). The first
662+
// Draws a cubic Bézier curve from the current point to (x,y). The first
658663
// control point is assumed to be the reflection of the second control
659664
// point on the previous command relative to the current point.
660665
// (x2,y2) is the second control point (i.e., the control point
661666
// at the end of the curve). S (uppercase) indicates that absolute
662667
// coordinates will follow; s (lowercase) indicates that relative
663668
// coordinates will follow. Multiple sets of coordinates may be specified
664-
// to draw a polybezier. At the end of the command, the new current point
665-
// becomes the final (x,y) coordinate pair used in the polybezier.
669+
// to draw a polybézier. At the end of the command, the new current point
670+
// becomes the final (x,y) coordinate pair used in the polybézier.
666671
case 'S': {
667672
// (If there is no previous command or if the previous command was not
668673
// an C, c, S or s, assume the first control point is coincident with
@@ -716,12 +721,12 @@ protected void parsePath() {
716721
break;
717722

718723
// Q - quadratic curve to (absolute)
719-
// Draws a quadratic Bezier curve from the current point to (x,y) using
724+
// Draws a quadratic Bézier curve from the current point to (x,y) using
720725
// (x1,y1) as the control point. Q (uppercase) indicates that absolute
721726
// coordinates will follow; q (lowercase) indicates that relative
722727
// coordinates will follow. Multiple sets of coordinates may be specified
723-
// to draw a polybezier. At the end of the command, the new current point
724-
// becomes the final (x,y) coordinate pair used in the polybezier.
728+
// to draw a polybézier. At the end of the command, the new current point
729+
// becomes the final (x,y) coordinate pair used in the polybézier.
725730
case 'Q': {
726731
ctrlX = PApplet.parseFloat(pathTokens[i + 1]);
727732
ctrlY = PApplet.parseFloat(pathTokens[i + 2]);
@@ -806,6 +811,11 @@ protected void parsePath() {
806811

807812
case 'Z':
808813
case 'z':
814+
// since closing the path, the 'current' point needs
815+
// to return back to the last moveto location.
816+
// http://code.google.com/p/processing/issues/detail?id=1058
817+
cx = movetoX;
818+
cy = movetoY;
809819
close = true;
810820
i++;
811821
break;
@@ -819,7 +829,7 @@ protected void parsePath() {
819829
System.err.println("unparsed: " + unparsed);
820830
if (pathTokens[i].equals("a") || pathTokens[i].equals("A")) {
821831
String msg = "Sorry, elliptical arc support for SVG files " +
822-
"is not yet implemented (See issue #130 for updates)";
832+
"is not yet implemented (See issue 130 for updates)";
823833
throw new RuntimeException(msg);
824834
}
825835
throw new RuntimeException("shape command not handled: " + pathTokens[i]);

0 commit comments

Comments
 (0)