Skip to content

Commit 9396ecf

Browse files
SinanMtllsongdev
authored andcommitted
Style parameter added to tableCustom
1 parent e2d9baa commit 9396ecf

File tree

4 files changed

+119
-74
lines changed

4 files changed

+119
-74
lines changed

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ device.open(function(error){
4141
.text('敏捷的棕色狐狸跳过懒狗')
4242
.barcode('1234567', 'EAN8')
4343
.table(["One", "Two", "Three"])
44-
.tableCustom([
45-
{ text:"Left", align:"LEFT", width:0.33 },
46-
{ text:"Center", align:"CENTER", width:0.33},
47-
{ text:"Right", align:"RIGHT", width:0.33 }
48-
])
44+
.tableCustom(
45+
[
46+
{ text:"Left", align:"LEFT", width:0.33, style: 'B' },
47+
{ text:"Center", align:"CENTER", width:0.33},
48+
{ text:"Right", align:"RIGHT", width:0.33 }
49+
],
50+
{ encoding: 'cp857', size: [1, 1] } // Optional
51+
)
4952
.qrimage('https://github.com/song940/node-escpos', function(err){
5053
this.cut();
5154
this.close();

packages/printer/README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ device.open(function(error){
5252
.text('敏捷的棕色狐狸跳过懒狗')
5353
.barcode('1234567', 'EAN8')
5454
.table(["One", "Two", "Three"])
55-
.tableCustom([
56-
{ text:"Left", align:"LEFT", width:0.33 },
57-
{ text:"Center", align:"CENTER", width:0.33},
58-
{ text:"Right", align:"RIGHT", width:0.33 }
59-
])
55+
.tableCustom(
56+
[
57+
{ text:"Left", align:"LEFT", width:0.33, style: 'B' },
58+
{ text:"Center", align:"CENTER", width:0.33},
59+
{ text:"Right", align:"RIGHT", width:0.33 }
60+
],
61+
{ encoding: 'cp857', size: [1, 1] } // Optional
62+
)
6063
.qrimage('https://github.com/song940/node-escpos', function(err){
6164
this.cut();
6265
this.close();

packages/printer/index.js

+102-63
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ Printer.prototype.table = function (data, encoding) {
194194
* @param {[Array]} size [optional]
195195
* @return {[Printer]} printer [the escpos printer instance]
196196
*/
197-
Printer.prototype.tableCustom = function (data, encoding, size) {
198-
let [ width = 1, height = 1 ] = size || []
197+
Printer.prototype.tableCustom = function (data, options = {}) {
198+
options = options || { size: [], encoding: this.encoding }
199+
let [width = 1, height = 1] = options.size || []
199200
let baseWidth = Math.floor(this.width / width)
200201
let cellWidth = Math.floor(baseWidth / data.length)
201202
let leftoverSpace = baseWidth - cellWidth * data.length
@@ -210,7 +211,7 @@ Printer.prototype.tableCustom = function (data, encoding, size) {
210211

211212
obj.text = obj.text.toString()
212213
let textLength = obj.text.length
213-
214+
214215
if (obj.width) {
215216
cellWidth = baseWidth * obj.width
216217
} else if (obj.cols) {
@@ -229,14 +230,23 @@ Printer.prototype.tableCustom = function (data, encoding, size) {
229230
lineStr += ' '
230231
}
231232

232-
if (obj.text !== '') lineStr += obj.text
233+
if (obj.text !== '') {
234+
if (obj.style) {
235+
lineStr += (
236+
this._getStyle(obj.style) +
237+
obj.text +
238+
this._getStyle("NORMAL")
239+
)
240+
} else {
241+
lineStr += obj.text
242+
}
243+
}
233244

234245
for (let s = 0; s < spaces - 1; s++) {
235246
lineStr += ' '
236247
}
237-
238248
} else if (align === 'RIGHT') {
239-
let spaces = (cellWidth - textLength)
249+
let spaces = cellWidth - textLength
240250
if (leftoverSpace > 0) {
241251
spaces += leftoverSpace
242252
leftoverSpace = 0
@@ -246,10 +256,29 @@ Printer.prototype.tableCustom = function (data, encoding, size) {
246256
lineStr += ' '
247257
}
248258

249-
if (obj.text !== '') lineStr += obj.text
250-
259+
if (obj.text !== '') {
260+
if (obj.style) {
261+
lineStr += (
262+
this._getStyle(obj.style) +
263+
obj.text +
264+
this._getStyle("NORMAL")
265+
)
266+
} else {
267+
lineStr += obj.text
268+
}
269+
}
251270
} else {
252-
if (obj.text !== '') lineStr += obj.text
271+
if (obj.text !== '') {
272+
if (obj.style) {
273+
lineStr += (
274+
this._getStyle(obj.style) +
275+
obj.text +
276+
this._getStyle("NORMAL")
277+
)
278+
} else {
279+
lineStr += obj.text
280+
}
281+
}
253282

254283
let spaces = Math.floor(cellWidth - textLength)
255284
if (leftoverSpace > 0) {
@@ -283,12 +312,12 @@ Printer.prototype.tableCustom = function (data, encoding, size) {
283312

284313
// Write the line
285314
this.buffer.write(
286-
iconv.encode(lineStr + _.EOL, encoding || this.encoding)
315+
iconv.encode(lineStr + _.EOL, options.encoding || this.encoding)
287316
)
288-
317+
289318
if (secondLineEnabled) {
290319
// Writes second line if has
291-
return this.tableCustom(secondLine, encoding, size)
320+
return this.tableCustom(secondLine, options)
292321
} else {
293322
return this
294323
}
@@ -362,79 +391,89 @@ Printer.prototype.font = function (family) {
362391
this.width = this.options && this.options.width || 56;
363392
return this;
364393
};
394+
365395
/**
366396
* [font style]
367397
* @param {[type]} type [description]
368398
* @return {[Printer]} printer [the escpos printer instance]
369399
*/
370-
Printer.prototype.style = function (type) {
400+
Printer.prototype._getStyle = function (type) {
401+
let styled = ''
371402
switch (type.toUpperCase()) {
372-
373403
case 'B':
374-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
375-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
376-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF);
377-
break;
404+
styled += _.TEXT_FORMAT.TXT_BOLD_ON
405+
styled += _.TEXT_FORMAT.TXT_ITALIC_OFF
406+
styled += _.TEXT_FORMAT.TXT_UNDERL_OFF
407+
break
378408
case 'I':
379-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
380-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
381-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF);
382-
break;
409+
styled += _.TEXT_FORMAT.TXT_BOLD_OFF
410+
styled += _.TEXT_FORMAT.TXT_ITALIC_ON
411+
styled += _.TEXT_FORMAT.TXT_UNDERL_OFF
412+
break
383413
case 'U':
384-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
385-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
386-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON);
387-
break;
414+
styled += _.TEXT_FORMAT.TXT_BOLD_OFF
415+
styled += _.TEXT_FORMAT.TXT_ITALIC_OFF
416+
styled += _.TEXT_FORMAT.TXT_UNDERL_ON
417+
break
388418
case 'U2':
389-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
390-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
391-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON);
392-
break;
419+
styled += _.TEXT_FORMAT.TXT_BOLD_OFF
420+
styled += _.TEXT_FORMAT.TXT_ITALIC_OFF
421+
styled += _.TEXT_FORMAT.TXT_UNDERL2_ON
422+
break
393423

394424
case 'BI':
395-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
396-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
397-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF);
398-
break;
425+
styled += _.TEXT_FORMAT.TXT_BOLD_ON
426+
styled += _.TEXT_FORMAT.TXT_ITALIC_ON
427+
styled += _.TEXT_FORMAT.TXT_UNDERL_OFF
428+
break
399429
case 'BIU':
400-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
401-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
402-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON);
403-
break;
430+
styled += _.TEXT_FORMAT.TXT_BOLD_ON
431+
styled += _.TEXT_FORMAT.TXT_ITALIC_ON
432+
styled += _.TEXT_FORMAT.TXT_UNDERL_ON
433+
break
404434
case 'BIU2':
405-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
406-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
407-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON);
408-
break;
435+
styled += _.TEXT_FORMAT.TXT_BOLD_ON
436+
styled += _.TEXT_FORMAT.TXT_ITALIC_ON
437+
styled += _.TEXT_FORMAT.TXT_UNDERL2_ON
438+
break
409439
case 'BU':
410-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
411-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
412-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON);
413-
break;
440+
styled += _.TEXT_FORMAT.TXT_BOLD_ON
441+
styled += _.TEXT_FORMAT.TXT_ITALIC_OFF
442+
styled += _.TEXT_FORMAT.TXT_UNDERL_ON
443+
break
414444
case 'BU2':
415-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
416-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
417-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON);
418-
break;
445+
styled += _.TEXT_FORMAT.TXT_BOLD_ON
446+
styled += _.TEXT_FORMAT.TXT_ITALIC_OFF
447+
styled += _.TEXT_FORMAT.TXT_UNDERL2_ON
448+
break
419449
case 'IU':
420-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
421-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
422-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON);
423-
break;
450+
styled += _.TEXT_FORMAT.TXT_BOLD_OFF
451+
styled += _.TEXT_FORMAT.TXT_ITALIC_ON
452+
styled += _.TEXT_FORMAT.TXT_UNDERL_ON
453+
break
424454
case 'IU2':
425-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
426-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
427-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON);
428-
break;
455+
styled += _.TEXT_FORMAT.TXT_BOLD_OFF
456+
styled += _.TEXT_FORMAT.TXT_ITALIC_ON
457+
styled += _.TEXT_FORMAT.TXT_UNDERL2_ON
458+
break
429459

430460
case 'NORMAL':
431461
default:
432-
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
433-
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
434-
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF);
435-
break;
436-
462+
styled += _.TEXT_FORMAT.TXT_BOLD_OFF
463+
styled += _.TEXT_FORMAT.TXT_ITALIC_OFF
464+
styled += _.TEXT_FORMAT.TXT_UNDERL_OFF
465+
break
437466
}
467+
return styled
468+
}
469+
470+
/**
471+
* [font style]
472+
* @param {[type]} type [description]
473+
* @return {[Printer]} printer [the escpos printer instance]
474+
*/
475+
Printer.prototype.style = function (type) {
476+
this.buffer.write(this._getStyle(type));
438477
return this;
439478
};
440479

test/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('ESC/POS printing test', function() {
3434
.tableCustom([
3535
{ text: "Check:13", align: "LEFT" },
3636
{ text: "Table:A1", align: "RIGHT" }
37-
], null, [2, 1])
37+
], { size: [2, 1] })
3838
.newLine()
3939
.align("CT")
4040
.size(1, 1)

0 commit comments

Comments
 (0)