From a25b8fcc247d0e231e1a976c35824d6f9a048053 Mon Sep 17 00:00:00 2001 From: scchn Date: Mon, 9 Sep 2024 11:44:16 +0800 Subject: [PATCH] Enable StrictConcurrency --- Package.swift | 4 ++-- .../ZPLBuilder/Commands/GraphicField.swift | 6 ++--- .../ZPLBuilder/Utils/ZPLGeometryUtils.swift | 15 ++++++++++++ .../Utils/ZPLImageEncoder/ZPLImage.swift | 6 ++--- .../ZPLImageEncoder/ZPLImageEncoder.swift | 24 ++++++++++++------- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Package.swift b/Package.swift index b5c1683..4823bdd 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.7 +// swift-tools-version: 5.9 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -9,7 +9,7 @@ let package = Package( .library(name: "ZPLBuilder", targets: ["ZPLBuilder"]), ], targets: [ - .target(name: "ZPLBuilder"), + .target(name: "ZPLBuilder", swiftSettings: [.enableExperimentalFeature("StrictConcurrency")]), .testTarget(name: "ZPLBuilderTests", dependencies: ["ZPLBuilder"]), ] ) diff --git a/Sources/ZPLBuilder/Commands/GraphicField.swift b/Sources/ZPLBuilder/Commands/GraphicField.swift index f2ee4c3..021e5df 100644 --- a/Sources/ZPLBuilder/Commands/GraphicField.swift +++ b/Sources/ZPLBuilder/Commands/GraphicField.swift @@ -27,7 +27,7 @@ public struct GraphicField: ZPLCommandConvertible { self.encoder = encoder } - public init(imageReader: ZPLImageReader, encoder: ZPLImageEncoder = .default) { + public init(imageReader: ZPLImageReader, encoder: ZPLImageEncoder = .init()) { self.encoder = encoder self.image = imageReader } @@ -37,7 +37,7 @@ public struct GraphicField: ZPLCommandConvertible { import AppKit extension GraphicField { - public init(image: NSImage, size: CGSize? = nil, encoder: ZPLImageEncoder = .default) { + public init(image: NSImage, size: CGSize? = nil, encoder: ZPLImageEncoder = .init()) { if let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil), let image = ZPLCGImageReader(cgImage: cgImage, targetSize: size) { @@ -51,7 +51,7 @@ extension GraphicField { import UIKit extension GraphicField { - public init(image: UIImage, size: CGSize? = nil, encoder: ZPLImageEncoder = .default) { + public init(image: UIImage, size: CGSize? = nil, encoder: ZPLImageEncoder = .init()) { if let cgImage = image.cgImage, let image = ZPLCGImageReader(cgImage: cgImage, targetSize: size) { diff --git a/Sources/ZPLBuilder/Utils/ZPLGeometryUtils.swift b/Sources/ZPLBuilder/Utils/ZPLGeometryUtils.swift index ed362f7..835cf9b 100644 --- a/Sources/ZPLBuilder/Utils/ZPLGeometryUtils.swift +++ b/Sources/ZPLBuilder/Utils/ZPLGeometryUtils.swift @@ -48,3 +48,18 @@ private func makeRect(aspectRatio: CGSize, boundingRect: CGRect) -> CGRect { return .init(origin: origin, size: size) } + +import UIKit + +class A: UIViewController { + override func viewDidLoad() { + super.viewDidLoad() + + let image = UIImage() + let encoder = ZPLImageEncoder(isCompressed: true) + + _ = ZPL { + GraphicField(image: image, encoder: encoder) + } + } +} diff --git a/Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImage.swift b/Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImage.swift index 861ae79..2e5b5ca 100644 --- a/Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImage.swift +++ b/Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImage.swift @@ -1,13 +1,13 @@ // -// File.swift -// +// ZPLImage.swift +// // // Created by chen on 2024/5/4. // import Foundation -public struct ZPLImage { +public struct ZPLImage: Sendable { public var bytesPerRow: Int public var totalBytes: Int public var data: String diff --git a/Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImageEncoder.swift b/Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImageEncoder.swift index 3c14ad1..7ae0969 100644 --- a/Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImageEncoder.swift +++ b/Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImageEncoder.swift @@ -7,18 +7,26 @@ import Foundation -public class ZPLImageEncoder { - /// The default encoder of ``GraphicField``. - public static let `default` = ZPLImageEncoder() - +/// Encodes images into ZPL format. +/// Supports both compressed and uncompressed encoding modes. +public final class ZPLImageEncoder: Sendable { private static let componentValueSize = 8 - public var isCompressed: Bool = false - public var whiteThreshold = 383 + /// If true, uses compression for encoding; if false, generates uncompressed data. + public let isCompressed: Bool - public init() { - + /// Threshold for determining black/white pixels. + public let whiteThreshold: Int + + /// Initializes the encoder. + /// - Parameters: + /// - isCompressed: Use compression mode. Default: false. + /// - whiteThreshold: Black/white threshold. Default: 383 (50% gray). + public init(isCompressed: Bool = false, whiteThreshold: Int = 383) { + self.isCompressed = isCompressed + self.whiteThreshold = whiteThreshold } + public func encode(imageReader: ZPLImageReader) -> ZPLImage? { guard let data = isCompressed ? makeCompressedData(imageReader: imageReader) : makeData(imageReader: imageReader),