/
SVGNode.swift
213 lines (183 loc) · 6.88 KB
/
SVGNode.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// File.swift
//
//
// Created by Mathieu Barnachon on 28/06/2019.
//
import Foundation
/// Represents an SVG tag like `<circle>{{content}}</circle>`, with a tag and an
/// optional SVG child.
struct SVGNode: SVG {
var tag: String
var child: SVG?
var inline = false
func getTag() -> String? {
return self.tag
}
func renderAsHTML(into stream: HTMLOutputStream, attributes: [Attribute]) {
/// Open the tag
stream.writeIndent()
stream.write("<")
stream.write(tag)
/// Append the tag with all attributes.
for attr in attributes {
stream.write(" ")
stream.write(attr.key)
/// If an attribute does not have a value, do not add the equal sign.
if let value = attr.value {
stream.write("=")
stream.writeDoubleQuoted(value)
}
}
/// If the element has no children, close it on the same line.
guard let child = child else {
stream.write("/>")
stream.writeNewline()
return
}
stream.write(">")
/// Check to see if the tag being streamed should write its child and close it on the same line
if inline {
if let childString = child as? String {
stream.write(childString)
}
/// Otherwise, add the child indented on a new line
} else {
stream.writeNewline()
stream.withIndent {
child.renderAsHTML(into: stream, attributes: [])
}
stream.writeIndent()
}
/// Add a new line and write the closing tag.
stream.write("</")
stream.write(tag)
stream.write(">")
stream.writeNewline()
}
}
/// Wraps an SVG object with a given attribute. these attributes are collected
/// while walking the node hierarchy and finally printed when printing an
/// `SVGNode`.
struct AttributedSVGNode: SVG {
let attribute: Attribute
let child: SVG
func getTag() -> String? {
return child.getTag()
}
func renderAsHTML(into stream: HTMLOutputStream, attributes: [Attribute]) {
var fullAttrs = attributes
fullAttrs.append(attribute)
child.renderAsHTML(into: stream, attributes: fullAttrs)
}
}
/// MultiNode is an implementation detail for representing multiple sequenced
/// SVG nodes
struct MultiSVGNode: SVG {
func getTag() -> String? {
return nil
}
let children: [SVG]
func renderAsHTML(into stream: HTMLOutputStream, attributes: [Attribute]) {
for child in children {
child.renderAsHTML(into: stream, attributes: attributes)
}
}
}
struct HTMLSVGNode: HTML {
let node: SVGNode
func renderAsHTML(into stream: HTMLOutputStream, attributes: [Attribute]) {
node.renderAsHTML(into: stream, attributes: attributes)
}
func getTag() -> String? {
return node.getTag()
}
}
/// Wraps multiples SVG filters into a `<filter></filter>` element.
public struct SVGFilter: SVG {
let node: SVGNode
let attributes: [Attribute]
public init(id: String,
types: [SVGFilterType],
attributes: [Attribute] = []) {
node = SVGNode(tag: "filter", child: MultiSVGNode(children: types.compactMap{ $0.node }))
self.attributes = [Attribute(key: "id", value: id)] + attributes
}
public func renderAsHTML(into stream: HTMLOutputStream, attributes: [Attribute]) {
node.renderAsHTML(into: stream, attributes: self.attributes + attributes)
}
public func getTag() -> String? {
return node.getTag()
}
}
/// Create a SVG linear or radial gradient.
public struct SVGGradient: SVG {
let node: SVGNode
let attributes: [Attribute]
public enum GradientType {
case linear
case radial
}
/// Create a gradient with full control.
/// - Parameters:
/// - id: The identifier of the gradient. Used to be applied to a SVG element.
/// - type: The type of gradient, linear or radial.
/// - offsets: The stops for the colors, 2 are needed to get a valid gradient.
/// - attributes: A list of attributes to apply to the gradient element. Do not set `id` here.
public init(id: String,
type: GradientType,
offsets: [(offset: String, style: [StyleAttribute])],
attributes: [Attribute] = []) {
let stops = offsets.compactMap {
SVGNode(tag: "stop")
.attr("offset", $0.offset)
.style($0.style)
}
switch type {
case .linear:
node = SVGNode(tag: "linearGradient", child: MultiSVGNode(children: stops))
case .radial:
node = SVGNode(tag: "radialGradient", child: MultiSVGNode(children: stops))
}
self.attributes = [Attribute(key: "id", value: id)] + attributes
}
/// Convinent initializer to create a linear gradient.
public init(id: String,
controls: (x1: String, y1: String, x2: String, y2: String),
offsets: [(offset: String, style: [StyleAttribute])]) {
let stops = offsets.compactMap {
SVGNode(tag: "stop")
.attr("offset", $0.offset)
.style($0.style)
}
node = SVGNode(tag: "linearGradient", child: MultiSVGNode(children: stops))
attributes = [Attribute(key: "id", value: id),
Attribute(key: "x1", value: controls.x1),
Attribute(key: "y1", value: controls.y1),
Attribute(key: "x2", value: controls.x2),
Attribute(key: "y2", value: controls.y2)]
}
/// Convinent initializer to create a radial gradient.
public init(id: String,
controls: (cx: String, cy: String, r: String, fx: String, fy: String),
offsets: [(offset: String, style: [StyleAttribute])]) {
let stops = offsets.compactMap {
SVGNode(tag: "stop")
.attr("offset", $0.offset)
.style($0.style)
}
node = SVGNode(tag: "radialGradient", child: MultiSVGNode(children: stops))
attributes = [Attribute(key: "id", value: id),
Attribute(key: "cx", value: controls.cx),
Attribute(key: "cy", value: controls.cy),
Attribute(key: "r", value: controls.r),
Attribute(key: "fx", value: controls.fx),
Attribute(key: "fy", value: controls.fy)]
}
public func renderAsHTML(into stream: HTMLOutputStream, attributes: [Attribute]) {
node.renderAsHTML(into: stream, attributes: self.attributes + attributes)
}
public func getTag() -> String? {
return node.getTag()
}
}