Skip to content

Commit

Permalink
Fix #68. Change SVGParser.parse signature
Browse files Browse the repository at this point in the history
  • Loading branch information
shipinev committed Dec 2, 2016
1 parent a58e6f5 commit 7a5f59c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Example/Example/Examples/SVGChartsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Macaw
class SVGChartsView: MacawView {

required init?(coder aDecoder: NSCoder) {
super.init(node: SVGParser.parse(path: "pie-chart"), coder: aDecoder)
super.init(node: SVGParser.parse(path: "pie-chart") ?? Group(), coder: aDecoder)
}

}
16 changes: 11 additions & 5 deletions Source/svg/SVGParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ open class SVGParser {

/// Parse an SVG file identified by the specified bundle, name and file extension.
/// - returns: Root node of the corresponding Macaw scene.
open class func parse(bundle: Bundle, path: String, ofType: String = "svg") -> Node {
let path = bundle.path(forResource: path, ofType: ofType)
let text = try! String(contentsOfFile: path!, encoding: String.Encoding.utf8)
return SVGParser.parse(text: text)
open class func parse(bundle: Bundle, path: String, ofType: String = "svg") -> Node? {
guard let path = bundle.path(forResource: path, ofType: ofType) else {
return .none
}
do {
let text = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
return SVGParser.parse(text: text)
} catch _ {
return .none
}
}

/// Parse an SVG file identified by the specified name and file extension.
/// - returns: Root node of the corresponding Macaw scene.
open class func parse(path: String, ofType: String = "svg") -> Node {
open class func parse(path: String, ofType: String = "svg") -> Node? {
return SVGParser.parse(bundle: Bundle.main, path: path, ofType: ofType)
}

Expand Down
6 changes: 3 additions & 3 deletions Source/svg/SVGView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ open class SVGView: MacawView {

private let rootNode = Group()
private func render() {
guard let svgNode = SVGParser.parse(path: fileName ?? "") else {
return
}
let viewBounds = self.bounds
let svgNode = SVGParser.parse(
path: fileName ?? ""
)
if let nodeBounds = svgNode.bounds()?.cgRect() {
let svgWidth = nodeBounds.origin.x + nodeBounds.width
let svgHeight = nodeBounds.origin.y + nodeBounds.height
Expand Down

0 comments on commit 7a5f59c

Please sign in to comment.