Skip to content

Commit

Permalink
Fix recursive use and clipPath parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ystrot committed Oct 30, 2018
1 parent 4dc3781 commit 1018b76
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
26 changes: 26 additions & 0 deletions MacawTests/w3cSVGTests/struct-use-12-f-manual.reference
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,47 @@
"contents" : [
{
"contents" : [
{
"contents" : [

],
"node" : "Group"
}
],
"node" : "Group"
},
{
"contents" : [
{
"contents" : [

],
"node" : "Group"
}
],
"node" : "Group"
},
{
"contents" : [
{
"contents" : [
{
"contents" : [
{
"contents" : [

],
"node" : "Group"
},
{
"contents" : [

],
"node" : "Group"
}
],
"node" : "Group"
}
],
"node" : "Group"
}
Expand Down
23 changes: 15 additions & 8 deletions Source/svg/SVGParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ open class SVGParser {
if let element = node.element {
if element.name == "style" {
parseStyle(node)
} else if (element.name == "defs" || element.name == "g") {
} else if element.name == "defs" || element.name == "g" {
try node.children.forEach { child in
try prepareSvg(child)
}
Expand All @@ -160,7 +160,7 @@ open class SVGParser {
defMasks[id] = try parseMask(node)
case "filter":
defEffects[id] = try parseEffect(node)
case "clip":
case "clip", "clipPath":
defClip[id] = try parseClip(node)
default:
defNodes[id] = node
Expand Down Expand Up @@ -311,7 +311,8 @@ open class SVGParser {
stroke: getStroke(style, groupStyle: style), opacity: getOpacity(style), fontName: getFontName(style), fontSize: getFontSize(style), fontWeight: getFontWeight(style), pos: position)
case "use":
return try parseUse(node, groupStyle: style, place: position)
case "title":
case "title", "desc", "mask", "clip", "filter",
"linearGradient", "radialGradient", "fill":
break
default:
print("SVG parsing error. Shape \(element.name) not supported")
Expand Down Expand Up @@ -953,6 +954,8 @@ open class SVGParser {
return Transform.move(dx: xPos, dy: yPos)
}

private var usedReferenced = [String: String]()

fileprivate func parseUse(_ use: XMLIndexer, groupStyle: [String: String] = [:], place: Transform = .identity) throws -> Node? {
guard let element = use.element, let link = element.allAttributes["xlink:href"]?.text else {
return .none
Expand All @@ -962,9 +965,13 @@ open class SVGParser {
id = id.replacingOccurrences(of: "#", with: "")
}
if let referenceNode = self.defNodes[id] {
if let node = try parseNode(referenceNode, groupStyle: groupStyle) {
node.place = place.move(dx: getDoubleValue(element, attribute: "x") ?? 0, dy: getDoubleValue(element, attribute: "y") ?? 0)
return node
if usedReferenced[id] == nil {
usedReferenced[id] = id
if let node = try parseNode(referenceNode, groupStyle: groupStyle) {
node.place = place.move(dx: getDoubleValue(element, attribute: "x") ?? 0, dy: getDoubleValue(element, attribute: "y") ?? 0)
return node
}
usedReferenced.removeValue(forKey: id)
}
}
return .none
Expand Down Expand Up @@ -1213,8 +1220,8 @@ open class SVGParser {

let xScale = abs(transform.m11)
let yScale = abs(transform.m22)
if (xScale == yScale) {
r = r * xScale
if xScale == yScale {
r *= xScale
} else {
print("SVG parsing error. No oval radial gradients supported")
}
Expand Down

0 comments on commit 1018b76

Please sign in to comment.