Skip to content

Commit

Permalink
Enable StrictConcurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
scchn committed Sep 9, 2024
1 parent 35315d5 commit a25b8fc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"]),
]
)
6 changes: 3 additions & 3 deletions Sources/ZPLBuilder/Commands/GraphicField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down
15 changes: 15 additions & 0 deletions Sources/ZPLBuilder/Utils/ZPLGeometryUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
6 changes: 3 additions & 3 deletions Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImage.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 16 additions & 8 deletions Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImageEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit a25b8fc

Please sign in to comment.