Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % qrcode.ins
4 % Copyright 2014 by Anders O.F. Hendrickson ([email protected])
5 %
6 % This work may be distributed and/or modified under the
7 % conditions of the LaTeX Project Public License, either version 1.3
8 % of this license or (at your option) any later version.
9 % The latest version of this license is in
10 % http://www.latex-project.org/lppl.txt
11 % and version 1.3 or later is part of all distributions of LaTeX
12 % version 2005/12/01 or later.
13 %
14 % This work has the LPPL maintenance status `maintained'.
15 %
16 % The Current Maintainer of this work is Anders O.F. Hendrickson.
17 %
18 % This work consists of the files qrcode.dtx and qrcode.ins
19 % and the derived file qrcode.sty.
20 %
21 % \fi
22 %
23 % \iffalse
24 %<*driver>
25 \ProvidesFile{qrcode.dtx}
26 %</driver>
27 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
28 %<package>\ProvidesPackage{qrcode}
29 %<*package>
30 [2015/01/08 v1.51 QR code generation]
31 %</package>
32 %
33 %<*driver>
34 \documentclass{ltxdoc}
35 \usepackage{hyperref}
36 \usepackage[nolinks]{qrcode}
37 \EnableCrossrefs
38 \CodelineIndex
39 \OnlyDescription
40 \RecordChanges
41 \begin{document}
42 \DocInput{qrcode.dtx}
43 \PrintChanges
44 %\PrintIndex
45 \end{document}
46 %</driver>
47 % \fi
48 %
49 % \CheckSum{0}
50 %
51 % \CharacterTable
52 % {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
53 % 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
54 % Digits \0\1\2\3\4\5\6\7\8\9
55 % Exclamation \! Double quote \" Hash (number) \#
56 % Dollar \$ Percent \% Ampersand \&
57 % Acute accent \' Left paren \( Right paren \)
58 % Asterisk \* Plus \+ Comma \,
59 % Minus \- Point \. Solidus \/
60 % Colon \: Semicolon \; Less than \<
61 % Equals \= Greater than \> Question mark \?
62 % Commercial at \@ Left bracket \[ Backslash \\
63 % Right bracket \] Circumflex \^ Underscore \_
64 % Grave accent \` Left brace \{ Vertical bar \|
65 % Right brace \} Tilde \~}
66 %
67 %
68 % \changes{v1.0}{2014/09/26}{Initial version}
69 % \changes{v1.5}{2015/01/08}{Added support for new lines and hyperlinks.}
70 % \changes{v1.51}{2015/01/14}{Bug fix.}
71 %
72 % \GetFileInfo{qrcode.sty}
73 %
74 % \DoNotIndex{\newcommand,\newenvironment,\def}
75 %
76 % \def\F{F}
77 %
78 % \title{The \textsf{qrcode} package: \\
79 % \makebox[0pt][r]{\raisebox{-0.3\height}[0pt][0pt]{\qrcode[hyperlink,tight]{http://ctan.org/pkg/qrcode}}\rule{0.75in}{0pt}}Quick Response code \\
80 % generation in \LaTeX\thanks{This document
81 % corresponds to \textsf{qrcode}~\fileversion, dated \filedate.}}
82 % \author{Anders Hendrickson\\ St.~Norbert College, De~Pere, WI, USA \\ \texttt{[email protected]}}
83 % \date{January 8, 2015}
84 %
85 % \maketitle
86 %
87 %
88 % \section{Introduction}
89 %
90 % The proliferation of smartphones and tablets has led to the widespread
91 % use of Quick Response (QR) codes, which encode numeric, alphanumeric, kanji,
92 % or binary information into a square matrix of black and white pixels called modules.
93 % Although QR codes can encode any information up to almost three kilobytes,
94 % their most common use is as physical hyperlinks: a mobile device scans
95 % a printed QR code, decodes a URL, and automatically points a browser to that location.
96 %
97 % It is natural to want to include QR codes in certain \LaTeX\ documents;
98 % for example, one may want to direct the reader of a printed page to
99 % related interactive content online.
100 % Before now, the only \LaTeX\ package for producing QR codes was the
101 % immensely flexible {\tt pst-barcode}. As that package relies on
102 % {\tt pstricks}, however, it can be difficult to integrate with
103 % a pdf\LaTeX\ workflow,\footnote{%
104 % The {\tt auto-pst-pdf} or {\tt pstool} packages can make this possible
105 % by automatically running
106 % \LaTeX${}\rightarrow \tt dvips \rightarrow ps2pdf \rightarrow pdfcrop$
107 % for each barcode generated in {\tt pstricks},
108 % so long as the user is able and willing to enable {\tt\string\write18}
109 % in {\tt pdflatex} and install Perl.
110 % Judging by questions on {\tt tex.stackexchange.com} and {\tt latexcommunity.org},
111 % this is a significant hurdle for some users.
112 % Moreover, according to {\tt http://tex.stackexchange.com/questions/72876/}
113 % this workflow may have trouble if the QR code is in a header.}
114 % and a pdf\LaTeX\ user may not want the extra overhead just to produce a QR code.
115 % If one wants to avoid {\tt pstricks}, a Lua\TeX\ solution was proposed at
116 % {\tt http://tex.stackexchange.com/questions/89649/},
117 % and a plain\TeX\ solution can be found at
118 % {\catcode`\~=12\tt http://ktiml.mff.cuni.cz/~maj/QRcode.TeX},
119 % but until now no \LaTeX\ package had been available that did not call on outside machinery.
120 %
121 % The {\tt qrcode} package, in contrast, implements the QR code algorithm using
122 % only \TeX\ and \LaTeX\ commands, so it should work with any \LaTeX\ workflow.
123 % Because it draws the squares constituting a QR code using the \TeX\ primitive
124 % |\rule|, there is no need to load any graphics package whatsoever.
125 % For a user who merely wants a QR code, this is the simplest solution.
126 %
127 % \section{Usage}\label{sect:usage}
128 %
129 %
130 % \DescribeMacro{\qrcode}
131 % The package provides just one command, |\qrcode|, with the following syntax:
132 % \begin{center}
133 % |\qrcode|\oarg{options}\marg{text to be encoded}
134 % \end{center}
135 % For example, |\qrcode[hyperlink,height=0.5in]{http://www.ctan.org}| produces
136 % \begin{center}
137 % \qrcode[hyperlink,height=0.5in]{http://www.ctan.org}
138 % \end{center}
139 % Although the most common use of QR codes is as URLs,
140 % the \meta{text to be encoded} can be almost any typed text.
141 % The few exceptions to this are described in section \ref{sect:specialcharacters}.
142 %
143 % \subsection{Package Options}
144 %
145 % \DescribeMacro{nolinks}
146 % When the |hyperref| package is loaded,
147 % by default |\qrcode| assumes its argument is a URL
148 % and makes the QR code produced a hyperlink to that URL.
149 % This default behavior may be changed by invoking the |nolinks| package option.
150 % For example, most of the QR codes in this document are not in fact URLs,
151 % so this documentation was typeset with |\usepackage[nolinks]{qrcode}|.
152 % The |hyperlinks| option is an antonym to |nolinks| and is the default.
153 % These options have no effect if hyperref is not loaded.
154 %
155 % \DescribeMacro{draft}
156 % \DescribeMacro{final}
157 % Creating QR codes for short URLs takes relatively little time.\footnote{On
158 % this author's laptop, even a 60-character URL (version 4, level M) adds
159 % only about 0.7 seconds of compilation time.}
160 % Because \TeX\ was designed for typesetting, not for extensive computations,
161 % however, if many small QR codes or a single large one are required,
162 % the time spent can be quite noticeable. To save compilation
163 % time while working on a large document, calling the |draft| option
164 % causes the package not to compute QR codes, but merely to insert placeholder
165 % symbols with no data. The |final| option is an antonym to |draft|
166 % and is the default.
167 % \begin{quote}
168 % \begin{tabular}{p{1.25in}p{3.5in}}
169 % {\qrcode[draft,version=15]{http://www.tug.org}}
170 % &
171 % \begin{minipage}{3in}
172 % \tt
173 % |\documentclass{article}| \\
174 % |\usepackage[draft]{qrcode}| \\
175 % |\begin{document}| \\
176 % | \qrcode[version=15]{Dummy code}| \\
177 % |\end{document}|
178 % \end{minipage}
179 % \end{tabular}
180 % \end{quote}
181 % The placeholder symbol produced in {\tt draft} mode will have the same size
182 % and dimensions as the actual QR code.
183 %
184 % To conserve processing time, when |\qrcode| computes the binary matrix representing
185 % a QR code, it saves that binary data as a string of 1's and 0's
186 % both in a macro and in the {\tt .aux} file.
187 % Thus if the same QR code is desired later in the document, or upon the next
188 % run of \LaTeX, the QR symbol can be redrawn immediately from the saved binary data.
189 %
190 % \DescribeMacro{forget}
191 % There may be times when this is not desired; testing of this package is the chief
192 % example, but one might also have reason to believe that the {\tt .aux} file
193 % contains bad data.
194 % Invoking the |forget| package option causes |\qrcode| to calculate
195 % every QR code anew, even if a QR code for that \meta{text to be encoded}, level,
196 % and version
197 % was read from the {\tt .aux} file or was already computed earlier in the document.
198 %
199 % \subsection{Options}
200 % \DescribeMacro{\qrset}
201 % Several options affect the appearance and encoding of the QR code;
202 % {\tt qrcode} uses the {\tt xkeyval} package to handle the setting
203 % and processing of key-value pairs.
204 % The following options may either be given as optional arguments
205 % to |\qrcode| or changed within a \TeX-grouping using the
206 % macro |\qrset|.
207 % \begin{quote}
208 % \begin{tabular}{p{6cm}p{2in}}
209 % \qrcode{ABCD}
210 % {\qrset{height=1cm}%
211 % \qrcode{EFGH}}
212 % \qrcode{IJKL}
213 % &
214 % \begin{minipage}{3in}
215 % |\qrcode{ABCD}| \\
216 % |{\qrset{height=1cm}%| \\
217 % | \qrcode{EFGH}}| \\
218 % |\qrcode{IJKL}|
219 % \end{minipage}
220 % \end{tabular}
221 % \end{quote}
222 %
223 % \DescribeMacro{height}
224 % The |height=|\meta{dimen} key sets the printed height (and width) of the
225 % QR code. The default value is {\tt 2cm}.
226 % \begin{quote}
227 % \begin{tabular}{p{2in}p{2in}}
228 % \qrcode{ABCD} \qrcode[height=1cm]{ABCD}
229 % & |\qrcode{ABCD}| |\qrcode[height=1cm]{ABCD}|
230 % \end{tabular}
231 % \end{quote}
232 %
233 % \DescribeMacro{level}
234 % The QR code specification (ISO 18004:2006) includes four
235 % levels of encoding: Low, Medium, Quality, and High, in
236 % increasing order of error-correction capabaility.
237 % In general, for a given text a higher error-correction
238 % level requires more bits of information in the QR code.
239 % The key |level=|\meta{level specification}
240 % selects the minimum acceptable level.
241 % The \meta{level specification} may be |L|, |M|, |Q|, or |H|;
242 % the default is |M|.
243 % It may happen that the smallest QR code able to encode
244 % the specified text at the desired level
245 % is in fact large enough to provide a higher level of
246 % error-correction. If so, {\tt qrcode} automatically upgrades to the higher
247 % error-correction level, and a message is printed in the log file.
248 %
249 % \DescribeMacro{version}
250 % QR codes range in size from $21\times 21$ modules (``version 1'')
251 % to $177\times 177$ modules (``version 40''), in steps of 4 modules.
252 % The package automatically selects the smallest version large enough to encode
253 % the specified text at the desired error-correction level.
254 % Nevertheless, there might be occasions when a specific version is required;
255 % for example, perhaps a set of QR codes should have the same dimensions for
256 % aesthetic reasons, even though some encode shorter texts than others.
257 % For this reason, the key |version=|\meta{version specification} allows the user
258 % to specify a minimum version number, from 1 through 40, for the QR code.
259 % Setting |version=0| means ``as small as possible''; this is the default.
260 % If the desired version is not large enough to encode the text, the version
261 % will automatically be increased to accommodate the text, and a message will
262 % be placed in the log file.
263 % \begin{quote}
264 % \begin{tabular}{p{5.2cm}p{3in}}
265 % \raggedright
266 % \qrcode{ABCD}
267 % \qrcode[version=5]{ABCD}
268 % \medskip \\
269 % \qrcode[version=10]{ABCD}
270 % \qrcode[version=20]{ABCD}
271 % &
272 % \begin{minipage}{3in}
273 % |\qrcode{ABCD}| \\
274 % |\qrcode[version=5]{ABCD}| \\
275 % |\medskip \\| \\
276 % |\qrcode[version=10]{ABCD}| \\
277 % |\qrcode[version=20]{ABCD}|
278 % \end{minipage}
279 % \end{tabular}
280 % \end{quote}
281 %
282 %
283 % \DescribeMacro{tight}
284 % \DescribeMacro{padding}
285 % The QR specification states that a QR code should be surrounded by white\-space
286 % of a width equal to that of four modules. In many applications, a document
287 % author is likely to provide sufficient spacing anyway (e.g., by placing the
288 % QR code in a {\tt center} environment, header, or |\marginpar|), so by
289 % default the |qrcode| package adds no spacing. If the option |padding| is
290 % specified, however, the QR code will automatically be surrounded with 4 modules'
291 % worth of white\-space. The key |tight| is an antonym of |padding|; the default is |tight|.
292 %
293 % \DescribeMacro{link}
294 % \DescribeMacro{nolink}
295 % \DescribeMacro{\qrcode*}
296 % As described above, if the |hyperref| package is loaded,
297 % then the QR codes produced in a PDF document can be made
298 % into hyperlinks to their text. The default behavior
299 % can be controlled with the options |nolinks| and |hyperlinks|,
300 % but this default can be overridden for individual QR codes by invoking
301 % the options |link| or |nolink|.
302 % Moreover, the starred version of the macro, |\qrcode*|, is a shorthand
303 % equivalent to |\qrcode[nolink]|.
304 % \begin{quote}
305 % \begin{tabular}{p{5.2cm}p{3in}}
306 % \raggedright
307 % \qrset{link, height=1.5cm}
308 % \qrcode{http://www.ctan.org}
309 % \qrcode[nolink]{This is not a URL.}
310 % \qrcode*{Neither is this.}
311 % &
312 % \begin{minipage}{3in}
313 % |\qrset{link, height=1.5cm}| \\
314 % |\qrcode{http://www.ctan.org}| \\
315 % |\qrcode[nolink]{This is not a URL.}| \\
316 % |\qrcode*{Neither is this.}|
317 % \end{minipage}
318 % \end{tabular}
319 % \end{quote}
320 %
321 % \subsection{Special characters}\label{sect:specialcharacters}
322 % Many URLs can be processed by \TeX\ with no hiccups,
323 % but not infrequently a URL may contain the symbols |%|, |#|,
324 % |~|, |_|, and |&|. Moreover, QR codes need not just contain
325 % URL's, so a user may wish to encode text containing |^|, |$|, or spaces.
326 % The |qrcode| package offers two ways of coping with these special characters.
327 %
328 % First, the |\qrcode| command itself processes its \meta{text to be encoded}
329 % in a limited verbatim mode. The following characters will be encoded into
330 % the QR code as typed:
331 % \begin{center}
332 % |#| |$| |&| |^| |_| |~| |%| {\tt\char32}
333 % \end{center}
334 % and line breaks as well.\footnote{Technically, when the input character
335 % {\tt\char`\^\char`\^M} (CR, charcode 13) is encountered,
336 % the character {\tt\char`\^\char`\^J} (LF, charcode 10) is placed into the QR code.}
337 % Conspicuously absent from this list are |\|, |{|, and |}|.
338 % This is intentional, so that macros may be used within |\qrcode|
339 % to generate the \meta{text to be encoded} automatically.
340 % If these characters are desired, they may be obtained by ``escaping'' them
341 % with an extra backslash.
342 % \begin{quote}
343 % \begin{tabular}{p{2in}p{3in}}
344 % \qrset{height=1.5cm}%
345 % \qrcode{We can include #$&^_~%.}
346 % \def\foo{bar}%
347 % \qrcode{Set the \foo\ high.}
348 % \qrcode{We must escape \\emph\{this\}.}
349 % & \begin{minipage}{3in}
350 % |\qrset{height=1.5cm}%| \\
351 % |\qrcode{We can include #$&^_~%.}| \\
352 % |\def\foo{bar}%| \\
353 % |\qrcode{Set the \foo\ high.}| \\
354 % |\qrcode{We must escape \\emph\{this\}.}|
355 % \end{minipage}
356 % \end{tabular}
357 % \end{quote}
358 %
359 % As with all verbatim modes, however, because \TeX\ irrevocably sets catcodes
360 % when it first encounters characters, this will not work if the |\qrcode| macro
361 % is contained in another macro. If you call |\qrcode| inside an
362 % |\fbox| or a |\marginpar|, for example, and if your URL contains one of those
363 % special characters, you will either encounter error messages or (worse, because
364 % it is undetectable to the naked eye) have the wrong QR code typeset.
365 % In this scenario, you can still include any of the characters
366 % |#$&^_~%|{\tt\char32}|\{}|
367 % by escaping them with an extra backslash;
368 % so long as they eventually pass unexpanded to |\qrcode|,
369 % they will produce the correct QR code.
370 % A line break may be obtained with |\?|.
371 % \begin{quote}
372 % \begin{tabular}{p{1.5cm}p{2in}}
373 % \fbox{\qrcode[height=1cm]{\#\$\&\^\_\~\?\%\ \\\{\}}}
374 % & |\fbox{qrcode[height=1cm]{\#\$\&\^\_\~\?\%\ \\\{\}}}|
375 % \end{tabular}
376 % \end{quote}
377 %
378 % \section{Limitations and Cautions}
379 %
380 % \begin{itemize}
381 % \item The QR specification includes modes for encoding numeric, alphanumeric,
382 % or Kanji data more efficiently. This package does not (yet) offer
383 % those options.
384 % \item The QR specification offers ways to string lengthy data across multiple
385 % QR codes. This package does not implement that possibility.
386 % \end{itemize}
387 %
388 % \StopEventually{}
389 %
390 % \section{Implementation}
391 % \subsection{Key handling and options}
392 % \begin{macrocode}
393 %%PACKAGE LOADING
394 \RequirePackage{xcolor}%
395 \RequirePackage{xkeyval}%
396
397 %%INITIAL CODE
398 \newif\ifqr@draft@mode
399 \newif\ifqr@forget@mode
400
401 %%DECLARATION OF OPTIONS
402 \define@boolkey{qr}[qr@]{draft}[true]{\ifqr@draft\qr@draft@modetrue\else\qr@draft@modefalse\fi}%
403 \define@boolkey{qr}[qr@]{final}[true]{\ifqr@final\qr@draft@modefalse\else\qr@draft@modetrue\fi}%
404 \define@boolkey{qr}[qr@]{forget}[true]{\ifqr@forget\qr@forget@modetrue\else\qr@forget@modefalse\fi}%
405 \define@boolkey{qr}[qr@]{hyperlink}[true]{}% %This creates \ifqr@hyperlink.
406 \define@boolkey{qr}[qr@]{hyperlinks}[true]{\ifqr@hyperlinks\qr@hyperlinktrue\else\qr@hyperlinkfalse\fi}%
407 \define@boolkey{qr}[qr@]{link}[true]{\ifqr@link\qr@hyperlinktrue\else\qr@hyperlinkfalse\fi}%
408 \define@boolkey{qr}[qr@]{nolink}[true]{\ifqr@nolink\qr@hyperlinkfalse\else\qr@hyperlinktrue\fi}% %Make nolink an antonym.
409 \define@boolkey{qr}[qr@]{links}[true]{\ifqr@links\qr@hyperlinktrue\else\qr@hyperlinkfalse\fi}%
410 \define@boolkey{qr}[qr@]{nolinks}[true]{\ifqr@nolinks\qr@hyperlinkfalse\else\qr@hyperlinktrue\fi}% %Make nolinks an antonym.
411
412 %%EXECUTION OF OPTIONS
413 \qr@draft@modefalse
414 \qr@forget@modefalse
415 \qr@hyperlinktrue
416
417 \ProcessOptionsX<qr>
418
419 % \end{macrocode}
420 % \subsection{Utilities}
421 % \begin{macrocode}
422 %COUNTERS
423 \newcounter{qr@i}%
424 \newcounter{qr@j}%
425 \newcount\qr@a
426 \newcount\qr@b
427 \newcount\qr@c
428
429 %BASICS
430 \let\xa=\expandafter
431
432 %This is for messages.
433 \newlinechar=`\^^J
434
435
436 %Tests
437 \def\qr@relax{\relax}%
438
439 %Manipulating macros
440 \def\qr@preface@macro#1#2{%
441 % #1 = macro name
442 % #2 = text to add to front of macro
443 \def\qr@tempb{#2}%
444 \xa\xa\xa\def\xa\xa\xa#1\xa\xa\xa{\xa\qr@tempb #1}%
445 }%
446
447 \def\qr@g@preface@macro#1#2{%
448 % #1 = macro to be appended to
449 % #2 = code to add
450 \edef\qr@tempb{#2}%
451 \xa\xa\xa\gdef\xa\xa\xa#1\xa\xa\xa{\xa\qr@tempb#1}%
452 }
453
454
455 \def\qr@getstringlength#1{%
456 \bgroup
457 \qr@a=0%
458 \xdef\qr@thestring{#1}%
459 \xa\qr@stringlength@recursive\xa(\qr@thestring\relax\relax)%
460 \xdef\qr@stringlength{\the\qr@a}%
461 \egroup
462 }%
463
464 \def\qr@stringlength@recursive(#1#2){%
465 \def\qr@testi{#1}%
466 \ifx\qr@testi\qr@relax
467 %we are done.
468 \let\qr@next=\relax%
469 \else
470 \advance\qr@a by 1%
471 \def\qr@next{\qr@stringlength@recursive(#2)}%
472 \fi
473 \qr@next
474 }%
475 % \end{macrocode}
476 % \subsubsection{For-loop macro}
477 % We implement a macro |\qr@for| with the syntax
478 % \begin{center}
479 % |\qr@for| \meta{control sequence}=\meta{number} to \meta{number} by \meta{number} \marg{loop body}
480 % \end{center}
481 % The \meta{control sequence} becomes the loop variable,
482 % which is a \TeX\ counter.
483 % For example, |\qr@for \i=1 to 8 by 2 {\fbox{\number\i}}|
484 % produces {\makeatletter\qr@for \i=1 to 8 by 2 {\fbox{\number\i}}}.
485 % \begin{macrocode}
486 %The \qr@for@depth counter measures the depth of our loop.
487 %The outermost loop has depth zero.
488 \newcount\qr@for@depth%
489 \newcount\qr@for@maxdepth%
490 \qr@for@depth=0%
491 \qr@for@maxdepth=0%
492 %These counters are used in the qr@for loop.
493 \newcount\qr@for@start%
494 \newcount\qr@for@end%
495 \newcount\qr@for@step%
496 %Now a macro to get a new count for every depth as needed.
497 \def\qr@allocate@new@for@counter{%
498 \global\advance\qr@for@maxdepth by 1%
499 \newcount\qr@newforcount%
500 \xa\global\xa\let\csname qr@for@var@\the\qr@for@maxdepth\endcsname=\qr@newforcount%
501 }%
502
503 \newif\ifqr@loopshouldrun
504 %The extra # in the following definition makes sure #4 scoops up everything up to the next opening brace.
505 %This is needed so the step can include more than one character without being enclosed in braces itself.
506 \def\qr@for #1=#2to#3by#4#{%
507 \qr@for@int{#1}{#2}{#3}{#4}%
508 }%
509 \long\def\qr@for@int#1#2#3#4#5{%
510 \bgroup
511 %Because we're working within a TeX group,
512 %any values of \qr@for@start, \qr@for@end, and \qr@for@step from an outer loop
513 %will be restored after the \egroup.
514 %
515 %For the \qr@for@var itself, however, we need a different counter,
516 %because the user's text within the loop might need to access the variable from the outer loop.
517 \advance\qr@for@depth by 1\relax% This is a local change.
518 \ifnum\qr@for@depth>\qr@for@maxdepth%
519 %This is the first time we have gone to this depth of nesting!
520 %We should only be over by one.
521 \qr@allocate@new@for@counter%
522 \fi
523 % \showthe\qr@for@depth%
524 \xa\let\xa\qr@for@var\xa=\csname qr@for@var@\the\qr@for@depth\endcsname%
525 %Now \qr@for@var points to the same register as \qr@for@var@3 or something.
526 %The next line lets the user-level variable (e.g., \i or \j) point to the same count register.
527 \let#1=\qr@for@var%
528 %Now establish the looping parameters.
529 \edef\qr@for@start@text{#2}%
530 \edef\qr@for@end@text{#3}%
531 \edef\qr@for@step@text{#4}%
532 \def\qr@for@body{\bgroup #5\egroup}%
533 \xa\qr@for@start\qr@for@start@text\relax%
534 \xa\qr@for@end \qr@for@end@text\relax%
535 \xa\qr@for@step \qr@for@step@text\relax%
536 %
537 %Next, test whether the loop should run at all.
538 % * "\qr@for \i = 1 to 0 by 1" should fail.
539 % * "\qr@for \i = 3 to 5 by -1" should fail.
540 % * "\qr@for \i = 6 to 2 by 1" should fail.
541 % * "\qr@for \i = 4 to 4 by -1" should run.
542 % * "\qr@for \i = 4 to 4 by 1" should run.
543 % * "\qr@for \i = 5 to 7 by 0" should fail.
544 %The loop should fail if (step)=0 or if (step) and (end-start) have opposite signs.
545 %The loop will fail if (step=0) or (step)*(end-start)<0.
546 % TODO: "\qr@for \i = 5 to 5 by 0" should run (just one iteration).
547 \qr@loopshouldruntrue
548 \ifnum\qr@for@step=0\relax
549 \qr@loopshouldrunfalse
550 \fi
551 \qr@a=\qr@for@end%
552 \advance\qr@a by -\qr@for@start%
553 \multiply\qr@a by \qr@for@step%
554 \ifnum\qr@a<0\relax
555 \qr@loopshouldrunfalse
556 \fi
557 \ifqr@loopshouldrun
558 \qr@for@var=\qr@for@start%
559 \ifnum\qr@for@step>0\relax
560 \def\qr@for@recursive{%
561 \qr@for@body%
562 \advance\qr@for@var by \qr@for@step%
563 \ifnum\qr@for@var>\qr@for@end%
564 \let\qr@for@next=\relax%
565 \else%
566 \let\qr@for@next=\qr@for@recursive%
567 \fi%
568 \qr@for@next%
569 }%
570 \else
571 \def\qr@for@recursive{%
572 \qr@for@body%
573 \advance\qr@for@var by \qr@for@step%
574 \ifnum\qr@for@var<\qr@for@end%
575 \let\qr@for@next=\relax%
576 \else%
577 \let\qr@for@next=\qr@for@recursive%
578 \fi%
579 \qr@for@next%
580 }%
581 \fi
582 \qr@for@recursive%
583 \fi
584 \egroup
585 }%
586 % \end{macrocode}
587 % \subsubsection{Base conversions}
588 % \begin{macrocode}
589 \def\qr@padatfront#1#2{%
590 % #1 = macro containing text to pad
591 % #2 = desired number of characters
592 % Pads a number with initial zeros.
593 \qr@getstringlength{#1}%
594 \qr@a=\qr@stringlength\relax%
595 \advance\qr@a by 1\relax%
596 \qr@for \i = \qr@a to #2 by 1\relax%
597 {\qr@g@preface@macro{#1}{0}}%
598 }
599
600
601 \qr@a=-1\relax%
602 \def\qr@savehexsymbols(#1#2){%
603 \advance\qr@a by 1\relax%
604 \xa\def\csname qr@hexchar@\the\qr@a\endcsname{#1}%
605 \xa\edef\csname qr@hextodecimal@#1\endcsname{\the\qr@a}%
606 \ifnum\qr@a=15\relax
607 %Done.
608 \let\qr@next=\relax%
609 \else
610 \def\qr@next{\qr@savehexsymbols(#2)}%
611 \fi%
612 \qr@next%
613 }%
614 \qr@savehexsymbols(0123456789abcdef\relax\relax)%
615
616
617 \def\qr@decimaltobase#1#2#3{%
618 % #1 = macro to store result
619 % #2 = decimal representation of a positive integer
620 % #3 = new base
621 \bgroup
622 \edef\qr@newbase{#3}%
623 \gdef\qr@base@result{}%
624 \qr@a=#2\relax%
625 \qr@decimaltobase@recursive%
626 \xdef#1{\qr@base@result}%
627 \egroup
628 }
629 \def\qr@decimaltobase@recursive{%
630 \qr@b=\qr@a%
631 \divide\qr@b by \qr@newbase\relax
632 \multiply\qr@b by -\qr@newbase\relax
633 \advance\qr@b by \qr@a\relax%
634 \divide\qr@a by \qr@newbase\relax%
635 \ifnum\qr@b<10\relax
636 \edef\qr@newdigit{\the\qr@b}%
637 \else
638 \edef\qr@newdigit{\csname qr@hexchar@\the\qr@b\endcsname}%
639 \fi
640 \edef\qr@argument{{\noexpand\qr@base@result}{\qr@newdigit}}%
641 \xa\qr@g@preface@macro\qr@argument%
642 \ifnum\qr@a=0\relax
643 \relax
644 \else
645 \xa\qr@decimaltobase@recursive
646 \fi
647 }
648
649 \newcommand\qr@decimaltohex[3][0]{%
650 % #1 (opt.) = number of hex digits to create
651 % #2 = macro to store result
652 % #3 = decimal digits to convert
653 \qr@decimaltobase{#2}{#3}{16}%
654 \qr@padatfront{#2}{#1}%
655 }
656
657
658 \newcommand\qr@decimaltobinary[3][0]{%
659 % #1 (opt.) = number of bits to create
660 % #2 = macro to store result
661 % #3 = decimal digits to convert
662 \qr@decimaltobase{#2}{#3}{2}%
663 \qr@padatfront{#2}{#1}%
664 }
665
666 \qr@for \i = 0 to 15 by 1%
667 {%
668 \qr@decimaltohex[1]{\qr@hexchar}{\the\i}%
669 \qr@decimaltobinary[4]{\qr@bits}{\the\i}%
670 \xa\xdef\csname qr@b2h@\qr@bits\endcsname{\qr@hexchar}%
671 \xa\xdef\csname qr@h2b@\qr@hexchar\endcsname{\qr@bits}%
672 }%
673
674
675
676 \newcommand\qr@binarytohex[3][\relax]{%
677 % #1 (optional) = # digits desired
678 % #2 = macro to save to
679 % #3 = binary string (must be multiple of 4 bits)
680 \def\qr@test@i{#1}%
681 \ifx\qr@test@i\qr@relax%
682 %No argument specified
683 \def\qr@desireddigits{0}%
684 \else
685 \def\qr@desireddigits{#1}%
686 \fi
687 \gdef\qr@base@result{}%
688 \edef\qr@argument{(#3\relax\relax\relax\relax\relax)}%
689 \xa\qr@binarytohex@int\qr@argument%
690 \qr@padatfront{\qr@base@result}{\qr@desireddigits}%
691 \xdef#2{\qr@base@result}%
692 }
693 \def\qr@binarytohex@int(#1#2#3#4#5){%
694 % #1#2#3#4 = 4 bits
695 % #5 = remainder, including \relax\relax\relax\relax\relax terminator
696 \def\qr@test@i{#1}%
697 \ifx\qr@test@i\qr@relax%
698 %Done.
699 \def\qr@next{\relax}%
700 \else%
701 \xdef\qr@base@result{\qr@base@result\csname qr@b2h@#1#2#3#4\endcsname}%
702 \def\qr@next{\qr@binarytohex@int(#5)}%
703 \fi%
704 \qr@next%
705 }
706
707 \newcommand\qr@hextobinary[3][\relax]{%
708 % #1 (optional) = # bits desired
709 % #2 = macro to save to
710 % #3 = hexadecimal string
711 \bgroup
712 \def\qr@test@i{#1}%
713 \ifx\qr@test@i\qr@relax%
714 %No argument specified
715 \def\qr@desireddigits{0}%
716 \else
717 \def\qr@desireddigits{#1}%
718 \fi
719 \gdef\qr@base@result{}%
720 \edef\qr@argument{(#3\relax\relax)}%
721 \xa\qr@hextobinary@int\qr@argument%
722 \qr@padatfront{\qr@base@result}{\qr@desireddigits}%
723 \xdef#2{\qr@base@result}%
724 \egroup
725 }
726 \def\qr@hextobinary@int(#1#2){%
727 % #1 = hexadecimal character
728 % #2 = remainder, including \relax\relax terminator
729 \def\qr@test@@i{#1}%
730 \ifx\qr@test@@i\qr@relax%
731 %Done.
732 \def\qr@next{\relax}%
733 \else%
734 \xdef\qr@base@result{\qr@base@result\csname qr@h2b@#1\endcsname}%
735 \def\qr@next{\qr@hextobinary@int(#2)}%
736 \fi%
737 \qr@next%
738 }
739
740 \def\qr@hextodecimal#1#2{%
741 \edef\qr@argument{#2}%
742 \xa\qr@a\xa=\xa\number\xa"\qr@argument\relax%
743 \edef#1{\the\qr@a}%
744 }
745
746 \def\qr@hextodecimal#1#2{%
747 % #1 = macro to store result
748 % #2 = hexadecimal representation of a positive integer
749 \bgroup
750 \qr@a=0\relax%
751 \edef\qr@argument{(#2\relax)}%
752 \xa\qr@hextodecimal@recursive\qr@argument%
753 \xdef#1{\the\qr@a}%
754 \egroup
755 }
756 \def\qr@hextodecimal@recursive(#1#2){%
757 % #1 = first hex char
758 % #2 = remainder
759 \advance \qr@a by \csname qr@hextodecimal@#1\endcsname\relax%
760 \edef\qr@testii{#2}%
761 \ifx\qr@testii\qr@relax%
762 %Done.
763 \let\qr@next=\relax%
764 \else
765 %There's at least one more digit.
766 \multiply\qr@a by 16\relax
767 \edef\qr@next{\noexpand\qr@hextodecimal@recursive(#2)}%
768 \fi%
769 \qr@next%
770 }
771 % \end{macrocode}
772 % \subsubsection{Catcode setup}
773 % \begin{macrocode}
774 %The following catcode trickery creates special characters
775 %with catcode 12 (other) for use in our verbatim handling.
776 {\catcode`\ =12\relax\gdef\qr@otherspace{ }}%
777 {\catcode`\%=12\relax\gdef\qr@otherpercent{%}}%
778 {\catcode`\#=12\relax\gdef\qr@otherpound{#}}%
779 {\catcode`\|=0\relax|catcode`|\=12|relax|gdef|qr@otherbackslash{\}}%
780 {\catcode`\^^J=12\relax\gdef\qr@otherlf{^^J}}%
781 \bgroup
782 \catcode`\<=1\relax
783 \catcode`\>=2\relax
784 \catcode`\{=12\relax\gdef\qr@otherleftbrace<{>%
785 \catcode`\}=12\relax\gdef\qr@otherrightbrace<}>%
786 \egroup%
787 {\catcode`\&=12\relax\gdef\qr@otherampersand{&}}%
788 {\catcode`\~=12\relax\gdef\qr@othertilde{~}}%
789 {\catcode`\^=12\relax\gdef\qr@othercaret{^}}%
790 {\catcode`\_=12\relax\gdef\qr@otherunderscore{_}}%
791 {\catcode`\$=12\relax\gdef\qr@otherdollar{$}}%
792
793 %Line feeds require some special handling. \TeX\ reads a line feed in the input
794 %as |^^M| (carriage return, character code 13), but it should be encoded in a
795 %QR code as |^^J| (line feed, character code 10).
796 %To do this, we make |^^M| an active character and a synonym for a |^^J| with catcode 12.
797 %Note that the macro |\qr@verbatimlinefeeds| must itself be processed with |^^M| active.
798 {\catcode`\^^M=13\relax\gdef\qr@verbatimlinefeeds{\let^^M=\qr@otherlf}}
799 \def\qr@verbatimcatcodes{%
800 \catcode`\#=12\relax
801 \catcode`\$=12\relax
802 \catcode`\&=12\relax
803 \catcode`\^=12\relax
804 \catcode`\_=12\relax
805 \catcode`\~=12\relax
806 \catcode`\%=12\relax
807 \catcode`\ =12\relax
808 \catcode`\^^M=13\relax\qr@verbatimlinefeeds}%
809
810 \def\qr@setescapedspecials{%
811 \let\ =\qr@otherspace%
812 \let\%=\qr@otherpercent%
813 \let\#=\qr@otherpound%
814 \let\&=\qr@otherampersand%
815 \let\^=\qr@othercaret%
816 \let\_=\qr@otherunderscore%
817 \let\~=\qr@othertilde%
818 \let\$=\qr@otherdollar%
819 \let\\=\qr@otherbackslash%
820 \let\{=\qr@otherleftbrace%
821 \let\}=\qr@otherrightbrace%
822 \let\?=\qr@otherlf%
823 }%
824 % \end{macrocode}
825 % \subsection{Plotting}
826 % \begin{macrocode}
827 \def\qr@creatematrix#1{%
828 \xa\gdef\csname #1\endcsname##1##2{%
829 \csname #1@##1@##2\endcsname
830 }%
831 }%
832
833
834 \def\qr@storetomatrix#1#2#3#4{%
835 % #1 = matrix name
836 % #2 = row number
837 % #3 = column number
838 % #4 = value of matrix entry
839 \xa\gdef\csname #1@#2@#3\endcsname{#4}%
840 }%
841
842 \def\qr@estoretomatrix#1#2#3#4{%
843 % This version performs exactly one expansion on #4.
844 % #1 = matrix name
845 % #2 = row number
846 % #3 = column number
847 % #4 = value of matrix
848 \xa\xa\xa\gdef\xa\xa\csname #1@#2@#3\endcsname\xa{#4}%
849 }%
850
851 \def\qr@matrixentry#1#2#3{%
852 % #1 = matrix name
853 % #2 = row number
854 % #3 = column number
855 \csname #1@#2@#3\endcsname%
856 }%
857
858
859
860 \def\qr@createsquareblankmatrix#1#2{%
861 \qr@creatematrix{#1}%
862 \xa\gdef\csname #1@numrows\endcsname{#2}%
863 \xa\gdef\csname #1@numcols\endcsname{#2}%
864 \qr@for \i = 1 to #2 by 1%
865 {\qr@for \j = 1 to #2 by 1%
866 {\qr@storetomatrix{#1}{\the\i}{\the\j}{\qr@blank}}}%
867 }%
868
869 \def\qr@numberofrowsinmatrix#1{%
870 \csname #1@numrows\endcsname%
871 }%
872
873 \def\qr@numberofcolsinmatrix#1{%
874 \csname #1@numcols\endcsname%
875 }%
876
877 \def\qr@setnumberofrows#1#2{%
878 \xa\xdef\csname #1@numrows\endcsname{#2}%
879 }%
880
881 \def\qr@setnumberofcols#1#2{%
882 \xa\xdef\csname #1@numcols\endcsname{#2}%
883 }%
884
885 \newlength\qr@desiredheight
886 \setlength\qr@desiredheight{2cm}%
887 \newlength\qr@modulesize
888 \newlength\qr@minipagewidth
889
890 \def\qr@printmatrix#1{%
891 \def\qr@black{\rule{\qr@modulesize}{\qr@modulesize}}%
892 \def\qr@white{\rule{\qr@modulesize}{0pt}}%
893 \def\qr@black@fixed{\rule{\qr@modulesize}{\qr@modulesize}}%
894 \def\qr@white@fixed{\rule{\qr@modulesize}{0pt}}%
895 \def\qr@black@format{\rule{\qr@modulesize}{\qr@modulesize}}%
896 \def\qr@white@format{\rule{\qr@modulesize}{0pt}}%
897 %Set module size
898 \setlength{\qr@modulesize}{\qr@desiredheight}%
899 \divide\qr@modulesize by \qr@size\relax%
900 %
901 \setlength{\qr@minipagewidth}{\qr@modulesize}%
902 \multiply\qr@minipagewidth by \qr@size\relax%
903 \ifqr@tight
904 \else
905 \advance\qr@minipagewidth by 8\qr@modulesize%
906 \fi
907 \begin{minipage}{\qr@minipagewidth}%
908 \baselineskip=\qr@modulesize%
909 \ifqr@tight\else\rule{0pt}{4\qr@modulesize}\par\fi% %Blank space at top.
910 \qr@for \i = 1 to \qr@numberofrowsinmatrix{#1} by 1%
911 {\ifqr@tight\else\rule{4\qr@modulesize}{0pt}\fi% %Blank space at left.
912 \qr@for \j = 1 to \qr@numberofcolsinmatrix{#1} by 1%
913 {\qr@matrixentry{#1}{\the\i}{\the\j}}%
914 \par}%
915 \ifqr@tight\else\rule{0pt}{4\qr@modulesize}\par\fi%
916 \end{minipage}%
917 }%
918
919 \def\qr@printsavedbinarymatrix#1{%
920 \edef\qr@binarystring{#1\relax\relax}%
921 %Set module size
922 \setlength{\qr@modulesize}{\qr@desiredheight}%
923 \divide\qr@modulesize by \qr@size\relax%
924 %
925 \setlength{\qr@minipagewidth}{\qr@modulesize}%
926 \multiply\qr@minipagewidth by \qr@size\relax%
927 \ifqr@tight
928 \else
929 \advance\qr@minipagewidth by 8\qr@modulesize%
930 \fi
931 \begin{minipage}{\qr@minipagewidth}%
932 \baselineskip=\qr@modulesize%
933 \ifqr@tight\else\rule{0pt}{4\qr@modulesize}\par\fi% %Blank space at top.
934 \qr@for \i = 1 to \qr@size by 1%
935 {\ifqr@tight\else\rule{4\qr@modulesize}{0pt}\fi% %Blank space at left.
936 \qr@for \j = 1 to \qr@size by 1%
937 {\edef\qr@theargument{(\qr@binarystring)}%
938 \xa\qr@printsavedbinarymatrix@int\qr@theargument
939 }%
940 \par}%
941 \ifqr@tight\else\rule{0pt}{4\qr@modulesize}\par\fi%
942 \end{minipage}%
943 }%
944
945 \def\qr@printsavedbinarymatrix@int(#1#2){%
946 % #1 = first bit, either 1 or 0.
947 % #2 = remainder of string, terminating with \relax\relax
948 % There's no need to check for EOF here, because
949 % we'll only call this n^2 times.
950 \ifcase #1\relax
951 \rule{\qr@modulesize}{0pt}% % 0: white square
952 \or
953 \rule{\qr@modulesize}{\qr@modulesize}% % 1: black square
954 \fi
955 \xdef\qr@binarystring{#2}%
956 }%
957
958 \def\qr@createliteralmatrix#1#2#3{%
959 % #1 = matrix name
960 % #2 = m, the number of rows and columns in the square matrix
961 % #3 = a string of m^2 tokens to be written into the matrix
962 \qr@creatematrix{#1}%
963 \xa\xdef\csname #1@numrows\endcsname{#2}%
964 \xa\xdef\csname #1@numcols\endcsname{#2}%
965 \gdef\qr@literalmatrix@tokens{#3}%
966 \qr@for \i = 1 to #2 by 1%
967 {\qr@for \j = 1 to #2 by 1%
968 {\xa\qr@createliteralmatrix@int\xa(\qr@literalmatrix@tokens)%
969 \qr@estoretomatrix{#1}{\the\i}{\the\j}{\qr@entrytext}%
970 }%
971 }%
972 }
973 \def\qr@createliteralmatrix@int(#1#2){%
974 \def\qr@entrytext{#1}%
975 \gdef\qr@literalmatrix@tokens{#2}%
976 }
977
978
979 \qr@createliteralmatrix{finderpattern}{8}{%
980 \qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@white@fixed%
981 \qr@black@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@black@fixed\qr@white@fixed%
982 \qr@black@fixed\qr@white@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@white@fixed\qr@black@fixed\qr@white@fixed%
983 \qr@black@fixed\qr@white@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@white@fixed\qr@black@fixed\qr@white@fixed%
984 \qr@black@fixed\qr@white@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@white@fixed\qr@black@fixed\qr@white@fixed%
985 \qr@black@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@black@fixed\qr@white@fixed%
986 \qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@white@fixed%
987 \qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed%
988 }%
989
990 \qr@createliteralmatrix{alignmentpattern}{5}{%
991 \qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed%
992 \qr@black@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@black@fixed%
993 \qr@black@fixed\qr@white@fixed\qr@black@fixed\qr@white@fixed\qr@black@fixed%
994 \qr@black@fixed\qr@white@fixed\qr@white@fixed\qr@white@fixed\qr@black@fixed%
995 \qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed\qr@black@fixed%
996 }%
997
998
999
1000
1001 \def\qr@copymatrixentry#1#2#3#4#5#6{%
1002 % Copy the (#2,#3) entry of matrix #1
1003 % to the (#5,#6) position of matrix #4.
1004 \xa\xa\xa\global%
1005 \xa\xa\xa\let\xa\xa\csname #4@#5@#6\endcsname%
1006 \csname #1@#2@#3\endcsname%
1007 }%
1008
1009 \def\qr@createduplicatematrix#1#2{%
1010 % #1 = name of copy
1011 % #2 = original matrix to be copied
1012 \qr@creatematrix{#1}%
1013 \qr@for \i = 1 to \qr@numberofrowsinmatrix{#2} by 1%
1014 {\qr@for \j = 1 to \qr@numberofcolsinmatrix{#2} by 1%
1015 {\qr@copymatrixentry{#2}{\the\i}{\the\j}{#1}{\the\i}{\the\j}%
1016 }%
1017 }%
1018 \qr@setnumberofrows{#1}{\qr@numberofrowsinmatrix{#2}}%
1019 \qr@setnumberofcols{#1}{\qr@numberofcolsinmatrix{#2}}%
1020 }%
1021
1022 \def\qr@placefinderpattern@int#1#2#3#4#5{%
1023 % Work on matrix #1.
1024 % Start in position (#2, #3) -- should be a corner
1025 % #4 indicates horizontal direction (1=right, -1=left)
1026 % #5 indicates vertical direction (1=down, -1=up)
1027 %
1028 % In this code, \sourcei and \sourcej are TeX counts working through the finderpattern matrix,
1029 % and i and j are LaTeX counters indicating positions in the big matrix.
1030 \setcounter{qr@i}{#2}%
1031 \qr@for \sourcei=1 to 8 by 1%
1032 {\setcounter{qr@j}{#3}%
1033 \qr@for \sourcej=1 to 8 by 1%
1034 {\qr@copymatrixentry{finderpattern}{\the\sourcei}{\the\sourcej}%
1035 {#1}{\theqr@i}{\theqr@j}%
1036 \addtocounter{qr@j}{#5}%
1037 }%
1038 \addtocounter{qr@i}{#4}%
1039 }%
1040 }%
1041
1042 \def\qr@placefinderpatterns#1{%
1043 % #1=matrix name
1044 \qr@placefinderpattern@int{#1}{1}{1}{1}{1}%
1045 \qr@placefinderpattern@int{#1}{\qr@numberofrowsinmatrix{#1}}{1}{-1}{1}%
1046 \qr@placefinderpattern@int{#1}{1}{\qr@numberofcolsinmatrix{#1}}{1}{-1}%
1047 }%
1048
1049 \def\qr@placetimingpatterns#1{%
1050 %Set \qr@endingcol to n-8.
1051 \qr@a=\qr@size\relax%
1052 \advance\qr@a by -8\relax%
1053 \edef\qr@endingcol{\the\qr@a}%
1054 \qr@for \j = 9 to \qr@endingcol by 1%
1055 {\ifodd\j\relax%
1056 \qr@storetomatrix{#1}{7}{\the\j}{\qr@black@fixed}%
1057 \qr@storetomatrix{#1}{\the\j}{7}{\qr@black@fixed}%
1058 \else%
1059 \qr@storetomatrix{#1}{7}{\the\j}{\qr@white@fixed}%
1060 \qr@storetomatrix{#1}{\the\j}{7}{\qr@white@fixed}%
1061 \fi%
1062 }%
1063 }%
1064
1065 \def\qr@placealignmentpattern@int#1#2#3{%
1066 % Work on matrix #1.
1067 % Write an alignment pattern into the matrix, centered on (#2,#3).
1068 \qr@a=#2\relax%
1069 \advance\qr@a by -2\relax%
1070 \qr@b=#3\relax%
1071 \advance\qr@b by -2\relax%
1072 \setcounter{qr@i}{\the\qr@a}%
1073 \qr@for \i=1 to 5 by 1%
1074 {\setcounter{qr@j}{\the\qr@b}%
1075 \qr@for \j=1 to 5 by 1%
1076 {\qr@copymatrixentry{alignmentpattern}{\the\i}{\the\j}%
1077 {#1}{\theqr@i}{\theqr@j}%
1078 \stepcounter{qr@j}%
1079 }%
1080 \stepcounter{qr@i}%
1081 }%
1082 }%
1083
1084 \newif\ifqr@incorner%
1085 \def\qr@placealignmentpatterns#1{%
1086 %There are k^2-3 alignment patterns,
1087 %arranged in a (k x k) grid within the matrix.
1088 %They begin in row 7, column 7,
1089 %except that the ones in the NW, NE, and SW corners
1090 %are omitted because of the finder patterns.
1091 %Recall that
1092 % * \qr@k stores k,
1093 % * \qr@alignment@firstskip stores how far between the 1st and 2nd row/col, &
1094 % * \qr@alignment@generalskip stores how far between each subsequent row/col.
1095 \xa\ifnum\qr@k>0\relax
1096 %There will be at least one alignment pattern.
1097 %N.B. k cannot equal 1.
1098 \xa\ifnum\qr@k=2\relax
1099 % 2*2-3 = exactly 1 alignment pattern.
1100 \qr@a=7\relax
1101 \advance\qr@a by \qr@alignment@firstskip\relax
1102 \xdef\qr@target@ii{\the\qr@a}%
1103 \qr@placealignmentpattern@int{#1}{\qr@target@ii}{\qr@target@ii}%
1104 \else
1105 % k is at least 3, so the following loops should be safe.
1106 \xdef\qr@target@ii{7}%
1107 \qr@for \ii = 1 to \qr@k by 1%
1108 {\ifcase\ii\relax%
1109 \relax% \ii should never equal 0.
1110 \or
1111 \xdef\qr@target@ii{7}% If \ii = 1, we start in row 7.
1112 \or
1113 %If \ii = 2, we add the firstskip.
1114 \qr@a=\qr@target@ii\relax%
1115 \advance\qr@a by \qr@alignment@firstskip\relax%
1116 \xdef\qr@target@ii{\the\qr@a}%
1117 \else
1118 %If \ii>2, we add the generalskip.
1119 \qr@a=\qr@target@ii\relax%
1120 \advance\qr@a by \qr@alignment@generalskip\relax%
1121 \xdef\qr@target@ii{\the\qr@a}%
1122 \fi
1123 \qr@for \jj = 1 to \qr@k by 1%
1124 {\ifcase\jj\relax%
1125 \relax% \jj should never equal 0.
1126 \or
1127 \xdef\qr@target@jj{7}% If \jj=1, we start in row 7.
1128 \or
1129 %If \jj=2, we add the firstskip.
1130 \qr@a=\qr@target@jj\relax%
1131 \advance\qr@a by \qr@alignment@firstskip%
1132 \xdef\qr@target@jj{\the\qr@a}%
1133 \else
1134 %If \jj>2, we add the generalskip.
1135 \qr@a=\qr@target@jj\relax%
1136 \advance\qr@a by \qr@alignment@generalskip%
1137 \xdef\qr@target@jj{\the\qr@a}%
1138 \fi
1139 \qr@incornerfalse%
1140 \ifnum\ii=1\relax
1141 \ifnum\jj=1\relax
1142 \qr@incornertrue
1143 \else
1144 \ifnum\qr@k=\jj\relax
1145 \qr@incornertrue
1146 \fi
1147 \fi
1148 \else
1149 \xa\ifnum\qr@k=\ii\relax
1150 \ifnum\jj=1\relax
1151 \qr@incornertrue
1152 \fi
1153 \fi
1154 \fi
1155 \ifqr@incorner
1156 \relax
1157 \else
1158 \qr@placealignmentpattern@int{#1}{\qr@target@ii}{\qr@target@jj}%
1159 \fi
1160 }% ends \qr@for \jj
1161 }% ends \qr@for \ii
1162 \fi
1163 \fi
1164 }%
1165
1166 \def\qr@placedummyformatpatterns#1{%
1167 \qr@for \j = 1 to 9 by 1%
1168 {\ifnum\j=7\relax%
1169 \else%
1170 \qr@storetomatrix{#1}{9}{\the\j}{\qr@format@square}%
1171 \qr@storetomatrix{#1}{\the\j}{9}{\qr@format@square}%
1172 \fi%
1173 }%
1174 \setcounter{qr@j}{\qr@size}%
1175 \qr@for \j = 1 to 8 by 1%
1176 {\qr@storetomatrix{#1}{9}{\theqr@j}{\qr@format@square}%
1177 \qr@storetomatrix{#1}{\theqr@j}{9}{\qr@format@square}%
1178 \addtocounter{qr@j}{-1}%
1179 }%
1180 %Now go back and change the \qr@format@square in (n-8,9) to \qr@black@fixed.
1181 \addtocounter{qr@j}{1}%
1182 \qr@storetomatrix{#1}{\theqr@j}{9}{\qr@black@fixed}%
1183 }%
1184
1185 \def\qr@placedummyversionpatterns#1{%
1186 \xa\ifnum\qr@version>6\relax
1187 %Must include version information.
1188 \global\c@qr@i=\qr@size%
1189 \global\advance\c@qr@i by -10\relax%
1190 \qr@for \i = 1 to 3 by 1%
1191 {\qr@for \j = 1 to 6 by 1%
1192 {\qr@storetomatrix{#1}{\theqr@i}{\the\j}{\qr@format@square}%
1193 \qr@storetomatrix{#1}{\the\j}{\theqr@i}{\qr@format@square}%
1194 }%
1195 \stepcounter{qr@i}%
1196 }%
1197 \fi
1198 }%
1199
1200 \def\qr@writebit(#1#2)#3{%
1201 % #3 = matrix name
1202 % (qr@i,qr@j) = position to write in (LaTeX counters)
1203 % #1 = bit to be written
1204 % #2 = remaining bits plus '\relax' as an end-of-file marker
1205 \edef\qr@datatowrite{#2}%
1206 \ifnum#1=1
1207 \qr@storetomatrix{#3}{\theqr@i}{\theqr@j}{\qr@black}%
1208 \else
1209 \qr@storetomatrix{#3}{\theqr@i}{\theqr@j}{\qr@white}%
1210 \fi
1211 }%
1212
1213 \newif\ifqr@rightcol
1214 \newif\ifqr@goingup
1215
1216 \def\qr@writedata@hex#1#2{%
1217 % #1 = name of a matrix that has been prepared with finder patterns, timing patterns, etc.
1218 % #2 = a string consisting of bytes to write into the matrix, in two-char hex format.
1219 \setcounter{qr@i}{\qr@numberofrowsinmatrix{#1}}%
1220 \setcounter{qr@j}{\qr@numberofcolsinmatrix{#1}}%
1221 \qr@rightcoltrue%
1222 \qr@goinguptrue%
1223 \edef\qr@argument{{#1}(#2\relax\relax\relax)}%
1224 \xa\qr@writedata@hex@recursive\qr@argument%
1225 }%
1226
1227 \def\qr@writedata@hex@recursive#1(#2#3#4){%
1228 % #1 = name of a matrix that has been prepared with finder patterns, timing patterns, etc.
1229 % (qr@i,qr@j) = position to write in LaTeX counters
1230 % #2#3#4 contains the hex codes of the bytes to be written, plus \relax\relax\relax
1231 % as an end-of-file marker
1232 \edef\qr@testii{#2}%
1233 \ifx\qr@testii\qr@relax%
1234 % #2 is \relax, so there is nothing more to write.
1235 \relax
1236 \let\qr@next=\relax
1237 \else
1238 % #2 is not \relax, so there is another byte to write.
1239 \qr@hextobinary[8]{\bytetowrite}{#2#3}%
1240 \xdef\qr@datatowrite{\bytetowrite\relax}% %Add terminating "\relax"
1241 \qr@writedata@recursive{#1}% %This function actually writes the 8 bits.
1242 \edef\qr@argument{{#1}(#4)}%
1243 \xa\def\xa\qr@next\xa{\xa\qr@writedata@hex@recursive\qr@argument}% %Call self to write the next bit.
1244 \fi
1245 \qr@next
1246 }%
1247
1248 \def\qr@writedata#1#2{%
1249 % #1 = name of a matrix that has been prepared with finder patterns, timing patterns, etc.
1250 % #2 = a string consisting of 0's and 1's to write into the matrix.
1251 \setcounter{qr@i}{\qr@numberofrowsinmatrix{#1}}%
1252 \setcounter{qr@j}{\qr@numberofcolsinmatrix{#1}}%
1253 \qr@rightcoltrue
1254 \qr@goinguptrue
1255 \edef\qr@datatowrite{#2\relax}%
1256 \qr@writedata@recursive{#1}%
1257 }%
1258
1259 \def\qr@@blank{\qr@blank}%
1260
1261 \def\qr@writedata@recursive#1{%
1262 % #1 = matrix name
1263 % (qr@i,qr@j) = position to write in (LaTeX counters)
1264 % \qr@datatowrite contains the bits to be written, plus '\relax' as an end-of-file marker
1265 \xa\let\xa\squarevalue\csname #1@\theqr@i @\theqr@j\endcsname%
1266 \ifx\squarevalue\qr@@blank
1267 %Square is blank, so write data in it.
1268 \xa\qr@writebit\xa(\qr@datatowrite){#1}%
1269 %The \qr@writebit macro not only writes the first bit of \qr@datatowrite into the matrix,
1270 %but also removes the bit from the 'bitstream' of \qr@datatowrite.
1271 \fi
1272 %Now adjust our position in the matrix.
1273 \ifqr@rightcol
1274 %From the right-hand half of the two-bit column, we always move left. Easy peasy.
1275 \addtocounter{qr@j}{-1}%
1276 \qr@rightcolfalse
1277 \else
1278 %If we're in the left-hand column, things are harder.
1279 \ifqr@goingup
1280 %First, suppose we're going upwards.
1281 \ifnum\c@qr@i>1\relax%
1282 %If we're not in the first row, things are easy.
1283 %We move one to the right and one up.
1284 \addtocounter{qr@j}{1}%
1285 \addtocounter{qr@i}{-1}%
1286 \qr@rightcoltrue
1287 \else
1288 %If we are in the first row, then we move to the left,
1289 %and we are now in the right-hand column on a downward pass.
1290 \addtocounter{qr@j}{-1}%
1291 \qr@goingupfalse
1292 \qr@rightcoltrue
1293 \fi
1294 \else
1295 %Now, suppose we're going downwards.
1296 \xa\ifnum\qr@size>\c@qr@i\relax%
1297 %If we're not yet in the bottom row, things are easy.
1298 %We move one to the right and one down.
1299 \addtocounter{qr@j}{1}%
1300 \addtocounter{qr@i}{1}%
1301 \qr@rightcoltrue
1302 \else
1303 %If we are in the bottom row, then we move to the left,
1304 %and we are now in the right-hand column on an upward pass.
1305 \addtocounter{qr@j}{-1}%
1306 \qr@rightcoltrue
1307 \qr@goinguptrue
1308 \fi
1309 \fi
1310 %One problem: what if we just moved into the 7th column?
1311 %Das ist verboten.
1312 %If we just moved (left) into the 7th column, we should move on into the 6th column.
1313 \ifnum\c@qr@j=7\relax%
1314 \setcounter{qr@j}{6}%
1315 \fi
1316 \fi
1317 %Now check whether there are any more bits to write.
1318 \ifx\qr@datatowrite\qr@relax
1319 % \qr@datatowrite is just `\relax', so we're done.
1320 \let\qr@next=\relax
1321 \relax
1322 \else
1323 % Write some more!
1324 \def\qr@next{\qr@writedata@recursive{#1}}%
1325 \fi
1326 \qr@next
1327 }%
1328
1329 \def\qr@writeremainderbits#1{%
1330 % #1 = name of a matrix that has been prepared and partly filled.
1331 % (qr@i,qr@j) = position to write in LaTeX counters
1332 \xa\ifnum\qr@numremainderbits>0\relax
1333 \def\qr@datatowrite{}%
1334 \qr@for \i = 1 to \qr@numremainderbits by 1%
1335 {\g@addto@macro{\qr@datatowrite}{0}}%
1336 \g@addto@macro{\qr@datatowrite}{\relax}% terminator
1337 \qr@writedata@recursive{#1}%
1338 \fi
1339 }%
1340
1341 \newif\ifqr@cellinmask
1342
1343 \def\qr@setmaskingfunction#1{%
1344 % #1 = 1 decimal digit for the mask. (I see no reason to use the 3-bit binary code.)
1345 % The current position is (\themaski,\themaskj), with indexing starting at 0.
1346 \edef\qr@maskselection{#1}%
1347 \xa\ifcase\qr@maskselection\relax
1348 %Case 0: checkerboard
1349 \def\qr@parsemaskingfunction{%
1350 % Compute mod(\themaski+\themaskj,2)%
1351 \qr@a=\c@maski%
1352 \advance\qr@a by \c@maskj%
1353 \qr@b=\qr@a%
1354 \divide\qr@b by 2%
1355 \multiply\qr@b by 2%
1356 \advance\qr@a by -\qr@b%
1357 \edef\qr@maskfunctionresult{\the\qr@a}%
1358 }%
1359 \or
1360 %Case 1: horizontal stripes
1361 \def\qr@parsemaskingfunction{%
1362 % Compute mod(\themaski,2)%
1363 \ifodd\c@maski\relax%
1364 \def\qr@maskfunctionresult{1}%
1365 \else%
1366 \def\qr@maskfunctionresult{0}%
1367 \fi%
1368 }%
1369 \or
1370 %Case 2: vertical stripes
1371 \def\qr@parsemaskingfunction{%
1372 % Compute mod(\themaskj,3)%
1373 \qr@a=\c@maskj%
1374 \divide\qr@a by 3%
1375 \multiply\qr@a by 3%
1376 \advance\qr@a by -\c@maskj%
1377 \edef\qr@maskfunctionresult{\the\qr@a}%
1378 }%
1379 \or
1380 %Case 3: diagonal stripes
1381 \def\qr@parsemaskingfunction{%
1382 % Compute mod(\themaski+\themaskj,3)%
1383 \qr@a=\c@maski%
1384 \advance\qr@a by \c@maskj%
1385 \qr@b=\qr@a%
1386 \divide\qr@b by 3%
1387 \multiply\qr@b by 3%
1388 \advance\qr@b by -\qr@a%
1389 \edef\qr@maskfunctionresult{\the\qr@b}%
1390 }%
1391 \or
1392 %Case 4: wide checkerboard
1393 \def\qr@parsemaskingfunction{%
1394 % Compute mod(floor(\themaski/2) + floor(\themaskj/3),2) %
1395 \qr@a=\c@maski%
1396 \divide\qr@a by 2%
1397 \qr@b=\c@maskj%
1398 \divide\qr@b by 3%
1399 \advance\qr@a by \qr@b%
1400 \qr@b=\qr@a%
1401 \divide\qr@a by 2%
1402 \multiply\qr@a by 2%
1403 \advance\qr@a by -\qr@b%
1404 \edef\qr@maskfunctionresult{\the\qr@a}%
1405 }%
1406 \or
1407 %Case 5: quilt
1408 \def\qr@parsemaskingfunction{%
1409 % Compute mod(\themaski*\themaskj,2) + mod(\themaski*\themaskj,3) %
1410 \qr@a=\c@maski%
1411 \multiply\qr@a by \c@maskj%
1412 \qr@b=\qr@a%
1413 \qr@c=\qr@a%
1414 \divide\qr@a by 2%
1415 \multiply\qr@a by 2%
1416 \advance\qr@a by -\qr@c% (result will be -mod(i*j,2), which is negative.)
1417 \divide\qr@b by 3%
1418 \multiply\qr@b by 3%
1419 \advance\qr@b by -\qr@c% (result will be -mod(i*j,3), which is negative.)
1420 \advance\qr@a by \qr@b% (result is negative of what's in the spec.)
1421 \edef\qr@maskfunctionresult{\the\qr@a}%
1422 }%
1423 \or
1424 %Case 6: arrows
1425 \def\qr@parsemaskingfunction{%
1426 % Compute mod( mod(\themaski*\themaskj,2) + mod(\themaski*\themaskj,3) , 2 ) %
1427 \qr@a=\c@maski%
1428 \multiply\qr@a by \c@maskj%
1429 \qr@b=\qr@a%
1430 \qr@c=\qr@a%
1431 \multiply\qr@c by 2% % \qr@c equals 2*i*j.
1432 \divide\qr@a by 2%
1433 \multiply\qr@a by 2%
1434 \advance\qr@c by -\qr@a% Now \qr@c equals i*j + mod(i*j,2).
1435 \divide\qr@b by 3%
1436 \multiply\qr@b by 3%
1437 \advance\qr@c by -\qr@b% (Now \qr@c equals mod(i*j,2) + mod(i*j,3).
1438 \qr@a=\qr@c%
1439 \divide\qr@a by 2%
1440 \multiply\qr@a by 2%
1441 \advance\qr@c by-\qr@a%
1442 \edef\qr@maskfunctionresult{\the\qr@c}%
1443 }%
1444 \or
1445 %Case 7: shotgun
1446 \def\qr@parsemaskingfunction{%
1447 % Compute mod( mod(\themaski+\themaskj,2) + mod(\themaski*\themaskj,3) , 2 ) %
1448 \qr@a=\c@maski%
1449 \advance\qr@a by \c@maskj% %So \qr@a = i+j
1450 \qr@b=\c@maski%
1451 \multiply\qr@b by \c@maskj% %So \qr@b = i*j
1452 \qr@c=\qr@a%
1453 \advance\qr@c by \qr@b% So \qr@c = i+j+i*j
1454 \divide\qr@a by 2%
1455 \multiply\qr@a by 2%
1456 \advance\qr@c by -\qr@a% So \qr@c = mod(i+j,2) + i*j
1457 \divide\qr@b by 3%
1458 \multiply\qr@b by 3%
1459 \advance\qr@c by -\qr@b% So \qr@c = mod(i+j,2) + mod(i*j,3)
1460 \qr@a=\qr@c%
1461 \divide\qr@c by 2%
1462 \multiply\qr@c by 2%
1463 \advance\qr@a by -\qr@c%
1464 \edef\qr@maskfunctionresult{\the\qr@a}%
1465 }%
1466 \fi
1467 }%
1468
1469
1470 \def\qr@checkifcellisinmask{%
1471 % The current position is (\i,\j), in TeX counts,
1472 % but the LaTeX counters (maski,maskj) should contain
1473 % the current position with indexing starting at 0.
1474 % That is, maski = \i-1 and maskj = \j-1.
1475 %
1476 % \qr@parsemaskingfunction must have been set by a call to \qr@setmaskingfunction
1477 \qr@parsemaskingfunction
1478 \xa\ifnum\qr@maskfunctionresult=0\relax
1479 \qr@cellinmasktrue
1480 \else
1481 \qr@cellinmaskfalse
1482 \fi
1483 }%
1484
1485 \newcounter{maski}%
1486 \newcounter{maskj}%
1487
1488 \def\qr@applymask#1#2#3{%
1489 % #1 = name of a matrix that should be filled out completely
1490 % except for the format and/or version information.
1491 % #2 = name of a new matrix to contain the masked version
1492 % #3 = 1 decimal digit naming the mask
1493 \qr@createduplicatematrix{#2}{#1}%
1494 \qr@setmaskingfunction{#3}%
1495 \setcounter{maski}{-1}%
1496 \qr@for \i = 1 to \qr@size by 1%
1497 {\stepcounter{maski}%
1498 \setcounter{maskj}{-1}%
1499 \qr@for \j = 1 to \qr@size by 1%
1500 {\stepcounter{maskj}%
1501 \qr@checkifcellisinmask
1502 \ifqr@cellinmask
1503 \qr@checkifcurrentcellcontainsdata{#2}%
1504 \ifqr@currentcellcontainsdata
1505 \qr@flipcurrentcell{#2}%
1506 \fi
1507 \fi
1508 }%
1509 }%
1510 }%
1511
1512 \newif\ifqr@currentcellcontainsdata
1513 \qr@currentcellcontainsdatafalse
1514
1515 \def\qr@@white{\qr@white}%
1516 \def\qr@@black{\qr@black}%
1517
1518 \def\qr@checkifcurrentcellcontainsdata#1{%
1519 % #1 = name of matrix
1520 \qr@currentcellcontainsdatafalse
1521 \xa\ifx\csname #1@\the\i @\the\j\endcsname\qr@@white
1522 \qr@currentcellcontainsdatatrue
1523 \fi
1524 \xa\ifx\csname #1@\the\i @\the\j\endcsname\qr@@black
1525 \qr@currentcellcontainsdatatrue
1526 \fi
1527 }%
1528
1529 \def\qr@flipped@black{\qr@black}%
1530 \def\qr@flipped@white{\qr@white}%
1531
1532 \def\qr@flipcurrentcell#1{%
1533 % #1 = name of matrix
1534 % (\i, \j) = current position, in TeX counts.
1535 % This assumes the cell contains data, either black or white!
1536 \xa\ifx\csname #1@\the\i @\the\j\endcsname\qr@@white
1537 \qr@storetomatrix{#1}{\the\i}{\the\j}{\qr@flipped@black}%
1538 \else
1539 \qr@storetomatrix{#1}{\the\i}{\the\j}{\qr@flipped@white}%
1540 \fi
1541 }%
1542
1543 \def\qr@chooseandapplybestmask#1{%
1544 % #1 = name of a matrix that should be filled out completely
1545 % except for the format and/or version information.
1546 % This function applies all eight masks in succession,
1547 % calculates their penalties, and remembers the best.
1548 % The number indicating which mask was used is saved in \qr@mask@selected.
1549 \qr@createduplicatematrix{originalmatrix}{#1}%
1550 \message{<Applying Mask 0...}%
1551 \qr@applymask{originalmatrix}{#1}{0}%
1552 \message{done. Calculating penalty...}%
1553 \qr@evaluatemaskpenalty{#1}%
1554 \xdef\qr@currentbestpenalty{\qr@penalty}%
1555 \message{penalty is \qr@penalty>^^J}%
1556 \gdef\qr@currentbestmask{0}%
1557 \qr@for \i = 1 to 7 by 1%
1558 {\message{<Applying Mask \the\i...}%
1559 \qr@applymask{originalmatrix}{currentmasked}{\the\i}%
1560 \message{done. Calculating penalty...}%
1561 \qr@evaluatemaskpenalty{currentmasked}%
1562 \message{penalty is \qr@penalty>^^J}%
1563 \xa\xa\xa\ifnum\xa\qr@penalty\xa<\qr@currentbestpenalty\relax
1564 %We found a better mask.
1565 \xdef\qr@currentbestmask{\the\i}%
1566 \qr@createduplicatematrix{#1}{currentmasked}%
1567 \xdef\qr@currentbestpenalty{\qr@penalty}%
1568 \fi
1569 }%
1570 \xdef\qr@mask@selected{\qr@currentbestmask}%
1571 \message{<Selected Mask \qr@mask@selected>^^J}%
1572 }%
1573
1574
1575 \def\qr@Ni{3}%
1576 \def\qr@Nii{3}%
1577 \def\qr@Niii{40}%
1578 \def\qr@Niv{10}%
1579 \def\qr@fiveones{11111}%
1580 \def\qr@fivezeros{11111}%
1581 \def\qr@twoones{11}%
1582 \def\qr@twozeros{00}%
1583 \def\qr@finderA{00001011101}%
1584 \def\qr@finderB{10111010000}%
1585 \def\qr@finderB@three{1011101000}%
1586 \def\qr@finderB@two{101110100}%
1587 \def\qr@finderB@one{10111010}%
1588 \def\qr@finderB@zero{1011101}%
1589 \newif\ifqr@stringoffive
1590 \def\qr@addpenaltyiii{%
1591 \addtocounter{penaltyiii}{\qr@Niii}%
1592 }%
1593 \newcounter{totalones}%
1594 \newcounter{penaltyi}%
1595 \newcounter{penaltyii}%
1596 \newcounter{penaltyiii}%
1597 \newcounter{penaltyiv}%
1598 \def\qr@evaluatemaskpenalty#1{%
1599 % #1 = name of a matrix that we will test for the penalty
1600 % according to the specs.
1601 \setcounter{penaltyi}{0}%
1602 \setcounter{penaltyii}{0}%
1603 \setcounter{penaltyiii}{0}%
1604 \setcounter{penaltyiv}{0}%
1605 \bgroup%localize the meanings we give to the symbols
1606 \def\qr@black{1}\def\qr@white{0}%
1607 \def\qr@black@fixed{1}\def\qr@white@fixed{0}%
1608 \def\qr@format@square{0}% This is not stated in the specs, but seems
1609 % to be the standard implementation.
1610 \def\qr@blank{0}% These would be any bits at the end.
1611 %
1612 \setcounter{totalones}{0}%
1613 \qr@for \i=1 to \qr@size by 1%
1614 {\def\qr@lastfive{z}% %The z is a dummy, that will be removed before any testing.
1615 \qr@stringoffivefalse
1616 \def\qr@lasttwo@thisrow{z}% %The z is a dummy.
1617 \def\qr@lasttwo@nextrow{z}% %The z is a dummy.
1618 \def\qr@lastnine{z0000}% %The 0000 stands for the white space to the left. The z is a dummy.
1619 \def\qr@ignore@finderB@at{0}%
1620 \qr@for \j=1 to \qr@size by 1%
1621 {\edef\qr@newbit{\qr@matrixentry{#1}{\the\i}{\the\j}}%
1622 %
1623 % LASTFIVE CODE FOR PENALTY 1
1624 % First, add the new bit to the end.
1625 \xa\g@addto@macro\xa\qr@lastfive\xa{\qr@newbit}%
1626 \ifnum\j<5\relax%
1627 %Not yet on the 5th entry.
1628 %Don't do any testing.
1629 \else
1630 % 5th entry or later.
1631 % Remove the old one, and then test.
1632 \qr@removefirsttoken\qr@lastfive%
1633 \ifx\qr@lastfive\qr@fiveones%
1634 \ifqr@stringoffive%
1635 %This is a continuation of a previous block of five or more 1's.
1636 \stepcounter{penaltyi}%
1637 \else
1638 %This is a new string of five 1's.
1639 \addtocounter{penaltyi}{\qr@Ni}%
1640 \global\qr@stringoffivetrue
1641 \fi
1642 \else
1643 \ifx\qr@lastfive\qr@fivezeros%
1644 \ifqr@stringoffive
1645 %This is a continuation of a previous block of five or more 0's.
1646 \stepcounter{penaltyi}%
1647 \else
1648 %This is a new string of five 0's.
1649 \addtocounter{penaltyi}{\qr@Ni}%
1650 \global\qr@stringoffivetrue
1651 \fi
1652 \else
1653 %This is not a string of five 1's or five 0's.
1654 \global\qr@stringoffivefalse
1655 \fi
1656 \fi
1657 \fi
1658 %
1659 % 2x2 BLOCKS FOR PENALTY 2
1660 % Every 2x2 block of all 1's counts for \qr@Nii penalty points.
1661 % We do not need to run this test in the last row.
1662 \xa\ifnum\xa\i\xa<\qr@size\relax
1663 \xa\g@addto@macro\xa\qr@lasttwo@thisrow\xa{\qr@newbit}%
1664 %Compute \qr@iplusone
1665 \qr@a=\i\relax%
1666 \advance\qr@a by 1%
1667 \edef\qr@iplusone{\the\qr@a}%
1668 %
1669 \edef\qr@nextrowbit{\qr@matrixentry{#1}{\qr@iplusone}{\the\j}}%
1670 \xa\g@addto@macro\xa\qr@lasttwo@nextrow\xa{\qr@nextrowbit}%
1671 \ifnum\j<2\relax%
1672 %Still in the first column; no check.
1673 \else
1674 %Second column or later. Remove the old bits, and then test.
1675 \qr@removefirsttoken\qr@lasttwo@thisrow
1676 \qr@removefirsttoken\qr@lasttwo@nextrow
1677 \ifx\qr@lasttwo@thisrow\qr@twoones
1678 \ifx\qr@lasttwo@nextrow\qr@twoones
1679 \addtocounter{penaltyii}{\qr@Nii}%
1680 \fi
1681 \else
1682 \ifx\qr@lasttwo@thisrow\qr@twozeros
1683 \ifx\qr@lasttwo@nextrow\qr@twozeros
1684 \addtocounter{penaltyii}{\qr@Nii}%
1685 \fi
1686 \fi
1687 \fi
1688 \fi
1689 \fi
1690 %
1691 % LASTNINE CODE FOR PENALTY 3
1692 % First, add the new bit to the end.
1693 \xa\g@addto@macro\xa\qr@lastnine\xa{\qr@newbit}%
1694 \ifnum\j<7\relax%
1695 %Not yet on the 7th entry.
1696 %Don't do any testing.
1697 \else
1698 % 7th entry or later.
1699 % Remove the old one, and then test.
1700 \qr@removefirsttoken\qr@lastnine
1701 \xa\ifnum\qr@size=\j\relax%
1702 % Last column. Any of the following should count:
1703 % 1011101 (\qr@finderB@zero)
1704 % 10111010 (\qr@finderB@one)
1705 % 101110100 (\qr@finderB@two)
1706 % 1011101000 (\qr@finderB@three)
1707 % 10111010000 (\qr@finderB)
1708 \ifx\qr@lastnine\qr@finderB
1709 \qr@addpenaltyiii
1710 \else
1711 \qr@removefirsttoken\qr@lastnine
1712 \ifx\qr@lastnine\qr@finderB@three
1713 \qr@addpenaltyiii
1714 \else
1715 \qr@removefirsttoken\qr@lastnine
1716 \ifx\qr@lastnine\qr@finderB@two
1717 \qr@addpenaltyiii
1718 \else
1719 \qr@removefirsttoken\qr@lastnine
1720 \ifx\qr@lastnine\qr@finderB@one
1721 \qr@addpenaltyiii
1722 \else
1723 \qr@removefirsttoken\qr@lastnine
1724 \ifx\qr@lastnine\qr@finderB@zero
1725 \qr@addpenaltyiii
1726 \fi
1727 \fi
1728 \fi
1729 \fi
1730 \fi
1731 \else
1732 \ifx\qr@lastnine\qr@finderA% %Matches 0000 1011101
1733 \qr@addpenaltyiii
1734 %Also, we record our discovery, so that we can't count this pattern again
1735 %if it shows up four columns later as 1011101 0000.
1736 %
1737 %Set \qr@ignore@finderB@at to \j+4.
1738 \qr@a=\j\relax%
1739 \advance\qr@a by 4%
1740 \xdef\qr@ignore@finderB@at{\the\qr@a}%
1741 \else
1742 \ifx\qr@lastfive\qr@finderB% %Matches 1011101 0000.
1743 \xa\ifnum\qr@ignore@finderB@at=\j\relax
1744 %This pattern was *not* counted already earlier.
1745 \qr@addpenaltyiii
1746 \fi
1747 \fi
1748 \fi
1749 \fi
1750 \fi
1751 %
1752 %COUNT 1's FOR PENALTY 4
1753 \xa\ifnum\qr@newbit=1\relax%
1754 \stepcounter{totalones}%
1755 \fi
1756 }% end of j-loop
1757 }% end of i-loop
1758 %
1759 %NOW WE ALSO NEED TO RUN DOWN THE COLUMNS TO FINISH CALCULATING PENALTIES 1 AND 3.
1760 \qr@for \j=1 to \qr@size by 1%
1761 {\def\qr@lastfive{z}% %The z is a dummy, that will be removed before any testing.
1762 \qr@stringoffivefalse
1763 \def\qr@lastnine{z0000}% %The 0000 stands for the white space to the left. The z is a dummy.
1764 \def\qr@ignore@finderB@at{0}%
1765 \qr@for \i=1 to \qr@size by 1%
1766 {\edef\qr@newbit{\qr@matrixentry{#1}{\the\i}{\the\j}}%
1767 %
1768 % LASTFIVE CODE FOR PENALTY 1
1769 % First, add the new bit to the end.
1770 \xa\g@addto@macro\xa\qr@lastfive\xa{\qr@newbit}%
1771 \ifnum\i<5\relax%
1772 %Not yet on the 5th entry.
1773 %Don't do any testing.
1774 \else
1775 % 5th entry or later.
1776 % Remove the old one, and then test.
1777 \qr@removefirsttoken\qr@lastfive%
1778 \ifx\qr@lastfive\qr@fiveones%
1779 \ifqr@stringoffive%
1780 %This is a continuation of a previous block of five or more 1's.
1781 \stepcounter{penaltyi}%
1782 \else
1783 %This is a new string of five 1's.
1784 \addtocounter{penaltyi}{\qr@Ni}%
1785 \global\qr@stringoffivetrue
1786 \fi
1787 \else
1788 \ifx\qr@lastfive\qr@fivezeros%
1789 \ifqr@stringoffive
1790 %This is a continuation of a previous block of five or more 0's.
1791 \stepcounter{penaltyi}%
1792 \else
1793 %This is a new string of five 0's.
1794 \addtocounter{penaltyi}{\qr@Ni}%
1795 \global\qr@stringoffivetrue
1796 \fi
1797 \else
1798 %This is not a string of five 1's or five 0's.
1799 \global\qr@stringoffivefalse
1800 \fi
1801 \fi
1802 \fi
1803 %
1804 % HAPPILY, WE DON'T NEED TO CALCULATE PENALTY 2 AGAIN.
1805 %
1806 % LASTNINE CODE FOR PENALTY 3
1807 % First, add the new bit to the end.
1808 \xa\g@addto@macro\xa\qr@lastnine\xa{\qr@newbit}%
1809 \ifnum\i<7\relax%
1810 %Not yet on the 7th entry.
1811 %Don't do any testing.
1812 \else
1813 % 7th entry or later.
1814 % Remove the old one, and then test.
1815 \qr@removefirsttoken\qr@lastnine
1816 \xa\ifnum\qr@size=\i\relax%
1817 % Last column. Any of the following should count:
1818 % 1011101 (\qr@finderB@zero)
1819 % 10111010 (\qr@finderB@one)
1820 % 101110100 (\qr@finderB@two)
1821 % 1011101000 (\qr@finderB@three)
1822 % 10111010000 (\qr@finderB)
1823 \ifx\qr@lastnine\qr@finderB
1824 \qr@addpenaltyiii
1825 \else
1826 \qr@removefirsttoken\qr@lastnine
1827 \ifx\qr@lastnine\qr@finderB@three
1828 \qr@addpenaltyiii
1829 \else
1830 \qr@removefirsttoken\qr@lastnine
1831 \ifx\qr@lastnine\qr@finderB@two
1832 \qr@addpenaltyiii
1833 \else
1834 \qr@removefirsttoken\qr@lastnine
1835 \ifx\qr@lastnine\qr@finderB@one
1836 \qr@addpenaltyiii
1837 \else
1838 \qr@removefirsttoken\qr@lastnine
1839 \ifx\qr@lastnine\qr@finderB@zero
1840 \qr@addpenaltyiii
1841 \fi
1842 \fi
1843 \fi
1844 \fi
1845 \fi
1846 \else
1847 \ifx\qr@lastnine\qr@finderA% %Matches 0000 1011101
1848 \qr@addpenaltyiii
1849 %Also, we record our discovery, so that we can't count this pattern again
1850 %if it shows up four columns later as 1011101 0000.
1851 %
1852 %Set \qr@ignore@finderB@at to \i+4.
1853 \qr@a=\i\relax%
1854 \advance\qr@a by 4%
1855 \xdef\qr@ignore@finderB@at{\the\qr@a}%
1856 \else
1857 \ifx\qr@lastfive\qr@finderB% %Matches 1011101 0000.
1858 \xa\ifnum\qr@ignore@finderB@at=\i\relax
1859 %This pattern was *not* counted already earlier.
1860 \qr@addpenaltyiii
1861 \fi
1862 \fi
1863 \fi
1864 \fi
1865 \fi
1866 %
1867 }% end of i-loop
1868 }% end of j-loop
1869 \egroup%
1870 %
1871 %CALCULATE PENALTY 4
1872 %According to the spec, penalty #4 is computed as
1873 % floor( |(i/n^2)-0.5|/0.05 )
1874 % where i is the total number of 1's in the matrix.
1875 % This is equal to abs(20*i-10n^2) div n^2.
1876 %
1877 \qr@a=\c@totalones\relax
1878 \multiply\qr@a by 20\relax
1879 \qr@b=\qr@size\relax
1880 \multiply\qr@b by \qr@size\relax
1881 \qr@c=10\relax
1882 \multiply\qr@c by \qr@b\relax
1883 \advance\qr@a by -\qr@c\relax
1884 \ifnum\qr@a<0\relax
1885 \multiply\qr@a by -1\relax
1886 \fi
1887 \divide\qr@a by \qr@b\relax
1888 \setcounter{penaltyiv}{\the\qr@a}%
1889 %
1890 %CALCULATE TOTAL PENALTY
1891 \qr@a=\thepenaltyi\relax%
1892 \advance\qr@a by \thepenaltyii\relax%
1893 \advance\qr@a by \thepenaltyiii\relax%
1894 \advance\qr@a by \thepenaltyiv\relax%
1895 \edef\qr@penalty{\the\qr@a}%
1896 }%
1897
1898 \def\qr@removefirsttoken#1{%
1899 %Removes the first token from the macro named in #1.
1900 \edef\qr@argument{(#1)}%
1901 \xa\qr@removefirsttoken@int\qr@argument%
1902 \xdef#1{\qr@removefirsttoken@result}%
1903 }%
1904 \def\qr@removefirsttoken@int(#1#2){%
1905 \def\qr@removefirsttoken@result{#2}%
1906 }%
1907
1908 \def\qr@writeformatstring#1#2{%
1909 % #1 = matrix name
1910 % #2 = binary string representing the encoded and masked format information
1911 \setcounter{qr@i}{9}%
1912 \setcounter{qr@j}{1}%
1913 \edef\qr@argument{{#1}(#2\relax)}%
1914 \xa\qr@writeformatA@recursive\qr@argument
1915 %
1916 \setcounter{qr@i}{\qr@numberofrowsinmatrix{#1}}%
1917 \setcounter{qr@j}{9}%
1918 \xa\qr@writeformatB@recursive\qr@argument
1919 }%
1920
1921 \def\qr@writeformatA@recursive#1(#2#3){%
1922 % #1 = matrix name
1923 % #2 = first bit of string
1924 % #3 = rest of bitstream
1925 % (qr@i,qr@j) = current (valid) position to write (in LaTeX counters)
1926 \ifnum#2=1\relax
1927 \qr@storetomatrix{#1}{\theqr@i}{\theqr@j}{\qr@black@format}%
1928 \else
1929 \qr@storetomatrix{#1}{\theqr@i}{\theqr@j}{\qr@white@format}%
1930 \fi
1931 % Now the tricky part--moving \i and \j to their next positions.
1932 \ifnum\c@qr@j<9\relax
1933 %If we're not yet in column 9, move right.
1934 \stepcounter{qr@j}%
1935 \ifnum\c@qr@j=7\relax
1936 %But we skip column 7!
1937 \stepcounter{qr@j}%
1938 \fi
1939 \else
1940 %If we're in column 9, we move up.
1941 \addtocounter{qr@i}{-1}%
1942 \ifnum\c@qr@i=7\relax
1943 %But we skip row 7!
1944 \addtocounter{qr@i}{-1}%
1945 \fi
1946 \fi
1947 %N.B. that at the end of time, this will leave us at invalid position (0,9).
1948 %That makes for an easy test to know when we are done.
1949 \ifnum\c@qr@i<1
1950 \let\qr@next=\relax
1951 \else
1952 \def\qr@next{\qr@writeformatA@recursive{#1}(#3)}%
1953 \fi
1954 \qr@next
1955 }%
1956
1957 \def\qr@writeformatB@recursive#1(#2#3){%
1958 % #1 = matrix name
1959 % #2 = first bit of string
1960 % #3 = rest of bitstream
1961 % (qr@i,qr@j) = current (valid) position to write (in LaTeX counters)
1962 \ifnum#2=1\relax
1963 \qr@storetomatrix{#1}{\theqr@i}{\theqr@j}{\qr@black@format}%
1964 \else
1965 \qr@storetomatrix{#1}{\theqr@i}{\theqr@j}{\qr@white@format}%
1966 \fi
1967 % Now the tricky part--moving counters i and j to their next positions.
1968 \qr@a=\qr@size%
1969 \advance\qr@a by -6\relax%
1970 \ifnum\qr@a<\c@qr@i\relax
1971 %If we're not yet in row n-6, move up.
1972 \addtocounter{qr@i}{-1}%
1973 \else
1974 \ifnum\qr@a=\c@qr@i\relax
1975 %If we're actually in row n-6, we jump to position (9,n-7).
1976 \setcounter{qr@i}{9}%
1977 %Set counter j equal to \qr@size-7.
1978 \global\c@qr@j=\qr@size\relax%
1979 \global\advance\c@qr@j by -7\relax%
1980 \else
1981 %Otherwise, we must be in row 9.
1982 %In this case, we move right.
1983 \stepcounter{qr@j}%
1984 \fi
1985 \fi
1986 %N.B. that at the end of time, this will leave us at invalid position (9,n+1).
1987 %That makes for an easy test to know when we are done.
1988 \xa\ifnum\qr@size<\c@qr@j\relax
1989 \let\qr@next=\relax
1990 \else
1991 \def\qr@next{\qr@writeformatB@recursive{#1}(#3)}%
1992 \fi
1993 \qr@next
1994 }%
1995
1996 \def\qr@writeversionstring#1#2{%
1997 % #1 = matrix name
1998 % #2 = binary string representing the encoded version information
1999 %
2000 % Plot the encoded version string into the matrix.
2001 % This is only done for versions 7 and higher.
2002 \xa\ifnum\qr@version>6\relax
2003 %Move to position (n-8,6).
2004 \setcounter{qr@i}{\qr@size}\relax%
2005 \addtocounter{qr@i}{-8}\relax%
2006 \setcounter{qr@j}{6}%
2007 \edef\qr@argument{{#1}(#2\relax)}%
2008 \xa\qr@writeversion@recursive\qr@argument
2009 \fi
2010 }%
2011
2012 \def\qr@writeversion@recursive#1(#2#3){%
2013 % #1 = matrix name
2014 % #2 = first bit of string
2015 % #3 = rest of bitstream
2016 % (qr@i,qr@j) = current (valid) position to write (in LaTeX counters)
2017 %
2018 % The version information is stored symmetrically in the matrix
2019 % In two transposed regions, so we can write both at the same time.
2020 % In the comments, we describe what happens in the lower-left region,
2021 % not the upper-right.
2022 %
2023 %Set \qr@topline equal to n-10.
2024 \qr@a=\qr@size\relax%
2025 \advance\qr@a by -10\relax%
2026 \edef\qr@topline{\the\qr@a}%
2027 %
2028 \ifnum#2=1\relax
2029 \qr@storetomatrix{#1}{\theqr@i}{\theqr@j}{\qr@black@format}%
2030 \qr@storetomatrix{#1}{\theqr@j}{\theqr@i}{\qr@black@format}%
2031 \else
2032 \qr@storetomatrix{#1}{\theqr@i}{\theqr@j}{\qr@white@format}%
2033 \qr@storetomatrix{#1}{\theqr@j}{\theqr@i}{\qr@white@format}%
2034 \fi
2035 % Now the tricky part--moving counters i and j to their next positions.
2036 \addtocounter{qr@i}{-1}%
2037 \xa\ifnum\qr@topline>\c@qr@i\relax
2038 %We've overshot the top of the region.
2039 %We need to move left one column and down three.
2040 \addtocounter{qr@j}{-1}%
2041 \addtocounter{qr@i}{3}%
2042 \fi
2043 %N.B. that at the end of time, this will leave us at invalid position (n-8,0).
2044 %That makes for an easy test to know when we are done.
2045 \ifnum\c@qr@j<1\relax
2046 \let\qr@next=\relax
2047 \else
2048 \def\qr@next{\qr@writeversion@recursive{#1}(#3)}%
2049 \fi
2050 \qr@next
2051 }%
2052 % \end{macrocode}
2053 %
2054 % \subsection{Encoding and error correction}
2055 % \begin{macrocode}
2056 \newcounter{qr@hexchars}%
2057
2058 \def\qr@string@binarytohex#1{%
2059 \qr@binarytohex{\qr@hex@result}{#1}%
2060 }%
2061
2062 \def\qr@encode@binary#1{%
2063 % #1 = string of ascii characters, to be converted into bitstream
2064 %
2065 % We do this one entirely in hex, rather than binary, because we can.
2066 \edef\qr@plaintext{#1}%
2067 %
2068 %First, the mode indicator.
2069 \def\qr@codetext{4}% %This means `binary'
2070 %
2071 %Next, the character count.
2072 \qr@getstringlength{\qr@plaintext}%
2073 %Set \qr@charactercountlengthinhex to \qr@charactercountbits@byte/4%
2074 \qr@a=\qr@charactercountbits@byte\relax%
2075 \divide \qr@a by 4\relax%
2076 \edef\qr@charactercountlengthinhex{\the\qr@a}%
2077 \qr@decimaltohex[\qr@charactercountlengthinhex]{\qr@charactercount}{\qr@stringlength}%
2078 \xa\g@addto@macro\xa\qr@codetext\xa{\qr@charactercount}%
2079 %
2080 %Now comes the actual data.
2081 \edef\qr@argument{(,\qr@plaintext\relax\relax\relax)}%
2082 \xa\qr@encode@ascii@recursive\qr@argument%
2083 %
2084 %Now the terminator.
2085 \g@addto@macro\qr@codetext{0}% %This is '0000' in binary.
2086 %
2087 %There is no need to pad bits to make a multiple of 8,
2088 %because the data length is already 4 + 8 + 8n + 4.
2089 %
2090 %Now add padding codewords if needed.
2091 \setcounter{qr@hexchars}{0}%
2092 \qr@getstringlength{\qr@codetext}%
2093 \setcounter{qr@hexchars}{\qr@stringlength}%
2094 %Set \qr@numpaddingcodewords equal to \qr@totaldatacodewords - qr@hexchars/2.
2095 \qr@a=-\c@qr@hexchars\relax
2096 \divide\qr@a by 2\relax
2097 \advance\qr@a by \qr@totaldatacodewords\relax
2098 \edef\qr@numpaddingcodewords{\the\qr@a}%
2099 %
2100 \xa\ifnum\qr@numpaddingcodewords<0%
2101 \edef\ds{ERROR: Too much data! Over by \qr@numpaddingcodewords bytes.}\show\ds%
2102 \fi%
2103 \xa\ifnum\qr@numpaddingcodewords>0%
2104 \qr@for \i = 2 to \qr@numpaddingcodewords by 2%
2105 {\g@addto@macro{\qr@codetext}{ec11}}%
2106 \xa\ifodd\qr@numpaddingcodewords\relax%
2107 \g@addto@macro{\qr@codetext}{ec}%
2108 \fi%
2109 \fi%
2110 }%
2111
2112 \def\qr@encode@ascii@recursive(#1,#2#3){%
2113 % #1 = hex codes translated so far
2114 % #2 = next plaintext character to translate
2115 % #3 = remainder of plaintext
2116 \edef\qr@testii{#2}%
2117 \ifx\qr@testii\qr@relax%
2118 % All done!
2119 \g@addto@macro\qr@codetext{#1}%
2120 \else%
2121 % Another character to translate.
2122 \edef\qr@asciicode{\number`#2}%
2123 \qr@decimaltohex[2]{\qr@newhexcodes}{\qr@asciicode}%
2124 \edef\qr@argument{(#1\qr@newhexcodes,#3)}%
2125 %\show\qr@argument
2126 \xa\qr@encode@ascii@recursive\qr@argument%
2127 \fi%
2128 }%
2129
2130 \def\qr@splitcodetextintoblocks{%
2131 \setcounter{qr@i}{0}%
2132 \qr@for \j = 1 to \qr@numshortblocks by 1%
2133 {\stepcounter{qr@i}%
2134 \qr@splitoffblock{\qr@codetext}{\theqr@i}{\qr@shortblock@size}%
2135 }%
2136 \xa\ifnum\qr@numlongblocks>0\relax%
2137 \qr@for \j = 1 to \qr@numlongblocks by 1%
2138 {\stepcounter{qr@i}%
2139 \qr@splitoffblock{\qr@codetext}{\theqr@i}{\qr@longblock@size}%
2140 }%
2141 \fi%
2142 }%
2143
2144 \def\qr@splitoffblock#1#2#3{%
2145 % #1 = current codetext in hexadecimal
2146 % #2 = number to use in csname "\datablock@#2".
2147 % #3 = number of bytes to split off
2148 \message{<Splitting off block #2>}%
2149 \xa\gdef\csname datablock@#2\endcsname{}% %This line is important!
2150 \qr@for \i = 1 to #3 by 1%
2151 {\edef\qr@argument{{#2}(#1)}%
2152 \xa\qr@splitoffblock@int\qr@argument%
2153 }%
2154 }%
2155
2156 \def\qr@splitoffblock@int#1(#2#3#4){%
2157 % #1 = number to use in csname "\datablock@#1".
2158 % #2#3 = next byte to split off
2159 % #4 = remaining text
2160 %
2161 % We add the next byte to "\datablock@#1",
2162 % and we remove it from the codetext.
2163 \xa\xdef\csname datablock@#1\endcsname{\csname datablock@#1\endcsname#2#3}%
2164 \xdef\qr@codetext{#4}%
2165 }%
2166
2167 \def\qr@createerrorblocks{%
2168 \qr@for \ii = 1 to \qr@numblocks by 1%
2169 {\message{<Making error block \the\ii>}%
2170 \FX@generate@errorbytes{\csname datablock@\the\ii\endcsname}{\qr@num@eccodewords}%
2171 \xa\xdef\csname errorblock@\the\ii\endcsname{\FX@errorbytes}%
2172 }%
2173 }%
2174
2175 \def\qr@interleave{%
2176 \setcounter{qr@i}{0}%
2177 \def\qr@interleaved@text{}%
2178 \message{<Interleaving datablocks of length \qr@shortblock@size\ and \qr@longblock@size: }%
2179 \qr@for \ii = 1 to \qr@shortblock@size by 1%
2180 {\qr@for \jj = 1 to \qr@numblocks by 1%
2181 {\qr@writefromblock{datablock}{\the\jj}%
2182 }%
2183 \message{\the\ii,}%
2184 }%
2185 %The long blocks are numbered \qr@numshortblocks+1, \qr@numshortblocks+2, ..., \qr@numblocks.
2186 \qr@a=\qr@numshortblocks\relax%
2187 \advance\qr@a by 1\relax%
2188 \qr@for \jj = \qr@a to \qr@numblocks by 1%
2189 {\qr@writefromblock{datablock}{\the\jj}}%
2190 \xa\ifnum\qr@numlongblocks>0\relax%
2191 \message{\qr@longblock@size.>}%
2192 \else
2193 \message{.>}%
2194 \fi
2195 \message{<Interleaving errorblocks of length \qr@num@eccodewords: }%
2196 \qr@for \ii = 1 to \qr@num@eccodewords by 1%
2197 {\message{\the\ii,}%
2198 \qr@for \jj = 1 to \qr@numblocks by 1%
2199 {\qr@writefromblock{errorblock}{\the\jj}%
2200 }%
2201 }%
2202 \message{.><Interleaving complete.>}%
2203 }%
2204
2205 \def\qr@writefromblock#1#2{%
2206 % #1 = either 'datablock' or 'errorblock'
2207 % #2 = block number, in {1,...,\qr@numblocks}%
2208 \edef\qr@argument{(\csname #1@#2\endcsname\relax\relax\relax)}%
2209 \xa\qr@writefromblock@int\qr@argument
2210 \xa\xdef\csname #1@#2\endcsname{\qr@writefromblock@remainder}%
2211 }%
2212
2213 \def\qr@writefromblock@int(#1#2#3){%
2214 % #1#2 = first byte (in hex) of text, which will be written to \qr@interleaved@text
2215 % #3 = remainder, including \relax\relax\relax terminator.
2216 \g@addto@macro{\qr@interleaved@text}{#1#2}%
2217 \qr@writefromblock@intint(#3)%
2218 }%
2219
2220 \def\qr@writefromblock@intint(#1\relax\relax\relax){%
2221 \xdef\qr@writefromblock@remainder{#1}%
2222 }%
2223 % \end{macrocode}
2224 %
2225 % \subsection{Encoding format and version information}
2226 % \begin{macrocode}
2227 \let\xa=\expandafter
2228 \makeatletter
2229
2230 \def\qr@preface@macro#1#2{%
2231 % #1 = macro name
2232 % #2 = text to add to front of macro
2233 \def\qr@tempb{#2}%
2234 \xa\xa\xa\gdef\xa\xa\xa#1\xa\xa\xa{\xa\qr@tempb #1}%
2235 }%
2236
2237 \newif\ifqr@leadingcoeff
2238 \def\qr@testleadingcoeff(#1#2){%
2239 % Tests whether the leading digit of #1#2 is 1.
2240 \ifnum#1=1\relax
2241 \qr@leadingcoefftrue
2242 \else
2243 \qr@leadingcoefffalse
2244 \fi
2245 }%
2246
2247 \def\qr@polynomialdivide#1#2{%
2248 \edef\qr@numerator{#1}%
2249 \edef\qr@denominator{#2}%
2250 \qr@divisiondonefalse%
2251 \xa\xa\xa\qr@oneroundofdivision\xa\xa\xa{\xa\qr@numerator\xa}\xa{\qr@denominator}%
2252 }%
2253
2254 \def\@qr@empty{}%
2255 \def\qr@oneroundofdivision#1#2{%
2256 % #1 = f(x), of degree n
2257 % #2 = g(x), of degree m
2258 % Obtains a new polynomial h(x), congruent to f(x) modulo g(x),
2259 % but of degree at most n-1.
2260 %
2261 % If leading coefficient of f(x) is 1, subtracts off g(x) * x^(n-m).
2262 % If leading coefficient of f(x) is 0, strips off that leading zero.
2263 %
2264 \qr@testleadingcoeff(#1)%
2265 \ifqr@leadingcoeff
2266 \qr@xorbitstrings{#1}{#2}%
2267 \ifqr@xorfailed
2268 %If xor failed, that means our #1 was already the remainder!
2269 \qr@divisiondonetrue
2270 \edef\qr@theremainder{#1}%
2271 \else
2272 %xor succeeded. We need to recurse.
2273 \xa\xa\xa\edef\xa\xa\xa\qr@numerator\xa\xa\xa{\xa\qr@stripleadingzero\xa(\qr@xorresult)}%
2274 \fi
2275 \else
2276 \xa\def\xa\qr@numerator\xa{\qr@stripleadingzero(#1)}%
2277 \ifx\qr@numerator\@qr@empty
2278 \qr@divisiondonetrue
2279 \def\qr@theremainder{0}%
2280 \fi
2281 \fi
2282 \ifqr@divisiondone
2283 \relax
2284 \else
2285 \xa\qr@oneroundofdivision\xa{\qr@numerator}{#2}%
2286 \fi
2287 }%
2288
2289 \def\qr@stripleadingzero(0#1){#1}%Strips off a leading zero.
2290
2291 \newif\ifqr@xorfailed% This flag will trigger when #2 is longer than #1.
2292
2293 \def\qr@xorbitstrings#1#2{%
2294 % #1 = bitstring
2295 % #2 = bitstring no longer than #1
2296 \qr@xorfailedfalse
2297 \edef\qr@argument{(,#1\relax\relax)(#2\relax\relax)}%
2298 \xa\qr@xorbitstrings@recursive\qr@argument
2299 %\qr@xorbitstrings@recursive(,#1\relax\relax)(#2\relax\relax)%
2300 }%
2301
2302 \def\qr@xorbitstrings@recursive(#1,#2#3)(#4#5){%
2303 % #1#2#3 is the first bitstring, xor'ed up through #1.
2304 % #4#5 is the remaining portion of the second bitstring.
2305 \def\qr@testii{#2}%
2306 \def\qr@testiv{#4}%
2307 \ifx\qr@testii\qr@relax
2308 % #1 contains the whole string.
2309 % Now if #4 is also \relax, that means the two strings started off with equal lengths.
2310 % If, however, #4 is not \relax, that means the second string was longer than the first, a problem.
2311 \ifx\qr@testiv\qr@relax
2312 %No problem. We are done.
2313 \qr@xorbit@saveresult(#1#2#3)%
2314 \else
2315 %Problem! The second string was longer than the first.
2316 \qr@xorfailedtrue
2317 \def\qr@xorresult{}%
2318 \fi
2319 \else
2320 % There is still a bit to manipulate in #2.
2321 % Check whether #4 contains anything.
2322 \ifx\qr@testiv\qr@relax
2323 % No, #4 is empty. We are done. "#2#3" contains the remainder of the first string,
2324 % which we append untouched and then strip off the two \relax-es.
2325 \qr@xorbit@saveresult(#1#2#3)%
2326 \else
2327 % Yes, #4 still has something to XOR. Do the task.
2328 \ifnum#2=#4\relax
2329 \qr@xorbitstrings@recursive(#1%
2330 0,#3)(#5)%
2331 \else
2332 \qr@xorbitstrings@recursive(#1%
2333 1,#3)(#5)%
2334 \fi
2335 \fi
2336 \fi
2337 }%
2338
2339 \def\qr@xorbit@saveresult(#1\relax\relax){%
2340 %Strips off the extra '\relax'es at the end.
2341 \def\qr@xorresult{#1}%
2342 }%
2343
2344 \newif\ifqr@divisiondone
2345
2346 \def\qr@BCHcode#1{%
2347 \edef\qr@formatinfo{#1}%
2348 \def\qr@formatinfopadded{\qr@formatinfo 0000000000}%
2349 \def\qr@divisor{10100110111}%
2350 \qr@divisiondonefalse
2351 \qr@polynomialdivide{\qr@formatinfopadded}{\qr@divisor}%
2352 %
2353 \qr@getstringlength{\qr@theremainder}%
2354 %Run loop from stringlength+1 to 10.
2355 \qr@a=\qr@stringlength\relax%
2356 \advance\qr@a by 1\relax%
2357 \qr@for \i = \qr@a to 10 by 1%
2358 {\qr@preface@macro{\qr@theremainder}{0}%
2359 \xdef\qr@theremainder{\qr@theremainder}%
2360 }%
2361 \edef\qr@BCHresult{\qr@formatinfo\qr@theremainder}%
2362 }%
2363
2364 \def\qr@formatmask{101010000010010}%
2365
2366 \def\qr@encodeandmaskformat#1{%
2367 \qr@BCHcode{#1}%
2368 \qr@xorbitstrings{\qr@BCHresult}{\qr@formatmask}%
2369 \edef\qr@format@bitstring{\qr@xorresult}%
2370 }%
2371
2372 \def\qr@Golaycode#1{%
2373 % #1 = 6-bit version number
2374 \edef\qr@versioninfo{#1}%
2375 \def\qr@versioninfopadded{\qr@versioninfo 000000000000}% %Append 12 zeros.
2376 \def\qr@divisor{1111100100101}%
2377 \qr@divisiondonefalse
2378 \qr@polynomialdivide{\qr@versioninfopadded}{\qr@divisor}%
2379 %
2380 \qr@getstringlength{\qr@theremainder}%
2381 %Run loop from stringlength+1 to 12.
2382 \qr@a=\qr@stringlength\relax%
2383 \advance\qr@a by 1\relax%
2384 \qr@for \i = \qr@a to 12 by 1%
2385 {\qr@preface@macro{\qr@theremainder}{0}%
2386 \xdef\qr@theremainder{\qr@theremainder}%
2387 }%
2388 \edef\qr@Golayresult{\qr@versioninfo\qr@theremainder}%
2389 }%
2390 % \end{macrocode}
2391 %
2392 % \subsection{Error correction}
2393 % The error-correction code is defined over $\F_{256}=GF(256)$,
2394 % the finite field of order $256$.
2395 % The QR specification encodes this field as $\F_2[X]/(X^8+X^4+X^3+X^2+1)$;
2396 % in other words, each field element is an 8-bit binary string representing
2397 % an integer between 0 and 255.
2398 %
2399 % We represent these 8-bit strings as two hexadecimal characters;
2400 % for example, {\tt 5a} represents {\tt 01011010}.
2401 % Because addition is done by xor-ing the bitstrings,
2402 % we can define addition on hexadecimal characters.
2403 % Since there are only $16^2$ possibilities,
2404 % we create a lookup table.
2405 % \begin{macrocode}
2406 \def\F@result{}%
2407
2408 \def\qr@xorbitstring#1#2#3{%
2409 % #1 = new macro to receive result
2410 % #2, #3 = bitstrings to xor. The second can be shorter than the first.
2411 \def\qr@xor@result{}%
2412 \edef\qr@argument{(#2\relax\relax)(#3\relax\relax)}%
2413 \xa\qr@xorbitstring@recursive\qr@argument%
2414 \edef#1{\qr@xor@result}%
2415 }%
2416 \def\qr@xorbitstring@recursive(#1#2)(#3#4){%
2417 \edef\qr@testi{#1}%
2418 \ifx\qr@testi\qr@relax%
2419 %Done.
2420 \let\qr@next=\relax%
2421 \else
2422 \if#1#3\relax
2423 \g@addto@macro{\qr@xor@result}{0}%
2424 \else
2425 \g@addto@macro{\qr@xor@result}{1}%
2426 \fi
2427 \edef\qr@next{\noexpand\qr@xorbitstring@recursive(#2)(#4)}%
2428 \fi
2429 \qr@next
2430 }
2431
2432 \def\F@addchar@raw#1#2{%
2433 %Add two hexadecimal digits using bitwise xor
2434 \qr@hextobinary[4]{\qr@summandA}{#1}%
2435 \qr@hextobinary[4]{\qr@summandB}{#2}%
2436 \qr@xorbitstring{\F@result}{\qr@summandA}{\qr@summandB}%
2437 \qr@binarytohex[1]{\F@result}{\F@result}%
2438 }%
2439
2440 \def\qr@canceltwos#1{%
2441 \edef\qr@argument{(#1\relax\relax)}%
2442 \xa\qr@canceltwos@int\qr@argument%
2443 }%
2444
2445 \def\qr@canceltwos@int(#1#2){%
2446 \xa\qr@canceltwos@recursion(,#1#2)%
2447 }%
2448
2449 \def\qr@canceltwos@recursion(#1,#2#3){%
2450 \def\qr@testii{#2}%
2451 \ifx\qr@testii\qr@relax
2452 %Cancelling complete.
2453 \qr@striptworelaxes(#1#2#3)%
2454 %Now \F@result contains the answer.
2455 \else
2456 \relax
2457 \ifnum#2=2\relax
2458 \qr@canceltwos@recursion(#10,#3)%
2459 \else
2460 \qr@canceltwos@recursion(#1#2,#3)%
2461 \fi
2462 \fi
2463 }%
2464
2465 \def\qr@striptworelaxes(#1\relax\relax){%
2466 \gdef\F@result{#1}%
2467 }%
2468
2469 \qr@for \i = 0 to 15 by 1%
2470 {\qr@decimaltohex[1]{\qr@tempa}{\the\i}%
2471 \qr@for \j = 0 to 15 by 1%
2472 {\qr@decimaltohex[1]{\qr@tempb}{\the\j}%
2473 \F@addchar@raw\qr@tempa\qr@tempb
2474 \xa\xdef\csname F@addchar@\qr@tempa\qr@tempb\endcsname{\F@result}%
2475 }%
2476 }%
2477
2478 \def\F@addchar#1#2{%
2479 \xa\def\xa\F@result\xa{\csname F@addchar@#1#2\endcsname}%
2480 }%
2481
2482 \def\F@addstrings#1#2{%
2483 \edef\qr@argument{(,#1\relax\relax)(#2\relax\relax)}%
2484 \xa\F@addstrings@recursion\qr@argument%
2485 }%
2486
2487 \def\F@addstrings@recursion(#1,#2#3)(#4#5){%
2488 %Adds two hexadecimal strings, bitwise, from left to right.
2489 %The second string is allowed to be shorter than the first.
2490 \def\qr@testii{#2}%
2491 \def\qr@testiv{#4}%
2492 \ifx\qr@testii\qr@relax
2493 %The entire string has been processed.
2494 \gdef\F@result{#1}%
2495 \else
2496 \ifx\qr@testiv\qr@relax
2497 %The second string is over.
2498 \qr@striptworelaxes(#1#2#3)%
2499 %Now \F@result contains the answer.
2500 \else
2501 %We continue to add.
2502 \F@addchar{#2}{#4}%
2503 \edef\qr@argument{(#1\F@result,#3)(#5)}%
2504 \xa\F@addstrings@recursion\qr@argument%
2505 \fi
2506 \fi
2507 }%
2508 % \end{macrocode}
2509 % Now we handle multiplication.
2510 % Our approach to multiplying $a,b\in\F_{256}$
2511 % is to take discrete logarithms (with 2 as our base),
2512 % add the logarithms, and then take the antilog.
2513 % We first create a log table.
2514 % \begin{macrocode}
2515 \gdef\F@stripleadingzero(0#1){\edef\F@result{#1}}%
2516
2517 \setcounter{qr@i}{0}%
2518 \def\qr@poweroftwo{1}%
2519 \qr@for \i = 1 to 254 by 1%
2520 {\stepcounter{qr@i}%
2521 \qr@a=\qr@poweroftwo\relax
2522 \multiply\qr@a by 2\relax
2523 \edef\qr@poweroftwo{\the\qr@a}%
2524 %\show\qr@poweroftwo
2525 \qr@decimaltohex[2]{\qr@poweroftwo@hex}{\qr@poweroftwo}%
2526 \xa\ifnum\qr@poweroftwo>255\relax
2527 %We need to bitwise add the polynomial represented by 100011101, i.e. 0x11d.
2528 \F@addstrings{\qr@poweroftwo@hex}{11d}% %Now it should start with 0.
2529 \xa\F@stripleadingzero\xa(\F@result)% %Now it should be two hex digits.
2530 \edef\qr@poweroftwo@hex{\F@result}% %Save the hex version.
2531 \qr@hextodecimal{\qr@poweroftwo}{\F@result}%
2532 \fi
2533 \xdef\qr@poweroftwo{\qr@poweroftwo}%
2534 \xa\xdef\csname F@twotothe@\theqr@i\endcsname{\qr@poweroftwo@hex}%
2535 \xa\xdef\csname F@logtwo@\qr@poweroftwo@hex\endcsname{\theqr@i}%
2536 }%
2537 \xa\xdef\csname F@twotothe@0\endcsname{01}%
2538 \xa\xdef\csname F@logtwo@01\endcsname{0}%
2539
2540 \def\F@twotothe#1{%
2541 \xa\xdef\xa\F@result\xa{\csname F@twotothe@#1\endcsname}%
2542 }%
2543 \def\F@logtwo#1{%
2544 \xa\xdef\xa\F@result\xa{\csname F@logtwo@#1\endcsname}%
2545 }%
2546
2547 \def\qr@zerozero{00}%
2548
2549 \def\F@multiply#1#2{%
2550 % #1 and #2 are two elements of F_256,
2551 % given as two-character hexadecimal strings.
2552 % Multiply them within F_256, and place the answer in \F@result
2553 \edef\qr@argA{#1}%
2554 \edef\qr@argB{#2}%
2555 \ifx\qr@argA\qr@zerozero
2556 \def\F@result{00}%
2557 \else
2558 \ifx\qr@argB\qr@zerozero
2559 \def\F@result{00}%
2560 \else
2561 \xa\F@logtwo\xa{\qr@argA}%
2562 \edef\qr@logA{\F@result}%
2563 \xa\F@logtwo\xa{\qr@argB}%
2564 \edef\qr@logB{\F@result}%
2565 \xa\qr@a\xa=\qr@logA\relax% \qr@a = \qr@logA
2566 \xa\advance\xa\qr@a\qr@logB\relax% \advance \qr@a by \qr@logB
2567 \ifnum\qr@a>254\relax%
2568 \advance\qr@a by -255\relax%
2569 \fi%
2570 \xa\F@twotothe\xa{\the\qr@a}%
2571 % Now \F@result contains the product, as desired.
2572 \fi
2573 \fi
2574 }%
2575
2576
2577 \def\F@multiply#1#2{%
2578 % #1 and #2 are two elements of F_256,
2579 % given as two-character hexadecimal strings.
2580 % Multiply them within F_256, and place the answer in \F@result
2581 \edef\qr@argA{#1}%
2582 \edef\qr@argB{#2}%
2583 \ifx\qr@argA\qr@zerozero
2584 \def\F@result{00}%
2585 \else
2586 \ifx\qr@argB\qr@zerozero
2587 \def\F@result{00}%
2588 \else
2589 \xa\F@logtwo\xa{\qr@argA}%
2590 \edef\qr@logA{\F@result}%
2591 \xa\F@logtwo\xa{\qr@argB}%
2592 \edef\qr@logB{\F@result}%
2593 \xa\qr@a\xa=\qr@logA\relax% \qr@a = \qr@logA
2594 \xa\advance\xa\qr@a\qr@logB\relax% \advance \qr@a by \qr@logB
2595 \ifnum\qr@a>254\relax%
2596 \advance\qr@a by -255\relax%
2597 \fi%
2598 \xa\F@twotothe\xa{\the\qr@a}%
2599 % Now \F@result contains the product, as desired.
2600 \fi
2601 \fi
2602 }%
2603 % \end{macrocode}
2604 % Having developed $\F_{256}$, we now turn to the real task.
2605 % Each byte of data is read as an element of $\F_{256}$,
2606 % and a sequence of bytes corresponds a polynomial in
2607 % $\F_{256}[X]$.
2608 % The Reed-Solomon code takes that codeword $f(X)\in \F_{256}[X]$
2609 % of degree $k$, divides $f(X)\cdot X^\ell$ by a fixed
2610 % polynomial,
2611 % and keeps the remainder $r(X)$ as the error-correction word.
2612 % We therefore must implement polynomial division over $\F_{256}$.
2613 % \begin{macrocode}
2614 %These polynomials are represented as sequences of coefficients,
2615 %from highest power to lowest,
2616 %with each coefficient represented by two hexadecimal characters.
2617
2618 \def\FX@getstringlength#1{%
2619 %Count number of two-character coefficients
2620 \setcounter{qr@i}{0}%
2621 \xdef\qr@argument{(#1\relax\relax\relax)}%
2622 \xa\FX@stringlength@recursive\qr@argument%
2623 \xdef\stringresult{\arabic{qr@i}}%
2624 }%
2625
2626 \def\FX@stringlength@recursive(#1#2#3){%
2627 \def\qr@testi{#1}%
2628 \ifx\qr@testi\qr@relax
2629 %we are done.
2630 \else
2631 \stepcounter{qr@i}%
2632 %\showthe\c@qr@i
2633 \qr@stringlength@recursive(#3)%
2634 \fi
2635 }%
2636
2637 \newif\ifFX@leadingcoeff@zero
2638 \def\FX@testleadingcoeff(#1#2#3){%
2639 % Tests whether the leading coefficient of the hex-string #1#2#3 is '00'.
2640 \edef\FX@leadingcoefficient{#1#2}%
2641 \FX@leadingcoeff@zerofalse
2642 \ifx\FX@leadingcoefficient\qr@zerozero
2643 \FX@leadingcoeff@zerotrue
2644 \fi
2645 }%
2646
2647 \newif\ifFX@divisiondone
2648
2649 \newcounter{qr@divisionsremaining} %Keep track of how many divisions to go!
2650 \def\FX@polynomialdivide#1#2{%
2651 \edef\FX@numerator{#1}%
2652 \edef\FX@denominator{#2}%
2653 \qr@getstringlength\FX@numerator%
2654 \setcounter{qr@divisionsremaining}{\qr@stringlength}%
2655 \qr@getstringlength\FX@denominator%
2656 \addtocounter{qr@divisionsremaining}{-\qr@stringlength}%
2657 \addtocounter{qr@divisionsremaining}{2}%
2658 \divide\c@qr@divisionsremaining by 2\relax% %2 hex chars per number
2659 \FX@divisiondonefalse%
2660 \xa\xa\xa\FX@polynomialdivide@recursive\xa\xa\xa{\xa\FX@numerator\xa}\xa{\FX@denominator}%
2661 }%
2662
2663 \def\FX@polynomialdivide@recursive#1#2{%
2664 % #1 = f(x), of degree n
2665 % #2 = g(x), of degree m
2666 % Obtains a new polynomial h(x), congruent to f(x) modulo g(x),
2667 % but of degree at most n-1.
2668 %
2669 % If leading coefficient of f(x) is 0, strips off that leading zero.
2670 % If leading coefficient of f(x) is a, subtracts off a * g(x) * x^(n-m).
2671 % N.B. we assume g is monic.
2672 %
2673 \FX@testleadingcoeff(#1)%
2674 \ifFX@leadingcoeff@zero%
2675 %Leading coefficient is zero, so remove it.
2676 \xa\def\xa\FX@numerator\xa{\FX@stripleadingzero(#1)}%
2677 \else%
2678 %Leading coefficient is nonzero, and contained in \FX@leadingcoefficient
2679 \FX@subtractphase{#1}{#2}{\FX@leadingcoefficient}%
2680 \ifFX@subtract@failed%
2681 %If subtraction failed, that means our #1 was already the remainder!
2682 \FX@divisiondonetrue%
2683 \edef\qr@theremainder{#1}%
2684 \else%
2685 %xor succeeded. We need to recurse.
2686 \xa\xa\xa\edef\xa\xa\xa\FX@numerator\xa\xa\xa{\xa\FX@stripleadingzero\xa(\FX@subtraction@result)}%
2687 \fi%
2688 \fi%
2689 \addtocounter{qr@divisionsremaining}{-1}%
2690 \ifnum\c@qr@divisionsremaining=0\relax
2691 %Division is done!
2692 \FX@divisiondonetrue%
2693 \edef\qr@theremainder{\FX@numerator}%
2694 \relax%
2695 \else%
2696 \xa\FX@polynomialdivide@recursive\xa{\FX@numerator}{#2}%
2697 \fi%
2698 }%
2699
2700 \def\FX@stripleadingzero(00#1){#1}%Strips off a single leading zero of F_256.
2701
2702 \newif\ifFX@subtract@failed% This flag will trigger when #2 is longer than #1.
2703
2704 \def\FX@subtractphase#1#2#3{%
2705 % #1 = bitstring
2706 % #2 = bitstring no longer than #1
2707 % #3 = leading coefficient
2708 \FX@subtract@failedfalse%
2709 \edef\qr@argument{(,#1\relax\relax\relax)(#2\relax\relax\relax)(#3)}%
2710 \xa\FX@subtract@recursive\qr@argument%
2711 }%
2712
2713 \def\FX@subtract@recursive(#1,#2#3#4)(#5#6#7)(#8){%
2714 % This is a recursive way to compute f(x) - a*g(x)*x^k.
2715 % #1#2#3#4 is the first bitstring, subtracted up through #1.
2716 % Thus #2#3 constitutes the next two-character coefficient.
2717 % #5#6#7 is the remaining portion of the second bitstring.
2718 % Thus #5#6 constitutes the next two-character coefficient
2719 % #8 is the element a of F_256. It should contain two characters.
2720 \def\qr@testii{#2}%
2721 \def\qr@testv{#5}%
2722 \ifx\qr@testii\qr@relax
2723 % #1 contains the whole string.
2724 % Now if #5 is also \relax, that means the two strings started off with equal lengths.
2725 % If, however, #5 is not \relax, that means the second string was longer than the first, a problem.
2726 \ifx\qr@testv\qr@relax
2727 %No problem. We are done.
2728 \FX@subtract@saveresult(#1#2#3#4)% %We keep the #2#3#4 to be sure we have all three relax-es to strip off.
2729 \else
2730 %Problem! The second string was longer than the first.
2731 %This usually indicates the end of the long division process.
2732 \FX@subtract@failedtrue
2733 \def\FX@subtraction@result{}%
2734 \fi
2735 \else
2736 % There is still a coefficient to manipulate in #2#3.
2737 % Check whether #5 contains anything.
2738 \ifx\qr@testv\qr@relax
2739 % No, #5 is empty. We are done. "#2#3#4" contains the remainder of the first string,
2740 % which we append untouched and then strip off the three \relax-es.
2741 \FX@subtract@saveresult(#1#2#3#4)%
2742 \else
2743 % Yes, #5#6 still has something to XOR. Do the task.
2744 \F@multiply{#5#6}{#8}% Multiply by the factor 'a'.
2745 \F@addstrings{#2#3}{\F@result}% Subtract. (We're in characteristic two, so adding works.)
2746 \edef\qr@argument{(#1\F@result,#4)(#7)(#8)}%
2747 \xa\FX@subtract@recursive\qr@argument%
2748 \fi
2749 \fi
2750 }%
2751
2752 \def\FX@subtract@saveresult(#1\relax\relax\relax){%
2753 %Strips off the three extra '\relax'es at the end.
2754 \def\FX@subtraction@result{#1}%
2755 }%
2756
2757 \def\FX@creategeneratorpolynomial#1{%
2758 % #1 = n, the number of error codewords desired.
2759 % We need to create \prod_{j=0}^{n-1} (x-2^j).
2760 \edef\FX@generator@degree{#1}%
2761 \def\FX@generatorpolynomial{01}% Initially, set it equal to 1.
2762 \setcounter{qr@i}{0}%
2763 \FX@creategenerator@recursive%
2764 %The result is now stored in \FX@generatorpolynomial
2765 }%
2766
2767 \def\FX@creategenerator@recursive{%
2768 % \c@qr@i contains the current value of i.
2769 % \FX@generatorpolynomial contains the current polynomial f(x),
2770 % which should be a degree-i polynomial
2771 % equal to \prod_{j=0}^{i-1} (x-2^j).
2772 % (If i=0, then \FX@generatorpolynomial should be 01.)
2773 % This recursion step should multiply the existing polynomial by (x-2^i),
2774 % increment i by 1, and check whether we're done or not.
2775 \edef\qr@summandA{\FX@generatorpolynomial 00}% This is f(x) * x
2776 \edef\qr@summandB{00\FX@generatorpolynomial}% This is f(x), with a 0x^{i+1} in front.
2777 \F@twotothe{\theqr@i}%
2778 \edef\qr@theconstant{\F@result}%
2779 \FX@subtractphase{\qr@summandA}{\qr@summandB}{\qr@theconstant}%
2780 %This calculates \qr@summandA + \qr@theconstant * \qr@summandB
2781 %and stores the result in \FX@subtraction@result
2782 \edef\FX@generatorpolynomial{\FX@subtraction@result}%
2783 \stepcounter{qr@i}%
2784 \xa\ifnum\FX@generator@degree=\c@qr@i\relax%
2785 %We just multiplied by (x-2^{n-1}), so we're done.
2786 \relax%
2787 \else%
2788 %We need to do this again!
2789 \xa%
2790 \FX@creategenerator@recursive%
2791 \fi%
2792 }%
2793
2794 \def\FX@generate@errorbytes#1#2{%
2795 % #1 = datastream in hex
2796 % #2 = number of error correction bytes requested
2797 \edef\qr@numerrorbytes{#2}%
2798 \xa\FX@creategeneratorpolynomial\xa{\qr@numerrorbytes}%
2799 \edef\FX@numerator{#1}%
2800 \qr@for \i = 1 to \qr@numerrorbytes by 1%
2801 {\g@addto@macro\FX@numerator{00}}% %One error byte means two hex codes.
2802 \FX@polynomialdivide{\FX@numerator}{\FX@generatorpolynomial}%
2803 \edef\FX@errorbytes{\qr@theremainder}%
2804 }%
2805 % \end{macrocode}
2806 %
2807 % \subsection{Version handling}
2808 % \begin{macrocode}
2809 \newif\ifqr@versionmodules
2810
2811 \def\qr@level@char#1{%
2812 \xa\ifcase#1
2813 M\or L\or H\or Q\fi}%
2814
2815 \newif\ifqr@versiongoodenough
2816 \def\qr@choose@best@version#1{%
2817 % \qr@desiredversion = user-requested version
2818 % \qr@desiredlevel = user-requested error-correction level
2819 \edef\qr@plaintext{#1}%
2820 \qr@getstringlength{\qr@plaintext}%
2821 %
2822 %Run double loop over levels and versions, looking for
2823 %the smallest version that can contain our data,
2824 %and then choosing the best error-correcting level at that version,
2825 %subject to the level being at least as good as the user desires.
2826 \global\qr@versiongoodenoughfalse%
2827 \gdef\qr@bestversion{0}%
2828 \gdef\qr@bestlevel{0}%
2829 \ifnum\qr@desiredversion=0\relax
2830 \qr@a=1\relax
2831 \else
2832 \qr@a=\qr@desiredversion\relax
2833 \fi
2834 \qr@for \i=\qr@a to 40 by 1
2835 {\edef\qr@version{\the\i}%
2836 \global\qr@versiongoodenoughfalse
2837 \qr@for \j=0 to 3 by 1%
2838 {%First, we map {0,1,2,3} to {1,0,4,3}, so that we loop through {M,L,H,Q}
2839 %in order of increasing error-correction capabilities.
2840 \qr@a = \j\relax
2841 \divide \qr@a by 2\relax
2842 \multiply \qr@a by 4\relax
2843 \advance \qr@a by 1\relax
2844 \advance \qr@a by -\j\relax
2845 \edef\qr@level{\the\qr@a}%
2846 \ifnum\qr@desiredlevel=\qr@a\relax
2847 \global\qr@versiongoodenoughtrue
2848 \fi
2849 \ifqr@versiongoodenough
2850 \qr@calculate@capacity{\qr@version}{\qr@level}%
2851 \xa\xa\xa\ifnum\xa\qr@truecapacity\xa<\qr@stringlength\relax
2852 %Too short
2853 \relax
2854 \else
2855 %Long enough!
2856 \xdef\qr@bestversion{\qr@version}%
2857 \xdef\qr@bestlevel{\qr@level}%
2858 \global\i=40%
2859 \fi
2860 \fi
2861 }%
2862 }%
2863 \edef\qr@version{\qr@bestversion}%
2864 \edef\qr@level{\qr@bestlevel}%
2865 \xa\ifnum\qr@desiredversion>0\relax
2866 \ifx\qr@bestversion\qr@desiredversion\relax
2867 %No change from desired version.
2868 \else
2869 %Version was increased
2870 \message{<Requested QR version '\qr@desiredversion' is too small for desired text.}%
2871 \message{Version increased to '\qr@bestversion' to fit text.>^^J}%
2872 \fi
2873 \fi
2874 \ifx\qr@bestlevel\qr@desiredlevel\relax
2875 %No change in level.
2876 \else
2877 \message{<Error-correction level increased from \qr@level@char{\qr@desiredlevel}}%
2878 \message{to \qr@level@char{\qr@bestlevel} at no cost.>^^J}%
2879 \fi
2880 }%
2881
2882 \def\qr@calculate@capacity#1#2{%
2883 \edef\qr@version{#1}%
2884 \edef\qr@level{#2}%
2885 %Calculate \qr@size, the number of modules per side.
2886 % The formula is 4\qr@version+17.
2887 \qr@a=\qr@version\relax%
2888 \multiply\qr@a by 4\relax%
2889 \advance\qr@a by 17\relax%
2890 \edef\qr@size{\the\qr@a}%
2891 %
2892 % Calculate \qr@k, which governs the number of alignment patterns.
2893 % The alignment patterns lie in a kxk square, except for 3 that are replaced by finding patterns.
2894 % The formula is 2 + floor( \qr@version / 7 ), except that k=0 for version 1.
2895 \xa\ifnum\qr@version=1\relax%
2896 \def\qr@k{0}%
2897 \else%
2898 \qr@a=\qr@version\relax
2899 \divide \qr@a by 7\relax
2900 \advance\qr@a by 2\relax
2901 \edef\qr@k{\the\qr@a}%
2902 \fi%
2903 %
2904 %Calculate number of function pattern modules.
2905 %This consists of the three 8x8 finder patterns, the two timing strips, and the (k^2-3) 5x5 alignment patterns.
2906 %The formula is 160+2n+25(k^2-3)-10(k-2), unless k=0 in which case we just have 160+2n.
2907 \qr@a=\qr@size\relax
2908 \multiply\qr@a by 2\relax
2909 \advance\qr@a by 160\relax
2910 \xa\ifnum\qr@k=0\relax\else
2911 %\qr@k is nonzero, hence at least 2, so we continue to add 25(k^2-3)-10(k-2).
2912 \qr@b=\qr@k\relax
2913 \multiply\qr@b by \qr@k\relax
2914 \advance\qr@b by -3\relax
2915 \multiply\qr@b by 25\relax
2916 \advance\qr@a by \qr@b\relax
2917 \qr@b=\qr@k\relax
2918 \advance\qr@b by -2\relax
2919 \multiply\qr@b by 10\relax
2920 \advance\qr@a by -\qr@b\relax
2921 \fi
2922 \edef\qr@numfunctionpatternmodules{\the\qr@a}%
2923 %
2924 %Calculate the number of version modules, either 36 or 0.
2925 \xa\ifnum\qr@version>6\relax
2926 \qr@versionmodulestrue
2927 \def\qr@numversionmodules{36}%
2928 \else
2929 \qr@versionmodulesfalse
2930 \def\qr@numversionmodules{0}%
2931 \fi
2932 %
2933 %Now calculate the codeword capacity and remainder bits.
2934 %Take n^2 modules, subtract all those dedicated to finder patterns etc., format information, and version information,
2935 %and what's left is the number of bits we can play with.
2936 %The number of complete bytes is \qr@numdatacodewords;
2937 %the leftover bits are \qr@numremainderbits.
2938 \qr@a=\qr@size\relax
2939 \multiply \qr@a by \qr@size\relax
2940 \advance \qr@a by -\qr@numfunctionpatternmodules\relax
2941 \advance \qr@a by -31\relax% % There are 31 format modules.
2942 \advance \qr@a by -\qr@numversionmodules\relax
2943 \qr@b=\qr@a\relax
2944 \divide \qr@a by 8\relax
2945 \edef\qr@numdatacodewords{\the\qr@a}%
2946 \multiply\qr@a by 8\relax
2947 \advance \qr@b by -\qr@a\relax
2948 \edef\qr@numremainderbits{\the\qr@b}%
2949 %
2950 %The size of the character count indicator also varies by version.
2951 %There are only two options, so hardcoding seems easier than expressing these functionally.
2952 \xa\ifnum\qr@version<10\relax
2953 \def\qr@charactercountbytes@byte{1}%
2954 \def\qr@charactercountbits@byte{8}%
2955 \else
2956 \def\qr@charactercountbytes@byte{2}%
2957 \def\qr@charactercountbits@byte{16}%
2958 \fi
2959 %
2960 %Now we call on the table, from the QR specification,
2961 %of how many blocks to divide the message into, and how many error bytes each block gets.
2962 %This affects the true capacity for data, which we store into \qr@totaldatacodewords.
2963 % The following macro sets \qr@numblocks and \qr@num@eccodewords
2964 % based on Table 9 of the QR specification.
2965 \qr@settableix
2966 \qr@a = -\qr@numblocks\relax
2967 \multiply \qr@a by \qr@num@eccodewords\relax
2968 \advance\qr@a by \qr@numdatacodewords\relax
2969 \edef\qr@totaldatacodewords{\the\qr@a}%
2970 \advance\qr@a by -\qr@charactercountbytes@byte\relax%Subtract character count
2971 \advance\qr@a by -1\relax% Subtract 1 byte for the 4-bit mode indicator and the 4-bit terminator at the end.
2972 \edef\qr@truecapacity{\the\qr@a}%
2973 }
2974
2975
2976 \def\qr@setversion#1#2{%
2977 % #1 = version number, an integer between 1 and 40 inclusive.
2978 % #2 = error-correction level, as an integer between 0 and 3 inclusive.
2979 % 0 = 00 = M
2980 % 1 = 01 = L
2981 % 2 = 10 = H
2982 % 3 = 11 = Q
2983 % This macro calculates and sets a variety of global macros and/or counters
2984 % storing version information that is used later in construction the QR code.
2985 % Thus \qr@setversion should be called every time!
2986 %
2987 \edef\qr@version{#1}%
2988 \edef\qr@level{#2}%
2989 %
2990 \qr@calculate@capacity{\qr@version}{\qr@level}%
2991 %The capacity-check code sets the following:
2992 % * \qr@size
2993 % * \qr@k
2994 % * \ifqr@versionmodules
2995 % * \qr@numversionmodules
2996 % * \qr@numdatacodewords
2997 % * \qr@numremainderbits
2998 % * \qr@charactercountbits@byte
2999 % * \qr@charactercountbytes@byte
3000 % * \qr@numblocks (via \qr@settableix)
3001 % * \qr@num@eccodewords (via \qr@settableix)
3002 % * \qr@totaldatacodewords
3003 %
3004 % The alignment patterns' square is 7 modules in from each edge.
3005 % They are spaced "as evenly as possible" with an even number of modules between each row/column,
3006 % unevenness in division being accommodated by making the first such gap smaller.
3007 % The formula seems to be
3008 % general distance = 2*round((n-13)/(k-1)/2+0.25)
3009 % = 2*floor((n-13)/(k-1)/2+0.75)
3010 % = 2*floor( (2*(n-13)/(k-1)+3) / 4 )
3011 % = (((2*(n-13)) div (k-1) + 3 ) div 4 ) * 2
3012 % first distance = leftovers
3013 % The 0.25 is to accommodate version 32, which is the only time we round down.
3014 % Otherwise a simple 2*ceiling((n-13)/(k-1)/2) would have sufficed.
3015 %
3016 \qr@a = \qr@size\relax
3017 \advance\qr@a by -13\relax
3018 \multiply\qr@a by 2\relax
3019 \qr@b = \qr@k\relax
3020 \advance \qr@b by -1\relax
3021 \divide\qr@a by \qr@b\relax
3022 \advance\qr@a by 3\relax
3023 \divide\qr@a by 4\relax
3024 \multiply\qr@a by 2\relax
3025 \edef\qr@alignment@generalskip{\the\qr@a}%
3026 %
3027 %Now set \qr@alignment@firstskip to (\qr@size-13)-(\qr@k-2)*\qr@alignment@generalskip %
3028 \qr@a = \qr@k\relax
3029 \advance\qr@a by -2\relax
3030 \multiply\qr@a by -\qr@alignment@generalskip\relax
3031 \advance\qr@a by \qr@size\relax
3032 \advance\qr@a by -13\relax
3033 \edef\qr@alignment@firstskip{\the\qr@a}%
3034 %
3035 %
3036 %
3037 % Our \qr@totaldatacodewords bytes of data are broken up as evenly as possible
3038 % into \qr@numblocks datablocks; some may be one byte longer than others.
3039 % We set \qr@shortblock@size to floor(\qr@totaldatacodewords / \qr@numblocks)
3040 % and \qr@numlongblocks to mod(\qr@totaldatacodewords , \qr@numblocks).
3041 \qr@a=\qr@totaldatacodewords\relax
3042 \divide\qr@a by \qr@numblocks\relax
3043 \edef\qr@shortblock@size{\the\qr@a}%
3044 \multiply\qr@a by -\qr@numblocks\relax
3045 \advance\qr@a by \qr@totaldatacodewords\relax
3046 \edef\qr@numlongblocks{\the\qr@a}%
3047 %
3048 %Set \qr@longblock@size to \qr@shortblock@size+1.
3049 \qr@a=\qr@shortblock@size\relax
3050 \advance\qr@a by 1\relax
3051 \edef\qr@longblock@size{\the\qr@a}%
3052 %
3053 %Set \qr@numshortblocks to \qr@numblocks - \qr@numlongblocks
3054 \qr@b=\qr@numblocks\relax
3055 \advance\qr@b by -\qr@numlongblocks\relax
3056 \edef\qr@numshortblocks{\the\qr@b}%
3057 }%
3058
3059 \def\qr@settableix@int(#1,#2){%
3060 \edef\qr@numblocks{#1}%
3061 \edef\qr@num@eccodewords{#2}%
3062 }%
3063
3064 \def\qr@settableix{%
3065 \xa\ifcase\qr@level\relax
3066 %00: Level 'M', medium error correction
3067 \edef\qr@tempdata{(%
3068 \ifcase\qr@version\relax
3069 \relax %There is no version 0.
3070 \or1,10%
3071 \or1,16%
3072 \or1,26%
3073 \or2,18%
3074 \or2,24%
3075 \or4,16%
3076 \or4,18%
3077 \or4,22%
3078 \or5,22%
3079 \or5,26%
3080 \or5,30%
3081 \or8,22%
3082 \or9,22%
3083 \or9,24%
3084 \or10,24%
3085 \or10,28%
3086 \or11,28%
3087 \or13,26%
3088 \or14,26%
3089 \or16,26%
3090 \or17,26%
3091 \or17,28%
3092 \or18,28%
3093 \or20,28%
3094 \or21,28%
3095 \or23,28%
3096 \or25,28%
3097 \or26,28%
3098 \or28,28%
3099 \or29,28%
3100 \or31,28%
3101 \or33,28%
3102 \or35,28%
3103 \or37,28%
3104 \or38,28%
3105 \or40,28%
3106 \or43,28%
3107 \or45,28%
3108 \or47,28%
3109 \or49,28%
3110 \fi)}%
3111 \or
3112 %01: Level 'L', low error correction
3113 \edef\qr@tempdata{%
3114 (\ifcase\qr@version\relax
3115 \relax %There is no version 0.
3116 \or 1,7%
3117 \or 1,10%
3118 \or 1,15%
3119 \or 1,20%
3120 \or 1,26%
3121 \or 2,18%
3122 \or 2,20%
3123 \or 2,24%
3124 \or 2,30%
3125 \or 4,18%
3126 \or 4,20%
3127 \or 4,24%
3128 \or 4,26%
3129 \or 4,30%
3130 \or 6,22%
3131 \or 6,24%
3132 \or 6,28%
3133 \or 6,30%
3134 \or 7,28%
3135 \or 8,28%
3136 \or 8,28%
3137 \or 9,28%
3138 \or 9,30%
3139 \or 10,30%
3140 \or 12,26%
3141 \or 12,28%
3142 \or 12,30%
3143 \or 13,30%
3144 \or 14,30%
3145 \or 15,30%
3146 \or 16,30%
3147 \or 17,30%
3148 \or 18,30%
3149 \or 19,30%
3150 \or 19,30%
3151 \or 20,30%
3152 \or 21,30%
3153 \or 22,30%
3154 \or 24,30%
3155 \or 25,30%
3156 \fi)}%
3157 \or
3158 %10: Level 'H', high error correction
3159 \edef\qr@tempdata{(%
3160 \ifcase\qr@version\relax
3161 \relax %There is no version 0.
3162 \or1,17%
3163 \or1,28%
3164 \or2,22%
3165 \or4,16%
3166 \or4,22%
3167 \or4,28%
3168 \or5,26%
3169 \or6,26%
3170 \or8,24%
3171 \or8,28%
3172 \or11,24%
3173 \or11,28%
3174 \or16,22%
3175 \or16,24%
3176 \or18,24%
3177 \or16,30%
3178 \or19,28%
3179 \or21,28%
3180 \or25,26%
3181 \or25,28%
3182 \or25,30%
3183 \or34,24%
3184 \or30,30%
3185 \or32,30%
3186 \or35,30%
3187 \or37,30%
3188 \or40,30%
3189 \or42,30%
3190 \or45,30%
3191 \or48,30%
3192 \or51,30%
3193 \or54,30%
3194 \or57,30%
3195 \or60,30%
3196 \or63,30%
3197 \or66,30%
3198 \or70,30%
3199 \or74,30%
3200 \or77,30%
3201 \or81,30%
3202 \fi)}%
3203 \or
3204 %11: Level 'Q', quality error correction
3205 \edef\qr@tempdata{(%
3206 \ifcase\qr@version\relax
3207 \relax %There is no version 0.
3208 \or1,13%
3209 \or1,22%
3210 \or2,18%
3211 \or2,26%
3212 \or4,18%
3213 \or4,24%
3214 \or6,18%
3215 \or6,22%
3216 \or8,20%
3217 \or8,24%
3218 \or8,28%
3219 \or10,26%
3220 \or12,24%
3221 \or16,20%
3222 \or12,30%
3223 \or17,24%
3224 \or16,28%
3225 \or18,28%
3226 \or21,26%
3227 \or20,30%
3228 \or23,28%
3229 \or23,30%
3230 \or25,30%
3231 \or27,30%
3232 \or29,30%
3233 \or34,28%
3234 \or34,30%
3235 \or35,30%
3236 \or38,30%
3237 \or40,30%
3238 \or43,30%
3239 \or45,30%
3240 \or48,30%
3241 \or51,30%
3242 \or53,30%
3243 \or56,30%
3244 \or59,30%
3245 \or62,30%
3246 \or65,30%
3247 \or68,30%
3248 \fi)}%
3249 \fi
3250 \xa\qr@settableix@int\qr@tempdata
3251 }%
3252 % \end{macrocode}
3253 % \subsection{More key handling}
3254 % \begin{macrocode}
3255 \define@key{qr}{version}{\edef\qr@desiredversion{#1}}%
3256 \define@key{qr}{level}{\qr@setlevel{#1}}%
3257 \define@key{qr}{height}{\qr@setheight{#1}}%
3258 \define@boolkey{qr}[qr@]{tight}[true]{}% %This creates \ifqr@tight and initializes it to true.
3259 \define@boolkey{qr}[qr@]{padding}[true]{\ifqr@padding\qr@tightfalse\else\qr@tighttrue\fi}% %Define 'padding' as antonym to 'tight'
3260
3261 \def\@qr@M{M}\def\@qr@z{0}%
3262 \def\@qr@L{L}\def\@qr@i{1}%
3263 \def\@qr@H{H}\def\@qr@ii{2}%
3264 \def\@qr@Q{Q}\def\@qr@iii{3}%
3265 \def\qr@setlevel#1{%
3266 \edef\qr@level@selected{#1}%
3267 \ifx\qr@level@selected\@qr@M
3268 \edef\qr@desiredlevel{0}%
3269 \fi
3270 \ifx\qr@level@selected\@qr@L
3271 \edef\qr@desiredlevel{1}%
3272 \fi
3273 \ifx\qr@level@selected\@qr@H
3274 \edef\qr@desiredlevel{2}%
3275 \fi
3276 \ifx\qr@level@selected\@qr@Q
3277 \edef\qr@desiredlevel{3}%
3278 \fi
3279 \ifx\qr@level@selected\@qr@z
3280 \edef\qr@desiredlevel{0}%
3281 \fi
3282 \ifx\qr@level@selected\@qr@i
3283 \edef\qr@desiredlevel{1}%
3284 \fi
3285 \ifx\qr@level@selected\@qr@ii
3286 \edef\qr@desiredlevel{2}%
3287 \fi
3288 \ifx\qr@level@selected\@qr@iii
3289 \edef\qr@desiredlevel{3}%
3290 \fi
3291 }%
3292
3293 \def\qr@setheight#1{%
3294 \setlength{\qr@desiredheight}{#1}%
3295 }%
3296
3297 \newcommand\qrset[1]{%
3298 \setkeys{qr}{#1}%
3299 }
3300
3301 %SET DEFAULTS
3302 \qrset{version=0, level=0, tight}
3303 % \end{macrocode}
3304 % \subsection{Main user functions}
3305 % At last we are ready to define the main user functions.
3306 % Some care is needed to ensure
3307 % that the \meta{text to be encoded} is read with the appropriate catcodes,
3308 % and that any escaped special characters are converted to the intended
3309 % characters with category code 12 (other).
3310 % \begin{macrocode}
3311 \newif\ifqr@starinvoked%
3312 \def\qrcode{\@ifstar\qrcode@star\qrcode@nostar}%
3313 \def\qrcode@star{\qr@starinvokedtrue\qrcode@i}%
3314 \def\qrcode@nostar{\qr@starinvokedfalse\qrcode@i}%
3315
3316 \newcommand\qrcode@i[1][]{%
3317 \begingroup%
3318 \ifqr@starinvoked%
3319 \qr@hyperlinkfalse%
3320 \fi%
3321 \setkeys{qr}{#1}%
3322 \bgroup\qr@verbatimcatcodes\qr@setescapedspecials\qrcode@in}%
3323
3324 \def\qrcode@in#1{\xdef\qr@texttoencode{#1}\egroup\qrcode@int\endgroup}%
3325
3326 \def\qrcode@hyperwrapper@hyperref{\href{\qr@texttoencode}}%
3327 \def\qrcode@hyperwrapper@nohyperref{\relax}%
3328
3329 \AtBeginDocument{%
3330 \@ifpackageloaded{hyperref}%
3331 {\global\let\qrcode@hyperwrapper=\qrcode@hyperwrapper@hyperref}%
3332 {\global\let\qrcode@hyperwrapper=\qrcode@hyperwrapper@nohyperref}%
3333 }%
3334
3335 \def\qrcode@int{%
3336 \message{^^J^^J<QR code requested for "\qr@texttoencode" in version
3337 \qr@desiredversion-\qr@level@char{\qr@desiredlevel}.>^^J}%
3338 %First, choose the version and level.
3339 %Recall that \qr@choose@best@version sets \qr@version and \qr@level.
3340 \xa\qr@choose@best@version\xa{\qr@texttoencode}%
3341 \qr@setversion{\qr@version}{\qr@level}%
3342 %
3343 \ifqr@hyperlink%
3344 \let\qrcode@wrapper=\qrcode@hyperwrapper%
3345 \else%
3346 \let\qrcode@wrapper=\relax%
3347 \fi%
3348 %
3349 %Next, check whether we have already encoded this text at this version
3350 %and level.
3351 \qrcode@wrapper{%
3352 \xa\ifx\csname qr@savedbinarymatrix@\qr@texttoencode @\qr@version @\qr@level\endcsname
3353 \relax%
3354 %This text has not yet been encoded.
3355 \qrcode@int@new%
3356 \else
3357 %This text has already been encoded!
3358 \ifqr@forget@mode
3359 %In 'forget' mode, we deliberately recalculate anyway.
3360 \qrcode@int@new%
3361 \else
3362 \qrcode@int@remember%
3363 \fi
3364 \fi%
3365 }%
3366 }%
3367
3368 \def\qrcode@int@new{%
3369 \qr@createsquareblankmatrix{newqr}{\qr@size}%
3370 \qr@placefinderpatterns{newqr}%
3371 \qr@placetimingpatterns{newqr}%
3372 \qr@placealignmentpatterns{newqr}%
3373 \qr@placedummyformatpatterns{newqr}%
3374 \qr@placedummyversionpatterns{newqr}%
3375 \ifqr@draft@mode
3376 \message{<Inserting dummy QR code in draft mode for "\qr@texttoencode" in
3377 version \qr@version-\qr@level@char{\qr@level}.>^^J}%
3378 \relax% Draft mode---don't load any data or do any work. Also don't save!
3379 \def\qr@format@square{\qr@black}%
3380 \def\qr@blank{\qr@white}%
3381 \fboxsep=-\fboxrule%
3382 \fbox{\qr@printmatrix{newqr}}%
3383 \else
3384 \message{<Calculating QR code for "\qr@texttoencode" in
3385 version \qr@version-\qr@level@char{\qr@level}.>^^J}%
3386 \xa\qr@encode@binary\xa{\qr@texttoencode}%
3387 \qr@splitcodetextintoblocks
3388 \qr@createerrorblocks
3389 \qr@interleave
3390 \message{<Writing data...}%
3391 \qr@writedata@hex{newqr}{\qr@interleaved@text}%
3392 \message{done.>^^J}%
3393 \qr@writeremainderbits{newqr}%
3394 \qr@chooseandapplybestmask{newqr}%
3395 \qr@decimaltobinary[2]{\qr@level@binary}{\qr@level}%
3396 \qr@decimaltobinary[3]{\qr@mask@binary}{\qr@mask@selected}%
3397 \edef\qr@formatstring{\qr@level@binary\qr@mask@binary}%
3398 \message{<Encoding and writing format string...}%
3399 \xa\qr@encodeandmaskformat\xa{\qr@formatstring}%
3400 \qr@writeformatstring{newqr}{\qr@format@bitstring}%
3401 \message{done.>^^J}%
3402 \message{<Encoding and writing version information...}%
3403 \qr@decimaltobinary[6]{\qr@version@binary}{\qr@version}%
3404 \qr@Golaycode{\qr@version@binary}%
3405 \qr@writeversionstring{newqr}{\qr@Golayresult}%
3406 \message{done.>^^J}%
3407 \message{<Saving QR code to memory...}%
3408 \qr@matrixtobinary{newqr}%
3409 %
3410 %Now save the binary version into TeX's memory for later use in this document.
3411 \xa\xdef\csname qr@savedbinarymatrix@\qr@texttoencode @\qr@version @\qr@level\endcsname
3412 {\qr@binarymatrix@result}%
3413 \message{done.>^^J}%
3414 %
3415 %Also save the binary version into the aux file, for use in later runs.
3416 \message{<Writing QR code to aux file...}%
3417 \qr@writebinarymatrixtoauxfile{\qr@binarymatrix@result}%
3418 \message{done.>^^J}%
3419 \message{<Printing matrix...}%
3420 \qr@printmatrix{newqr}%
3421 \message{done.>^^J}%
3422 \fi
3423 \message{^^J}%
3424 }%
3425 \def\qrcode@int@remember{%
3426 %This text has already been encoded,
3427 %so we just copy it from the saved binary string.
3428 \message{<Copying the QR code for "\qr@texttoencode" in version \qr@version-\qr@level@char{\qr@level} as previously calculated.>^^J}%
3429 \xa\qr@printsavedbinarymatrix\xa{\csname qr@savedbinarymatrix@\qr@texttoencode @\qr@version @\qr@level\endcsname}%
3430 %
3431 % Now this still might need to be written to the aux file.
3432 %
3433 \xa\ifx\csname qr@savedflag@\qr@texttoencode @\qr@version @\qr@level\endcsname\@qr@TRUE
3434 %Okay, this has already been written to aux file.
3435 %Do nothing.
3436 \relax%
3437 \else%
3438 %This has NOT been written to the aux file yet.
3439 %We need to do so now.
3440 \xa\qr@writebinarymatrixtoauxfile\xa{\csname qr@savedbinarymatrix@\qr@texttoencode @\qr@version @\qr@level\endcsname}%
3441 \fi%
3442 }%
3443
3444 \def\qr@matrixtobinary#1{%
3445 \def\qr@binarymatrix@result{}%
3446 \bgroup
3447 \def\qr@black{1}%
3448 \def\qr@white{0}%
3449 \def\qr@blank{0}%
3450 \def\qr@black@fixed{1}%
3451 \def\qr@white@fixed{0}%
3452 \def\qr@black@format{1}%
3453 \def\qr@white@format{0}%
3454 %
3455 \qr@for \i = 1 to \qr@size by 1%
3456 {\qr@for \j = 1 to \qr@size by 1%
3457 {\edef\qr@theentry{\qr@matrixentry{#1}{\the\i}{\the\j}}%
3458 \xa\g@addto@macro\xa\qr@binarymatrix@result\xa{\qr@theentry}%
3459 }%
3460 }%
3461 \egroup%
3462 }%
3463
3464 \def\qr@sanitize@output#1{%
3465 %Read through ASCII text '#1' and escape backslashes and braces
3466 \def\qr@sanitized@result{}%
3467 \edef\qr@argument{(#1\relax\relax\relax)}%
3468 \xa\qr@sanitize@output@int\qr@argument%
3469 }
3470
3471 \def\qr@sanitize@output@int(#1#2){%
3472 % #1 = first character
3473 % #2 = rest of output, including terminator
3474 \edef\qr@testi{#1}%
3475 \ifx\qr@testi\qr@relax
3476 % Done.
3477 \let\qr@next=\relax
3478 \else
3479 \ifx\qr@testi\qr@otherrightbrace
3480 \edef\qr@sanitized@result{\qr@sanitized@result\qr@otherbackslash}%
3481 \else\ifx\qr@testi\qr@otherleftbrace
3482 \edef\qr@sanitized@result{\qr@sanitized@result\qr@otherbackslash}%
3483 \else\ifx\qr@testi\qr@otherbackslash
3484 \edef\qr@sanitized@result{\qr@sanitized@result\qr@otherbackslash}%
3485 \fi
3486 \fi
3487 \fi
3488 \edef\qr@sanitized@result{\qr@sanitized@result#1}%
3489 \def\qr@next{\qr@sanitize@output@int(#2)}%
3490 \fi
3491 \qr@next
3492 }
3493
3494 \def\@qr@TRUE{TRUE}%
3495 \def\qr@writebinarymatrixtoauxfile#1{%
3496 \qr@sanitize@output{\qr@texttoencode}%
3497 \edef\qr@theargument{{\qr@sanitized@result}{\qr@version}{\qr@level}{#1}}%
3498 \xa\write\xa\@auxout\xa{\xa\string\xa\qr@savematrix\qr@theargument}%
3499 %
3500 % Now set a flag, so we don't write this again.
3501 \xa\gdef\csname qr@savedflag@\qr@texttoencode @\qr@version @\qr@level\endcsname{TRUE}%
3502 }%
3503
3504 \gdef\qr@dummyqrsavedefinition{}%
3505 \begingroup
3506 \catcode`\#=12\relax
3507 \catcode`\<=1\relax
3508 \catcode`\{=12\relax
3509 \catcode`\>=2\relax
3510 \catcode`\}=12\relax
3511 \catcode`\|=0\relax
3512 \catcode`\\=12|relax
3513 |gdef|qr@dummyqrsavedefinition<%
3514 \ifx\qr@savematrix\@undefined%
3515 \def\qr@savematrix{\begingroup\let\do\@makeother\dospecials\catcode`\{=1\catcode`\}=2\relax
3516 \qr@savematrix@int}%
3517 \def\qr@savematrix@int#1#2#3#4{\endgroup}%
3518 \fi%
3519 >
3520 |endgroup
3521
3522 \edef\qr@argument{(\qr@dummyqrsavedefinition)}%
3523 \xa\write\xa\@auxout\xa{\qr@dummyqrsavedefinition}%
3524
3525 \def\qr@savematrix{\bgroup\qr@verbatimcatcodes\qr@setescapedspecials\qr@savematrix@int}%
3526
3527 \def\qr@savematrix@int#1{\xdef\qr@savedmatrix@name{#1}\egroup\qr@savematrix@int@int}%
3528
3529 \def\qr@savematrix@int@int#1#2#3{%
3530 % \qr@savedmatrix@name = encoded text
3531 % #1 = version
3532 % #2 = level
3533 % #3 = binary text
3534 \def\ds{<Reading QR code for "\qr@savedmatrix@name" at level #1-\qr@level@char{#2} from aux file.>^^J}\xa\message\xa{\ds}%
3535 {\let\%=\qr@otherpercent
3536 \xa\gdef\csname qr@savedbinarymatrix@\qr@savedmatrix@name @#1@#2\endcsname{#3}%
3537 }%
3538 }%
3539 % \end{macrocode}
3540 %
3541 % \Finale
3542 \endinput