const { getCanvasNode } = require('./a4HistoryPrint'); const qrcode = require('./qrcode'); const T_W = 384; const MAX_HEIGHT = 2400; const MARGIN = 12; const LINE = 28; function concatBytes(parts) { let len = 0; for (let i = 0; i < parts.length; i += 1) len += parts[i].length; const out = new Uint8Array(len); let offset = 0; for (let i = 0; i < parts.length; i += 1) { out.set(parts[i], offset); offset += parts[i].length; } return out; } function initEscpos() { return Uint8Array.of(0x1b, 0x40); } function feedAndCut() { return Uint8Array.of(0x1d, 0x56, 0x00); } function gsRaster(width, height, bitmap) { const xL = width & 0xff; const xH = (width >> 8) & 0xff; const yL = height & 0xff; const yH = (height >> 8) & 0xff; const header = Uint8Array.of(0x1d, 0x76, 0x30, 0x00, xL, xH, yL, yH); return concatBytes([header, bitmap]); } function imageDataToRaster(imageData) { const { width, height, data } = imageData; const bytesPerRow = Math.ceil(width / 8); const out = new Uint8Array(bytesPerRow * height); for (let y = 0; y < height; y += 1) { for (let x = 0; x < width; x += 1) { const i = (y * width + x) * 4; const lum = data[i] * 0.299 + data[i + 1] * 0.587 + data[i + 2] * 0.114; if (lum < 160) { out[y * bytesPerRow + (x >> 3)] |= 0x80 >> (x & 7); } } } return { width: bytesPerRow, height, bitmap: out }; } function ellipsize(ctx, text, maxWidth) { const value = String(text || ''); if (ctx.measureText(value).width <= maxWidth) return value; let s = value; while (s.length && ctx.measureText(`${s}…`).width > maxWidth) s = s.slice(0, -1); return s ? `${s}…` : ''; } function sumAmounts(rows) { let total = 0; let count = 0; for (const row of rows || []) { const amount = Number(row[5]); if (Number.isFinite(amount)) total += amount; count += 1; } return { count, total: Math.round(total * 100) / 100 }; } function neededHeight(rowCount) { return Math.min(MAX_HEIGHT, 90 + rowCount * LINE + 50); } // 分拣标签版式与旧安卓称重终端 PrintManager.buildPickingLabelBitmap 逐项对齐: // padding 8 / lineHeight 28 / qrSize 110 / 二维码右上(码文在其下);**不印订单号**。 const LABEL_PADDING = 8; const LABEL_LINE = 28; const LABEL_QR = 110; /** 内容底部(基线 146 的日期行 vs 二维码下码文行),+ 底部少量白边。 */ function labelHeight() { const yDate = LABEL_PADDING + 24 + LABEL_LINE * 3 + 2 + LABEL_LINE; // 146 const yCode = LABEL_PADDING + 8 + LABEL_QR + 16; // 142 const bottom = Math.max(yDate + Math.round(18 * 0.25) + 2, yCode + Math.round(14 * 0.25) + 2); return Math.ceil(bottom) + LABEL_PADDING + 8; } /** 二维码画在 (x,y) 指定方块内(含 1 模块静区,对齐 zxing MARGIN=1);失败则什么都不画。 */ function drawQr(ctx, x, y, text, size) { let qr; try { qr = qrcode(0, 'M'); qr.addData(String(text)); qr.make(); } catch (err) { return; } const count = qr.getModuleCount(); const cell = Math.max(1, Math.floor(size / (count + 2))); const side = cell * (count + 2); const left = x + Math.floor((size - side) / 2) + cell; const top = y + Math.floor((size - side) / 2) + cell; ctx.fillStyle = '#000'; for (let r = 0; r < count; r += 1) { for (let c = 0; c < count; c += 1) { if (qr.isDark(r, c)) ctx.fillRect(left + c * cell, top + r * cell, cell, cell); } } } /** 单张分拣标签(与旧秤端版式一致):标题 / 客户 / 品名 / 重量|数量 / 日期,右上角二维码+码文。 */ function drawLabel(ctx, w, h, label) { ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, w, h); ctx.fillStyle = '#000'; ctx.textAlign = 'left'; ctx.textBaseline = 'alphabetic'; const qrSize = LABEL_QR; const gap = 8; const textMaxW = w - LABEL_PADDING * 2 - qrSize - gap; let y = LABEL_PADDING + 24; ctx.font = 'bold 24px sans-serif'; ctx.fillText(label.title || '分拣标签', LABEL_PADDING, y); y += LABEL_LINE; ctx.font = '20px sans-serif'; ctx.fillText(ellipsize(ctx, label.customer || '', textMaxW), LABEL_PADDING, y); y += LABEL_LINE; ctx.fillText(ellipsize(ctx, label.itemNameLine || '', textMaxW), LABEL_PADDING, y); y += LABEL_LINE + 2; ctx.font = 'bold 26px sans-serif'; ctx.fillText(ellipsize(ctx, label.weightLine || '', textMaxW), LABEL_PADDING, y); y += LABEL_LINE; ctx.font = '18px sans-serif'; ctx.fillText(ellipsize(ctx, label.dateLine || '', textMaxW), LABEL_PADDING, y); const qrX = w - LABEL_PADDING - qrSize; const qrY = LABEL_PADDING + 8; const code = String(label.qrText || ''); if (!code) return; drawQr(ctx, qrX, qrY, code, qrSize); // 码文:14px,超宽截断加「…」,居中在二维码下方 ctx.font = '14px sans-serif'; let codeLine = code; while (codeLine.length > 4 && ctx.measureText(codeLine).width > qrSize) { codeLine = codeLine.slice(0, -1); } if (codeLine !== code && codeLine.length > 3) { codeLine = `${codeLine.slice(0, Math.max(1, codeLine.length - 1))}…`; } const codeX = qrX + Math.max(0, (qrSize - ctx.measureText(codeLine).width) / 2); ctx.fillText(codeLine, codeX, qrY + qrSize + 16); } function drawThermal(ctx, w, h, options) { ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, w, h); ctx.fillStyle = '#000'; ctx.textBaseline = 'top'; ctx.textAlign = 'center'; ctx.font = 'bold 22px sans-serif'; ctx.fillText(options.title || '安惟衡', w / 2, 8); ctx.font = '16px sans-serif'; ctx.fillText(options.subtitle || '', w / 2, 36); ctx.textAlign = 'left'; ctx.beginPath(); ctx.moveTo(MARGIN, 62); ctx.lineTo(w - MARGIN, 62); ctx.stroke(); let y = 70; ctx.font = '16px sans-serif'; for (const row of options.rows || []) { const name = `${row[0] || ''} ${row[1] || ''}`.trim(); ctx.fillText(ellipsize(ctx, name, w - MARGIN * 2), MARGIN, y); y += 18; const line2 = `${row[2] || ''} ${row[3] || ''} ${row[4] || ''} ${row[5] || ''}`; ctx.fillText(ellipsize(ctx, line2, w - MARGIN * 2), MARGIN, y); y += LINE - 18; if (y > h - 48) break; } ctx.beginPath(); ctx.moveTo(MARGIN, h - 40); ctx.lineTo(w - MARGIN, h - 40); ctx.stroke(); ctx.font = 'bold 16px sans-serif'; const { count, total } = sumAmounts(options.rows); ctx.fillText(`合计 ${count} 项 ¥${total}`, MARGIN, h - 32); } async function buildThermalBytes(component, options) { const label = options.label; let height; if (label) { height = labelHeight(); } else { const rows = options.rows || []; if (!rows.length) throw new Error(options.emptyText || '没有可打印的数据'); height = neededHeight(rows.length); if (90 + rows.length * LINE + 50 > MAX_HEIGHT) { throw new Error('行数过多,请缩小日期范围后再打印'); } } const canvas = await getCanvasNode(component, options.selector || '#printCanvas'); canvas.width = T_W; canvas.height = height; const ctx = canvas.getContext('2d'); if (label) { drawLabel(ctx, T_W, height, label); } else { drawThermal(ctx, T_W, height, options); } let imageData; try { imageData = ctx.getImageData(0, 0, T_W, height); } catch { throw new Error('当前基础库无法生成热敏小票,请改用 A4 打印'); } const raster = imageDataToRaster(imageData); return concatBytes([initEscpos(), gsRaster(raster.width, raster.height, raster.bitmap), feedAndCut()]); } module.exports = { buildThermalBytes, T_W, };