Debian Codegraph @

texmf-dist/source/latex/qrcodetikz/qrcodetikz.dtx

raw
1 % \iffalse
2 % File: qrcodetikz.dtx
3 % Copyright (C) 2025 Miguel V. S. Frasson ([email protected])
4 %
5 % This package may be distributed under the terms of the LaTeX
6 % Project Public License, as described in lppl.txt in the base
7 % LaTeX distribution, either version 1.3 or (at your option)
8 % any later version.
9 %
10 %<*driver>
11 \documentclass{ltxdoc}
12 \usepackage{hyperref}
13 \hypersetup{
14 colorlinks,
15 linkcolor={blue!50!black},
16 citecolor={black},
17 urlcolor={black}
18 }
19 \usepackage{doc,amsmath,enumitem,mathabx,array,booktabs,wasysym}
20 \usepackage[utf8]{inputenc}
21 \usepackage[T1]{fontenc}
22 \usepackage{microtype}
23 \usepackage[nolinks]{qrcodetikz}
24 \usepackage{tikz}
25 \usetikzlibrary{arrows,arrows.meta}
26 \makeatletter
27 \newcommand{\nDescribeMacro}[1]{%
28 \xa\xa\xa\DescribeMacro\xa\csname\xa\@gobble\string#1\endcsname\ignorespaces}%
29 \makeatother
30 \begin{document}
31 \DocInput{qrcodetikz.dtx}
32 \end{document}
33 %</driver>
34 % \fi
35 %
36 % \def\exclamacao{!}
37 % \MakeShortVerb\!
38 % \def\vars#1{\textlangle\textit{#1}\textrangle}
39 % \def\markpage#1{\refstepcounter{enumiv}\label{#1}}
40 %
41 % \title{\textbf{qrcodetikz}: prettier \textsc{qr} codes}
42 % \author{Miguel Vinícius Santini Frasson\\ \texttt{[email protected]}}
43 % \date{2025--05--28, version 1.0}
44 %
45 % \noindent
46 % \raisebox{-4mm}{%
47 % \qrcode[link,height=1in]{https://ctan.org/pkg/qrcodetikz}}\qquad
48 % \begin{minipage}[c]{0.65\linewidth}
49 % \maketitle
50 % \end{minipage}
51 %
52 % \tableofcontents
53 %
54 % \section{Introduction}
55 %
56 % The package \href{https://ctan.org/pkg/qrcodetikz}{\texttt{qrcodetikz}}
57 % aims to improve the display of QR codes provided by package
58 % \href{https://ctan.org/pkg/qrcode}{\texttt{qrcode}}.
59 %
60 % \begin{center}
61 % \begin{tabular}{l@{\qquad}c@{\qquad}l}
62 % {\qrcodetikzOff\qrcode[height=2cm]{QR codes}} & vs.
63 % & \qrcodetikzOn\qrcode[height=2cm]{QR codes}\\[0.35in]
64 % with \texttt{qrcode} & &
65 % with \texttt{qrcodetikz}
66 % \end{tabular}
67 % \end{center}
68 %
69 % The Quick Response (QR) codes provided by package \texttt{qrcode}
70 % show white borders on each square (from little to very proeminent
71 % depending on pdf viewer). This is because the QR code is printed
72 % square by square, not the connected regions of squares as such, and
73 % pdf screen viewers show these undesired square borders.
74 %
75 % This package overwrites the \texttt{qrcode} printing functions to
76 % fill connected regions of the QR code using Ti\textit{k}Z, allowing
77 % prettier qrcodes on screen visualization, with possibility of
78 % customization.
79 %
80 % \section{Usage}
81 %
82 % To switch the display of QR codes with Ti\textit{k}Z, just load
83 % package \texttt{qrcodetikz} instead of \texttt{qrcode}. The package
84 % options are the same of \texttt{qrcode} package\footnote{All options
85 % are passed to \texttt{qrcode} package.}, that we repeat briefly (for
86 % details, see \texttt{qrcode} documentation):
87 % \begin{description}
88 % \item[nolinks] disable qrcode links if \texttt{hyperref} is loaded.
89 % \item[draft] doesn't compute QR codes, dummy QR codes are
90 % displayed, like
91 % \raisebox{\depth}[0pt]{\qrcode[draft,version=2,height=1.5cm]{ABCD}}
92 %
93 % \item[final] opposite to draft, display complete QR codes.
94 % \item[forget] do not store QR codes into \texttt{.aux} for reusage,
95 % recompute them in every run (mainly for debug purposes).
96 % \end{description}
97 %
98 % You can set other options using
99 % \texttt{\string\qruse\{\textit{\vars{options}}\}}, which can be set
100 % as options to !\qrcode! command. These options are:
101 % \begin{description}
102 % \item[height=\vars{dimension}] sets the height (and width) of the QR
103 % codes. Default is 2~cm;.
104 % \item[level=\vars{L-M-Q-H}] sets the level of error correction; the
105 % levels are set with one letter: \texttt{L} (low), \texttt{M} (medium),
106 % \texttt{Q} (quality) and \texttt{H} (high). Default is \texttt{M}.
107 % \item[version=\vars{1-to-40}] is related to the size if the QR code.
108 % Must be a number between 1 and 40. Default is the lowest version
109 % that encodes text with desired quality.
110 % \item[padding, tight] QR code specification says that a QR code should be
111 % surrounded by some white space (specifically the width of 4
112 % modules). With \texttt{padding}, this white border is added. With
113 % \texttt{tight}, this white border is omitted, since it is usually
114 % provided by the user. Defult to \texttt{tight}.
115 % \item[link, nolink] Allows/disallows use of clickable links with the
116 % QR code.
117 % \end{description}
118 %
119 % For many more details, like special characters, please refer to the
120 % documentation of \texttt{qrcode} package.
121 %
122 % \subsection{Fill options}
123 %
124 % \DescribeMacro{\qrcodeFillOptions}
125 % The default plain QR codes are fine and recommended. However, with
126 % \texttt{qrcodetikz}, the user can pass fill options !\fill! command
127 % to display the QR codes, like colors, rounded corners, shades,
128 % etc. using
129 % \begin{center}
130 % \texttt{\string\qrcodeFillOptions\{\vars{fill-options}\}}
131 % \end{center}
132 % The settings persist until the next call of !\qrcodeFillOptions!
133 % (for example, a call of !\qrcodeFillOptions{}! erases previous
134 % customizations). As an example:\medskip
135 %
136 % \DeleteShortVerb\|\DeleteShortVerb\!\MakeShortVerb\+
137 % \noindent
138 % +\begin{center}+\\
139 % + +\texttt{\textit{\% rounded corners}}\\
140 % + \qrcodeFillOptions{rounded corners=1pt}+\\
141 % + \qrcode{QR codes}\qquad+\\
142 % + +\texttt{\textit{\% shades}}\\
143 % + \qrcodeFillOptions{left color=gray,right color=black,draw=black,thin}+\\
144 % + \qrcode{QR codes}\qquad+\\
145 % + +\texttt{\textit{\% removing customization}}\\
146 % + \qrcodeFillOptions{}+\\
147 % + \qrcode{QR codes}+\\
148 % +\end{center}+\DeleteShortVerb\+\MakeShortVerb\!
149 %
150 % \noindent would print:
151 %
152 % \begin{center}
153 % \qrcodeFillOptions{rounded corners=1pt}
154 % \qrcode{QR codes}\qquad
155 % \qrcodeFillOptions{left color=gray,right color=black,draw=black,thin}
156 % \qrcode{QR codes}\qquad
157 % \qrcodeFillOptions{}
158 % \qrcode{QR codes}\refstepcounter{enumiv}\label{qrcode-bug}
159 % \end{center}
160 %
161 % Notice that last qrcode was printed plain, without customizations.
162 %
163 % In our humble opinion, plain QR codes are prettier. The
164 % implementation made an effort to produce good looking QR codes with
165 % fill option !rounded corners!, filling individual connected
166 % components of the QR code to avoid straight corners. Other
167 % customizations with fill options are left for the convenience of the
168 % user.\medskip
169 %
170 % \DescribeMacro{\qrcodetikzOn}\DescribeMacro{\qrcodetikzOff}
171 % One can turn on and off QRcodes with Ti\textit{k}Z with commands
172 % \texttt{\string\qrcodetikzOn} and \texttt{\string\qrcodetikzOff}.
173 % When turned off, original \texttt{qrcode} functions for printing are
174 % restored. These functions probably will never be used outside this
175 % manual \smiley.
176 %
177 % This package was not intended specifically to display artistic QR
178 % codes. For that you can take a look into
179 % \href{https://ctan.org/pkg/fancyqr}{\texttt{fancyqr}} package.
180 %
181 % For the regular user, the documentation ends here. In the following
182 % pages we give details of the implementation.
183 %
184 % \section{Implementation}
185 %
186 % \subsection{Ideas and conventions}
187 %
188 % Using the \texttt{qrcode} package, that saves computed QR codes into
189 % the \texttt{.aux} file, if one calls !\qrcode{QrCode}!, we get into
190 % the \texttt{.aux} file the line below (as a long line without
191 % linebreaks):\medskip
192 %
193 % \noindent\texttt{\small\string\qr@savematrix\{QrCode\}\{1\}\{2\}\{\relax
194 % 1111111010000011111111000001000100010000011011\\
195 % 101010000010111011011101000100010111011011101010101010111011000001001010010\\
196 % 000011111111010101011111110000000011000000000000000011001011010101011011100\\
197 % 111100110101100000111000100010100100101100010111010111101011001110010010011\\
198 % 110000000011110110001101111111000001111010101000001010100001111001011101000\\
199 % 010100000101011101001011110110001011101001001010100111000001000001110011001\\
200 % 11111100000101000010\}}\medskip
201 %
202 % Counting the 0's and 1's, we get $441=21^2$ “bits”. Dividing the string into
203 % 21 lines of 21 bits, coloring 1's as black boxes and 0's as light boxes
204 % to ease visualization we get:\medskip
205 %
206 % \begin{center}
207 % \scalebox{0.7}{\begin{minipage}[c]{0.606\linewidth}
208 % \footnotesize
209 % \def\1{\colorbox{black}{\textcolor{white}{1}}}\relax
210 % \def\0{\colorbox{white}{\textcolor{black}{0}}}\ttfamily
211 % \1\1\1\1\1\1\1\0\1\0\0\0\0\0\1\1\1\1\1\1\1\\
212 % \1\0\0\0\0\0\1\0\0\0\1\0\0\0\1\0\0\0\0\0\1\\
213 % \1\0\1\1\1\0\1\0\1\0\0\0\0\0\1\0\1\1\1\0\1\\
214 % \1\0\1\1\1\0\1\0\0\0\1\0\0\0\1\0\1\1\1\0\1\\
215 % \1\0\1\1\1\0\1\0\1\0\1\0\1\0\1\0\1\1\1\0\1\\
216 % \1\0\0\0\0\0\1\0\0\1\0\1\0\0\1\0\0\0\0\0\1\\
217 % \1\1\1\1\1\1\1\0\1\0\1\0\1\0\1\1\1\1\1\1\1\\
218 % \0\0\0\0\0\0\0\0\1\1\0\0\0\0\0\0\0\0\0\0\0\\
219 % \0\0\0\0\0\1\1\0\0\1\0\1\1\0\1\0\1\0\1\0\1\\
220 % \1\0\1\1\1\0\0\1\1\1\1\0\0\1\1\0\1\0\1\1\0\\
221 % \0\0\0\0\1\1\1\0\0\0\1\0\0\0\1\0\1\0\0\1\0\\
222 % \0\1\0\1\1\0\0\0\1\0\1\1\1\0\1\0\1\1\1\1\0\\
223 % \1\0\1\1\0\0\1\1\1\0\0\1\0\0\1\0\0\1\1\1\1\\
224 % \0\0\0\0\0\0\0\0\1\1\1\1\0\1\1\0\0\0\1\1\0\\
225 % \1\1\1\1\1\1\1\0\0\0\0\0\1\1\1\1\0\1\0\1\0\\
226 % \1\0\0\0\0\0\1\0\1\0\1\0\0\0\0\1\1\1\1\0\0\\
227 % \1\0\1\1\1\0\1\0\0\0\0\1\0\1\0\0\0\0\0\1\0\\
228 % \1\0\1\1\1\0\1\0\0\1\0\1\1\1\1\0\1\1\0\0\0\\
229 % \1\0\1\1\1\0\1\0\0\1\0\0\1\0\1\0\1\0\0\1\1\\
230 % \1\0\0\0\0\0\1\0\0\0\0\0\1\1\1\0\0\1\1\0\0\\
231 % \1\1\1\1\1\1\1\0\0\0\0\0\1\0\1\0\0\0\0\1\0
232 % \end{minipage}}\qquad
233 % \begin{tabular}{c}
234 % {\qrcodetikzOff\qrcode[height=5cm]{QrCode}}\\[2.7cm]
235 % (printed with \texttt{qrcode})
236 % \end{tabular}
237 %
238 % \mbox{}
239 % \end{center}
240 %
241 % So one sees that the binary data must be printed in rows from left
242 % to right, from top to bottom. On the right of the picture above it
243 % is the QR code as printed with the \texttt{qrcode} package: it
244 % prints individual black squares for each 1 and equivalent space for
245 % 0. An unpleasant side effect are the visible thin borders of squares
246 % when pdf file is visualized on screen. This package is meant to fix
247 % this, printing regions as a whole with Ti\textit{k}Z.
248 %
249 % We observed that if we make a path connecting all connected borders
250 % of the pixels, and fill this path with the “even-odd
251 % rule”\,\footnote{The SVG defines the even-odd rule by saying: “This
252 % rule determines the ‘insideness’ of a point on the canvas by drawing
253 % a ray from that point to infinity in any direction and counting the
254 % number of path segments from the given shape that the ray
255 % crosses. If this number is odd, the point is inside; if even, the
256 % point is outside.”
257 % (\href{https://en.wikipedia.org/wiki/Even–odd_rule}{\relax
258 % \texttt{https://en.wikipedia.org/wiki/Even–odd\_rule}}).}, it would
259 % beautifully print the QRcode.
260 %
261 % \begin{center}
262 % \begin{tikzpicture}[scale=1.5]
263 % \draw (0,0) rectangle (0.7,0.7) (0.1,0.1) rectangle (0.6,0.6)
264 % (0.2,0.2) rectangle (0.5,0.5);
265 % \draw[>=stealth',->] (1,0.35) -- ++(0.7,0);
266 % \fill[even odd rule,xshift=2.1cm] (0,0) rectangle
267 % (0.7,0.7) (0.1,0.1) rectangle (0.6,0.6) (0.2,0.2) rectangle
268 % (0.5,0.5);
269 % \end{tikzpicture}\medskip
270 %
271 % Fill with even-odd rule.
272 % \end{center}
273 %
274 % This package is the implementation of such idea. We took advantage
275 % of facilities from \LaTeX3 such as loops, sequences (lists), arrays
276 % of integers, property lists, easy integer computations and other
277 % goodies already loaded into the \LaTeX\ kernel in every run in any
278 % recent \TeX\ distribution. The only external packages needed are
279 % \texttt{qrcode} (to compute the qrcode) and \texttt{tikz} (to fill a
280 % path in even-odd rule).
281 %
282 % If any other package wants to take advance of the conversion (binary
283 % data) $\leftrightarrow$ (Ti\textit{k}Z path), for artistic
284 % reasons for instance, could use\\
285 % ! \QRTZgetTikzPathMaybeSaved!\texttt{\{\vars{binary data}\}\{\vars{cmd to store path}\}}\\
286 % explained in section~\ref{sec:prop}, that tries to reuse computed
287 % paths or computes it otherwise. The first argument is expanded, so
288 % it can be a command that holds the binary data.
289 %
290 % In QRcode jargon, the size in number of squares of the QR code is
291 % given by its \textit{version}. This size is
292 % $4 \times \text{\vars{qrcode version}} + 17$. The maximum version
293 % os 40, so the maximum size is $4\times40+17 = 177$. We take advance
294 % of integer arrays in \LaTeX3 (module \textsf{l3intarray}) that
295 % provide fast access to any integer in the array and store all
296 % matrices $n\times n$ as linear sequence of $n^2$ integers, in the
297 % order as the binary data.
298 %
299 % As Ti\textit{k}Z uses cartesian coordinates, we decided to use them
300 % as the coordinates of the pixels. We implement the conversion from
301 % $(x,y)$ coordiante, with $x$ and $y$ from $1$ to $n$, to sequence
302 % index from $1$ to $n^2$ so that
303 % \begin{itemize}[itemsep=0pt,parsep=0pt]
304 % \item pixel $(1,n)$ – top left – is in index $1$;
305 % \item pixel $(n,n)$ – top right – is in index $n$;
306 % \item pixel $(n,n)$ – bottom right – is in index $n^2$;
307 % \item pixel $(1,1)$ – bottom left – is in index $(n-1)n + 1$.
308 % \end{itemize}
309 % Doing the math, index is $(n-y)n + x$.\medskip
310 %
311 % We need to put a convention to the coodinates of the corners. We
312 % define that a pixel $(x,y)$ gives its coordinates to the bottom-left
313 % corner. The coordinates of the lines vary from 1 to $n+1$. This
314 % way, the size of the smallest line segment is 1 and the size of a QR
315 % code is a square of size $n$, so the path must be scaled down to fit
316 % the required size of the QR code.
317 %
318 % With these conventions, we can begin implementation according the
319 % following steps:
320 %
321 % \begin{enumerate}
322 % \item We store the binary matrix into a array of integers; we use
323 % intarrays from \LaTeX3 to get fast read/write access. The
324 % interface $(x,y) \leftrightarrow i$ (array index) is explained in
325 % section~\ref{sec:matrix}. At this point, every pixel is stored as
326 % values 0 (for empty) or 1 (for black).
327 % \item We compute the connected components of the QR code using
328 % “find/union” operations, a fast algorithm, explained in
329 % section~\ref{sec:cc}. The content of the pixel will be 0 (for
330 % empty) or a positive integer that labels the connected component,
331 % shared by all pixels in that same connected component. A positive
332 % value will be printed as black.
333 % \item We build a list of all borders of pixels. We have to fix
334 % notation for a border: after some reimplementations\footnote{After
335 % first successful implementation, we realized that \texttt{rounded
336 % corners} fill option produced ugly straight crossings so we
337 % decided to implement connected components and follow the path
338 % always inward in a crossing with multiple ways. A natural choice
339 % as to walk the borders in “positive” sense according to Calculus
340 % rules, that is, interior is always on the left, so inward means
341 % turn left in most cases. After some reasoning, we came up with
342 % the directions 0 to 3, so turn left means add 1 modulus 4 and turn
343 % right means add $-1$ modulus 4.}, we came up with the notation of
344 % border as a comma list $x,y,d,c$ where $(x,y)$ is the origin of
345 % the border, $d$ is a direction number from 0 to 3 (representing
346 % angle $d\cdot 90$°): 0 ($\rightarrow$), 1 ($\uparrow$), 2
347 % ($\leftarrow$), 3 ($\downarrow$) in such way that the black pixel
348 % is always to the left of the border and $c$ is the number of the
349 % connected component that such pixel belongs to. Following a
350 % border to the next in the same connected component will walk the
351 % border in “positive” direction (counterclockwise), and in a
352 % crossing, turn left (that is, add 1 modulus 4) will always turn
353 % inward. The process of creating the list of borders is described
354 % in section~\ref{sec:borders}.
355 %
356 % \item Now we build the Ti\textit{k}Z path, the main part of the
357 % package. Starting from any border, the next border is either turn
358 % left (inward), go straight or turn right in the same connected
359 % component, trying in this sequence. Every walked border is
360 % removed from the list of borders. In the beginning and at every
361 % turn, output “$(x,y)$-{}-” to the path, where $(x,y)$ is the
362 % origin of the border. When none of these borders is in the list,
363 % we arrive at a dead end and output “cycle” to the path. This
364 % pocess is reeated while the list of borders is not empty. This
365 % algorithm is implemented in section~\ref{sec:path}.
366 %
367 % \item The package \texttt{qrcode} saves the correspondence
368 % “text+version” $\leftrightarrow$ “binary data” into the aux file,
369 % and at beginning of document, this code saves the binary data as a
370 % macro, avoiding most of the computation. For the computed
371 % Ti\textit{k}Z paths we implement this saving/restoring as a
372 % \LaTeX3 prop list, also saving the correspondence “binary data”
373 % $\leftrightarrow$ “path” into the aux file. This is done in
374 % section~\ref{sec:prop}.
375 %
376 % \item In the mood of \texttt{qrcode} package, we save into the aux
377 % file a line that populates the prop list from previous item. This
378 % is done in section~\ref{sec:aux}.
379 %
380 % \item In section \ref{sec:print} we implement functions that are
381 % meant to replace \texttt{qrcode} functions: that package
382 % implements matrices as a series of macros. Fortunately
383 % \texttt{qrcode} has a function to convert its matrices as binary
384 % data. To avoid any change of behavior and get the closest result
385 % from \texttt{qrcode}, we copied most of the computations plainly
386 % from that package, but printing the path with Ti\textit{k}Z
387 % instead of the original display of individual boxes.
388 %
389 % \item Finally, in section \ref{sec:on-off} we save copies of former functions
390 % and implement turning on and off of new printing functions.
391 % \end{enumerate}
392 %
393 % \subsection{Package identification}
394 %
395 % This package uses \LaTeX3 interface, with most recently implemented
396 % functions in \textsf{l3intarray} module from 2018. In
397 % 2020\footnote{See
398 % \url{https://www.latex-project.org/news/latex2e-news/}, entry for
399 % 2020/02/02.}, \LaTeX3 was included into \LaTeXe{}, so we require
400 % \LaTeXe{} version at least as recent as 2020. If you only have
401 % access to ealier versions of \LaTeXe, you can use
402 % !\usepackage{expl3}!, but we still depend on \LaTeX3
403 % \textit{intarray} module from 2018.
404 %
405 % This package passes all options to \texttt{qrcode} package and loads
406 % it, if not already loaded.
407 %
408 % \begin{macrocode}
409 \NeedsTeXFormat{LaTeX2e}[2020-02-02]
410 \ProvidesPackage{qrcodetikz}[2025-05-28 v1.0 Prettier qrcodes]
411 \DeclareOption*{\PassOptionsToPackage{\CurrentOption}{qrcode}}
412 \ProcessOptions
413 \RequirePackage{qrcode}
414 \RequirePackage{tikz}
415 % \end{macrocode}
416 %
417 % The package uses \LaTeX3 in most of its extension. Only the final
418 % printing functions that will replace \texttt{qrcode} printing
419 % functions don't use \LaTeX3.
420 % \begin{macrocode}
421 \ExplSyntaxOn
422 % \end{macrocode}
423 %
424 % \subsection{Declaration of variables}
425 %
426 % We declare some few variables. Variables of type “token list”
427 % should end with the suffix !_tl!, but we decided to ommit it for
428 % readability.
429 %
430 % \begin{itemize}
431 % \item Integer variable !\l_qrtz_size_int! holds the size of the side
432 % of QR code.
433 % \begin{macrocode}
434 \int_new:N \l_qrtz_size_int
435 % \end{macrocode}
436 %
437 % \item Individual coordinates of $(x,y)$ are stored in
438 % \begin{macrocode}
439 \tl_new:N \l_qrtz_x
440 \tl_new:N \l_qrtz_y
441 % \end{macrocode}
442 %
443 % \item The direction number (0 to 3) is stored in
444 % \begin{macrocode}
445 \tl_new:N \l_qrtz_dir
446 % \end{macrocode}
447 %
448 % \item The number of current connected component is stored in
449 % \begin{macrocode}
450 \tl_new:N \l_qrtz_component
451 % \end{macrocode}
452 %
453 % \item Current path in construction is stored in
454 % \begin{macrocode}
455 \tl_new:N \l_qrtz_path
456 % \end{macrocode}
457 %
458 % \item Borders are stored in
459 % \begin{macrocode}
460 \tl_new:N \l_qrtz_border
461 \tl_new:N \l_qrtz_inner
462 \tl_new:N \l_qrtz_straight
463 \tl_new:N \l_qrtz_outer
464 % \end{macrocode}
465 %
466 % \item \LaTeX3 intarray that stores matrix. Its size is $177^2=31\,329$,
467 % the worst possible case.
468 % \begin{macrocode}
469 \intarray_new:Nn \g_qrtz_labels_intarray {177 * 177}
470 % \end{macrocode}
471 %
472 % \item \LaTeX3 sequence to store the list of borders
473 % \begin{macrocode}
474 \seq_new:N \l_qrtz_border_seq
475 % \end{macrocode}
476 %
477 % \item \LaTeX3 property list that stores saved information (binary-data
478 % $\leftrightarrow$ path correspondence)
479 % \begin{macrocode}
480 \prop_new:N \g_qrtz_paths_prop
481 % \end{macrocode}
482 %
483 % \item Switch for fallback definition of .aux commands
484 % \begin{macrocode}
485 \bool_new:N \g_qrtz_aux_fallback_written_bool
486 \bool_gset_false:N \g_qrtz_aux_fallback_written_bool
487 % \end{macrocode}
488 %
489 % \item Auxiliary switches
490 % \begin{macrocode}
491 \bool_new:N \l_qrtz_continue_straight_bool
492 \bool_new:N \l_qrtz_can_continue_bool
493 % \end{macrocode}
494 % \end{itemize}
495 %
496 % \subsection{Interface for matrix and intarray correspondence }
497 % \label{sec:matrix}
498 %
499 % \nDescribeMacro{\qrtz_index:nn}
500 % Pixel $(x,y)$ corresponds to index $(n-y)n+x$ on the intarray.
501 % Function !\qrtz_index:nn {!\vars{x}!}{!\vars{y}!}!
502 % returns the linear index of a pixel in coordinates $(x,y)$.
503 % Size is stored in !\l_qrtz_size_int!.
504 % \nDescribeMacro{\qrtz_pixel:nn}
505 % Function !\qrtz_pixel:nn {!\vars{x}!}{!\vars{y}!}! returns the value
506 % of the entry, that is, 0 for white pixel, positive (the connected
507 % component number) if black.
508 % \begin{macrocode}
509 \cs_new:Nn \qrtz_index:nn {
510 ( \l_qrtz_size_int - (#2) ) * \l_qrtz_size_int + (#1)
511 }
512 %
513 \cs_new:Nn \qrtz_pixel:nn {
514 \intarray_item:Nn \g_qrtz_labels_intarray { \qrtz_index:nn {#1}{#2} }
515 }
516 % \end{macrocode}
517 %
518 % \subsection{Computation of connected components}
519 % \label{sec:cc}
520 %
521 % We use \textit{union-find algorithm} to compute connected
522 % components. The idea is that every pixel is a node in a graph, and a
523 % pointed connection $A \to B$ means that “$B$ is parent of $A$”. If
524 % $A$ points to itself, it means that $A$ is a “patriarch”. Two nodes
525 % $A$ and $B$ belong to the same family if they share the same patriarch.
526 %
527 % For example, consider the pointed graph
528 % \[
529 % 1 \to
530 % \begin{array}[b]{c}
531 % 3\\ \downarrow\\2
532 % \end{array}
533 % \to 4 \righttoleftarrow,\quad
534 % 5 \righttoleftarrow,
535 % \quad 6 \to 7 \to 8 \righttoleftarrow
536 % \]
537 %
538 % The patriarchs are 4, 5 and 8. The families (\textit{i.e.},
539 % connected components) are $\{1,2,3,4\}$, $\{5\}$ and $\{6,7,8\}$.
540 %
541 % There are two functions: \textit{find} that returns the patriarch of
542 % an element, and \textit{union} that joins two families, making a
543 % former patriarch to point another node in another family (otherwise
544 % we get a cycle), joining the families under one of the patriarchs.
545 % A way to avoid cycles is to make a patriarch to point another patriarch.
546 %
547 % We implement the “pointing” as the result of an array $A[i]=j$,
548 % meaning $i \to j$.
549 %
550 % The graph above is represented by the array:
551 % \begin{center}
552 % \begin{tabular}{@{}c*{8}{c}@{}}
553 % \toprule
554 % $i$ & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\ \midrule
555 % $A[i]$ & 2 & 4 & 2 & 4 & 5 & 7 & 8 & 8 \\ \bottomrule
556 % \end{tabular}
557 % \end{center}
558 %
559 % \begin{itemize}
560 % \item find($i$): $j\leftarrow i$. while $A[j]\neq j$ do: $j \leftarrow A[j]$. Return $j$.
561 %
562 % \item union($i,j$): $A[\text{find}(i)] \leftarrow \text{find}(j)$.
563 % \end{itemize}
564 %
565 % We initialize the array with $A[i]=i$ if pixel is black or $A[i]=0$
566 % otherwise, for all $i$ (all black pixels are disconnected).
567 %
568 % \textit{First pass}: Loop through all pixels $x,y$, that correspond to index
569 % $i$. If a pixel $p$ is black: if pixel $q$ to the right of $p$ is
570 % also black, perform union($p,q$); if pixel $q$ below $p$ is
571 % also black, perform union($p,q$).
572 %
573 % At the end of first pass, all connected components are determined.
574 % It remains to clearly label the component of each pixel, doing the
575 % second pass:
576 %
577 % \textit{Second pass}: loop for all indexes $i$:
578 % $A[i] \leftarrow \text{find(i)}$.
579 %
580 % Now each pixel $p$, which has index $i$\footnote{The index of a
581 % pixel $(x,y)$ is given by function \texttt{\string\qrtz\_index:nn
582 % \{\textit{x}\} \{\textit{y}\}}.}, belongs to component $c = A[i]$.
583 % Below we implement that. The array $A$ is the variable
584 % !\g_qrtz_labels_intarray!.
585 %
586 % \nDescribeMacro{\qrtz_find:nnN}
587 % Implementation of find: !\qrtz_find:nnN {!\vars{x}!}{!\vars{y}!} #3!.
588 % Last arg \texttt{\#3} should be an integer var (module \textsf{l3int}) that
589 % will receive the index of the patriarc of $(x,y)$.
590 % \begin{macrocode}
591 \cs_new:Nn \qrtz_find:nnN {
592 \int_set:Nn #3 { \qrtz_index:nn {#1}{#2} }
593 \int_until_do:nNnn {\intarray_item:Nn \g_qrtz_labels_intarray #3} = #3
594 {
595 \int_set:Nn #3 {\intarray_item:Nn \g_qrtz_labels_intarray #3}
596 }
597 }
598 % \end{macrocode}
599 %
600 % % \nDescribeMacro{\qrtz_union:nnnn}
601 % Implementation of \textit{union} of pixels $(x_0,y_0)$ and $(x_1,y_1)$.
602 % \begin{macrocode}
603 \cs_new:Nn \qrtz_union:nnnn {
604 \qrtz_find:nnN {#1}{#2} \l_tmpa_int
605 \qrtz_find:nnN {#3}{#4} \l_tmpb_int
606 \intarray_gset:Nnn \g_qrtz_labels_intarray \l_tmpb_int \l_tmpa_int
607 }
608 % \end{macrocode}
609 %
610 % \nDescribeMacro{\qrtz_compute_components:n}
611 % A function that performs the two passes of the
612 % algorithm described above. Its argument \texttt{\#1} is the binary
613 % data of the QR code, loaded into the intarray
614 % !\l_qrtz_labels_intarray!: 1 becomes the index $i$, 0 remains 0.
615 % Index will be stored in !\l_tmpa_int!
616 % \begin{macrocode}
617 \cs_new:Nn \qrtz_compute_components:n {
618 \int_zero:N \l_tmpa_int
619 \tl_map_inline:nn {#1}
620 {
621 \int_incr:N \l_tmpa_int
622 \intarray_gset:Nnn
623 \g_qrtz_labels_intarray { \l_tmpa_int } { ##1 * \l_tmpa_int }
624 }
625 % \end{macrocode}
626 % Set matrix size $n$ as $n\leftarrow \sqrt{l}$,
627 % where $l=\text{\textit{length}}$ is stored in !\l_tmpa_int! after
628 % previous loop.
629 % \begin{macrocode}
630 \int_set:Nn \l_qrtz_size_int { \fp_to_int:n { sqrt( \l_tmpa_int ) } }
631 % \end{macrocode}
632 %
633 % \textit{First pass}: Iterate $x$,$y$ from 1 to
634 % !\l_qrtz_size_int! (size of side of % QRcode);
635 % if (it make sense pixel at right of $(x,y)$) \textit{and}
636 % (current pixel is black) \textit{and} (pixel to the right is black)
637 % [“and” computed with !lazy_all!, that is, one false condition stops
638 % evaluation, returns false], do union operation of these pixels. The
639 % analogue conditions for pixel below current pixel.
640 % \begin{macrocode}
641 \int_step_inline:nn { \l_qrtz_size_int } { % i=##1
642 \int_step_inline:nn { \l_qrtz_size_int } { % j = ####1
643
644 \bool_if:nT {
645 \bool_lazy_all_p:n {
646 { \int_compare_p:nNn {##1} < \l_qrtz_size_int }
647 { \int_compare_p:nNn { \qrtz_pixel:nn {##1}{####1} } > 0 }
648 { \int_compare_p:nNn { \qrtz_pixel:nn {1 + ##1}{####1} } > 0 }
649 }
650 }
651 { \qrtz_union:nnnn {##1}{####1} {1 + ##1}{####1} }
652
653 \bool_if:nT {
654 \bool_lazy_all_p:n {
655 { \int_compare_p:nNn {####1} < \l_qrtz_size_int }
656 { \int_compare_p:nNn { \qrtz_pixel:nn {##1}{####1} } > 0 }
657 { \int_compare_p:nNn { \qrtz_pixel:nn {##1}{1 + ####1} } > 0 }
658 }
659 }
660 { \qrtz_union:nnnn {##1}{####1} {##1}{1 + ####1} }
661 }
662 }
663 % \end{macrocode}
664 %
665 % \textit{Second pass}: if pixel is black, set its value as its
666 % patriarch (find operation). !\l_tmpa_int! holds index of current
667 % pixel, !\l_tmpb_int! holds index of its patriarch.
668 % \begin{macrocode}
669 \int_step_inline:nn { \l_qrtz_size_int } { % i = ##1
670 \int_step_inline:nn { \l_qrtz_size_int } { % j = ####1
671
672 \int_set:Nn \l_tmpa_int { \qrtz_index:nn {##1} {####1} }
673
674 \int_compare:nNnF
675 {\intarray_item:Nn \g_qrtz_labels_intarray \l_tmpa_int} = 0
676 {
677 \qrtz_find:nnN {##1} {####1} \l_tmpb_int
678 \intarray_gset:Nnn \g_qrtz_labels_intarray \l_tmpa_int \l_tmpb_int
679 }
680 }
681 }
682 }
683 % \end{macrocode}
684 %
685 % \subsection{Construction of the list of borders}
686 % \label{sec:borders}
687 %
688 % We decided that a border is a comma list $x,y,d,c$ where $(x,y)$ is
689 % the origin of the border, $c$ is the number of the connected
690 % component of the pixel to which this border corresponds and $d$ is
691 % one of the numbers $0$, $1$, $2$ or $3$ meaning the direction
692 % $d\cdot 90\text{°}$, with the convention that the interior (black
693 % pixel) in on the left of the border, so that when one walks by
694 % subsequent borders, the path is walked in positive sense,
695 % \textit{i.e.}, counterclockwise.
696 % \begin{center}
697 % \begin{tikzpicture}[>=stealth']
698 % \draw (0,0) rectangle (6,6);
699 % \fill[gray!30!white,draw=gray]
700 % (0,2.5) coordinate (r1) rectangle ++(1,1)
701 % (6,2.5) coordinate (r2) rectangle ++(-1,1)
702 % (3,0) coordinate (r3) rectangle ++(1,1)
703 % (3,6) coordinate (r4) rectangle ++(1,-1)
704 % (r1-|r3) coordinate (r5) rectangle ++(1,1) coordinate (r6);
705 % \draw
706 % (r1) ++(0.5,0.5) node {\parbox{1cm}{\centering{\footnotesize pixel}\\$1,y$}}
707 % (r2) ++(-0.5,0.5) node {\parbox{1cm}{\centering{\footnotesize pixel}\\$n,y$}}
708 % (r3) ++(0.5,0.5) node {\parbox{1cm}{\centering{\footnotesize pixel}\\$x,1$}}
709 % (r4) ++(0.5,-0.5) node {\parbox{1cm}{\centering{\footnotesize pixel}\\$x,n$}}
710 % (r5) ++(0.5,0.5) node {\parbox{1cm}{\centering{\footnotesize pixel}\\$x,y$}};
711 %
712 % \draw[very thick,red,<-] (r1) -- ++(0,1);
713 % \draw[very thick,red,->] (r2) -- ++(0,1);
714 % \draw[very thick,red,->] (r3) -- ++(1,0);
715 % \draw[very thick,red,<-] (r4) -- ++(1,0);
716 % \draw[very thick,red,->] (r5) -- ++(1,0);;
717 % \draw[very thick,red,<-] (r5) -- ++(0,1);
718 % \draw[very thick,red,->] (r6) -- ++(-1,0);;
719 % \draw[very thick,red,<-] (r6) -- ++(0,-1);
720 %
721 % \draw[<-] (r1) ++(-0.1,0.5) -- ++(-0.5,0) node[left] {border 1,$y+1$,3,$c_1$};
722 % \draw[<-] (r2) ++(0.1,0.5) -- ++(0.5,0) node[right] {border $n+1$,$y$,1,$c_2$};
723 % \draw[<-] (r3) ++(0.5,-0.1) -- ++(0,-0.5) node[below] {border $x$,1,0,$c_3$};
724 % \draw[<-] (r4) ++(0.5,0.1) -- ++(0,0.5) node[above] {border $x+1$,$n+1$,2,$c_4$};
725 %
726 % \draw[<-] (r6) ++(0.1,-0.3) -- ++(2.2,1.3) node[above right] {border $x+1$,$y$,1,$c_5$};%
727 % \draw[<-] (r5) ++(-0.1,0.5) -- ++(-3.2,1.3) node[above left] {border $x$,$y+1$,3,$c_5$};%
728 % \draw[<-] (r5) ++(0.5,-0.1) -- ++(0,-0.5) node[below] {border $x$,$y$,0,$c_5$};
729 % \draw[<-] (r6) ++(-0.5,0.1) -- ++(0,0.5) node[above] {border $x$,$y+1$,2,$c_5$};%
730 % \fill
731 % (r5) circle[radius=1.5pt] node[below left,xshift=1pt,yshift=1pt]
732 % {\scriptsize$(x,y)$}
733 % (r5-|r6) circle[radius=1.5pt] node[below right,xshift=-1pt,yshift=1pt]
734 % {\scriptsize$(x+1,y)$}
735 % (r5|-r6) circle[radius=1.5pt] node[above left,xshift=1pt,yshift=-1pt]
736 % {\scriptsize$(x,y+1)$}
737 % (r6) circle[radius=1.5pt] node[above right,xshift=-1pt,yshift=-1pt]
738 % {\scriptsize$(x+1,y+1)$}
739 % ;
740 % \end{tikzpicture}
741 % \end{center}
742 %
743 % \nDescribeMacro{\qrtz_build_border_list:n}
744 % We build the list of borders with the function
745 % !\qrtz_build_border_list:n!, whose arg !#1! is the binary data. It
746 % stores the borders in !\l_qrtz_border_seq! variable. Each border is
747 % a comma list \vars{x},\vars{y},\vars{dir},\vars{component}, as
748 % described above.
749 %
750 % Loop for $x,y$ from 1 to $n$. First !\int_step_inline:nn! for $x$,
751 % secont for $y$, so that $x = {}$!##1!, $y = {}$!####1!.
752 % \begin{macrocode}
753 \cs_new:Nn \qrtz_build_border_list:n {
754 % \end{macrocode}
755 % \par
756 % Next macro sets size and matrix from binary.
757 % \begin{macrocode}
758 \qrtz_compute_components:n { #1 }
759 %
760 \seq_clear:N \l_qrtz_border_seq
761 %
762 \int_step_inline:nn { \l_qrtz_size_int }
763 {
764 % x = ##1
765 \int_step_inline:nn { \l_qrtz_size_int }
766 {
767 % y = ####1
768 % \end{macrocode}
769 % Connected component number = !\l_qrtz_component!.
770 % \begin{macrocode}
771 \tl_set:Ne \l_qrtz_component { \qrtz_pixel:nn {##1}{####1} }
772 % \end{macrocode}
773 % If pixel$(x,y)$ is black ($\text{component}>0$), check whether
774 % neighbor pixels are white. We use explicitly the feature of
775 % !\bool_lazy_or:nnT! that the second test is done only if first is false.
776 % \begin{macrocode}
777 \int_compare:nNnT { \l_qrtz_component } > 0
778 {
779 % \end{macrocode}
780 % For left borders: add border $x$,$y+1$,3,\vars{component}
781 % \begin{macrocode}
782 \bool_lazy_or:nnT
783 { \int_compare_p:nNn { ##1 } = 1 }
784 { \int_compare_p:nNn { \qrtz_pixel:nn {##1 - 1}{####1} } = 0 }
785 {
786 \seq_put_left:Ne \l_qrtz_border_seq
787 { ##1, \int_eval:n{ ####1 + 1 }, 3, \l_qrtz_component }
788 }
789 %
790 % \end{macrocode}
791 % For right borders: add border $x+1$,$y$,1,\vars{component}
792 % \begin{macrocode}
793 \bool_lazy_or:nnT
794 { \int_compare_p:nNn { ##1 } = { \l_qrtz_size_int } }
795 { \int_compare_p:nNn { \qrtz_pixel:nn {##1 + 1}{####1} } = 0 }
796 {
797 \seq_put_left:Ne \l_qrtz_border_seq
798 { \int_eval:n{ ##1 + 1 }, ####1, 1, \l_qrtz_component }
799 }
800 %
801 % \end{macrocode}
802 % For top borders: add border $x+1$,$y+1$,2,\vars{component}
803 % \begin{macrocode}
804 \bool_lazy_or:nnT
805 { \int_compare_p:nNn { ####1 } = { \l_qrtz_size_int } }
806 { \int_compare_p:nNn { \qrtz_pixel:nn {##1}{####1+1} } = 0 }
807 {
808 \seq_put_left:Ne \l_qrtz_border_seq
809 {\int_eval:n{##1+1}, \int_eval:n{####1+1}, 2, \l_qrtz_component }
810 }
811 %
812 % \end{macrocode}
813 % For bottom borders: add border $x$,$y$,0,\vars{component}
814 % \begin{macrocode}
815 \bool_lazy_or:nnT
816 { \int_compare_p:nNn { ####1 } = 1 }
817 { \int_compare_p:nNn { \qrtz_pixel:nn {##1}{####1-1} } = 0 }
818 {
819 \seq_put_left:Ne \l_qrtz_border_seq
820 { ##1, ####1, 0, \l_qrtz_component }
821 }
822 }
823 }
824 }
825 }
826 % \end{macrocode}
827 %
828 % \subsection{Computation of the path}
829 % \label{sec:path}
830 %
831 % Now we get to the main task of the package. Basicly, get a border, follow
832 % the path (always removing the “walked” borders) in the same
833 % connected component until get a closed path, output that subpath and
834 % repeat until list of borders is empty.
835 %
836 % The algorithm is the following:\medskip
837 %
838 % \noindent
839 % \textit{path} $\leftarrow$ “\,”\medskip
840 %
841 % \noindent
842 % \makebox[0pt][r]{\framebox{A}\quad}while \textit{border-list}\\
843 % \mbox{}~~\textit{can-cont} $\leftarrow$ true\\
844 % \mbox{}~~\textit{go-straight} $\leftarrow$ false \quad\textit{\color{gray!50!black}\% to force first output to path}\\
845 % \mbox{}~~get \textit{border} in \textit{border-list} \quad\textit{\color{gray!50!black}\% without removing it}\\
846 % \mbox{}~~set \textit{x},\textit{y},\textit{dir},\textit{component} from \textit{border}\medskip
847 %
848 % \noindent
849 % \makebox[0pt][r]{\framebox{B}\quad}\mbox{}~~while \textit{can-cont}\\
850 % \mbox{}~~~~remove \textit{border} \quad\textit{\color{gray!50!black}\% got at the beginning or tested in the if's at end of loop} \\
851 % \mbox{}~~~~if not \textit{go-straight}\quad\textit{\color{gray!50!black}\% at turn, output to path}\\
852 % \mbox{}~~~~~|~~\textit{path} $\leftarrow$ \textit{path} + “$(x,y)$-{}-”\\
853 % \mbox{}~~~~end-if\\
854 % \mbox{}~~~~set \textit{x},\textit{y} as the end point from \textit{border} \quad\textit{\color{gray!50!black}\% walk the border}\medskip
855 %
856 % \noindent
857 % \makebox[0pt][r]{\framebox{C}\quad}\mbox{}~~~~compute \textit{inner},\textit{outer},\textit{straight} borders\\
858 % \mbox{}~~~~\textit{\color{gray!50!black}\% inner = x,y,dir\emph{+}1,c,\quad straight = x,y,dir,c,\quad outer = x,y,dir–1,c,}\\
859 % \mbox{}~~~~if \textit{inner} in \textit{border-list}\\
860 % \mbox{}~~~~|~~\textit{go-straight} $\leftarrow$ false\\
861 % \mbox{}~~~~|~~\textit{border} $\leftarrow$ \textit{inner}\\
862 % \mbox{}~~~~else\\
863 % \mbox{}~~~~|~~if \textit{straight} in \textit{border-list}\\
864 % \mbox{}~~~~|~~|~~\textit{go-straight} $\leftarrow$ true\\
865 % \mbox{}~~~~|~~|~~\textit{border} $\leftarrow$ \textit{straight}\\
866 % \mbox{}~~~~|~~else\\
867 % \mbox{}~~~~|~~|~~if \textit{outer} in \textit{border-list}\\
868 % \mbox{}~~~~|~~|~~|~~\textit{go-straight} $\leftarrow$ false\\
869 % \mbox{}~~~~|~~|~~|~~\textit{border} $\leftarrow$ \textit{outer}\\
870 % \mbox{}~~~~|~~|~~else\\
871 % \mbox{}~~~~|~~|~~|~~\textit{can-cont} $\leftarrow$ false\quad\textit{\color{gray!50!black}\% dead end, restart process to new subpath}\\
872 % \mbox{}~~~~|~~|~~|~~\textit{path} $\leftarrow$ \textit{path} + “cycle”\\
873 % \mbox{}~~~~|~~|~~end-if\\
874 % \mbox{}~~~~|~~end-if\\
875 % \mbox{}~~~~end-if\\
876 % \mbox{}~~end-while\\
877 % end-while\bigskip
878 %
879 % \nDescribeMacro{\qrtz_build_tikz_path:nN}
880 % We implement the construction of the path in function
881 % !\qrtz_build_tikz_path:nN!. The args are !#1! = \vars{binary data}
882 % and !#2! = command that will hold the path.
883 % \begin{macrocode}
884 \cs_new:Nn \qrtz_build_tikz_path:nN {
885 \message{<Computing~ Tikz~ path~ ...}
886 % build border list
887 \qrtz_build_border_list:n { #1 }
888 % \end{macrocode}
889 % Now the main action.
890 %
891 % Initializing !path!
892 % \begin{macrocode}
893 \tl_set:Nn \l_qrtz_path {}
894 % \end{macrocode}
895 % First loop at point \framebox{A}
896 % \begin{macrocode}
897 \bool_until_do:nn { \seq_if_empty_p:N \l_qrtz_border_seq }
898 {
899 % \end{macrocode}
900 % First code after \framebox{A}: initialize \textit{can-cont},
901 % \textit{go-straight}, get border and set \textit{x}, \textit{y},
902 % \textit{dir} and \textit{component} from it. The value false to
903 % \textit{go-straight} forces first output to \textit{path} that must
904 % happen at beginning of new subpath.
905 % \begin{macrocode}
906 \bool_set_true:N \l_qrtz_can_continue_bool
907 \bool_set_false:N \l_qrtz_continue_straight_bool
908 \seq_get_left:NN \l_qrtz_border_seq \l_qrtz_border
909 \tl_set:Ne \l_qrtz_x { \clist_item:Nn \l_qrtz_border 1 }
910 \tl_set:Ne \l_qrtz_y { \clist_item:Nn \l_qrtz_border 2 }
911 \tl_set:Ne \l_qrtz_dir { \clist_item:Nn \l_qrtz_border 3 }
912 \tl_set:Ne \l_qrtz_component { \clist_item:Nn \l_qrtz_border 4 }
913 % \end{macrocode}
914 % Second loop at point \framebox{B}: looping through a full subpath,
915 % walking borders continuously until this is not possible in thhe same
916 % connected component.
917 % \begin{macrocode}
918 \bool_while_do:Nn \l_qrtz_can_continue_bool
919 {
920 % \end{macrocode}
921 % Processing the border: remove from list of borders, update path if
922 % there was a turn in direction, “walk” the border, that is, update
923 % $(x,y)$ as the end of that border.
924 % \begin{macrocode}
925 \seq_remove_all:NV \l_qrtz_border_seq \l_qrtz_border
926
927 \bool_if:NF \l_qrtz_continue_straight_bool
928 { % else
929 \tl_put_right:Ne \l_qrtz_path { (\l_qrtz_x,\l_qrtz_y) -- }
930 }
931
932 \tl_set:Ne \l_qrtz_dir { \clist_item:Nn \l_qrtz_border 3 }
933
934 \int_case:nn { \l_qrtz_dir }
935 {
936 0 { \tl_set:Ne \l_qrtz_x { \int_eval:n { \l_qrtz_x + 1 } } }
937 1 { \tl_set:Ne \l_qrtz_y { \int_eval:n { \l_qrtz_y + 1 } } }
938 2 { \tl_set:Ne \l_qrtz_x { \int_eval:n { \l_qrtz_x - 1 } } }
939 3 { \tl_set:Ne \l_qrtz_y { \int_eval:n { \l_qrtz_y - 1 } } }
940 }
941 % \end{macrocode}
942 % Point \framebox{C}: compute all derivated borders that could
943 % continue the subpath: inner border has direction
944 % $\text{\textit{dir}}+1 \mod 4$, straight border has same
945 % \textit{dir} and outer has direction $\text{\textit{dir}}-1 \mod 4$.
946 % \begin{macrocode}
947 \tl_set:Ne \l_qrtz_inner { % inner=left: dir+1 mod 4
948 \l_qrtz_x, \l_qrtz_y,
949 \int_eval:n { \int_mod:nn { \l_qrtz_dir + 1 } { 4 } },
950 \l_qrtz_component
951 }
952
953 \tl_set:Ne \l_qrtz_straight {
954 \l_qrtz_x, \l_qrtz_y, \l_qrtz_dir, \l_qrtz_component
955 }
956
957 \tl_set:Ne \l_qrtz_outer { % outer=right: dir-1 +4 mod 4
958 \l_qrtz_x, \l_qrtz_y,
959 \int_eval:n { \int_mod:nn { \l_qrtz_dir + 3 } { 4 } },
960 \l_qrtz_component
961 }
962 % \end{macrocode}
963 % Tests in block at point \framebox{C}: test if inner, straight or
964 % outer borders are in list of borders, setting \textit{go-straight}
965 % accordingly and setting \textit{border} as the found member. his
966 % border set now will be processed in next iteration of the loop at
967 % \framebox{B}.
968 % \begin{macrocode}
969 \seq_if_in:NVTF \l_qrtz_border_seq \l_qrtz_inner
970 {
971 \bool_set_false:N \l_qrtz_continue_straight_bool
972 \tl_set_eq:NN \l_qrtz_border \l_qrtz_inner
973 }
974 { % else
975 \seq_if_in:NVTF \l_qrtz_border_seq \l_qrtz_straight
976 {
977 \bool_set_true:N \l_qrtz_continue_straight_bool
978 \tl_set_eq:NN \l_qrtz_border \l_qrtz_straight
979 }
980 { % else
981 \seq_if_in:NVTF \l_qrtz_border_seq \l_qrtz_outer
982 {
983 \bool_set_false:N \l_qrtz_continue_straight_bool
984 \tl_set_eq:NN \l_qrtz_border \l_qrtz_outer
985 }
986 { % else (dead-end)
987 \bool_set_false:N \l_qrtz_can_continue_bool
988 \tl_put_right:Nn \l_qrtz_path { cycle }
989 } % fi
990 } % fi
991 } % fi
992 } % end-while
993 } % end-while
994 % \end{macrocode}
995 % At this point !\l_qrtz_path! holds the path of QRcode. Set !#2! as it.
996 % \begin{macrocode}
997 \tl_set_eq:NN #2 \l_qrtz_path
998 \message{~done>.^^J}%
999 }
1000 % \end{macrocode}
1001 % \medskip
1002 %
1003 % Geneate variant \texttt{eN} to always expand binary data and make as
1004 % non-expl alias of that.
1005 % \begin{macrocode}
1006 \cs_generate_variant:Nn \qrtz_build_tikz_path:nN { eN }
1007
1008 \tl_set_eq:NN \QRTZBinaryToTikzPath \qrtz_build_tikz_path:eN
1009 % \end{macrocode}
1010 %
1011 % \subsection{Save paths in a \LaTeX3 prop list}
1012 % \label{sec:prop}
1013 %
1014 % \DescribeMacro{\QRTZgetTikzPathMaybeSaved}
1015 % The macro !\QRTZgetTikzPathMaybeSaved! is the main function to
1016 % retrieve the path corresponding to a binary data. If it was already
1017 % computed in a previous run (either just compuuted or stored in the
1018 % aux file) this path is stored into a \LaTeX3 property list
1019 % (\textsf{l3prop} module) and retrieved by this function. It is a
1020 % variant of the function below.
1021 % \begin{macrocode}
1022 \cs_new:Nn \qrtz_get_tikz_path_maybe_saved:nN {
1023 \prop_get:NeN \g_qrtz_paths_prop { #1 } { #2 }
1024 % \end{macrocode}
1025 % \texttt{qrcode} oftion \texttt{forget} is respected.
1026 % \begin{macrocode}
1027 \use:c {ifqr@forget@mode} \tl_set_eq:NN #2 \q_no_value \fi
1028 \quark_if_no_value:NTF #2
1029 {
1030 \qrtz_build_tikz_path:nN { #1 }{ #2 }
1031 \qrtz_save_write_path_to_aux:ee { #1 }{ #2 }
1032 }
1033 {
1034 \message{<Using~ saved~ Tikz~ path>^^J}%
1035 }
1036 }
1037
1038 \cs_generate_variant:Nn \qrtz_get_tikz_path_maybe_saved:nN { eN }
1039
1040 \tl_set_eq:NN \QRTZgetTikzPathMaybeSaved \qrtz_get_tikz_path_maybe_saved:eN
1041 % \end{macrocode}
1042 %
1043 % \subsection{Saving paths to aux file}
1044 % \label{sec:aux}
1045 %
1046 % \DescribeMacro{\QRTZsavePath}
1047 % The macro !\QRTZsavePath! \texttt{\{\vars{binary}\} \{\vars{tikz path}\}}
1048 % associates key \vars{binary} to value \vars{tikz path}, for later
1049 % use, to avoid recomputation of path and write correspondence to aux
1050 % file. It writes a fallback dummy definition of itself, in case this
1051 % package is removed. Its definition is actually a variant of the
1052 % macro below.
1053 % \begin{macrocode}
1054 \cs_new:Nn \qrtz_save_write_path_to_aux:nn {
1055 \bool_if:NF \g_qrtz_aux_fallback_written_bool
1056 {
1057 \iow_shipout:cn { @auxout } { \providecommand{\QRTZsavePath}[2]{} }
1058 \bool_gset_true:N \g_qrtz_aux_fallback_written_bool
1059 }
1060 \message{<Writing~ Tikz~ path~ to~ aux~ file>^^J}%
1061 \iow_shipout:cn { @auxout } { \QRTZsavePath {#1}{#2} }
1062 % \end{macrocode}
1063 % Verifying if binary data was already saved; save if necessary.
1064 % \begin{macrocode}
1065 \prop_get:NnN \g_qrtz_paths_prop { #1 } \l_tmpa_tl
1066 \quark_if_no_value:NT \l_tmpa_tl
1067 {
1068 \message{<Saving~ Tikz~ path~ to~ memory~ for~ later~ use>^^J}%
1069 \prop_gput:Nnn \g_qrtz_paths_prop { #1 } { #2 }
1070 }
1071 }
1072
1073 \cs_generate_variant:Nn \qrtz_save_write_path_to_aux:nn { ee }
1074
1075 \tl_set_eq:NN \QRTZsavePath \qrtz_save_write_path_to_aux:nn
1076 % \end{macrocode}
1077 %
1078 % \subsection{Printing function for \texttt{qrcode} matrix and binary data}
1079 % \label{sec:print}
1080 %
1081 % \nDescribeMacro{\QRTZprintQRmatrix} !\QRTZprintQRmatrix! is
1082 % \texttt{qrcodetikz}'s counterpart of !\qr@printmatrix! from
1083 % \texttt{qrcode}, that prints the QR code stored in a \texttt{qrcode}
1084 % matrix. In that package, matrices are stored with macros formed by
1085 % the matrix name and matrix indexes. Fortunately it has a function
1086 % to convert these matrices to binary data, that we print with
1087 % !\QRTZprintBinaryString!.
1088 %
1089 % \begin{macrocode}
1090 \cs_new:Nn \qrtz_print_qr_matrix:n {
1091 \message{^^J~(}
1092 \use:c{qr@matrixtobinary}{#1} % stores into \qr@binarymatrix@result
1093 \QRTZprintBinaryString{ \use:c{qr@binarymatrix@result} }
1094 \message{~)~}
1095 }
1096
1097 \tl_set_eq:NN \QRTZprintQRmatrix \qrtz_print_qr_matrix:n
1098 % \end{macrocode}
1099 %
1100 % We will copy lots of chunks of !\qr@printsavedbinarymatrix! from
1101 % \texttt{qrcode} package, so we decided to go in \LaTeXe{} mode, but
1102 % missing \LaTeX3, specially by the trouble of the chain of
1103 % !\expandafter! below.
1104 % \begin{macrocode}
1105 \ExplSyntaxOff
1106 % \end{macrocode}
1107 %
1108 % \DescribeMacro{\qrcodeFillOptions}\DescribeMacro{\QRTZ@extraFillOpts}
1109 % Macro that stores fill options and a macro that sets it.
1110 % \begin{macrocode}
1111 \newcommand{\QRTZ@extraFillOpts}{}
1112 \newcommand{\qrcodeFillOptions}[1]{\gdef\QRTZ@extraFillOpts{#1}}
1113 % \end{macrocode}
1114 %
1115 % \DescribeMacro{\QRTZprintBinaryString}
1116
1117 % Function !\QRTZprintBinaryString! is \texttt{qrcodetikz} counterpart
1118 % of function !\qr@printsavedbinarymatrix!, the macro of
1119 % \texttt{qrcode} that prints the QR code represented by a binary
1120 % string. We copy from it the computations and the use of !\parbox!,
1121 % to ensure the same behavior of \texttt{qrcode} printing functions.
1122 % It supposes context of a call of !\qrcode! command: !\qr@size! holds
1123 % size, !\qr@desiredheight!, !\qr@modulesize!, !\qr@minipagewidth! are
1124 % \TeX\ lengths and !\ifqr@tight! holds tight option.
1125 % \begin{macrocode}
1126 \newcommand{\QRTZprintBinaryString}[1]{%
1127 \setlength{\qr@modulesize}{\qr@desiredheight}%
1128 \divide\qr@modulesize by \qr@size\relax
1129 \setlength{\qr@minipagewidth}{\qr@modulesize}%
1130 \multiply\qr@minipagewidth by \qr@size\relax
1131 \ifqr@tight
1132 \else
1133 \advance\qr@minipagewidth by 8\qr@modulesize
1134 \fi
1135 % \end{macrocode}
1136 % After computations, we prepair the Ti\textit{k}Z settings: get the
1137 % path for the binary data in !#1! \dots
1138 % \begin{macrocode}
1139 \QRTZgetTikzPathMaybeSaved{#1}{\QRTZtikzPath}%
1140 % \end{macrocode}
1141 % and compute the scale. !\QRTZtikzPath! holds a path that displays
1142 % data on a square of size $n ={}$!\qr@size! cm that has to fit the
1143 % length !\qr@desiredheight!. 1 in = 2.54 cm = 72.27 pt, so 1pt =
1144 % 0.03514598 cm. Doing the math, we have the scale below.
1145 % \begin{macrocode}
1146 \pgfmathsetmacro\QRTZtikzScale{0.03514598*\qr@desiredheight/\qr@size}%
1147 % \end{macrocode}
1148 % We embed fill options in !\QRTZ@extraFillOpts! expanded once
1149 % together with scale and even odd rule options.
1150 % \begin{macrocode}
1151 \expandafter\def\expandafter\QRTZinternalFillOptions\expandafter{\expandafter
1152 [\QRTZ@extraFillOpts,scale=\QRTZtikzScale,even odd rule]}%
1153 % \end{macrocode}
1154 % Using the same !\parbox! as \texttt{qrcode} we fill the path. With
1155 % option \texttt{padding}, we enlarge the bounding box by 4 units: as
1156 % the path contains data in the rectangle from $(1,1)$ to $(n+1,n+1)$,
1157 % we add coordinates $(-3,-3)$ and $(n+5,n+5)$ to path, enlarging the
1158 % bounding box inside Ti\textit{k}Z.
1159 % \begin{macrocode}
1160 \parbox{\qr@minipagewidth}{%
1161 \tikz
1162 \expandafter\fill\QRTZinternalFillOptions
1163 \ifqr@tight\else(-3,-3)(\qr@size+5,\qr@size+5)\fi
1164 \QRTZtikzPath ;%
1165 }%
1166 }
1167 % \end{macrocode}
1168 %
1169 % \subsection{Replacing qrcode printing functions by new functions}
1170 % \label{sec:on-off}
1171 %
1172 % To implement switches to turn on and off the replacements, we save
1173 % copies of original functions and define macros that set original
1174 % functions accordingly.
1175 % \begin{macrocode}
1176 \let\qr@printmatrixORIGINAL\qr@printmatrix
1177 \let\qr@printsavedbinarymatrixORIGINAL\qr@printsavedbinarymatrix
1178
1179 \newcommand{\qrcodetikzOn}{%
1180 \let\qr@printmatrix\QRTZprintQRmatrix
1181 \let\qr@printsavedbinarymatrix\QRTZprintBinaryString
1182 }
1183
1184 \newcommand{\qrcodetikzOff}{%
1185 \let\qr@printmatrix\qr@printmatrixORIGINAL
1186 \let\qr@printsavedbinarymatrix\qr@printsavedbinarymatrixORIGINAL
1187 }
1188
1189 \qrcodetikzOn
1190 % \end{macrocode}
1191 %
1192 % We silently fix a typo in \texttt{qrcode} (there,
1193 % !\def\qr@fivezeros{11111}!), a not-so-serious bug, used only to compute
1194 % a penalty).
1195 % \begin{macrocode}
1196 \def\qr@fivezeros{00000}%
1197 % \end{macrocode}
1198 %
1199 % \Finale
1200 %
1201 \endinput
1202 %
1203 %% \CharacterTable
1204 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
1205 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
1206 %% Digits \0\1\2\3\4\5\6\7\8\9
1207 %% Exclamation \! Double quote \" Hash (number) \#
1208 %% Dollar \$ Percent \% Ampersand \&
1209 %% Acute accent \' Left paren \( Right paren \)
1210 %% Asterisk \* Plus \+ Comma \,
1211 %% Minus \- Point \. Solidus \/
1212 %% Colon \: Semicolon \; Less than \<
1213 %% Equals \= Greater than \> Question mark \?
1214 %% Commercial at \@ Left bracket \[ Backslash \\
1215 %% Right bracket \] Circumflex \^ Underscore \_
1216 %% Grave accent \` Left brace \{ Vertical bar \|
1217 %% Right brace \} Tilde \~}