inkless 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +65 -2
- package/bin/inkless.js +10 -0
- package/console-dist/assets/BlueprintTool-CLWuM21a.js +4226 -0
- package/console-dist/assets/index-CIqLZU6s.js +33 -0
- package/console-dist/assets/index-Dv7Gg9qm.css +1 -0
- package/console-dist/favicon.svg +11 -0
- package/console-dist/index.html +18 -0
- package/native/linux/rfcomm.py +84 -0
- package/native/macos/build/InklessBluetooth.app/Contents/Info.plist +26 -0
- package/native/macos/build/InklessBluetooth.app/Contents/MacOS/InklessBluetooth +0 -0
- package/native/macos/build/InklessBluetooth.app/Contents/_CodeSignature/CodeResources +115 -0
- package/package.json +60 -4
- package/skills/inkless/SKILL.md +213 -0
- package/src/core/bits.js +143 -0
- package/src/core/blueprint/demos.js +119 -0
- package/src/core/blueprint/geometry.js +232 -0
- package/src/core/blueprint/index.js +100 -0
- package/src/core/blueprint/layout.js +210 -0
- package/src/core/blueprint/mesh.js +101 -0
- package/src/core/blueprint/methods.js +95 -0
- package/src/core/blueprint/remesh.js +250 -0
- package/src/core/blueprint/slice.js +134 -0
- package/src/core/blueprint/unfold.js +185 -0
- package/src/core/design/codes.js +39 -0
- package/src/core/design/filters.js +109 -0
- package/src/core/design/render.js +317 -0
- package/src/core/design/schema.js +132 -0
- package/src/core/formats/fit.js +46 -0
- package/src/core/formats/index.js +210 -0
- package/src/core/formats/library.js +341 -0
- package/src/core/formats/share.js +65 -0
- package/src/core/index.js +15 -0
- package/src/core/printers/hp6.js +160 -0
- package/src/core/printers/index.js +14 -0
- package/src/core/printers/pm290.js +115 -0
- package/src/core/session.js +56 -0
- package/src/node/artwork.js +33 -0
- package/src/node/cli.js +430 -0
- package/src/node/console/actions.js +30 -0
- package/src/node/console/client.js +68 -0
- package/src/node/console/paths.js +8 -0
- package/src/node/console/server.js +286 -0
- package/src/node/design.js +99 -0
- package/src/node/formats.js +39 -0
- package/src/node/frames.js +93 -0
- package/src/node/index.js +6 -0
- package/src/node/kit.js +12 -0
- package/src/node/skill.js +44 -0
- package/src/node/transports/index.js +21 -0
- package/src/node/transports/linux.js +52 -0
- package/src/node/transports/macos.js +81 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// HP6-C 12 mm label maker: framed binary packets over Classic SPP.
|
|
3
|
+
// Frames are `64 cmd seq len16le payload 00 00 00 00 9b`; replies carry a real
|
|
4
|
+
// checksum and a `34 12` magic before the terminator. See RESEARCH.md.
|
|
5
|
+
import { packRows } from "../bits.js";
|
|
6
|
+
|
|
7
|
+
export const ROW_BYTES = 12;
|
|
8
|
+
export const WIDTH_DOTS = ROW_BYTES * 8; // 96 dots across the tape
|
|
9
|
+
export const LABEL_ROWS = 288; // 36 mm at 8 dots/mm
|
|
10
|
+
const REPLY_OVERHEAD = 10;
|
|
11
|
+
const MAX_REPLY = 600;
|
|
12
|
+
|
|
13
|
+
export function frame(command, sequence, payload = new Uint8Array(0)) {
|
|
14
|
+
if (payload.length > 0xffff) throw new Error("hp6: payload exceeds one frame");
|
|
15
|
+
const out = new Uint8Array(payload.length + 10);
|
|
16
|
+
out.set([0x64, command, sequence & 0xff, payload.length & 0xff, payload.length >> 8]);
|
|
17
|
+
out.set(payload, 5);
|
|
18
|
+
out.set([0, 0, 0, 0, 0x9b], 5 + payload.length);
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Speed, density, start, raster, end: the vendor app's exact order. The raster
|
|
24
|
+
* goes out as-is; this printer burns 1 bits, so nothing is inverted.
|
|
25
|
+
* @param {Uint8Array} rows printer-order rows, 12 bytes each
|
|
26
|
+
*/
|
|
27
|
+
export function buildHp6Job(rows, { density = 9, firstSequence = 1 } = {}) {
|
|
28
|
+
if (rows.length % ROW_BYTES !== 0) throw new Error("hp6: raster is not whole rows");
|
|
29
|
+
if (density < 0 || density > 255) throw new Error("hp6: density must fit one byte");
|
|
30
|
+
const frames = [];
|
|
31
|
+
let seq = firstSequence;
|
|
32
|
+
const add = (command, payload) => frames.push(frame(command, seq++, Uint8Array.from(payload)));
|
|
33
|
+
add(0x0a, [25]); // speed, fixed in the app
|
|
34
|
+
add(0x09, [density]);
|
|
35
|
+
add(0x03, [0x02, 0x20, 0x03]); // start of job
|
|
36
|
+
add(0x02, [0x0b, 0x00]); // paper/mode selector, as captured
|
|
37
|
+
// 288-byte chunks = 24 rows, like the app; a short last chunk is fine.
|
|
38
|
+
for (let offset = 0; offset < rows.length; offset += 288) {
|
|
39
|
+
frames.push(frame(0x00, seq++, rows.subarray(offset, Math.min(rows.length, offset + 288))));
|
|
40
|
+
}
|
|
41
|
+
add(0x03, [0x01, 0x20, 0x03]); // end of job
|
|
42
|
+
const out = new Uint8Array(frames.reduce((n, f) => n + f.length, 0));
|
|
43
|
+
let at = 0;
|
|
44
|
+
for (const f of frames) {
|
|
45
|
+
out.set(f, at);
|
|
46
|
+
at += f.length;
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Centre landscape artwork (the label as it reads, up to 288 x 96) and turn it
|
|
53
|
+
* a quarter turn into printer rows: row 0 is the label's trailing edge, each
|
|
54
|
+
* row runs across the tape.
|
|
55
|
+
* @param {Uint8Array} landscape packed rows, 1 = black
|
|
56
|
+
*/
|
|
57
|
+
export function rasterRows(landscape, bytesPerRow, height, rows = LABEL_ROWS) {
|
|
58
|
+
if (bytesPerRow <= 0 || height <= 0) throw new Error("hp6: empty raster");
|
|
59
|
+
if (bytesPerRow * 8 > rows) throw new Error(`hp6: artwork is ${bytesPerRow * 8} dots long; the label holds ${rows}`);
|
|
60
|
+
if (height > WIDTH_DOTS) throw new Error(`hp6: artwork is ${height} dots tall; the label holds ${WIDTH_DOTS}`);
|
|
61
|
+
const offsetX = Math.floor((rows - bytesPerRow * 8) / 2);
|
|
62
|
+
const offsetY = Math.floor((WIDTH_DOTS - height) / 2);
|
|
63
|
+
const out = new Uint8Array(ROW_BYTES * rows);
|
|
64
|
+
for (let y = 0; y < height; y++) {
|
|
65
|
+
for (let x = 0; x < bytesPerRow * 8; x++) {
|
|
66
|
+
if (!(landscape[y * bytesPerRow + (x >> 3)] & (0x80 >> (x & 7)))) continue;
|
|
67
|
+
const row = rows - 1 - (x + offsetX);
|
|
68
|
+
const column = y + offsetY;
|
|
69
|
+
out[row * ROW_BYTES + (column >> 3)] |= 0x80 >> (column & 7);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Width to rasterise artwork at so it fits the label both ways. */
|
|
76
|
+
export function fitWidth(aspectRatio, rows = LABEL_ROWS) {
|
|
77
|
+
if (!(aspectRatio > 0)) return rows;
|
|
78
|
+
return Math.max(8, Math.min(rows, Math.round(WIDTH_DOTS * aspectRatio)));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The serial-number reply (`f2`) is the handshake. After arm(), a status frame
|
|
83
|
+
* (`ff`) reporting printing (payload byte 3, bit 0x08) and a later one
|
|
84
|
+
* reporting idle means the label is done.
|
|
85
|
+
*/
|
|
86
|
+
export class Hp6Replies {
|
|
87
|
+
constructor() {
|
|
88
|
+
this.pending = [];
|
|
89
|
+
this.handshake = false;
|
|
90
|
+
this.printing = false;
|
|
91
|
+
this.done = false;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
arm() {
|
|
95
|
+
this.pending = [];
|
|
96
|
+
this.printing = false;
|
|
97
|
+
this.done = false;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
feed(bytes) {
|
|
101
|
+
for (const byte of bytes) {
|
|
102
|
+
if (this.pending.length === MAX_REPLY) this.pending.shift();
|
|
103
|
+
this.pending.push(byte);
|
|
104
|
+
this.#parse();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
#parse() {
|
|
109
|
+
const p = this.pending;
|
|
110
|
+
while (p.length) {
|
|
111
|
+
if (p[0] !== 0x64) {
|
|
112
|
+
p.shift();
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (p.length < 5) return;
|
|
116
|
+
const payload = p[3] | (p[4] << 8);
|
|
117
|
+
const total = payload + REPLY_OVERHEAD;
|
|
118
|
+
if (total > MAX_REPLY) {
|
|
119
|
+
p.shift();
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (p.length < total) return;
|
|
123
|
+
if (p[total - 1] !== 0x9b) {
|
|
124
|
+
p.shift();
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (p[1] === 0xf2) this.handshake = true;
|
|
128
|
+
if (p[1] === 0xff && payload >= 3) {
|
|
129
|
+
if (p[7] & 0x08) this.printing = true;
|
|
130
|
+
else if (this.printing) this.done = true;
|
|
131
|
+
}
|
|
132
|
+
p.splice(0, total);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export const hp6 = {
|
|
138
|
+
id: "hp6",
|
|
139
|
+
name: "HP6-C",
|
|
140
|
+
aliases: [],
|
|
141
|
+
bluetoothName: "HP6",
|
|
142
|
+
media: { kind: "label", widthDots: LABEL_ROWS, heightDots: WIDTH_DOTS, widthMm: 36, heightMm: 12, dotsPerMm: 8 },
|
|
143
|
+
/** It forgets its link key when the phone app pairs; macOS re-pairs on connect, and RFCOMM needs baseband first. */
|
|
144
|
+
connect: { requirePaired: false, openBasebandFirst: true },
|
|
145
|
+
handshakeName: "serial-number reply",
|
|
146
|
+
handshake: frame(0x12, 1),
|
|
147
|
+
density: ({ dark = false, light = false } = {}) => (dark ? 13 : light ? 5 : 9),
|
|
148
|
+
/** Fit inside the 288 x 96 landscape label, keeping the aspect ratio. */
|
|
149
|
+
fit(sourceWidth, sourceHeight) {
|
|
150
|
+
const aspect = sourceWidth / sourceHeight;
|
|
151
|
+
const width = fitWidth(aspect);
|
|
152
|
+
return { width, height: Math.max(8, Math.min(WIDTH_DOTS, Math.round(width / aspect))) };
|
|
153
|
+
},
|
|
154
|
+
/** bits are the label as it reads, 1 = black, at most 288 x 96. */
|
|
155
|
+
buildJob({ bits, width, height, density = 9 }) {
|
|
156
|
+
const bytesPerRow = Math.ceil(width / 8);
|
|
157
|
+
return buildHp6Job(rasterRows(packRows(bits, width, height), bytesPerRow, height), { density, firstSequence: 1 });
|
|
158
|
+
},
|
|
159
|
+
createReplies: () => new Hp6Replies(),
|
|
160
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { pm290 } from "./pm290.js";
|
|
3
|
+
import { hp6 } from "./hp6.js";
|
|
4
|
+
|
|
5
|
+
export const printers = { pm290, hp6 };
|
|
6
|
+
|
|
7
|
+
/** @param {string} id */
|
|
8
|
+
export function getPrinter(id = "pm290") {
|
|
9
|
+
const printer = printers[id.toLowerCase()];
|
|
10
|
+
if (!printer) throw new Error(`unknown printer ${id}; use ${Object.keys(printers).join(" or ")}`);
|
|
11
|
+
return printer;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { pm290, hp6 };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// PM290 / Labelnize / TYPONOS: TSPL text with a binary BITMAP over Classic SPP.
|
|
3
|
+
// Framing matches the captured vendor-app jobs byte for byte (see RESEARCH.md).
|
|
4
|
+
import { packRows } from "../bits.js";
|
|
5
|
+
|
|
6
|
+
const WIDTH_DOTS = 384;
|
|
7
|
+
const BYTES_PER_ROW = WIDTH_DOTS / 8;
|
|
8
|
+
const encode = (text) => new TextEncoder().encode(text);
|
|
9
|
+
const CONFIG_PREFIX = encode("CONFIG ");
|
|
10
|
+
const CONFIG_LENGTH = 19;
|
|
11
|
+
|
|
12
|
+
/** SIZE needs the exact eighth-millimetre height or the printer replays its buffer. */
|
|
13
|
+
function heightMm(dots) {
|
|
14
|
+
const eighths = [".0", ".125", ".25", ".375", ".5", ".625", ".75", ".875"];
|
|
15
|
+
return `${Math.floor(dots / 8)}${eighths[dots % 8]}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** @param {{ packed: Uint8Array, height: number, density?: number }} job packed rows, 1 = black */
|
|
19
|
+
export function buildTsplJob({ packed, height, density = 10 }) {
|
|
20
|
+
if (!Number.isInteger(height) || height <= 0) throw new Error("pm290: height must be a positive integer");
|
|
21
|
+
if (packed.length !== BYTES_PER_ROW * height) {
|
|
22
|
+
throw new Error(`pm290: raster is ${packed.length} bytes, expected ${BYTES_PER_ROW}x${height}`);
|
|
23
|
+
}
|
|
24
|
+
const header = encode(
|
|
25
|
+
`SIZE 54.0 mm,${heightMm(height)} mm\r\nDIRECTION 0,0\r\nDENSITY ${density}\r\nCLS\r\nPRINT 1\r\n` +
|
|
26
|
+
`BITMAP 0,0,${BYTES_PER_ROW},${height},1,`,
|
|
27
|
+
);
|
|
28
|
+
const trailer = encode("ALLEND\r\n\r\n");
|
|
29
|
+
const out = new Uint8Array(header.length + packed.length + trailer.length);
|
|
30
|
+
out.set(header);
|
|
31
|
+
// TSPL burns 0 bits: invert exactly once, here.
|
|
32
|
+
for (let i = 0; i < packed.length; i++) out[header.length + i] = ~packed[i] & 0xff;
|
|
33
|
+
out.set(trailer, header.length + packed.length);
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Printer replies: a 19-byte `CONFIG ...\r\n` answer to `CONFIG?`, `f0` acks,
|
|
39
|
+
* `63 20` progress and `63 00` completion. Split-safe: bytes may arrive in any
|
|
40
|
+
* chunking, and a `63 00` inside another frame never counts.
|
|
41
|
+
*/
|
|
42
|
+
export class Pm290Replies {
|
|
43
|
+
constructor() {
|
|
44
|
+
this.pending = [];
|
|
45
|
+
this.handshake = false;
|
|
46
|
+
this.armed = false;
|
|
47
|
+
this.done = false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Forget anything before the job starts so a stale completion can't finish it. */
|
|
51
|
+
arm() {
|
|
52
|
+
this.pending = [];
|
|
53
|
+
this.armed = true;
|
|
54
|
+
this.done = false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
feed(bytes) {
|
|
58
|
+
for (const byte of bytes) {
|
|
59
|
+
this.pending.push(byte);
|
|
60
|
+
this.#parse();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#parse() {
|
|
65
|
+
const p = this.pending;
|
|
66
|
+
while (p.length) {
|
|
67
|
+
if (p[0] === 0xf0) {
|
|
68
|
+
if (p.length < 4) return;
|
|
69
|
+
p.splice(0, 4);
|
|
70
|
+
} else if (p[0] === 0x63) {
|
|
71
|
+
if (p.length < 2) return;
|
|
72
|
+
if (p[1] === 0x00) {
|
|
73
|
+
p.splice(0, 2);
|
|
74
|
+
if (this.armed) this.done = true;
|
|
75
|
+
} else if (p[1] === 0x20) {
|
|
76
|
+
if (p.length < 5) return;
|
|
77
|
+
p.splice(0, 5);
|
|
78
|
+
} else p.shift();
|
|
79
|
+
} else if (p[0] === CONFIG_PREFIX[0]) {
|
|
80
|
+
const n = Math.min(p.length, CONFIG_PREFIX.length);
|
|
81
|
+
if (p.slice(0, n).some((b, i) => b !== CONFIG_PREFIX[i])) p.shift();
|
|
82
|
+
else if (p.length < CONFIG_LENGTH) return;
|
|
83
|
+
else if (p[17] === 0x0d && p[18] === 0x0a) {
|
|
84
|
+
p.splice(0, CONFIG_LENGTH);
|
|
85
|
+
this.handshake = true;
|
|
86
|
+
} else p.shift();
|
|
87
|
+
} else p.shift();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const pm290 = {
|
|
93
|
+
id: "pm290",
|
|
94
|
+
name: "PM290",
|
|
95
|
+
aliases: ["Labelnize", "TYPONOS"],
|
|
96
|
+
bluetoothName: "PM290",
|
|
97
|
+
media: { kind: "roll", widthDots: WIDTH_DOTS, widthMm: 48, paperMm: 54, dotsPerMm: 8 },
|
|
98
|
+
/** The PM290 keeps its link key; it must be paired before connecting. */
|
|
99
|
+
connect: { requirePaired: true, openBasebandFirst: false },
|
|
100
|
+
handshakeName: "CONFIG reply",
|
|
101
|
+
handshake: encode("CONFIG?\r\n"),
|
|
102
|
+
density: ({ dark = false, light = false } = {}) => (dark ? 15 : light ? 6 : 10),
|
|
103
|
+
/** Raster size for artwork of this aspect: full width, height follows. */
|
|
104
|
+
fit(sourceWidth, sourceHeight, { widthDots = WIDTH_DOTS } = {}) {
|
|
105
|
+
return { width: widthDots, height: Math.max(8, Math.round((sourceHeight * widthDots) / sourceWidth)) };
|
|
106
|
+
},
|
|
107
|
+
/** bits are the artwork as it reads, 1 = black, up to 384 dots wide. */
|
|
108
|
+
buildJob({ bits, width, height, density = 10 }) {
|
|
109
|
+
if (width > WIDTH_DOTS) throw new Error(`pm290: artwork is ${width} dots wide; the head holds ${WIDTH_DOTS}`);
|
|
110
|
+
const page = new Uint8Array(WIDTH_DOTS * height);
|
|
111
|
+
for (let y = 0; y < height; y++) page.set(bits.subarray(y * width, y * width + width), y * WIDTH_DOTS);
|
|
112
|
+
return buildTsplJob({ packed: packRows(page, WIDTH_DOTS, height), height, density });
|
|
113
|
+
},
|
|
114
|
+
createReplies: () => new Pm290Replies(),
|
|
115
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// One print session over any byte transport (macOS helper, Linux helper, Web
|
|
3
|
+
// Serial): handshake, then the job in paced chunks, then the printer's own
|
|
4
|
+
// completion. A job is never retried automatically.
|
|
5
|
+
|
|
6
|
+
export const CHUNK_BYTES = 512;
|
|
7
|
+
export const PACE_MS = 3;
|
|
8
|
+
export const TIMEOUTS = { handshakeMs: 6000, completionMs: 30000 };
|
|
9
|
+
|
|
10
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {{ write(bytes: Uint8Array): Promise<void>, onData(listener: (bytes: Uint8Array) => void): void, mtu?: number }} Transport
|
|
14
|
+
* @param {{ transport: Transport, printer: any, job?: Uint8Array, timeouts?: Partial<typeof TIMEOUTS>, onStatus?: (message: string) => void }} options
|
|
15
|
+
*/
|
|
16
|
+
export async function runSession({ transport, printer, job, timeouts = {}, onStatus = () => {} }) {
|
|
17
|
+
const limits = { ...TIMEOUTS, ...timeouts };
|
|
18
|
+
const replies = printer.createReplies();
|
|
19
|
+
let wake = () => {};
|
|
20
|
+
transport.onData((bytes) => {
|
|
21
|
+
replies.feed(bytes);
|
|
22
|
+
wake();
|
|
23
|
+
});
|
|
24
|
+
const until = (predicate, ms, what) =>
|
|
25
|
+
new Promise((resolve, reject) => {
|
|
26
|
+
if (predicate()) return resolve(undefined);
|
|
27
|
+
const timer = setTimeout(() => reject(new Error(`${printer.name}: ${what} timed out; nothing was retried`)), ms);
|
|
28
|
+
wake = () => {
|
|
29
|
+
if (!predicate()) return;
|
|
30
|
+
clearTimeout(timer);
|
|
31
|
+
wake = () => {};
|
|
32
|
+
resolve(undefined);
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
onStatus(`asking for the ${printer.handshakeName}`);
|
|
37
|
+
await transport.write(printer.handshake);
|
|
38
|
+
await until(() => replies.handshake, limits.handshakeMs, printer.handshakeName);
|
|
39
|
+
if (!job) return { handshake: true, bytesWritten: 0, completed: false };
|
|
40
|
+
|
|
41
|
+
replies.arm();
|
|
42
|
+
const chunk = Math.max(1, Math.min(CHUNK_BYTES, transport.mtu ?? CHUNK_BYTES));
|
|
43
|
+
let written = 0;
|
|
44
|
+
const finished = until(() => replies.done, limits.completionMs, "print completion");
|
|
45
|
+
finished.catch(() => {}); // observed below; avoid an unhandled rejection while writing
|
|
46
|
+
while (written < job.length) {
|
|
47
|
+
const count = Math.min(chunk, job.length - written);
|
|
48
|
+
await transport.write(job.subarray(written, written + count));
|
|
49
|
+
written += count;
|
|
50
|
+
onStatus(`sent ${written}/${job.length} bytes`);
|
|
51
|
+
if (written < job.length) await sleep(PACE_MS);
|
|
52
|
+
}
|
|
53
|
+
onStatus("waiting for the printer to finish");
|
|
54
|
+
await finished;
|
|
55
|
+
return { handshake: true, bytesWritten: written, completed: true };
|
|
56
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Artwork sniffing and previews.
|
|
3
|
+
import { createCanvas } from "@napi-rs/canvas";
|
|
4
|
+
|
|
5
|
+
export async function readStdin() {
|
|
6
|
+
const chunks = [];
|
|
7
|
+
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
8
|
+
return Buffer.concat(chunks);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function isImage(bytes) {
|
|
12
|
+
const sig = (...b) => b.every((v, i) => bytes[i] === v);
|
|
13
|
+
return sig(0x89, 0x50, 0x4e, 0x47) || sig(0xff, 0xd8, 0xff) || sig(0x47, 0x49, 0x46) ||
|
|
14
|
+
(sig(0x52, 0x49, 0x46, 0x46) && bytes.subarray(8, 12).toString() === "WEBP") ||
|
|
15
|
+
bytes.subarray(0, 512).toString().includes("<svg");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** A black-on-white PNG of exactly what the printer will burn, one pixel per dot. */
|
|
19
|
+
export function previewPng({ bits, width, height }, { scale = 1 } = {}) {
|
|
20
|
+
const canvas = createCanvas(width * scale, height * scale);
|
|
21
|
+
const ctx = canvas.getContext("2d");
|
|
22
|
+
const image = ctx.createImageData(width * scale, height * scale);
|
|
23
|
+
for (let y = 0; y < height * scale; y++) {
|
|
24
|
+
for (let x = 0; x < width * scale; x++) {
|
|
25
|
+
const v = bits[Math.floor(y / scale) * width + Math.floor(x / scale)] ? 0 : 255;
|
|
26
|
+
const o = (y * width * scale + x) * 4;
|
|
27
|
+
image.data[o] = image.data[o + 1] = image.data[o + 2] = v;
|
|
28
|
+
image.data[o + 3] = 255;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
ctx.putImageData(image, 0, 0);
|
|
32
|
+
return canvas.toBuffer("image/png");
|
|
33
|
+
}
|