Skip to content

Commit 3a0afae

Browse files
committed
SVG paths automatically draw a line after a move when the command is omitted.
1 parent 4490096 commit 3a0afae

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

Samples/path.svg

Lines changed: 5 additions & 2 deletions
Loading

SwiftDraw/Parser.XML.Path.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,31 @@ extension XMLParser {
5656
var lastCommand: Command?
5757

5858
repeat {
59-
if let cmd = parseCommand(&scanner) {
60-
lastCommand = cmd
59+
guard let cmd = nextPathCommand(&scanner, lastCommand: lastCommand) else {
60+
throw Error.invalid
6161
}
62-
guard let command = lastCommand else { throw Error.invalid }
63-
segments.append(try parsePathSegment(for: command, with: &scanner))
62+
lastCommand = cmd
63+
segments.append(try parsePathSegment(for: cmd, with: &scanner))
6464
} while !scanner.isAtEnd
6565

6666
return segments
6767
}
68+
69+
func nextPathCommand(_ scanner: inout PathScanner, lastCommand: Command?) -> Command? {
70+
if let cmd = parseCommand(&scanner) {
71+
return cmd
72+
}
73+
74+
switch lastCommand {
75+
case .some(.move):
76+
return .line
77+
case .some(.moveRelative):
78+
return .lineRelative
79+
default:
80+
return lastCommand
81+
}
82+
}
83+
6884

6985
func parsePathSegment(for command: Command, with scanner: inout PathScanner) throws -> Segment {
7086
switch command {

SwiftDrawTests/Parser.XML.PathTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ final class ParserXMLPathTests: XCTestCase {
7676
AssertSegmentEquals("M10-20", move(10, -20, .absolute))
7777

7878
AssertSegmentsEquals("M10-20 5 1", [move(10, -20, .absolute),
79-
move(5, 1, .absolute)])
79+
line(5, 1, .absolute)])
80+
81+
AssertSegmentsEquals("m10-20 5 1", [move(10, -20, .relative),
82+
line(5, 1, .relative)])
8083
}
8184

8285
func testLine() {

0 commit comments

Comments
 (0)