Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#14] Update barcode helper #22

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sainsburys.k2zpl.command.barcode

import com.sainsburys.k2zpl.command.ZplCommand
import com.sainsburys.k2zpl.command.options.ZplFieldOrientation

internal interface BarCode : ZplCommand {
val orientation: ZplFieldOrientation
val height: Int
}
75 changes: 75 additions & 0 deletions src/main/kotlin/com/sainsburys/k2zpl/command/barcode/BarCode128.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.sainsburys.k2zpl.command.barcode

import com.sainsburys.k2zpl.builder.ZplBuilder
import com.sainsburys.k2zpl.builder.toZplYesNo
import com.sainsburys.k2zpl.command.ZplParameters
import com.sainsburys.k2zpl.command.fieldData
import com.sainsburys.k2zpl.command.fieldOrigin
import com.sainsburys.k2zpl.command.fieldSeparator
import com.sainsburys.k2zpl.command.options.ZplBarCode128Mode
import com.sainsburys.k2zpl.command.options.ZplFieldOrientation
import com.sainsburys.k2zpl.command.options.ZplYesNo
import com.sainsburys.k2zpl.command.zplParameters

internal data class BarCode128(
override val orientation: ZplFieldOrientation,
override val height: Int,
val line: ZplYesNo,
val lineAbove: ZplYesNo,
val checkDigit: ZplYesNo,
val mode: ZplBarCode128Mode
) : BarCode {

init {
require(height in 1..32000) { "Height must be between 1 and 32000" }
}

override val command = "^BC"

override val parameters: ZplParameters = zplParameters(
"o" to orientation.code,
"h" to height,
"l" to line,
"la" to lineAbove.toString(),
"c" to checkDigit.toString(),
"m" to mode.toString()
)
}

