Skip to content

Commit 658b2e1

Browse files
committed
Parse Radial Gradients
1 parent 48b210a commit 658b2e1

11 files changed

+346
-83
lines changed
File renamed without changes.

Samples/radialGradient.svg

Lines changed: 41 additions & 0 deletions
Loading

SwiftDraw/DOM.Gradient.swift renamed to SwiftDraw/DOM.LinearGradient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// DOM.Gradient.swift
2+
// DOM.LinearGradient.swift
33
// SwiftDraw
44
//
55
// Created by Simon Whitty on 31/12/16.

SwiftDraw/DOM.RadialGradient.swift

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// DOM.RadialGradient.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 13/8/22.
6+
// Copyright 2022 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
extension DOM {
33+
34+
final class RadialGradient: Element {
35+
36+
var id: String
37+
38+
var stops: [Stop]
39+
var gradientUnits: Units?
40+
41+
//references another RadialGradient element id within defs
42+
var href: URL?
43+
44+
init(id: String) {
45+
self.id = id
46+
self.stops = []
47+
}
48+
49+
struct Stop: Equatable {
50+
var offset: Float
51+
var color: Color
52+
var opacity: Float
53+
54+
init(offset: Float, color: Color, opacity: Opacity = 1.0) {
55+
self.offset = offset
56+
self.color = color
57+
self.opacity = opacity
58+
}
59+
}
60+
}
61+
}
62+
63+
extension DOM.RadialGradient: Equatable {
64+
static func ==(lhs: DOM.RadialGradient, rhs: DOM.RadialGradient) -> Bool {
65+
return
66+
lhs.id == rhs.id &&
67+
lhs.stops == rhs.stops
68+
}
69+
}
70+
71+
extension DOM.RadialGradient {
72+
73+
enum Units: String {
74+
case userSpaceOnUse
75+
case objectBoundingBox
76+
}
77+
}

SwiftDraw/DOM.SVG.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extension DOM {
5454
struct Defs {
5555
var clipPaths = [ClipPath]()
5656
var linearGradients = [LinearGradient]()
57+
var radialGradients = [RadialGradient]()
5758
var masks = [Mask]()
5859
var patterns = [Pattern]()
5960

SwiftDraw/Parser.XML.Gradient.swift

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//
2+
// Parser.XML.LinearGradient.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 31/12/16.
6+
// Copyright 2020 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
extension XMLParser {
33+
34+
func parseLinearGradients(_ e: XML.Element) throws -> [DOM.LinearGradient] {
35+
var gradients = [DOM.LinearGradient]()
36+
37+
for n in e.children {
38+
if n.name == "linearGradient" {
39+
gradients.append(try parseLinearGradient(n))
40+
} else {
41+
gradients.append(contentsOf: try parseLinearGradients(n))
42+
}
43+
}
44+
return gradients
45+
}
46+
47+
func parseLinearGradient(_ e: XML.Element) throws -> DOM.LinearGradient {
48+
guard e.name == "linearGradient" else {
49+
throw Error.invalid
50+
}
51+
52+
let nodeAtt: AttributeParser = try parseAttributes(e)
53+
let node = DOM.LinearGradient(id: try nodeAtt.parseString("id"))
54+
node.x1 = try nodeAtt.parseCoordinate("x1")
55+
node.y1 = try nodeAtt.parseCoordinate("y1")
56+
node.x2 = try nodeAtt.parseCoordinate("x2")
57+
node.y2 = try nodeAtt.parseCoordinate("y2")
58+
59+
for n in e.children where n.name == "stop" {
60+
let att: AttributeParser = try parseAttributes(n)
61+
node.stops.append(try parseLinearGradientStop(att))
62+
}
63+
64+
node.gradientUnits = try nodeAtt.parseRaw("gradientUnits")
65+
node.href = try? nodeAtt.parseUrl("xlink:href")
66+
67+
return node
68+
}
69+
70+
func parseLinearGradientStop(_ att: AttributeParser) throws -> DOM.LinearGradient.Stop {
71+
let offset: DOM.Float? = try? att.parsePercentage("offset")
72+
let color: DOM.Color = try att.parseFill("stop-color").getColor()
73+
let opacity: DOM.Float? = try att.parsePercentage("stop-opacity")
74+
return DOM.LinearGradient.Stop(offset: offset ?? 0, color: color, opacity: opacity ?? 1.0)
75+
}
76+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// Parser.XML.RadialGradient.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 13/8/22.
6+
// Copyright 2022 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
extension XMLParser {
33+
34+
func parseRadialGradients(_ e: XML.Element) throws -> [DOM.RadialGradient] {
35+
var gradients = [DOM.RadialGradient]()
36+
37+
for n in e.children {
38+
if n.name == "radialGradient" {
39+
gradients.append(try parseRadialGradient(n))
40+
} else {
41+
gradients.append(contentsOf: try parseRadialGradients(n))
42+
}
43+
}
44+
return gradients
45+
}
46+
47+
func parseRadialGradient(_ e: XML.Element) throws -> DOM.RadialGradient {
48+
guard e.name == "radialGradient" else {
49+
throw Error.invalid
50+
}
51+
52+
let nodeAtt: AttributeParser = try parseAttributes(e)
53+
let node = DOM.RadialGradient(id: try nodeAtt.parseString("id"))
54+
55+
for n in e.children where n.name == "stop" {
56+
let att: AttributeParser = try parseAttributes(n)
57+
node.stops.append(try parseRadialGradientStop(att))
58+
}
59+
60+
node.gradientUnits = try nodeAtt.parseRaw("gradientUnits")
61+
node.href = try? nodeAtt.parseUrl("xlink:href")
62+
63+
return node
64+
}
65+
66+
func parseRadialGradientStop(_ att: AttributeParser) throws -> DOM.RadialGradient.Stop {
67+
let offset: DOM.Float? = try? att.parsePercentage("offset")
68+
let color: DOM.Color = try att.parseFill("stop-color").getColor()
69+
let opacity: DOM.Float? = try att.parsePercentage("stop-opacity")
70+
return DOM.RadialGradient.Stop(offset: offset ?? 0, color: color, opacity: opacity ?? 1.0)
71+
}
72+
}

SwiftDraw/Parser.XML.SVG.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ extension XMLParser {
9090
var defs = DOM.SVG.Defs()
9191
defs.clipPaths = try parseClipPaths(e)
9292
defs.linearGradients = try parseLinearGradients(e)
93+
defs.radialGradients = try parseRadialGradients(e)
9394
defs.masks = try parseMasks(e)
9495
defs.patterns = try parsePatterns(e)
9596

0 commit comments

Comments
 (0)