Skip to content

Commit 3482f92

Browse files
author
luostari
committed
Successive explicit MoveTo commands should be treated as MoveTo.
1 parent 4b4edad commit 3482f92

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

SwiftSVG/SVG/Iterators/PathCommand.swift

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ internal protocol PathCommand: PreviousCommand {
6969
- Parameter path: The path to append a new path to
7070
- Parameter previousCommand: An optional previous command. Used primarily with the shortcut cubic and quadratic Bezier types
7171
*/
72-
func execute(on path: UIBezierPath, previousCommand: PreviousCommand?)
72+
func execute(on path: UIBezierPath, previousCommand: PreviousCommand?) -> PreviousCommand?
7373
}
7474

7575
/**
@@ -162,25 +162,24 @@ internal struct MoveTo: PathCommand {
162162
/**
163163
This will move the current point to `CGPoint(self.coordinateBuffer[0], self.coordinateBuffer[1])`.
164164

165-
Sequential MoveTo commands should be treated as LineTos.
165+
Sequential implicit MoveTo commands should be treated as LineTos.
166166

167167
From Docs (https://www.w3.org/TR/SVG2/paths.html#PathDataMovetoCommands):
168168

169169
```
170170
Start a new sub-path at the given (x,y) coordinates. M (uppercase) indicates that absolute coordinates will follow; m (lowercase) indicates that relative coordinates will follow. If a moveto is followed by multiple pairs of coordinates, the subsequent pairs are treated as implicit lineto commands. Hence, implicit lineto commands will be relative if the moveto is relative, and absolute if the moveto is absolute.
171171
```
172172
*/
173-
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
174-
173+
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) -> PreviousCommand? {
175174
if previousCommand is MoveTo {
176175
var implicitLineTo = LineTo(pathType: self.pathType)
177176
implicitLineTo.coordinateBuffer = [self.coordinateBuffer[0], self.coordinateBuffer[1]]
178-
implicitLineTo.execute(on: path)
179-
return
177+
return implicitLineTo.execute(on: path)
180178
}
181179

182180
let point = self.pointForPathType(CGPoint(x: self.coordinateBuffer[0], y: self.coordinateBuffer[1]), relativeTo: path.currentPoint)
183181
path.move(to: point)
182+
return self
184183
}
185184
}
186185

@@ -206,8 +205,9 @@ internal struct ClosePath: PathCommand {
206205
/**
207206
Closes the current path
208207
*/
209-
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
208+
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) -> PreviousCommand? {
210209
path.close()
210+
return self
211211
}
212212

213213
}
@@ -234,9 +234,10 @@ internal struct LineTo: PathCommand {
234234
/**
235235
Creates a line from the `path.currentPoint` to point `CGPoint(self.coordinateBuffer[0], coordinateBuffer[1])`
236236
*/
237-
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
237+
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) -> PreviousCommand? {
238238
let point = self.pointForPathType(CGPoint(x: self.coordinateBuffer[0], y: self.coordinateBuffer[1]), relativeTo: path.currentPoint)
239239
path.addLine(to: point)
240+
return self
240241
}
241242
}
242243

@@ -262,10 +263,11 @@ internal struct HorizontalLineTo: PathCommand {
262263
/**
263264
Adds a horizontal line from the currentPoint to `CGPoint(self.coordinateBuffer[0], path.currentPoint.y)`
264265
*/
265-
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
266+
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) -> PreviousCommand? {
266267
let x = self.coordinateBuffer[0]
267268
let point = (self.pathType == .absolute ? CGPoint(x: CGFloat(x), y: path.currentPoint.y) : CGPoint(x: path.currentPoint.x + CGFloat(x), y: path.currentPoint.y))
268269
path.addLine(to: point)
270+
return self
269271
}
270272
}
271273

@@ -291,10 +293,11 @@ internal struct VerticalLineTo: PathCommand {
291293
/**
292294
Adds a vertical line from the currentPoint to `CGPoint(path.currentPoint.y, self.coordinateBuffer[0])`
293295
*/
294-
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
296+
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) -> PreviousCommand? {
295297
let y = self.coordinateBuffer[0]
296298
let point = (self.pathType == .absolute ? CGPoint(x: path.currentPoint.x, y: CGFloat(y)) : CGPoint(x: path.currentPoint.x, y: path.currentPoint.y + CGFloat(y)))
297299
path.addLine(to: point)
300+
return self
298301
}
299302
}
300303

