Skip to content

Commit

Permalink
Merge pull request #73 from uyu423/feature/set-encode
Browse files Browse the repository at this point in the history
global character encoding setting method.
  • Loading branch information
lsongdev authored Sep 6, 2017
2 parents fcfcb75 + b55f5da commit 8e26477
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,25 @@ 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.

For the encode type, see the [iconv-lite wiki document](https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings). Escpos uses `iconv-lite` for encoding.

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.

Expand All @@ -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.

Expand All @@ -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.

Expand Down Expand Up @@ -204,7 +214,7 @@ Default: A

Raises BarcodeTypeError, BarcodeSizeError, BarcodeCodeError exceptions.

### cut("mode")
#### cut("mode")

Cut paper.

Expand All @@ -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.

Expand Down
19 changes: 19 additions & 0 deletions examples/encode.js
Original file line number Diff line number Diff line change
@@ -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();
});
13 changes: 12 additions & 1 deletion printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function Printer(adapter){
EventEmitter.call(this);
this.adapter = adapter;
this.buffer = new Buffer();
this.encoding = 'GB18030';
};

/**
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 8e26477

Please sign in to comment.