zpl 0.3.2

A high accuracy ZPL renderer
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! Original ISO/IEC 16023 encoder. See docs/barcodes.md for source specifications.
use super::*;
fn set_a(c: u8) -> Option<usize> {
    match c {
        13 => Some(0),
        65..=90 => Some((c - 64) as usize),
        28..=30 | 32 | 34..=58 => Some(c as usize),
        _ => None,
    }
}
fn character(c: u8) -> (usize, usize) {
    if let Some(v) = set_a(c) {
        return (0, v);
    }
    if (96..=122).contains(&c) {
        return (59, (c - 96) as usize);
    }
    const B: &[u8] = &[
        123, 0, 125, 126, 127, 59, 60, 61, 62, 63, 91, 92, 93, 94, 95, 32, 44, 46, 47, 58, 64, 33,
        124,
    ];
    if c != 0 {
        if let Some(i) = B.iter().position(|&v| v == c) {
            return (59, 32 + i);
        }
    }
    if c <= 26 {
        return (62, c as usize);
    }
    if c == 27 {
        return (62, 30);
    }
    if c == 31 {
        return (62, 35);
    }
    if c >= 192 {
        return (
            if c >= 224 { 61 } else { 60 },
            if c % 32 < 27 {
                (c % 32) as usize
            } else {
                (c % 32 + 5) as usize
            },
        );
    }
    const C: &[u8] = &[
        170, 172, 177, 178, 179, 181, 185, 186, 188, 189, 190, 128, 129, 130, 131, 132, 133, 134,
        135, 136, 137,
    ];
    const D: &[u8] = &[
        161, 168, 171, 175, 176, 180, 183, 184, 187, 191, 138, 139, 140, 141, 142, 143, 144, 145,
        146, 147, 148,
    ];
    const E: &[u8] = &[
        159, 160, 162, 163, 164, 165, 166, 167, 169, 173, 174, 182, 149, 150, 151, 152, 153, 154,
        155, 156, 157, 158,
    ];
    for (shift, start, table) in [(60, 37, C), (61, 37, D), (62, 36, E)] {
        if let Some(i) = table.iter().position(|&v| v == c) {
            return (shift, start + i);
        }
    }
    unreachable!("MaxiCode character tables cover all 256 byte values")
}
// ISO/IEC 16023:2000 Annex A pp. 24–25, Annex F.1–F.4 pp. 32–33.
// Original implementation of the recommended run-based switching rules.
fn compact(data: &[u8], include_shared_b: bool) -> (Vec<usize>, usize) {
    fn value(c: u8, set: usize) -> Option<usize> {
        if set == 0 {
            return set_a(c);
        }
        if set == 1 {
            if (28..=30).contains(&c) {
                return Some(c as usize);
            }
            if (96..=122).contains(&c) {
                return Some((c - 96) as usize);
            }
            return b"{\0}~\x7f;<=>?[\\]^_ ,./:@!|"
                .iter()
                .position(|&v| v == c && c != 0)
                .map(|v| v + 32);
        }
        if c == 32 {
            return Some(59);
        }
        if (28..=30).contains(&c) {
            return Some(c as usize + if set == 4 { 4 } else { 0 });
        }
        if set == 4 && c <= 26 {
            return Some(c as usize);
        }
        let (shift, v) = character(c);
        (shift == set + 58).then_some(v)
    }
    let (mut out, mut set, mut i) = (Vec::new(), 0, 0);
    while i < data.len() {
        let tail = &data[i..];
        if tail.len() >= 9 && tail[..9].iter().all(u8::is_ascii_digit) {
            let n = tail[..9]
                .iter()
                .fold(0usize, |n, v| n * 10 + (v - b'0') as usize);
            out.push(31);
            out.extend((0..5).rev().map(|k| (n >> (6 * k)) & 63));
            i += 9;
            continue;
        }
        let run = |target| {
            tail.iter()
                .take_while(|&&c| value(c, target).is_some())
                .count()
        };
        if set == 1 && data[i] != b' ' && run(0) >= 4 {
            out.push(63);
            set = 0;
        }
        if let Some(v) = value(data[i], set) {
            out.push(v);
            i += 1;
            continue;
        }
        let target = (0..5).find(|&s| value(data[i], s).is_some()).unwrap();
        let count = if set == 0 && target == 1 && !include_shared_b {
            // Match native B-run boundaries at characters also available in A.
            // Native controls include B + RS + B: shift the first B character,
            // emit RS in A, then decide afresh whether to latch for the next run.
            tail.iter()
                .take_while(|&&c| value(c, 1).is_some() && value(c, 0).is_none())
                .count()
        } else {
            run(target)
        };
        if target < 2 {
            if set >= 2 || target == 1 && count >= 2 || target == 0 && count >= 4 {
                out.push(if target == 0 && set >= 2 { 58 } else { 63 });
                set = target;
            } else {
                let count = if target == 0 { count.min(3) } else { 1 };
                out.push(if count == 1 { 59 } else { 54 + count });
                for &c in &tail[..count] {
                    out.push(value(c, target).unwrap());
                }
                i += count;
            }
        } else {
            let shift = target + 58;
            out.push(shift);
            if count >= if set < 2 { 4 } else { 2 } {
                out.push(shift);
                set = target;
            } else {
                out.push(value(data[i], target).unwrap());
                i += 1;
            }
        }
    }
    (out, set)
}

