Skip to content

Commit

Permalink
Update addField handling
Browse files Browse the repository at this point in the history
- remove tracking of vertical position, will add a better implementation later
- Use extension functions for shorter naming while leaving functions on ZplBuilder to be more intentional
- Add missing extension for MediaMode
  • Loading branch information
itsmattking committed Jul 3, 2024
1 parent 0498206 commit 9a30f6f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "info.mking.k2zpl"
version = "0.0.2"
version = "0.0.3"

repositories {
mavenCentral()
Expand All @@ -25,7 +25,7 @@ tasks.test {
mavenPublishing {
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
signAllPublications()
coordinates("info.mking.k2zpl", "k2zpl", "0.0.2")
coordinates("info.mking.k2zpl", "k2zpl", "0.0.3")
pom {
name.set("k2zpl")
description.set("Kotlin DSL for ZPL (Zebra Programming Language)")
Expand Down
28 changes: 9 additions & 19 deletions src/main/kotlin/ZPLBuilder.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class ZplBuilder {
private val commands = mutableListOf<ZplCommand>()
private var _dpiSetting: DpiSetting = DpiSetting.Unset // Default DPI value
private var currentY: Int = 0
private var defaultFont: DefaultFont = DefaultFont()

var dpiSetting: DpiSetting
Expand All @@ -15,12 +14,12 @@ class ZplBuilder {
_dpiSetting = value
}

fun command(command: ZplCommand) {
fun addCommand(command: ZplCommand) {
commands.add(command)
}

fun command(commandString: String) {
command(ZplCommand.CustomCommand(commandString))
fun addCommand(commandString: String) {
addCommand(ZplCommand.CustomCommand(commandString))
}

fun build(): String {
Expand Down Expand Up @@ -84,32 +83,23 @@ class ZplBuilder {
}

/**
* Adds a field with specified font and size, and updates the current vertical position.
* Adds a field with specified font and size.
*/
fun field(x: Int = 0, font: ZplFont, fontHeight: Int, fontWidth: Int, data: String, spacing: Int = 4.mm) {
fieldOrigin(x, currentY)
fun addField(x: Int, y: Int, font: ZplFont, fontHeight: Int, fontWidth: Int, data: String) {
fieldOrigin(x, y)
font(font, fontHeight, fontWidth)
fieldData(data)
fieldSeparator()
currentY += fontHeight + spacing
}

/**
* Adds a field with the default font, and updates the current vertical position.
* Adds a field with the default font.
*/
fun field(x: Int = 0, data: String, spacing: Int = 4.mm) {
fieldOrigin(x, currentY)
fun addField(x: Int = 0, y: Int = 0, data: String) {
fieldOrigin(x, y)
font(defaultFont.font, defaultFont.fontHeight, defaultFont.fontWidth)
fieldData(data)
fieldSeparator()
currentY += defaultFont.fontHeight + spacing
}

/**
* Sets the initial vertical position.
*/
fun setInitialVerticalPosition(y: Int) {
currentY = y
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/main/kotlin/ZPLExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,38 @@ fun ZplBuilder.startFormat() {
fun ZplBuilder.endFormat() {
command(ZplCommand.EndFormat)
}

/**
* Sets Media Mode
*/
fun ZplBuilder.mediaMode(zplMediaMode: ZplMediaMode, labelHandling: ZplPreprintedLabelHandling = ZplPreprintedLabelHandling.NORMAL) {
command(ZplCommand.MediaModeCommand(zplMediaMode, labelHandling))
}

/**
* Shortcut for addCommand
*/
fun ZplBuilder.command(command: ZplCommand) {
addCommand(command)
}

/**
* Shortcut for addCommand for string
*/
fun ZplBuilder.command(command: String) {
addCommand(command)
}

/**
* Shortcut for addField
*/
fun ZplBuilder.field(x: Int = 0, y: Int = 0, data: String) {
addField(x, y, data)
}

/**
* Shortcut for addField with font attributes
*/
fun ZplBuilder.field(x: Int, y: Int, font: ZplFont, fontHeight: Int, fontWidth: Int, data: String) {
addField(x, y, font, fontHeight, fontWidth, data)
}

0 comments on commit 9a30f6f

Please sign in to comment.