Skip to content

Fix SFSymbol NonZero winding paths on macOS #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2023
Merged
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
30 changes: 28 additions & 2 deletions SwiftDraw/LayerTree.Path+Reversed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private extension LayerTree.Path {

for p in subpaths {
let node = SubPathNode(p)
if let idx = nodes.firstIndex(where: { $0.bounds.contains(point: node.bounds.center) }) {
if let idx = nodes.firstIndex(where: { $0.containsNode(node) }) {
nodes[idx].append(node)
} else {
nodes.append(node)
Expand All @@ -77,8 +77,16 @@ private extension LayerTree.Path {
}
}

#if canImport(CoreGraphics)
import CoreGraphics
#endif

private struct SubPathNode {
let path: LayerTree.Path
#if canImport(CoreGraphics)
let cgPath: CGPath
#endif

let bounds: LayerTree.Rect
let direction: LayerTree.Path.Direction
var children: [SubPathNode] = []
Expand All @@ -87,10 +95,14 @@ private struct SubPathNode {
self.path = path
self.bounds = path.bounds
self.direction = path.segments.direction

#if canImport(CoreGraphics)
self.cgPath = CGProvider().createPath(from: .path(path))
#endif
}

mutating func append(_ node: SubPathNode) {
if let idx = children.firstIndex(where: { $0.bounds.contains(point: node.bounds.center) }) {
if let idx = children.firstIndex(where: { $0.containsNode(node) }) {
children[idx].append(node)
} else {
children.append(node)
Expand All @@ -101,6 +113,20 @@ private struct SubPathNode {
windPaths(direction)
}

func containsNode(_ node: SubPathNode) -> Bool {
#if canImport(CoreGraphics)
let provider = CGProvider()
for point in node.path.segments.compactMap(\.location) {
if cgPath.contains(provider.createPoint(from: point)) {
return true
}
}
return false
#else
return bounds.contains(point: node.bounds.center)
#endif
}

func windPaths(_ direction: LayerTree.Path.Direction) -> [LayerTree.Path] {
var paths = [LayerTree.Path]()

Expand Down