@@ -194,8 +194,9 @@ Printer.prototype.table = function (data, encoding) {
194
194
* @param {[Array] } size [optional]
195
195
* @return {[Printer] } printer [the escpos printer instance]
196
196
*/
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 || [ ]
199
200
let baseWidth = Math . floor ( this . width / width )
200
201
let cellWidth = Math . floor ( baseWidth / data . length )
201
202
let leftoverSpace = baseWidth - cellWidth * data . length
@@ -210,7 +211,7 @@ Printer.prototype.tableCustom = function (data, encoding, size) {
210
211
211
212
obj . text = obj . text . toString ( )
212
213
let textLength = obj . text . length
213
-
214
+
214
215
if ( obj . width ) {
215
216
cellWidth = baseWidth * obj . width
216
217
} else if ( obj . cols ) {
@@ -229,14 +230,23 @@ Printer.prototype.tableCustom = function (data, encoding, size) {
229
230
lineStr += ' '
230
231
}
231
232
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
+ }
233
244
234
245
for ( let s = 0 ; s < spaces - 1 ; s ++ ) {
235
246
lineStr += ' '
236
247
}
237
-
238
248
} else if ( align === 'RIGHT' ) {
239
- let spaces = ( cellWidth - textLength )
249
+ let spaces = cellWidth - textLength
240
250
if ( leftoverSpace > 0 ) {
241
251
spaces += leftoverSpace
242
252
leftoverSpace = 0
@@ -246,10 +256,29 @@ Printer.prototype.tableCustom = function (data, encoding, size) {
246
256
lineStr += ' '
247
257
}
248
258
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
+ }
251
270
} 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
+ }
253
282
254
283
let spaces = Math . floor ( cellWidth - textLength )
255
284
if ( leftoverSpace > 0 ) {
@@ -283,12 +312,12 @@ Printer.prototype.tableCustom = function (data, encoding, size) {
283
312
284
313
// Write the line
285
314
this . buffer . write (
286
- iconv . encode ( lineStr + _ . EOL , encoding || this . encoding )
315
+ iconv . encode ( lineStr + _ . EOL , options . encoding || this . encoding )
287
316
)
288
-
317
+
289
318
if ( secondLineEnabled ) {
290
319
// Writes second line if has
291
- return this . tableCustom ( secondLine , encoding , size )
320
+ return this . tableCustom ( secondLine , options )
292
321
} else {
293
322
return this
294
323
}
@@ -362,79 +391,89 @@ Printer.prototype.font = function (family) {
362
391
this . width = this . options && this . options . width || 56 ;
363
392
return this ;
364
393
} ;
394
+
365
395
/**
366
396
* [font style]
367
397
* @param {[type] } type [description]
368
398
* @return {[Printer] } printer [the escpos printer instance]
369
399
*/
370
- Printer . prototype . style = function ( type ) {
400
+ Printer . prototype . _getStyle = function ( type ) {
401
+ let styled = ''
371
402
switch ( type . toUpperCase ( ) ) {
372
-
373
403
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
378
408
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
383
413
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
388
418
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
393
423
394
424
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
399
429
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
404
434
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
409
439
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
414
444
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
419
449
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
424
454
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
429
459
430
460
case 'NORMAL' :
431
461
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
437
466
}
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 ) ) ;
438
477
return this ;
439
478
} ;
440
479
0 commit comments