@slastra/tsplib 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/FINDINGS.md +63 -0
- package/LICENSE +21 -0
- package/README.md +85 -2
- package/dist/flow.d.ts +47 -0
- package/dist/flow.d.ts.map +1 -0
- package/dist/flow.js +52 -0
- package/dist/flow.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/raster.d.ts +32 -0
- package/dist/raster.d.ts.map +1 -0
- package/dist/raster.js +34 -0
- package/dist/raster.js.map +1 -0
- package/dist/transport.d.ts +34 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +31 -0
- package/dist/transport.js.map +1 -0
- package/dist/tspl.d.ts +62 -0
- package/dist/tspl.d.ts.map +1 -0
- package/dist/tspl.js +87 -0
- package/dist/tspl.js.map +1 -0
- package/dist/web-bluetooth.d.ts +45 -0
- package/dist/web-bluetooth.d.ts.map +1 -0
- package/dist/web-bluetooth.js +119 -0
- package/dist/web-bluetooth.js.map +1 -0
- package/package.json +65 -5
- package/src/flow.ts +73 -0
- package/src/index.ts +30 -0
- package/src/raster.ts +49 -0
- package/src/transport.ts +57 -0
- package/src/tspl.ts +137 -0
- package/src/web-bluetooth.ts +156 -0
package/FINDINGS.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Findings: Labeer Y43BT over BLE
|
|
2
|
+
|
|
3
|
+
How the Y43BT's BLE interface was mapped, on 2026-11-03, with a Labeer Y43BT on the bench and [bleak](https://github.com/hbldh/bleak) on Linux (BlueZ). Every claim below was checked on paper or on the wire.
|
|
4
|
+
|
|
5
|
+
## The advertisement
|
|
6
|
+
|
|
7
|
+
The printer is dual-mode and shows up twice:
|
|
8
|
+
|
|
9
|
+
| Name | Address | Transport |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| `Y43BT-69B2` | `28:D4:1E:FA:69:B2` | classic Bluetooth |
|
|
12
|
+
| `Y43BT-69B2_LE` | `E8:D4:1E:FA:69:B2` (random) | BLE |
|
|
13
|
+
|
|
14
|
+
The LE advertisement lists service UUIDs `18f0` and `fee7` (the Tencent/WeChat service), and manufacturer data carrying the classic MAC.
|
|
15
|
+
|
|
16
|
+
## The GATT table
|
|
17
|
+
|
|
18
|
+
| Service | Characteristics | What it turned out to be |
|
|
19
|
+
|---|---|---|
|
|
20
|
+
| `18f0` | `2af1` write, `2af0` notify | The usual home of TSPL on cheap BLE printers. **Accepts writes and does nothing.** No reply, no motor, no print. |
|
|
21
|
+
| `ff00` | `ff02` write, `ff01` notify, `ff03` notify | **The printer.** TSPL on `ff02`, credit flow on `ff03`. |
|
|
22
|
+
| `ff80` | `ff82` write, `ff81` notify | The BT module's AT command channel: anything else gets `\r\nERROR\r\n`. |
|
|
23
|
+
| `49535343-fe7d-…` | ISSC transparent UART | Answers every write with `00 00 00 f7 01`, whatever was sent. |
|
|
24
|
+
| `fff0`, `ff10`, `fee7` | assorted | Silent to everything tried. |
|
|
25
|
+
|
|
26
|
+
## Finding the channel
|
|
27
|
+
|
|
28
|
+
Status queries first, since they print nothing: TSPL `~!T` (model name), `ESC !?` (status byte) and `~!I`, `~!F`, plus ESC/POS `DLE EOT 1` and `GS I C`, sent to every writable characteristic in turn with every notify characteristic subscribed. **None got a printer reply on any channel.** That left a test print as the only way to tell.
|
|
29
|
+
|
|
30
|
+
A minimal TSPL job (`SIZE`, `GAP`, `DIRECTION`, `DENSITY`, `CLS`, `BOX`, `TEXT`, `BAR`, `PRINT`) on `2af1` did nothing at all. The same job on `ff02` started the motor, and on a clean paper path printed exactly as drawn.
|
|
31
|
+
|
|
32
|
+
## The credit channel (`ff03`)
|
|
33
|
+
|
|
34
|
+
On subscribing, `ff03` notifies two messages at once:
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
01 07 7 credits
|
|
38
|
+
02 f4 00 244, little-endian: the largest write the module takes
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Every write to `ff02` is then answered with `01 01`: one credit back. A job sent spending one credit per write, and waiting when none were left, never stalled. One credit covers one write of any size up to the limit, so a 240-byte write costs no more than a 20-byte one.
|
|
42
|
+
|
|
43
|
+
## Rasters
|
|
44
|
+
|
|
45
|
+
`BITMAP x,y,widthBytes,height,0,<data>` with mode 0 (overwrite) prints correctly with MSB-first packing and **1 = white**, the polarity already used by working USB TSPL code for a different printer.
|
|
46
|
+
|
|
47
|
+
An 800 × 600 raster (60 KB) with a millimetre ruler, large text, and 1-, 2- and 4-dot checkerboards printed flawlessly. The 1-dot checker came out as an even grey, which confirms the advertised 203 dpi (8 dots/mm). At a 247-byte ATT MTU with 240-byte writes, the whole job transferred in 2.7 s.
|
|
48
|
+
|
|
49
|
+
## Units and width
|
|
50
|
+
|
|
51
|
+
`SIZE 101.6 mm, 152.4 mm` and `GAP 3 mm, 0 mm` are accepted, so millimetres work as well as inches.
|
|
52
|
+
|
|
53
|
+
A 112 mm ruler printed under that `SIZE` stopped right after its 101 mm tick. That is the 813-dot label width, not the head: TSPL clips at `SIZE`. So the head prints at least 101.6 mm, and its real width is still unmeasured.
|
|
54
|
+
|
|
55
|
+
## Media
|
|
56
|
+
|
|
57
|
+
4 × 6 in gap stock prints cleanly. 2 × 1 in stock printed the start of a label and then jammed twice on adhesive in the paper path; after cleaning, 4 × 6 has been fine. Small stock still needs a clean run.
|
|
58
|
+
|
|
59
|
+
## Still open
|
|
60
|
+
|
|
61
|
+
- The true head width, which needs a `SIZE` wider than 4 in.
|
|
62
|
+
- Any status query this printer answers, if one exists. The vendor app (Flashlabel Pro) would show it in an HCI snoop.
|
|
63
|
+
- Whether a narrow label is positioned by the printer from `SIZE` or needs an `x` offset to sit under the head.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shaun Lastra
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
|
-
#
|
|
1
|
+
# tsplib
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript library for TSPL raster label printing over BLE, for printers that meter writes with a credit channel.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@slastra/tsplib)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
|
|
8
|
+
A TSPL job builder, 1-bit raster packing, and a Web Bluetooth transport. No dependencies, and no DOM in the core.
|
|
9
|
+
|
|
10
|
+
TSPL itself is an open, documented language, and plenty of code already speaks it over USB. What this library adds is the part that is not written down anywhere: the cheap 4×6 shipping printers sold as **Labeer** (and Joysea, Forc, and others) carry a BLE module that hides TSPL behind a credit-metered channel, and advertises a different service that silently swallows everything you send it. See [FINDINGS.md](./FINDINGS.md) for how that was worked out.
|
|
11
|
+
|
|
12
|
+
## Hardware
|
|
13
|
+
|
|
14
|
+
Verified on a **Labeer Y43BT** over BLE, at 203 dpi (8 dots/mm), on 4 × 6 in stock: text, rasters, and a 1-dot checkerboard all print clean. A 60 KB full-width raster transfers in under 3 seconds. [printrow](https://printrow.lastra.us) drives it through this library, from Chrome.
|
|
15
|
+
|
|
16
|
+
The module advertises twice, as `Y43BT-XXXX` (classic Bluetooth) and `Y43BT-XXXX_LE`. Only the `_LE` one has the GATT services, so that is the one to pick in Chrome's chooser.
|
|
17
|
+
|
|
18
|
+
The head prints at least the full 4 in (101.6 mm); its true width has not been measured past that. 2 × 1 in stock has printed but jammed on adhesive in the paper path, so small stock is not yet verified end to end.
|
|
19
|
+
|
|
20
|
+
**How to tell if your printer is this one.** Connect over BLE and look for service `ff00` with characteristics `ff02` (write) and `ff03` (notify). If subscribing to `ff03` immediately notifies `01 07` and `02 f4 00`, it is this module, whatever the case says.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
bun add @slastra/tsplib
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Requirements
|
|
29
|
+
|
|
30
|
+
Web Bluetooth is Chromium-only. On Linux it also needs `chrome://flags/#enable-web-bluetooth`, and it only works on a secure origin (https, or localhost).
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { buildLabel, imageDataToRows, printJob } from '@slastra/tsplib';
|
|
36
|
+
import { connect } from '@slastra/tsplib/web-bluetooth';
|
|
37
|
+
|
|
38
|
+
const link = await connect(); // from a click handler: shows Chrome's device chooser
|
|
39
|
+
|
|
40
|
+
const rows = imageDataToRows(ctx.getImageData(0, 0, 812, 1218)); // 4 x 6 in at 8 dots/mm
|
|
41
|
+
const job = buildLabel(rows, { widthMm: 101.6, heightMm: 152.4, density: 8 });
|
|
42
|
+
|
|
43
|
+
await printJob(link, [async () => job], { onProgress: (done, total) => console.log(done, total) });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`buildLabel` options:
|
|
47
|
+
|
|
48
|
+
| Option | Default | |
|
|
49
|
+
|---|---|---|
|
|
50
|
+
| `widthMm`, `heightMm` | required | Label size. TSPL clips anything drawn past the width. |
|
|
51
|
+
| `media` | `'gap'` | `'gap'`, `'mark'` (black mark, sent as `BLINE`) or `'continuous'`. |
|
|
52
|
+
| `gapMm` | `3` | Gap or mark length. |
|
|
53
|
+
| `density` | `8` | Burn darkness, 0 to 15. |
|
|
54
|
+
| `copies` | `1` | Copies of this label, at no extra cost on the wire. |
|
|
55
|
+
| `direction` | `0` | TSPL `DIRECTION`: `1` turns the label half a turn. |
|
|
56
|
+
| `x`, `y` | `0` | Where the raster's top-left lands, in dots. |
|
|
57
|
+
|
|
58
|
+
`connect` options are `namePrefix` (default `'Y43BT'`), `maxChunk` (240), `creditTimeoutMs` (10000) and `onDisconnect`.
|
|
59
|
+
|
|
60
|
+
Any transport with a `send(bytes)` that resolves once the bytes are accepted can drive `printJob`, so a USB `/dev/usb/lpN` writer works as well as Bluetooth.
|
|
61
|
+
|
|
62
|
+
## How it works
|
|
63
|
+
|
|
64
|
+
- **Only `BITMAP` draws.** The printer's own fonts and barcodes are left unused, so the design is rendered host-side and the preview can be trusted to match the paper.
|
|
65
|
+
- **In TSPL bitmaps, 1 is white.** That is the inverse of most raster formats, and getting it backwards prints a solid black slab with the design cut out of it. Padding bits are white for the same reason.
|
|
66
|
+
- **Credits are the backpressure.** Every GATT write costs one credit and earns one back, so writes go out exactly as fast as the printer takes them, with no pacing timer to tune.
|
|
67
|
+
- **The write size follows the printer.** It announces its limit (244 bytes) on the flow channel. If the browser's link never negotiated an MTU that big, the first large write fails, its credit is refunded, and the link drops to 20-byte writes for good.
|
|
68
|
+
- **Cancelling stops between labels, never inside one.** A half-sent `BITMAP` leaves the printer reading the next job's commands as image data.
|
|
69
|
+
- **There is no status.** The printer answered none of TSPL's or ESC/POS's status queries on any channel. A credit that never comes back (10 s by default) is the only fault signal, and it fails the send rather than hanging it.
|
|
70
|
+
|
|
71
|
+
## Development
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
bun install
|
|
75
|
+
bun test test # job bytes, bitmap packing, credit gate, print loop, BLE transport against a fake printer
|
|
76
|
+
bun run check # tsc
|
|
77
|
+
bun run build # emits dist/
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Credits
|
|
81
|
+
|
|
82
|
+
The Labeer protocol findings are original work from probing the hardware directly; see [FINDINGS.md](./FINDINGS.md). TSPL is TSC's printer language, documented in its public programming manual.
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
MIT © 2026 Shaun Lastra. See [LICENSE](LICENSE).
|
package/dist/flow.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The credit channel that meters writes into the printer.
|
|
3
|
+
*
|
|
4
|
+
* The Y43BT's BLE module exposes TSPL only on service `ff00`: bytes go in on
|
|
5
|
+
* `ff02`, and `ff03` notifies flow-control messages back. Two kinds have been
|
|
6
|
+
* seen, both on subscribing and throughout a job:
|
|
7
|
+
*
|
|
8
|
+
* 01 nn grant nn more writes (7 on subscribe, then 1 back per write)
|
|
9
|
+
* 02 lo hi the largest write the module takes, little-endian (f4 00 = 244)
|
|
10
|
+
*
|
|
11
|
+
* One credit buys one GATT write of any size up to that limit, NOT one byte:
|
|
12
|
+
* a 20-byte write and a 240-byte write each cost one and each earn one back.
|
|
13
|
+
* Spending credits only as they arrive is the whole of the backpressure, so
|
|
14
|
+
* there is no pacing timer to tune and no buffer depth to guess.
|
|
15
|
+
*/
|
|
16
|
+
export type FlowMessage = {
|
|
17
|
+
kind: 'credit';
|
|
18
|
+
count: number;
|
|
19
|
+
} | {
|
|
20
|
+
kind: 'mtu';
|
|
21
|
+
bytes: number;
|
|
22
|
+
} | {
|
|
23
|
+
kind: 'unknown';
|
|
24
|
+
raw: Uint8Array;
|
|
25
|
+
};
|
|
26
|
+
export declare function parseFlow(raw: Uint8Array): FlowMessage;
|
|
27
|
+
/**
|
|
28
|
+
* Counts credits and parks writers until one is free. Writers are served in
|
|
29
|
+
* the order they asked, so a job's chunks cannot overtake each other.
|
|
30
|
+
*/
|
|
31
|
+
export declare class CreditGate {
|
|
32
|
+
private credits;
|
|
33
|
+
private waiters;
|
|
34
|
+
get available(): number;
|
|
35
|
+
grant(count: number): void;
|
|
36
|
+
/**
|
|
37
|
+
* Spend one credit, waiting up to `timeoutMs` for the printer to return
|
|
38
|
+
* one. No status query has been found that this printer answers, so a
|
|
39
|
+
* stalled credit is the only sign a job gets that something is wrong.
|
|
40
|
+
*
|
|
41
|
+
* There is deliberately no abort here. Stopping between two chunks of a
|
|
42
|
+
* label leaves the TSPL parser owed BITMAP bytes, and it will then read the
|
|
43
|
+
* next job's commands as image data. Cancel between labels instead.
|
|
44
|
+
*/
|
|
45
|
+
take(timeoutMs: number): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=flow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flow.d.ts","sourceRoot":"","sources":["../src/flow.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,WAAW,GACpB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACjC;IAAE,IAAI,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,GAAG,EAAE,UAAU,CAAA;CAAE,CAAC;AAExC,wBAAgB,SAAS,CAAC,GAAG,EAAE,UAAU,GAAG,WAAW,CAItD;AAED;;;GAGG;AACH,qBAAa,UAAU;IACtB,OAAO,CAAC,OAAO,CAAK;IACpB,OAAO,CAAC,OAAO,CAAsB;IAErC,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAQ1B;;;;;;;;OAQG;IACH,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAiBtC"}
|
package/dist/flow.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export function parseFlow(raw) {
|
|
2
|
+
if (raw[0] === 0x01 && raw.length >= 2)
|
|
3
|
+
return { kind: 'credit', count: raw[1] };
|
|
4
|
+
if (raw[0] === 0x02 && raw.length >= 3)
|
|
5
|
+
return { kind: 'mtu', bytes: raw[1] | (raw[2] << 8) };
|
|
6
|
+
return { kind: 'unknown', raw };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Counts credits and parks writers until one is free. Writers are served in
|
|
10
|
+
* the order they asked, so a job's chunks cannot overtake each other.
|
|
11
|
+
*/
|
|
12
|
+
export class CreditGate {
|
|
13
|
+
credits = 0;
|
|
14
|
+
waiters = [];
|
|
15
|
+
get available() {
|
|
16
|
+
return this.credits;
|
|
17
|
+
}
|
|
18
|
+
grant(count) {
|
|
19
|
+
this.credits += count;
|
|
20
|
+
while (this.credits > 0 && this.waiters.length) {
|
|
21
|
+
this.credits--;
|
|
22
|
+
this.waiters.shift()();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Spend one credit, waiting up to `timeoutMs` for the printer to return
|
|
27
|
+
* one. No status query has been found that this printer answers, so a
|
|
28
|
+
* stalled credit is the only sign a job gets that something is wrong.
|
|
29
|
+
*
|
|
30
|
+
* There is deliberately no abort here. Stopping between two chunks of a
|
|
31
|
+
* label leaves the TSPL parser owed BITMAP bytes, and it will then read the
|
|
32
|
+
* next job's commands as image data. Cancel between labels instead.
|
|
33
|
+
*/
|
|
34
|
+
take(timeoutMs) {
|
|
35
|
+
if (this.credits > 0 && this.waiters.length === 0) {
|
|
36
|
+
this.credits--;
|
|
37
|
+
return Promise.resolve();
|
|
38
|
+
}
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
const done = () => {
|
|
41
|
+
clearTimeout(timer);
|
|
42
|
+
resolve();
|
|
43
|
+
};
|
|
44
|
+
const timer = setTimeout(() => {
|
|
45
|
+
this.waiters = this.waiters.filter((w) => w !== done);
|
|
46
|
+
reject(new Error('printer stopped taking data (out of paper, lid open, or jammed?)'));
|
|
47
|
+
}, timeoutMs);
|
|
48
|
+
this.waiters.push(done);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=flow.js.map
|
package/dist/flow.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flow.js","sourceRoot":"","sources":["../src/flow.ts"],"names":[],"mappings":"AAoBA,MAAM,UAAU,SAAS,CAAC,GAAe;IACxC,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;IACjF,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;IAC9F,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC;AACjC,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,UAAU;IACd,OAAO,GAAG,CAAC,CAAC;IACZ,OAAO,GAAmB,EAAE,CAAC;IAErC,IAAI,SAAS;QACZ,OAAO,IAAI,CAAC,OAAO,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,KAAa;QAClB,IAAI,CAAC,OAAO,IAAI,KAAK,CAAC;QACtB,OAAO,IAAI,CAAC,OAAO,GAAG,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YAChD,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,IAAI,CAAC,OAAO,CAAC,KAAK,EAAG,EAAE,CAAC;QACzB,CAAC;IACF,CAAC;IAED;;;;;;;;OAQG;IACH,IAAI,CAAC,SAAiB;QACrB,IAAI,IAAI,CAAC,OAAO,GAAG,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACnD,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;QAC1B,CAAC;QACD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACtC,MAAM,IAAI,GAAG,GAAG,EAAE;gBACjB,YAAY,CAAC,KAAK,CAAC,CAAC;gBACpB,OAAO,EAAE,CAAC;YACX,CAAC,CAAC;YACF,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;gBAC7B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC;gBACtD,MAAM,CAAC,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC,CAAC;YACvF,CAAC,EAAE,SAAS,CAAC,CAAC;YACd,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzB,CAAC,CAAC,CAAC;IACJ,CAAC;CACD"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tsplib — TSPL raster label printing, for printers that hide TSPL behind a
|
|
3
|
+
* credit-metered BLE channel.
|
|
4
|
+
*
|
|
5
|
+
* This entry point is pure: no DOM, no Node APIs, no dependencies. The Web
|
|
6
|
+
* Bluetooth transport lives behind `tsplib/web-bluetooth` so importing the job
|
|
7
|
+
* builder never drags browser types in.
|
|
8
|
+
*/
|
|
9
|
+
export { DOTS_PER_MM, DEFAULT_DENSITY, DEFAULT_GAP_MM, packBitmap, buildLabel, type MediaType, type LabelOptions } from './tspl.js';
|
|
10
|
+
export { THRESHOLD, lumaOverWhite, imageDataToRows, type RasterRow, type PixelSource } from './raster.js';
|
|
11
|
+
export { parseFlow, CreditGate, type FlowMessage } from './flow.js';
|
|
12
|
+
export { printJob, type Link, type PrintJobOptions } from './transport.js';
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACN,WAAW,EACX,eAAe,EACf,cAAc,EACd,UAAU,EACV,UAAU,EACV,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACN,SAAS,EACT,aAAa,EACb,eAAe,EACf,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AAEpE,OAAO,EAAE,QAAQ,EAAE,KAAK,IAAI,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tsplib — TSPL raster label printing, for printers that hide TSPL behind a
|
|
3
|
+
* credit-metered BLE channel.
|
|
4
|
+
*
|
|
5
|
+
* This entry point is pure: no DOM, no Node APIs, no dependencies. The Web
|
|
6
|
+
* Bluetooth transport lives behind `tsplib/web-bluetooth` so importing the job
|
|
7
|
+
* builder never drags browser types in.
|
|
8
|
+
*/
|
|
9
|
+
export { DOTS_PER_MM, DEFAULT_DENSITY, DEFAULT_GAP_MM, packBitmap, buildLabel } from './tspl.js';
|
|
10
|
+
export { THRESHOLD, lumaOverWhite, imageDataToRows } from './raster.js';
|
|
11
|
+
export { parseFlow, CreditGate } from './flow.js';
|
|
12
|
+
export { printJob } from './transport.js';
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACN,WAAW,EACX,eAAe,EACf,cAAc,EACd,UAAU,EACV,UAAU,EAGV,MAAM,WAAW,CAAC;AAEnB,OAAO,EACN,SAAS,EACT,aAAa,EACb,eAAe,EAGf,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,SAAS,EAAE,UAAU,EAAoB,MAAM,WAAW,CAAC;AAEpE,OAAO,EAAE,QAAQ,EAAmC,MAAM,gBAAgB,CAAC"}
|
package/dist/raster.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One row of the label, one entry per dot: 1 where the head fires, 0 where the
|
|
3
|
+
* stock stays bare. The same shape yplib and nblib use, so one renderer can
|
|
4
|
+
* feed all three.
|
|
5
|
+
*/
|
|
6
|
+
export type RasterRow = Uint8Array;
|
|
7
|
+
/**
|
|
8
|
+
* 1-bit cutoff. A pixel at or above this luma leaves the stock bare, below it
|
|
9
|
+
* the head fires.
|
|
10
|
+
*/
|
|
11
|
+
export declare const THRESHOLD = 128;
|
|
12
|
+
/** The subset of ImageData this module needs, so Node callers need no DOM. */
|
|
13
|
+
export interface PixelSource {
|
|
14
|
+
data: Uint8ClampedArray;
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* BT.601 luma composited over white paper. Alpha is composited against white
|
|
20
|
+
* because unburned stock is what shows through, so a half-transparent grey is
|
|
21
|
+
* lighter on paper than it looks on a dark screen.
|
|
22
|
+
*
|
|
23
|
+
* @param data RGBA bytes
|
|
24
|
+
* @param i byte offset of the pixel (4 bytes per pixel)
|
|
25
|
+
*/
|
|
26
|
+
export declare function lumaOverWhite(data: Uint8ClampedArray, i: number): number;
|
|
27
|
+
/**
|
|
28
|
+
* Reduce RGBA pixels to rows of dots. Pure over plain arrays: pass an ImageData
|
|
29
|
+
* in the browser, or any `{data, width, height}` from sharp or node-canvas.
|
|
30
|
+
*/
|
|
31
|
+
export declare function imageDataToRows(src: PixelSource, threshold?: number): RasterRow[];
|
|
32
|
+
//# sourceMappingURL=raster.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"raster.d.ts","sourceRoot":"","sources":["../src/raster.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC;AAEnC;;;GAGG;AACH,eAAO,MAAM,SAAS,MAAM,CAAC;AAE7B,8EAA8E;AAC9E,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,iBAAiB,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,iBAAiB,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAGxE;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,WAAW,EAAE,SAAS,SAAY,GAAG,SAAS,EAAE,CAWpF"}
|
package/dist/raster.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 1-bit cutoff. A pixel at or above this luma leaves the stock bare, below it
|
|
3
|
+
* the head fires.
|
|
4
|
+
*/
|
|
5
|
+
export const THRESHOLD = 128;
|
|
6
|
+
/**
|
|
7
|
+
* BT.601 luma composited over white paper. Alpha is composited against white
|
|
8
|
+
* because unburned stock is what shows through, so a half-transparent grey is
|
|
9
|
+
* lighter on paper than it looks on a dark screen.
|
|
10
|
+
*
|
|
11
|
+
* @param data RGBA bytes
|
|
12
|
+
* @param i byte offset of the pixel (4 bytes per pixel)
|
|
13
|
+
*/
|
|
14
|
+
export function lumaOverWhite(data, i) {
|
|
15
|
+
const a = data[i + 3] / 255;
|
|
16
|
+
return (0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2]) * a + 255 * (1 - a);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Reduce RGBA pixels to rows of dots. Pure over plain arrays: pass an ImageData
|
|
20
|
+
* in the browser, or any `{data, width, height}` from sharp or node-canvas.
|
|
21
|
+
*/
|
|
22
|
+
export function imageDataToRows(src, threshold = THRESHOLD) {
|
|
23
|
+
const { data, width, height } = src;
|
|
24
|
+
const rows = [];
|
|
25
|
+
for (let y = 0; y < height; y++) {
|
|
26
|
+
const row = new Uint8Array(width);
|
|
27
|
+
for (let x = 0; x < width; x++) {
|
|
28
|
+
row[x] = lumaOverWhite(data, (y * width + x) * 4) < threshold ? 1 : 0;
|
|
29
|
+
}
|
|
30
|
+
rows.push(row);
|
|
31
|
+
}
|
|
32
|
+
return rows;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=raster.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"raster.js","sourceRoot":"","sources":["../src/raster.ts"],"names":[],"mappings":"AAOA;;;GAGG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,CAAC;AAS7B;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,IAAuB,EAAE,CAAS;IAC/D,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC;IAC5B,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAC1F,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,GAAgB,EAAE,SAAS,GAAG,SAAS;IACtE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IACpC,MAAM,IAAI,GAAgB,EAAE,CAAC;IAC7B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACjC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;QAClC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;YAChC,GAAG,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACvE,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAChB,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The minimum a transport must provide: push bytes at the printer, resolving
|
|
3
|
+
* once they have all been accepted. Web Bluetooth is the one shipped here, but
|
|
4
|
+
* TSPL itself is transport-agnostic, so a USB `/dev/usb/lpN` writer or a
|
|
5
|
+
* classic SPP socket drives `printJob` just as well.
|
|
6
|
+
*
|
|
7
|
+
* There is no status read. The Y43BT answered none of TSPL's (`~!T`, `ESC !?`)
|
|
8
|
+
* or ESC/POS's status queries on any of its channels, so the link's own flow
|
|
9
|
+
* control is the only fault signal there is.
|
|
10
|
+
*/
|
|
11
|
+
export interface Link {
|
|
12
|
+
send(bytes: Uint8Array): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export interface PrintJobOptions {
|
|
15
|
+
/** Called after each label has been handed to the printer. */
|
|
16
|
+
onProgress?: (done: number, total: number) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Abort between labels. A job stops cleanly; it never tears a label in
|
|
19
|
+
* half, because a half-sent BITMAP leaves the printer reading the next
|
|
20
|
+
* job's commands as image data.
|
|
21
|
+
*/
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Run a print job. Each entry builds one label's TSPL job lazily, so a
|
|
26
|
+
* thousand-row batch does not rasterize a thousand labels up front, and label
|
|
27
|
+
* i+1 is built while label i is on the wire.
|
|
28
|
+
*
|
|
29
|
+
* Returns the number of labels sent, which is less than `builds.length` if the
|
|
30
|
+
* signal aborted. "Sent" is as far as anyone can know: the printer reports
|
|
31
|
+
* nothing back once a label has gone in.
|
|
32
|
+
*/
|
|
33
|
+
export declare function printJob(link: Link, builds: (() => Promise<Uint8Array>)[], { onProgress, signal }?: PrintJobOptions): Promise<number>;
|
|
34
|
+
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,WAAW,IAAI;IACpB,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACvC;AAED,MAAM,WAAW,eAAe;IAC/B,8DAA8D;IAC9D,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnD;;;;OAIG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CACrB;AAED;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,CAC7B,IAAI,EAAE,IAAI,EACV,MAAM,EAAE,CAAC,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC,EAAE,EACrC,EAAE,UAAU,EAAE,MAAM,EAAE,GAAE,eAAoB,GAC1C,OAAO,CAAC,MAAM,CAAC,CAkBjB"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run a print job. Each entry builds one label's TSPL job lazily, so a
|
|
3
|
+
* thousand-row batch does not rasterize a thousand labels up front, and label
|
|
4
|
+
* i+1 is built while label i is on the wire.
|
|
5
|
+
*
|
|
6
|
+
* Returns the number of labels sent, which is less than `builds.length` if the
|
|
7
|
+
* signal aborted. "Sent" is as far as anyone can know: the printer reports
|
|
8
|
+
* nothing back once a label has gone in.
|
|
9
|
+
*/
|
|
10
|
+
export async function printJob(link, builds, { onProgress, signal } = {}) {
|
|
11
|
+
let done = 0;
|
|
12
|
+
let next = builds[0]?.();
|
|
13
|
+
try {
|
|
14
|
+
for (let i = 0; i < builds.length; i++) {
|
|
15
|
+
if (signal?.aborted)
|
|
16
|
+
break;
|
|
17
|
+
const job = await next;
|
|
18
|
+
next = builds[i + 1]?.();
|
|
19
|
+
await link.send(job);
|
|
20
|
+
done = i + 1;
|
|
21
|
+
onProgress?.(done, builds.length);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
// a build already started for a label we are not printing must not
|
|
26
|
+
// surface as an unhandled rejection after the caller has moved on
|
|
27
|
+
next?.catch(() => { });
|
|
28
|
+
}
|
|
29
|
+
return done;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=transport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAyBA;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC7B,IAAU,EACV,MAAqC,EACrC,EAAE,UAAU,EAAE,MAAM,KAAsB,EAAE;IAE5C,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,IAAI,GAAoC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;IAC1D,IAAI,CAAC;QACJ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACxC,IAAI,MAAM,EAAE,OAAO;gBAAE,MAAM;YAC3B,MAAM,GAAG,GAAG,MAAM,IAAK,CAAC;YACxB,IAAI,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC;YACzB,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACrB,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;YACb,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;QACnC,CAAC;IACF,CAAC;YAAS,CAAC;QACV,mEAAmE;QACnE,kEAAkE;QAClE,IAAI,EAAE,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IACvB,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC"}
|
package/dist/tspl.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { RasterRow } from './raster.js';
|
|
2
|
+
/** Dots per millimetre at 203 dpi, the resolution of every printer verified here. */
|
|
3
|
+
export declare const DOTS_PER_MM = 8;
|
|
4
|
+
/**
|
|
5
|
+
* How the printer finds the start of each label.
|
|
6
|
+
*
|
|
7
|
+
* - `gap`: die-cut labels on a liner, found by the see-through gap between them.
|
|
8
|
+
* - `mark`: continuous stock with a black mark printed on the back.
|
|
9
|
+
* - `continuous`: no marks at all; the label is exactly as long as `heightMm`.
|
|
10
|
+
*/
|
|
11
|
+
export type MediaType = 'gap' | 'mark' | 'continuous';
|
|
12
|
+
export interface LabelOptions {
|
|
13
|
+
/** Label width across the head. TSPL clips anything drawn past it. */
|
|
14
|
+
widthMm: number;
|
|
15
|
+
/** Label length along the feed. */
|
|
16
|
+
heightMm: number;
|
|
17
|
+
media?: MediaType;
|
|
18
|
+
/** Gap or mark length. Ignored for continuous stock. */
|
|
19
|
+
gapMm?: number;
|
|
20
|
+
/** Burn darkness, 0–15. */
|
|
21
|
+
density?: number;
|
|
22
|
+
/** Copies of this label. They cost nothing extra on the wire. */
|
|
23
|
+
copies?: number;
|
|
24
|
+
/**
|
|
25
|
+
* TSPL `DIRECTION`: 0 prints the raster as drawn with its top leaving the
|
|
26
|
+
* printer first, 1 turns it half a turn. Not a quarter turn: rotating a
|
|
27
|
+
* design to fit the stock is the caller's job.
|
|
28
|
+
*/
|
|
29
|
+
direction?: 0 | 1;
|
|
30
|
+
/** Where the raster's top-left corner lands on the label, in dots. */
|
|
31
|
+
x?: number;
|
|
32
|
+
y?: number;
|
|
33
|
+
}
|
|
34
|
+
export declare const DEFAULT_DENSITY = 8;
|
|
35
|
+
export declare const DEFAULT_GAP_MM = 3;
|
|
36
|
+
/**
|
|
37
|
+
* Pack rows of dots into TSPL `BITMAP` data: MSB first, rows padded to whole
|
|
38
|
+
* bytes, and **1 is white**. That last part is the inverse of the row
|
|
39
|
+
* convention above and of most raster formats, so it is the bug to expect:
|
|
40
|
+
* get it backwards and the label comes out a solid black slab with the design
|
|
41
|
+
* cut out of it. Padding is white for the same reason.
|
|
42
|
+
*
|
|
43
|
+
* Every row must be the same width. A ragged row would not hang the printer
|
|
44
|
+
* the way it does under YPL, but it would shear everything below it sideways,
|
|
45
|
+
* so it is refused rather than sent.
|
|
46
|
+
*/
|
|
47
|
+
export declare function packBitmap(rows: RasterRow[]): {
|
|
48
|
+
widthBytes: number;
|
|
49
|
+
data: Uint8Array;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* One complete TSPL job for a single raster label: page setup, the bitmap,
|
|
53
|
+
* and the print command. The whole thing is self-contained, so a batch is
|
|
54
|
+
* just these laid end to end and a dropped job never leaves state behind for
|
|
55
|
+
* the next one.
|
|
56
|
+
*
|
|
57
|
+
* Only `BITMAP` draws. The printer's own fonts and barcode symbologies are
|
|
58
|
+
* deliberately unused: a host-side raster is the only way the preview can be
|
|
59
|
+
* trusted to match the paper.
|
|
60
|
+
*/
|
|
61
|
+
export declare function buildLabel(rows: RasterRow[], options: LabelOptions): Uint8Array;
|
|
62
|
+
//# sourceMappingURL=tspl.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tspl.d.ts","sourceRoot":"","sources":["../src/tspl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,qFAAqF;AACrF,eAAO,MAAM,WAAW,IAAI,CAAC;AAE7B;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,GAAG,KAAK,GAAG,MAAM,GAAG,YAAY,CAAC;AAEtD,MAAM,WAAW,YAAY;IAC5B,sEAAsE;IACtE,OAAO,EAAE,MAAM,CAAC;IAChB,mCAAmC;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,wDAAwD;IACxD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,2BAA2B;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iEAAiE;IACjE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;OAIG;IACH,SAAS,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC;IAClB,sEAAsE;IACtE,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,CAAC,CAAC,EAAE,MAAM,CAAC;CACX;AAED,eAAO,MAAM,eAAe,IAAI,CAAC;AACjC,eAAO,MAAM,cAAc,IAAI,CAAC;AAgBhC;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CActF;AAYD;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,YAAY,GAAG,UAAU,CAmC/E"}
|
package/dist/tspl.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/** Dots per millimetre at 203 dpi, the resolution of every printer verified here. */
|
|
2
|
+
export const DOTS_PER_MM = 8;
|
|
3
|
+
export const DEFAULT_DENSITY = 8;
|
|
4
|
+
export const DEFAULT_GAP_MM = 3;
|
|
5
|
+
const enc = new TextEncoder();
|
|
6
|
+
/** TSPL takes decimals but chokes on exponents; two places is finer than a dot. */
|
|
7
|
+
function mm(n) {
|
|
8
|
+
return `${Math.round(n * 100) / 100} mm`;
|
|
9
|
+
}
|
|
10
|
+
function int(name, n, min, max) {
|
|
11
|
+
if (!Number.isInteger(n) || n < min || n > max) {
|
|
12
|
+
throw new RangeError(`${name} must be an integer from ${min} to ${max}, got ${n}`);
|
|
13
|
+
}
|
|
14
|
+
return n;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Pack rows of dots into TSPL `BITMAP` data: MSB first, rows padded to whole
|
|
18
|
+
* bytes, and **1 is white**. That last part is the inverse of the row
|
|
19
|
+
* convention above and of most raster formats, so it is the bug to expect:
|
|
20
|
+
* get it backwards and the label comes out a solid black slab with the design
|
|
21
|
+
* cut out of it. Padding is white for the same reason.
|
|
22
|
+
*
|
|
23
|
+
* Every row must be the same width. A ragged row would not hang the printer
|
|
24
|
+
* the way it does under YPL, but it would shear everything below it sideways,
|
|
25
|
+
* so it is refused rather than sent.
|
|
26
|
+
*/
|
|
27
|
+
export function packBitmap(rows) {
|
|
28
|
+
const width = rows[0]?.length ?? 0;
|
|
29
|
+
const widthBytes = Math.ceil(width / 8);
|
|
30
|
+
const data = new Uint8Array(widthBytes * rows.length).fill(0xff);
|
|
31
|
+
rows.forEach((row, y) => {
|
|
32
|
+
if (row.length !== width) {
|
|
33
|
+
throw new RangeError(`row ${y} is ${row.length} dots wide, expected ${width}`);
|
|
34
|
+
}
|
|
35
|
+
const base = y * widthBytes;
|
|
36
|
+
for (let x = 0; x < width; x++) {
|
|
37
|
+
if (row[x])
|
|
38
|
+
data[base + (x >> 3)] &= ~(0x80 >> (x & 7));
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return { widthBytes, data };
|
|
42
|
+
}
|
|
43
|
+
function concat(parts) {
|
|
44
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
45
|
+
let at = 0;
|
|
46
|
+
for (const p of parts) {
|
|
47
|
+
out.set(p, at);
|
|
48
|
+
at += p.length;
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* One complete TSPL job for a single raster label: page setup, the bitmap,
|
|
54
|
+
* and the print command. The whole thing is self-contained, so a batch is
|
|
55
|
+
* just these laid end to end and a dropped job never leaves state behind for
|
|
56
|
+
* the next one.
|
|
57
|
+
*
|
|
58
|
+
* Only `BITMAP` draws. The printer's own fonts and barcode symbologies are
|
|
59
|
+
* deliberately unused: a host-side raster is the only way the preview can be
|
|
60
|
+
* trusted to match the paper.
|
|
61
|
+
*/
|
|
62
|
+
export function buildLabel(rows, options) {
|
|
63
|
+
const { widthMm, heightMm, media = 'gap', gapMm = DEFAULT_GAP_MM, density = DEFAULT_DENSITY, copies = 1, direction = 0, x = 0, y = 0 } = options;
|
|
64
|
+
if (!(widthMm > 0) || !(heightMm > 0)) {
|
|
65
|
+
throw new RangeError(`label size must be positive, got ${widthMm} x ${heightMm} mm`);
|
|
66
|
+
}
|
|
67
|
+
if (rows.length === 0 || rows[0].length === 0)
|
|
68
|
+
throw new RangeError('raster is empty');
|
|
69
|
+
int('density', density, 0, 15);
|
|
70
|
+
int('copies', copies, 1, 65535);
|
|
71
|
+
int('x', x, 0, 65535);
|
|
72
|
+
int('y', y, 0, 65535);
|
|
73
|
+
const gap = media === 'mark'
|
|
74
|
+
? `BLINE ${mm(gapMm)}, 0 mm`
|
|
75
|
+
: `GAP ${media === 'continuous' ? '0 mm' : mm(gapMm)}, 0 mm`;
|
|
76
|
+
const { widthBytes, data } = packBitmap(rows);
|
|
77
|
+
const head = [
|
|
78
|
+
`SIZE ${mm(widthMm)}, ${mm(heightMm)}`,
|
|
79
|
+
gap,
|
|
80
|
+
`DIRECTION ${direction}`,
|
|
81
|
+
`DENSITY ${density}`,
|
|
82
|
+
'CLS',
|
|
83
|
+
`BITMAP ${x},${y},${widthBytes},${rows.length},0,`
|
|
84
|
+
].join('\r\n');
|
|
85
|
+
return concat([enc.encode(head), data, enc.encode(`\r\nPRINT 1,${copies}\r\n`)]);
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=tspl.js.map
|
package/dist/tspl.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tspl.js","sourceRoot":"","sources":["../src/tspl.ts"],"names":[],"mappings":"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"}
|