@bobfrankston/brother-label 1.0.13 → 1.1.1

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/api.ts CHANGED
@@ -1,505 +1,427 @@
1
- /**
2
- * Brother Label Printer API
3
- * Programmatic interface for printing labels on Brother P-touch printers
4
- */
5
-
6
- import * as fs from "fs";
7
- import * as path from "path";
8
- import { fileURLToPath } from "url";
9
- import * as os from "os";
10
- import { renderHtmlFile, renderHtmlString, closeBrowser } from "./render.js";
11
- import { Jimp, loadFont, measureText, measureTextHeight } from "jimp";
12
- import { spawn } from "child_process";
13
- import QRCode from "qrcode";
14
-
15
- // Types
16
- export type TapeSize = 6 | 9 | 12 | 18 | 24;
17
- export type Orientation = "landscape" | "portrait";
18
-
19
- export interface PrinterConfig {
20
- defaultTape?: TapeSize;
21
- defaultPrinter?: string;
22
- }
23
-
24
- export interface PrinterInfo {
25
- name: string;
26
- }
27
-
28
- export interface PrintOptions {
29
- // Content (exactly one required)
30
- text?: string;
31
- html?: string;
32
- htmlPath?: string;
33
- textFile?: string;
34
- imagePath?: string;
35
- imageBuffer?: Buffer;
36
- qr?: string; // QR code data to encode
37
-
38
- // Settings
39
- tape?: TapeSize;
40
- printer?: string;
41
- orientation?: Orientation;
42
- length?: number; // explicit length in mm
43
- basePath?: string; // base path for inline HTML resources
44
- aspect?: string; // width:height ratio for HTML (e.g., "3.5:2")
45
- qrLabel?: string; // optional text label beside QR code
46
- textHeight?: string; // text height: "12mm", ".5in", "50%" (of tape height)
47
- }
48
-
49
- export interface PrintResult {
50
- image: Buffer;
51
- }
52
-
53
- // Constants
54
- const CONFIG_PATH = path.join(os.homedir(), ".brother-label.json");
55
- const DEFAULT_PRINTER = "Brother PT-P710BT";
56
- const PRINT_DPI = 300; // High resolution for quality output
57
-
58
- // Tape sizes: height is printable area in pixels at 300 DPI
59
- const TAPE_SIZES: Record<TapeSize, { width: number; height: number }> = {
60
- 6: { width: 1137, height: 56 }, // 3.79" x 0.19" @ 300 DPI
61
- 9: { width: 1137, height: 84 }, // 3.79" x 0.28" @ 300 DPI
62
- 12: { width: 1137, height: 113 }, // 3.79" x 0.38" @ 300 DPI
63
- 18: { width: 1137, height: 169 }, // 3.79" x 0.56" @ 300 DPI
64
- 24: { width: 1137, height: 213 }, // 3.79" x 0.71" @ 300 DPI
65
- };
66
-
67
- // Brother CustomMediaSize names and widths in microns
68
- const MEDIA_OPTIONS: Record<TapeSize, { name: string; width: number }> = {
69
- 6: { name: "CustomMediaSize257", width: 5900 },
70
- 9: { name: "CustomMediaSize258", width: 9000 },
71
- 12: { name: "CustomMediaSize259", width: 11900 },
72
- 18: { name: "CustomMediaSize260", width: 18100 },
73
- 24: { name: "CustomMediaSize261", width: 24000 },
74
- };
75
-
76
- // Config functions
77
- export function getConfig(): PrinterConfig {
78
- try {
79
- if (fs.existsSync(CONFIG_PATH)) {
80
- const raw = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf-8"));
81
- // Handle legacy format where tape was stored as "24mm" string
82
- if (typeof raw.defaultTape === "string") {
83
- raw.defaultTape = parseInt(raw.defaultTape.replace("mm", ""), 10) as TapeSize;
84
- }
85
- return raw;
86
- }
87
- } catch {
88
- // Ignore config errors
89
- }
90
- return {};
91
- }
92
-
93
- export function setConfig(config: Partial<PrinterConfig>): void {
94
- const current = getConfig();
95
- const merged = { ...current, ...config };
96
- fs.writeFileSync(CONFIG_PATH, JSON.stringify(merged, null, 2));
97
- }
98
-
99
- export function getConfigPath(): string {
100
- return CONFIG_PATH;
101
- }
102
-
103
- // Printer listing
104
- export async function listPrinters(): Promise<PrinterInfo[]> {
105
- return new Promise((resolve, reject) => {
106
- const ps = spawn("powershell", [
107
- "-Command",
108
- "Get-Printer | Where-Object { $_.Name -like '*Brother*' -or $_.Name -like '*PT*' -or $_.Name -like '*QL*' } | Select-Object -ExpandProperty Name"
109
- ], { stdio: "pipe" });
110
-
111
- let stdout = "";
112
- let stderr = "";
113
- ps.stdout.on("data", (data) => { stdout += data.toString(); });
114
- ps.stderr.on("data", (data) => { stderr += data.toString(); });
115
-
116
- ps.on("close", (code) => {
117
- if (code !== 0) {
118
- reject(new Error(`Failed to list printers: ${stderr}`));
119
- return;
120
- }
121
- const printers = stdout.trim().split("\n")
122
- .filter(p => p.trim())
123
- .map(name => ({ name: name.trim() }));
124
- resolve(printers);
125
- });
126
- });
127
- }
128
-
129
- // Calculate HTML viewport dimensions from tape height and optional aspect ratio
130
- function getHtmlDimensions(tapeHeight: number, aspect?: string): { width?: number; height: number } {
131
- if (!aspect) {
132
- // Auto-detect width from content
133
- return { width: undefined, height: tapeHeight };
134
- }
135
-
136
- // Parse aspect ratio "width:height" or "width/height" (e.g., "3.5:2" or "3.5/2")
137
- const separator = aspect.includes("/") ? "/" : ":";
138
- const parts = aspect.split(separator);
139
- if (parts.length !== 2) {
140
- throw new Error(`Invalid aspect ratio: ${aspect}. Use format "width:height" or "width/height"`);
141
- }
142
- const aspectWidth = parseFloat(parts[0]);
143
- const aspectHeight = parseFloat(parts[1]);
144
- if (isNaN(aspectWidth) || isNaN(aspectHeight) || aspectHeight === 0) {
145
- throw new Error(`Invalid aspect ratio: ${aspect}. Use format "width:height" (e.g., "3.5:2")`);
146
- }
147
-
148
- // Scale so height fits tape, width is proportional
149
- const height = tapeHeight;
150
- const width = Math.round(tapeHeight * (aspectWidth / aspectHeight));
151
- return { width, height };
152
- }
153
-
154
- // Validation
155
- function validateOptions(options: PrintOptions): void {
156
- const contentFields = [
157
- options.text,
158
- options.html,
159
- options.htmlPath,
160
- options.textFile,
161
- options.imagePath,
162
- options.imageBuffer,
163
- options.qr,
164
- ].filter(f => f !== undefined);
165
-
166
- if (contentFields.length === 0) {
167
- throw new Error("No content provided. Specify one of: text, html, htmlPath, textFile, imagePath, imageBuffer, qr");
168
- }
169
- if (contentFields.length > 1) {
170
- throw new Error("Multiple content options provided. Specify exactly one.");
171
- }
172
-
173
- if (options.tape !== undefined && !TAPE_SIZES[options.tape]) {
174
- throw new Error(`Invalid tape size: ${options.tape}. Valid sizes: 6, 9, 12, 18, 24`);
175
- }
176
- }
177
-
178
- // Resolve effective settings from options + config + defaults
179
- function resolveSettings(options: PrintOptions): { tape: TapeSize; printer: string } {
180
- const config = getConfig();
181
- return {
182
- tape: options.tape ?? config.defaultTape ?? 24,
183
- printer: options.printer ?? config.defaultPrinter ?? DEFAULT_PRINTER,
184
- };
185
- }
186
-
187
- // Parse text height spec to pixels
188
- function parseTextHeight(spec: string, tapeHeightPx: number): number {
189
- const trimmed = spec.trim().toLowerCase();
190
- if (trimmed.endsWith("%")) {
191
- const pct = parseFloat(trimmed.slice(0, -1));
192
- if (isNaN(pct) || pct <= 0) throw new Error(`Invalid text height: ${spec}`);
193
- return Math.round(tapeHeightPx * pct / 100);
194
- }
195
- if (trimmed.endsWith("mm")) {
196
- const mm = parseFloat(trimmed.slice(0, -2));
197
- if (isNaN(mm) || mm <= 0) throw new Error(`Invalid text height: ${spec}`);
198
- return Math.round(mm * PRINT_DPI / 25.4);
199
- }
200
- if (trimmed.endsWith("in")) {
201
- const inches = parseFloat(trimmed.slice(0, -2));
202
- if (isNaN(inches) || inches <= 0) throw new Error(`Invalid text height: ${spec}`);
203
- return Math.round(inches * PRINT_DPI);
204
- }
205
- throw new Error(`Invalid text height: ${spec}. Use "12mm", ".5in", or "50%"`);
206
- }
207
-
208
- // Render text to image buffer
209
- async function renderText(text: string, tape: TapeSize, textHeightSpec?: string): Promise<Buffer> {
210
- const tapeSize = TAPE_SIZES[tape];
211
- const targetHeight = textHeightSpec
212
- ? Math.min(parseTextHeight(textHeightSpec, tapeSize.height), tapeSize.height)
213
- : tapeSize.height;
214
- const lines = text.split("\\n").join("\n");
215
-
216
- // Load largest font for quality
217
- const fontDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "node_modules/@jimp/plugin-print/fonts/open-sans");
218
- const fontPath = path.join(fontDir, "open-sans-128-black", "open-sans-128-black.fnt");
219
- const font = await loadFont(fontPath);
220
-
221
- // Measure text
222
- const textWidth = measureText(font, lines);
223
- const textHeight = measureTextHeight(font, lines, textWidth + 100);
224
-
225
- // Create temp image for text
226
- const padding = 10;
227
- const imgWidth = textWidth + padding * 2;
228
- const imgHeight = textHeight + padding * 2;
229
-
230
- const tempImage = new Jimp({ width: imgWidth, height: imgHeight, color: 0xffffffff });
231
- tempImage.print({ font, x: padding, y: padding, text: lines });
232
-
233
- // Scale to fit tape height
234
- const scale = targetHeight / imgHeight;
235
- const finalWidth = Math.round(imgWidth * scale);
236
- const finalHeight = Math.round(imgHeight * scale);
237
- tempImage.resize({ w: finalWidth, h: finalHeight });
238
-
239
- // Create final image with padding
240
- const canvasHeight = tapeSize.height;
241
- const hPadding = Math.round(canvasHeight * 0.2);
242
- const outputWidth = finalWidth + hPadding * 2;
243
- const image = new Jimp({ width: outputWidth, height: canvasHeight, color: 0xffffffff });
244
-
245
- const xOffset = hPadding;
246
- const yOffset = Math.round((canvasHeight - finalHeight) / 2);
247
- image.composite(tempImage, xOffset, yOffset);
248
-
249
- return image.getBuffer("image/png");
250
- }
251
-
252
- // Render QR code to image buffer
253
- async function renderQr(data: string, tape: TapeSize, labelText?: string): Promise<Buffer> {
254
- const tapeSize = TAPE_SIZES[tape];
255
- const targetHeight = tapeSize.height;
256
- const qrSize = Math.floor(targetHeight * 0.95);
257
-
258
- // Margins in pixels at PRINT_DPI (3mm left, 4mm right)
259
- const pxPerMm = PRINT_DPI / 25.4;
260
- const leftMargin = Math.round(3 * pxPerMm);
261
- const rightMargin = Math.round(4 * pxPerMm);
262
-
263
- // Generate QR code at high resolution
264
- const qrBuffer = await QRCode.toBuffer(data, {
265
- type: "png",
266
- width: qrSize,
267
- margin: 0,
268
- errorCorrectionLevel: "M",
269
- color: { dark: "#000000", light: "#ffffff" },
270
- });
271
-
272
- const qrImage = await Jimp.read(qrBuffer);
273
-
274
- if (!labelText) {
275
- // QR only
276
- const outputWidth = leftMargin + qrSize + rightMargin;
277
- const image = new Jimp({ width: outputWidth, height: targetHeight, color: 0xffffffff });
278
- const yOffset = Math.floor((targetHeight - qrSize) / 2);
279
- image.composite(qrImage, leftMargin, yOffset);
280
- return image.getBuffer("image/png");
281
- }
282
-
283
- // QR + text label
284
- const fontDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "node_modules/@jimp/plugin-print/fonts/open-sans");
285
- const fontPath = path.join(fontDir, "open-sans-64-black", "open-sans-64-black.fnt");
286
- const font = await loadFont(fontPath);
287
-
288
- const textWidth = measureText(font, labelText);
289
- const textHeight = measureTextHeight(font, labelText, textWidth + 50);
290
-
291
- // Scale text to fit beside QR
292
- const maxTextHeight = targetHeight * 0.8;
293
- const textScale = Math.min(1, maxTextHeight / textHeight);
294
- const scaledTextWidth = Math.round(textWidth * textScale);
295
- const scaledTextHeight = Math.round(textHeight * textScale);
296
-
297
- // Create text image
298
- const textImg = new Jimp({ width: textWidth + 20, height: textHeight + 20, color: 0xffffffff });
299
- textImg.print({ font, x: 10, y: 10, text: labelText });
300
- if (textScale < 1) {
301
- textImg.resize({ w: scaledTextWidth, h: scaledTextHeight });
302
- }
303
-
304
- // Compose: QR on left, text on right
305
- const gap = Math.floor(targetHeight * 0.15);
306
- const outputWidth = leftMargin + qrSize + gap + scaledTextWidth + rightMargin;
307
- const image = new Jimp({ width: outputWidth, height: targetHeight, color: 0xffffffff });
308
-
309
- const qrY = Math.floor((targetHeight - qrSize) / 2);
310
- image.composite(qrImage, leftMargin, qrY);
311
-
312
- const textY = Math.floor((targetHeight - scaledTextHeight) / 2);
313
- image.composite(textImg, leftMargin + qrSize + gap, textY);
314
-
315
- return image.getBuffer("image/png");
316
- }
317
-
318
- // Render content to image buffer
319
- async function renderContent(options: PrintOptions, tape: TapeSize): Promise<Buffer> {
320
- const tapeSize = TAPE_SIZES[tape];
321
-
322
- if (options.text !== undefined) {
323
- return renderText(options.text, tape, options.textHeight);
324
- }
325
-
326
- if (options.textFile !== undefined) {
327
- const text = fs.readFileSync(options.textFile, "utf-8");
328
- return renderText(text, tape, options.textHeight);
329
- }
330
-
331
- if (options.html !== undefined) {
332
- const { width, height } = getHtmlDimensions(tapeSize.height, options.aspect);
333
- const buffer = await renderHtmlString(options.html, { width, height, tapeMm: tape }, options.basePath);
334
- await closeBrowser();
335
- return buffer;
336
- }
337
-
338
- if (options.htmlPath !== undefined) {
339
- const { width, height } = getHtmlDimensions(tapeSize.height, options.aspect);
340
- const buffer = await renderHtmlFile(options.htmlPath, { width, height, tapeMm: tape });
341
- await closeBrowser();
342
- return buffer;
343
- }
344
-
345
- if (options.imagePath !== undefined) {
346
- return fs.readFileSync(options.imagePath);
347
- }
348
-
349
- if (options.imageBuffer !== undefined) {
350
- return options.imageBuffer;
351
- }
352
-
353
- if (options.qr !== undefined) {
354
- return renderQr(options.qr, tape, options.qrLabel);
355
- }
356
-
357
- throw new Error("No content to render");
358
- }
359
-
360
- // Print single image buffer to printer
361
- async function printBuffer(imageBuffer: Buffer, printer: string, tape: TapeSize): Promise<void> {
362
- const media = MEDIA_OPTIONS[tape];
363
-
364
- const tempPath = path.join(os.tmpdir(), `label-${Date.now()}.png`);
365
- fs.writeFileSync(tempPath, imageBuffer);
366
-
367
- try {
368
- await new Promise<void>((resolve, reject) => {
369
- const psScript = `
370
- Add-Type -AssemblyName System.Drawing
371
- Add-Type -AssemblyName System.Printing
372
- Add-Type -AssemblyName ReachFramework
373
- Add-Type -AssemblyName PresentationCore
374
-
375
- $img = [System.Drawing.Image]::FromFile('${tempPath.replace(/\\/g, "\\\\")}')
376
-
377
- $DPI = ${PRINT_DPI}
378
- $MICRONS_PER_INCH = 25400
379
-
380
- $labelLengthMicrons = [int]($img.Width / $DPI * $MICRONS_PER_INCH) + 3000 # +2mm offset +1mm buffer
381
- $tapeWidthMicrons = ${media.width}
382
-
383
- $imgWidthWpf = $img.Width / $DPI * 96
384
- $imgHeightWpf = $img.Height / $DPI * 96
385
-
386
- $ticketXml = @"
387
- <?xml version="1.0" encoding="UTF-8"?>
388
- <psf:PrintTicket xmlns:psf="http://schemas.microsoft.com/windows/2003/08/printing/printschemaframework"
389
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1"
390
- xmlns:psk="http://schemas.microsoft.com/windows/2003/08/printing/printschemakeywords"
391
- xmlns:ns0001="http://schemas.brother.info/mfc/printing/2006/11/printschemakeywords">
392
- <psf:Feature name="psk:PageMediaSize">
393
- <psf:Option name="ns0001:${media.name}">
394
- <psf:ScoredProperty name="psk:MediaSizeWidth">
395
- <psf:Value xsi:type="xsd:integer">${media.width}</psf:Value>
396
- </psf:ScoredProperty>
397
- <psf:ScoredProperty name="psk:MediaSizeHeight">
398
- <psf:ParameterRef name="psk:PageMediaSizeMediaSizeHeight" />
399
- </psf:ScoredProperty>
400
- </psf:Option>
401
- </psf:Feature>
402
- <psf:ParameterInit name="psk:PageMediaSizeMediaSizeHeight">
403
- <psf:Value xsi:type="xsd:integer">$labelLengthMicrons</psf:Value>
404
- </psf:ParameterInit>
405
- <psf:Feature name="psk:PageOrientation">
406
- <psf:Option name="psk:Landscape" />
407
- </psf:Feature>
408
- <psf:Feature name="ns0001:PageRollFeedToEndOfSheet">
409
- <psf:Option name="ns0001:FeedToImageEdge" />
410
- </psf:Feature>
411
- <psf:Feature name="psk:PageMediaType">
412
- <psf:Option name="psk:Label" />
413
- </psf:Feature>
414
- <psf:Feature name="ns0001:JobRollCutAtEndOfJob">
415
- <psf:Option name="ns0001:Cut" />
416
- </psf:Feature>
417
- </psf:PrintTicket>
418
- "@
419
-
420
- $xmlBytes = [System.Text.Encoding]::UTF8.GetBytes($ticketXml)
421
- $memStream = New-Object System.IO.MemoryStream(,$xmlBytes)
422
- $ticket = New-Object System.Printing.PrintTicket($memStream)
423
-
424
- $server = New-Object System.Printing.LocalPrintServer
425
- $queue = $server.GetPrintQueue('${printer}')
426
-
427
- $xpsPath = [System.IO.Path]::GetTempFileName() + ".xps"
428
-
429
- $pageWidth = $labelLengthMicrons / $MICRONS_PER_INCH * 96
430
- $pageHeight = $tapeWidthMicrons / $MICRONS_PER_INCH * 96
431
-
432
- $package = [System.IO.Packaging.Package]::Open($xpsPath, [System.IO.FileMode]::Create)
433
- $xpsDoc = New-Object System.Windows.Xps.Packaging.XpsDocument($package)
434
- $writer = [System.Windows.Xps.Packaging.XpsDocument]::CreateXpsDocumentWriter($xpsDoc)
435
-
436
- $visual = New-Object System.Windows.Media.DrawingVisual
437
- $dc = $visual.RenderOpen()
438
-
439
- $bitmapImg = New-Object System.Windows.Media.Imaging.BitmapImage
440
- $bitmapImg.BeginInit()
441
- $bitmapImg.UriSource = New-Object System.Uri('${tempPath.replace(/\\/g, "\\\\")}')
442
- $bitmapImg.EndInit()
443
-
444
- $yOffset = ($pageHeight - $imgHeightWpf) / 2
445
- $xOffset = 2 / 25.4 * 96 # 2mm left margin in WPF units
446
- $rect = New-Object System.Windows.Rect($xOffset, $yOffset, $imgWidthWpf, $imgHeightWpf)
447
- $dc.DrawImage($bitmapImg, $rect)
448
- $dc.Close()
449
-
450
- $writer.Write($visual, $ticket)
451
-
452
- $xpsDoc.Close()
453
- $package.Close()
454
- $img.Dispose()
455
-
456
- $xpsDocForPrint = New-Object System.Windows.Xps.Packaging.XpsDocument($xpsPath, [System.IO.FileAccess]::Read)
457
- $seq = $xpsDocForPrint.GetFixedDocumentSequence()
458
-
459
- $xpsWriter = [System.Printing.PrintQueue]::CreateXpsDocumentWriter($queue)
460
- $xpsWriter.Write($seq, $ticket)
461
-
462
- $xpsDocForPrint.Close()
463
- Remove-Item $xpsPath -ErrorAction SilentlyContinue
464
- `;
465
-
466
- const ps = spawn("powershell", ["-Command", psScript], { stdio: "pipe" });
467
-
468
- let stdout = "";
469
- let stderr = "";
470
- ps.stdout.on("data", (data) => { stdout += data.toString(); });
471
- ps.stderr.on("data", (data) => { stderr += data.toString(); });
472
-
473
- ps.on("close", (code) => {
474
- if (code === 0) {
475
- resolve();
476
- } else {
477
- reject(new Error(`Print failed: ${stderr}`));
478
- }
479
- });
480
-
481
- ps.on("error", (err) => {
482
- reject(new Error(`Failed to print: ${err.message}`));
483
- });
484
- });
485
- } finally {
486
- if (fs.existsSync(tempPath)) {
487
- fs.unlinkSync(tempPath);
488
- }
489
- }
490
- }
491
-
492
- // Main API functions
493
- export async function render(options: PrintOptions): Promise<Buffer> {
494
- validateOptions(options);
495
- const { tape } = resolveSettings(options);
496
- return renderContent(options, tape);
497
- }
498
-
499
- export async function print(options: PrintOptions): Promise<PrintResult> {
500
- validateOptions(options);
501
- const { tape, printer } = resolveSettings(options);
502
- const image = await renderContent(options, tape);
503
- await printBuffer(image, printer, tape);
504
- return { image };
505
- }
1
+ /**
2
+ * Brother Label Printer API
3
+ * Programmatic interface for printing labels on Brother P-touch printers.
4
+ *
5
+ * Implements the unified LabelPrinter interface from @bobfrankston/label-core.
6
+ * All printer-agnostic logic (rendering, segments, clipboard, CLI primitives,
7
+ * online-wait) lives in label-core. This file holds the Brother-specific bits:
8
+ * TAPE_SIZES catalog, MEDIA_OPTIONS (CustomMediaSize* names), XPS PrintTicket
9
+ * builder with the Brother namespace, and detectTapeSize.
10
+ */
11
+
12
+ import * as fs from "fs";
13
+ import * as os from "os";
14
+ import * as path from "path";
15
+ import {
16
+ LabelPrinter,
17
+ PrintOptions as CorePrintOptions,
18
+ SegmentOptions as CoreSegmentOptions,
19
+ PrintResult as CorePrintResult,
20
+ PrinterStatus,
21
+ PrinterInfo as CorePrinterInfo,
22
+ PrinterConfig as CorePrinterConfig,
23
+ MediaInfo,
24
+ Segment,
25
+ WaitOptions,
26
+ validateContent,
27
+ resolveContent,
28
+ renderSegments as coreRenderSegments,
29
+ listPrinters as coreListPrinters,
30
+ getPrinterStatus,
31
+ waitOnline as coreWaitOnline,
32
+ runPowerShellOrThrow,
33
+ readJsonConfig,
34
+ mergeJsonConfig,
35
+ } from "@bobfrankston/label-core";
36
+
37
+ /* ----- Brother-specific types ----- */
38
+
39
+ export type TapeSize = 6 | 9 | 12 | 18 | 24;
40
+ export type Orientation = "landscape" | "portrait";
41
+
42
+ /** Backward-compat: brother-label's config kept the `defaultTape` name. */
43
+ export interface PrinterConfig {
44
+ defaultTape?: TapeSize;
45
+ defaultPrinter?: string;
46
+ }
47
+
48
+ export interface PrinterInfo {
49
+ name: string;
50
+ }
51
+
52
+ /** Brother-specific PrintOptions: same as core but with tape alias for media. */
53
+ export interface PrintOptions extends Omit<CorePrintOptions, "media"> {
54
+ tape?: TapeSize; /** Tape size in mm — alias for core.media */
55
+ orientation?: Orientation; /** Reserved (Brother PT prints landscape only) */
56
+ length?: number; /** Reserved explicit length in mm */
57
+ }
58
+
59
+ export interface SegmentOptions extends Omit<CoreSegmentOptions, "media"> {
60
+ tape?: TapeSize;
61
+ }
62
+
63
+ export interface PrintResult {
64
+ image: Buffer;
65
+ }
66
+
67
+ export type { Segment };
68
+
69
+ /* ----- Constants ----- */
70
+
71
+ const CONFIG_PATH = path.join(os.homedir(), ".brother-label.json");
72
+ const DEFAULT_PRINTER = "Brother PT-P710BT";
73
+ const DEFAULT_PRINTER_PATTERN = /brother|^pt-|^ql-/i;
74
+ const PRINT_DPI = 300;
75
+ const VALID_TAPES: TapeSize[] = [6, 9, 12, 18, 24];
76
+
77
+ /** Tape sizes: width in pixels @ 300 DPI = continuous direction; height = perpendicular. */
78
+ const TAPE_SIZES: Record<TapeSize, { width: number; height: number }> = {
79
+ 6: { width: 1137, height: 56 },
80
+ 9: { width: 1137, height: 84 },
81
+ 12: { width: 1137, height: 113 },
82
+ 18: { width: 1137, height: 169 },
83
+ 24: { width: 1137, height: 213 },
84
+ };
85
+
86
+ /** Brother CustomMediaSize names + tape widths in microns (XPS PrintTicket). */
87
+ const MEDIA_OPTIONS: Record<TapeSize, { name: string; width: number }> = {
88
+ 6: { name: "CustomMediaSize257", width: 5900 },
89
+ 9: { name: "CustomMediaSize258", width: 9000 },
90
+ 12: { name: "CustomMediaSize259", width: 11900 },
91
+ 18: { name: "CustomMediaSize260", width: 18100 },
92
+ 24: { name: "CustomMediaSize261", width: 24000 },
93
+ };
94
+
95
+ /* ----- Config (backward-compat note "defaultTape" stays in JSON) ----- */
96
+
97
+ export function getConfig(): PrinterConfig {
98
+ const raw: any = readJsonConfig(CONFIG_PATH);
99
+ if (typeof raw.defaultTape === "string") {
100
+ raw.defaultTape = parseInt(raw.defaultTape.replace("mm", ""), 10) as TapeSize;
101
+ }
102
+ return raw;
103
+ }
104
+
105
+ export function setConfig(config: Partial<PrinterConfig>): void {
106
+ mergeJsonConfig(CONFIG_PATH, config);
107
+ }
108
+
109
+ export function getConfigPath(): string {
110
+ return CONFIG_PATH;
111
+ }
112
+
113
+ /* ----- Printer listing (backward-compat shape: { name }[]) ----- */
114
+
115
+ export async function listPrinters(): Promise<PrinterInfo[]> {
116
+ const ps = await coreListPrinters(DEFAULT_PRINTER_PATTERN);
117
+ return ps.map(p => ({ name: p.name }));
118
+ }
119
+
120
+ /**
121
+ * Auto-detect tape size from printer's default print ticket. Returns null if
122
+ * detection fails. Brother's driver embeds the loaded tape's CustomMediaSize
123
+ * name in the default PrintTicket XML.
124
+ */
125
+ export async function detectTapeSize(printerName?: string): Promise<TapeSize> {
126
+ const config = getConfig();
127
+ const printer = printerName ?? config.defaultPrinter ?? DEFAULT_PRINTER;
128
+ const script = `
129
+ $ErrorActionPreference = 'SilentlyContinue'
130
+ Add-Type -AssemblyName System.Printing | Out-Null
131
+ try {
132
+ $server = New-Object System.Printing.LocalPrintServer
133
+ $queue = $server.GetPrintQueue('${printer.replace(/'/g, "''")}')
134
+ $ticket = $queue.DefaultPrintTicket
135
+ $stream = $ticket.GetXmlStream()
136
+ $reader = New-Object System.IO.StreamReader($stream)
137
+ Write-Output $reader.ReadToEnd()
138
+ $reader.Close()
139
+ } catch {
140
+ Write-Output "ERROR"
141
+ }
142
+ `;
143
+ try {
144
+ const out = await runPowerShellOrThrow(script);
145
+ for (const [size, media] of Object.entries(MEDIA_OPTIONS)) {
146
+ if (out.includes(media.name)) return parseInt(size, 10) as TapeSize;
147
+ }
148
+ } catch { /* fall through */ }
149
+ return null;
150
+ }
151
+
152
+ /* ----- Brother LabelPrinter implementation ----- */
153
+
154
+ class BrotherPrinterImpl implements LabelPrinter {
155
+ readonly driverName = "brother";
156
+
157
+ getConfig(): CorePrinterConfig {
158
+ const c = getConfig();
159
+ return {
160
+ defaultPrinter: c.defaultPrinter,
161
+ defaultMedia: c.defaultTape !== undefined ? String(c.defaultTape) : undefined,
162
+ };
163
+ }
164
+ setConfig(config: Partial<CorePrinterConfig>): void {
165
+ const update: Partial<PrinterConfig> = {};
166
+ if (config.defaultPrinter !== undefined) update.defaultPrinter = config.defaultPrinter;
167
+ if (config.defaultMedia !== undefined) {
168
+ const t = parseInt(config.defaultMedia, 10) as TapeSize;
169
+ if (VALID_TAPES.includes(t)) update.defaultTape = t;
170
+ }
171
+ setConfig(update);
172
+ }
173
+ getConfigPath(): string {
174
+ return CONFIG_PATH;
175
+ }
176
+
177
+ listMedia(): MediaInfo[] {
178
+ return VALID_TAPES.map(t => ({
179
+ id: String(t),
180
+ name: `${t}mm tape`,
181
+ widthPx: TAPE_SIZES[t].height, /** "media width" (perpendicular to feed) */
182
+ lengthPx: 0, /** continuous tape length determined by content */
183
+ continuous: true,
184
+ }));
185
+ }
186
+ getMedia(id: string): MediaInfo {
187
+ const t = parseInt(id.replace("mm", ""), 10) as TapeSize;
188
+ if (!VALID_TAPES.includes(t)) return null;
189
+ return {
190
+ id: String(t), name: `${t}mm tape`,
191
+ widthPx: TAPE_SIZES[t].height, lengthPx: 0, continuous: true,
192
+ };
193
+ }
194
+ async detectMedia(printerName?: string): Promise<MediaInfo> {
195
+ const t = await detectTapeSize(printerName);
196
+ return t ? this.getMedia(String(t)) : null;
197
+ }
198
+
199
+ async listPrinters(): Promise<CorePrinterInfo[]> {
200
+ return coreListPrinters(DEFAULT_PRINTER_PATTERN);
201
+ }
202
+ async getStatus(printerName?: string): Promise<PrinterStatus> {
203
+ const printer = printerName ?? this.getConfig().defaultPrinter ?? DEFAULT_PRINTER;
204
+ return getPrinterStatus(printer);
205
+ }
206
+ async waitOnline(printerName?: string, opts: WaitOptions = {}): Promise<PrinterStatus> {
207
+ const printer = printerName ?? this.getConfig().defaultPrinter ?? DEFAULT_PRINTER;
208
+ return coreWaitOnline(printer, opts, DEFAULT_PRINTER_PATTERN);
209
+ }
210
+
211
+ async render(opts: CorePrintOptions): Promise<Buffer> {
212
+ validateContent(opts);
213
+ const tape = await this.resolveTape(opts.media);
214
+ return resolveContent(opts, TAPE_SIZES[tape].height, tape);
215
+ }
216
+
217
+ async print(opts: CorePrintOptions): Promise<CorePrintResult> {
218
+ validateContent(opts);
219
+ const printer = opts.printer ?? this.getConfig().defaultPrinter ?? DEFAULT_PRINTER;
220
+ const tape = await this.resolveTape(opts.media);
221
+ const image = await resolveContent(opts, TAPE_SIZES[tape].height, tape);
222
+ await ensureOnline(printer, opts);
223
+ await printBuffer(image, printer, tape);
224
+ return { image, media: this.getMedia(String(tape)), printer };
225
+ }
226
+
227
+ async renderSegments(segments: Segment[], opts: CoreSegmentOptions = {}): Promise<Buffer> {
228
+ const tape = await this.resolveTape(opts.media);
229
+ return coreRenderSegments(segments, TAPE_SIZES[tape].height, opts.textHeight, opts.space);
230
+ }
231
+ async printSegments(segments: Segment[], opts: CoreSegmentOptions = {}): Promise<CorePrintResult> {
232
+ const printer = opts.printer ?? this.getConfig().defaultPrinter ?? DEFAULT_PRINTER;
233
+ const tape = await this.resolveTape(opts.media);
234
+ const image = await coreRenderSegments(segments, TAPE_SIZES[tape].height, opts.textHeight, opts.space);
235
+ await ensureOnline(printer, opts);
236
+ await printBuffer(image, printer, tape);
237
+ return { image, media: this.getMedia(String(tape)), printer };
238
+ }
239
+
240
+ private async resolveTape(media?: string): Promise<TapeSize> {
241
+ if (media !== undefined) {
242
+ const t = parseInt(media.replace("mm", ""), 10) as TapeSize;
243
+ if (!VALID_TAPES.includes(t)) throw new Error(`Invalid tape size: ${media}. Valid: ${VALID_TAPES.join(", ")}`);
244
+ return t;
245
+ }
246
+ const cfg = getConfig();
247
+ if (cfg.defaultTape) return cfg.defaultTape;
248
+ const detected = await detectTapeSize();
249
+ return detected ?? 24;
250
+ }
251
+ }
252
+
253
+ /** Public Brother singleton (implements LabelPrinter). */
254
+ export const brotherPrinter: LabelPrinter = new BrotherPrinterImpl();
255
+
256
+ /* ----- Backward-compatible function exports ----- */
257
+
258
+ /**
259
+ * Translate brother-label PrintOptions (with tape) to core (with media).
260
+ */
261
+ function toCore(opts: PrintOptions): CorePrintOptions {
262
+ const out: CorePrintOptions = { ...opts };
263
+ if (opts.tape !== undefined && out.media === undefined) {
264
+ out.media = String(opts.tape);
265
+ }
266
+ delete (out as any).tape;
267
+ return out;
268
+ }
269
+ function toCoreSeg(opts: SegmentOptions = {}): CoreSegmentOptions {
270
+ const out: CoreSegmentOptions = { ...opts };
271
+ if (opts.tape !== undefined && out.media === undefined) out.media = String(opts.tape);
272
+ delete (out as any).tape;
273
+ return out;
274
+ }
275
+
276
+ export async function render(options: PrintOptions): Promise<Buffer> {
277
+ return brotherPrinter.render(toCore(options));
278
+ }
279
+
280
+ export async function print(options: PrintOptions): Promise<PrintResult> {
281
+ const r = await brotherPrinter.print(toCore(options));
282
+ return { image: r.image };
283
+ }
284
+
285
+ export async function renderSegments(segments: Segment[], tape?: TapeSize, textHeight?: string, space?: string): Promise<Buffer> {
286
+ return brotherPrinter.renderSegments(segments, {
287
+ media: tape !== undefined ? String(tape) : undefined,
288
+ textHeight, space,
289
+ });
290
+ }
291
+
292
+ export async function printSegments(segments: Segment[], options?: SegmentOptions): Promise<PrintResult> {
293
+ const r = await brotherPrinter.printSegments(segments, toCoreSeg(options));
294
+ return { image: r.image };
295
+ }
296
+
297
+ /* ----- Brother XPS print path (kept here, Brother-specific) ----- */
298
+
299
+ async function ensureOnline(printer: string, opts: { wait?: boolean; waitTimeoutMs?: number; waitIntervalMs?: number; onWaiting?: CorePrintOptions["onWaiting"]; log?: CorePrintOptions["log"] }): Promise<void> {
300
+ const status = await getPrinterStatus(printer);
301
+ if (status.online) return;
302
+ if (opts.wait === false) {
303
+ throw new Error(`Printer "${printer}" is offline (${status.statusText}${status.error ? ", " + status.error : ""}). Use wait:true to wait, or pick another printer.`);
304
+ }
305
+ if (opts.log) opts.log(`[brother-label] ${printer} offline (${status.statusText}); waiting...`);
306
+ await coreWaitOnline(printer, {
307
+ timeoutMs: opts.waitTimeoutMs,
308
+ intervalMs: opts.waitIntervalMs,
309
+ onWaiting: opts.onWaiting,
310
+ log: opts.log,
311
+ }, DEFAULT_PRINTER_PATTERN);
312
+ }
313
+
314
+ /**
315
+ * Print a single image buffer using the Brother XPS PrintTicket path.
316
+ * Builds an XPS document with a custom CustomMediaSize for the tape, then
317
+ * sends it to the print queue.
318
+ */
319
+ async function printBuffer(imageBuffer: Buffer, printer: string, tape: TapeSize): Promise<void> {
320
+ const media = MEDIA_OPTIONS[tape];
321
+ const tempPath = path.join(os.tmpdir(), `label-${Date.now()}.png`);
322
+ fs.writeFileSync(tempPath, imageBuffer);
323
+ const psPath = tempPath.replace(/\\/g, "\\\\");
324
+
325
+ const script = `
326
+ $ErrorActionPreference = 'Stop'
327
+ Add-Type -AssemblyName System.Drawing
328
+ Add-Type -AssemblyName System.Printing
329
+ Add-Type -AssemblyName ReachFramework
330
+ Add-Type -AssemblyName PresentationCore
331
+
332
+ $img = [System.Drawing.Image]::FromFile('${psPath}')
333
+
334
+ $DPI = ${PRINT_DPI}
335
+ $MICRONS_PER_INCH = 25400
336
+
337
+ $labelLengthMicrons = [int]($img.Width / $DPI * $MICRONS_PER_INCH) + 3000
338
+ $tapeWidthMicrons = ${media.width}
339
+
340
+ $imgWidthWpf = $img.Width / $DPI * 96
341
+ $imgHeightWpf = $img.Height / $DPI * 96
342
+
343
+ $ticketXml = @"
344
+ <?xml version="1.0" encoding="UTF-8"?>
345
+ <psf:PrintTicket xmlns:psf="http://schemas.microsoft.com/windows/2003/08/printing/printschemaframework"
346
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1"
347
+ xmlns:psk="http://schemas.microsoft.com/windows/2003/08/printing/printschemakeywords"
348
+ xmlns:ns0001="http://schemas.brother.info/mfc/printing/2006/11/printschemakeywords">
349
+ <psf:Feature name="psk:PageMediaSize">
350
+ <psf:Option name="ns0001:${media.name}">
351
+ <psf:ScoredProperty name="psk:MediaSizeWidth">
352
+ <psf:Value xsi:type="xsd:integer">${media.width}</psf:Value>
353
+ </psf:ScoredProperty>
354
+ <psf:ScoredProperty name="psk:MediaSizeHeight">
355
+ <psf:ParameterRef name="psk:PageMediaSizeMediaSizeHeight" />
356
+ </psf:ScoredProperty>
357
+ </psf:Option>
358
+ </psf:Feature>
359
+ <psf:ParameterInit name="psk:PageMediaSizeMediaSizeHeight">
360
+ <psf:Value xsi:type="xsd:integer">$labelLengthMicrons</psf:Value>
361
+ </psf:ParameterInit>
362
+ <psf:Feature name="psk:PageOrientation">
363
+ <psf:Option name="psk:Landscape" />
364
+ </psf:Feature>
365
+ <psf:Feature name="ns0001:PageRollFeedToEndOfSheet">
366
+ <psf:Option name="ns0001:FeedToImageEdge" />
367
+ </psf:Feature>
368
+ <psf:Feature name="psk:PageMediaType">
369
+ <psf:Option name="psk:Label" />
370
+ </psf:Feature>
371
+ <psf:Feature name="ns0001:JobRollCutAtEndOfJob">
372
+ <psf:Option name="ns0001:Cut" />
373
+ </psf:Feature>
374
+ </psf:PrintTicket>
375
+ "@
376
+
377
+ $xmlBytes = [System.Text.Encoding]::UTF8.GetBytes($ticketXml)
378
+ $memStream = New-Object System.IO.MemoryStream(,$xmlBytes)
379
+ $ticket = New-Object System.Printing.PrintTicket($memStream)
380
+
381
+ $server = New-Object System.Printing.LocalPrintServer
382
+ $queue = $server.GetPrintQueue('${printer.replace(/'/g, "''")}')
383
+
384
+ $xpsPath = [System.IO.Path]::GetTempFileName() + ".xps"
385
+
386
+ $pageWidth = $labelLengthMicrons / $MICRONS_PER_INCH * 96
387
+ $pageHeight = $tapeWidthMicrons / $MICRONS_PER_INCH * 96
388
+
389
+ $package = [System.IO.Packaging.Package]::Open($xpsPath, [System.IO.FileMode]::Create)
390
+ $xpsDoc = New-Object System.Windows.Xps.Packaging.XpsDocument($package)
391
+ $writer = [System.Windows.Xps.Packaging.XpsDocument]::CreateXpsDocumentWriter($xpsDoc)
392
+
393
+ $visual = New-Object System.Windows.Media.DrawingVisual
394
+ $dc = $visual.RenderOpen()
395
+
396
+ $bitmapImg = New-Object System.Windows.Media.Imaging.BitmapImage
397
+ $bitmapImg.BeginInit()
398
+ $bitmapImg.UriSource = New-Object System.Uri('${psPath}')
399
+ $bitmapImg.EndInit()
400
+
401
+ $yOffset = ($pageHeight - $imgHeightWpf) / 2
402
+ $xOffset = 2 / 25.4 * 96
403
+ $rect = New-Object System.Windows.Rect($xOffset, $yOffset, $imgWidthWpf, $imgHeightWpf)
404
+ $dc.DrawImage($bitmapImg, $rect)
405
+ $dc.Close()
406
+
407
+ $writer.Write($visual, $ticket)
408
+
409
+ $xpsDoc.Close()
410
+ $package.Close()
411
+ $img.Dispose()
412
+
413
+ $xpsDocForPrint = New-Object System.Windows.Xps.Packaging.XpsDocument($xpsPath, [System.IO.FileAccess]::Read)
414
+ $seq = $xpsDocForPrint.GetFixedDocumentSequence()
415
+
416
+ $xpsWriter = [System.Printing.PrintQueue]::CreateXpsDocumentWriter($queue)
417
+ $xpsWriter.Write($seq, $ticket)
418
+
419
+ $xpsDocForPrint.Close()
420
+ Remove-Item $xpsPath -ErrorAction SilentlyContinue
421
+ `;
422
+ try {
423
+ await runPowerShellOrThrow(script);
424
+ } finally {
425
+ if (fs.existsSync(tempPath)) fs.unlinkSync(tempPath);
426
+ }
427
+ }