Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
scchn committed Nov 11, 2023
0 parents commit daaf144
Show file tree
Hide file tree
Showing 56 changed files with 1,995 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Binary file added Images/boxes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/code128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/qr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2023 scchn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

15 changes: 15 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "swift-zpl",
products: [
.library(name: "ZPLBuilder", targets: ["ZPLBuilder"]),
],
targets: [
.target(name: "ZPLBuilder"),
.testTarget(name: "ZPLBuilderTests", dependencies: ["ZPLBuilder"]),
]
)
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Swift ZPL

**Swift ZPL** provides a declarative interface to write ZPL faster, easier and safer.

```swift
ZPL {
LabelHome(x: 0, y: 0)
Field(x: 50, y: 50) {
FieldData(text: "swift-zpl")
}
}
```

# Usage

```swift
import ZPLBuilder

let zpl = ZPL {
LabelHome(x: 0, y: 0)
ChangeAlphanumericDefaultFont(font: "A", height: 50)
Field(x: 50, y: 50) {
FieldData(text: "hello")
}
}

print(zpl.string) // ^XA^LH0,0^CFA,50,0^FO50,50^FDhello^FS^XZ
```

[See All Commands](https://github.com/scchn/swift-zpl/tree/main/Sources/ZPLBuilder/Commands)

# Examples

## Boxes

```swift
ZPL {
LabelHome(x: 0, y: 0)

let width = 50
let spacing = 30

for row in 0..<3 {
for col in 0..<3 {
let x = width * col + spacing * (col + 1)
let y = width * row + spacing * (row + 1)
let lineWidth = col == row ? width / 2 : 1

Field(x: x, y: y) {
GraphicBox(width: width, height: width, lineWidth: lineWidth)
}
}
}
}
```

![boxes](https://github.com/scchn/swift-zpl/blob/main/Images/boxes.png)

## Barcodes

### Code 128:

```swift
ZPL {
LabelHome(x: 0, y: 0)
Field(x: 50, y: 50) {
BarcodeCode128(data: "hello30678", height: 100, interpretation: .bottom)
}
}
```

![boxes](https://github.com/scchn/swift-zpl/blob/main/Images/code128.png)

### QR code:

```swift
ZPL {
LabelHome(x: 0, y: 0)
Field(x: 50, y: 50) {
BarcodeQR(data: "hello", size: 10)
}
}
```

![boxes](https://github.com/scchn/swift-zpl/blob/main/Images/qr.png)

# More Commands

This package currently only contains a few commands, if you can't find what you need:

**Write a new command:**

```swift
struct NewCommand: ZPLCommandConvertible {
var command: String {
// command...
}
}

ZPL {
NewCommand()
}
```

**Open a pull request:**

Open a pull request to add a new command or improve/fix existing ones.
53 changes: 53 additions & 0 deletions Sources/ZPLBuilder/Commands/BarcodeCode128.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// BarcodeCode128.swift
//
//
// Created by chen on 2023/11/11.
//

import Foundation

/// The ^BC command creates the Code 128 barcode, a high-density, variable length, continuous, alphanumeric symbology.
/// It was designed for complexly encoded product identification.
///
/// **Code 128 Barcode (Subsets A, B, and C)**
///
/// Code 128 has three subsets of characters.
/// There are 106 encoded printing characters in each set, and each character can have up to three different meanings,
/// depending on the character subset being used. Each Code 128 character consists of six elements: three bars and three spaces.
/// * ^BC supports a fixed print ratio.
/// * Field data (^FD) is limited to the width (or length, if rotated) of the label.
public struct BarcodeCode128: ZPLCommandConvertible {
/// Field data
public var data: String
/// Orientation
public var orientation: Orientation
/// Barcode height (in dots)
///
/// Values: 1 to 32000
///
/// Default: value set by ^BY
public var height: Int
/// Interpretation line
///
/// The interpretation line can be printed in any font by placing the font command before the barcode command.
public var interpretation: BarcodeInterpretation
public var command: String {
"^BC\(orientation.rawValue),\(height),\(interpretation.visibility),\(interpretation.top),N,N" +
"^FD\(data)"
}

/// Barcode 128.
///
/// - Parameters:
/// - data: Field data.
/// - orientation: Orientation.
/// - height: Barcode height (in dots). 1 to 32000.
/// - interpretation: Interpretation line.
public init(data: String, orientation: Orientation = .normal, height: Int, interpretation: BarcodeInterpretation) {
self.data = data
self.orientation = orientation
self.height = height
self.interpretation = interpretation
}
}
49 changes: 49 additions & 0 deletions Sources/ZPLBuilder/Commands/BarcodeDefault.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// BarcodeDefault.swift
//
//
// Created by chen on 2023/11/11.
//

import Foundation

/// ^BY. 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).
/// It can be used as often as necessary within a label format.
///
/// **Bar Code Field Default**
///
/// Comments: Once a ^BY command is entered into a label format, it stays in effect until another ^BY command is encountered.
public struct BarcodeDefault: ZPLCommandConvertible {
/// Module width (in dots).
///
/// Values: 1 to 10
///
/// Initial Value at Power Up: 2
public var moduleWidth: Int = 2
/// Wide bar to narrow bar width ratio.
///
/// Values: 2.0 to 3.0, in 0.1 increments
///
/// This parameter has no effect on fixed-ratio bar codes. Default: 3.0
public var ratio: Int = 3
/// Bar code height (in dots).
///
/// Initial Value at Power Up: 10
public var height: Int = 10
public var command: String {
"^BY\(moduleWidth),\(ratio),\(height)"
}

/// Barcode field default.
///
/// - Parameters:
/// - moduleWidth: Module width (in dots). 1 to 10
/// - ratio: Wide bar to narrow bar width ratio. 2.0 to 3.0, in 0.1 increments.
/// - height: Bar code height (in dots).
public init(moduleWidth: Int, ratio: Int, height: Int) {
self.moduleWidth = moduleWidth
self.ratio = ratio
self.height = height
}
}
52 changes: 52 additions & 0 deletions Sources/ZPLBuilder/Commands/BarcodeQR.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// BarcodeQR.swift
//
//
// Created by chen on 2023/11/11.
//

import Foundation

/// The ^BQ command produces a matrix symbology consisting of an array of nominally square modules arranged in an overall square pattern.
/// A unique pattern at three of the symbol’s four corners assists in determining bar code size, position, and inclination.
///
/// **QR Code Bar Code**
///
/// A wide range of symbol sizes is possible, along with four levels of error correction.
/// User-specified module dimensions provide a wide variety of symbol production techniques.
///
/// QR Code Model 1 is the original specification, while QR Code Model 2 is an enhanced form of the symbology.
/// Model 2 provides additional features and can be automatically differentiated from Model 1.
///
/// Model 2 is the recommended model and should normally be used.
///
/// This bar code is printed using field data specified in a subsequent ^FD string.
///
/// Encodable character sets include numeric data, alphanumeric data, 8-bit byte data, and Kanji characters.
public struct BarcodeQR: ZPLCommandConvertible {
/// Field data
public var data: String
/// Magnification factor
///
/// Values: 1 to 10
///
/// Default:
/// * 1 on 150 dpi printers
/// * 2 on 200 dpi printers
/// * 3 on 300 dpi printers
/// * 6 on 600 dpi printers
public var magnificationFactor: Int
public var command: String {
"^BQN,2,\(magnificationFactor)^FDMM,A\(data)"
}

/// QR code.
///
/// - Parameters:
/// - data: Field data.
/// - size: Magnification factor. 1 to 10.
public init(data: String, size: Int) {
self.data = data
self.magnificationFactor = size
}
}
45 changes: 45 additions & 0 deletions Sources/ZPLBuilder/Commands/ChangeAlphanumericDefaultFont.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// ChangeAlphanumericDefaultFont.swift
//
//
// Created by chen on 2023/11/12.
//

import Foundation

/// The ^CF command sets the default font used in your printer. You can use the ^CF command to simplify your programs.
///
/// **Change Alphanumeric Default Font**
public struct ChangeAlphanumericDefaultFont: ZPLCommandConvertible {
/// Specified default font
///
/// Values: A through Z and 0 to 9
///
/// Initial Value at Power Up: A
public var font: Character
/// Individual character width (in dots)
///
/// Values: 0 to 32000
/// Initial Value at Power Up: 9
public var height: Int
/// individual character width (in dots)
///
/// Values: 0 to 32000
/// Initial Value at Power Up: 5 or last permanent saved value
public var width: Int
public var command: String {
"^CF\(font),\(height),\(width)"
}

/// Change Alphanumeric Default Font.
///
/// - Parameters:
/// - font: Specified default font. A through Z and 0 to 9.
/// - height: Individual character width (in dots). 0 to 32000.
/// - width: individual character width (in dots). 0 to 32000.
public init(font: Character, height: Int = 0, width: Int = 0) {
self.font = font
self.width = width
self.height = height
}
}
37 changes: 37 additions & 0 deletions Sources/ZPLBuilder/Commands/ChangeInternationalEncoding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ChangeInternationalEncoding.swift
//
//
// Created by chen on 2023/11/11.
//

import Foundation

/// The ^CI command enables you to call up the international character set you want to use for printing. You can mix character sets on a label.
///
/// **Change International Font/Encoding**
///
/// Zebra printers can print fonts using international character sets: U.S.A.1, U.S.A.2, UK, Holland, Denmark/ Norway, Sweden/Finland, Germany, France 1, France 2, Italy, Spain, and several other sets, including the Unicode character set.
///
/// A character within a font can be remapped to a different numerical position.
///
/// In x.14 version of firmware and later, this command allows character remapping when parameter a = 0-13.
public struct ChangeInternationalEncoding: ZPLCommandConvertible {
/// Desired character set
public var characterSet: UInt8
public var remapping: [(source: UInt8, destination: UInt8)]
public var command: String {
"^CI\(characterSet)" +
remapping.map { ",\($0),\($1)" }.joined()
}

/// Change International Font/Encoding
///
/// - Parameters:
/// - characterSet: Desired character set
/// - remapping: Remapping sources and destinations.
public init(characterSet: UInt8, remapping: (source: UInt8, destination: UInt8)...) {
self.characterSet = characterSet
self.remapping = remapping
}
}
Loading

0 comments on commit daaf144

Please sign in to comment.