Skip to content

Commit 4e70266

Browse files
committed
Preserve all layers marked with SFSymbol classes
1 parent 4200c3f commit 4e70266

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

SwiftDraw/Renderer.SFSymbol.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ extension SFSymbolRenderer {
126126
case black
127127
}
128128

129-
130129
func getInsets(for variant: Variant) -> CommandLine.Insets {
131130
switch variant {
132131
case .regular:
@@ -177,7 +176,8 @@ extension SFSymbolRenderer {
177176
static func getSymbolPaths(for layer: LayerTree.Layer,
178177
ctm: LayerTree.Transform.Matrix = .identity) -> [SymbolPath] {
179178

180-
guard layer.opacity > 0 else { return [] }
179+
let isSFSymbolLayer = containsAcceptedName(layer.class)
180+
guard isSFSymbolLayer || layer.opacity > 0 else { return [] }
181181
guard layer.clip.isEmpty else {
182182
print("Warning:", "clip-path unsupported in SF Symbols.", to: &.standardError)
183183
return []
@@ -190,17 +190,22 @@ extension SFSymbolRenderer {
190190
let ctm = ctm.concatenated(layer.transform.toMatrix())
191191
var paths = [SymbolPath]()
192192

193-
let symbolClass = containsAcceptedName(layer.class) ? layer.class : nil
193+
let symbolClass = isSFSymbolLayer ? layer.class : nil
194194

195195
for c in layer.contents {
196196
switch c {
197197
case let .shape(shape, stroke, fill):
198-
if let path = makePath(for: shape, stoke: stroke, fill: fill)?.applying(matrix: ctm) {
198+
if let path = makePath(for: shape,
199+
stoke: stroke,
200+
fill: fill,
201+
preserve: isSFSymbolLayer)?.applying(matrix: ctm) {
199202
if fill.rule == .evenodd {
200203
paths.append(SymbolPath(class: symbolClass, path: path.makeNonZero()))
201204
} else {
202205
paths.append(SymbolPath(class: symbolClass, path: path))
203206
}
207+
} else {
208+
print("skippibng")
204209
}
205210
case let .text(text, point, attributes):
206211
if let path = makePath(for: text, at: point, with: attributes) {
@@ -218,13 +223,14 @@ extension SFSymbolRenderer {
218223

219224
static func makePath(for shape: LayerTree.Shape,
220225
stoke: LayerTree.StrokeAttributes,
221-
fill: LayerTree.FillAttributes) -> LayerTree.Path? {
226+
fill: LayerTree.FillAttributes,
227+
preserve: Bool) -> LayerTree.Path? {
222228

223-
if fill.fill != .none && fill.opacity > 0 {
229+
if preserve || (fill.fill != .none && fill.opacity > 0) {
224230
return shape.path
225231
}
226232

227-
if stoke.color != .none && stoke.width > 0 {
233+
if preserve || (stoke.color != .none && stoke.width > 0) {
228234
#if canImport(CoreGraphics)
229235
return expandOutlines(for: shape.path, stroke: stoke)
230236
#else

SwiftDrawTests/Renderer.SFSymbolTests.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,62 @@ final class RendererSFSymbolTests: XCTestCase {
7272

7373
}
7474

75+
func testTransparentLayers_Are_Removed() throws {
76+
let source = try DOM.SVG.parse(#"""
77+
<?xml version="1.0" encoding="UTF-8"?>
78+
<svg width="64" height="64" version="1.1" xmlns="http://www.w3.org/2000/svg">
79+
<rect width="100" height="100" fill="currentColor" />
80+
<rect width="100" height="100" fill="transparent" />
81+
<rect width="100" height="100" fill="currentColor" opacity="0" />
82+
</svg>
83+
"""#)
84+
85+
let template = try SFSymbolTemplate.parse(
86+
SFSymbolRenderer.render(svg: source)
87+
)
88+
89+
XCTAssertEqual(
90+
template.ultralight.contents.paths.count,
91+
1
92+
)
93+
XCTAssertEqual(
94+
template.regular.contents.paths.count,
95+
1
96+
)
97+
XCTAssertEqual(
98+
template.black.contents.paths.count,
99+
1
100+
)
101+
}
102+
103+
func testTransparentSFSymboleLayers_AreNot_Removed() throws {
104+
let source = try DOM.SVG.parse(#"""
105+
<?xml version="1.0" encoding="UTF-8"?>
106+
<svg width="64" height="64" version="1.1" xmlns="http://www.w3.org/2000/svg">
107+
<rect width="100" height="100" fill="currentColor" />
108+
<rect width="100" height="100" fill="transparent" class="multicolor-0:custom" />
109+
<rect width="100" height="100" fill="currentColor" opacity="0" class="SFSymbolsPreview" />
110+
</svg>
111+
"""#)
112+
113+
let template = try SFSymbolTemplate.parse(
114+
SFSymbolRenderer.render(svg: source)
115+
)
116+
117+
XCTAssertEqual(
118+
template.ultralight.contents.paths.count,
119+
3
120+
)
121+
XCTAssertEqual(
122+
template.regular.contents.paths.count,
123+
3
124+
)
125+
XCTAssertEqual(
126+
template.black.contents.paths.count,
127+
3
128+
)
129+
}
130+
75131
#if canImport(CoreGraphics)
76132
func testStrokeSymbol() throws {
77133
let url = try Bundle.test.url(forResource: "key.svg")

0 commit comments

Comments
 (0)