Skip to content

Commit

Permalink
Merge pull request #29 from Desbeers/main
Browse files Browse the repository at this point in the history
Reduce amount of compiler directives
  • Loading branch information
BeauNouvelle authored Sep 23, 2023
2 parents 8040d5f + 6a0758a commit 38562c1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 130 deletions.
43 changes: 43 additions & 0 deletions Sources/SwiftyChords/Aliasses.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// File.swift
//
//
// Created by Nick Berendsen on 07/09/2023.
//

#if os(macOS)
import AppKit
#else
import UIKit
#endif


#if os(macOS)

/// Alias for NSImage
public typealias SWIFTImage = NSImage

/// Alias for NSColor
public typealias SWIFTColor = NSColor

let primaryColor = NSColor.labelColor
let backgroundColor = NSColor.windowBackgroundColor

/// Alias for NSFont
public typealias SWIFTFont = NSFont

#else

/// Alias for UIImage
public typealias SWIFTImage = UIImage

/// Alias for UIColor
public typealias SWIFTColor = UIColor

/// Alias for UIFont
public typealias SWIFTFont = UIFont

let primaryColor = UIColor.label
let backgroundColor = UIColor.systemBackground

#endif
119 changes: 38 additions & 81 deletions Sources/SwiftyChords/ChordPosition.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//
// File.swift
//
//
//
// Created by Beau Nouvelle on 2020-03-29.
//

import Foundation
import CoreGraphics

#if os(iOS)
import UIKit
#else
#if os(macOS)
import AppKit
#else
import UIKit
#endif

public struct ChordPosition: Codable, Identifiable, Equatable {

public init(id: UUID = UUID(), frets: [Int], fingers: [Int], baseFret: Int, barres: [Int], capo: Bool? = nil, midi: [Int], key: Chords.Key, suffix: Chords.Suffix) {
self.id = id
self.frets = frets
Expand All @@ -27,7 +27,7 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
self.key = key
self.suffix = suffix
}

public var id: UUID = UUID()

public let frets: [Int]
Expand All @@ -38,7 +38,7 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
public let midi: [Int]
public let key: Chords.Key
public let suffix: Chords.Suffix

static private let numberOfStrings = 6 - 1
static private let numberOfFrets = 5

Expand All @@ -57,7 +57,7 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
public func chordLayer(rect: CGRect, showFingers: Bool = true, chordName: Chords.Name = Chords.Name(), forPrint: Bool = false, mirror: Bool = false) -> CAShapeLayer {
return privateLayer(rect: rect, showFingers: showFingers, chordName: chordName, forScreen: !forPrint, mirror: mirror)
}

/// Now deprecated. Please see the chordLayer() function.
/// - Parameters:
/// - rect: The area for which the chord will be drawn to. This determines it's size. Chords have a set aspect ratio, and so the size of the chord will be based on the shortest side of the rect.
Expand Down Expand Up @@ -126,6 +126,8 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
private func stringsAndFretsLayer(fretConfig: LineConfig, stringConfig: LineConfig, origin: CGPoint, forScreen: Bool) -> CAShapeLayer {
let layer = CAShapeLayer()

let primaryColor = forScreen ? primaryColor.cgColor : SWIFTColor.black.cgColor

// Strings
let stringPath = CGMutablePath()

Expand All @@ -138,11 +140,7 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
let stringLayer = CAShapeLayer()
stringLayer.path = stringPath
stringLayer.lineWidth = stringConfig.spacing / 24
#if !os(macOS)
stringLayer.strokeColor = forScreen ? UIColor.label.cgColor : UIColor.black.cgColor
#else
stringLayer.strokeColor = forScreen ? NSColor.labelColor.cgColor : NSColor.black.cgColor
#endif
stringLayer.strokeColor = primaryColor
layer.addSublayer(stringLayer)

// Frets
Expand All @@ -161,21 +159,13 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
// Draw fret number
if baseFret != 1 {
let txtLayer = CAShapeLayer()
#if os(iOS)
let txtFont = UIFont.systemFont(ofSize: fretConfig.margin * 0.5)
#else
let txtFont = NSFont.systemFont(ofSize: fretConfig.margin * 0.5)
#endif
let txtFont = SWIFTFont.systemFont(ofSize: fretConfig.margin * 0.5)
let txtRect = CGRect(x: 0, y: 0, width: stringConfig.margin, height: fretConfig.spacing)
let transX = stringConfig.margin / 5 + origin.x
let transY = origin.y + (fretConfig.spacing / 2) + fretConfig.margin
let txtPath = "\(baseFret)".path(font: txtFont, rect: txtRect, position: CGPoint(x: transX, y: transY))
txtLayer.path = txtPath
#if os(iOS)
txtLayer.fillColor = forScreen ? UIColor.label.cgColor : UIColor.black.cgColor
#else
txtLayer.fillColor = forScreen ? NSColor.labelColor.cgColor : NSColor.black.cgColor
#endif
txtLayer.fillColor = primaryColor
fretLayer.addSublayer(txtLayer)
}

Expand All @@ -188,11 +178,7 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
fret.path = fretPath
fret.lineWidth = lineWidth
fret.lineCap = .square
#if os(iOS)
fret.strokeColor = forScreen ? UIColor.label.cgColor : UIColor.black.cgColor
#else
fret.strokeColor = forScreen ? NSColor.labelColor.cgColor : NSColor.black.cgColor
#endif
fret.strokeColor = primaryColor
fretLayer.addSublayer(fret)
}

Expand All @@ -202,6 +188,9 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
}

