-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_star.go
executable file
·261 lines (216 loc) · 5 KB
/
cmd_star.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package thermalize
import (
"image"
"io"
)
// NewStar returns the star set of printer commands for the given configuration.
//
// This function creates a new star sequence command set for printing images and text.
//
// Parameters:
// - cpl: characters per line.
// - ppl: pixels per line.
// - w: the writer to which the commands will be sent.
// - opts: a variadic list of options to customize the behavior of the command set.
//
// Options:
// You can customize various aspects of the postscript command set using the following options:
// - WithBarCodeFunc(barCodeFunc): sets a custom function for generating barcodes.
// - WithQRCodeFunc(qrCodeFunc): sets a custom function for generating QR codes.
//
// Example Usage:
//
// cmd := NewStar(48, 576, writer)
//
// In this example, a new star sequence command set is created with 48 characters per line,
// 576 pixels per line.
func NewStar(cpl, ppl int, w io.Writer, opts ...Options) Cmd {
cmd := &star{Cmd: NewSkipper(cpl, ppl, w), hriPosition: 1, barcodeWidth: 1, barcodeHeight: 100}
for _, opt := range opts {
opt.apply(cmd)
}
return cmd
}
type star struct {
Cmd
barCodeFunc func(byte, string) image.Image
qrCodeFunc func(string) image.Image
hriPosition, barcodeWidth, barcodeHeight byte
}
func (c *star) Init() {
c.Write(ESC, '@')
}
func (c *star) LeftMargin(n int) {
if n < c.CPL() {
c.Write(ESC, 'l', byte(n))
}
}
func (c *star) WidthArea(n int) {
if n < c.CPL() {
c.Write(ESC, 'Q', byte(n))
}
}
func (c *star) AbsolutePosition(n int) {
if n < c.PPL() {
c.Write(ESC, GS, 'A', byte(n), byte(n>>8))
}
}
func (c *star) Align(b byte) {
c.Write(ESC, GS, 'a', minByte(b, 2))
}
func (c *star) UpsideDown(b bool) {
if b {
c.Write(SI)
return
}
c.Write(DC2)
}
// TabPositions maximum of 16 horizontal tabs can be set.
func (c *star) TabPositions(bs ...byte) {
l := len(bs)
if l == 0 {
return
} else if l > 16 {
bs = bs[:16]
}
buf := make([]byte, 0, 3+l)
buf = append(buf, ESC, 'D')
var previous byte
for _, n := range bs {
if n <= previous {
continue
}
buf = append(buf, n)
previous = n
}
buf = append(buf, NUL)
c.Write(buf...)
}
func (c *star) Tab() {
c.Write(HT)
}
func (c *star) CodePage(b byte) {
c.Write(ESC, GS, 't', b)
}
// CharSize
//
// h: character height (0 - x1 `normal`, 1 - x2, 2 - x3, 3 - x4, 5 - x5, 5 - x6)
// w: character width (0 - x1 `normal`, 1 - x2, 2 - x3, 3 - x4, 4 - x5, 5 - x6)
func (c *star) CharSize(h, w byte) {
c.Write(ESC, 'i', minByte(w, 5), minByte(h, 5))
}
func (c *star) Bold(b bool) {
if b {
c.Write(ESC, 'E')
return
}
c.Write(ESC, 'F')
}
func (c *star) Underling(b byte) {
c.Write(ESC, '-', minByte(b, 1))
}
// BarcodeWidth 1 <= b <= 9.
func (c *star) BarcodeWidth(b byte) {
b = maxByte(b, 1)
c.barcodeWidth = minByte(b, 9)
}
func (c *star) BarcodeHeight(b byte) {
c.barcodeHeight = maxByte(b, 1)
}
func (c *star) HRIPosition(b byte) {
c.hriPosition = 1
if b > 1 {
c.hriPosition = 2
}
}
func (c *star) Barcode(m byte, s string) {
if len(s) == 0 {
return
}
if c.barCodeFunc != nil {
code := c.barCodeFunc(m, s)
c.Image(code, false)
return
}
c.Write(ESC, 'b', c.barcodeType(m), c.hriPosition, c.barcodeWidth, c.barcodeHeight)
c.Text(s, nil)
c.Write(RS)
}
// QRCodeSize
//
// 1 <= b <= 8.
func (c *star) QRCodeSize(b byte) {
b = maxByte(b, 1)
c.Write(ESC, GS, 'y', 'S', '2', minByte(b, 8))
}
func (c *star) QRCodeCorrectionLevel(b byte) {
c.Write(ESC, GS, 'y', 'S', '1', minByte(b, 3))
}
func (c *star) QRCode(s string) {
l := len(s)
if l == 0 {
return
}
if c.qrCodeFunc != nil {
code := c.qrCodeFunc(s)
c.Image(code, false)
return
}
h, w := byte(l), byte(l>>8)
// Store the data in the symbol storage area.
c.Write(ESC, GS, 'y', 'D', '1', 0, h, w)
c.Text(s, nil)
// Print the symbol data in the symbol storage area.
c.Write(ESC, GS, 'y', 'P')
}
func (c *star) Image(img image.Image, invert bool) {
w, bs := ImageToBin(img, invert)
xl, xh := byte(w), byte(w>>8)
l := len(bs)
block := w * 3
start := 0
for end := block; start < l; end += block {
end = minByte(end, l)
c.Write(DC2)
c.Write(ESC, 'X', xl, xh)
c.Write(bs[start:end]...)
c.Write(ESC, 'J', 12)
start = end
}
}
func (c *star) Feed(b byte) {
if b > 0 {
c.Write(ESC, 'J', b)
}
}
func (c *star) LineFeed() {
c.Write(LF)
}
// Cut
//
// m = 0, full cut at the current position;
// m = 1, partial cut at the current position;
// m = 2, paper is fed to cutting position, then a full cut;
// m = 3, paper is fed to cutting position, then a partial cut;
func (c *star) Cut(m, _ byte) {
c.Write(ESC, 'd', minByte(m, 3))
}
func (c *star) FullCut() {
c.Cut(2, 0)
}
// OpenCashDrawer
//
// 1 <= t1 <= 255 - specifies the pulse on time (20 ms x t1).
// 1 <= t2 <= 255 - specifies the pulse off time (20 ms x t2).
func (c *star) OpenCashDrawer(m, t1, t2 byte) {
if t1 == 0 || t2 == 0 {
return
}
c.Write(ESC, GS, BEL, minByte(m, 1)+1, t1, t2)
}
func (c *star) barcodeType(m byte) byte {
if m > 13 {
m = 4
}
return [14]byte{49, 48, 50, 51, 52, 55, 54, 53, 56, 57, 65, 66, 67, 68}[m]
}