From e4fc708dbb57d81d0aab4e5807a413efa890d894 Mon Sep 17 00:00:00 2001 From: EyreFree Date: Fri, 26 Jul 2024 05:27:13 +0800 Subject: [PATCH] fix: tvOS & watchOS --- SwiftDraw.podspec.json | 16 +++++++--- SwiftDraw/Renderer.CoreGraphics.swift | 10 +++--- SwiftDraw/UIImage+Image.swift | 32 +++++++++++++++++-- .../TextOutputStream+StandardError.swift | 2 +- SwiftDrawTests/Parser.XML.ImageTests.swift | 4 +-- 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/SwiftDraw.podspec.json b/SwiftDraw.podspec.json index 4afb1e2..7c9de5b 100644 --- a/SwiftDraw.podspec.json +++ b/SwiftDraw.podspec.json @@ -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" } diff --git a/SwiftDraw/Renderer.CoreGraphics.swift b/SwiftDraw/Renderer.CoreGraphics.swift index f239ef7..96cb226 100644 --- a/SwiftDraw/Renderer.CoreGraphics.swift +++ b/SwiftDraw/Renderer.CoreGraphics.swift @@ -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 { @@ -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) diff --git a/SwiftDraw/UIImage+Image.swift b/SwiftDraw/UIImage+Image.swift index 4e701d3..0f8e1aa 100644 --- a/SwiftDraw/UIImage+Image.swift +++ b/SwiftDraw/UIImage+Image.swift @@ -31,6 +31,9 @@ #if canImport(UIKit) import UIKit +#if canImport(WatchKit) +import WatchKit +#endif public extension UIImage { @@ -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() @@ -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) @@ -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 { diff --git a/SwiftDraw/Utilities/TextOutputStream+StandardError.swift b/SwiftDraw/Utilities/TextOutputStream+StandardError.swift index 918bd9d..714730d 100644 --- a/SwiftDraw/Utilities/TextOutputStream+StandardError.swift +++ b/SwiftDraw/Utilities/TextOutputStream+StandardError.swift @@ -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)!) diff --git a/SwiftDrawTests/Parser.XML.ImageTests.swift b/SwiftDrawTests/Parser.XML.ImageTests.swift index 3ec4907..3fa8704 100644 --- a/SwiftDrawTests/Parser.XML.ImageTests.swift +++ b/SwiftDrawTests/Parser.XML.ImageTests.swift @@ -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)