@@ -6,6 +6,7 @@ const getPixels = require('get-pixels');
66const Buffer = require ( 'mutable-buffer' ) ;
77const EventEmitter = require ( 'events' ) ;
88const Image = require ( './image' ) ;
9+ const Barcode = require ( './barcode' ) ;
910const _ = require ( './commands' ) ;
1011
1112/**
@@ -277,6 +278,8 @@ Printer.prototype.hardware = function(hw){
277278 * @return printer instance
278279 */
279280Printer . prototype . barcode = function ( code , type , width , height , position , font ) {
281+ var convertCode = String ( code ) ;
282+ var parityBit = ''
280283 if ( width >= 2 || width <= 6 ) {
281284 this . buffer . write ( _ . BARCODE_FORMAT . BARCODE_WIDTH [ width ] ) ;
282285 } else {
@@ -296,7 +299,16 @@ Printer.prototype.barcode = function(code, type, width, height, position, font){
296299 this . buffer . write ( _ . BARCODE_FORMAT [
297300 'BARCODE_' + ( ( type || 'EAN13' ) . replace ( '-' , '_' ) . toUpperCase ( ) )
298301 ] ) ;
299- this . buffer . write ( code ) ;
302+ if ( ( ! type || type === 'EAN13' ) && convertCode . length != 12 ) {
303+ throw Error ( 'EAN13 Barcode type requires code length 12' ) ;
304+ }
305+ if ( type === 'EAN8' && convertCode . length != 7 ) {
306+ throw Error ( 'EAN8 Barcode type requires code length 7' ) ;
307+ }
308+ if ( ! type || type === 'EAN13' || type === 'EAN8' ) {
309+ parityBit = Barcode . getParityBit ( code , type || 'EAN13' ) ;
310+ }
311+ this . buffer . write ( code + parityBit ) ;
300312 return this ;
301313} ;
302314
0 commit comments