diff --git a/README.md b/README.md index a4c8b99..cdaab59 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ const networkPrinter = new escpos.Printer(networkDevice); Escpos inherits its methods to the printers. the following methods are defined: -### text("text", encodeType) +#### text("text", encodeType) Prints raw text. Raises TextError exception. @@ -133,7 +133,17 @@ For the encode type, see the [iconv-lite wiki document](https://github.com/ashtu If the type is undefined, the default type is GB18030. -### control("align") +#### encode("encodeType") + +Sets the encoding value globally. default type is GB18030 (Chinese) + +```javascript +printer +.encode('EUC-KR') +.text('동해물과 백두산이 마르고 닳도록'); +``` + +#### control("align") Carrier feed and tabs. @@ -146,7 +156,7 @@ align is a string which takes any of the following values: + VT for Vertical Tab -### align("align") +#### align("align") Set text properties. @@ -158,16 +168,16 @@ align set horizontal position for text, the possible values are: Default: LT -### font("type") +#### font("type") font type could be A or B. Default: A -### size(width, heigth) +#### size(width, heigth) width is a numeric value, 1 is for regular size, and 2 is twice the standard size. Default: 1 height is a numeric value, 1 is for regular size and 2 is twice the standard size. Default: 1 -### barcode("code", "barcodeType", width, height, "position", "font") +#### barcode("code", "barcodeType", width, height, "position", "font") Prints a barcode. @@ -204,7 +214,7 @@ Default: A Raises BarcodeTypeError, BarcodeSizeError, BarcodeCodeError exceptions. -### cut("mode") +#### cut("mode") Cut paper. @@ -213,7 +223,7 @@ Partial cut is not implemented in all printers. *** Don't foget this, because cut will flush buffer to printer *** -### cashdraw(pin) +#### cashdraw(pin) Sends a pulse to the cash drawer in the specified pin. diff --git a/examples/encode.js b/examples/encode.js new file mode 100644 index 0000000..30cf76d --- /dev/null +++ b/examples/encode.js @@ -0,0 +1,19 @@ +const escpos = require('../'); + +const device = new escpos.USB(); +// const device = new escpos.Network('localhost'); +// const device = new escpos.Serial('/dev/usb/lp0'); +const printer = new escpos.Printer(device); + +device.open(function(err){ + + printer + .font('a') + .align('ct') + .size(1, 1) + .text('敏捷的棕色狐狸跳过懒狗') // default encoding set is GB18030 + .encode('EUC-KR') // set encode globally + .text('동해물과 백두산이 마르고 닳도록') + .text('こんにちは', 'EUC-JP') // set encode functional + .cut(); +}); diff --git a/printer.js b/printer.js index 51c4db3..4e9c942 100644 --- a/printer.js +++ b/printer.js @@ -22,6 +22,7 @@ function Printer(adapter){ EventEmitter.call(this); this.adapter = adapter; this.buffer = new Buffer(); + this.encoding = 'GB18030'; }; /** @@ -99,9 +100,19 @@ Printer.prototype.println = function(content){ * @return printer instance */ Printer.prototype.text = function(content, encoding){ - return this.print(iconv.encode(content + _.EOL, encoding || 'GB18030')); + return this.print(iconv.encode(content + _.EOL, encoding || this.encoding)); }; +/** + * [function encode text] + * @param {[String]} encoding [description] + * @return printer instance + */ +Printer.prototype.encode = function(encoding) { + this.encoding = encoding; + return this; +} + /** * [line feed] * @param {[type]} lines [description]