-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescpos.lua
349 lines (305 loc) · 10.4 KB
/
escpos.lua
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
#!/usr/bin/lua5.4
--[[--
LUA library for printing to ESC/POS-compatible thermal printers.
@module escpos
@author Sodomon <[email protected]>, Máster Vitronic <[email protected]>
@license MIT
@copyright 2024
]]
local escpos = {}
-- https://github.com/rrthomas/lrexlib/
local regex = require("rex_pcre2")
local NUL = "\x00" -- ASCII null control character
local LF = "\x0a" -- ASCII linefeed control character
local ESC = "\x1b" -- ASCII escape control character
local FS = "\x1c" -- ASCII form separator control character
local FF = "\x0c" -- ASCII form feed control character
local GS = "\x1d" -- ASCII group separator control character
local DLE = "\x10" -- ASCII data link escape control character
local EOT = "\x04" -- ASCII end of transmission control character
escpos.COLOR_1 = 0 -- Use the first color (usually black)
escpos.COLOR_2 = 1 -- Use the second color (usually red or blue)
escpos.width = 32 -- Width of paper (default 32, ticket)
escpos.BARCODE_TEXT_NONE = 0 -- does not show the barcode text
escpos.BARCODE_TEXT_ABOVE = 1 -- shows barcode text at the top of the page
escpos.BARCODE_TEXT_BELOW = 2 -- displays barcode text at the bottom
escpos.JUSTIFY_LEFT = 0 -- Justify content to left (default)
escpos.JUSTIFY_CENTER = 1 -- Justify content to center
escpos.JUSTIFY_RIGHT = 2 -- Justify content to right
escpos.MODE_FONT_A = 0 -- Use Font A
escpos.MODE_FONT_B = 1 -- Use Font B
escpos.MODE_EMPHASIZED = 8 -- Use text emphasis
escpos.MODE_DOUBLE_HEIGHT = 16 -- Use double height text
escpos.MODE_DOUBLE_WIDTH = 32 -- Use double width text
escpos.MODE_UNDERLINE = 128 -- User Underline text
escpos.QR_ERR_CO_LEVEL_L = 0 -- QR Code error code level L
escpos.QR_ERR_CO_LEVEL_M = 1 -- QR Code error code level M
escpos.QR_ERR_CO_LEVEL_Q = 2 -- QR Code error code level Q
escpos.QR_ERR_CO_LEVEL_H = 3 -- QR Code error code level H
escpos.QR_MODEL_1 = 1 -- Indicates QR model 1
escpos.QR_MODEL_2 = 2 -- Indicates QR model 2
escpos.QR_MICRO = 3 -- Indicates QR model 3(micro)
escpos.CUT_FULL = 65 -- Complete paper cut
escpos.CUT_PARTIAL = 66 -- Partial paper cut
--- Define the connector for devices
-- @param types linux or network
-- @usage escpos:connector_type("linux")
-- @see device:connector
function escpos:connector_type(types)
if types == "linux" then
device = require("connectors.linux")
elseif types == "network" then
device = require("connectors.network")
end
end
--- Aligns two columns from 2 strings.
-- @string col1
-- @string col2
-- @usage escpos:two_columns("TITLE", "SUBTITLE")
function escpos:two_columns(col1, col2)
local args= {col1, col2}
local result = ''
for index,value in pairs(args) do
result = (index == 1) and value or table.concat({
result, value
}, '-')
end
local s_r = #result
local s_i = (escpos.width-s_r)+1
result = string.gsub(result, '-', string.rep(" ", s_i))
return result, #result
end
--- Aligns three columns from 3 strings.
-- @string col1
-- @string col2
-- @string col3
-- @usage escpos:three_columns("TITLE", "SUBTITLE", "COMMENT")
function escpos:three_columns(col1, col2, col3)
-- Calcular longitud total de los strings sin espacios
local total_length = string.len(col1) + string.len(col2) + string.len(col3)
-- Calcular espacio disponible para rellenar
local padding_length = escpos.width - total_length
-- Calcular espacio a la izquierda del segundo string
local left_padding = math.floor(padding_length / 2)
-- Calcular espacio a la derecha del segundo string
local right_padding = padding_length - left_padding
-- Generar string centrado
local centered_string = col1 .. string.rep(" ", left_padding) .. col2 .. string.rep(" ", right_padding) .. col3
return centered_string
end
-- Generate two characters for a number
local function intLowHigh(input, length)
outp = ""
for i = 0, length, 1 do
if i >= length then
break
end
outp = table.concat({
outp, string.char(math.floor(input % 256))
}, '')
input = math.floor(input / 256)
end
return outp
end
local function wrapperSend2dCodeData(fn, cn, data, m)
local data, m = data, m or ''
header = intLowHigh(string.len(data) + string.len(m) + 2, 2)
device:write(GS .. "(k" .. header .. cn .. fn .. m .. data)
end
--- Defines the space between lines.
-- @int height
-- @usage escpos:set_line_spacing(4)
function escpos:set_line_spacing(height)
if height == nil then
device:write(ESC .. "2");
end
device:write(ESC .. "3" .. string.char(height));
end
function escpos:setPrintLeftMargin(margin)
device:write(GS .. 'L' .. intLowHigh(margin, 2));
end
function escpos:setPrintWidth(width)
device:write(GS .. 'W' .. intLowHigh(self.width, 2));
end
--[[--
Defines the type of print mode.
@param mode modes available for use:
escpos.MODE_FONT_A
escpos.MODE_FONT_B
escpos.MODE_EMPHASIZED
escpos.MODE_DOUBLE_HEIGHT
escpos.MODE_DOUBLE_WIDTH
escpos.MODE_UNDERLINE
@usage escpos:selectPrintMode(escpos.MODE_UNDERLINE)
]]
function escpos:selectPrintMode(mode)
device:write(ESC .. "!" .. string.char(mode))
end
--[[--
Defines the color to use (only if the printer supports color).
@param color Valid colors:
escpos.COLOR_1
escpos.COLOR_2
@usage escpos:set_color(escpos.COLOR_2)
]]
function escpos:set_color(color)
device:write(ESC .. "r" .. string.char(color))
end
--- Define the text size.
-- @int widthMultiplier
-- @int heightMultiplier
-- @usage escpos:set_text_size(3, 3)
function escpos:set_text_size(widthMultiplier, heightMultiplier)
local c = math.pow(2, 4) * (widthMultiplier - 1) + (heightMultiplier - 1)
device:write(ESC .. "!" .. string.char(c))
end
--- Defines the height of the barcode.
-- @int height
-- @usage escpos:setBarcodeHeight(4)
function escpos:setBarcodeHeight(height)
device:write(GS .. "h" .. string.char(height))
end
--- Defines the width of the barcode.
-- @int width
-- @usage escpos:setBarcodeWidth(7)
function escpos:setBarcodeWidth(width)
device:write(GS .. "w" .. string.char(width))
end
--[[--
Defines the position of the barcode content.
@param position avaliable position:
escpos.BARCODE_TEXT_NONE (does not show the text)
escpos.BARCODE_TEXT_ABOVE (shows text at the top of the page)
escpos.BARCODE_TEXT_BELOW (displays text at the bottom)
@usage escpos:setBarcodeTextPosition(escpos.BARCODE_TEXT_BELOW)
]]
function escpos:setBarcodeTextPosition(position)
device:write(GS .. "H" .. string.char(position))
end
--[[--
Creation of the barcode.
@param _type types of barcodes: upca, upce, jan13, jan8, code39, itf, codabar,
code93, code128
@string str content of the barcode
@usage escpos:barcode("code128", "HELLO WORLD!")
]]
function escpos:barcode(_type, str)
local validate = {
['upca'] =function (str)
return (regex.match("^[0-9]{11,12}$/", str)) , 65
end,
['upce'] =function (str)
return (regex.match("^([0-9]{6,8}|[0-9]{11,12})$", str)), 66
end,
['jan13'] =function (str)
return regex.match("^[0-9]{12,13}$/", str), 67
end,
['jan8'] =function (str)
return regex.match("^[0-9]{7,8}$/"), 68
end,
['code39'] =function (str)
return str, 69
end,
['itf'] =function (str)
return regex.match(str, "^([0-9]{2})+$/"), 70
end,
['codabar']=function (str)
return str, 71
end,
['code93'] =function (str)
return regex.match(str, "^[\\x00-\\x7F]+$/"), 72
end,
['code128']=function (str)
return regex.match(str, "^[A-Z][\\x00-\\x7F]+$"), 73
end
}
-- @TODO: Add validation to receive if BarcodeB is supported by the device
-- io.stdout:write(GS .. "k" .. string.char(code - 65) .. srt .. NUL)
local result, code = validate[_type](str)
if result == str then
device:write(('%sk%c%c%s'):format(
GS, code, (str):len(), str
))
else
print("Error with barcode type please select valid type")
end
end
--- Defines whether Emphasis will be activated.
-- @bool on true(activate) or false(deactivate)
-- @usage escpos:setEmphasis(true)
function escpos:setEmphasis(on)
local r = (on==true) and string.char(1) or string.char(0)
device:write(ESC .. "E".. r)
end
--[[--
Define the type of justification to use.
@param justification Types of justification:
escpos.JUSTIFY_LEFT
escpos.JUSTIFY_CENTER
escpos.JUSTIFY_RIGHT
@usage escpos:setJustification(escpos.JUSTIFY_CENTER)
]]
function escpos:setJustification(justification)
device:write(ESC .. "a" .. string.char(justification))
end
--[[--
Define what type of font to use.
@param font Type of fonts:
escpos.FONT_A (escpos.MODE_FONT_A)
escpos.FONT_B (escpos.MODE_FONT_B)
@usage escpos:setFont(escpos.MODE_FONT_A)
]]
function escpos:setFont(font)
device:write(ESC .. "M" .. string.char(font))
end
--- Send a feed.
-- @int nl
-- @usage escpos:feed(2)
function escpos:feed(nl)
device:write(ESC .. "d" .. string.char(nl))
end
--- Send a text with line break.
-- @string text
-- @usage escpos:text("HELLO WOLRD!!")
function escpos:text(text)
device:write(text .. "\n")
end
--[[--
Print the given data as a QR code on the printer.
@string content the content of the code
@param err_co QR error-correction level to use:
escpos.QR_ERR_CO_LEVEL_L
escpos.QR_ERR_CO_LEVEL_M
escpos.QR_ERR_CO_LEVEL_Q
escpos.QR_ERR_CO_LEVEL_H
@int size Pixel size to use. must be from 1 to 16 (default 3).
@param model Define the QR code model to use.
escpos.QR_MODEL_1
escpos.QR_MODEL_2
escpos.QR_MICRO
@usage escpos:print_qrcode("QR TESTING", escpos.QR_ERR_CO_LEVEL_M, 4, escpos.QR_MODEL_2)
]]
function escpos:print_qrcode(content, err_co, size, model)
local err_co = err_co or escpos.QR_ERR_CO_LEVEL_L
local size = size or 3
local model = model or escpos.QR_MODEL_2
cn = '1'
wrapperSend2dCodeData(string.char(65), cn, string.char(48 + model) .. string.char(0))
wrapperSend2dCodeData(string.char(67), cn, string.char(size))
wrapperSend2dCodeData(string.char(69), cn, string.char(48 + err_co))
wrapperSend2dCodeData(string.char(80), cn, content, '0')
wrapperSend2dCodeData(string.char(81), cn, '', '0')
end
--[[--
Cut the paper.
@param mode cut modes:
escpos.CUT_FULL (default)
escpos.CUT_PARTIAL
@int lines integer
]]
function escpos:cut(mode, lines)
local mode = mode or escpos.CUT_FULL
local lines = lines or 3
device:write(GS .. "V" .. string.char(mode) .. string.char(lines));
end
return escpos