Giter Club home page Giter Club logo

swift-zpl's Introduction

Swift ZPL

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

Usage

import ZPLBuilder

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

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

Commands

Utilities

Commands

swift-zpl ZPL
BarcodeCode128 ^BC and ^FD
BarcodeDefault ^BY
BarcodeQR ^BQ and ^FD
ChangeAlphanumericDefaultFont ^CF
ChangeInternationalEncoding ^CI
Field ^FO and ^FS
FieldBlock ^FB
FieldData ^FD
FieldHexadecimal N/A
FieldHexadecimalIndicator ^FH
FieldOrientation ^FW
FieldOrigin ^FO
FieldParameter ^FP
FieldReversePrint ^FR
FieldSeparator ^FS
FieldTypeset ^FT
FontIdentifier ^CW
GraphicBox ^GB
GraphicCircle ^GC
GraphicDiagonalLine ^GD
GraphicEllipse ^GE
GraphicField ^GF
GraphicSymbol ^GS and ^FD
HostDirectoryList ^HW
Hyphen N/A
LabelHome ^LH
LabelLength ^LL
LabelReversePrint ^LR
LabelShift ^LS
PrintDirectoryLabel ^WD
PrintWidth ^PW
ScalableBitmappedFont ^A
SelectEncodingTable ^SE
SetDarkness ~SD
TextBlock ^TB
UseFontNameToCallFont ^A@

Examples

Boxes

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

Barcodes

Code 128

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

boxes

QR code

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

boxes

Text & Image

let image: UIImage = ...
let labelWidth = 4 * 203
let labelHeight = 6 * 203

ZPLImageEncoder.default.isCompressed = true

let zpl = ZPL {
    LabelHome(x: 0, y: 0)
    LabelReversePrint(enabled: true)
    
    let imageWidth = labelWidth / 2
    let imageSize = ZPLGeometryUtils.size(aspectRatio: image.size, fillWidth: imageWidth)
    
    Field(x: 0, y: 0) {
        GraphicField(image: image, size: imageSize)
    }
    Field(x: imageWidth, y: 0) {
        GraphicField(image: image, size: imageSize)
    }
    Field(x: imageWidth, y: 0) {
        GraphicBox(
            width: imageWidth,
            height: Int(imageSize.height),
            lineWidth: Int(min(imageSize.width, imageSize.height))
        )
    }
}

image

let image: UIImage = ...
let labelWidth = 72 * 8
let labelHeight = 72 * 9
let settings = ZPL {
    LabelHome(x: 0, y: 0)
    LabelReversePrint(enabled: true)
    ChangeAlphanumericDefaultFont(font: "A", height: 35)
}
let body = ZPL {
    Field(x: labelWidth / 2, y: 0) {
        GraphicBox(width: labelWidth / 2, height: labelHeight, lineWidth: labelWidth / 2)
    }
    
    let imageSize = ZPLGeometryUtils.size(aspectRatio: image.size, fillWidth: labelWidth)
    
    Field(x: 0, y: 0) {
        GraphicField(image: image, size: imageSize)
    }
    
    Field(x: 0, y: Int(imageSize.height)) {
        FieldBlock(width: labelWidth, lines: 1, justification: .center)
        FieldData(text: "Swift")
        Hyphen.return
    }
}
let zpl = ZPL {
    settings
    body
}

text&image

More Commands

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

You can create a new command with just a few lines of code:

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

ZPL {
    NewCommand()
}

or create a pull request to add a new command or improve/fix existing ones.

Preview

Labelary Online ZPL Viewer

swift-zpl's People

Contributors

scchn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

linkjoinagri

swift-zpl's Issues

Support Linux / Non-Apple platforms

First of all: thanks for creating this package!

A few commands (mainly around GraphicField/image conversion) rely on CoreGraphics, which is only available on Apple platforms.

It would be great to only conditionally depend on these, and maybe add "raw" support for images on other platforms (without automatic conversions).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.