summaryrefslogtreecommitdiff
path: root/tools/gen_code_vectors.py
blob: a87b0c933d8fed26b97101f69076d8ab0d854e3d (plain) (blame)
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
#!/usr/bin/env python3
# Author:  cirakg & claude ai
# Summary: Generate test/data/CodeVectors.mc: zxing-cpp reference module rows for the linear barcode encoders.
"""Reference module rows for README 12.4 check 1 (all fake payloads).

For every linear symbology: fixed edge cases plus seeded random payloads,
each with the options the watch uses and zxing-cpp's module row. Where the
watch adds something zxing doesn't (a Code 39 or ITF check character, Codabar
start/stop characters), the reference is made from the completed text.

    gen_code_vectors.py [--seed 1212] [--count 25] [--out test/data/CodeVectors.mc]
"""

from __future__ import annotations

import argparse
import random
import re
import string
from pathlib import Path
from typing import Any, cast

import barcode_ref as ref
import segno

CHECK_ADD, CHECK_FIX, CHECK_OFF = 0, 1, 2
TEXT_ON = 0x10


def options(check: int) -> int:
    return TEXT_ON | (check << 2) | 1


def digits(rng: random.Random, n: int) -> str:
    return "".join(rng.choice(string.digits) for _ in range(n))


def valid_upce(rng: random.Random) -> str:
    """Six UPC-E digits that are a canonical zero suppression."""
    while True:
        d = digits(rng, 6)
        last = d[5]
        if (last == "3" and d[2] in "012") or (last == "4" and d[3] == "0") or (last in "56789" and d[4] == "0"):
            continue
        return d


def cases(rng: random.Random, count: int) -> list[tuple[int, str, int, str]]:
    """(symbology, payload, options, zxing text) tuples."""
    out: list[tuple[int, str, int, str]] = []
    fixed = [
        (ref.EAN13, "590123412345", options(CHECK_ADD), "590123412345"),
        (ref.EAN13, "5901234123457", options(CHECK_ADD), "5901234123457"),
        (ref.EAN8, "1234567", options(CHECK_ADD), "1234567"),
        (ref.UPCA, "01234567890", options(CHECK_ADD), "01234567890"),
        (ref.UPCE, "0425261", options(CHECK_ADD), "0425261"),
        (ref.UPCE, "1234565", options(CHECK_ADD), "1234565"),
        (ref.CODE128, "1234", options(CHECK_ADD), "1234"),
        (ref.CODE128, "123456789", options(CHECK_ADD), "123456789"),
        (ref.CODE128, "AB12345678", options(CHECK_ADD), "AB12345678"),
        (ref.CODE128, "A1234B12345", options(CHECK_ADD), "A1234B12345"),
        (ref.CODE128, " ~", options(CHECK_ADD), " ~"),
        (ref.CODE39, "CODE 39", options(CHECK_OFF), "CODE 39"),
        (ref.CODE39, "CODE-39", options(CHECK_ADD), "CODE-39" + ref.code39_check("CODE-39")),
        (ref.ITF, "1234", options(CHECK_OFF), "1234"),
        (ref.ITF, "123", options(CHECK_ADD), "123" + str(ref.gs1_check("123"))),
        (ref.CODABAR, "A40156B", options(CHECK_ADD), "A40156B"),
        (ref.CODABAR, "31117013206375", options(CHECK_ADD), "A31117013206375A"),
    ]
    out.extend(fixed)
    printable = string.ascii_letters + string.digits + " !#$%&()*+,-./:;<=>?@[]^_{|}~"
    for _ in range(count):
        out.append((ref.EAN13, digits(rng, 12), options(CHECK_ADD), ""))
        out.append((ref.EAN8, digits(rng, 7), options(CHECK_ADD), ""))
        out.append((ref.UPCA, digits(rng, 11), options(CHECK_ADD), ""))
        out.append((ref.UPCE, rng.choice("01") + valid_upce(rng), options(CHECK_ADD), ""))
        n = rng.randint(1, 20)
        mixed = "".join(rng.choice(string.digits) if rng.random() < 0.5 else rng.choice(printable) for _ in range(n))
        out.append((ref.CODE128, mixed, options(CHECK_ADD), mixed))
        c39 = "".join(rng.choice(ref.CODE39_ALPHABET) for _ in range(rng.randint(1, 12)))
        with_check = rng.random() < 0.5
        out.append(
            (
                ref.CODE39,
                c39,
                options(CHECK_ADD if with_check else CHECK_OFF),
                c39 + ref.code39_check(c39) if with_check else c39,
            )
        )
        itf = digits(rng, 2 * rng.randint(1, 8))
        out.append((ref.ITF, itf, options(CHECK_OFF), itf))
        inner = "".join(rng.choice("0123456789-$:/.+") for _ in range(rng.randint(1, 12)))
        guard = rng.choice("ABCD") + inner + rng.choice("ABCD")
        out.append((ref.CODABAR, guard, options(CHECK_ADD), guard))
    return out


QR_LEVELS = "LMQH"
LEVEL_CONSTS = [
    segno.consts.ERROR_LEVEL_L,
    segno.consts.ERROR_LEVEL_M,
    segno.consts.ERROR_LEVEL_Q,
    segno.consts.ERROR_LEVEL_H,
]


