jc-printer-sdk-ts 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -0
- package/dist/builder.d.ts +60 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +411 -0
- package/dist/builder.js.map +1 -0
- package/dist/client.d.ts +85 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +352 -0
- package/dist/client.js.map +1 -0
- package/dist/constants.d.ts +186 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +179 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/transport.d.ts +132 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +334 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +310 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +40 -0
- package/src/builder.ts +562 -0
- package/src/client.ts +588 -0
- package/src/constants.ts +210 -0
- package/src/index.ts +5 -0
- package/src/transport.ts +526 -0
- package/src/types.ts +385 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
import { AddressType, AddressTypeCode } from "./constants.js";
|
|
2
|
+
import {
|
|
3
|
+
createAtBitmapDocument,
|
|
4
|
+
createLabelBuilder,
|
|
5
|
+
LabelBuilder,
|
|
6
|
+
} from "./builder.js";
|
|
7
|
+
import type {
|
|
8
|
+
BitmapLike,
|
|
9
|
+
BluetoothDeviceLike,
|
|
10
|
+
BundleLike,
|
|
11
|
+
IAtBitmapDocument,
|
|
12
|
+
LabelBarcodeOptions,
|
|
13
|
+
LabelGraphOptions,
|
|
14
|
+
LabelImageOptions,
|
|
15
|
+
LabelLineOptions,
|
|
16
|
+
LabelQrCodeOptions,
|
|
17
|
+
LabelTextOptions,
|
|
18
|
+
MaybePromise,
|
|
19
|
+
PaperInfo,
|
|
20
|
+
PrintCallback,
|
|
21
|
+
PrintDocument,
|
|
22
|
+
PrinterSdkCallback,
|
|
23
|
+
PrinterAddress,
|
|
24
|
+
PrinterInfo,
|
|
25
|
+
PrinterState,
|
|
26
|
+
Point,
|
|
27
|
+
ScanCallback,
|
|
28
|
+
} from "./types.js";
|
|
29
|
+
import type { PrinterTransport } from "./transport.js";
|
|
30
|
+
import { createMemoryPrinterTransport } from "./transport.js";
|
|
31
|
+
|
|
32
|
+
function clone<T>(value: T): T {
|
|
33
|
+
if (typeof globalThis.structuredClone === "function") {
|
|
34
|
+
return globalThis.structuredClone(value);
|
|
35
|
+
}
|
|
36
|
+
return JSON.parse(JSON.stringify(value)) as T;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function isPromise<T>(value: MaybePromise<T>): value is Promise<T> {
|
|
40
|
+
return !!value && typeof (value as Promise<T>).then === "function";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function resolveBoolean(result: MaybePromise<boolean | void> | undefined): MaybePromise<boolean> {
|
|
44
|
+
if (isPromise(result)) {
|
|
45
|
+
return result.then((value) => (typeof value === "boolean" ? value : true));
|
|
46
|
+
}
|
|
47
|
+
return typeof result === "boolean" ? result : true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function toPrinterAddress(device: BluetoothDeviceLike): PrinterAddress {
|
|
51
|
+
return {
|
|
52
|
+
macAddress: device.address,
|
|
53
|
+
addressType: inferAddressType(device.type),
|
|
54
|
+
shownName: device.name ?? device.address,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function inferAddressType(code?: number): AddressType {
|
|
59
|
+
if (typeof code !== "number") {
|
|
60
|
+
return AddressType.SPP;
|
|
61
|
+
}
|
|
62
|
+
switch (code) {
|
|
63
|
+
case 2:
|
|
64
|
+
case AddressTypeCode.BLE:
|
|
65
|
+
return AddressType.BLE;
|
|
66
|
+
case 3:
|
|
67
|
+
case AddressTypeCode.DUAL:
|
|
68
|
+
return AddressType.DUAL;
|
|
69
|
+
case 1:
|
|
70
|
+
case AddressTypeCode.SPP:
|
|
71
|
+
return AddressType.SPP;
|
|
72
|
+
case AddressTypeCode.WiFi:
|
|
73
|
+
return AddressType.WiFi;
|
|
74
|
+
default:
|
|
75
|
+
return AddressType.SPP;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function encodeAddressType(type: AddressType | undefined): number {
|
|
80
|
+
switch (type) {
|
|
81
|
+
case AddressType.BLE:
|
|
82
|
+
return AddressTypeCode.BLE;
|
|
83
|
+
case AddressType.DUAL:
|
|
84
|
+
return AddressTypeCode.DUAL;
|
|
85
|
+
case AddressType.WiFi:
|
|
86
|
+
return AddressTypeCode.WiFi;
|
|
87
|
+
default:
|
|
88
|
+
return AddressTypeCode.SPP;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function isBluetoothAddressType(type: AddressType | undefined): boolean {
|
|
93
|
+
return type === AddressType.SPP || type === AddressType.BLE || type === AddressType.DUAL;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function createPrinterAddress(
|
|
97
|
+
macAddress: string,
|
|
98
|
+
addressType: AddressType = AddressType.SPP,
|
|
99
|
+
shownName = macAddress,
|
|
100
|
+
): PrinterAddress {
|
|
101
|
+
return { macAddress, addressType, shownName };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function createPrinterAddressFromDevice(
|
|
105
|
+
device: BluetoothDeviceLike,
|
|
106
|
+
): PrinterAddress {
|
|
107
|
+
return toPrinterAddress(device);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function printerStateGroup(state: PrinterState): number {
|
|
111
|
+
switch (state) {
|
|
112
|
+
case "Connecting":
|
|
113
|
+
return 1;
|
|
114
|
+
case "Disconnected":
|
|
115
|
+
return 0;
|
|
116
|
+
default:
|
|
117
|
+
return 2;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function buildCommitInfoPayload(document: PrintDocument): string[] {
|
|
122
|
+
return document.pages.map((page) =>
|
|
123
|
+
JSON.stringify({
|
|
124
|
+
printerImageProcessingInfo: {
|
|
125
|
+
orientation: page.orientation,
|
|
126
|
+
margin: [0, 0, 0, 0],
|
|
127
|
+
printQuantity: page.printQuantity || document.job.totalQuantity || 1,
|
|
128
|
+
horizontalOffset: 0,
|
|
129
|
+
verticalOffset: 0,
|
|
130
|
+
width: page.width,
|
|
131
|
+
height: page.height,
|
|
132
|
+
printMultiple: document.job.printMultiple ?? 1,
|
|
133
|
+
epc: document.job.epc ?? "",
|
|
134
|
+
},
|
|
135
|
+
}),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export class PrinterClient {
|
|
140
|
+
readonly transport: PrinterTransport;
|
|
141
|
+
readonly builder: LabelBuilder;
|
|
142
|
+
|
|
143
|
+
private lastPrintCallback: PrintCallback | undefined;
|
|
144
|
+
private sdkCallback: PrinterSdkCallback | undefined;
|
|
145
|
+
private currentDocument: PrintDocument | null = null;
|
|
146
|
+
private currentAtBitmap: IAtBitmapDocument | null = null;
|
|
147
|
+
private pendingApplication: unknown;
|
|
148
|
+
|
|
149
|
+
constructor(
|
|
150
|
+
transport: PrinterTransport = createMemoryPrinterTransport(),
|
|
151
|
+
sdkCallback?: PrinterSdkCallback,
|
|
152
|
+
) {
|
|
153
|
+
this.transport = transport;
|
|
154
|
+
this.builder = createLabelBuilder("mm");
|
|
155
|
+
if (sdkCallback) {
|
|
156
|
+
this.setSdkCallback(sdkCallback);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
getSdkVersion(): MaybePromise<string> {
|
|
161
|
+
return this.transport.getSdkVersion?.() ?? "ts";
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
init(application: unknown): MaybePromise<boolean> {
|
|
165
|
+
return this.initSdk(application);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
initSdk(application: unknown): MaybePromise<boolean> {
|
|
169
|
+
this.pendingApplication = application;
|
|
170
|
+
return this.transport.initSdk?.(application) ?? this.transport.init?.(application) ?? true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
setSdkCallback(callback?: PrinterSdkCallback): void {
|
|
174
|
+
this.sdkCallback = callback;
|
|
175
|
+
this.transport.setSdkCallback?.(callback);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
getSdkCallback(): PrinterSdkCallback | undefined {
|
|
179
|
+
return this.sdkCallback;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
initDefaultImageLibrarySettings(
|
|
183
|
+
fontDirectory: string,
|
|
184
|
+
extra = "",
|
|
185
|
+
): MaybePromise<number> {
|
|
186
|
+
return this.transport.initDefaultImageLibrarySettings?.(fontDirectory, extra) ?? 0;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
initImageProcessingDefault(
|
|
190
|
+
fontDirectory: string,
|
|
191
|
+
extra = "",
|
|
192
|
+
): MaybePromise<number> {
|
|
193
|
+
return this.transport.initImageProcessingDefault?.(fontDirectory, extra) ?? 0;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
openPrinterByAddress(address: string): MaybePromise<number> {
|
|
197
|
+
return this.transport.openPrinterByAddress?.(address) ?? this.transport.connectBluetoothPrinter?.(address) ?? 0;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
openPrinterSync(address: string): MaybePromise<number> {
|
|
201
|
+
return this.transport.openPrinterSync?.(address) ?? this.openPrinterByAddress(address);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
connectBluetoothPrinter(address: string): MaybePromise<number> {
|
|
205
|
+
return this.transport.connectBluetoothPrinter?.(address) ?? this.transport.openPrinterByAddress?.(address) ?? 0;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
connectWifiPrinter(host: string, port: number): MaybePromise<number> {
|
|
209
|
+
return this.transport.connectWifiPrinter?.(host, port) ?? 0;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
configurationWifi(ssid: string, password: string): MaybePromise<number> {
|
|
213
|
+
return this.transport.configurationWifi?.(ssid, password) ?? 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
getConfigurationWifiName(): MaybePromise<string> {
|
|
217
|
+
return this.transport.getConfigurationWifiName?.() ?? "";
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
scanWifiPrinter(callback: ScanCallback): MaybePromise<void> {
|
|
221
|
+
return this.transport.scanWifiPrinter?.(callback);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
setPrinterAutoShutdownTime(minutes: number): MaybePromise<number> {
|
|
225
|
+
return this.transport.setPrinterAutoShutdownTime?.(minutes) ?? 0;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
setTotalPrintQuantity(quantity: number): MaybePromise<void> {
|
|
229
|
+
this.builder.setTotalPrintQuantity(quantity);
|
|
230
|
+
return this.transport.setTotalPrintQuantity?.(quantity);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
setTotalQuantityOfPrints(quantity: number): MaybePromise<void> {
|
|
234
|
+
this.builder.setTotalQuantityOfPrints(quantity);
|
|
235
|
+
return this.transport.setTotalQuantityOfPrints?.(quantity);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
startPrintJob(
|
|
239
|
+
density: number,
|
|
240
|
+
paperType: number,
|
|
241
|
+
printMode: number,
|
|
242
|
+
callback: PrintCallback,
|
|
243
|
+
): MaybePromise<void> {
|
|
244
|
+
this.lastPrintCallback = callback;
|
|
245
|
+
this.builder.startJob(density, paperType, printMode);
|
|
246
|
+
this.currentDocument = null;
|
|
247
|
+
return this.transport.startPrintJob?.(density, paperType, printMode, callback);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
commitData(jsonPages: string[], infoPages: string[]): MaybePromise<void> {
|
|
251
|
+
this.currentDocument = clone(this.builder.toDocument());
|
|
252
|
+
return this.transport.commitData?.(jsonPages, infoPages);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
commitDocument(document: PrintDocument): MaybePromise<void> {
|
|
256
|
+
const infoPages = buildCommitInfoPayload(document);
|
|
257
|
+
this.currentDocument = clone(document);
|
|
258
|
+
if (this.transport.commitDocument) {
|
|
259
|
+
return this.transport.commitDocument(document, infoPages);
|
|
260
|
+
}
|
|
261
|
+
return this.transport.commitData?.(
|
|
262
|
+
document.pages.map((page) => JSON.stringify(page)),
|
|
263
|
+
infoPages,
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
endJob(): MaybePromise<boolean> {
|
|
268
|
+
this.lastPrintCallback = undefined;
|
|
269
|
+
return resolveBoolean(this.transport.endJob?.());
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
endPrintJob(): MaybePromise<boolean> {
|
|
273
|
+
this.lastPrintCallback = undefined;
|
|
274
|
+
return resolveBoolean(this.transport.endPrintJob?.());
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
cancelJob(): MaybePromise<boolean> {
|
|
278
|
+
this.builder.abortJob();
|
|
279
|
+
this.currentDocument = null;
|
|
280
|
+
this.lastPrintCallback = undefined;
|
|
281
|
+
return resolveBoolean(this.transport.cancelJob?.());
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
cancel(): MaybePromise<void> {
|
|
285
|
+
this.builder.abortJob();
|
|
286
|
+
this.currentDocument = null;
|
|
287
|
+
this.lastPrintCallback = undefined;
|
|
288
|
+
if (this.transport.cancel) {
|
|
289
|
+
return this.transport.cancel();
|
|
290
|
+
}
|
|
291
|
+
const result = this.transport.cancelJob?.();
|
|
292
|
+
if (isPromise(result)) {
|
|
293
|
+
return result.then(() => undefined);
|
|
294
|
+
}
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
reopenPrinter(): MaybePromise<boolean> {
|
|
299
|
+
if (this.transport.reopenPrinter) {
|
|
300
|
+
return resolveBoolean(this.transport.reopenPrinter());
|
|
301
|
+
}
|
|
302
|
+
if (this.transport.reopenPrinterSync) {
|
|
303
|
+
return resolveBoolean(this.transport.reopenPrinterSync());
|
|
304
|
+
}
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
reopenPrinterSync(): MaybePromise<boolean> {
|
|
309
|
+
if (this.transport.reopenPrinterSync) {
|
|
310
|
+
return resolveBoolean(this.transport.reopenPrinterSync());
|
|
311
|
+
}
|
|
312
|
+
if (this.transport.reopenPrinter) {
|
|
313
|
+
return resolveBoolean(this.transport.reopenPrinter());
|
|
314
|
+
}
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
close(): MaybePromise<void> {
|
|
319
|
+
this.builder.abortJob();
|
|
320
|
+
this.currentDocument = null;
|
|
321
|
+
this.lastPrintCallback = undefined;
|
|
322
|
+
return this.transport.close?.();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
quit(): MaybePromise<void> {
|
|
326
|
+
this.builder.abortJob();
|
|
327
|
+
this.currentDocument = null;
|
|
328
|
+
this.lastPrintCallback = undefined;
|
|
329
|
+
if (this.transport.quit) {
|
|
330
|
+
return this.transport.quit();
|
|
331
|
+
}
|
|
332
|
+
return this.transport.close?.();
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
printBitmap(bitmap: BitmapLike, bundle: BundleLike = {}): MaybePromise<boolean> {
|
|
336
|
+
return this.transport.printBitmap?.(bitmap, bundle) ?? true;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
printATBitmap(
|
|
340
|
+
bitmap: IAtBitmapDocument,
|
|
341
|
+
bundle: BundleLike = {},
|
|
342
|
+
): MaybePromise<boolean> {
|
|
343
|
+
this.currentAtBitmap = clone(bitmap);
|
|
344
|
+
return this.transport.printATBitmap?.(bitmap, bundle) ?? true;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
getPaperInfo(): MaybePromise<PaperInfo | null> {
|
|
348
|
+
return this.transport.getPaperInfo?.() ?? null;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
isConnection(): MaybePromise<number> {
|
|
352
|
+
return this.transport.isConnection?.() ?? -1;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
getPrintScaleMultiplier(): MaybePromise<number> {
|
|
356
|
+
return this.transport.getPrintScaleMultiplier?.() ?? this.builder.getPrintMultiple();
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
measureFontHeight(
|
|
360
|
+
text: string,
|
|
361
|
+
x: number,
|
|
362
|
+
y: number,
|
|
363
|
+
width: number,
|
|
364
|
+
height: number,
|
|
365
|
+
rotation: number,
|
|
366
|
+
fontSize: number,
|
|
367
|
+
): MaybePromise<number> {
|
|
368
|
+
return this.transport.measureFontHeight?.(
|
|
369
|
+
text,
|
|
370
|
+
x,
|
|
371
|
+
y,
|
|
372
|
+
width,
|
|
373
|
+
height,
|
|
374
|
+
rotation,
|
|
375
|
+
fontSize,
|
|
376
|
+
) ?? this.builder.measureFontHeight(text, x, y, width, height, rotation, fontSize);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
getStrPrintSize(text: string, fontStyle: number, fontSize: number): Point {
|
|
380
|
+
return this.builder.getStrPrintSize(text, fontStyle, fontSize);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
getMin1DBarcode(text: string, codeType: number): BitmapLike {
|
|
384
|
+
return this.builder.getMin1DBarcode(text, codeType);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
getMin2DQRCode(text: string): BitmapLike {
|
|
388
|
+
return this.builder.getMin2DQRCode(text);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
getPrinterName(): MaybePromise<string> {
|
|
392
|
+
return this.transport.getPrinterName?.() ?? "";
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
getPrinterInfo(): MaybePromise<PrinterInfo | null> {
|
|
396
|
+
return this.transport.getPrinterInfo?.() ?? null;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
getPrinterState(): MaybePromise<PrinterState> {
|
|
400
|
+
return this.transport.getPrinterState?.() ?? "Disconnected";
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
waitPrinterState(state: PrinterState, timeoutMs: number): MaybePromise<boolean> {
|
|
404
|
+
return this.transport.waitPrinterState?.(state, timeoutMs) ?? false;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
drawEmptyLabel(
|
|
408
|
+
width: number,
|
|
409
|
+
height: number,
|
|
410
|
+
orientation: number,
|
|
411
|
+
fonts: string | string[],
|
|
412
|
+
): void {
|
|
413
|
+
this.builder.drawEmptyLabel(width, height, orientation, fonts);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
drawLabelText(
|
|
417
|
+
x: number,
|
|
418
|
+
y: number,
|
|
419
|
+
width: number,
|
|
420
|
+
height: number,
|
|
421
|
+
text: string,
|
|
422
|
+
fontName: string,
|
|
423
|
+
fontSize: number,
|
|
424
|
+
options: LabelTextOptions = {},
|
|
425
|
+
): void {
|
|
426
|
+
this.builder.drawLabelText(x, y, width, height, text, fontName, fontSize, options);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
drawLabelBarCode(
|
|
430
|
+
x: number,
|
|
431
|
+
y: number,
|
|
432
|
+
width: number,
|
|
433
|
+
height: number,
|
|
434
|
+
codeType: number,
|
|
435
|
+
text: string,
|
|
436
|
+
options: LabelBarcodeOptions = {},
|
|
437
|
+
): void {
|
|
438
|
+
this.builder.drawLabelBarCode(x, y, width, height, codeType, text, options);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
drawLabelQrCode(
|
|
442
|
+
x: number,
|
|
443
|
+
y: number,
|
|
444
|
+
width: number,
|
|
445
|
+
height: number,
|
|
446
|
+
text: string,
|
|
447
|
+
options: LabelQrCodeOptions = {},
|
|
448
|
+
): void {
|
|
449
|
+
this.builder.drawLabelQrCode(x, y, width, height, text, options);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
drawLabelGraph(
|
|
453
|
+
x: number,
|
|
454
|
+
y: number,
|
|
455
|
+
width: number,
|
|
456
|
+
height: number,
|
|
457
|
+
graphType: number,
|
|
458
|
+
options: LabelGraphOptions = {},
|
|
459
|
+
): void {
|
|
460
|
+
this.builder.drawLabelGraph(x, y, width, height, graphType, options);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
drawLabelImage(
|
|
464
|
+
source: string | BitmapLike,
|
|
465
|
+
x: number,
|
|
466
|
+
y: number,
|
|
467
|
+
width: number,
|
|
468
|
+
height: number,
|
|
469
|
+
options: LabelImageOptions = {},
|
|
470
|
+
): void {
|
|
471
|
+
this.builder.drawLabelImage(source, x, y, width, height, options);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
drawLabelLine(
|
|
475
|
+
x: number,
|
|
476
|
+
y: number,
|
|
477
|
+
width: number,
|
|
478
|
+
height: number,
|
|
479
|
+
options: LabelLineOptions = {},
|
|
480
|
+
): void {
|
|
481
|
+
this.builder.drawLabelLine(x, y, width, height, options);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
generateLabelJson(): Uint8Array {
|
|
485
|
+
return this.builder.generateLabelJson();
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
commitImageData(
|
|
489
|
+
orientation: number,
|
|
490
|
+
bitmap: BitmapLike,
|
|
491
|
+
width: number,
|
|
492
|
+
height: number,
|
|
493
|
+
printQuantity: number,
|
|
494
|
+
marginLeft: number,
|
|
495
|
+
marginTop: number,
|
|
496
|
+
marginRight: number,
|
|
497
|
+
marginBottom: number,
|
|
498
|
+
rfid: string,
|
|
499
|
+
): MaybePromise<void> {
|
|
500
|
+
this.builder.commitImageData(
|
|
501
|
+
orientation,
|
|
502
|
+
bitmap,
|
|
503
|
+
width,
|
|
504
|
+
height,
|
|
505
|
+
printQuantity,
|
|
506
|
+
marginLeft,
|
|
507
|
+
marginTop,
|
|
508
|
+
marginRight,
|
|
509
|
+
marginBottom,
|
|
510
|
+
rfid,
|
|
511
|
+
);
|
|
512
|
+
this.currentDocument = clone(this.builder.toDocument());
|
|
513
|
+
return undefined;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
generatePreviewImage(json: string, info: string): MaybePromise<BitmapLike> {
|
|
517
|
+
const preview = this.transport.generatePreviewImage?.(json, info);
|
|
518
|
+
return preview ?? this.builder.generatePreviewImage(json, info);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
static generatePrintPreviewImage(
|
|
522
|
+
json: string,
|
|
523
|
+
widthMm: number,
|
|
524
|
+
heightMm: number,
|
|
525
|
+
orientation: number,
|
|
526
|
+
): BitmapLike {
|
|
527
|
+
return {
|
|
528
|
+
width: widthMm,
|
|
529
|
+
height: heightMm,
|
|
530
|
+
description: "print-preview",
|
|
531
|
+
meta: { json, orientation },
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
getMultiple(): MaybePromise<number> {
|
|
536
|
+
return this.transport.getPrintScaleMultiplier?.() ?? this.builder.getMultiple();
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
getDocument(): PrintDocument {
|
|
540
|
+
return this.builder.toDocument();
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
getLastCommittedDocument(): PrintDocument | null {
|
|
544
|
+
return this.currentDocument ? clone(this.currentDocument) : null;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
getLastCommittedAtBitmap(): IAtBitmapDocument | null {
|
|
548
|
+
return this.currentAtBitmap ? clone(this.currentAtBitmap) : null;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
createAtBitmapDocument(job: PrintDocument["job"] = {}): IAtBitmapDocument {
|
|
552
|
+
return createAtBitmapDocument(job);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
createLabelBuilder(): LabelBuilder {
|
|
556
|
+
return createLabelBuilder("mm");
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
getPendingApplication(): unknown {
|
|
560
|
+
return this.pendingApplication;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
export class JCPrintApi extends PrinterClient {
|
|
565
|
+
private static instance?: JCPrintApi;
|
|
566
|
+
|
|
567
|
+
static getInstance(
|
|
568
|
+
callback?: PrinterSdkCallback,
|
|
569
|
+
transport?: PrinterTransport,
|
|
570
|
+
): JCPrintApi {
|
|
571
|
+
if (!JCPrintApi.instance) {
|
|
572
|
+
JCPrintApi.instance = new JCPrintApi(
|
|
573
|
+
transport ?? createMemoryPrinterTransport(),
|
|
574
|
+
callback,
|
|
575
|
+
);
|
|
576
|
+
} else if (callback) {
|
|
577
|
+
JCPrintApi.instance.setSdkCallback(callback);
|
|
578
|
+
}
|
|
579
|
+
return JCPrintApi.instance;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
export function createPrinterClient(
|
|
584
|
+
transport: PrinterTransport = createMemoryPrinterTransport(),
|
|
585
|
+
sdkCallback?: PrinterSdkCallback,
|
|
586
|
+
): PrinterClient {
|
|
587
|
+
return new PrinterClient(transport, sdkCallback);
|
|
588
|
+
}
|