brother_ql 3.0.1

Compile and print images using Brother QL label printers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Media type definitions and settings
//!
//! This module defines all supported label media types for Brother QL printers.
//! Media types include both continuous rolls and die-cut labels in various sizes.
//!
//! # Media Naming Convention
//!
//! - **C** prefix = Continuous roll (e.g., [`Media::C62`] = 62mm continuous)
//! - **D** prefix = Die-cut labels (e.g., [`Media::D17x54`] = 17mm × 54mm labels)
//! - **R** suffix = Red/black two-color support (e.g., [`Media::C62R`])
//!
//! # Continuous vs. Die-Cut
//!
//! **Continuous rolls** can be cut to any length by the printer. The image height
//! can be any size, and the printer will feed the appropriate amount of media.
//!
//! **Die-cut labels** are pre-cut to specific dimensions. Your image must match
//! the exact label dimensions, or you'll get a dimension mismatch error.
//!
//! # Image Dimensions
//!
//! The required image dimensions in pixels are documented in [`Media`]
//!
//! # Color Printing
//!
//! Only [`Media::C62R`] currently supports two-color (black/red) printing.
//! All other media types support black-only printing.
//!
//! See [`PrintJobBuilder`](crate::printjob::PrintJobBuilder) for details.

use crate::error::StatusParsingError;

/// Type of label media
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum LabelType {
    /// Continuous roll media (cut to any length)
    Continuous,
    /// Die-cut pre-sized labels
    DieCut,
}

impl TryFrom<u8> for LabelType {
    type Error = StatusParsingError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            // The documentation states 0x4a and 0x4b instead
            0x0a => Ok(Self::Continuous),
            0x0b => Ok(Self::DieCut),
            invalid => Err(StatusParsingError {
                reason: format!("invalid media type code {invalid:#x}"),
            }),
        }
    }
}

macro_rules! define_media {
    // Optional literal → Option
    (@opt $val:literal) => { Some($val) };
    (@opt) => { None };

    // Bullet list formatting
    (@products [$($product:literal),+]) => {
        concat!($("\n- ", $product),+)
    };

    // Continuous tape
    (@ty_doc Continuous, , , $wmm:literal, $wpx:literal) => {
        concat!(
            "Continuous tape, ", $wmm, "mm (", $wpx, "px) wide"
        )
    };

    // DieCut labels
    (@ty_doc DieCut, $lmm:literal, $lpx:literal, $wmm:literal, $wpx:literal) => {
        concat!(
            "Die-cut labels, WxH ", $wmm, "x", $lmm, "mm (", $wpx, "x", $lpx, "px)"
        )
    };

    (
        $(
            $name:ident {
                products: [$($product:literal),+ $(,)?],
                label_type: $label_type:ident,
                width_mm: $width_mm:literal,
                width_dots: $width_dots:literal,
                left_margin: $left_margin:literal,
                supports_color: $supports_color:literal,
                $( length_mm: $length_mm:literal, length_dots: $length_dots:literal, )?
            }
        ),+ $(,)?
    ) => {
        /// Available media types for Brother QL printers
        #[derive(Copy, PartialEq, Clone, Debug, strum::EnumIter, strum::Display)]
        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
        #[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
        pub enum Media {
            $(
                #[doc = concat!(
                    define_media!(
                        @ty_doc
                        $label_type,
                        $( $length_mm )?,
                        $( $length_dots )?,
                        $width_mm,
                        $width_dots
                    ),
                    "\n\n**Compatible products:**",
                    define_media!(@products [$($product),+])
                )]
                $name,
            )+
        }

        impl Media {
            /// Returns the label type (`Continuous` or `DieCut`)
            pub const fn label_type(self) -> LabelType {
                match self { $( Media::$name => LabelType::$label_type ),+ }
            }
            /// Returns the media width in millimeters
            pub const fn width_mm(self) -> u8 {
                match self { $( Media::$name => $width_mm ),+ }
            }
            /// Returns the media width in dots (at 300 DPI)
            pub const fn width_dots(self) -> u32 {
                match self { $( Media::$name => $width_dots ),+ }
            }
            /// Returns the left margin in dots
            pub(crate) const fn left_margin(self) -> u32 {
                match self { $( Media::$name => $left_margin ),+ }
            }
            /// Returns whether this media supports red/black two-color printing
            pub const fn supports_color(self) -> bool {
                match self { $( Media::$name => $supports_color ),+ }
            }
            /// Returns the label length in millimeters (`None` for continuous media)
            pub const fn length_mm(self) -> Option<u8> {
                match self { $( Media::$name => define_media!(@opt $( $length_mm )? ) ),+ }
            }
            /// Returns the label length in dots (None for continuous media)
            pub const fn length_dots(self) -> Option<u32> {
                match self { $( Media::$name => define_media!(@opt $( $length_dots )? ) ),+ }
            }
        }
    };
}

