Debian Codegraph @

texmf-dist/tex/latex/qrcode/qrcode.sty

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