-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.js
More file actions
598 lines (537 loc) · 22.6 KB
/
printer.js
File metadata and controls
598 lines (537 loc) · 22.6 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// printer.js - safe printer module for D30 printing
export const printer = {
device: null,
server: null,
characteristic: null,
connected: false,
batteryChar: null,
settings: {
labelWidthMM: 12,
labelLengthMM: 40,
dpiPerMM: 8,
protocol: "phomemo_raw",
fontFamily: "Inter, sans-serif"
},
logs: []
};
function pushLog(msg) {
printer.logs.push(msg);
console.log(msg);
const la = document.getElementById('logArea');
if (la) { la.value += `[${new Date().toLocaleTimeString()}] ${msg}\n`; la.scrollTop = la.scrollHeight; }
}
export async function connect() {
try {
pushLog("Requesting Bluetooth device...");
const device = await navigator.bluetooth.requestDevice({
acceptAllDevices: true,
optionalServices: [
'0000ff00-0000-1000-8000-00805f9b34fb',
'0000ff01-0000-1000-8000-00805f9b34fb',
'0000ff02-0000-1000-8000-00805f9b34fb',
'battery_service'
]
});
printer.device = device;
device.addEventListener('gattserverdisconnected', () => {
pushLog("Device disconnected");
printer.connected = false;
printer.characteristic = null;
printer.batteryChar = null;
updateConnUI(false);
});
printer.server = await device.gatt.connect();
pushLog("GATT connected");
const services = await printer.server.getPrimaryServices();
for (const s of services) {
try {
if (s.uuid.includes('180f')) continue;
const chars = await s.getCharacteristics();
for (const c of chars) {
if (c.properties.write || c.properties.writeWithoutResponse) {
printer.characteristic = c;
printer.connected = true;
pushLog(`Using char ${c.uuid}`);
updateConnUI(true);
}
}
} catch(e) {}
}
if (!printer.connected) {
pushLog("No writable characteristic found");
return;
}
try {
const battService = await printer.server.getPrimaryService('battery_service');
const battChar = await battService.getCharacteristic('battery_level');
printer.batteryChar = battChar;
await readBattery();
if (battChar.properties.notify) {
await battChar.startNotifications();
battChar.addEventListener('characteristicvaluechanged', readBattery);
}
} catch(e) {}
} catch (e) {
if (!e.toString().includes("User cancelled")) {
pushLog("Connect failed: " + e);
}
updateConnUI(false);
}
}
async function readBattery(e) {
try {
const val = e ? e.target.value : await printer.batteryChar.readValue();
const pct = val.getUint8(0);
const el = document.getElementById('battPercent');
const wrap = document.getElementById('batteryLevel');
if (el && wrap) {
el.textContent = pct + '%';
wrap.style.display = 'flex';
}
} catch(e) {}
}
export async function disconnect() {
if (printer.device && printer.device.gatt && printer.device.gatt.connected) {
printer.device.gatt.disconnect();
printer.connected = false;
printer.characteristic = null;
printer.batteryChar = null;
pushLog("Disconnected");
updateConnUI(false);
}
}
function updateConnUI(connected) {
const el = document.getElementById("connectionStatus");
if (el) el.textContent = connected ? "Connected" : "Not connected";
const btn = document.getElementById("connectBtn");
if (btn) btn.textContent = connected ? "Disconnect" : "Connect";
const batt = document.getElementById("batteryLevel");
if (!connected && batt) batt.style.display = 'none';
}
export function makeLabelCanvas(labelWidthMM, labelLengthMM, dpi, invert=false) {
const widthPx = Math.round(labelWidthMM * dpi);
const heightPx = Math.round(labelLengthMM * dpi);
const bytesPerRow = Math.ceil(widthPx / 8);
const alignedWidth = bytesPerRow * 8;
const canvas = document.createElement('canvas');
canvas.width = alignedWidth;
canvas.height = heightPx;
const ctx = canvas.getContext('2d', { willReadFrequently: true });
ctx.fillStyle = invert ? "#000000" : "#FFFFFF";
ctx.fillRect(0, 0, canvas.width, canvas.height);
return { canvas, ctx, bytesPerRow, widthPx: alignedWidth, heightPx };
}
function drawFrame(ctx, width, height, style, invert) {
if (!style || style === 'none') return;
const marginX = 16; const marginY = 8;
const x = marginX; const y = marginY;
const w = width - (marginX * 2); const h = height - (marginY * 2);
ctx.save();
ctx.strokeStyle = invert ? "#FFFFFF" : "#000000";
ctx.fillStyle = invert ? "#FFFFFF" : "#000000";
ctx.lineWidth = 4;
if (style === 'simple') ctx.strokeRect(x, y, w, h);
else if (style === 'thick') { ctx.lineWidth = 8; ctx.strokeRect(x, y, w, h); }
else if (style === 'rounded') { ctx.beginPath(); ctx.roundRect(x, y, w, h, 20); ctx.stroke(); }
else if (style === 'double') { ctx.lineWidth = 2; ctx.strokeRect(x, y, w, h); ctx.strokeRect(x + 6, y + 6, w - 12, h - 12); }
else if (style === 'dashed') { ctx.setLineDash([15, 10]); ctx.strokeRect(x, y, w, h); }
else if (style === 'ticket') {
const r = 15; ctx.beginPath();
ctx.moveTo(x, y); ctx.lineTo(x + w, y); ctx.lineTo(x + w, y + h/2 - r);
ctx.arc(x + w, y + h/2, r, 1.5 * Math.PI, 0.5 * Math.PI, true);
ctx.lineTo(x + w, y + h); ctx.lineTo(x, y + h); ctx.lineTo(x, y + h/2 + r);
ctx.arc(x, y + h/2, r, 0.5 * Math.PI, 1.5 * Math.PI, true);
ctx.closePath(); ctx.stroke();
} else if (style === 'cut_corners' || style === 'cut_corners_double') {
const r = 15;
const drawPath = (inset) => {
const ix = x + inset; const iy = y + inset; const iw = w - 2*inset; const ih = h - 2*inset;
ctx.beginPath(); ctx.moveTo(ix + r, iy); ctx.lineTo(ix + iw - r, iy);
ctx.arc(ix + iw, iy, r, Math.PI, 0.5*Math.PI, true);
ctx.lineTo(ix + iw, iy + ih - r); ctx.arc(ix + iw, iy + ih, r, 1.5*Math.PI, Math.PI, true);
ctx.lineTo(ix + r, iy + ih); ctx.arc(ix, iy + ih, r, 0, 1.5*Math.PI, true);
ctx.lineTo(ix, iy + r); ctx.arc(ix, iy, r, 0.5*Math.PI, 0, true);
ctx.closePath(); ctx.stroke();
};
if (style === 'cut_corners') { ctx.lineWidth = 4; drawPath(0); }
else { ctx.lineWidth = 6; drawPath(0); ctx.lineWidth = 2; drawPath(6); }
} else if (style === 'brackets') {
ctx.lineWidth = 6; const len = Math.min(w, h) / 3;
ctx.beginPath();
ctx.moveTo(x, y + len); ctx.lineTo(x, y); ctx.lineTo(x + len, y);
ctx.moveTo(x + w - len, y); ctx.lineTo(x + w, y); ctx.lineTo(x + w, y + len);
ctx.moveTo(x + w, y + h - len); ctx.lineTo(x + w, y + h); ctx.lineTo(x + w - len, y + h);
ctx.moveTo(x + len, y + h); ctx.lineTo(x, y + h); ctx.lineTo(x, y + h - len);
ctx.stroke();
}
ctx.restore();
}
export function renderTextCanvas(text, fontSize=40, alignment='center', invert=false, labelWidthMM=12, labelLengthMM=40, dpi=8, fontFamily='Inter, sans-serif', frameStyle='none') {
const { canvas, ctx, bytesPerRow, widthPx, heightPx } = makeLabelCanvas(labelWidthMM, labelLengthMM, dpi, invert);
ctx.save();
ctx.translate(0, canvas.height);
ctx.rotate(-Math.PI / 2);
drawFrame(ctx, heightPx, widthPx, frameStyle, invert);
ctx.fillStyle = invert ? "#FFFFFF" : "#000000";
ctx.font = `${fontFamily.includes('bold') ? 'bold ' : ''}${fontSize}px ${fontFamily.replace('bold','').trim()}`;
ctx.textBaseline = "middle";
const lines = text.split('\n');
const lineHeight = fontSize * 1.0;
const totalBlockHeight = lines.length * lineHeight;
let x = 0;
if (alignment === 'left') { ctx.textAlign = "left"; x = 10; }
else if (alignment === 'right') { ctx.textAlign = "right"; x = heightPx - 10; }
else { ctx.textAlign = "center"; x = heightPx / 2; }
const startY = (widthPx - totalBlockHeight) / 2 + 2;
lines.forEach((line, i) => {
const y = startY + (i * lineHeight) + (lineHeight / 2);
ctx.fillText(line, x, y);
});
ctx.restore();
return { canvas, bytesPerRow, widthPx, heightPx, bakedInvert: true };
}
export function renderImageCanvas(image, threshold=128, invert=false, labelWidthMM=12, labelLengthMM=40, dpi=8, dither=false, rotation=0, scalePct=100) {
const { canvas, ctx, bytesPerRow, widthPx, heightPx } = makeLabelCanvas(labelWidthMM, labelLengthMM, dpi, false);
let srcImage = image;
if (rotation !== 0) {
const rotCanvas = document.createElement('canvas');
if (rotation % 180 !== 0) { rotCanvas.width = image.height; rotCanvas.height = image.width; }
else { rotCanvas.width = image.width; rotCanvas.height = image.height; }
const rctx = rotCanvas.getContext('2d');
rctx.translate(rotCanvas.width/2, rotCanvas.height/2);
rctx.rotate(rotation * Math.PI / 180);
rctx.drawImage(image, -image.width/2, -image.height/2);
srcImage = rotCanvas;
}
let ratio = Math.min(canvas.width / srcImage.width, canvas.height / srcImage.height);
ratio *= (scalePct / 100);
const dw = Math.floor(srcImage.width * ratio);
const dh = Math.floor(srcImage.height * ratio);
const tempCv = document.createElement('canvas');
tempCv.width = dw; tempCv.height = dh;
const tCtx = tempCv.getContext('2d');
tCtx.drawImage(srcImage, 0, 0, dw, dh);
const iData = tCtx.getImageData(0, 0, dw, dh);
const d = iData.data;
if (dither) {
for (let i = 0; i < d.length; i += 4) {
const gray = 0.299 * d[i] + 0.587 * d[i + 1] + 0.114 * d[i + 2];
d[i] = d[i+1] = d[i+2] = gray;
}
for (let y = 0; y < dh; y++) {
for (let x = 0; x < dw; x++) {
const i = (y * dw + x) * 4; const oldPixel = d[i];
const newPixel = oldPixel < 128 ? 0 : 255;
d[i] = d[i+1] = d[i+2] = newPixel;
const quantError = oldPixel - newPixel;
if (x + 1 < dw) d[((y * dw + x + 1) * 4)] += quantError * 7 / 16;
if (x - 1 >= 0 && y + 1 < dh) d[(( (y + 1) * dw + x - 1) * 4)] += quantError * 3 / 16;
if (y + 1 < dh) d[(( (y + 1) * dw + x) * 4)] += quantError * 5 / 16;
if (x + 1 < dw && y + 1 < dh) d[(((y + 1) * dw + x + 1) * 4)] += quantError * 1 / 16;
}
}
if (invert) {
for (let i = 0; i < d.length; i += 4) { const v = d[i] === 0 ? 255 : 0; d[i] = d[i+1] = d[i+2] = v; }
}
} else {
for (let i = 0; i < d.length; i += 4) {
const gray = 0.299 * d[i] + 0.587 * d[i + 1] + 0.114 * d[i + 2];
let isDark = gray < threshold; let finalVal = 255;
if (!invert) { if (isDark) finalVal = 0; } else { if (!isDark) finalVal = 0; }
d[i] = d[i + 1] = d[i + 2] = finalVal; d[i + 3] = 255;
}
}
tCtx.putImageData(iData, 0, 0);
ctx.save();
ctx.translate(0, canvas.height);
ctx.rotate(-Math.PI / 2);
ctx.drawImage(tempCv, (heightPx - dw)/2, (widthPx - dh)/2);
ctx.restore();
return { canvas, bytesPerRow, widthPx, heightPx, bakedInvert: true };
}
export function renderBarcodeCanvas(value, type='CODE128', scale=2, labelWidthMM=12, labelLengthMM=40, dpi=8) {
const { canvas, ctx, bytesPerRow, widthPx, heightPx } = makeLabelCanvas(labelWidthMM, labelLengthMM, dpi, false);
const bcCanvas = document.createElement('canvas');
try {
JsBarcode(bcCanvas, value, { format: type, displayValue: false, width: scale, margin: 0 });
const ratio = Math.min(heightPx / bcCanvas.width, widthPx / bcCanvas.height);
const dw = bcCanvas.width * ratio;
const dh = bcCanvas.height * ratio;
ctx.save();
ctx.translate(0, heightPx);
ctx.rotate(-Math.PI / 2);
ctx.drawImage(bcCanvas, (heightPx - dw)/2, (widthPx - dh)/2, dw, dh);
ctx.restore();
} catch (e) {
pushLog("Barcode render error: " + e);
}
return { canvas, bytesPerRow, widthPx, heightPx };
}
export async function renderQRCanvas(value, typeOrSize='M', size=70, labelWidthMM=12, labelLengthMM=40, dpi=8) {
const { canvas, ctx, bytesPerRow, widthPx, heightPx } = makeLabelCanvas(labelWidthMM, labelLengthMM, dpi, false);
const qrCanvas = document.createElement('canvas');
if (typeOrSize === 'AZTEC') {
try {
bwipjs.toCanvas(qrCanvas, { bcid: 'azteccode', text: value, scale: 4, includetext: false });
} catch (e) { console.warn('Aztec error', e); }
} else {
await QRCode.toCanvas(qrCanvas, value, { errorCorrectionLevel: typeOrSize, margin: 0 });
}
const availableW = heightPx;
const availableH = widthPx;
const targetSize = size;
const fitScale = Math.min(availableW / qrCanvas.width, availableH / qrCanvas.height);
const requestedScale = targetSize / qrCanvas.width;
const scale = Math.min(fitScale, requestedScale);
const dw = qrCanvas.width * scale;
const dh = qrCanvas.height * scale;
ctx.save();
ctx.translate(0, heightPx);
ctx.rotate(-Math.PI / 2);
ctx.drawImage(qrCanvas, (availableW - dw)/2, (availableH - dh)/2, dw, dh);
ctx.restore();
return { canvas, bytesPerRow, widthPx, heightPx };
}
// --- Updated Combined Logic ---
export async function renderCombinedCanvas(data, labelWidthMM, labelLengthMM, dpi) {
const { canvas, ctx, bytesPerRow, widthPx, heightPx } = makeLabelCanvas(labelWidthMM, labelLengthMM, dpi, false);
const mx = 16;
const visW = heightPx - (2*mx);
const visH = widthPx;
const slots = { left: false, right: false };
['text','image','barcode','qr'].forEach(k => {
if (data[k].enabled) {
if (data[k].pos === 'left') slots.left = true;
if (data[k].pos === 'right') slots.right = true;
}
});
const lx = mx;
const lw = slots.left ? visW * 0.25 : 0;
const rw = slots.right ? visW * 0.25 : 0;
const rx = mx + visW - rw;
const sx = lx + lw;
const sw = visW - lw - rw;
// 20% Vertical Split for Top/Bottom
const vSplit = 0.20;
const getRect = (pos) => {
if (pos === 'left') return { x: lx, y: 0, w: lw, h: visH };
if (pos === 'right') return { x: rx, y: 0, w: rw, h: visH };
if (pos === 'top') return { x: sx, y: 0, w: sw, h: visH * vSplit };
if (pos === 'bottom') return { x: sx, y: visH * (1 - vSplit), w: sw, h: visH * vSplit };
let topUsed = false; let botUsed = false;
['text','image','barcode','qr'].forEach(k => {
if (data[k].enabled) {
if (data[k].pos === 'top') topUsed = true;
if (data[k].pos === 'bottom') botUsed = true;
}
});
const cy = topUsed ? visH * vSplit : 0;
// Original Center Height calculation
let ch = visH - (topUsed ? visH*vSplit : 0) - (botUsed ? visH*vSplit : 0);
// FIX: If Bottom is used, expand center height by 5% downwards
// This reduces the visual gap without changing the Bottom element's rect.
if (botUsed) ch += visH * 0.05;
if (pos === 'center') return { x: sx, y: cy, w: sw, h: ch };
return { x: 0, y: 0, w: 0, h: 0 };
};
ctx.save();
ctx.translate(0, canvas.height);
ctx.rotate(-Math.PI / 2);
const positions = ['center', 'left', 'right', 'top', 'bottom'];
// LAYER 1: IMAGES
for (const pos of positions) {
const rect = getRect(pos);
if (rect.w <= 0 || rect.h <= 0) continue;
if (data.image.enabled && data.image.pos === pos && data.image.img) {
let img = data.image.img;
if (data.image.rotation !== 0) {
const rotCanvas = document.createElement('canvas');
if (data.image.rotation % 180 !== 0) { rotCanvas.width = img.height; rotCanvas.height = img.width; }
else { rotCanvas.width = img.width; rotCanvas.height = img.height; }
const rctx = rotCanvas.getContext('2d');
rctx.translate(rotCanvas.width/2, rotCanvas.height/2);
rctx.rotate(data.image.rotation * Math.PI / 180);
rctx.drawImage(img, -img.width/2, -img.height/2);
img = rotCanvas;
}
let ratio = Math.min(rect.w / img.width, rect.h / img.height);
ratio *= (data.image.scalePct / 100);
const dw = Math.floor(img.width * ratio);
const dh = Math.floor(img.height * ratio);
const tCv = document.createElement('canvas');
tCv.width = dw; tCv.height = dh;
const tCtx = tCv.getContext('2d');
tCtx.drawImage(img, 0, 0, dw, dh);
const iData = tCtx.getImageData(0, 0, dw, dh);
const dd = iData.data;
if (data.image.dither) {
for (let i=0; i<dd.length; i+=4) {
const g = 0.299*dd[i] + 0.587*dd[i+1] + 0.114*dd[i+2];
dd[i]=dd[i+1]=dd[i+2]=g;
}
for(let y=0; y<dh; y++){
for(let x=0; x<dw; x++){
const i = (y*dw + x)*4; const oldP = dd[i];
const newP = oldP < 128 ? 0 : 255;
dd[i]=dd[i+1]=dd[i+2]=newP;
const err = oldP - newP;
if(x+1<dw) dd[((y*dw+x+1)*4)] += err*7/16;
if(x-1>=0 && y+1<dh) dd[(((y+1)*dw+x-1)*4)] += err*3/16;
if(y+1<dh) dd[(((y+1)*dw+x)*4)] += err*5/16;
if(x+1<dw && y+1<dh) dd[(((y+1)*dw+x+1)*4)] += err*1/16;
}
}
} else {
for(let i=0; i<dd.length; i+=4) {
const g = 0.299*dd[i] + 0.587*dd[i+1] + 0.114*dd[i+2];
const v = g < data.image.threshold ? 0 : 255;
dd[i]=dd[i+1]=dd[i+2]=v;
}
}
if (data.image.invert) {
for(let i=0; i<dd.length; i+=4) {
const v = dd[i] === 0 ? 255 : 0;
dd[i]=dd[i+1]=dd[i+2]=v;
}
}
tCtx.putImageData(iData, 0, 0);
ctx.drawImage(tCv, rect.x + (rect.w - dw)/2, rect.y + (rect.h - dh)/2);
}
}
// LAYER 2: CONTENT
for (const pos of positions) {
const rect = getRect(pos);
if (rect.w <= 0 || rect.h <= 0) continue;
if (data.barcode.enabled && data.barcode.pos === pos) {
const bcCanvas = document.createElement('canvas');
try {
JsBarcode(bcCanvas, data.barcode.val, { format: 'CODE128', displayValue: false, width: data.barcode.scale, margin:0 });
const ratio = Math.min(rect.w / bcCanvas.width, rect.h / bcCanvas.height);
const dw = bcCanvas.width * ratio;
const dh = bcCanvas.height * ratio;
ctx.drawImage(bcCanvas, rect.x + (rect.w - dw)/2, rect.y + (rect.h - dh)/2, dw, dh);
} catch(e) {}
}
if (data.qr.enabled && data.qr.pos === pos) {
const qCanvas = document.createElement('canvas');
if (data.qr.type === 'AZTEC') {
try { bwipjs.toCanvas(qCanvas, { bcid: 'azteccode', text: data.qr.val, scale: 4, includetext: false }); } catch(e){}
} else {
await QRCode.toCanvas(qCanvas, data.qr.val, { errorCorrectionLevel: data.qr.type, margin: 0 });
}
const targetSize = data.qr.size;
const fitScale = Math.min(rect.w / qCanvas.width, rect.h / qCanvas.height);
const reqScale = targetSize / qCanvas.width;
const scale = Math.min(fitScale, reqScale);
const dw = qCanvas.width * scale;
const dh = qCanvas.height * scale;
ctx.drawImage(qCanvas, rect.x + (rect.w - dw)/2, rect.y + (rect.h - dh)/2, dw, dh);
}
if (data.text.enabled && data.text.pos === pos) {
ctx.save();
ctx.beginPath(); ctx.rect(rect.x, rect.y, rect.w, rect.h); ctx.clip();
ctx.fillStyle = "#000000";
ctx.font = `${data.text.bold?'bold ':''}${data.text.fontSize}px ${data.text.fontFamily}`;
ctx.textBaseline = "middle"; ctx.textAlign = "center";
// Added +2px offset
ctx.fillText(data.text.val, rect.x + rect.w/2, rect.y + rect.h/2 + 2);
ctx.restore();
}
}
ctx.restore();
return { canvas, bytesPerRow, widthPx, heightPx, bakedInvert: true };
}
export function canvasToBitmap(canvas, bytesPerRow, invert=false) {
const ctx = canvas.getContext('2d', { willReadFrequently: true });
const w = canvas.width;
const h = canvas.height;
const img = ctx.getImageData(0, 0, w, h).data;
const out = new Uint8Array(bytesPerRow * h);
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const idx = (y * w + x) * 4;
const r = img[idx];
let isBlack = r < 128;
if (invert) isBlack = !isBlack;
if (isBlack) out[y * bytesPerRow + (x >> 3)] |= (0x80 >> (x & 7));
}
}
return out;
}
export function buildPacketFromBitmap(bitmap, bytesPerRow, heightPx) {
const reset = new Uint8Array([0x1B, 0x40]);
const header = new Uint8Array([0x1D, 0x76, 0x30, 0x00, bytesPerRow & 0xff, (bytesPerRow >> 8) & 0xff, heightPx & 0xff, (heightPx >> 8) & 0xff]);
const footer = new Uint8Array([0x1B, 0x64, 0x00]);
const out = new Uint8Array(reset.length + header.length + bitmap.length + footer.length);
let p = 0;
out.set(reset, p); p += reset.length;
out.set(header, p); p += header.length;
out.set(bitmap, p); p += bitmap.length;
out.set(footer, p);
return out;
}
async function writeChunks(u8) {
if (!printer.characteristic) throw new Error("Not connected");
const CHUNK = 128;
for (let i = 0; i < u8.length; i += CHUNK) {
const slice = u8.slice(i, i + CHUNK);
if (printer.characteristic.properties.write) {
await printer.characteristic.writeValue(slice);
} else {
await printer.characteristic.writeValueWithoutResponse(slice);
}
await new Promise(r => setTimeout(r, 20));
}
}
export async function printCanvasObject(canvasObj, copies = 1, invert = false) {
if (!printer.characteristic) throw new Error("Not connected");
const { canvas, bytesPerRow, heightPx, bakedInvert } = canvasObj;
const effectiveInvert = bakedInvert ? false : invert;
let bitmap = canvasToBitmap(canvas, bytesPerRow, effectiveInvert);
const packet = buildPacketFromBitmap(bitmap, bytesPerRow, heightPx);
for (let i = 0; i < copies; i++) {
await writeChunks(packet);
await new Promise(r => setTimeout(r, 300));
}
pushLog("Printing done");
}
export function makePreviewFromPrintCanvas(printCanvas) {
const src = printCanvas;
const preview = document.createElement('canvas');
preview.width = src.height;
preview.height = src.width;
const ctx = preview.getContext('2d');
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(0, 0, preview.width, preview.height);
ctx.save();
ctx.translate(preview.width, 0);
ctx.rotate(Math.PI / 2);
ctx.drawImage(src, 0, 0);
ctx.restore();
return preview;
}
export async function detectLabel() {
if (!printer.server) throw new Error("Not connected");
try {
const svcs = await printer.server.getPrimaryServices();
for (const s of svcs) {
try {
const chars = await s.getCharacteristics();
for (const c of chars) {
try {
const v = await c.readValue();
if (v && v.byteLength >= 1) {
const b0 = v.getUint8(0);
if (b0 >= 8 && b0 <= 60) {
printer.settings.labelWidthMM = b0;
pushLog("Detected label width mm: " + b0);
return b0;
}
}
} catch (e) {}
}
} catch (e) {}
}
} catch (e) {}
return null;
}