/**
* Creates a Code 128 barcode marker
* @param data data encoded in the barcode
* @param x horizontal position
* @param y vertical position
* @param orientation The orientation of the barcode
* @param height the height of the barcode
* @param interpretationLine print interpretation line
* @param lineAbove print interpretation line above code
* @param checkDigit whether to include a UCC check digit
* @param mode barcode mode
*/
fun ZplBuilder.barcode128(
data: String,
x: Int,
y: Int,
height: Int,
itsmattking marked this conversation as resolved.
Show resolved Hide resolved
orientation: ZplFieldOrientation = ZplFieldOrientation.NORMAL,
interpretationLine: Boolean = false,
lineAbove: Boolean = false,
checkDigit: Boolean = false,
mode: ZplBarCode128Mode = ZplBarCode128Mode.NONE
) {
fieldOrigin(x, y)
command(
BarCode128(
orientation = orientation,
height = height,
line = interpretationLine.toZplYesNo(),
lineAbove = lineAbove.toZplYesNo(),
checkDigit = checkDigit.toZplYesNo(),
mode = mode
)
)
fieldData(data)
fieldSeparator()
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
package com.sainsburys.k2zpl.command
package com.sainsburys.k2zpl.command.barcode

import com.sainsburys.k2zpl.builder.ZplBuilder
import com.sainsburys.k2zpl.builder.toZplYesNo
import com.sainsburys.k2zpl.command.options.ZplBarcodeType
import com.sainsburys.k2zpl.command.ZplParameters
import com.sainsburys.k2zpl.command.fieldData
import com.sainsburys.k2zpl.command.fieldOrigin
import com.sainsburys.k2zpl.command.fieldSeparator
import com.sainsburys.k2zpl.command.options.ZplFieldOrientation
import com.sainsburys.k2zpl.command.options.ZplYesNo
import com.sainsburys.k2zpl.command.zplParameters

internal data class BarCode(
val type: ZplBarcodeType,
val orientation: ZplFieldOrientation,
internal data class BarCode39(
override val height: Int,
override val orientation: ZplFieldOrientation,
val checkDigit: ZplYesNo,
val height: Int,
val line: Int,
val lineAbove: ZplYesNo
) : ZplCommand {
val line: ZplYesNo,
itsmattking marked this conversation as resolved.
Show resolved Hide resolved
val lineAbove: ZplYesNo,
) : BarCode {

init {
require(height in 1..32000) { "Height must be between 1 and 32000" }
require(line in 1..7) { "Line thickness must be between 1 and 7" }
}

override val command: CharSequence = "^B1"
override val command = "^B3"

override val parameters: ZplParameters = zplParameters(
"o" to orientation.code,
"c" to checkDigit.toString(),
"h" to height,
"l" to line,
"l" to line.toString(),
"la" to lineAbove.toString()
)
}
Expand All @@ -34,32 +38,29 @@ internal data class BarCode(
* @param data data encoded in the barcode
* @param x horizontal position
* @param y vertical position
* @param barcodeType Barcode type
* @param orientation The orientation of the barcode.
* @param checkDigit Whether to include a check digit.
* @param height The height of the barcode.
* @param lineThickness The line thickness of the barcode.
* @param lineAbove Whether to include a line above the barcode.
* @param orientation the orientation of the barcode.
* @param checkDigit whether to include a Mod-43 check digit
* @param height the height of the barcode.
* @param interpretationLine print interpretation line
* @param lineAbove print interpretation line above code
*/
itsmattking marked this conversation as resolved.
Show resolved Hide resolved
fun ZplBuilder.barcode(
fun ZplBuilder.barcode39(
data: String,
x: Int,
y: Int,
height: Int,
lineThickness: Int,
barcodeType: ZplBarcodeType = ZplBarcodeType.CODE_39,
orientation: ZplFieldOrientation = ZplFieldOrientation.NORMAL,
checkDigit: Boolean = false,
interpretationLine: Boolean = false,
lineAbove: Boolean = false
) {
fieldOrigin(x, y)
command(
BarCode(
type = barcodeType,
BarCode39(
orientation = orientation,
checkDigit = checkDigit.toZplYesNo(),
height = height,
line = lineThickness,
line = interpretationLine.toZplYesNo(),
lineAbove = lineAbove.toZplYesNo()
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sainsburys.k2zpl.command.options

enum class ZplBarCode128Mode(val value: String) {
NONE("N"),
UCC("U"),
AUTOMATIC("A"),
UCC_EAN("D");

override fun toString(): String {
return value
}
}

This file was deleted.

4 changes: 4 additions & 0 deletions src/test/kotlin/com/sainsburys/k2zpl/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.sainsburys.k2zpl

import com.sainsburys.k2zpl.command.ZplCommand
import io.kotest.data.row
import kotlin.enums.EnumEntries

fun ZplCommand.testBuildString() = buildString { build(this) }

inline fun <reified T : Enum<T>> EnumEntries<T>.toRows() = map(::row)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sainsburys.k2zpl.command
import com.sainsburys.k2zpl.command.options.ZplTextAlignment
import com.sainsburys.k2zpl.k2zpl
import com.sainsburys.k2zpl.testBuildString
import com.sainsburys.k2zpl.toRows
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
Expand Down Expand Up @@ -50,7 +51,7 @@ class FieldBlockTest : DescribeSpec({
}
}
it("uses alignment parameter correctly") {
ZplTextAlignment.entries.forEach {
table(headers("alignment"), ZplTextAlignment.entries.toRows()).forAll {
fieldBlock.copy(alignment = it).testBuildString() shouldBe "^FB10,1,10,$it,0"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sainsburys.k2zpl.command
import com.sainsburys.k2zpl.command.options.ZplJustification
import com.sainsburys.k2zpl.k2zpl
import com.sainsburys.k2zpl.testBuildString
import com.sainsburys.k2zpl.toRows
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
Expand All @@ -26,7 +27,7 @@ class FieldOriginTest : DescribeSpec({
fieldOrigin.testBuildString() shouldBe "^FO10,10,0"
}
it("uses alignment parameter correctly") {
ZplJustification.entries.forEach {
table(headers("justification"), ZplJustification.entries.toRows()).forAll {
fieldOrigin.copy(justification = it).testBuildString() shouldBe "^FO10,10,$it"
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/kotlin/com/sainsburys/k2zpl/command/FontTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.sainsburys.k2zpl.command.options.ZplFieldOrientation
import com.sainsburys.k2zpl.command.options.ZplFont
import com.sainsburys.k2zpl.k2zpl
import com.sainsburys.k2zpl.testBuildString
import com.sainsburys.k2zpl.toRows
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
Expand All @@ -23,12 +24,12 @@ class FontTest : DescribeSpec({
font.testBuildString() shouldBe "^AAN,30,20"
}
it("correctly uses font parameter") {
ZplFont.entries.forEach {
table(headers("font"), ZplFont.entries.toRows()).forAll {
font.copy(font = it).testBuildString() shouldBe "^A${it}N,30,20"
}
}
it("correctly uses orientation parameter") {
ZplFieldOrientation.entries.forEach {
table(headers("orientation"), ZplFieldOrientation.entries.toRows()).forAll {
font.copy(orientation = it).testBuildString() shouldBe "^AA${it},30,20"
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/test/kotlin/com/sainsburys/k2zpl/command/MediaModeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import com.sainsburys.k2zpl.command.options.ZplMediaMode
import com.sainsburys.k2zpl.command.options.ZplYesNo
import com.sainsburys.k2zpl.k2zpl
import com.sainsburys.k2zpl.testBuildString
import com.sainsburys.k2zpl.toRows
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.data.forAll
import io.kotest.data.headers
import io.kotest.data.table
import io.kotest.matchers.shouldBe

class MediaModeTest : DescribeSpec({
Expand All @@ -18,12 +22,12 @@ class MediaModeTest : DescribeSpec({
mediaMode.testBuildString() shouldBe "^MMC,N"
}
it("uses mediaMode parameter properly") {
ZplMediaMode.entries.forEach {
table(headers("mediaMode"), ZplMediaMode.entries.toRows()).forAll {
mediaMode.copy(mediaMode = it).testBuildString() shouldBe "^MM${it.value},N"
}
}
it("uses prePeelSelect parameter properly") {
ZplYesNo.entries.forEach {
table(headers("prePeelSelect"), ZplYesNo.entries.toRows()).forAll {
mediaMode.copy(prePeelSelect = it).testBuildString() shouldBe "^MMC,${it}"
}
}
Expand Down
Loading