-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
158 lines (129 loc) · 3.61 KB
/
parser.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
package zebrash
import (
"fmt"
"strings"
"github.com/ingridhq/zebrash/elements"
"github.com/ingridhq/zebrash/parsers"
"github.com/ingridhq/zebrash/printers"
)
type Parser struct {
printer *printers.VirtualPrinter
commandParsers []*parsers.CommandParser
}
func NewParser() *Parser {
return &Parser{
printer: printers.NewVirtualPrinter(),
commandParsers: []*parsers.CommandParser{
parsers.NewLabelHomeParser(),
parsers.NewLabelReversePrintParser(),
parsers.NewGraphicBoxParser(),
parsers.NewGraphicCircleParser(),
parsers.NewGraphicFieldParser(),
parsers.NewGraphicDiagonalLineParser(),
parsers.NewChangeDefaultFontParser(),
parsers.NewChangeFontParser(),
parsers.NewFieldOriginParser(),
parsers.NewFieldTypesetParser(),
parsers.NewFieldBlockParser(),
parsers.NewFieldSeparatorParser(),
parsers.NewFieldDataParser(),
parsers.NewFieldValueParser(),
parsers.NewFieldOrientationParser(),
parsers.NewFieldReversePrintParser(),
parsers.NewHexEscapeParser(),
parsers.NewMaxicodeParser(),
parsers.NewBarcode128Parser(),
parsers.NewBarcodePdf417Parser(),
parsers.NewBarcodeAztecParser(),
parsers.NewBarcodeDatamatrixParser(),
parsers.NewBarcodeQrParser(),
parsers.NewDownloadGraphicsParser(),
parsers.NewRecallGraphicsParser(),
parsers.NewBarcodeFieldDefaults(),
},
}
}
func (p *Parser) Parse(zplData []byte) ([]elements.LabelInfo, error) {
var results []elements.LabelInfo
var resultElements []interface{}
const startCode = "^XA"
const endCode = "^XZ"
commands, err := splitZplCommands(zplData)
if err != nil {
return nil, fmt.Errorf("failed to split zpl commands: %w", err)
}
for _, command := range commands {
if strings.ToUpper(command) == startCode {
p.printer.NextDownloadFormatName = ""
continue
}
if strings.ToUpper(command) == endCode {
results = append(results, elements.LabelInfo{
DownloadFormatName: p.printer.NextDownloadFormatName,
Elements: resultElements,
})
resultElements = nil
continue
}
for _, cp := range p.commandParsers {
if !cp.CanParse(command) {
continue
}
el, err := cp.Parse(command, p.printer)
if err != nil {
return nil, fmt.Errorf("failed to parse zpl command %v: %w", command, err)
}
if el != nil {
resultElements = append(resultElements, el)
}
}
}
return results, nil
}
func splitZplCommands(zplData []byte) ([]string, error) {
data := strings.ReplaceAll(string(zplData), "\n", "")
data = strings.ReplaceAll(data, "\r", "")
data = strings.ReplaceAll(data, "\t", "")
caret := byte('^')
tilde := byte('~')
var buff strings.Builder
var results []string
const changeTildeCode = "CT"
const changeCaretCode = "CC"
for i := 0; i < len(data); i++ {
c := data[i]
if c == caret || c == tilde {
command := buff.String()
buff.Reset()
if len(command) > 2 {
command = normalizeCommand(command, tilde, caret)
switch {
case strings.Index(command, changeTildeCode) == 1:
tilde = command[3]
case strings.Index(command, changeCaretCode) == 1:
caret = command[3]
default:
results = append(results, command)
}
}
}
if err := buff.WriteByte(c); err != nil {
return nil, err
}
}
command := buff.String()
if len(command) > 0 {
command = normalizeCommand(command, tilde, caret)
results = append(results, command)
}
return results, nil
}
func normalizeCommand(command string, tilde, caret byte) string {
if caret != '^' && command[0] == caret {
command = "^" + command[1:]
}
if tilde != '~' && command[0] == tilde {
command = "~" + command[1:]
}
return strings.TrimLeft(command, " ")
}