fn encode(b: &Barcode, data: &[u8]) -> Result<Matrix, String> {
    let mode = b.integer(0, 2, 2, 6)?;
    let number = b.integer(1, 1, 1, 8)?;
    let total = b.integer(2, 1, 1, 8)?;
    if number > total {
        return Err("MaxiCode symbol number exceeds total".into());
    }
    let mut words = vec![0usize; 144];
    let mut body = data;
    if mode <= 3 {
        let header = if mode == 2 { 15 } else { 12 };
        if data.len() < header {
            return Err("MaxiCode structured carrier header is too short".into());
        }
        let decimal = |v: &[u8]| -> Result<u64, String> {
            Ok(digits(v)?.iter().fold(0, |n, &v| n * 10 + v as u64))
        };
        let service = decimal(&data[..3])?;
        let country = decimal(&data[3..6])?;
        let postal = if mode == 2 {
            decimal(&data[6..15])? | (9 << 30)
        } else {
            let mut n = 0;
            for &c in &data[6..12] {
                if !c.is_ascii_uppercase() && !c.is_ascii_digit() {
                    return Err(
                        "MaxiCode mode 3 postal code requires six uppercase letters/digits".into(),
                    );
                }
                n = n * 64 + set_a(c).unwrap() as u64;
            }
            n
        };
        let packed = mode as u64 | (postal << 4) | (country << 40) | (service << 50);
        for (i, w) in words[..10].iter_mut().enumerate() {
            *w = ((packed >> (6 * i)) & 63) as usize;
        }
        body = &data[header..];
    } else {
        words[0] = mode;
    }
    if b.compatibility.maxicode_nul_terminates_data {
        body = body.split(|&c| c == 0).next().unwrap_or_default();
    }
    let secondary = if mode == 5 { 68 } else { 84 };
    let capacity = secondary + if mode >= 4 { 9 } else { 0 };
    // Even all-numeric data cannot exceed this bound. Limit the compaction
    // search before allocating suffixes for arbitrary input lengths.
    if body.len() > capacity * 9 / 6 {
        return Err("MaxiCode data exceeds symbol capacity".into());
    }
    let mut stream = Vec::new();
    if total > 1 {
        stream.extend([33, (number - 1) * 8 + total - 1]);
    }
    let (mut encoded, mut final_set) =
        compact(body, !b.compatibility.maxicode_printer_run_boundaries);
    if stream.len() + encoded.len() > capacity {
        // Annex A permits shared punctuation in B. The native run heuristic
        // above can waste shifts on alternating B/shared characters. Retain
        // the wider-run encoding when it is needed to fit a valid message;
        // this also preserves inputs accepted before the native run fix.
        let alternative = compact(body, true);
        if alternative.0.len() < encoded.len() {
            (encoded, final_set) = alternative;
        }
    }
    stream.extend(encoded);
    // ISO/IEC 16023 Annex F: return to A/B for padding, not after a full
    // secondary message. The SurePost native control fills all 84 codewords;
    // adding a padding latch there incorrectly rejects a valid symbol.
    if stream.len() < capacity && (final_set >= 2 || b.compatibility.maxicode_terminal_latch) {
        stream.push(63);
    }
    if stream.len() > capacity {
        return Err("MaxiCode data exceeds symbol capacity".into());
    }
    stream.resize(capacity, 33);
    let offset = if mode >= 4 {
        words[1..10].copy_from_slice(&stream[..9]);
        9
    } else {
        0
    };
    words[20..20 + secondary].copy_from_slice(&stream[offset..]);
    let check = reed_solomon::parity(&words[..10], 10, 0x43, 1);
    words[10..20].copy_from_slice(&check);
    for lane in 0..2 {
        let data: Vec<_> = (0..secondary / 2)
            .map(|i| words[20 + 2 * i + lane])
            .collect();
        for (i, c) in reed_solomon::parity(&data, (124 - secondary) / 2, 0x43, 1)
            .into_iter()
            .enumerate()
        {
            words[20 + secondary + 2 * i + lane] = c;
        }
    }
    let mut matrix = Matrix::new(30, 33);
    for (y, row) in maxicode_modules::MODULES.iter().enumerate() {
        for (x, &n) in row.iter().enumerate() {
            let dark = if n == 865 {
                true
            } else if n == 0 {
                false
            } else {
                let bit = (n - 1) as usize;
                words[bit / 6] & (1 << (5 - bit % 6)) != 0
            };
            matrix.set(x, y, dark);
        }
    }
    matrix.set(28, 0, true);
    matrix.set(29, 0, true);
    Ok(matrix)
}
pub(super) fn render(b: &Barcode, data: &[u8]) -> Result<Path, String> {
    if b.compatibility.maxicode_standard_minimum_six_bytes
        && matches!(b.integer(0, 2, 2, 6)?, 4 | 6)
        && data.len() < 6
    {
        return Ok(Path::default());
    }
    let mut m = encode(b, data)?;
    if b.compatibility.maxicode_mode5_preview_omits_data && b.integer(0, 2, 2, 6)? == 5 {
        for (y, row) in maxicode_modules::MODULES.iter().enumerate() {
            for (x, &n) in row.iter().enumerate() {
                m.set(x, y, n == 865 || y == 0 && x >= 28);
            }
        }
    }
    if b.compatibility.maxicode_printer_dot_geometry {
        return Ok(printer_geometry(&m, b.dpi));
    }
    // ISO/IEC 16023:2000 4.11, Tables 6–8: nominal L=25.50 mm.
    let dots_per_mm = b.dpi as f64 / 25.4;
    let pitch = (25.50 / 29.) * dots_per_mm;
    let radius = pitch / 3f64.sqrt();
    let ink_x = (pitch - 0.12 * dots_per_mm) / 3f64.sqrt();
    let ink_y = radius - 0.06 * dots_per_mm;
    let dy = pitch * 3f64.sqrt() / 2.;
    let mut p = Path::default();
    for y in 0..33 {
        for x in 0..30 {
            if m.get(x, y) {
                let cx = (x as f64 + 0.5 + (y % 2) as f64 / 2.) * pitch;
                let cy = y as f64 * dy + radius;
                for k in 0..6 {
                    let angle = (k as f64 * 60. - 90.).to_radians();
                    let point = Point::new(cx + ink_x * angle.cos(), cy + ink_y * angle.sin());
                    p.segments.push(if k == 0 {
                        crate::output::Segment::Move(point)
                    } else {
                        crate::output::Segment::Line(point)
                    });
                }
                p.segments.push(crate::output::Segment::Close);
            }
        }
    }
    let cx = 14.5 * pitch;
    let cy = 16. * dy + radius;
    for r in [0.51, 1.18, 1.86, 2.53, 3.20, 3.87] {
        let r = r * dots_per_mm;
        p.ellipse(cx - r, cy - r, 2. * r, 2. * r);
    }
    Ok(p)
}
/// The ZD621's fixed dot stencil, measured from independent mode 2–6 preview
/// captures (tests/fixtures/maxicode-zd621-v1). It is independent of payload.
/// Six-by-seven-dot hexagons use a 7×6 pitch; odd rows shift three dots.
/// Squared ring thresholds lie between the last inside and first outside
/// pixel-centre distances in the captured concentric finder. These are printer
/// calibration values, not the nominal millimetre radii in ISO 16023 §4.11.
fn printer_geometry(m: &Matrix, dpi: u32) -> Path {
    let scale = dpi as f64 / 203.;
    let mut p = Path::default();
    for y in 0..33 {
        for x in 0..30 {
            if !m.get(x, y) {
                continue;
            }
            for (dy, width) in [2, 4, 6, 6, 6, 4, 2].into_iter().enumerate() {
                p.rect(
                    (1 + x * 7 + (y % 2) * 3 + (6 - width) / 2) as f64 * scale,
                    (1 + y * 6 + dy) as f64 * scale,
                    width as f64 * scale,
                    scale,
                );
            }
        }
    }
    for y in 0..64 {
        let mut start = None;
        for x in 0..=64 {
            let d = (x as f64 - 32.5).powi(2) + (y as f64 - 32.5).powi(2);
            let dark = x < 64
                && [(16.5, 81.5), (229.5, 402.5), (689.5, 967.5)]
                    .iter()
                    .any(|&(lo, hi)| d > lo && d < hi);
            if dark && start.is_none() {
                start = Some(x);
            }
            if !dark {
                if let Some(first) = start.take() {
                    p.rect(
                        (69 + first) as f64 * scale,
                        (68 + y) as f64 * scale,
                        (x - first) as f64 * scale,
                        scale,
                    );
                }
            }
        }
    }
    p
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn shared_b_runs_fit_without_repeated_shifts() {
        // ISO/IEC 16023 Annex A: these characters are available in A and B.
        // Forty alternating pairs fit with a B latch; forty shifts do not.
        for profile in [
            crate::render::profiles::SPECIFICATION,
            crate::render::profiles::ZD621_203_DPI,
        ] {
            for mode in ["2", "4"] {
                let b =
                    Barcode::new("BD", &[mode], 2., 2., 90., 203, profile.compatibility).unwrap();
                for common in b" ,./:\x1c\x1d\x1e" {
                    let body: Vec<_> = (0..40).flat_map(|_| [b'a', *common]).collect();
                    let mut data = if mode == "2" {
                        b"988840000000000".to_vec()
                    } else {
                        Vec::new()
                    };
                    data.extend_from_slice(&body);
                    let m = encode(&b, &data).unwrap();
                    let mut matrix = anyd::output::BitMatrix::new(30, 33, 0);
                    for y in 0..33 {
                        for x in 0..30 {
                            matrix.set(x, y, m.get(x, y));
                        }
                    }
                    let decoded = anyd::codes::maxicode::MaxiCodeDecoder::new()
                        .decode_matrix(&matrix)
                        .unwrap();
                    assert!(decoded.payload_bytes().ends_with(&body));
                }
            }
        }
    }
    #[test]
    fn full_capacity_needs_no_padding_latch() {
        // ISO/IEC 16023 data capacities: 84 secondary data words in mode 2,
        // plus nine primary data words in mode 4. Exercise a final E shift
        // as well as a full A run, with independent decoding and overflow.
        for profile in [
            crate::render::profiles::SPECIFICATION,
            crate::render::profiles::ZD621_203_DPI,
        ] {
            for (mode, capacity, header) in [("2", 84, "988840000000000"), ("4", 93, "")] {
                let b =
                    Barcode::new("BD", &[mode], 2., 2., 90., 203, profile.compatibility).unwrap();
                for control in [false, true] {
                    let mut body = vec![b'A'; capacity - if control { 2 } else { 0 }];
                    if control {
                        body.push(4);
                    }
                    let mut data = header.as_bytes().to_vec();
                    data.extend_from_slice(&body);
                    let m = encode(&b, &data).unwrap();
                    let mut matrix = anyd::output::BitMatrix::new(30, 33, 0);
                    for y in 0..33 {
                        for x in 0..30 {
                            matrix.set(x, y, m.get(x, y));
                        }
                    }
                    let decoded = anyd::codes::maxicode::MaxiCodeDecoder::new()
                        .decode_matrix(&matrix)
                        .unwrap();
                    assert!(decoded.payload_bytes().ends_with(&body));
                    data.push(b'A');
                    assert_eq!(
                        encode(&b, &data).err().unwrap(),
                        "MaxiCode data exceeds symbol capacity"
                    );
                }
            }
        }
    }
    #[test]
    fn specification_dimensions() {
        fn extent(segments: &[crate::output::Segment]) -> (f64, f64) {
            use crate::output::Segment;
            let (mut min_x, mut min_y, mut max_x, mut max_y) = (
                f64::INFINITY,
                f64::INFINITY,
                f64::NEG_INFINITY,
                f64::NEG_INFINITY,
            );
            let mut point = |p: &Point| {
                min_x = min_x.min(p.x);
                min_y = min_y.min(p.y);
                max_x = max_x.max(p.x);
                max_y = max_y.max(p.y);
            };
            for s in segments {
                match s {
                    Segment::Move(p) | Segment::Line(p) => point(p),
                    Segment::Cubic(a, b, c) => {
                        point(a);
                        point(b);
                        point(c);
                    }
                    _ => {}
                }
            }
            (max_x - min_x, max_y - min_y)
        }
        for dpi in [203, 300, 600] {
            let b = Barcode::new(
                "BD",
                &["4"],
                2.,
                2.,
                90.,
                dpi,
                crate::render::profiles::SPECIFICATION.compatibility,
            )
            .unwrap();
            let path = render(&b, b"A").unwrap();
            let unit = dpi as f64 / 25.4;
            let (width, height) = extent(&path.segments[..7]);
            assert!((width / unit - (25.50 / 29. - 0.12)).abs() < 1e-9);
            assert!((height / unit - (2. * 25.50 / 29. / 3f64.sqrt() - 0.12)).abs() < 1e-9);
            // Each ellipse has a move, four cubics, and a close. Tables 7/8
            // define the six radii, independent of symbol data and printer DPI.
            let rings = &path.segments[path.segments.len() - 36..];
            for (segments, radius) in rings
                .as_chunks::<6>()
                .0
                .iter()
                .zip([0.51, 1.18, 1.86, 2.53, 3.20, 3.87])
            {
                let (width, height) = extent(segments);
                assert!((width / unit - 2. * radius).abs() < 1e-9);
                assert!((height / unit - 2. * radius).abs() < 1e-9);
            }
        }
    }
    #[test]
    fn full_byte_alphabet_round_trip() {
        let b = Barcode::new(
            "BD",
            &["4"],
            2.,
            2.,
            90.,
            203,
            crate::render::profiles::SPECIFICATION.compatibility,
        )
        .unwrap();
        for first in (0u16..256).step_by(16) {
            let data: Vec<_> = (first..first + 16).map(|v| v as u8).collect();
            let m = encode(&b, &data).unwrap();
            let mut other = anyd::output::BitMatrix::new(30, 33, 0);
            for y in 0..33 {
                for x in 0..30 {
                    other.set(x, y, m.get(x, y));
                }
            }
            let result = anyd::codes::maxicode::MaxiCodeDecoder::new()
                .decode_matrix(&other)
                .unwrap();
            assert_eq!(result.payload_bytes(), data);
        }
    }
    #[test]
    fn independent_decode() {
        for mode in [2, 3, 4, 5, 6] {
            let b = Barcode::new(
                "BD",
                &[&mode.to_string()],
                2.,
                2.,
                90.,
                203,
                crate::render::profiles::SPECIFICATION.compatibility,
            )
            .unwrap();
            let data = match mode {
                2 => b"001840123450000Hello MaxiCode".as_slice(),
                3 => b"001826ABC123Hello MaxiCode".as_slice(),
                _ => b"Hello MaxiCode".as_slice(),
            };
            let m = encode(&b, data).unwrap();
            let mut other = anyd::output::BitMatrix::new(30, 33, 0);
            for y in 0..33 {
                for x in 0..30 {
                    other.set(x, y, m.get(x, y));
                }
            }
            let result = anyd::codes::maxicode::MaxiCodeDecoder::new()
                .decode_matrix(&other)
                .unwrap();
            assert!(result.text().unwrap().ends_with("Hello MaxiCode"));
        }
    }
}