-
-
Notifications
You must be signed in to change notification settings - Fork 101
Fix ArrayIndexOutOfBoundsException with compact SVG arc notation #1282
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
Open
hxrshxz
wants to merge
2
commits into
processing:main
Choose a base branch
from
hxrshxz:svg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -961,14 +961,36 @@ else if (lexState == LexState.EXP_HEAD) { | |
| float rx = PApplet.parseFloat(pathTokens[i + 1]); | ||
| float ry = PApplet.parseFloat(pathTokens[i + 2]); | ||
| float angle = PApplet.parseFloat(pathTokens[i + 3]); | ||
| boolean fa = PApplet.parseFloat(pathTokens[i + 4]) != 0; | ||
| boolean fs = PApplet.parseFloat(pathTokens[i + 5]) != 0; | ||
| float endX = PApplet.parseFloat(pathTokens[i + 6]); | ||
| float endY = PApplet.parseFloat(pathTokens[i + 7]); | ||
| // In compact arc notation, flags and coordinates may be concatenated. | ||
| // e.g. "013" is parsed as large-arc=0, sweep=1, x=3 | ||
| String token4 = pathTokens[i + 4]; | ||
| boolean fa; | ||
| boolean fs; | ||
| float endX; | ||
| float endY; | ||
| int tokenOffset = 0; | ||
| if (isCompactArcNotation(token4)) { | ||
| fa = token4.charAt(0) == '1'; | ||
| fs = token4.charAt(1) == '1'; | ||
| if (token4.length() > 2) { | ||
| endX = PApplet.parseFloat(token4.substring(2)); | ||
| endY = PApplet.parseFloat(pathTokens[i + 5]); | ||
| tokenOffset = -2; | ||
| } else { | ||
| endX = PApplet.parseFloat(pathTokens[i + 5]); | ||
| endY = PApplet.parseFloat(pathTokens[i + 6]); | ||
| tokenOffset = -1; | ||
| } | ||
| } else { | ||
| fa = PApplet.parseFloat(token4) != 0; | ||
| fs = PApplet.parseFloat(pathTokens[i + 5]) != 0; | ||
| endX = PApplet.parseFloat(pathTokens[i + 6]); | ||
| endY = PApplet.parseFloat(pathTokens[i + 7]); | ||
| } | ||
| parsePathArcto(cx, cy, rx, ry, angle, fa, fs, endX, endY); | ||
| cx = endX; | ||
| cy = endY; | ||
| i += 8; | ||
| i += 8 + tokenOffset; | ||
| prevCurve = true; | ||
| } | ||
| break; | ||
|
|
@@ -978,14 +1000,34 @@ else if (lexState == LexState.EXP_HEAD) { | |
| float rx = PApplet.parseFloat(pathTokens[i + 1]); | ||
| float ry = PApplet.parseFloat(pathTokens[i + 2]); | ||
| float angle = PApplet.parseFloat(pathTokens[i + 3]); | ||
| boolean fa = PApplet.parseFloat(pathTokens[i + 4]) != 0; | ||
| boolean fs = PApplet.parseFloat(pathTokens[i + 5]) != 0; | ||
| float endX = cx + PApplet.parseFloat(pathTokens[i + 6]); | ||
| float endY = cy + PApplet.parseFloat(pathTokens[i + 7]); | ||
| String token4 = pathTokens[i + 4]; | ||
| boolean fa; | ||
| boolean fs; | ||
| float endX; | ||
| float endY; | ||
| int tokenOffset = 0; | ||
| if (isCompactArcNotation(token4)) { | ||
| fa = token4.charAt(0) == '1'; | ||
| fs = token4.charAt(1) == '1'; | ||
| if (token4.length() > 2) { | ||
| endX = cx + PApplet.parseFloat(token4.substring(2)); | ||
| endY = cy + PApplet.parseFloat(pathTokens[i + 5]); | ||
| tokenOffset = -2; | ||
| } else { | ||
| endX = cx + PApplet.parseFloat(pathTokens[i + 5]); | ||
| endY = cy + PApplet.parseFloat(pathTokens[i + 6]); | ||
| tokenOffset = -1; | ||
| } | ||
| } else { | ||
| fa = PApplet.parseFloat(token4) != 0; | ||
| fs = PApplet.parseFloat(pathTokens[i + 5]) != 0; | ||
| endX = cx + PApplet.parseFloat(pathTokens[i + 6]); | ||
| endY = cy + PApplet.parseFloat(pathTokens[i + 7]); | ||
| } | ||
| parsePathArcto(cx, cy, rx, ry, angle, fa, fs, endX, endY); | ||
| cx = endX; | ||
| cy = endY; | ||
| i += 8; | ||
| i += 8 + tokenOffset; | ||
| prevCurve = true; | ||
| } | ||
| break; | ||
|
|
@@ -1054,6 +1096,29 @@ private void parsePathMoveto(float px, float py) { | |
| } | ||
|
|
||
|
|
||
| /** | ||
| * Checks if a token represents compact arc notation where flags and coordinates | ||
| * are concatenated (e.g., "013" for large-arc=0, sweep=1, x=3). | ||
| * | ||
| * @param token the token to check | ||
| * @return true if the token is in compact arc notation format | ||
| */ | ||
| private boolean isCompactArcNotation(String token) { | ||
| if (token == null) { | ||
| return false; | ||
| } | ||
| return token.length() > 1 && | ||
| (token.charAt(0) == '0' || token.charAt(0) == '1') && | ||
| (token.charAt(1) == '0' || token.charAt(1) == '1') && | ||
| (token.length() == 2 || | ||
| (token.length() > 2 && ( | ||
| Character.isDigit(token.charAt(2)) || | ||
| token.charAt(2) == '+' || | ||
| token.charAt(2) == '-' || | ||
| token.charAt(2) == '.'))); | ||
|
Comment on lines
+1111
to
+1118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this makes sense but could use some comments to describe what is going on |
||
| } | ||
|
|
||
|
|
||
hxrshxz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private void parsePathLineto(float px, float py) { | ||
| parsePathCode(VERTEX); | ||
| parsePathVertex(px, py); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package processing.core; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import processing.data.XML; | ||
|
|
||
| public class PShapeSVGPathTest { | ||
|
|
||
| @Test | ||
| public void testCompactPathNotation() { | ||
| // Test the failing SVG from issue #1244 | ||
| String svgContent = "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.0\" viewBox=\"0 0 29 29\">" + | ||
| "<path d=\"m0 6 3-2 15 4 7-7a2 2 0 013 3l-7 7 4 15-2 3-7-13-5 5v4l-2 2-2-5-5-2 2-2h4l5-5z\"/>" + | ||
| "</svg>"; | ||
|
|
||
| try { | ||
| XML xml = XML.parse(svgContent); | ||
| PShapeSVG shape = new PShapeSVG(xml); | ||
| Assert.assertNotNull(shape); | ||
| } catch (Exception e) { | ||
| Assert.fail("Encountered exception " + e); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testWorkingPathNotation() { | ||
| // Test the working SVG (with explicit decimal points) | ||
| String svgContent = "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.0\" viewBox=\"0 0 29 29\">" + | ||
| "<path d=\"m 0,5.9994379 2.9997,-1.9998 14.9985,3.9996 6.9993,-6.99930004 a 2.1211082,2.1211082 0 0 1 2.9997,2.99970004 l -6.9993,6.9993001 3.9996,14.9985 -1.9998,2.9997 -6.9993,-12.9987 -4.9995,4.9995 v 3.9996 l -1.9998,1.9998 -1.9998,-4.9995 -4.9995,-1.9998 1.9998,-1.9998 h 3.9996 l 4.9995,-4.9995 z\"/>" + | ||
| "</svg>"; | ||
|
|
||
| try { | ||
| XML xml = XML.parse(svgContent); | ||
| PShapeSVG shape = new PShapeSVG(xml); | ||
| Assert.assertNotNull(shape); | ||
| } catch (Exception e) { | ||
| Assert.fail("Encountered exception " + e); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testCompactArcNotationVariations() { | ||
| // Test various compact arc notations | ||
| String[] testCases = { | ||
| // Flags only concatenated (e.g., "01") | ||
| "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 100 100\"><path d=\"M10 10 A30 30 0 0110 50\"/></svg>", | ||
| // Flags and coordinate concatenated (e.g., "013") | ||
| "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 100 100\"><path d=\"M10 10 A30 30 0 013 50\"/></svg>", | ||
| // Standard notation (should still work) | ||
| "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 100 100\"><path d=\"M10 10 A30 30 0 0 1 10 50\"/></svg>" | ||
| }; | ||
|
|
||
| try { | ||
| for (String svgContent : testCases) { | ||
| XML xml = XML.parse(svgContent); | ||
| PShapeSVG shape = new PShapeSVG(xml); | ||
| Assert.assertNotNull(shape); | ||
| } | ||
| } catch (Exception e) { | ||
| Assert.fail("Encountered exception " + e); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testCompactArcWithNegativeCoordinates() { | ||
| // Test compact arc notation with negative coordinates | ||
| String svgContent = "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 100 100\">" + | ||
| "<path d=\"M50 50 a20 20 0 01-10 20\"/>" + | ||
| "</svg>"; | ||
|
|
||
| try { | ||
| XML xml = XML.parse(svgContent); | ||
| PShapeSVG shape = new PShapeSVG(xml); | ||
| Assert.assertNotNull(shape); | ||
| } catch (Exception e) { | ||
| Assert.fail("Encountered exception " + e); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the significance of the 5, 6 and 7 index offsets for pathTokens?S Specifically
pathTokens[i+5]etc