This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.go
738 lines (621 loc) · 16.4 KB
/
printer.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
package hoin
import (
"fmt"
"image"
"image/color"
"io"
"net"
"strings"
)
const (
// Default ip and port for hoin printers
DefaultPrinterIP = "192.168.1.23:9100"
HT = 0x09
LF = 0x0A
CR = 0x0D
GS = 0x1D
ESC = 0x1B
DLE = 0x10
)
type Font int
const (
FontA Font = iota
FontB
)
type Justification int
const (
LeftJustify Justification = iota
CenterJustify
RightJustify
)
type HRIPosition int
const (
HRINone HRIPosition = iota
HRIAbove
HRIBelow
HRIBoth
)
// Density represents the DPI to use when printing images.
type Density int
const (
// SingleDensity is 90dpi
SingleDensity Density = iota
// DoubleDensity is 180dpi
DoubleDensity
)
type BarCode int
const (
BcUPCA BarCode = iota
BcUPCE
BcJAN13
BcJAN8
BcCODE39
BcITF
BcCODABAR
BcCODE93 BarCode = 72
BcCODE123 BarCode = 73
)
var (
lengthBarcodes = []BarCode{BcCODE93, BcCODE123}
allBarcodes = append(lengthBarcodes, BcUPCA, BcUPCE, BcJAN13, BcJAN8, BcCODE39, BcITF, BcCODABAR)
)
func inSlice[T ~int](v T, s ...T) bool {
for _, a := range s {
if v == a {
return true
}
}
return false
}
func checkEnum[T ~int](e T, enums ...T) error {
if inSlice(e, enums...) {
return nil
}
return fmt.Errorf("%v was not a valid choice from %v", e, enums)
}
func checkRange(n, min, max int, info string) error {
if n < min || max < n {
return fmt.Errorf("%s must be between %d and %d", info, min, max)
}
return nil
}
func boolToByte(b bool) byte {
if b {
return 1
}
return 0
}
type Printer struct {
dst io.ReadWriter
}
func NewPrinter(dst io.ReadWriter) Printer {
return Printer{
dst: dst,
}
}
func NewIpPrinter(addr string) (Printer, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return Printer{}, fmt.Errorf("unable to dial: %w", err)
}
return NewPrinter(conn), nil
}
func (p Printer) Close() error {
closer, ok := p.dst.(io.Closer)
if p.dst == nil || !ok {
return nil
}
err := closer.Close()
if err != nil {
return fmt.Errorf("could not close printer: %w", err)
}
return nil
}
func (p Printer) Write(b []byte) (int, error) {
n, err := p.dst.Write(b)
if err != nil {
return n, fmt.Errorf("could not write to printer: %w", err)
}
return n, nil
}
func (p Printer) Read(b []byte) (int, error) {
n, err := p.dst.Read(b)
if err != nil {
return n, fmt.Errorf("could not read from printer: %w", err)
}
return n, nil
}
func (p Printer) Initialize() error {
_, err := p.Write([]byte{ESC, '@'})
if err != nil {
return fmt.Errorf("could not initialize printer: %w", err)
}
return nil
}
// Beep makes a beep sound n times for t duration
//
// Duration is dependent on the model. For the HOP-E802
// each duration is around 100ms
func (p Printer) Beep(n, t int) error {
errMsg := "could not beep the printer: %w"
err := checkRange(n, 1, 9, "n")
if err != nil {
return fmt.Errorf(errMsg, err)
}
err = checkRange(t, 1, 9, "t")
if err != nil {
return fmt.Errorf(errMsg, err)
}
_, err = p.Write([]byte{ESC, 'B', byte(n), byte(t)})
if err != nil {
return fmt.Errorf(errMsg, err)
}
return nil
}
func (p Printer) Print(a ...any) error {
_, err := p.Write([]byte(fmt.Sprint(a...)))
if err != nil {
return fmt.Errorf("could not print %q: %w", a, err)
}
return nil
}
func (p Printer) Println(a ...any) error {
return p.Print(fmt.Sprint(a...) + string(rune(LF)))
}
func (p Printer) Printf(format string, a ...any) error {
return p.Print(fmt.Sprintf(format, a...))
}
// HT moves the print position to the next horizontal tab position
//
// By default HT will do nothing if SetHT is not called with tab positions
func (p Printer) HT() error {
_, err := p.Write([]byte{HT})
if err != nil {
return fmt.Errorf("could not send HT: %w", err)
}
return nil
}
// LF prints the data in the print buffer and feeds one line
func (p Printer) LF() error {
_, err := p.Write([]byte{LF})
if err != nil {
return fmt.Errorf("could not send LF: %w", err)
}
return nil
}
// CR prints and does a carriage return
func (p Printer) CR() error {
_, err := p.Write([]byte{CR})
if err != nil {
return fmt.Errorf("could not send CR: %w", err)
}
return nil
}
// Cut cuts the paper
func (p Printer) Cut() error {
_, err := p.Write([]byte{GS, 'V', 0})
if err != nil {
return fmt.Errorf("could not cut paper: %w", err)
}
return nil
}
// CutFeed feeds the paper n units and then cuts it
func (p Printer) CutFeed(n int) error {
errMsg := "could not feed and cut the paper: %w"
err := checkRange(n, 0, 255, "n")
if err != nil {
return fmt.Errorf(errMsg, err)
}
_, err = p.Write([]byte{GS, 'V', 66, byte(n)})
if err != nil {
return fmt.Errorf(errMsg, err)
}
return nil
}
// ResetLineSpacing sets the spacing to the default which
// is 1/6-inch lines (approx. 4.23mm)
func (p Printer) ResetLineSpacing() error {
_, err := p.Write([]byte{ESC, '2'})
if err != nil {
return fmt.Errorf("could not reset line spacing: %w", err)
}
return nil
}
// SetLineSpacing sets the line spacing to n * v/h motion units in inches
func (p Printer) SetLineSpacing(n int) error {
errMsg := "could not set line spacing: %w"
err := checkRange(n, 0, 255, "n")
if err != nil {
return fmt.Errorf(errMsg, err)
}
_, err = p.Write([]byte{ESC, '3', byte(n)})
if err != nil {
return fmt.Errorf(errMsg, err)
}
return err
}
// Feed feeds the paper n units
func (p Printer) Feed(n int) error {
errMsg := "could not feed paper: %w"
err := checkRange(n, 0, 255, "n")
if err != nil {
return fmt.Errorf(errMsg, err)
}
_, err = p.Write([]byte{ESC, 'J', byte(n)})
if err != nil {
return fmt.Errorf(errMsg, err)
}
return err
}
// FeedLines feeds the paper n lines
func (p Printer) FeedLines(n int) error {
errMsg := "could not feed lines: %w"
err := checkRange(n, 0, 255, "n")
if err != nil {
return fmt.Errorf(errMsg, err)
}
_, err = p.Write([]byte{ESC, 'd', byte(n)})
if err != nil {
return fmt.Errorf(errMsg, err)
}
return err
}
// SetHT sets the horizontal tab positions
//
// This command cancels previous SetHT commands
// Multiple positions can be set for tabbing
// A max of 32 positions can be set
// Calling SetHT with no argments resets the tab positions
func (p Printer) SetHT(positions ...int) error {
errMsg := "could not set horizontal tab positions: %w"
if len(positions) > 32 {
return fmt.Errorf("more than 32 positions was set")
}
var data []byte
for i, pos := range positions {
err := checkRange(pos, 1, 255, fmt.Sprintf("position %d", i))
if err != nil {
return fmt.Errorf(errMsg, err)
}
data = append(data, byte(pos))
}
data = append([]byte{ESC, 'D'}, data...)
data = append(data, 0)
_, err := p.Write(data)
if err != nil {
return fmt.Errorf(errMsg, err)
}
return nil
}
// SetTabs will set up to 32 tab positions at the given width intervals. If
// the tab value exceeds 256, fewer than 32 positions will be set.
func (p Printer) SetTabs(width int) error {
tabs := []int{}
for i := width; i < 256 && len(tabs) < 32; i+=width {
tabs = append(tabs, i)
}
return p.SetHT(tabs...)
}
// SetBold turns emphasized mode on or off
func (p Printer) SetBold(b bool) error {
_, err := p.Write([]byte{ESC, 'E', boolToByte(b)})
if err != nil {
return fmt.Errorf("could not set bold to %t: %w", b, err)
}
return nil
}
// SetRotate90 turns on 90 clockwise rotation mode for the text
//
// When text is double-width or double-height the text will be mirrored
func (p Printer) SetRotate90(b bool) error {
_, err := p.Write([]byte{ESC, 'V', boolToByte(b)})
if err != nil {
return fmt.Errorf("could not set bold to %t: %w", b, err)
}
return nil
}
// SetReversePrinting sets the white/black printing mode
//
// If b is true then it will print black text on white background
// If b is false then it will print white text on black background
func (p Printer) SetReversePrinting(b bool) error {
_, err := p.Write([]byte{GS, 'B', boolToByte(b)})
if err != nil {
return fmt.Errorf("could not set reverse printing mode: %w", err)
}
return nil
}
// SetFont changes the font
//
// n=0 selects font A
// n=1 selects font B
func (p Printer) SetFont(f Font) error {
errMsg := "could not set font to %v: %w"
err := checkEnum(f, FontA, FontB)
if err != nil {
return fmt.Errorf(errMsg, f, err)
}
_, err = p.Write([]byte{ESC, 'M', byte(f)})
if err != nil {
return fmt.Errorf(errMsg, f, err)
}
return nil
}
// Justify sets the alignment to n
func (p Printer) Justify(j Justification) error {
errMsg := "could not set justify to %v: %w"
err := checkEnum(j, CenterJustify, LeftJustify, RightJustify)
if err != nil {
return fmt.Errorf(errMsg, j, err)
}
_, err = p.Write([]byte{ESC, 'a', byte(j)})
if err != nil {
return fmt.Errorf(errMsg, j, err)
}
return nil
}
// PrintImage8 prints an image in the 8-bit row format. In this format each
// row is 8 dots tall.
//
// The density selects the horizontal DPI of the image. SingleDensity is
// 90dpi while DoubleDensity is 180dpi. Vertical DPI is always 60dpi for
// 8-bit image data.
//
// No black and white conversion is performed on the provided image. The
// image should be converted before calling this function.
func (p Printer) PrintImage8(img image.Image, density Density) error {
imgRect := img.Bounds()
var err error
errMsg := "could not print 8 dot image: %w"
err = checkEnum(density, SingleDensity, DoubleDensity)
if err != nil {
return fmt.Errorf(errMsg, err)
}
// 8 dot density (meta row is 8 dots tall)
for y := 0; y < imgRect.Max.Y; y += 8 {
row := []byte{}
for x := 0; x < imgRect.Max.X; x++ {
col := byte(0)
for i := 0; i < 8; i++ {
col <<= 1
// Pad the bottom row to be white
if y+i > imgRect.Max.Y {
continue
}
c := color.GrayModel.Convert(img.At(x, y+i)).(color.Gray)
if c.Y < 0x80 {
col |= 1
}
}
row = append(row, col)
}
data := []byte{ESC, '*', byte(density), byte(len(row)), byte(len(row) >> 8)}
if err = p.SetLineSpacing(0); err != nil {
return fmt.Errorf(errMsg, err)
}
if _, err = p.Write(append(data, row...)); err != nil {
return fmt.Errorf(errMsg, err)
}
if err = p.LF(); err != nil {
return fmt.Errorf(errMsg, err)
}
// Wait for line to finish
_, err = p.TransmitErrorStatus()
if err != nil {
return fmt.Errorf(errMsg, err)
}
}
return nil
}
// PrintImage24 prints an image in the 24-bit row format. In this format each
// row is 24 dots tall.
//
// This works the same as PrintImage8() with the only difference being the DPI
// of the printed image. SingleDensity is 90dpi while DoubleDensity is
// 180dpi. Vertical DPI is always 180dpi for 24-bit image data.
func (p Printer) PrintImage24(img image.Image, density Density) error {
imgRect := img.Bounds()
var err error
errMsg := "could not print 24 dot image: %w"
err = checkEnum(density, SingleDensity, DoubleDensity)
if err != nil {
return fmt.Errorf(errMsg, err)
}
command := []byte{ESC, 0x2A, byte(density + 32), byte(imgRect.Max.X), byte(imgRect.Max.X >> 8)}
// 24 dot density (meta row is 24 dots tall (3 bytes))
for y := 0; y < imgRect.Max.Y; y += 24 {
row := []byte{}
for x := 0; x < imgRect.Max.X; x++ {
for z := 0; z < 3; z++ {
col := byte(0)
for i := 0; i < 8; i++ {
col <<= 1
// Pad the bottom row to be white
if (y+z*8)+i > imgRect.Max.Y {
continue
}
c := color.GrayModel.Convert(img.At(x, (y+z*8)+i)).(color.Gray)
if c.Y < 0x80 {
col |= 1
}
}
row = append(row, col)
}
}
err = p.SetLineSpacing(0)
if err != nil {
return fmt.Errorf(errMsg, err)
}
_, err = p.Write(append(command, row...))
if err != nil {
return fmt.Errorf(errMsg, err)
}
err = p.LF()
if err != nil {
return fmt.Errorf(errMsg, err)
}
// If data is sent to fast it won't make it to the printer and will
// stop printing part of the way through an image. This will also
// lose any commands sent after the image. Sleeping for 35ms seems to
// be the best balance between not printing and reducing banding.
// time.Sleep(time.Millisecond * 35)
// Leaving above there just in case below is a footgun, but below should
// wait till print buffer is done to write more lines
_, err = p.TransmitErrorStatus()
if err != nil {
return fmt.Errorf(errMsg, err)
}
}
return nil
}
// SetHRIPosition sets the printing position of the HRI characters
// in relation to the barcode
func (p Printer) SetHRIPosition(hp HRIPosition) error {
errMsg := "could not set HRI position: %w"
err := checkEnum(hp, HRINone, HRIAbove, HRIBelow, HRIBoth)
if err != nil {
return fmt.Errorf(errMsg, err)
}
_, err = p.Write([]byte{GS, 'H', byte(hp)})
if err != nil {
return fmt.Errorf(errMsg, err)
}
return nil
}
// ResetBarCodeHeight sets the bar code height to 162
func (p Printer) ResetBarCodeHeight() error {
err := p.SetBarCodeHeight(162)
if err != nil {
return fmt.Errorf("could not reset bar code height: %w", err)
}
return nil
}
// SetBarCodeHeight sets the bar code height in n dots
func (p Printer) SetBarCodeHeight(n int) error {
errMsg := "could not set bar code height: %w"
err := checkRange(n, 1, 255, "height")
if err != nil {
return fmt.Errorf(errMsg, err)
}
_, err = p.Write([]byte{GS, 'h', byte(n)})
if err != nil {
return fmt.Errorf(errMsg, err)
}
return nil
}
func checkBarcodeCodabarData(data string) error {
body := "0123456789-$:/.+"
wrappers := "ABCD"
if !strings.ContainsRune(wrappers, rune(data[0])) || !strings.ContainsRune(wrappers, rune(data[len(data)-1])) {
return fmt.Errorf("the first and last byte of CODABAR must be one of %s", wrappers)
}
for _, d := range data {
if !strings.ContainsRune(body, d) {
return fmt.Errorf("%s was in the bar code data and only %q is accepted", string(d), body)
}
}
return nil
}
func checkBarcodeData(data, accepted string) error {
for _, d := range data {
if !strings.ContainsRune(accepted, d) {
return fmt.Errorf("%s was in the bar code data and only %q is accepted", string(d), accepted)
}
}
return nil
}
// PrintBarCode prints the bar code passed in with data.
//
// The size ranges are as follows in (Type: min, max):
//
// BcUPCA: 11, 12
// BcUPCE: 6, 7
// BcJAN13: 12, 13
// BcJAN8: 7, 8
// BcCODE39: 0, 14
// BcITF: 0, 22
// BcCODABAR: 2, 19
// BcCODE93: 1, 17
// BcCODE123: 0, 65
//
// For the accepted data values:
//
// BcUPCA, BcUPCE, BcJAN13, BcJAN8, BcITF all only accept [0123456789]
// BcCODE39, BcCODE93, BcCODE123 can accept [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.*$/+% ]
// BcCODABAR:
// The first and last character of the CODABAR code bar has to be one of [ABCD]
// and the rest of the characters in between can be one of [0123456789-$:/.+]
//
// Note on the CODE123 length:
//
// ...the docs say it's between 2 and 255 but the printer
// does not have that limit. On one hand it can go down to 0 character, but also I could
// not find the limit for max characters. At 15 characters it went off the page with a
// HOP-E802 printer and at 34 characters it starts printing the HRI weird. At 66 0s
// repeating it seems to break and stop printing, and the same at 65 As repeating.
// Long story short...I think they didn't finish programming the checks on CODE123
func (p Printer) PrintBarCode(barcodeType BarCode, data string) error {
errMsg := "could not print bar code: %w"
err := checkEnum(barcodeType, allBarcodes...)
if err != nil {
return fmt.Errorf(errMsg, err)
}
dataLength := len(data)
// Check length
var min, max int
switch barcodeType {
case BcUPCA:
min, max = 11, 12
case BcUPCE:
min, max = 6, 7
case BcJAN13:
min, max = 12, 13
case BcJAN8:
min, max = 7, 8
case BcCODE39:
min, max = 0, 14
case BcITF:
min, max = 0, 22
case BcCODABAR:
min, max = 2, 19
case BcCODE93:
min, max = 1, 17
case BcCODE123:
// At 66 characters for 'A...' the printer seems to cry
// for printing all 0s it cried at 65
// maybe it needs some friends
min, max = 0, 60
}
err = checkRange(dataLength, min, max, "data length")
if err != nil {
return fmt.Errorf(errMsg, err)
}
// Check data ranges
switch barcodeType {
case BcUPCA, BcUPCE, BcJAN13, BcJAN8, BcITF:
err = checkBarcodeData(data, "0123456789")
case BcCODE39, BcCODE93, BcCODE123:
err = checkBarcodeData(data, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.*$/+% ")
case BcCODABAR:
err = checkBarcodeCodabarData(data)
}
if err != nil {
return fmt.Errorf(errMsg, err)
}
msg := []byte{0x1D, 'k', byte(barcodeType)}
// Add data
if inSlice(barcodeType, lengthBarcodes...) {
// length defined barcode
msg = append(msg, byte(dataLength))
msg = append(msg, data...)
} else {
// Null ending barcode
msg = append(msg, data...)
msg = append(msg, 0)
}
_, err = p.Write(msg)
if err != nil {
return fmt.Errorf(errMsg, err)
}
return nil
}