Skip to content

Commit 28c1586

Browse files
committed
Preserve SVG class in SFSymbols
1 parent 796bdad commit 28c1586

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

Samples/circle.svg

Lines changed: 10 additions & 0 deletions
Loading

SwiftDraw/LayerTree.Builder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extension LayerTree {
7878
let state = createState(for: element, inheriting: previousState)
7979
let attributes = element.attributes
8080
let l = Layer()
81-
81+
l.class = element.class
8282
guard state.display == .inline else { return l }
8383

8484
l.transform = Builder.createTransforms(from: attributes.transform ?? [])

SwiftDraw/LayerTree.Layer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
extension LayerTree {
3030
final class Layer: Equatable {
31+
var `class`: String? = nil
3132
var contents: [Contents] = []
3233
var opacity: Float = 1.0
3334
var transform: [Transform] = []

SwiftDraw/Renderer.SFSymbol.swift

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,19 @@ extension SFSymbolRenderer {
138138
return bounds
139139
}
140140

141-
static func getPaths(for svg: DOM.SVG) -> [LayerTree.Path]? {
141+
static func getPaths(for svg: DOM.SVG) -> [SymbolPath]? {
142142
let layer = LayerTree.Builder(svg: svg).makeLayer()
143-
let paths = getPaths(for: layer)
143+
let paths = getSymbolPaths(for: layer)
144144
return paths.isEmpty ? nil : paths
145145
}
146146

147-
static func getPaths(for layer: LayerTree.Layer,
148-
ctm: LayerTree.Transform.Matrix = .identity) -> [LayerTree.Path] {
147+
struct SymbolPath {
148+
var `class`: String?
149+
var path: LayerTree.Path
150+
}
151+
152+
static func getSymbolPaths(for layer: LayerTree.Layer,
153+
ctm: LayerTree.Transform.Matrix = .identity) -> [SymbolPath] {
149154

150155
guard layer.opacity > 0 else { return [] }
151156
guard layer.clip.isEmpty else {
@@ -158,24 +163,24 @@ extension SFSymbolRenderer {
158163
}
159164

160165
let ctm = ctm.concatenated(layer.transform.toMatrix())
161-
var paths = [LayerTree.Path]()
166+
var paths = [SymbolPath]()
162167

163168
for c in layer.contents {
164169
switch c {
165170
case let .shape(shape, stroke, fill):
166171
if let path = makePath(for: shape, stoke: stroke, fill: fill)?.applying(matrix: ctm) {
167172
if fill.rule == .evenodd {
168-
paths.append(path.makeNonZero())
173+
paths.append(SymbolPath(class: layer.class, path: path.makeNonZero()))
169174
} else {
170-
paths.append(path)
175+
paths.append(SymbolPath(class: layer.class, path: path))
171176
}
172177
}
173178
case let .text(text, point, attributes):
174179
if let path = makePath(for: text, at: point, with: attributes) {
175-
paths.append(path.applying(matrix: ctm))
180+
paths.append(SymbolPath(class: layer.class, path: path.applying(matrix: ctm)))
176181
}
177182
case .layer(let l):
178-
paths.append(contentsOf: getPaths(for: l, ctm: ctm))
183+
paths.append(contentsOf: getSymbolPaths(for: l, ctm: ctm))
179184
default:
180185
()
181186
}
@@ -216,11 +221,11 @@ extension SFSymbolRenderer {
216221
#endif
217222
}
218223

219-
static func makeBounds(for paths: [LayerTree.Path]) -> LayerTree.Rect {
224+
static func makeBounds(for paths: [SymbolPath]) -> LayerTree.Rect {
220225
var min = LayerTree.Point.maximum
221226
var max = LayerTree.Point.minimum
222227
for p in paths {
223-
let bounds = p.bounds
228+
let bounds = p.path.bounds
224229
min = min.minimum(combining: .init(bounds.minX, bounds.minY))
225230
max = max.maximum(combining: .init(bounds.maxX, bounds.maxY))
226231
}
@@ -471,11 +476,15 @@ private extension ContainerElement {
471476

472477
private extension SFSymbolTemplate.Variant {
473478

474-
mutating func appendPaths(_ paths: [LayerTree.Path], from source: LayerTree.Rect) {
479+
mutating func appendPaths(_ paths: [SFSymbolRenderer.SymbolPath], from source: LayerTree.Rect) {
475480
let matrix = SFSymbolRenderer.makeTransformation(from: source, to: bounds)
476481
contents.paths = paths
477-
.map { $0.applying(matrix: matrix) }
478-
.map(SFSymbolRenderer.makeDOMPath)
482+
.map {
483+
let transformed = $0.path.applying(matrix: matrix)
484+
let dom = SFSymbolRenderer.makeDOMPath(for: transformed)
485+
dom.class = $0.class
486+
return dom
487+
}
479488

480489
let midX = bounds.midX
481490
let newWidth = ((source.width * matrix.a) / 2) + 10

SwiftDraw/XML.Formatter.SVG.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ extension XML.Formatter {
9494
var attributes = makeGraphicsAttributes(from: graphic.attributes)
9595
attributes["id"] = graphic.id
9696
attributes["style"] = makeStyleAttribute(from: graphic.style)
97+
attributes["class"] = graphic.class
9798
return attributes
9899
}
99100

0 commit comments

Comments
 (0)