Skip to content
Merged
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
9 changes: 7 additions & 2 deletions DOM/Sources/DOM.SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ package extension DOM {
package var childElements = [GraphicsElement]()
}

struct Mask: ContainerElement {
package var id: String
final class Mask: GraphicsElement, ContainerElement {
package var childElements = [GraphicsElement]()

init(id: String, childElements: [GraphicsElement] = []) {
super.init()
self.id = id
self.childElements = childElements
}
}

struct StyleSheet {
Expand Down
8 changes: 6 additions & 2 deletions DOM/Sources/Parser.XML.SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ package extension XMLParser {
let att = try parseAttributes(e)
let id: String = try att.parseString("id")

let children = try parseGraphicsElements(e.children)
return DOM.Mask(id: id, childElements: children)
let mask = DOM.Mask(id: id)
mask.class = try att.parseString("class")
mask.attributes = try parsePresentationAttributes(e)
mask.style = try parseStyleAttributes(e)
mask.childElements = try parseGraphicsElements(e.children)
return mask
}

func parsePatterns(_ e: XML.Element) throws -> [DOM.Pattern] {
Expand Down
29 changes: 29 additions & 0 deletions Samples.bundle/robin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion SwiftDraw/Sources/LayerTree/LayerTree.Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ extension LayerTree {

let l = Layer()

let maskState = createState(for: mask, inheriting: State())
mask.childElements.forEach {
let contents = Layer.Contents.layer(makeLayer(from: $0, inheriting: State()))
let contents = Layer.Contents.layer(makeLayer(from: $0, inheriting: maskState))
l.appendContents(contents)
}

Expand Down
16 changes: 13 additions & 3 deletions SwiftDraw/Tests/LayerTree/LayerTree.BuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,20 @@ final class LayerTreeBuilderTests: XCTestCase {
func testDOMMaskMakesLayer() {
let circle = DOM.Circle(cx: 5, cy: 5, r: 5)
let line = DOM.Line(x1: 0, y1: 0, x2: 10, y2: 0)
let mask = DOM.Mask(id: "mask1", childElements: [circle, line])
mask.attributes.fill = .color(.keyword(.white))

let svg = DOM.SVG(width: 10, height: 10)
svg.defs.masks.append(DOM.Mask(id: "mask1", childElements: [circle, line]))
svg.defs.masks.append(mask)

let builder = LayerTree.Builder(svg: svg)

let element = DOM.GraphicsElement()
element.attributes.mask = URL(string: "#mask1")

let layer = builder.createMaskLayer(for: element)

XCTAssertEqual(layer?.contents.count, 2)
XCTAssertEqual(layer?.contents[0].shape?.fill.fill, .color(.white))
}

func testDOMClipMakesShape() {
Expand Down Expand Up @@ -206,3 +209,10 @@ extension DOM.Group {
return group
}
}

private extension LayerTree.Layer.Contents {
var shape: (shape: LayerTree.Shape, stroke: LayerTree.StrokeAttributes, fill: LayerTree.FillAttributes)? {
guard case let .shape(shape, stroke, fill) = self else { return nil }
return (shape, stroke, fill)
}
}