nicklockwood/SVGPath

Relative sub-paths not handled correctly

rustyshelf opened this issue · 3 comments

Firstly, awesome library, thanks Nick! One issue I found is that if you have a relative path that has multiple sub-paths that are also relative, these don't seem to be offset correctly.

For example:

<path d="m246.7881938,177.5848955c-13.9366996-.4842-27.8722993-.77-41.8169989-.8486,6.3990998,6.5819998,12.7983997,13.1638997,19.1974995,19.7456995,7.3688998-6.5151998,14.8972996-12.8044997,22.6194994-18.8970995Zm-45.4452989,2.3984999c-7.2300998,6.6123998-14.2535996,13.4058997-21.1025995,20.4121995,12.8467997,13.5595997,25.6935994,27.1189993,38.540699,40.678399,6.9114998-7.2348998,14.0072996-14.2496996,21.3211995-21.0778995-12.9196997-13.3375997-25.8395993-26.6751993-38.759299-40.012699Zm-25.1074994,66.6697983c13.0343997-.8329,26.0729993-1.4686,39.126399-1.9186-6.4864998-6.8710998-12.9726997-13.7420997-19.4587995-20.6130995-6.7758998,7.3287998-13.3217997,14.8273996-19.6675995,22.5316994Z"/>

Should draw a stylised "S", but instead the second two sub-paths end up not being relative to the first. I tested a fix for this that seems to work, where end also takes an SVGPoint:

public enum SVGCommand: Hashable {
    ....
    case end(SVGPoint)
}

and then setting it like this:

func end() throws -> SVGCommand {
    _ = try assertArgs(0)
    return .end(commands.last?.point ?? .zero)
}

and having the extension also return it:

public extension SVGCommand {
    var point: SVGPoint {
        switch self {
            case let .moveTo(point),
                 let .lineTo(point),
                 let .cubic(_, _, point),
                 let .quadratic(_, point):
                 let .quadratic(_, point),
                 let .end(point):
                return point
...

I didn't know if you wanted to fix it the same way or if you have a better way to do it but figured I'd raise an issue so you knew about the bug at least.

@rustyshelf thanks for reporting this. I think I've found another solution - I'll push an update shortly.

@rustyshelf fixed in 1.1.2

@nicklockwood prefer your solution, nice work!