Skip to content

Commit 4fd2438

Browse files
authored
Merge pull request #98 from swhitty/mask-graphics-element
convert Mask to GraphicsElement
2 parents f946d7b + 37f3ad3 commit 4fd2438

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

DOM/Sources/DOM.SVG.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,14 @@ package extension DOM {
8080
package var childElements = [GraphicsElement]()
8181
}
8282

83-
struct Mask: ContainerElement {
84-
package var id: String
83+
final class Mask: GraphicsElement, ContainerElement {
8584
package var childElements = [GraphicsElement]()
85+
86+
init(id: String, childElements: [GraphicsElement] = []) {
87+
super.init()
88+
self.id = id
89+
self.childElements = childElements
90+
}
8691
}
8792

8893
struct StyleSheet {

DOM/Sources/Parser.XML.SVG.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,12 @@ package extension XMLParser {
176176
let att = try parseAttributes(e)
177177
let id: String = try att.parseString("id")
178178

179-
let children = try parseGraphicsElements(e.children)
180-
return DOM.Mask(id: id, childElements: children)
179+
let mask = DOM.Mask(id: id)
180+
mask.class = try att.parseString("class")
181+
mask.attributes = try parsePresentationAttributes(e)
182+
mask.style = try parseStyleAttributes(e)
183+
mask.childElements = try parseGraphicsElements(e.children)
184+
return mask
181185
}
182186

183187
func parsePatterns(_ e: XML.Element) throws -> [DOM.Pattern] {

Samples.bundle/robin.svg

Lines changed: 29 additions & 0 deletions
Loading

SwiftDraw/Sources/LayerTree/LayerTree.Builder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ extension LayerTree {
190190

191191
let l = Layer()
192192

193+
let maskState = createState(for: mask, inheriting: State())
193194
mask.childElements.forEach {
194-
let contents = Layer.Contents.layer(makeLayer(from: $0, inheriting: State()))
195+
let contents = Layer.Contents.layer(makeLayer(from: $0, inheriting: maskState))
195196
l.appendContents(contents)
196197
}
197198

SwiftDraw/Tests/LayerTree/LayerTree.BuilderTests.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,20 @@ final class LayerTreeBuilderTests: XCTestCase {
5454
func testDOMMaskMakesLayer() {
5555
let circle = DOM.Circle(cx: 5, cy: 5, r: 5)
5656
let line = DOM.Line(x1: 0, y1: 0, x2: 10, y2: 0)
57+
let mask = DOM.Mask(id: "mask1", childElements: [circle, line])
58+
mask.attributes.fill = .color(.keyword(.white))
59+
5760
let svg = DOM.SVG(width: 10, height: 10)
58-
svg.defs.masks.append(DOM.Mask(id: "mask1", childElements: [circle, line]))
59-
61+
svg.defs.masks.append(mask)
62+
6063
let builder = LayerTree.Builder(svg: svg)
6164

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

6568
let layer = builder.createMaskLayer(for: element)
66-
6769
XCTAssertEqual(layer?.contents.count, 2)
70+
XCTAssertEqual(layer?.contents[0].shape?.fill.fill, .color(.white))
6871
}
6972

7073
func testDOMClipMakesShape() {
@@ -206,3 +209,10 @@ extension DOM.Group {
206209
return group
207210
}
208211
}
212+
213+
private extension LayerTree.Layer.Contents {
214+
var shape: (shape: LayerTree.Shape, stroke: LayerTree.StrokeAttributes, fill: LayerTree.FillAttributes)? {
215+
guard case let .shape(shape, stroke, fill) = self else { return nil }
216+
return (shape, stroke, fill)
217+
}
218+
}

0 commit comments

Comments
 (0)