def qr_cases(rng: random.Random, count: int) -> list[tuple[str, int, int, int]]:
    """(payload, level 0-3, version 0 = auto, mask -1 = auto) for QR.

    Single-mode payloads only (numeric, alphanumeric or bytes), so segno's
    one-segment encoding and the watch's optimal segmentation agree: a digit
    run inside other text would be a cheaper numeric segment on the watch.
    Mixed content is covered by the decoding check instead.
    """
    fixed = [
        ("01234567", 1, 1, 2),
        ("HELLO WORLD", 2, 1, 6),
        ("hello, world", 0, 2, 0),
        ("Zürich ✓ fake", 1, 5, 3),
        ("https://example.com/member/abc", 1, 0, -1),
        ("314159265358979323846264338327950288419716939937510", 3, 0, -1),
        ("THE QUICK BROWN FOX 0123456789 $%*+-./:", 2, 7, 4),
        ("v" * 120, 1, 10, 5),
        ("Z" * 300, 0, 0, -1),
        ("9" * 700, 1, 0, -1),
        ("byte data " * 40, 3, 0, -1),
    ]
    out = list(fixed)
    for _ in range(count):
        kind = rng.randrange(3)
        n = rng.randint(1, 60)
        if kind == 0:
            payload = digits(rng, n)
        elif kind == 1:
            payload = "".join(rng.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 $%*+-./:") for _ in range(n))
            if payload.isdigit():
                payload += "A"
        else:
            payload = "".join(rng.choice(string.ascii_lowercase + "!?,;") for _ in range(n))
        if not payload.isdigit() and re.search(r"\d{4,}", payload):
            continue
        out.append((payload, rng.randrange(4), 0, -1))
    return out


def single_mode_bits(payload: str, version: int) -> int:
    """Bits of payload as one numeric, alphanumeric or byte segment."""
    c = 0 if version <= 9 else (1 if version <= 26 else 2)
    if payload.isdigit():
        n = len(payload)
        return 4 + (10, 12, 14)[c] + 10 * (n // 3) + (0, 4, 7)[n % 3]
    if all(ch in "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:" for ch in payload):
        n = len(payload)
        return 4 + (9, 11, 13)[c] + 11 * (n // 2) + 6 * (n % 2)
    return 4 + (8, 16, 16)[c] + 8 * len(payload.encode())


def segno_pads_extra_byte(payload: str, level: int, version: int) -> bool:
    """True when segno would insert an extra zero byte: after the terminator
    its write_padding_bits adds 8 - (length % 8) bits even when the stream
    already ends on a codeword boundary (ISO/IEC 18004 7.4.10 adds none).
    The watch follows the standard, so such payloads make no exact reference."""
    groups = cast(dict[int, dict[int, tuple[Any, ...]]], segno.consts.ECC)[version][LEVEL_CONSTS[level]]
    capacity = 8 * sum(g.num_blocks * g.num_data for g in groups)
    bits = single_mode_bits(payload, version)
    terminated = bits + min(4, capacity - bits)
    return bool(terminated < capacity and terminated % 8 == 0)


def qr_reference(payload: str, level: int, version: int, mask: int) -> tuple[int, int, str]:
    """segno's (version, mask, matrix as hex of row-major bits) for the case."""
    qr = segno.make_qr(
        payload,
        error=QR_LEVELS[level],
        version=version or None,
        mask=None if mask < 0 else mask,
        boost_error=False,
        encoding="utf-8",
    )
    bits = "".join(str(b) for row in qr.matrix for b in row)
    bits += "0" * (-len(bits) % 8)
    packed = bytes(int(bits[i : i + 8], 2) for i in range(0, len(bits), 8))
    return int(qr.designator.split("-")[0]), qr.mask, packed.hex()


def render(rows: list[tuple[int, str, int, str]], seed: int) -> str:
    lines = []
    for symbology, payload, opts, text in rows:
        modules = ref.reference_modules(symbology, text or payload)
        escaped = payload.replace("\\", "\\\\").replace('"', '\\"')
        lines.append(f'        [{symbology}, "{escaped}", {opts}, "{modules}"]')
    body = ",\n".join(lines)
    qr_lines = []
    for case in qr_cases(random.Random(seed + 1), 20):
        payload, level, version, mask = case
        ref_version, ref_mask, hexbits = qr_reference(payload, level, version, mask)
        while segno_pads_extra_byte(payload, level, ref_version):
            # Lengthen by one character of the same kind until segno's padding
            # quirk doesn't apply.
            payload += payload[-1]
            ref_version, ref_mask, hexbits = qr_reference(payload, level, version, mask)
        escaped = payload.replace("\\", "\\\\").replace('"', '\\"')
        qr_lines.append(f'        ["{escaped}", {level}, {version}, {mask}, {ref_version}, {ref_mask}, "{hexbits}"]')
    qr_body = ",\n".join(qr_lines)
    return (
        "// Author:  cirakg & claude ai\n"
        "// Summary: zxing-cpp reference module rows for the linear barcode encoders (generated; do not edit).\n"
        f"//\n// Generated by tools/gen_code_vectors.py, seed {seed}. Rows: [symbology, payload, options,\n"
        "// expected modules without quiet zones]. Fake payloads only.\n\n"
        "import Toybox.Lang;\n\n(:test)\nmodule CodeVectors {\n    const LINEAR = [\n"
        + body
        + "\n    ];\n\n    // [payload, level, version (0 auto), mask (-1 auto), segno version, segno mask,\n"
        + "    //  segno matrix as hex of row-major bits]\n    const QR = [\n"
        + qr_body
        + "\n    ];\n}\n"
    )


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    parser.add_argument("--seed", type=int, default=1212)
    parser.add_argument("--count", type=int, default=25)
    parser.add_argument("--out", type=Path, default=Path("test/data/CodeVectors.mc"))
    args = parser.parse_args(argv)
    rows = cases(random.Random(args.seed), args.count)
    args.out.write_text(render(rows, args.seed), encoding="utf-8")
    print(f"wrote {len(rows)} reference rows to {args.out}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())