From 9a30f6f43d3cdf2ba333a8d5ed2b3180e50dbb26 Mon Sep 17 00:00:00 2001 From: Matt King Date: Wed, 3 Jul 2024 10:11:03 +0100 Subject: [PATCH] Update addField handling - 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 --- build.gradle.kts | 4 ++-- src/main/kotlin/ZPLBuilder.kt | 28 ++++++++----------------- src/main/kotlin/ZPLExtensions.kt | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 2c09a81..4cd2f2a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,7 +8,7 @@ plugins { } group = "info.mking.k2zpl" -version = "0.0.2" +version = "0.0.3" repositories { mavenCentral() @@ -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)") diff --git a/src/main/kotlin/ZPLBuilder.kt b/src/main/kotlin/ZPLBuilder.kt index 96b361b..74c85a7 100644 --- a/src/main/kotlin/ZPLBuilder.kt +++ b/src/main/kotlin/ZPLBuilder.kt @@ -1,7 +1,6 @@ class ZplBuilder { private val commands = mutableListOf() private var _dpiSetting: DpiSetting = DpiSetting.Unset // Default DPI value - private var currentY: Int = 0 private var defaultFont: DefaultFont = DefaultFont() var dpiSetting: DpiSetting @@ -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 { @@ -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 } } diff --git a/src/main/kotlin/ZPLExtensions.kt b/src/main/kotlin/ZPLExtensions.kt index fba1272..652e400 100644 --- a/src/main/kotlin/ZPLExtensions.kt +++ b/src/main/kotlin/ZPLExtensions.kt @@ -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) +} \ No newline at end of file