Skip to content

Commit d34b616

Browse files
karwaKarthikRIyer
authored andcommitted
[bfix][feat][conf]Darwin/CG fixes (#55)
* Small fixes for cross-(Darwin)-platform View hosting: - Fixed availability annotations on QuartzRenderer, Color - Add a method to QuartzRenderer to make a CGImage - Improve fallback when converting Color -> CGColor * Make it easier to flip to the portable-Darwin subset of products * Simplified a BarChart method
1 parent f16a041 commit d34b616

File tree

4 files changed

+29
-32
lines changed

4 files changed

+29
-32
lines changed

Package.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ let platformProducts: [Product] = [
4545
.library(name: "AGGRenderer", targets: ["AGGRenderer"]),
4646
]
4747

48+
// Building for iOS/tvOS/watchOS:
49+
// Change the condition below to "!os(macOS)".
4850
#elseif os(macOS)
4951

5052
let platformTargets: [Target] = [
@@ -93,13 +95,7 @@ let platformProducts: [Product] = [
9395
.library(name: "QuartzRenderer", targets: ["QuartzRenderer"])
9496
]
9597

96-
#elseif os(iOS) || os(tvOS) || os(watchOS)
97-
98-
// Note: This isn't the correct way to do this, because
99-
// "#if os(...)" depends on the build OS, not the target OS.
100-
// But SwiftPM doesn't have platform-specific targets, so to make this work
101-
// on iOS/tvOS/watchOS, you have to comment out the AGG-related things on the macOS
102-
// configuration until it looks like this:
98+
#elseif os(iOS) || os(tvOS) || os(watchOS) || os(macOS)
10399

104100
let platformTargets: [Target] = [
105101
.target(name: "SwiftPlot"),

Sources/QuartzRenderer/QuartzRenderer.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import AppKit
1010
import UIKit
1111
#endif
1212

13-
@available(tvOS 13, watchOS 13, *)
13+
@available(tvOS 13.0, watchOS 6.0, *)
1414
public class QuartzRenderer: Renderer {
1515
static let colorSpace = CGColorSpaceCreateDeviceRGB()
1616
static let bitmapInfo = CGImageAlphaInfo.premultipliedFirst.rawValue
@@ -482,6 +482,10 @@ public class QuartzRenderer: Renderer {
482482
#endif
483483
}
484484
}
485+
486+
public func makeCGImage() -> CGImage? {
487+
context.makeImage()
488+
}
485489
}
486490

487491
// - Helpers
@@ -534,6 +538,7 @@ fileprivate extension NSImage {
534538
}
535539
}
536540
#elseif canImport(UIKit)
541+
@available(tvOS 13.0, watchOS 6.0, *)
537542
fileprivate extension UIImage {
538543
func writePng(to url: URL, options: Data.WritingOptions = .atomic) throws {
539544
guard let data = pngData() else { throw QuartzRenderer.WritePNGError.imageCouldNotBeRendered }

Sources/SwiftPlot/BarChart.swift

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,9 @@ public class BarGraph<T:LosslessStringConvertible,U:FloatConvertible>: Plot {
7373
color: Color = Color.lightBlue,
7474
hatchPattern: BarGraphSeriesOptions.Hatching = .none,
7575
graphOrientation: BarGraph.GraphOrientation = .vertical){
76-
var values = [Pair<T,U>]()
77-
for i in 0..<x.count {
78-
values.append(Pair<T,U>(x[i], y[i]))
79-
}
80-
let s = Series<T,U>(values: values,
81-
label: label,
82-
color: color,
83-
hatchPattern: hatchPattern)
84-
addSeries(s)
85-
self.graphOrientation = graphOrientation
76+
self.addSeries(values: zip(x, y).map { Pair($0.0, $0.1) },
77+
label: label, color: color, hatchPattern: hatchPattern,
78+
graphOrientation: graphOrientation)
8679
}
8780
}
8881

Sources/SwiftPlot/Color.swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ public struct Color {
3232

3333
#if canImport(CoreGraphics)
3434
import CoreGraphics
35+
36+
fileprivate let RGBColorSpace = CGColorSpaceCreateDeviceRGB()
37+
3538
public extension Color {
36-
@available(tvOS 13, watchOS 13, *)
39+
@available(tvOS 13.0, watchOS 6.0, *)
3740
var cgColor : CGColor {
38-
if #available(OSX 10.15, iOS 13, *) {
39-
return CGColor(srgbRed: CGFloat(r),
40-
green: CGFloat(g),
41-
blue: CGFloat(b),
42-
alpha: CGFloat(a))
43-
} else {
44-
#if !os(tvOS) && !os(watchOS) // Shouldn't be necessary, but it is.
45-
return CGColor(red: CGFloat(r),
46-
green: CGFloat(g),
47-
blue: CGFloat(b),
48-
alpha: CGFloat(a))
49-
#endif
50-
}
41+
if #available(OSX 10.15, iOS 13.0, *) {
42+
return CGColor(srgbRed: CGFloat(r),
43+
green: CGFloat(g),
44+
blue: CGFloat(b),
45+
alpha: CGFloat(a))
46+
} else {
47+
var tuple = (CGFloat(r), CGFloat(g), CGFloat(b), CGFloat(a))
48+
return withUnsafePointer(to: &tuple) { tupPtr in
49+
return tupPtr.withMemoryRebound(to: CGFloat.self, capacity: 4) { floatPtr in
50+
return CGColor(colorSpace: RGBColorSpace, components:floatPtr)!
51+
}
52+
}
53+
}
5154
}
5255
}
5356
#endif

0 commit comments

Comments
 (0)