Skip to content

Commit

Permalink
Add workflow and compilation conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
scchn authored Jan 9, 2024
1 parent 6c7c282 commit f867b78
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 25 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This workflow will build a Swift project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-swift

name: Swift

on: [push]

jobs:
build:
name: Swift on ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.9
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
1 change: 0 additions & 1 deletion Sources/ZPLBuilder/Commands/BarcodeDefault.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import RegexBuilder

/// The ^BY command is used to change the default values for the module width (in dots),
/// the wide bar to narrow bar width ratio and the bar code height (in dots).
Expand Down
4 changes: 2 additions & 2 deletions Sources/ZPLBuilder/Commands/FieldOrientation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public struct FieldOrientation: ZPLCommandConvertible {
public var justification: FieldJustification?
public var command: String {
if let justification {
"^FW\(orientation.rawValue),\(justification.rawValue)"
return "^FW\(orientation.rawValue),\(justification.rawValue)"
} else {
"^FW\(orientation.rawValue)"
return "^FW\(orientation.rawValue)"
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/ZPLBuilder/Commands/FieldOrigin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public struct FieldOrigin: ZPLCommandConvertible {
public var justification: FieldJustification?
public var command: String {
if let justification {
"^FO\(x),\(y),\(justification.rawValue)"
return "^FO\(x),\(y),\(justification.rawValue)"
} else {
"^FO\(x),\(y)"
return "^FO\(x),\(y)"
}
}
}
4 changes: 2 additions & 2 deletions Sources/ZPLBuilder/Commands/FieldTypeset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public struct FieldTypeset: ZPLCommandConvertible {
public var justification: FieldJustification?
public var command: String {
if let justification {
"^FT\(x),\(y),\(justification.rawValue)"
return "^FT\(x),\(y),\(justification.rawValue)"
} else {
"^FT\(x),\(y)"
return "^FT\(x),\(y)"
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/ZPLBuilder/Commands/GraphicField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by chen on 2023/11/11.
//

#if canImport(CoreGraphics)
import CoreGraphics

/// The ^GF command allows you to download graphic field data directly into the printer’s bitmap storage area.
Expand Down Expand Up @@ -36,6 +37,7 @@ public struct GraphicField: ZPLCommandConvertible {
self.size = size
}
}
#endif

#if canImport(AppKit)
import AppKit
Expand Down
9 changes: 7 additions & 2 deletions Sources/ZPLBuilder/Utils/ZPLGeometryUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

import Foundation
import AVFoundation.AVGeometry

public enum ZPLGeometryUtils {
public static func size(aspectRatio: CGSize, fillWidth width: Int) -> CGSize {
Expand All @@ -18,7 +17,12 @@ public enum ZPLGeometryUtils {
let width = Int(Double(height) * Double(aspectRatio.width) / Double(aspectRatio.height))
return .init(width: width, height: height)
}

}

#if canImport(AVFoundation)
import AVFoundation.AVGeometry

extension ZPLGeometryUtils {
public static func rect(aspectRatio: CGSize, insideRect boundingRect: CGRect) -> CGRect {
AVMakeRect(aspectRatio: aspectRatio, insideRect: boundingRect)
}
Expand All @@ -32,3 +36,4 @@ public enum ZPLGeometryUtils {
rect(aspectRatio: aspectRatio, fitBoundingSize: boundingSize).size
}
}
#endif
26 changes: 17 additions & 9 deletions Sources/ZPLBuilder/Utils/ZPLImageEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by chen on 2023/11/18.
//

#if canImport(CoreGraphics)
import Foundation
import CoreGraphics
import OSLog
Expand Down Expand Up @@ -32,15 +33,19 @@ public class ZPLImageEncoder {
}

public func encode(cgImage: CGImage, targetSize: CGSize?) -> ZPLImage? {
let width = if let width = targetSize?.width {
Int(width)
let width: Int
let height: Int

if let targetWidth = targetSize?.width {
width = Int(targetWidth)
} else {
cgImage.width
width = cgImage.width
}
let height = if let height = targetSize?.height {
Int(height)

if let targetHeight = targetSize?.height {
height = Int(targetHeight)
} else {
cgImage.height
height = cgImage.height
}

guard width > 0, height > 0, cgImage.width > 0, cgImage.height > 0 else {
Expand Down Expand Up @@ -110,10 +115,12 @@ public class ZPLImageEncoder {
componentValue <<= Self.componentValueSize - pos
}

let components = if componentValue <= 15 {
"0" + String(componentValue, radix: 16, uppercase: true)
let components: String

if componentValue <= 15 {
components = "0" + String(componentValue, radix: 16, uppercase: true)
} else {
String(componentValue, radix: 16, uppercase: true)
components = String(componentValue, radix: 16, uppercase: true)
}

block(x, y, components, x == width - 1)
Expand Down Expand Up @@ -249,3 +256,4 @@ private let encodingTable: [Int: Character] = [
380: "y",
400: "z",
]
#endif
12 changes: 6 additions & 6 deletions Sources/ZPLBuilder/ZPLBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import Foundation
@resultBuilder
public struct ZPLBuilder {
public static func buildBlock(_ components: ZPLComponent...) -> ZPLComponent {
ZPL(commands: components.flatMap(\.zpl.commands))
return ZPL(commands: components.flatMap(\.zpl.commands))
}

public static func buildArray(_ components: [ZPLComponent]) -> ZPLComponent {
ZPL(commands: components.flatMap(\.zpl.commands))
return ZPL(commands: components.flatMap(\.zpl.commands))
}

public static func buildEither(first component: ZPLComponent) -> ZPLComponent {
component
return component
}

public static func buildEither(second component: ZPLComponent) -> ZPLComponent {
component
return component
}

public static func buildOptional(_ component: ZPLComponent?) -> ZPLComponent {
if let component {
component
return component
} else {
ZPL(commands: [])
return ZPL(commands: [])
}
}
}
2 changes: 2 additions & 0 deletions Sources/ZPLBuilder/_Extensions/CGImage+Resize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by chen on 2023/11/10.
//

#if canImport(CoreGraphics)
import CoreGraphics

extension CGImage {
Expand All @@ -28,3 +29,4 @@ extension CGImage {
return ctx.makeImage()
}
}
#endif
2 changes: 2 additions & 0 deletions Tests/ZPLBuilderTests/CGImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by chen on 2023/11/12.
//

#if canImport(CoreGraphics)
import XCTest
@testable import ZPLBuilder

Expand Down Expand Up @@ -37,3 +38,4 @@ func makeCGImage(width: Int, height: Int) -> CGImage {
.cgImage!
#endif
}
#endif
2 changes: 2 additions & 0 deletions Tests/ZPLBuilderTests/ZPLGeometryUtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ZPLGeometryUtilsTests: XCTestCase {
XCTAssertEqual(size4, CGSize(width: 20, height: labelHeight2))
}

#if canImport(AVFoundation)
func test_fit_size() {
let boundingSize = CGSize(width: 80, height: 80)

Expand All @@ -62,4 +63,5 @@ class ZPLGeometryUtilsTests: XCTestCase {
let size2 = ZPLGeometryUtils.size(aspectRatio: imageAspectRatio2, fitBoundingSize: boundingSize)
XCTAssertEqual(size2, CGSize(width: 80, height: 40))
}
#endif
}

0 comments on commit f867b78

Please sign in to comment.