Skip to content

Commit

Permalink
Merge pull request #48 from EyreFree/main
Browse files Browse the repository at this point in the history
fix: tvOS & watchOS
  • Loading branch information
swhitty authored Aug 30, 2024
2 parents 053e908 + e4fc708 commit 67d6bb3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
16 changes: 12 additions & 4 deletions SwiftDraw.podspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@
},
"source_files": "SwiftDraw/**/*.swift",
"ios": {
"exclude_files": "SwiftDraw/NSImage+Image.swift",
"frameworks": ["UIKit", "Foundation"]
"exclude_files": "SwiftDraw/NSImage+Image.swift",
"frameworks": ["UIKit", "Foundation"]
},
"osx": {
"exclude_files": "SwiftDraw/UIImage+Image.swift",
"frameworks": ["AppKit", "Foundation"]
"exclude_files": "SwiftDraw/UIImage+Image.swift",
"frameworks": ["AppKit", "Foundation"]
},
"tvos": {
"exclude_files": "SwiftDraw/NSImage+Image.swift",
"frameworks": ["UIKit", "Foundation"]
},
"watchos": {
"exclude_files": "SwiftDraw/NSImage+Image.swift",
"frameworks": ["UIKit", "WatchKit", "Foundation"]
},
"swift_version": "5.0"
}
10 changes: 5 additions & 5 deletions SwiftDraw/Renderer.CoreGraphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
#if canImport(CoreGraphics)
import Foundation
import CoreText
#if os(macOS)
import AppKit
#elseif os(iOS)
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif

struct CGTypes: RendererTypes {
Expand Down Expand Up @@ -295,9 +295,9 @@ struct CGProvider: RendererTypeProvider {
//TODO: replace with CG implementation
private extension CGImage {
static func from(data: Data) -> CGImage? {
#if os(iOS)
#if canImport(UIKit)
return UIImage(data: data)?.cgImage
#elseif os(macOS)
#elseif canImport(AppKit)
guard let image = NSImage(data: data) else { return nil }
var rect = NSRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
return image.cgImage(forProposedRect: &rect, context: nil, hints: nil)
Expand Down
32 changes: 29 additions & 3 deletions SwiftDraw/UIImage+Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

#if canImport(UIKit)
import UIKit
#if canImport(WatchKit)
import WatchKit
#endif

public extension UIImage {

Expand Down Expand Up @@ -72,6 +75,22 @@ public extension SVG {
return rasterize(with: size)
}

#if os(watchOS)
func rasterize(with size: CGSize? = nil, scale: CGFloat = 0, insets: UIEdgeInsets = .zero) -> UIImage {
let insets = Insets(top: insets.top, left: insets.left, bottom: insets.bottom, right: insets.right)
let (bounds, pixelsWide, pixelsHigh) = makeBounds(size: size, scale: 1, insets: insets)

let actualScale = scale <= 0 ? WKInterfaceDevice.current().screenScale : scale
UIGraphicsBeginImageContextWithOptions(CGSize(width: pixelsWide, height: pixelsHigh), false, actualScale)
defer { UIGraphicsEndImageContext() }

if let context = UIGraphicsGetCurrentContext() {
context.draw(self, in: bounds)
}

return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
}
#else
private func makeFormat() -> UIGraphicsImageRendererFormat {
guard #available(iOS 12.0, *) else {
let f = UIGraphicsImageRendererFormat.default()
Expand All @@ -87,13 +106,14 @@ public extension SVG {
let insets = Insets(top: insets.top, left: insets.left, bottom: insets.bottom, right: insets.right)
let (bounds, pixelsWide, pixelsHigh) = makeBounds(size: size, scale: 1, insets: insets)
let f = makeFormat()
f.scale = scale
f.scale = scale <= 0 ? UIScreen.main.scale : scale
f.opaque = false
let r = UIGraphicsImageRenderer(size: CGSize(width: pixelsWide, height: pixelsHigh), format: f)
return r.image{
$0.cgContext.draw(self, in: bounds)
}
}
#endif

func pngData(size: CGSize? = nil, scale: CGFloat = 0, insets: UIEdgeInsets = .zero) throws -> Data {
let image = rasterize(with: size, scale: scale, insets: insets)
Expand Down Expand Up @@ -125,8 +145,14 @@ extension SVG {
}

func makeBounds(size: CGSize?, scale: CGFloat, insets: Insets) -> (bounds: CGRect, pixelsWide: Int, pixelsHigh: Int) {
let scale = scale == 0 ? UIScreen.main.scale : scale
return Self.makeBounds(size: size, defaultSize: self.size, scale: scale, insets: insets)
let newScale: CGFloat = {
#if os(watchOS)
return scale <= 0 ? WKInterfaceDevice.current().screenScale : scale
#else
return scale <= 0 ? UIScreen.main.scale : scale
#endif
}()
return Self.makeBounds(size: size, defaultSize: self.size, scale: newScale, insets: insets)
}

private struct Error: LocalizedError {
Expand Down
2 changes: 1 addition & 1 deletion SwiftDraw/Utilities/TextOutputStream+StandardError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct StandardErrorStream: TextOutputStream {
fileprivate static var shared = StandardErrorStream()

func write(_ string: String) {
if #available(macOS 10.15.4, iOS 13.4, *) {
if #available(macOS 10.15.4, iOS 13.4, tvOS 13.4, watchOS 6.2, *) {
try! FileHandle.standardError.write(contentsOf: string.data(using: .utf8)!)
} else {
FileHandle.standardError.write(string.data(using: .utf8)!)
Expand Down
4 changes: 2 additions & 2 deletions SwiftDrawTests/Parser.XML.ImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import CoreGraphics

extension CGImage {
static func from(data: Data) -> CGImage? {
#if os(iOS)
#if canImport(UIKit)
return UIImage(data: data)?.cgImage
#elseif os(macOS)
#elseif canImport(AppKit)
guard let image = NSImage(data: data) else { return nil }
var rect = NSRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
return image.cgImage(forProposedRect: &rect, context: nil, hints: nil)
Expand Down

0 comments on commit 67d6bb3

Please sign in to comment.