Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Samples.bundle/checkmark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion SwiftDraw/Parser.XML.Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ extension XMLParser {
}

private func parseColorNone(data: String) -> DOM.Color? {
if data.trimmingCharacters(in: .whitespaces) == "none" {
let trimmed = data.trimmingCharacters(in: .whitespaces)
if trimmed == "none" || trimmed == "transparent" {
return DOM.Color.none // .none resolves to Optional.none
}
return nil
Expand Down
18 changes: 11 additions & 7 deletions SwiftDraw/Renderer.SFSymbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ extension SFSymbolRenderer {
case black
}


func getInsets(for variant: Variant) -> CommandLine.Insets {
switch variant {
case .regular:
Expand Down Expand Up @@ -177,7 +176,8 @@ extension SFSymbolRenderer {
static func getSymbolPaths(for layer: LayerTree.Layer,
ctm: LayerTree.Transform.Matrix = .identity) -> [SymbolPath] {

guard layer.opacity > 0 else { return [] }
let isSFSymbolLayer = containsAcceptedName(layer.class)
guard isSFSymbolLayer || layer.opacity > 0 else { return [] }
guard layer.clip.isEmpty else {
print("Warning:", "clip-path unsupported in SF Symbols.", to: &.standardError)
return []
Expand All @@ -190,12 +190,15 @@ extension SFSymbolRenderer {
let ctm = ctm.concatenated(layer.transform.toMatrix())
var paths = [SymbolPath]()

let symbolClass = containsAcceptedName(layer.class) ? layer.class : nil
let symbolClass = isSFSymbolLayer ? layer.class : nil

for c in layer.contents {
switch c {
case let .shape(shape, stroke, fill):
if let path = makePath(for: shape, stoke: stroke, fill: fill)?.applying(matrix: ctm) {
if let path = makePath(for: shape,
stoke: stroke,
fill: fill,
preserve: isSFSymbolLayer)?.applying(matrix: ctm) {
if fill.rule == .evenodd {
paths.append(SymbolPath(class: symbolClass, path: path.makeNonZero()))
} else {
Expand All @@ -218,13 +221,14 @@ extension SFSymbolRenderer {

static func makePath(for shape: LayerTree.Shape,
stoke: LayerTree.StrokeAttributes,
fill: LayerTree.FillAttributes) -> LayerTree.Path? {
fill: LayerTree.FillAttributes,
preserve: Bool) -> LayerTree.Path? {

if fill.fill != .none && fill.opacity > 0 {
if preserve || (fill.fill != .none && fill.opacity > 0) {
return shape.path
}

if stoke.color != .none && stoke.width > 0 {
if preserve || (stoke.color != .none && stoke.width > 0) {
#if canImport(CoreGraphics)
return expandOutlines(for: shape.path, stroke: stoke)
#else
Expand Down
6 changes: 6 additions & 0 deletions SwiftDrawTests/Parser.XML.ColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ final class ParserColorTests: XCTestCase {
XCTAssertEqual(try XMLParser().parseColor("\t none \t"), .none)
}

func testColorTransparent() {
XCTAssertEqual(try XMLParser().parseColor("transparent"), .none)
XCTAssertEqual(try XMLParser().parseColor(" transparent"), .none)
XCTAssertEqual(try XMLParser().parseColor("\t transparent \t"), .none)
}

func testColorCurrent() {
XCTAssertEqual(try XMLParser().parseColor("currentColor"), .currentColor)
XCTAssertEqual(try XMLParser().parseColor(" currentColor"), .currentColor)
Expand Down
56 changes: 56 additions & 0 deletions SwiftDrawTests/Renderer.SFSymbolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,62 @@ final class RendererSFSymbolTests: XCTestCase {

}

func testTransparentLayers_Are_Removed() throws {
let source = try DOM.SVG.parse(#"""
<?xml version="1.0" encoding="UTF-8"?>
<svg width="64" height="64" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect width="100" height="100" fill="currentColor" />
<rect width="100" height="100" fill="transparent" />
<rect width="100" height="100" fill="currentColor" opacity="0" />
</svg>
"""#)

let template = try SFSymbolTemplate.parse(
SFSymbolRenderer.render(svg: source)
)

XCTAssertEqual(
template.ultralight.contents.paths.count,
1
)
XCTAssertEqual(
template.regular.contents.paths.count,
1
)
XCTAssertEqual(
template.black.contents.paths.count,
1
)
}

func testTransparentSFSymboleLayers_AreNot_Removed() throws {
let source = try DOM.SVG.parse(#"""
<?xml version="1.0" encoding="UTF-8"?>
<svg width="64" height="64" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect width="100" height="100" fill="currentColor" />
<rect width="100" height="100" fill="transparent" class="multicolor-0:custom" />
<rect width="100" height="100" fill="currentColor" opacity="0" class="SFSymbolsPreview" />
</svg>
"""#)

let template = try SFSymbolTemplate.parse(
SFSymbolRenderer.render(svg: source)
)

XCTAssertEqual(
template.ultralight.contents.paths.count,
3
)
XCTAssertEqual(
template.regular.contents.paths.count,
3
)
XCTAssertEqual(
template.black.contents.paths.count,
3
)
}

#if canImport(CoreGraphics)
func testStrokeSymbol() throws {
let url = try Bundle.test.url(forResource: "key.svg")
Expand Down