Codes PDF417

Une page de codes PDF417, tracés en barres empilées.

Python write_pdf417.py 159 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
152
153
154
155
156
157
158
159
"""Writes a page of PDF417 codes, drawn as stacked bars.

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

Whether a PDF417 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_pdf417.sh` points a decoder at the
rendered image and compares.

The head of the sheet says what the stacked bars 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_pdf417.py [out.pdf] [font.ttf]
       HQF_PDF_LANG=fr python examples/write_pdf417.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",
]

# How many data columns each code is drawn in, which is what settles how wide the symbol
# comes out: the rows follow from the text.
COLUMNS = 4

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

# Where the foot of the first code sits, in points up from the foot of the sheet.
TOP = 660.0

# How far in from the left edge of the sheet the head of the page is set. The quiet zone
# of each code starts here too, so the heading stands over the left edge of the block 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="PDF417 codes, drawn as stacked bars",
    lines=(
        "A PDF417 code is rows of bars stacked one over another, and it holds a whole",
        "form rather than a number. Under each one is what a scanner reads back.",
    ),
)

# The head of the sheet in French.
FRENCH = Words(
    title="Des codes PDF417, dessinés en barres empilées",
    lines=(
        "Un code PDF417 est fait de rangées de barres empilées, et il porte un formulaire",
        "entier plutôt qu'un numéro. 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("pdf417.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.Pdf417.quiet_zone() * MODULE
    aspect = hqf_pdf.Pdf417.row_aspect()
    left = MARGIN
    top = TOP
    for carried in CARRIED:
        code = hqf_pdf.Pdf417(carried, COLUMNS)
        width = code.width * MODULE
        # A module is drawn taller than it is wide, so a row is that much deeper than a
        # module is across.
        height = code.height * MODULE * aspect

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

        top -= height + quiet * 2.0 + 36.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()