TextPrintable.kt
3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.printer.sdk.serial
data class TextPrintable private constructor(val text: String,
val fontSize: Byte,
val alignment: Byte,
val newLinesAfter: Int,
val bold: Byte,
val underlined: Byte,
val characterCode: Byte,
val lineSpacing: Byte,
val customConverter: Converter?) : Printable {
override fun getPrintableByteArray(printer: Printer): List<ByteArray> {
val operations = mutableListOf(
printer.justificationCommand.plus(alignment),
printer.fontSizeCommand.plus(fontSize),
printer.emphasizedModeCommand.plus(bold),
printer.underlineModeCommand.plus(underlined),
printer.characterCodeCommand.plus(characterCode),
printer.lineSpacingCommand.plus(lineSpacing)
)
customConverter?.let {
operations.add(it.toByteArray(text))
} ?: run {
operations.add(printer.converter.toByteArray(text))
}
if (newLinesAfter > 0) {
operations.add(printer.feedLineCommand.plus(newLinesAfter.toByte()))
}
return operations
}
class Builder {
private var text = ""
private var fontSize = DefaultPrinter.FONT_SIZE_NORMAL
private var alignment: Byte = DefaultPrinter.ALIGNMENT_LEFT
private var newLinesAfter = 0
private var bold: Byte = DefaultPrinter.EMPHASIZED_MODE_NORMAL
private var underlined: Byte = DefaultPrinter.UNDERLINED_MODE_OFF
private var characterCode: Byte = DefaultPrinter.CHARCODE_PC437
private var lineSpacing: Byte = DefaultPrinter.LINE_SPACING_30
private var customConverter: Converter? = null
fun setText(text: String): Builder {
this.text = text
return this
}
fun setFontSize(fontSize: Byte): Builder {
this.fontSize = fontSize
return this
}
fun setAlignment(alignment: Byte): Builder {
this.alignment = alignment
return this
}
fun setNewLinesAfter(lines: Int): Builder {
this.newLinesAfter = lines
return this
}
fun setEmphasizedMode(mode: Byte): Builder {
this.bold = mode
return this
}
fun setUnderlined(mode: Byte): Builder {
this.underlined = mode
return this
}
fun setCharacterCode(characterCode: Byte): Builder {
this.characterCode = characterCode
return this
}
fun setLineSpacing(lineSpacing: Byte): Builder {
this.lineSpacing = lineSpacing
return this
}
fun setCustomConverter(converter: Converter): Builder {
this.customConverter = converter
return this
}
fun build(): Printable {
return TextPrintable(text, fontSize, alignment, newLinesAfter, bold, underlined, characterCode, lineSpacing, customConverter)
}
}
}