-
Notifications
You must be signed in to change notification settings - Fork 20
/
buffer-builder.ts
227 lines (184 loc) · 5.19 KB
/
buffer-builder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { Command } from './command';
import { MutableBuffer } from 'mutable-buffer';
export class BufferBuilder {
private buffer: MutableBuffer;
constructor(private defaultSettings: boolean = true) {
this.buffer = new MutableBuffer();
if (this.defaultSettings) {
this.resetCharacterSize();
this.resetCharacterCodeTable();
}
}
public end(): BufferBuilder {
return this;
}
public resetCharacterCodeTable(): BufferBuilder {
this.buffer.write(Command.ESC_t(0));
return this;
}
public setCharacterSize(width: number = 0, height: number = 0): BufferBuilder {
let size = (width << 4) + height;
this.buffer.write(Command.GS_exclamation(size));
return this;
}
public resetCharacterSize(): BufferBuilder {
this.buffer.write(Command.GS_exclamation(0));
return this;
}
public startCompressedCharacter(): BufferBuilder {
this.buffer.write(Command.ESC_M(1));
return this;
}
public endCompressedCharacter(): BufferBuilder {
this.buffer.write(Command.ESC_M(0));
return this;
}
public startBold(): BufferBuilder {
this.buffer.write(Command.ESC_E(1));
return this;
}
public endBold(): BufferBuilder {
this.buffer.write(Command.ESC_E(0));
return this;
}
public startUnderline(underlineMode: UNDERLINE_MODE = UNDERLINE_MODE.TWO_POINTS_OF_COARSE): BufferBuilder {
this.buffer.write(Command.ESC_minus(underlineMode));
return this;
}
public endUnderline(): BufferBuilder {
this.buffer.write(Command.ESC_minus(48));
return this;
}
public startAlign(alignment: ALIGNMENT): BufferBuilder {
this.buffer.write(Command.ESC_a(alignment));
return this;
}
public resetAlign(): BufferBuilder {
return this.startAlign(ALIGNMENT.LEFT);
}
public startWhiteMode(): BufferBuilder {
this.buffer.write(Command.GS_B(1));
return this;
}
public endWhiteMode(): BufferBuilder {
this.buffer.write(Command.GS_B(0));
return this;
}
public startReverseMode(): BufferBuilder {
this.buffer.write(Command.ESC_rev(1));
return this;
}
public endReverseMode(): BufferBuilder {
this.buffer.write(Command.ESC_rev(0));
return this;
}
public printBarcode(data: string, barcodeSystem: BARCODE_SYSTEM, width: BARCODE_WIDTH = BARCODE_WIDTH.DOT_375, height: number = 162, labelFont: BARCODE_LABEL_FONT = BARCODE_LABEL_FONT.FONT_A, labelPosition: BARCODE_LABEL_POSITION = BARCODE_LABEL_POSITION.BOTTOM, leftSpacing: number = 0): BufferBuilder {
this.buffer.write(Command.GS_w(width)); // width
this.buffer.write(Command.GS_h(height)); // height
this.buffer.write(Command.GS_x(leftSpacing)); // left spacing
this.buffer.write(Command.GS_f(labelFont)); // HRI font
this.buffer.write(Command.GS_H(labelPosition)); // HRI font
this.buffer.write(Command.GS_K(barcodeSystem, data.length)); // data is a string in UTF-8
this.buffer.write(data, 'ascii');
return this;
}
public printQRcode(data: string, version: number = 1, errorCorrectionLevel: QR_EC_LEVEL = QR_EC_LEVEL.H, componentTypes: number = 8): BufferBuilder {
this.buffer.write(Command.ESC_Z(version, errorCorrectionLevel, componentTypes));
this.buffer.writeUInt16LE(data.length); // data is a string in UTF-8
this.buffer.write(data, 'ascii');
return this;
}
public printBitmap(image: number[], width: number, height: number, scale: BITMAP_SCALE = BITMAP_SCALE.NORMAL): BufferBuilder {
//TODO
return this;
}
public printText(text: string): BufferBuilder {
this.buffer.write(text, 'ascii');
return this;
}
public printTextLine(text: string): BufferBuilder {
return this.printText(text).breakLine();
}
public breakLine(lines: number = 0): BufferBuilder {
this.buffer.write(Command.ESC_d(lines));
return this;
}
public lineFeed(): BufferBuilder {
this.buffer.write(Command.LF);
return this;
}
public transmitStatus(statusType: STATUS_TYPE): BufferBuilder {
this.buffer.write(Command.DLE_EOT(statusType));
return this;
}
public build(): number[] {
if (this.defaultSettings) {
this.lineFeed();
this.buffer.write(Command.ESC_init);
}
return this.buffer.flush();
}
/**
* Register Paper Cut Action
* @return BufferBuilder
*/
public paperCut(): BufferBuilder {
this.buffer.write(Command.GS_v(1));
return this;
}
}
export enum UNDERLINE_MODE {
ONE_POINT_OF_COARSE = 49,
TWO_POINTS_OF_COARSE = 50
}
export enum ALIGNMENT {
LEFT = 48,
CENTER = 49,
RIGHT = 50
}
export enum BARCODE_SYSTEM {
UPC_A = 65,
UPC_E = 66,
EAN_13 = 67,
EAN_8 = 68,
CODE_39 = 69,
ITF = 70,
CODABAR = 71,
CODE_93 = 72,
CODE_128 = 73
}
export enum BARCODE_WIDTH {
DOT_250 = 2,
DOT_375 = 3,
DOT_560 = 4,
DOT_625 = 5,
DOT_750 = 6
}
export enum BARCODE_LABEL_FONT {
FONT_A = 48,
FONT_B = 49
}
export enum BARCODE_LABEL_POSITION {
NOT_PRINT = 48,
ABOVE = 49,
BOTTOM = 50,
ABOVE_BOTTOM = 51
}
export enum QR_EC_LEVEL {
L = 0,
M = 1,
Q = 2,
H = 3
}
export enum BITMAP_SCALE {
NORMAL = 48,
DOUBLE_WIDTH = 49,
DOUBLE_HEIGHT = 50,
FOUR_TIMES = 51
}
export enum STATUS_TYPE {
PRINTER_STATUS = 1,
OFFLINE_STATUS = 2,
ERROR_STATUS = 3,
PAPER_ROLL_SENSOR_STATUS = 4
}