Skip to content

Refactor SVG path parser to accept scientific notation #873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 67 additions & 34 deletions core/src/processing/core/PShapeSVG.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,53 +514,86 @@ protected void parsePath() {
char[] pathDataChars = pathData.toCharArray();

StringBuilder pathBuffer = new StringBuilder();
boolean lastSeparate = false;
boolean isOnDecimal = false;

/*
* The state of the lexer:
* -1: just after the command (i.e. a single alphabet)
* 0: neutral state
* 1: on a digit sequence for integer representation
* 2: on a decimal
* 3: on a digit or a sign in exponent in scientific notation, e.g. 3.14e-2)
* 4: on a digit sequence in exponent
*/
int lexState = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understandable, could it be with a enum instead of an integer? I'm afraid we might lose this documentation at some stage


for (int i = 0; i < pathDataChars.length; i++) {
char c = pathDataChars[i];
boolean separate = false;

if (c == 'M' || c == 'm' ||
c == 'L' || c == 'l' ||
c == 'H' || c == 'h' ||
c == 'V' || c == 'v' ||
c == 'C' || c == 'c' || // beziers
c == 'S' || c == 's' ||
c == 'Q' || c == 'q' || // quadratic beziers
c == 'T' || c == 't' ||
c == 'A' || c == 'a' || // elliptical arc
c == 'Z' || c == 'z' || // closepath
c == ',') {
separate = true;
if (i != 0) {

// Put a separator after a command.
if (lexState == -1) {
pathBuffer.append("|");
lexState = 0;
}

if (c >= '0' && c <= '9') {
if (lexState == 0 || lexState == 3) {
// If it is a head of a number representation, enter the 'inside' of the digit sequence.
++lexState;
}
pathBuffer.append(c);
continue;
}

if (c == '-') {
if (lexState == 0) {
// In neutral state, enter 'digit sequence'.
lexState = 1;
}
else if (lexState == 3) {
// In the begining of an exponent, enter 'exponent digit sequence'.
lexState = 4;
}
else {
// Otherwise, begin a new number representation.
pathBuffer.append("|");
lexState = 1;
}
pathBuffer.append("-");
continue;
}
if (c == 'Z' || c == 'z') {
separate = false;

if (c == '.') {
if (lexState >= 2) {
// Begin a new decimal number unless it is in a neutral state or after a digit sequence
pathBuffer.append("|");
}
pathBuffer.append(".");
lexState = 2;
continue;
}
if (c == '.' && !isOnDecimal) {
isOnDecimal = true;

if (c == 'e' || c == 'E') {
// Found 'e' or 'E', enter the 'exponent' state immediately.
pathBuffer.append("e");
lexState = 3;
continue;
}
else if (isOnDecimal && (c < '0' || c > '9')) {

// The following are executed for non-numeral elements

if (lexState != 0) {
pathBuffer.append("|");
isOnDecimal = c == '.';
}
if (c == '-' && !lastSeparate) {
// allow for 'e' notation in numbers, e.g. 2.10e-9
// https://download.processing.org/bugzilla/1408.html
if (i == 0 || pathDataChars[i-1] != 'e') {
pathBuffer.append("|");
}
lexState = 0;
}

if (c != ',') {
pathBuffer.append(c); //"" + pathDataBuffer.charAt(i));
pathBuffer.append(c);
}
if (separate && c != ',' && c != '-') {
pathBuffer.append("|");

if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
// Every alphabet character except for 'e' and 'E' are considered as a command.
lexState = -1;
}
lastSeparate = separate;
}

// use whitespace constant to get rid of extra spaces and CR or LF
Expand Down
32 changes: 28 additions & 4 deletions core/test/processing/core/PShapeSVGTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,47 @@

public class PShapeSVGTest {

private static final String TEST_CONTENT = "<svg><g><path d=\"L 0,3.1.4.1\"/></g></svg>";
private static final String[] TEST_CONTENT = {
"<svg><g><path d=\"L 0,3.1.4.1\"/></g></svg>",
"<svg><g><path d=\"m-13.6 4.69-1.35 0.78h-0.00034l1.33 2.27 1.33-0.77a6.48 6.47 43.14 0 1-1.31-2.27z\"/></g></svg>"
};
private static final int[] TEST_NVERTEX = {2, 8};
private static final String TEST_EXPONENT =
"<svg><g><path d=\"m-1.36E1 469e-2-1.35 0.78h-3.4e-4l1.33 2.27 1.33-0.77a6.48 6.47 43.14 0 1-1.31-2.27z\"/></g></svg>";

@Test
public void testDecimals() {
try {
XML xml = XML.parse(TEST_CONTENT);
for (int i = 0; i < TEST_CONTENT.length; ++i) {
XML xml = XML.parse(TEST_CONTENT[i]);
PShapeSVG shape = new PShapeSVG(xml);
PShape[] children = shape.getChildren();
Assert.assertEquals(1, children.length);
PShape[] grandchildren = children[0].getChildren();
Assert.assertEquals(1, grandchildren.length);
Assert.assertEquals(0, grandchildren[0].getChildCount());
Assert.assertEquals(TEST_NVERTEX[i], grandchildren[0].getVertexCount());
}
}
catch (Exception e) {
Assert.fail("Encountered exception " + e);
}
}

@Test
public void testExponent() {
try {
XML xml = XML.parse(TEST_EXPONENT);
PShapeSVG shape = new PShapeSVG(xml);
PShape[] children = shape.getChildren();
Assert.assertEquals(1, children.length);
PShape[] grandchildren = children[0].getChildren();
Assert.assertEquals(1, grandchildren.length);
Assert.assertEquals(0, grandchildren[0].getChildCount());
Assert.assertEquals(2, grandchildren[0].getVertexCount());
Assert.assertEquals(8, grandchildren[0].getVertexCount());
}
catch (Exception e) {
Assert.fail("Encountered exception " + e);
}
}

}
Loading