Skip to content

Commit 8e26477

Browse files
authored
Merge pull request #73 from uyu423/feature/set-encode
global character encoding setting method.
2 parents fcfcb75 + b55f5da commit 8e26477

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,25 @@ const networkPrinter = new escpos.Printer(networkDevice);
125125

126126
Escpos inherits its methods to the printers. the following methods are defined:
127127

128-
### text("text", encodeType)
128+
#### text("text", encodeType)
129129

130130
Prints raw text. Raises TextError exception.
131131

132132
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.
133133

134134
If the type is undefined, the default type is GB18030.
135135

136-
### control("align")
136+
#### encode("encodeType")
137+
138+
Sets the encoding value globally. default type is GB18030 (Chinese)
139+
140+
```javascript
141+
printer
142+
.encode('EUC-KR')
143+
.text('동해물과 백두산이 마르고 닳도록');
144+
```
145+
146+
#### control("align")
137147

138148
Carrier feed and tabs.
139149

@@ -146,7 +156,7 @@ align is a string which takes any of the following values:
146156
+ VT for Vertical Tab
147157

148158

149-
### align("align")
159+
#### align("align")
150160

151161
Set text properties.
152162

@@ -158,16 +168,16 @@ align set horizontal position for text, the possible values are:
158168

159169
Default: LT
160170

161-
### font("type")
171+
#### font("type")
162172
font type could be A or B. Default: A
163173

164-
### size(width, heigth)
174+
#### size(width, heigth)
165175

166176
width is a numeric value, 1 is for regular size, and 2 is twice the standard size. Default: 1
167177

168178
height is a numeric value, 1 is for regular size and 2 is twice the standard size. Default: 1
169179

170-
### barcode("code", "barcodeType", width, height, "position", "font")
180+
#### barcode("code", "barcodeType", width, height, "position", "font")
171181

172182
Prints a barcode.
173183

@@ -204,7 +214,7 @@ Default: A
204214

205215
Raises BarcodeTypeError, BarcodeSizeError, BarcodeCodeError exceptions.
206216

207-
### cut("mode")
217+
#### cut("mode")
208218

209219
Cut paper.
210220

@@ -213,7 +223,7 @@ Partial cut is not implemented in all printers.
213223

214224
*** Don't foget this, because cut will flush buffer to printer ***
215225

216-
### cashdraw(pin)
226+
#### cashdraw(pin)
217227

218228
Sends a pulse to the cash drawer in the specified pin.
219229

examples/encode.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const escpos = require('../');
2+
3+
const device = new escpos.USB();
4+
// const device = new escpos.Network('localhost');
5+
// const device = new escpos.Serial('/dev/usb/lp0');
6+
const printer = new escpos.Printer(device);
7+
8+
device.open(function(err){
9+
10+
printer
11+
.font('a')
12+
.align('ct')
13+
.size(1, 1)
14+
.text('敏捷的棕色狐狸跳过懒狗') // default encoding set is GB18030
15+
.encode('EUC-KR') // set encode globally
16+
.text('동해물과 백두산이 마르고 닳도록')
17+
.text('こんにちは', 'EUC-JP') // set encode functional
18+
.cut();
19+
});

printer.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function Printer(adapter){
2222
EventEmitter.call(this);
2323
this.adapter = adapter;
2424
this.buffer = new Buffer();
25+
this.encoding = 'GB18030';
2526
};
2627

2728
/**
@@ -99,9 +100,19 @@ Printer.prototype.println = function(content){
99100
* @return printer instance
100101
*/
101102
Printer.prototype.text = function(content, encoding){
102-
return this.print(iconv.encode(content + _.EOL, encoding || 'GB18030'));
103+
return this.print(iconv.encode(content + _.EOL, encoding || this.encoding));
103104
};
104105

106+
/**
107+
* [function encode text]
108+
* @param {[String]} encoding [description]
109+
* @return printer instance
110+
*/
111+
Printer.prototype.encode = function(encoding) {
112+
this.encoding = encoding;
113+
return this;
114+
}
115+
105116
/**
106117
* [line feed]
107118
* @param {[type]} lines [description]

0 commit comments

Comments
 (0)