mchoe / SwiftSVG

A simple, performant, and lightweight SVG parser
Other
1.92k stars 229 forks source link

SVGView crashes when rendering this SVG file #101

Open edopelawi opened 6 years ago

edopelawi commented 6 years ago

Dear all,

I'm currently using SwiftSVG v2.1.5, installed through Carthage. I added SVGView to my XIB, and tried to load an SVG asset:

svgView.SVGName = "insurance"

And the insurance.svg asset contains this:

<svg xmlns="http://www.w3.org/2000/svg" width="21" height="25"><path fill="#00B894" fill-rule="evenodd" d="M15.358 14.079h-3.239v3.16c0 .873-.725 1.58-1.619 1.58s-1.62-.707-1.62-1.58v-3.16H5.643c-.894 0-1.62-.708-1.62-1.58 0-.873.726-1.58 1.62-1.58h3.239v-3.16c0-.873.725-1.58 1.619-1.58s1.62.707 1.62 1.58v3.16h3.238c.894 0 1.62.707 1.62 1.58 0 .872-.726 1.58-1.62 1.58m4.955-9.252a1.424 1.424 0 0 0-.8-.932L11.105.129a1.486 1.486 0 0 0-1.212 0L1.488 3.895c-.397.178-.692.52-.8.932a20.88 20.88 0 0 0-.557 7.672 20.81 20.81 0 0 0 2.123 7.098c.05.1.114.194.187.28a17.435 17.435 0 0 0 6.693 4.777S9.855 25 10.5 25c.645 0 1.366-.346 1.366-.346 2.61-1.05 4.9-2.7 6.691-4.775.074-.086.137-.18.188-.28a20.806 20.806 0 0 0 2.124-7.1 21.052 21.052 0 0 0-.556-7.672"/></svg>

When I run the code in Xcode 9.4's Simulator (iPhone 5S, 11.4), I got a crash in PathCommand.swift, line 326. I can't trace it further since the framework was compiled with optimization. Here's the method that contains line 326 in PathCommand.swift:

   /**
     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])
     */
    internal func execute(on path: UIBezierPath, previousCommand: PreviousCommand? = nil) {
        let startControl = self.pointForPathType(CGPoint(x: self.coordinateBuffer[0], y: self.coordinateBuffer[1]), relativeTo: path.currentPoint)
        let endControl = self.pointForPathType(CGPoint(x: self.coordinateBuffer[2], y: self.coordinateBuffer[3]), relativeTo: path.currentPoint)
        let point = self.pointForPathType(CGPoint(x: self.coordinateBuffer[4], y: self.coordinateBuffer[5]), relativeTo: path.currentPoint)
        path.addCurve(to: point, controlPoint1: startControl, controlPoint2: endControl)
    }

Any help would be appreciated! :)

Gujci commented 5 years ago

Hi,

I have a similar issue, my SVG parsing crashes at line 355 in PathCommand.swift

let point = self.pointForPathType(CGPoint(x: self.coordinateBuffer[2], y: self.coordinateBuffer[3]), relativeTo: path.currentPoint)

My usage of the library is similar to @edopelawi s. I have also tried adding it as a UIView extension, which ended with a similar result.

I also nee to mention, that, the example SVG works fine in every scenario, so the crash might has to do something with the svg format.

Gujci commented 5 years ago

I don't know much about SVG parsing, but I have found the "problem" in my svg file. There was a missing close path Z, in the last command. Adding it fixed only the crash, the rendered image is nowhere near the original.