private func nameLayer(fretConfig: LineConfig, origin: CGPoint, center: CGFloat, forScreen: Bool, name: Chords.Name) -> CAShapeLayer {

let primaryColor = forScreen ? primaryColor.cgColor : SWIFTColor.black.cgColor

var displayKey: String {
switch name.key {
case .raw:
Expand All @@ -224,28 +213,22 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
return suffix.display.altSymbol
}
}
#if os(iOS)
let txtFont = UIFont.systemFont(ofSize: fretConfig.margin, weight: .medium)
#else
let txtFont = NSFont.systemFont(ofSize: fretConfig.margin, weight: .medium)
#endif

let txtFont = SWIFTFont.systemFont(ofSize: fretConfig.margin, weight: .medium)
let txtRect = CGRect(x: 0, y: 0, width: fretConfig.length, height: fretConfig.margin + origin.y)
let transY = (origin.y + fretConfig.margin) * 0.35
let txtPath = (displayKey + " " + displaySuffix).path(font: txtFont, rect: txtRect, position: CGPoint(x: center, y: transY))
let shape = CAShapeLayer()
shape.path = txtPath
#if os(iOS)
shape.fillColor = forScreen ? UIColor.label.cgColor : UIColor.black.cgColor
#else
shape.fillColor = forScreen ? NSColor.labelColor.cgColor : NSColor.black.cgColor
#endif
shape.fillColor = primaryColor
return shape
}