impl Media {
    /// Infer the media variant from printer-reported status fields.
    ///
    /// Returns `None` if no known media matches the given parameters.
    pub fn from_status_info(label_type: LabelType, width_mm: u8, length_mm: u8) -> Option<Media> {
        use strum::IntoEnumIterator;
        Media::iter().find(|m| {
            m.label_type() == label_type
                && m.width_mm() == width_mm
                && m.length_mm().unwrap_or(0) == length_mm
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use strum::IntoEnumIterator;

    #[test]
    fn all_media_fit_within_print_head() {
        for media in Media::iter() {
            assert!(
                media.left_margin() + media.width_dots() <= 720,
                "{media:?}: left_margin({}) + width_dots({}) = {} > 720",
                media.left_margin(),
                media.width_dots(),
                media.left_margin() + media.width_dots()
            );
        }
    }

    #[test]
    fn continuous_media_has_no_length() {
        for media in Media::iter() {
            if media.label_type() == LabelType::Continuous {
                assert!(
                    media.length_mm().is_none(),
                    "{media:?} is continuous but has length_mm"
                );
                assert!(
                    media.length_dots().is_none(),
                    "{media:?} is continuous but has length_dots"
                );
            }
        }
    }

    #[test]
    fn die_cut_media_has_length() {
        for media in Media::iter() {
            if media.label_type() == LabelType::DieCut {
                assert!(
                    media.length_mm().is_some(),
                    "{media:?} is die-cut but has no length_mm"
                );
                assert!(
                    media.length_dots().is_some(),
                    "{media:?} is die-cut but has no length_dots"
                );
            }
        }
    }

    #[test]
    fn from_status_info_continuous() {
        let media = Media::from_status_info(LabelType::Continuous, 62, 0);
        assert_eq!(media, Some(Media::C62));
    }

    #[test]
    fn from_status_info_die_cut() {
        let media = Media::from_status_info(LabelType::DieCut, 24, 24);
        assert_eq!(media, Some(Media::D24));
    }

    #[test]
    fn from_status_info_no_match() {
        let media = Media::from_status_info(LabelType::Continuous, 99, 0);
        assert!(media.is_none());
    }

    #[test]
    fn width_dots_nonzero() {
        for media in Media::iter() {
            assert!(media.width_dots() > 0, "{media:?} has zero width_dots");
        }
    }
}

define_media! {
    C12 {
        products: ["DK-22214 Tape"],
        label_type: Continuous,
        width_mm: 12,
        width_dots: 106,
        left_margin: 585,
        supports_color: false,
    },
    C29 {
        products: ["DK-22210 Tape", "DK-22211 Film Tape"],
        label_type: Continuous,
        width_mm: 29,
        width_dots: 306,
        left_margin: 408,
        supports_color: false,
    },
    C38 {
        products: ["DK-22225 Tape"],
        label_type: Continuous,
        width_mm: 38,
        width_dots: 413,
        left_margin: 295,
        supports_color: false,
    },
    C50 {
        products: ["DK-22223 Tape"],
        label_type: Continuous,
        width_mm: 50,
        width_dots: 554,
        left_margin: 154,
        supports_color: false,
    },
    C54 {
        products: ["DK-N55224 Non-Adhesive Tape"],
        label_type: Continuous,
        width_mm: 54,
        width_dots: 590,
        left_margin: 130,
        supports_color: false,
    },
    C62 {
        products: ["DK-22205 Tape", "DK-22212 Film Tape", "DK-22606 Yellow Film Tape", "DDK-22213 Transparent Tape", "DK-44205 Removable Tape", "DK-44605 Yellow Removable Tape"],
        label_type: Continuous,
        width_mm: 62,
        width_dots: 696,
        left_margin: 12,
        supports_color: false,
    },
    C62R {
        products: ["DK-22251 Red/Black Tape"],
        label_type: Continuous,
        width_mm: 62,
        width_dots: 696,
        left_margin: 12,
        supports_color: true,
    },
    D17x54 {
        products: ["DK-11204 Labels"],
        label_type: DieCut,
        width_mm: 17,
        width_dots: 165,
        left_margin: 555,
        supports_color: false,
        length_mm: 54,
        length_dots: 566,
    },
    D17x87 {
        products: ["DK-11203 File Folder Labels"],
        label_type: DieCut,
        width_mm: 17,
        width_dots: 165,
        left_margin: 555,
        supports_color: false,
        length_mm: 87,
        length_dots: 912,
    },
    D23x23 {
        products: ["DK-11221 Square Labels"],
        label_type: DieCut,
        width_mm: 23,
        width_dots: 236,
        left_margin: 442,
        supports_color: false,
        length_mm: 23,
        length_dots: 236,
    },
    D29x42 {
        products: ["DK-11215 Labels (⚠️ no official data)"],
        label_type: DieCut,
        width_mm: 29,
        width_dots: 306,
        left_margin: 408,
        supports_color: false,
        length_mm: 42,
        length_dots: 442,
    },
    D29x90 {
        products: ["DK-11201 Standard Address Labels"],
        label_type: DieCut,
        width_mm: 29,
        width_dots: 306,
        left_margin: 408,
        supports_color: false,
        length_mm: 90,
        length_dots: 944,
    },
    D38x90 {
        products: ["DK-11208 Large Address Labels"],
        label_type: DieCut,
        width_mm: 38,
        width_dots: 413,
        left_margin: 295,
        supports_color: false,
        length_mm: 90,
        length_dots: 944,
    },
    D39x48 {
        products: ["DK-11220 Labels (⚠️ no official data)"],
        label_type: DieCut,
        width_mm: 39,
        width_dots: 425,
        left_margin: 289,
        supports_color: false,
        length_mm: 48,
        length_dots: 512,
    },
    D52x29 {
        products: ["DK-11226 Labels (⚠️ no official data)"],
        label_type: DieCut,
        width_mm: 52,
        width_dots: 578,
        left_margin: 142,
        supports_color: false,
        length_mm: 29,
        length_dots: 318,
    },
    D54x29 {
        products: ["DK-3235 Removable"],
        label_type: DieCut,
        width_mm: 54,
        width_dots: 602,
        left_margin: 59,
        supports_color: false,
        length_mm: 29,
        length_dots: 318,
    },
    D60x86 {
        products: ["DK-11234 Name Badge Labels"],
        label_type: DieCut,
        width_mm: 60,
        width_dots: 672,
        left_margin: 24,
        supports_color: false,
        length_mm: 86,
        length_dots: 902,
    },
    D62x29 {
        products: ["DK-11209 Small Address Labels"],
        label_type: DieCut,
        width_mm: 62,
        width_dots: 696,
        left_margin: 12,
        supports_color: false,
        length_mm: 29,
        length_dots: 318,
    },
    D62x100 {
        products: ["DK-11202 Shipping Labels"],
        label_type: DieCut,
        width_mm: 62,
        width_dots: 696,
        left_margin: 12,
        supports_color: false,
        length_mm: 100,
        length_dots: 1104,
    },
    D12 {
        products: ["DK-11219 Round Labels"],
        label_type: DieCut,
        width_mm: 12,
        width_dots: 94,
        left_margin: 513,
        supports_color: false,
        length_mm: 12,
        length_dots: 94,
    },
    D24 {
        products: ["DK-11218 Round Labels"],
        label_type: DieCut,
        width_mm: 24,
        width_dots: 236,
        left_margin: 442,
        supports_color: false,
        length_mm: 24,
        length_dots: 236,
    },
    D58 {
        products: ["DK-11207 CD/DVD Labels"],
        label_type: DieCut,
        width_mm: 58,
        width_dots: 618,
        left_margin: 51,
        supports_color: false,
        length_mm: 58,
        length_dots: 630,
    },
}