@@ -320,11 +323,12 @@ internal struct CurveTo: PathCommand {
320323
/**
321324
Adds a cubic Bezier curve to `path`. The path will end up at `CGPoint(self.coordinateBuffer[4], self.coordinateBuffer[5])`. The control point for `path.currentPoint` will be `CGPoint(self.coordinateBuffer[0], self.coordinateBuffer[1])`. Then controle point for the end point will be CGPoint(self.coordinateBuffer[2], self.coordinateBuffer[3])
322325
*/
323-
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
326+
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) -> PreviousCommand? {
324327
let startControl = self.pointForPathType(CGPoint(x: self.coordinateBuffer[0], y: self.coordinateBuffer[1]), relativeTo: path.currentPoint)
325328
let endControl = self.pointForPathType(CGPoint(x: self.coordinateBuffer[2], y: self.coordinateBuffer[3]), relativeTo: path.currentPoint)
326329
let point = self.pointForPathType(CGPoint(x: self.coordinateBuffer[4], y: self.coordinateBuffer[5]), relativeTo: path.currentPoint)
327330
path.addCurve(to: point, controlPoint1: startControl, controlPoint2: endControl)
331+
return self
328332
}
329333
}
330334

@@ -350,7 +354,7 @@ internal struct SmoothCurveTo: PathCommand {
350354
/**
351355
Shortcut cubic Bezier curve to that add a new path ending up at `CGPoint(self.coordinateBuffer[0], self.coordinateBuffer[1])` with a single control point in the middle.
352356
*/
353-
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
357+
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) -> PreviousCommand? {
354358

355359
let point = self.pointForPathType(CGPoint(x: self.coordinateBuffer[2], y: self.coordinateBuffer[3]), relativeTo: path.currentPoint)
356360
let controlEnd = self.pointForPathType(CGPoint(x: self.coordinateBuffer[0], y: self.coordinateBuffer[1]), relativeTo: path.currentPoint)
@@ -394,6 +398,7 @@ internal struct SmoothCurveTo: PathCommand {
394398
controlStart = path.currentPoint
395399
}
396400
path.addCurve(to: point, controlPoint1: controlStart, controlPoint2: controlEnd)
401+
return self
397402
}
398403
}
399404

@@ -416,10 +421,11 @@ internal struct QuadraticCurveTo: PathCommand {
416421
self.pathType = pathType
417422
}
418423

419-
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
424+
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) -> PreviousCommand? {
420425
let controlPoint = self.pointForPathType(CGPoint(x: self.coordinateBuffer[0], y: self.coordinateBuffer[1]), relativeTo: path.currentPoint)
421426
let point = self.pointForPathType(CGPoint(x: self.coordinateBuffer[2], y: self.coordinateBuffer[3]), relativeTo: path.currentPoint)
422427
path.addQuadCurve(to: point, controlPoint: controlPoint)
428+
return self
423429
}
424430
}
425431

@@ -445,7 +451,7 @@ internal struct SmoothQuadraticCurveTo: PathCommand {
445451
self.pathType = pathType
446452
}
447453

448-
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
454+
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) -> PreviousCommand? {
449455

450456
let point = self.pointForPathType(CGPoint(x: self.coordinateBuffer[0], y: self.coordinateBuffer[1]), relativeTo: path.currentPoint)
451457

@@ -471,6 +477,7 @@ internal struct SmoothQuadraticCurveTo: PathCommand {
471477
controlPoint = path.currentPoint
472478
}
473479
path.addQuadCurve(to: point, controlPoint: controlPoint)
480+
return self
474481
}
475482
}
476483

@@ -495,7 +502,8 @@ internal struct EllipticalArc: PathCommand {
495502
}
496503

497504
/// :nodoc:
498-
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
505+
internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) -> PreviousCommand? {
499506
assert(false, "Needs Implementation")
507+
return self
500508
}
501509
}

0 commit comments

Comments
 (0)