mirror of
https://github.com/exyte/Macaw.git
synced 2024-09-11 13:15:35 +03:00
Fix #344: add parse multiple svg nodes
This commit is contained in:
parent
91483c0143
commit
26ab5c5137
@ -57,29 +57,6 @@ class GroupRenderer: NodeRenderer {
|
||||
renderers.removeAll()
|
||||
}
|
||||
|
||||
private func updateRenderers() {
|
||||
|
||||
renderers.forEach {
|
||||
animationCache?.freeLayerHard($0)
|
||||
$0.dispose()
|
||||
}
|
||||
renderers.removeAll()
|
||||
|
||||
if let updatedRenderers = group?.contents.compactMap ({ child -> NodeRenderer? in
|
||||
let childRenderer = RenderUtils.createNodeRenderer(child, view: view, animationCache: animationCache)
|
||||
childRenderer.parentRenderer = self
|
||||
return childRenderer
|
||||
}) {
|
||||
renderers = updatedRenderers
|
||||
}
|
||||
|
||||
var parent: NodeRenderer = self
|
||||
while let parentRenderer = parent.parentRenderer {
|
||||
parent = parentRenderer
|
||||
}
|
||||
parent.calculateZPositionRecursively()
|
||||
}
|
||||
|
||||
override func replaceNode(with replacementNode: Node) {
|
||||
super.replaceNode(with: replacementNode)
|
||||
|
||||
@ -87,4 +64,35 @@ class GroupRenderer: NodeRenderer {
|
||||
group = node
|
||||
}
|
||||
}
|
||||
|
||||
private func updateRenderers() {
|
||||
updateLayout()
|
||||
|
||||
renderers.forEach {
|
||||
animationCache?.freeLayerHard($0)
|
||||
$0.dispose()
|
||||
}
|
||||
renderers.removeAll()
|
||||
|
||||
if let updatedRenderers = group?.contents.compactMap ({ child -> NodeRenderer? in
|
||||
let childRenderer = RenderUtils.createNodeRenderer(child, view: view, animationCache: animationCache)
|
||||
childRenderer.parentRenderer = self
|
||||
return childRenderer
|
||||
}) {
|
||||
renderers = updatedRenderers
|
||||
}
|
||||
|
||||
var parent: NodeRenderer = self
|
||||
while let parentRenderer = parent.parentRenderer {
|
||||
parent = parentRenderer
|
||||
}
|
||||
parent.calculateZPositionRecursively()
|
||||
}
|
||||
|
||||
private func updateLayout() {
|
||||
guard let canvas = group as? SVGCanvas, let view = view else {
|
||||
return
|
||||
}
|
||||
_ = canvas.layout(size: view.bounds.size.toMacaw())
|
||||
}
|
||||
}
|
||||
|
@ -185,6 +185,20 @@ open class SVGParser {
|
||||
}
|
||||
}
|
||||
|
||||
private func parseSVG(_ node: XMLIndexer, groupStyle: [String: String] = [:]) throws -> Node? {
|
||||
guard let element = node.element else {
|
||||
return .none
|
||||
}
|
||||
guard let layout = try parseViewBox(element), let group = try parseGroup(node, style: groupStyle) else {
|
||||
return .none
|
||||
}
|
||||
let root = SVGCanvas(layout: layout, contents: group.contents)
|
||||
if let opacity = element.attribute(by: "opacity") {
|
||||
root.opacity = getOpacity(opacity.text)
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
fileprivate func parseViewBox(_ element: SWXMLHash.XMLElement) throws -> SVGNodeLayout? {
|
||||
if element.allAttributes["width"] == nil && element.allAttributes["height"] == nil && element.allAttributes["viewBox"] == nil {
|
||||
return .none
|
||||
@ -318,6 +332,8 @@ open class SVGParser {
|
||||
case "title", "desc", "mask", "clip", "filter",
|
||||
"linearGradient", "radialGradient", SVGKeys.fill:
|
||||
break
|
||||
case "svg":
|
||||
return try parseSVG(node, groupStyle: style)
|
||||
default:
|
||||
print("SVG parsing error. Shape \(element.name) not supported")
|
||||
return .none
|
||||
|
Loading…
Reference in New Issue
Block a user