const { getCanvasNode } = require('./a4HistoryPrint'); 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); } 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 rows = options.rows || []; if (!rows.length) throw new Error(options.emptyText || '没有可打印的数据'); const 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'); 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, };