private func barreLayer(fretConfig: LineConfig, stringConfig: LineConfig, origin: CGPoint, showFingers: Bool, forScreen: Bool) -> CAShapeLayer {
let layer = CAShapeLayer()

let primaryColor = forScreen ? primaryColor.cgColor : SWIFTColor.black.cgColor
let backgroundColor = forScreen ? backgroundColor.cgColor : SWIFTColor.white.cgColor

for barre in barres {
let barrePath = CGMutablePath()

Expand Down Expand Up @@ -279,21 +262,13 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
barreLayer.path = barrePath
barreLayer.lineCap = .round
barreLayer.lineWidth = fretConfig.spacing * 0.65
#if os(iOS)
barreLayer.strokeColor = forScreen ? UIColor.label.cgColor : UIColor.black.cgColor
#else
barreLayer.strokeColor = forScreen ? NSColor.labelColor.cgColor : NSColor.black.cgColor
#endif
barreLayer.strokeColor = primaryColor

layer.addSublayer(barreLayer)

if showFingers {
let fingerLayer = CAShapeLayer()
#if os(iOS)
let txtFont = UIFont.systemFont(ofSize: stringConfig.margin, weight: .medium)
#else
let txtFont = NSFont.systemFont(ofSize: stringConfig.margin, weight: .medium)
#endif
let txtFont = SWIFTFont.systemFont(ofSize: stringConfig.margin, weight: .medium)
let txtRect = CGRect(x: 0, y: 0, width: stringConfig.spacing, height: fretConfig.spacing)
let transX = startingX + ((endingX - startingX) / 2)
let transY = y
Expand All @@ -302,11 +277,7 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
let txtPath = "\(fingers[fretIndex])".path(font: txtFont, rect: txtRect, position: CGPoint(x: transX, y: transY))
fingerLayer.path = txtPath
}
#if os(iOS)
fingerLayer.fillColor = forScreen ? UIColor.systemBackground.cgColor : UIColor.white.cgColor
#else
fingerLayer.fillColor = forScreen ? NSColor.windowBackgroundColor.cgColor : NSColor.white.cgColor
#endif
fingerLayer.fillColor = backgroundColor
layer.addSublayer(fingerLayer)
}
}
Expand All @@ -317,6 +288,9 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
private func dotsLayer(stringConfig: LineConfig, fretConfig: LineConfig, origin: CGPoint, showFingers: Bool, forScreen: Bool, rect: CGRect, mirror: Bool) -> CAShapeLayer {
let layer = CAShapeLayer()

let primaryColor = forScreen ? primaryColor.cgColor : SWIFTColor.black.cgColor
let backgroundColor = forScreen ? backgroundColor.cgColor : SWIFTColor.white.cgColor

for index in 0..<frets.count {
let fret = frets[index]

Expand All @@ -334,13 +308,9 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
let circleLayer = CAShapeLayer()
circleLayer.path = circle
circleLayer.lineWidth = fretConfig.spacing / 24
#if os(iOS)
circleLayer.strokeColor = forScreen ? UIColor.label.cgColor : UIColor.black.cgColor
circleLayer.fillColor = forScreen ? UIColor.systemBackground.cgColor : UIColor.white.cgColor
#else
circleLayer.strokeColor = forScreen ? NSColor.labelColor.cgColor : NSColor.black.cgColor
circleLayer.fillColor = forScreen ? NSColor.windowBackgroundColor.cgColor : NSColor.white.cgColor
#endif
circleLayer.strokeColor = primaryColor
circleLayer.fillColor = backgroundColor

layer.addSublayer(circleLayer)

continue
Expand All @@ -367,11 +337,7 @@ public struct ChordPosition: Codable, Identifiable, Equatable {
crossLayer.path = cross
crossLayer.lineWidth = fretConfig.spacing / 24

#if os(iOS)
crossLayer.strokeColor = forScreen ? UIColor.label.cgColor : UIColor.black.cgColor
#else
crossLayer.strokeColor = forScreen ? NSColor.labelColor.cgColor : NSColor.black.cgColor
#endif
crossLayer.strokeColor = primaryColor

layer.addSublayer(crossLayer)

Expand Down Expand Up @@ -402,28 +368,19 @@ public struct ChordPosition: Codable, Identifiable, Equatable {

let dotLayer = CAShapeLayer()
dotLayer.path = dotPath
#if os(iOS)
dotLayer.fillColor = forScreen ? UIColor.label.cgColor : UIColor.black.cgColor
#else
dotLayer.fillColor = forScreen ? NSColor.labelColor.cgColor : NSColor.black.cgColor
#endif
dotLayer.fillColor = primaryColor

layer.addSublayer(dotLayer)

if showFingers {
#if os(iOS)
let txtFont = UIFont.systemFont(ofSize: stringConfig.margin, weight: .medium)
#else
let txtFont = NSFont.systemFont(ofSize: stringConfig.margin, weight: .medium)
#endif
let txtFont = SWIFTFont.systemFont(ofSize: stringConfig.margin, weight: .medium)

let txtRect = CGRect(x: 0, y: 0, width: stringConfig.spacing, height: fretConfig.spacing)
let txtPath = "\(fingers[index])".path(font: txtFont, rect: txtRect, position: CGPoint(x: dotX, y: dotY))
let txtLayer = CAShapeLayer()
txtLayer.path = txtPath
#if os(iOS)
txtLayer.fillColor = forScreen ? UIColor.systemBackground.cgColor : UIColor.white.cgColor
#else
txtLayer.fillColor = forScreen ? NSColor.windowBackgroundColor.cgColor : NSColor.white.cgColor
#endif
txtLayer.fillColor = backgroundColor

layer.addSublayer(txtLayer)
}
}
Expand Down
42 changes: 19 additions & 23 deletions Sources/SwiftyChords/ImageFromLayer.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
//
// File.swift
//
//
//
// Created by Beau Nouvelle on 2021-01-23.
//

import Foundation

#if os(iOS)
#if os(macOS)
import AppKit
#else
import UIKit
public extension CALayer {
func image() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.frame.size, self.isOpaque, 0)

defer { UIGraphicsEndImageContext() }

guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
#endif

self.render(in: context)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
#else
import AppKit
public extension CALayer {
func image() -> NSImage? {
func image() -> SWIFTImage? {
#if os(macOS)
self.sublayerTransform = CATransform3DMakeScale(1.0, -1.0, 1.0)

let width = Int(bounds.width * self.contentsScale)
Expand All @@ -43,8 +30,17 @@ public extension CALayer {
}

return NSImage(cgImage: image, size: bounds.size)
}
}
#endif
#else
UIGraphicsBeginImageContextWithOptions(self.frame.size, self.isOpaque, 0)

defer { UIGraphicsEndImageContext() }

guard let context = UIGraphicsGetCurrentContext() else {
return nil
}

self.render(in: context)
return UIGraphicsGetImageFromCurrentImageContext()
#endif
}
}
Loading

0 comments on commit 38562c1

Please sign in to comment.