Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions SwiftSVG.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
39B8D8B01B3C32DD009DAF60 /* UIView+SVG.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E25E9031AB0D4C600E8D6CB /* UIView+SVG.swift */; };
8AC4DBEA1CB37EA700137DC9 /* CrossPlatform.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AC4DBE91CB37EA700137DC9 /* CrossPlatform.swift */; };
8AC4DBEB1CB37EBD00137DC9 /* CrossPlatform.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AC4DBE91CB37EA700137DC9 /* CrossPlatform.swift */; };
AC0FA9781E4C2CF800B62B9E /* URL+SVG.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC0FA9771E4C2CF800B62B9E /* URL+SVG.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -74,6 +75,7 @@
9EA820851AAB649700DEE6CA /* String+SVG.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+SVG.swift"; sourceTree = "<group>"; };
9EA820871AAB6AD700DEE6CA /* UIBezierPath+SVG.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIBezierPath+SVG.swift"; sourceTree = "<group>"; };
9EB308781AC5BF350007DD82 /* SVGView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SVGView.swift; sourceTree = "<group>"; };
AC0FA9771E4C2CF800B62B9E /* URL+SVG.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "URL+SVG.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -208,6 +210,7 @@
9EB308781AC5BF350007DD82 /* SVGView.swift */,
9EA820871AAB6AD700DEE6CA /* UIBezierPath+SVG.swift */,
9E25E9031AB0D4C600E8D6CB /* UIView+SVG.swift */,
AC0FA9771E4C2CF800B62B9E /* URL+SVG.swift */,
);
name = SVG;
sourceTree = "<group>";
Expand Down Expand Up @@ -394,6 +397,7 @@
39B8D8A61B3C32DD009DAF60 /* NSObject+Extensions.swift in Sources */,
39B8D8A71B3C32DD009DAF60 /* String+Subscript.swift in Sources */,
39B8D8A81B3C32DD009DAF60 /* String+ToHex.swift in Sources */,
AC0FA9781E4C2CF800B62B9E /* URL+SVG.swift in Sources */,
39B8D8A91B3C32DD009DAF60 /* Stack.swift in Sources */,
39B8D8AA1B3C32DD009DAF60 /* UIColor+Extensions.swift in Sources */,
8AC4DBEA1CB37EA700137DC9 /* CrossPlatform.swift in Sources */,
Expand Down
13 changes: 12 additions & 1 deletion SwiftSVG/SVGParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ private var tagMapping: [String: String] = [

var path: UIBezierPath = UIBezierPath()
var shapeLayer: CAShapeLayer = CAShapeLayer()

var name: String = ""

var d: String? {
didSet {
if let pathStringToParse = d {
Expand All @@ -60,6 +61,14 @@ private var tagMapping: [String: String] = [
}
}
}

var id: String? {
didSet {
if let nameString = id {
self.name = nameString
}
}
}
}

@objc(SVGElement) private class SVGElement: NSObject { }
Expand All @@ -71,6 +80,7 @@ open class SVGParser : NSObject, XMLParserDelegate {
open var containerLayer: CALayer?
open var shouldParseSinglePathOnly = false
open fileprivate(set) var paths = [UIBezierPath]()
open fileprivate(set) var pathsByName: [String:UIBezierPath] = [:]

convenience init(parser: XMLParser, containerLayer: CALayer? = nil, shouldParseSinglePathOnly: Bool = false) {
self.init()
Expand Down Expand Up @@ -115,6 +125,7 @@ open class SVGParser : NSObject, XMLParserDelegate {
self.containerLayer!.addSublayer(thisPath.shapeLayer)
}
self.paths.append(thisPath.path)
self.pathsByName[thisPath.name] = thisPath.path

if self.shouldParseSinglePathOnly == true {
parser.abortParsing()
Expand Down
38 changes: 38 additions & 0 deletions SwiftSVG/URL+SVG.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// URL+SVG.swift
// SwiftSVG
//
// Copyright (c) 2017 Eric Celeste
// http://www.tenseg.net
// http://www.github.com/efc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#if os(iOS)
import UIKit
#elseif os(OSX)
import AppKit
#endif

extension URL {
public func pathsByName() -> [String: UIBezierPath] {
let parser = SVGParser(SVGURL: self, containerLayer: nil, shouldParseSinglePathOnly: false)
return parser.pathsByName
}
}