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 | """Writes a page of barcodes, drawn as bars: Code 128 for arbitrary text, an EAN-13
for a thirteen-digit article number, a UPC-A for its twelve-digit North American
cousin, and a short EAN-8.
The Python twin of the `write_barcode` example in Rust: the same page, through the
binding rather than through the library directly.
Whether a barcode 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 barcode says
under it, and `scripts/check_barcode.sh` points a decoder at the rendered image and
compares.
The head of the sheet says what the 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
barcodes carry stands apart from them: a scanner reads it, not a person, and it is the
same in every language.
Usage: python examples/write_barcode.py [out.pdf] [font.ttf]
HQF_PDF_LANG=fr python examples/write_barcode.py
"""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import _language
import _licence
import _out
import hqf_pdf
# What the Code 128 barcodes carry: an invoice number, a payment reference, and a line
# holding every kind of character code set B carries.
CARRIED = ["FR-1964413", "REF 2026-07-16/ACME", "a-Z 0..9 +*#%"]
# The thirteen-digit article number the EAN-13 barcode carries.
ARTICLE = "9780201379624"
# The twelve-digit article number the UPC-A barcode carries.
UPC_ARTICLE = "036000291452"
# The eight-digit article number the EAN-8 barcode carries.
EAN8_ARTICLE = "96385074"
# The width of the narrowest bar, in points. Everything else is a whole number of these.
MODULE = 1.0
# How tall the bars are drawn, in points.
BAR_HEIGHT = 40.0
# Where the foot of the first barcode sits, in points up from the foot of the sheet.
TOP = 700.0
# How far in from the left edge of the sheet the head of the page is set. The quiet zone
# of the first barcode starts here too, so the heading stands over the left edge of the
# block of bars.
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="Barcodes, drawn as bars",
lines=(
"Each barcode below is drawn as bars, not pasted in as a picture, so it prints",
"exactly as wide as it should. Under each one is what a scanner reads back.",
),
)
# The head of the sheet in French.
FRENCH = Words(
title="Des codes-barres, dessinés en barres",
lines=(
"Chaque code-barres ci-dessous est dessiné en barres, pas collé comme une image :",
"il s'imprime à la largeur exacte. 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("barcode.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)
top = TOP
for carried in CARRIED:
code = hqf_pdf.Code128(carried)
# The quiet zone is the caller's: the bars start ten modules in, and nothing
# else is drawn beside them.
left = MARGIN + hqf_pdf.Code128.quiet_zone() * MODULE
content.draw_barcode(code, left, top, code.module_count * MODULE, BAR_HEIGHT)
content.draw_text(handle, 9.0, left, top - 12.0, carried)
top -= 120.0
left = MARGIN + hqf_pdf.Code128.quiet_zone() * MODULE
ean = hqf_pdf.Ean13(ARTICLE)
content.draw_ean13(ean, left, top, ean.module_count * MODULE, BAR_HEIGHT)
content.draw_text(handle, 9.0, left, top - 12.0, ARTICLE)
top -= 120.0
upc = hqf_pdf.UpcA(UPC_ARTICLE)
content.draw_upc_a(upc, left, top, upc.module_count * MODULE, BAR_HEIGHT)
content.draw_text(handle, 9.0, left, top - 12.0, UPC_ARTICLE)
top -= 120.0
ean8 = hqf_pdf.Ean8(EAN8_ARTICLE)
content.draw_ean8(ean8, left, top, ean8.module_count * MODULE, BAR_HEIGHT)
content.draw_text(handle, 9.0, left, top - 12.0, EAN8_ARTICLE)
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()
|