Codes Data Matrix

Une page de codes Data Matrix, tracés en carrés.

Python write_datamatrix.py 151 lignes
  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
"""Writes a page of Data Matrix codes, drawn as squares.

The Python twin of the `write_datamatrix` example in Rust: the same page,
through the binding rather than through the library directly.

Whether a Data Matrix code works is not a question the page can answer, and not
one a human eye can: it is answered by a scanner. So the page carries what each
code says under it, and `scripts/check_datamatrix.sh` points a decoder at the
rendered image and compares.

The head of the sheet says what the squares are for, so that the page is worth
something to whoever opens it without the code beside it. Those words are held in
`Words`, once per language, and `HQF_PDF_LANG` picks which set the sheet is headed in.
What the codes carry stands apart from them: a scanner reads it, not a person, and it is
the same in every language.

Usage: python examples/write_datamatrix.py [out.pdf] [font.ttf]
       HQF_PDF_LANG=fr python examples/write_datamatrix.py
"""

from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path

import _language
import _licence
import _out

import hqf_pdf

# What the codes carry: a tracking link, a serial number, and a line of mixed
# punctuation.
CARRIED = [
    "https://example.test/track/FR-1964413",
    "SN:2026-07-19/ACME-4472",
    "a-Z 0..9 +*#% and on",
]

# The side of one module, in points. Four pixels to a module at 288dpi, so every
# module's edge lands on a pixel boundary.
MODULE = 3.0

# Where the foot of the row of codes sits, in points up from the foot of the sheet.
TOP = 640.0

# How far in from the left edge of the sheet the head of the page is set. The quiet zone
# of the first code starts here too, so the heading stands over the left edge of the row
# of codes.
MARGIN = 72.0

# Where the baseline of the heading sits, in points up from the foot of the sheet.
HEAD_TOP = 794.0

# The size the heading is set at, in points.
HEADING = 16.0

# The size the lines under the heading are set at, in points.
BODY = 10.5

# How far under the heading the first line beneath it sits, in points.
HEAD_GAP = 24.0

# How far below one line under the heading the next one sits, in points.
LINE_STEP = 15.0

@dataclass(frozen=True)
class Words:
    """Every word the sheet is headed in, in one language."""

    # What the page says it is, at its head and in what the file says of itself.
    title: str
    # What the page says under its heading.
    lines: tuple[str, str]


# The head of the sheet in English.
ENGLISH = Words(
    title="Data Matrix codes, drawn as squares",
    lines=(
        "A Data Matrix holds as much as a QR code in less room, which is why it is found",
        "on small parts and on parcels. Under each one is what a scanner reads back.",
    ),
)

# The head of the sheet in French.
FRENCH = Words(
    title="Des codes Data Matrix, dessinés en carrés",
    lines=(
        "Un Data Matrix porte autant qu'un code QR dans moins de place : on le trouve sur",
        "les petites pièces et sur les colis. Sous chacun est écrit ce qu'un scanner y lit.",
    ),
)

# Every language the example is written in. A language is added by writing its own set
# of words and naming it here.
WORDS = {_language.ENGLISH: ENGLISH, _language.FRENCH: FRENCH}


def head(content, handle, words) -> None:
    """Draw the heading, and the lines under it that say what the page is for."""
    content.draw_text(handle, HEADING, MARGIN, HEAD_TOP, words.title)

    y = HEAD_TOP - HEAD_GAP
    for line in words.lines:
        content.draw_text(handle, BODY, MARGIN, y, line)
        y -= LINE_STEP


def main() -> None:
    language = _language.from_environment()
    words = _language.words_of(WORDS, language)

    # A named file is written as named; the default one carries the language, so the two
    # languages do not overwrite each other in `tmp/`.
    out = _out.output_path(Path(_language.file_name("datamatrix.pdf", language)).stem)

    document = hqf_pdf.Document()
    document.set_license(_licence.licensed())
    document.set_info("Title", words.title)

    handle = document.add_font(hqf_pdf.Font.from_path(_out.font_path()))

    content = hqf_pdf.Content()
    content.set_fill(hqf_pdf.Rgb(0.0, 0.0, 0.0))
    head(content, handle, words)

    quiet = hqf_pdf.DataMatrix.quiet_zone() * MODULE
    left = MARGIN
    top = TOP
    for carried in CARRIED:
        code = hqf_pdf.DataMatrix(carried)
        side = code.size * MODULE

        # The quiet zone is the caller's: the code is drawn that far in, and nothing
        # else is drawn beside it.
        content.draw_datamatrix(code, left + quiet, top, side)
        content.draw_text(handle, 8.0, left + quiet, top - 12.0, carried)

        left += side + quiet * 2.0 + 24.0

    page = hqf_pdf.Page.a4()
    page.set_content(content)
    document.add_page(page)

    written = document.write(out)
    print(f"wrote {out} ({written} bytes)")


if __name__ == "__main__":
    main()