| 1 |
% This is macro package used by OpTeX, see http://petr.olsak.net/optex |
| 2 |
% qrcode.opm, Petr Olšák <[email protected]>, 2020 |
| 3 |
|
| 4 |
% See end of the file for more information |
| 5 |
|
| 6 |
\_codedecl \qrcode {Macro for QR code printing <2020-05-04>} |
| 7 |
|
| 8 |
\_namespace{qr} |
| 9 |
|
| 10 |
\_newcount\.i |
| 11 |
\_newcount\.j |
| 12 |
\_newcount\.a |
| 13 |
\_newcount\.b |
| 14 |
\_newcount\.c |
| 15 |
|
| 16 |
\_def\.relax{\_relax}% |
| 17 |
|
| 18 |
\_def\.utfstring#1{\_directlua{ |
| 19 |
local text="\_luaescapestring{#1}" |
| 20 |
tex.print(string.format('\_pcent.2x',string.len(text))) |
| 21 |
for i=1,string.len(text) do |
| 22 |
tex.print(string.format('\_pcent.2x',string.byte(text,i))) |
| 23 |
end |
| 24 |
}} |
| 25 |
\_def\.utfstringlen#1{\_directlua{ |
| 26 |
tex.print(string.len("\_luaescapestring{#1}")) |
| 27 |
}} |
| 28 |
|
| 29 |
\_def\.g_preface_macro#1#2{% |
| 30 |
% #1 = macro to be appended to |
| 31 |
% #2 = code to add |
| 32 |
\_edef\.codeA{#2}% |
| 33 |
\_ea\_ea\_ea\_gdef\_ea\_ea\_ea#1\_ea\_ea\_ea{\_ea\.codeA#1}% |
| 34 |
} |
| 35 |
|
| 36 |
\_def\.getstringlength#1{% |
| 37 |
\_xdef\.stringlength{\.utfstringlen{#1}}% |
| 38 |
}% |
| 39 |
|
| 40 |
\_def\.for#1=#2to#3by#4#{\.forA{#1}{#2}{#3}{#4}} |
| 41 |
\_long\_def\.forA#1#2#3#4#5{\_begingroup |
| 42 |
{\_escapechar=`\\ % allocation of #1 as counter: |
| 43 |
\_ea \_ifx\_csname for:\_string#1\_endcsname \_relax |
| 44 |
\_csname newcount\_ea\_endcsname \_csname for:\_string#1\_endcsname\_fi |
| 45 |
\_ea}\_ea\_let\_ea#1\_csname for:\_string#1\_endcsname |
| 46 |
#1=#2% |
| 47 |
\_def\.forB{#5\_advance#1by#4\_relax \_ea\.forC}% |
| 48 |
\_ifnum#4>0 \_def\.forC{\_ifnum#1>#3\_relax\_else\.forB\_fi}% |
| 49 |
\_else \_def\.forC{\_ifnum#1<#3\_relax\_else\.forB\_fi}% |
| 50 |
\_fi |
| 51 |
\_ifnum#4=0 \let\.forC=\_relax \_fi |
| 52 |
\.forC \_endgroup |
| 53 |
} |
| 54 |
|
| 55 |
\_def\.padatfront#1#2{% |
| 56 |
% #1 = macro containing text to pad |
| 57 |
% #2 = desired number of characters |
| 58 |
% Pads a number with initial zeros. |
| 59 |
\.getstringlength{#1}% |
| 60 |
\.a=\.stringlength\_relax |
| 61 |
\_advance\.a by 1\_relax |
| 62 |
\.for \.iz = \.a to #2 by 1 |
| 63 |
{\.g_preface_macro{#1}{0}}% |
| 64 |
} |
| 65 |
|
| 66 |
\.a=-1\_relax |
| 67 |
\_def\.savehexsymbols(#1#2){% |
| 68 |
\_advance\.a by 1\_relax% |
| 69 |
\_ea\_def\_csname _qr_hexchar:\_the\.a\_endcsname{#1}% |
| 70 |
\_ea\_edef\_csname _qr_hextodecimal:#1\_endcsname{\_the\.a}% |
| 71 |
\_ifnum\.a=15\_relax |
| 72 |
%Done. |
| 73 |
\_let\.next=\_relax |
| 74 |
\_else |
| 75 |
\_def\.next{\.savehexsymbols(#2)}% |
| 76 |
\_fi |
| 77 |
\.next |
| 78 |
}% |
| 79 |
\.savehexsymbols(0123456789abcdef\_relax\_relax)% |
| 80 |
|
| 81 |
\_def\.decimaltobase#1#2#3{% |
| 82 |
% #1 = macro to store result |
| 83 |
% #2 = decimal representation of a positive integer |
| 84 |
% #3 = new base |
| 85 |
\_bgroup |
| 86 |
\_edef\.newbase{#3}% |
| 87 |
\_gdef\.base_result{}% |
| 88 |
\.a=#2\_relax% |
| 89 |
\.decimaltobase_recursive% |
| 90 |
\_xdef#1{\.base_result}% |
| 91 |
\_egroup |
| 92 |
} |
| 93 |
\_def\.decimaltobase_recursive{% |
| 94 |
\.b=\.a |
| 95 |
\_divide\.b by \.newbase\_relax |
| 96 |
\_multiply\.b by -\.newbase\_relax |
| 97 |
\_advance\.b by \.a\_relax |
| 98 |
\_divide\.a by \.newbase\_relax |
| 99 |
\_ifnum\.b<10\_relax |
| 100 |
\_edef\.newdigit{\_the\.b}% |
| 101 |
\_else |
| 102 |
\_edef\.newdigit{\_csname _qr_hexchar:\_the\.b\_endcsname}% |
| 103 |
\_fi |
| 104 |
\_edef\.argument{{\_noexpand\.base_result}{\.newdigit}}% |
| 105 |
\_ea\.g_preface_macro\.argument |
| 106 |
\_ifnum\.a=0\_relax |
| 107 |
\_relax |
| 108 |
\_else |
| 109 |
\_ea\.decimaltobase_recursive |
| 110 |
\_fi |
| 111 |
} |
| 112 |
\_long\_def\.xaddto#1#2{\_xdef#1{#1#2}} |
| 113 |
|
| 114 |
\_def\.decimaltohex[#1]#2#3{% |
| 115 |
% #1 (opt.) = number of hex digits to create |
| 116 |
% #2 = macro to store result |
| 117 |
% #3 = decimal digits to convert |
| 118 |
\.decimaltobase{#2}{#3}{16}% |
| 119 |
\.padatfront{#2}{#1}% |
| 120 |
} |
| 121 |
\_def\.decimaltobinary[#1]#2#3{% |
| 122 |
% #1 (opt.) = number of bits to create |
| 123 |
% #2 = macro to store result |
| 124 |
% #3 = decimal digits to convert |
| 125 |
\.decimaltobase{#2}{#3}{2}% |
| 126 |
\.padatfront{#2}{#1}% |
| 127 |
} |
| 128 |
|
| 129 |
\.for \.iz = 0 to 15 by 1% |
| 130 |
{% |
| 131 |
\.decimaltohex[1]{\.hexchar}{\_the\.iz}% |
| 132 |
\.decimaltobinary[4]{\.bits}{\_the\.iz}% |
| 133 |
\_ea\_xdef\_csname _qr_b2h:\.bits\_endcsname{\.hexchar}% |
| 134 |
\_ea\_xdef\_csname _qr_h2b:\.hexchar\_endcsname{\.bits}% |
| 135 |
}% |
| 136 |
|
| 137 |
\_def\.binarytohex[#1]#2#3{% |
| 138 |
% #1 (optional) = # digits desired |
| 139 |
% #2 = macro to save to |
| 140 |
% #3 = binary string (must be multiple of 4 bits) |
| 141 |
\_def\.testi{#1}% |
| 142 |
\_ifx\.testi\.relax% |
| 143 |
%No argument specified |
| 144 |
\_def\.desireddigits{0}% |
| 145 |
\_else |
| 146 |
\_def\.desireddigits{#1}% |
| 147 |
\_fi |
| 148 |
\_gdef\.base_result{}% |
| 149 |
\_edef\.argument{(#3\_relax\_relax\_relax\_relax\_relax)}% |
| 150 |
\_ea\.binarytohex_int\.argument% |
| 151 |
\.padatfront{\.base_result}{\.desireddigits}% |
| 152 |
\_xdef#2{\.base_result}% |
| 153 |
} |
| 154 |
\_def\.binarytohex_int(#1#2#3#4#5){% |
| 155 |
% #1#2#3#4 = 4 bits |
| 156 |
% #5 = remainder, including \_relax\_relax\_relax\_relax\_relax terminator |
| 157 |
\_def\.testi{#1}% |
| 158 |
\_ifx\.testi\.relax% |
| 159 |
%Done. |
| 160 |
\_def\.next{\_relax}% |
| 161 |
\_else% |
| 162 |
\_xdef\.base_result{\.base_result\_csname _qr_b2h:#1#2#3#4\_endcsname}% |
| 163 |
\_def\.next{\.binarytohex_int(#5)}% |
| 164 |
\_fi% |
| 165 |
\.next% |
| 166 |
} |
| 167 |
|
| 168 |
\_def\.hextobinary[#1]#2#3{% |
| 169 |
% #1 (optional) = # bits desired |
| 170 |
% #2 = macro to save to |
| 171 |
% #3 = hexadecimal string |
| 172 |
\_bgroup |
| 173 |
\_def\.testi{#1}% |
| 174 |
\_ifx\.testi\.relax |
| 175 |
%No argument specified |
| 176 |
\_def\.desireddigits{0}% |
| 177 |
\_else |
| 178 |
\_def\.desireddigits{#1}% |
| 179 |
\_fi |
| 180 |
\_gdef\.base_result{}% |
| 181 |
\_edef\.argument{(#3\_relax\_relax)}% |
| 182 |
\_ea\.hextobinary_int\.argument% |
| 183 |
\.padatfront{\.base_result}{\.desireddigits}% |
| 184 |
\_xdef#2{\.base_result}% |
| 185 |
\_egroup |
| 186 |
} |
| 187 |
\_def\.hextobinary_int(#1#2){% |
| 188 |
% #1 = hexadecimal character |
| 189 |
% #2 = remainder, including \_relax\_relax terminator |
| 190 |
\_def\.testii{#1}% |
| 191 |
\_ifx\.testii\.relax |
| 192 |
%Done. |
| 193 |
\_def\.next{\_relax}% |
| 194 |
\_else |
| 195 |
\_xdef\.base_result{\.base_result\_csname _qr_h2b:#1\_endcsname}% |
| 196 |
\_def\.next{\.hextobinary_int(#2)}% |
| 197 |
\_fi |
| 198 |
\.next |
| 199 |
} |
| 200 |
|
| 201 |
\_def\.hextodecimal#1#2{% |
| 202 |
\_edef\.argument{#2}% |
| 203 |
\_ea\.a\_ea=\_ea\_number\_ea"\.argument\_relax |
| 204 |
\_edef#1{\_the\.a}% |
| 205 |
} |
| 206 |
|
| 207 |
\_def\.hextodecimal#1#2{% |
| 208 |
% #1 = macro to store result |
| 209 |
% #2 = hexadecimal representation of a positive integer |
| 210 |
\_bgroup |
| 211 |
\.a=0\_relax |
| 212 |
\_edef\.argument{(#2\_relax)}% |
| 213 |
\_ea\.hextodecimal_recursive\.argument% |
| 214 |
\_xdef#1{\_the\.a}% |
| 215 |
\_egroup |
| 216 |
} |
| 217 |
\_def\.hextodecimal_recursive(#1#2){% |
| 218 |
% #1 = first hex char |
| 219 |
% #2 = remainder |
| 220 |
\_advance \.a by \_csname _qr_hextodecimal:#1\_endcsname\_relax% |
| 221 |
\_edef\.testii{#2}% |
| 222 |
\_ifx\.testii\.relax% |
| 223 |
%Done. |
| 224 |
\_let\.next=\_relax% |
| 225 |
\_else |
| 226 |
%There's at least one more digit. |
| 227 |
\_multiply\.a by 16\_relax |
| 228 |
\_edef\.next{\_noexpand\.hextodecimal_recursive(#2)}% |
| 229 |
\_fi% |
| 230 |
\.next% |
| 231 |
} |
| 232 |
|
| 233 |
\_def\.storetomatrix#1#2#3#4{% |
| 234 |
% #1 = matrix name |
| 235 |
% #2 = row number |
| 236 |
% #3 = column number |
| 237 |
% #4 = value of matrix entry |
| 238 |
\_ea\_gdef\_csname #1@#2@#3\_endcsname{#4}% |
| 239 |
}% |
| 240 |
|
| 241 |
\_def\.estoretomatrix#1#2#3#4{% |
| 242 |
% This version performs exactly one expansion on #4. |
| 243 |
% #1 = matrix name |
| 244 |
% #2 = row number |
| 245 |
% #3 = column number |
| 246 |
% #4 = value of matrix |
| 247 |
\_ea\_gdef\_csname #1@#2@#3\_ea\_endcsname\_ea{#4}% |
| 248 |
}% |
| 249 |
|
| 250 |
\_def\.matrixentry#1#2#3{% |
| 251 |
% #1 = matrix name |
| 252 |
% #2 = row number |
| 253 |
% #3 = column number |
| 254 |
\_csname #1@#2@#3\_endcsname |
| 255 |
}% |
| 256 |
|
| 257 |
\_def\.createsquareblankmatrix#1#2{% |
| 258 |
% \.creatematrix{#1}% |
| 259 |
\_ea\_gdef\_csname #1@numrows\_endcsname{#2}% |
| 260 |
\_ea\_gdef\_csname #1@numcols\_endcsname{#2}% |
| 261 |
\.for \.iz = 1 to #2 by 1% |
| 262 |
{\.for \.jz = 1 to #2 by 1% |
| 263 |
{\.storetomatrix{#1}{\_the\.iz}{\_the\.jz}{\.blank}}}% |
| 264 |
}% |
| 265 |
|
| 266 |
\_def\.numberofrowsinmatrix#1{% |
| 267 |
\_csname #1@numrows\_endcsname% |
| 268 |
}% |
| 269 |
|
| 270 |
\_def\.numberofcolsinmatrix#1{% |
| 271 |
\_csname #1@numcols\_endcsname% |
| 272 |
}% |
| 273 |
|
| 274 |
\_def\.setnumberofrows#1#2{% |
| 275 |
\_ea\_xdef\_csname #1@numrows\_endcsname{#2}% |
| 276 |
}% |
| 277 |
|
| 278 |
\_def\.setnumberofcols#1#2{% |
| 279 |
\_ea\_xdef\_csname #1@numcols\_endcsname{#2}% |
| 280 |
}% |
| 281 |
|
| 282 |
\_newdimen\.desiredheight |
| 283 |
\_newdimen\.modulesize |
| 284 |
|
| 285 |
\_def\.link#1#2{\_hbox{\_pdfstartlink height\_ht0 depth0pt \.border |
| 286 |
user{/Subtype/Link/A <</Type/Action/S/URI/URI(#1)>>}\_relax #2\_pdfendlink}% |
| 287 |
} |
| 288 |
\_def\.border{% |
| 289 |
\_ifcsname _qr_kv:qrborder\_endcsname attr{/C[\.kv{qrborder}] /Border[0 0 .6]}% |
| 290 |
\_else attr{/Border[0 0 0]}% |
| 291 |
\_fi |
| 292 |
} |
| 293 |
|
| 294 |
\_def\.createliteralmatrix#1#2#3{% |
| 295 |
% #1 = matrix name |
| 296 |
% #2 = m, the number of rows and columns in the square matrix |
| 297 |
% #3 = a string of m^2 tokens to be written into the matrix |
| 298 |
% \.creatematrix{#1}% |
| 299 |
\_ea\_xdef\_csname #1@numrows\_endcsname{#2}% |
| 300 |
\_ea\_xdef\_csname #1@numcols\_endcsname{#2}% |
| 301 |
\_gdef\.literalmatrix_tokens{#3}% |
| 302 |
\.for \.iz = 1 to #2 by 1% |
| 303 |
{\.for \.jz = 1 to #2 by 1% |
| 304 |
{\_ea\.createliteralmatrix_int\_ea(\.literalmatrix_tokens)% |
| 305 |
\.estoretomatrix{#1}{\_the\.iz}{\_the\.jz}{\.entrytext}% |
| 306 |
}% |
| 307 |
}% |
| 308 |
} |
| 309 |
\_def\.createliteralmatrix_int(#1#2){% |
| 310 |
\_def\.entrytext{#1}% |
| 311 |
\_gdef\.literalmatrix_tokens{#2}% |
| 312 |
} |
| 313 |
|
| 314 |
\.createliteralmatrix{finderpattern}{8}{% |
| 315 |
\.blackfixed\.blackfixed\.blackfixed\.blackfixed\.blackfixed\.blackfixed\.blackfixed\.whitefixed |
| 316 |
\.blackfixed\.whitefixed\.whitefixed\.whitefixed\.whitefixed\.whitefixed\.blackfixed\.whitefixed |
| 317 |
\.blackfixed\.whitefixed\.blackfixed\.blackfixed\.blackfixed\.whitefixed\.blackfixed\.whitefixed |
| 318 |
\.blackfixed\.whitefixed\.blackfixed\.blackfixed\.blackfixed\.whitefixed\.blackfixed\.whitefixed |
| 319 |
\.blackfixed\.whitefixed\.blackfixed\.blackfixed\.blackfixed\.whitefixed\.blackfixed\.whitefixed |
| 320 |
\.blackfixed\.whitefixed\.whitefixed\.whitefixed\.whitefixed\.whitefixed\.blackfixed\.whitefixed |
| 321 |
\.blackfixed\.blackfixed\.blackfixed\.blackfixed\.blackfixed\.blackfixed\.blackfixed\.whitefixed |
| 322 |
\.whitefixed\.whitefixed\.whitefixed\.whitefixed\.whitefixed\.whitefixed\.whitefixed\.whitefixed |
| 323 |
}% |
| 324 |
|
| 325 |
\.createliteralmatrix{alignmentpattern}{5}{% |
| 326 |
\.blackfixed\.blackfixed\.blackfixed\.blackfixed\.blackfixed |
| 327 |
\.blackfixed\.whitefixed\.whitefixed\.whitefixed\.blackfixed |
| 328 |
\.blackfixed\.whitefixed\.blackfixed\.whitefixed\.blackfixed |
| 329 |
\.blackfixed\.whitefixed\.whitefixed\.whitefixed\.blackfixed |
| 330 |
\.blackfixed\.blackfixed\.blackfixed\.blackfixed\.blackfixed |
| 331 |
}% |
| 332 |
|
| 333 |
\_def\.copymatrixentry#1#2#3#4#5#6{% |
| 334 |
% Copy the (#2,#3) entry of matrix #1 |
| 335 |
% to the (#5,#6) position of matrix #4. |
| 336 |
\_ea\_ea\_ea\_global% |
| 337 |
\_ea\_ea\_ea\_let\_ea\_ea\_csname #4@#5@#6\_endcsname |
| 338 |
\_csname #1@#2@#3\_endcsname |
| 339 |
}% |
| 340 |
|
| 341 |
\_def\.createduplicatematrix#1#2{% |
| 342 |
% #1 = name of copy |
| 343 |
% #2 = original matrix to be copied |
| 344 |
% \.creatematrix{#1}% |
| 345 |
\.for \.iz = 1 to \.numberofrowsinmatrix{#2} by 1% |
| 346 |
{\.for \.jz = 1 to \.numberofcolsinmatrix{#2} by 1% |
| 347 |
{\.copymatrixentry{#2}{\_the\.iz}{\_the\.jz}{#1}{\_the\.iz}{\_the\.jz}% |
| 348 |
}% |
| 349 |
}% |
| 350 |
\.setnumberofrows{#1}{\.numberofrowsinmatrix{#2}}% |
| 351 |
\.setnumberofcols{#1}{\.numberofcolsinmatrix{#2}}% |
| 352 |
}% |
| 353 |
|
| 354 |
\_def\.placefinderpattern_int#1#2#3#4#5{% |
| 355 |
% Work on matrix #1. |
| 356 |
% Start in position (#2, #3) -- should be a corner |
| 357 |
% #4 indicates horizontal direction (1=right, -1=left) |
| 358 |
% #5 indicates vertical direction (1=down, -1=up) |
| 359 |
% |
| 360 |
% In this code, \sourcei and \sourcej are TeX counts working through the finderpattern matrix, |
| 361 |
% and i and j are counters indicating positions in the big matrix. |
| 362 |
\.setcounter\.i{#2}% |
| 363 |
\.for \.sourcei=1 to 8 by 1% |
| 364 |
{\.setcounter\.j{#3}% |
| 365 |
\.for \.sourcej=1 to 8 by 1% |
| 366 |
{\.copymatrixentry{finderpattern}{\_the\.sourcei}{\_the\.sourcej}% |
| 367 |
{#1}{\_the\.i}{\_the\.j}% |
| 368 |
\.addtocounter\.j{#5}% |
| 369 |
}% |
| 370 |
\.addtocounter\.i{#4}% |
| 371 |
}% |
| 372 |
}% |
| 373 |
|
| 374 |
\_def\.placefinderpatterns#1{% |
| 375 |
% #1=matrix name |
| 376 |
\.placefinderpattern_int{#1}{1}{1}{1}{1}% |
| 377 |
\.placefinderpattern_int{#1}{\.numberofrowsinmatrix{#1}}{1}{-1}{1}% |
| 378 |
\.placefinderpattern_int{#1}{1}{\.numberofcolsinmatrix{#1}}{1}{-1}% |
| 379 |
}% |
| 380 |
|
| 381 |
\_def\.placetimingpatterns#1{% |
| 382 |
%Set \.endingcol to n-8. |
| 383 |
\.a=\.size\_relax% |
| 384 |
\_advance\.a by -8\_relax% |
| 385 |
\_edef\.endingcol{\_the\.a}% |
| 386 |
\.for \.jz = 9 to \.endingcol by 1% |
| 387 |
{\_ifodd\.jz\_relax% |
| 388 |
\.storetomatrix{#1}{7}{\_the\.jz}{\.blackfixed}% |
| 389 |
\.storetomatrix{#1}{\_the\.jz}{7}{\.blackfixed}% |
| 390 |
\_else% |
| 391 |
\.storetomatrix{#1}{7}{\_the\.jz}{\.whitefixed}% |
| 392 |
\.storetomatrix{#1}{\_the\.jz}{7}{\.whitefixed}% |
| 393 |
\_fi% |
| 394 |
}% |
| 395 |
}% |
| 396 |
|
| 397 |
\_def\.placealignmentpattern_int#1#2#3{% |
| 398 |
% Work on matrix #1. |
| 399 |
% Write an alignment pattern into the matrix, centered on (#2,#3). |
| 400 |
\.a=#2\_relax% |
| 401 |
\_advance\.a by -2\_relax% |
| 402 |
\.b=#3\_relax% |
| 403 |
\_advance\.b by -2\_relax% |
| 404 |
\.setcounter\.i{\_the\.a}% |
| 405 |
\.for \.iz=1 to 5 by 1% |
| 406 |
{\.setcounter\.j{\_the\.b}% |
| 407 |
\.for \.jz=1 to 5 by 1% |
| 408 |
{\.copymatrixentry{alignmentpattern}{\_the\.iz}{\_the\.jz}% |
| 409 |
{#1}{\_the\.i}{\_the\.j}% |
| 410 |
\.stepcounter\.j |
| 411 |
}% |
| 412 |
\.stepcounter\.i |
| 413 |
}% |
| 414 |
}% |
| 415 |
|
| 416 |
\_newifi\_ifqr_incorner% |
| 417 |
\_def\.placealignmentpatterns#1{% |
| 418 |
%There are k^2-3 alignment patterns, |
| 419 |
%arranged in a (k x k) grid within the matrix. |
| 420 |
%They begin in row 7, column 7, |
| 421 |
%except that the ones in the NW, NE, and SW corners |
| 422 |
%are omitted because of the finder patterns. |
| 423 |
%Recall that |
| 424 |
% * \.k stores k, |
| 425 |
% * \.alignment_firstskip stores how far between the 1st and 2nd row/col, & |
| 426 |
% * \.alignment_generalskip stores how far between each subsequent row/col. |
| 427 |
\_ea\_ifnum\.k>0\_relax |
| 428 |
%There will be at least one alignment pattern. |
| 429 |
%N.B. k cannot equal 1. |
| 430 |
\_ea\_ifnum\.k=2\_relax |
| 431 |
% 2*2-3 = exactly 1 alignment pattern. |
| 432 |
\.a=7\_relax |
| 433 |
\_advance\.a by \.alignment_firstskip\_relax |
| 434 |
\_xdef\.targetii{\_the\.a}% |
| 435 |
\.placealignmentpattern_int{#1}{\.targetii}{\.targetii}% |
| 436 |
\_else |
| 437 |
% k is at least 3, so the following loops should be safe. |
| 438 |
\_xdef\.targetii{7}% |
| 439 |
\.for \.ii = 1 to \.k by 1% |
| 440 |
{\_ifcase\.ii\_relax% |
| 441 |
\_relax% \.ii should never equal 0. |
| 442 |
\_or |
| 443 |
\_xdef\.targetii{7}% If \.ii = 1, we start in row 7. |
| 444 |
\_or |
| 445 |
%If \.ii = 2, we add the firstskip. |
| 446 |
\.a=\.targetii\_relax% |
| 447 |
\_advance\.a by \.alignment_firstskip\_relax% |
| 448 |
\_xdef\.targetii{\_the\.a}% |
| 449 |
\_else |
| 450 |
%If \.ii>2, we add the generalskip. |
| 451 |
\.a=\.targetii\_relax% |
| 452 |
\_advance\.a by \.alignment_generalskip\_relax% |
| 453 |
\_xdef\.targetii{\_the\.a}% |
| 454 |
\_fi |
| 455 |
\.for \.jj = 1 to \.k by 1% |
| 456 |
{\_ifcase\.jj\_relax% |
| 457 |
\_relax% \.jj should never equal 0. |
| 458 |
\_or |
| 459 |
\_xdef\.targetjj{7}% If \.jj=1, we start in row 7. |
| 460 |
\_or |
| 461 |
%If \.jj=2, we add the firstskip. |
| 462 |
\.a=\.targetjj\_relax% |
| 463 |
\_advance\.a by \.alignment_firstskip% |
| 464 |
\_xdef\.targetjj{\_the\.a}% |
| 465 |
\_else |
| 466 |
%If \.jj>2, we add the generalskip. |
| 467 |
\.a=\.targetjj\_relax% |
| 468 |
\_advance\.a by \.alignment_generalskip% |
| 469 |
\_xdef\.targetjj{\_the\.a}% |
| 470 |
\_fi |
| 471 |
\.incornerfalse% |
| 472 |
\_ifnum\.ii=1\_relax |
| 473 |
\_ifnum\.jj=1\_relax |
| 474 |
\.incornertrue |
| 475 |
\_else |
| 476 |
\_ifnum\.k=\.jj\_relax |
| 477 |
\.incornertrue |
| 478 |
\_fi |
| 479 |
\_fi |
| 480 |
\_else |
| 481 |
\_ea\_ifnum\.k=\.ii\_relax |
| 482 |
\_ifnum\.jj=1\_relax |
| 483 |
\.incornertrue |
| 484 |
\_fi |
| 485 |
\_fi |
| 486 |
\_fi |
| 487 |
\_ifqr_incorner |
| 488 |
\_relax |
| 489 |
\_else |
| 490 |
\.placealignmentpattern_int{#1}{\.targetii}{\.targetjj}% |
| 491 |
\_fi |
| 492 |
}% ends \.for \.jj |
| 493 |
}% ends \.for \.ii |
| 494 |
\_fi |
| 495 |
\_fi |
| 496 |
}% |
| 497 |
|
| 498 |
\_def\.placedummyformatpatterns#1{% |
| 499 |
\.for \.jz = 1 to 9 by 1% |
| 500 |
{\_ifnum\.jz=7\_relax% |
| 501 |
\_else% |
| 502 |
\.storetomatrix{#1}{9}{\_the\.jz}{\.formatsquare}% |
| 503 |
\.storetomatrix{#1}{\_the\.jz}{9}{\.formatsquare}% |
| 504 |
\_fi% |
| 505 |
}% |
| 506 |
\.setcounter\.j{\.size}% |
| 507 |
\.for \.jz = 1 to 8 by 1% |
| 508 |
{\.storetomatrix{#1}{9}{\_the\.j}{\.formatsquare}% |
| 509 |
\.storetomatrix{#1}{\_the\.j}{9}{\.formatsquare}% |
| 510 |
\.addtocounter\.j{-1}% |
| 511 |
}% |
| 512 |
%Now go back and change the \.formatsquare in (n-8,9) to \.blackfixed. |
| 513 |
\.addtocounter\.j{1}% |
| 514 |
\.storetomatrix{#1}{\_the\.j}{9}{\.blackfixed}% |
| 515 |
}% |
| 516 |
|
| 517 |
\_def\.placedummyversionpatterns#1{% |
| 518 |
\_ea\_ifnum\.version>6\_relax |
| 519 |
%Must include version information. |
| 520 |
\_global\.i=\.size% |
| 521 |
\_global\_advance\.i by -10\_relax% |
| 522 |
\.for \.iz = 1 to 3 by 1% |
| 523 |
{\.for \.jz = 1 to 6 by 1% |
| 524 |
{\.storetomatrix{#1}{\_the\.i}{\_the\.jz}{\.formatsquare}% |
| 525 |
\.storetomatrix{#1}{\_the\.jz}{\_the\.i}{\.formatsquare}% |
| 526 |
}% |
| 527 |
\.stepcounter\.i |
| 528 |
}% |
| 529 |
\_fi |
| 530 |
}% |
| 531 |
|
| 532 |
\_def\.writebit(#1#2)#3{% |
| 533 |
% #3 = matrix name |
| 534 |
% (qr_i,qr_j) = position to write in (counters) |
| 535 |
% #1 = bit to be written |
| 536 |
% #2 = remaining bits plus '\_relax' as an end-of-file marker |
| 537 |
\_edef\.datatowrite{#2}% |
| 538 |
\_ifnum#1=1 |
| 539 |
\.storetomatrix{#3}{\_the\.i}{\_the\.j}{\.black}% |
| 540 |
\_else |
| 541 |
\.storetomatrix{#3}{\_the\.i}{\_the\.j}{\.white}% |
| 542 |
\_fi |
| 543 |
}% |
| 544 |
|
| 545 |
\_newifi\_ifqr_rightcol |
| 546 |
\_newifi\_ifqr_goingup |
| 547 |
|
| 548 |
\_def\.writedata_hex#1#2{% |
| 549 |
% #1 = name of a matrix that has been prepared with finder patterns, timing patterns, etc. |
| 550 |
% #2 = a string consisting of bytes to write into the matrix, in two-char hex format. |
| 551 |
\.setcounter\.i{\.numberofrowsinmatrix{#1}}% |
| 552 |
\.setcounter\.j{\.numberofcolsinmatrix{#1}}% |
| 553 |
\.rightcoltrue |
| 554 |
\.goinguptrue |
| 555 |
\_edef\.argument{{#1}(#2\_relax\_relax\_relax)}% |
| 556 |
\_ea\.writedata_hex_recursive\.argument% |
| 557 |
}% |
| 558 |
|
| 559 |
\_def\.writedata_hex_recursive#1(#2#3#4){% |
| 560 |
% #1 = name of a matrix that has been prepared with finder patterns, timing patterns, etc. |
| 561 |
% (qr_i,qr_j) = position to write in counters |
| 562 |
% #2#3#4 contains the hex codes of the bytes to be written, plus \_relax\_relax\_relax |
| 563 |
% as an end-of-file marker |
| 564 |
\_edef\.testii{#2}% |
| 565 |
\_ifx\.testii\.relax% |
| 566 |
% #2 is \_relax, so there is nothing more to write. |
| 567 |
\_relax |
| 568 |
\_let\.go=\_relax |
| 569 |
\_else |
| 570 |
% #2 is not \_relax, so there is another byte to write. |
| 571 |
\.hextobinary[8]{\.bytetowrite}{#2#3}% |
| 572 |
\_xdef\.datatowrite{\.bytetowrite\_relax}% %Add terminating "\_relax" |
| 573 |
\.writedata_recursive{#1}% %This function actually writes the 8 bits. |
| 574 |
\_edef\.argument{{#1}(#4)}% |
| 575 |
\_ea\_def\_ea\.go\_ea{\_ea\.writedata_hex_recursive\.argument}% %Call self to write the next bit. |
| 576 |
\_fi |
| 577 |
\.go |
| 578 |
}% |
| 579 |
|
| 580 |
\_def\.writedata#1#2{% |
| 581 |
% #1 = name of a matrix that has been prepared with finder patterns, timing patterns, etc. |
| 582 |
% #2 = a string consisting of 0's and 1's to write into the matrix. |
| 583 |
\.setcounter\.i{\.numberofrowsinmatrix{#1}}% |
| 584 |
\.setcounter\.j{\.numberofcolsinmatrix{#1}}% |
| 585 |
\.rightcoltrue |
| 586 |
\.goinguptrue |
| 587 |
\_edef\.datatowrite{#2\_relax}% |
| 588 |
\.writedata_recursive{#1}% |
| 589 |
}% |
| 590 |
|
| 591 |
\_def\.iblank{\.blank}% |
| 592 |
|
| 593 |
\_def\.writedata_recursive#1{% |
| 594 |
% #1 = matrix name |
| 595 |
% (qr_i,qr_j) = position to write in (counters) |
| 596 |
% \.datatowrite contains the bits to be written, plus '\_relax' as an end-of-file marker |
| 597 |
\_ea\_let\_ea\.squarevalue\_csname #1@\_the\.i @\_the\.j\_endcsname% |
| 598 |
\_ifx\.squarevalue\.iblank |
| 599 |
%Square is blank, so write data in it. |
| 600 |
\_ea\.writebit\_ea(\.datatowrite){#1}% |
| 601 |
%The \.writebit macro not only writes the first bit of \.datatowrite into the matrix, |
| 602 |
%but also removes the bit from the 'bitstream' of \.datatowrite. |
| 603 |
\_fi |
| 604 |
%Now adjust our position in the matrix. |
| 605 |
\_ifqr_rightcol |
| 606 |
%From the right-hand half of the two-bit column, we always move left. Easy peasy. |
| 607 |
\.addtocounter\.j{-1}% |
| 608 |
\.rightcolfalse |
| 609 |
\_else |
| 610 |
%If we're in the left-hand column, things are harder. |
| 611 |
\_ifqr_goingup |
| 612 |
%First, suppose we're going upwards. |
| 613 |
\_ifnum\.i>1\_relax% |
| 614 |
%If we're not in the first row, things are easy. |
| 615 |
%We move one to the right and one up. |
| 616 |
\.addtocounter\.j{1}% |
| 617 |
\.addtocounter\.i{-1}% |
| 618 |
\.rightcoltrue |
| 619 |
\_else |
| 620 |
%If we are in the first row, then we move to the left, |
| 621 |
%and we are now in the right-hand column on a downward pass. |
| 622 |
\.addtocounter\.j{-1}% |
| 623 |
\.goingupfalse |
| 624 |
\.rightcoltrue |
| 625 |
\_fi |
| 626 |
\_else |
| 627 |
%Now, suppose we're going downwards. |
| 628 |
\_ea\_ifnum\.size>\.i\_relax% |
| 629 |
%If we're not yet in the bottom row, things are easy. |
| 630 |
%We move one to the right and one down. |
| 631 |
\.addtocounter\.j{1}% |
| 632 |
\.addtocounter\.i{1}% |
| 633 |
\.rightcoltrue |
| 634 |
\_else |
| 635 |
%If we are in the bottom row, then we move to the left, |
| 636 |
%and we are now in the right-hand column on an upward pass. |
| 637 |
\.addtocounter\.j{-1}% |
| 638 |
\.rightcoltrue |
| 639 |
\.goinguptrue |
| 640 |
\_fi |
| 641 |
\_fi |
| 642 |
%One problem: what if we just moved into the 7th column? |
| 643 |
%Das ist verboten. |
| 644 |
%If we just moved (left) into the 7th column, we should move on into the 6th column. |
| 645 |
\_ifnum\.j=7\_relax% |
| 646 |
\.setcounter\.j{6}% |
| 647 |
\_fi |
| 648 |
\_fi |
| 649 |
%Now check whether there are any more bits to write. |
| 650 |
\_ifx\.datatowrite\.relax |
| 651 |
% \.datatowrite is just `\_relax', so we're done. |
| 652 |
\_let\.nexttoken=\_relax |
| 653 |
\_relax |
| 654 |
\_else |
| 655 |
% Write some more! |
| 656 |
\_def\.nexttoken{\.writedata_recursive{#1}}% |
| 657 |
\_fi |
| 658 |
\.nexttoken |
| 659 |
}% |
| 660 |
|
| 661 |
\_def\.writeremainderbits#1{% |
| 662 |
% #1 = name of a matrix that has been prepared and partly filled. |
| 663 |
% (qr_i,qr_j) = position to write in counters |
| 664 |
\_ea\_ifnum\.numremainderbits>0\_relax |
| 665 |
\_def\.datatowrite{}% |
| 666 |
\.for \.iz = 1 to \.numremainderbits by 1% |
| 667 |
{\.xaddto{\.datatowrite}{0}}% |
| 668 |
\.xaddto{\.datatowrite}{\_relax}% terminator |
| 669 |
\.writedata_recursive{#1}% |
| 670 |
\_fi |
| 671 |
}% |
| 672 |
|
| 673 |
\_newifi\_ifqr_cellinmask |
| 674 |
|
| 675 |
\_def\.setmaskingfunction#1{% |
| 676 |
% #1 = 1 decimal digit for the mask. (I see no reason to use the 3-bit binary code.) |
| 677 |
% The current position is (\themaski,\themaskj), with indexing starting at 0. |
| 678 |
\_edef\.maskselection{#1}% |
| 679 |
\_ea\_ifcase\.maskselection\_relax |
| 680 |
%Case 0: checkerboard |
| 681 |
\_def\.parsemaskingfunction{% |
| 682 |
% Compute mod(\themaski+\themaskj,2)% |
| 683 |
\.a=\.maski% |
| 684 |
\_advance\.a by \.maskj% |
| 685 |
\.b=\.a% |
| 686 |
\_divide\.b by 2% |
| 687 |
\_multiply\.b by 2% |
| 688 |
\_advance\.a by -\.b% |
| 689 |
\_edef\.maskfunctionresult{\_the\.a}% |
| 690 |
}% |
| 691 |
\_or |
| 692 |
%Case 1: horizontal stripes |
| 693 |
\_def\.parsemaskingfunction{% |
| 694 |
% Compute mod(\themaski,2)% |
| 695 |
\_ifodd\.maski\_relax% |
| 696 |
\_def\.maskfunctionresult{1}% |
| 697 |
\_else% |
| 698 |
\_def\.maskfunctionresult{0}% |
| 699 |
\_fi% |
| 700 |
}% |
| 701 |
\_or |
| 702 |
%Case 2: vertical stripes |
| 703 |
\_def\.parsemaskingfunction{% |
| 704 |
% Compute mod(\themaskj,3)% |
| 705 |
\.a=\.maskj% |
| 706 |
\_divide\.a by 3% |
| 707 |
\_multiply\.a by 3% |
| 708 |
\_advance\.a by -\.maskj% |
| 709 |
\_edef\.maskfunctionresult{\_the\.a}% |
| 710 |
}% |
| 711 |
\_or |
| 712 |
%Case 3: diagonal stripes |
| 713 |
\_def\.parsemaskingfunction{% |
| 714 |
% Compute mod(\themaski+\themaskj,3)% |
| 715 |
\.a=\.maski% |
| 716 |
\_advance\.a by \.maskj% |
| 717 |
\.b=\.a% |
| 718 |
\_divide\.b by 3% |
| 719 |
\_multiply\.b by 3% |
| 720 |
\_advance\.b by -\.a% |
| 721 |
\_edef\.maskfunctionresult{\_the\.b}% |
| 722 |
}% |
| 723 |
\_or |
| 724 |
%Case 4: wide checkerboard |
| 725 |
\_def\.parsemaskingfunction{% |
| 726 |
% Compute mod(floor(\themaski/2) + floor(\themaskj/3),2) % |
| 727 |
\.a=\.maski% |
| 728 |
\_divide\.a by 2% |
| 729 |
\.b=\.maskj% |
| 730 |
\_divide\.b by 3% |
| 731 |
\_advance\.a by \.b% |
| 732 |
\.b=\.a% |
| 733 |
\_divide\.a by 2% |
| 734 |
\_multiply\.a by 2% |
| 735 |
\_advance\.a by -\.b% |
| 736 |
\_edef\.maskfunctionresult{\_the\.a}% |
| 737 |
}% |
| 738 |
\_or |
| 739 |
%Case 5: quilt |
| 740 |
\_def\.parsemaskingfunction{% |
| 741 |
% Compute mod(\themaski*\themaskj,2) + mod(\themaski*\themaskj,3) % |
| 742 |
\.a=\.maski% |
| 743 |
\_multiply\.a by \.maskj% |
| 744 |
\.b=\.a% |
| 745 |
\.c=\.a% |
| 746 |
\_divide\.a by 2% |
| 747 |
\_multiply\.a by 2% |
| 748 |
\_advance\.a by -\.c% (result will be -mod(i*j,2), which is negative.) |
| 749 |
\_divide\.b by 3% |
| 750 |
\_multiply\.b by 3% |
| 751 |
\_advance\.b by -\.c% (result will be -mod(i*j,3), which is negative.) |
| 752 |
\_advance\.a by \.b% (result is negative of what's in the spec.) |
| 753 |
\_edef\.maskfunctionresult{\_the\.a}% |
| 754 |
}% |
| 755 |
\_or |
| 756 |
%Case 6: arrows |
| 757 |
\_def\.parsemaskingfunction{% |
| 758 |
% Compute mod( mod(\themaski*\themaskj,2) + mod(\themaski*\themaskj,3) , 2 ) % |
| 759 |
\.a=\.maski% |
| 760 |
\_multiply\.a by \.maskj% |
| 761 |
\.b=\.a% |
| 762 |
\.c=\.a% |
| 763 |
\_multiply\.c by 2% % \.c equals 2*i*j. |
| 764 |
\_divide\.a by 2% |
| 765 |
\_multiply\.a by 2% |
| 766 |
\_advance\.c by -\.a% Now \.c equals i*j + mod(i*j,2). |
| 767 |
\_divide\.b by 3% |
| 768 |
\_multiply\.b by 3% |
| 769 |
\_advance\.c by -\.b% (Now \.c equals mod(i*j,2) + mod(i*j,3). |
| 770 |
\.a=\.c% |
| 771 |
\_divide\.a by 2% |
| 772 |
\_multiply\.a by 2% |
| 773 |
\_advance\.c by-\.a% |
| 774 |
\_edef\.maskfunctionresult{\_the\.c}% |
| 775 |
}% |
| 776 |
\_or |
| 777 |
%Case 7: shotgun |
| 778 |
\_def\.parsemaskingfunction{% |
| 779 |
% Compute mod( mod(\themaski+\themaskj,2) + mod(\themaski*\themaskj,3) , 2 ) % |
| 780 |
\.a=\.maski% |
| 781 |
\_advance\.a by \.maskj% %So \.a = i+j |
| 782 |
\.b=\.maski% |
| 783 |
\_multiply\.b by \.maskj% %So \.b = i*j |
| 784 |
\.c=\.a% |
| 785 |
\_advance\.c by \.b% So \.c = i+j+i*j |
| 786 |
\_divide\.a by 2% |
| 787 |
\_multiply\.a by 2% |
| 788 |
\_advance\.c by -\.a% So \.c = mod(i+j,2) + i*j |
| 789 |
\_divide\.b by 3% |
| 790 |
\_multiply\.b by 3% |
| 791 |
\_advance\.c by -\.b% So \.c = mod(i+j,2) + mod(i*j,3) |
| 792 |
\.a=\.c% |
| 793 |
\_divide\.c by 2% |
| 794 |
\_multiply\.c by 2% |
| 795 |
\_advance\.a by -\.c% |
| 796 |
\_edef\.maskfunctionresult{\_the\.a}% |
| 797 |
}% |
| 798 |
\_fi |
| 799 |
}% |
| 800 |
|
| 801 |
\_def\.checkifcellisinmask{% |
| 802 |
% The current position is (\.iz,\.jz), in TeX counts, |
| 803 |
% but the counters (maski,maskj) should contain |
| 804 |
% the current position with indexing starting at 0. |
| 805 |
% That is, maski = \.iz-1 and maskj = \.jz-1. |
| 806 |
% |
| 807 |
% \.parsemaskingfunction must have been set by a call to \.setmaskingfunction |
| 808 |
\.parsemaskingfunction |
| 809 |
\_ea\_ifnum\.maskfunctionresult=0\_relax |
| 810 |
\.cellinmasktrue |
| 811 |
\_else |
| 812 |
\.cellinmaskfalse |
| 813 |
\_fi |
| 814 |
}% |
| 815 |
|
| 816 |
\_newcount\.maski |
| 817 |
\_newcount\.maskj |
| 818 |
|
| 819 |
\_def\.applymask#1#2#3{% |
| 820 |
% #1 = name of a matrix that should be filled out completely |
| 821 |
% except for the format and/or version information. |
| 822 |
% #2 = name of a new matrix to contain the masked version |
| 823 |
% #3 = 1 decimal digit naming the mask |
| 824 |
\.createduplicatematrix{#2}{#1}% |
| 825 |
\.setmaskingfunction{#3}% |
| 826 |
\.setcounter\.maski{-1}% |
| 827 |
\.for \.iz = 1 to \.size by 1% |
| 828 |
{\.stepcounter\.maski |
| 829 |
\.setcounter\.maskj{-1}% |
| 830 |
\.for \.jz = 1 to \.size by 1% |
| 831 |
{\.stepcounter\.maskj |
| 832 |
\.checkifcellisinmask |
| 833 |
\_ifqr_cellinmask |
| 834 |
\.checkifcurrentcellcontainsdata{#2}% |
| 835 |
\_ifqr_currentcellcontainsdata |
| 836 |
\.flipcurrentcell{#2}% |
| 837 |
\_fi |
| 838 |
\_fi |
| 839 |
}% |
| 840 |
}% |
| 841 |
}% |
| 842 |
|
| 843 |
\_newifi\_ifqr_currentcellcontainsdata |
| 844 |
\.currentcellcontainsdatafalse |
| 845 |
|
| 846 |
\_def\.iwhite{\.white}% |
| 847 |
\_def\.iblack{\.black}% |
| 848 |
|
| 849 |
\_def\.checkifcurrentcellcontainsdata#1{% |
| 850 |
% #1 = name of matrix |
| 851 |
\.currentcellcontainsdatafalse |
| 852 |
\_ea\_ifx\_csname #1@\_the\.iz @\_the\.jz\_endcsname\.iwhite |
| 853 |
\.currentcellcontainsdatatrue |
| 854 |
\_fi |
| 855 |
\_ea\_ifx\_csname #1@\_the\.iz @\_the\.jz\_endcsname\.iblack |
| 856 |
\.currentcellcontainsdatatrue |
| 857 |
\_fi |
| 858 |
}% |
| 859 |
|
| 860 |
\_def\.flippedblack{\.black}% |
| 861 |
\_def\.flippedwhite{\.white}% |
| 862 |
|
| 863 |
\_def\.flipcurrentcell#1{% |
| 864 |
% #1 = name of matrix |
| 865 |
% (\.iz, \.jz) = current position, in TeX counts. |
| 866 |
% This assumes the cell contains data, either black or white! |
| 867 |
\_ea\_ifx\_csname #1@\_the\.iz @\_the\.jz\_endcsname\.iwhite |
| 868 |
\.storetomatrix{#1}{\_the\.iz}{\_the\.jz}{\.flippedblack}% |
| 869 |
\_else |
| 870 |
\.storetomatrix{#1}{\_the\.iz}{\_the\.jz}{\.flippedwhite}% |
| 871 |
\_fi |
| 872 |
}% |
| 873 |
|
| 874 |
\_def\.chooseandapplybestmask#1{% |
| 875 |
% #1 = name of a matrix that should be filled out completely |
| 876 |
% except for the format and/or version information. |
| 877 |
% This function applies all eight masks in succession, |
| 878 |
% calculates their penalties, and remembers the best. |
| 879 |
% The number indicating which mask was used is saved in \.mask_selected. |
| 880 |
\.createduplicatematrix{originalmatrix}{#1}% |
| 881 |
\.message{<Applying Mask 0...}% |
| 882 |
\.applymask{originalmatrix}{#1}{0}% |
| 883 |
\.message{done. Calculating penalty...}% |
| 884 |
\.evaluatemaskpenalty{#1}% |
| 885 |
\_xdef\.currentbestpenalty{\.penalty}% |
| 886 |
\.message{penalty is \.penalty>^^J}% |
| 887 |
\_gdef\.currentbestmask{0}% |
| 888 |
\.for \.iz = 1 to 7 by 1% |
| 889 |
{\.message{<Applying Mask \_the\.iz...}% |
| 890 |
\.applymask{originalmatrix}{currentmasked}{\_the\.iz}% |
| 891 |
\.message{done. Calculating penalty...}% |
| 892 |
\.evaluatemaskpenalty{currentmasked}% |
| 893 |
\.message{penalty is \.penalty>^^J}% |
| 894 |
\_ea\_ea\_ea\_ifnum\_ea\.penalty\_ea<\.currentbestpenalty\_relax |
| 895 |
%We found a better mask. |
| 896 |
\_xdef\.currentbestmask{\_the\.iz}% |
| 897 |
\.createduplicatematrix{#1}{currentmasked}% |
| 898 |
\_xdef\.currentbestpenalty{\.penalty}% |
| 899 |
\_fi |
| 900 |
}% |
| 901 |
\_xdef\.mask_selected{\.currentbestmask}% |
| 902 |
\.message{<Selected Mask \.mask_selected>^^J}% |
| 903 |
}% |
| 904 |
|
| 905 |
\_def\.Ni{3}% |
| 906 |
\_def\.Nii{3}% |
| 907 |
\_def\.Niii{40}% |
| 908 |
\_def\.Niv{10}% |
| 909 |
\_def\.fiveones{11111}% |
| 910 |
\_def\.fivezeros{00000}% |
| 911 |
\_def\.twoones{11}% |
| 912 |
\_def\.twozeros{00}% |
| 913 |
\_def\.finderA{00001011101}% |
| 914 |
\_def\.finderB{10111010000}% |
| 915 |
\_def\.finderBthree{1011101000}% |
| 916 |
\_def\.finderBtwo{101110100}% |
| 917 |
\_def\.finderBone{10111010}% |
| 918 |
\_def\.finderBzero{1011101}% |
| 919 |
\_newifi\_ifqr_stringoffive |
| 920 |
\_def\.addpenaltyiii{% |
| 921 |
\.addtocounter\.penaltyiii{\.Niii}% |
| 922 |
}% |
| 923 |
\_newcount\.totalones |
| 924 |
\_newcount\.penaltyi |
| 925 |
\_newcount\.penaltyii |
| 926 |
\_newcount\.penaltyiii |
| 927 |
\_newcount\.penaltyiv |
| 928 |
\_def\.evaluatemaskpenalty#1{% |
| 929 |
% #1 = name of a matrix that we will test for the penalty |
| 930 |
% according to the specs. |
| 931 |
\.setcounter\.penaltyi{0}% |
| 932 |
\.setcounter\.penaltyii{0}% |
| 933 |
\.setcounter\.penaltyiii{0}% |
| 934 |
\.setcounter\.penaltyiv{0}% |
| 935 |
\_bgroup%localize the meanings we give to the symbols |
| 936 |
\_def\.black{1}\_def\.white{0}% |
| 937 |
\_def\.blackfixed{1}\_def\.whitefixed{0}% |
| 938 |
\_def\.formatsquare{0}% This is not stated in the specs, but seems |
| 939 |
% to be the standard implementation. |
| 940 |
\_def\.blank{0}% These would be any bits at the end. |
| 941 |
% |
| 942 |
\.setcounter\.totalones{0}% |
| 943 |
\.for \.iz=1 to \.size by 1% |
| 944 |
{\_def\.lastfive{z}% %The z is a dummy, that will be removed before any testing. |
| 945 |
\.stringoffivefalse |
| 946 |
\_def\.lasttwo_thisrow{z}% %The z is a dummy. |
| 947 |
\_def\.lasttwo_nextrow{z}% %The z is a dummy. |
| 948 |
\_def\.lastnine{z0000}% %The 0000 stands for the white space to the left. The z is a dummy. |
| 949 |
\_def\.ignore_finderBat{0}% |
| 950 |
\.for \.jz=1 to \.size by 1% |
| 951 |
{\_edef\.newbit{\.matrixentry{#1}{\_the\.iz}{\_the\.jz}}% |
| 952 |
% |
| 953 |
% LASTFIVE CODE FOR PENALTY 1 |
| 954 |
% First, add the new bit to the end. |
| 955 |
\_ea\.xaddto\_ea\.lastfive\_ea{\.newbit}% |
| 956 |
\_ifnum\.jz<5\_relax% |
| 957 |
%Not yet on the 5th entry. |
| 958 |
%Don't do any testing. |
| 959 |
\_else |
| 960 |
% 5th entry or later. |
| 961 |
% Remove the old one, and then test. |
| 962 |
\.removefirsttoken\.lastfive% |
| 963 |
\_ifx\.lastfive\.fiveones% |
| 964 |
\_ifqr_stringoffive |
| 965 |
%This is a continuation of a previous block of five or more 1's. |
| 966 |
\.stepcounter\.penaltyi |
| 967 |
\_else |
| 968 |
%This is a new string of five 1's. |
| 969 |
\.addtocounter\.penaltyi{\.Ni}% |
| 970 |
\_global\.stringoffivetrue |
| 971 |
\_fi |
| 972 |
\_else |
| 973 |
\_ifx\.lastfive\.fivezeros% |
| 974 |
\_ifqr_stringoffive |
| 975 |
%This is a continuation of a previous block of five or more 0's. |
| 976 |
\.stepcounter\.penaltyi |
| 977 |
\_else |
| 978 |
%This is a new string of five 0's. |
| 979 |
\.addtocounter\.penaltyi{\.Ni}% |
| 980 |
\_global\.stringoffivetrue |
| 981 |
\_fi |
| 982 |
\_else |
| 983 |
%This is not a string of five 1's or five 0's. |
| 984 |
\_global\.stringoffivefalse |
| 985 |
\_fi |
| 986 |
\_fi |
| 987 |
\_fi |
| 988 |
% |
| 989 |
% 2x2 BLOCKS FOR PENALTY 2 |
| 990 |
% Every 2x2 block of all 1's counts for \.Nii penalty points. |
| 991 |
% We do not need to run this test in the last row. |
| 992 |
\_ea\_ifnum\_ea\.iz\_ea<\.size\_relax |
| 993 |
\_ea\.xaddto\_ea\.lasttwo_thisrow\_ea{\.newbit}% |
| 994 |
%Compute \.iplusone |
| 995 |
\.a=\.iz\_relax% |
| 996 |
\_advance\.a by 1% |
| 997 |
\_edef\.iplusone{\_the\.a}% |
| 998 |
% |
| 999 |
\_edef\.nextrowbit{\.matrixentry{#1}{\.iplusone}{\_the\.jz}}% |
| 1000 |
\_ea\.xaddto\_ea\.lasttwo_nextrow\_ea{\.nextrowbit}% |
| 1001 |
\_ifnum\.jz<2\_relax% |
| 1002 |
%Still in the first column; no check. |
| 1003 |
\_else |
| 1004 |
%Second column or later. Remove the old bits, and then test. |
| 1005 |
\.removefirsttoken\.lasttwo_thisrow |
| 1006 |
\.removefirsttoken\.lasttwo_nextrow |
| 1007 |
\_ifx\.lasttwo_thisrow\.twoones |
| 1008 |
\_ifx\.lasttwo_nextrow\.twoones |
| 1009 |
\.addtocounter\.penaltyii{\.Nii}% |
| 1010 |
\_fi |
| 1011 |
\_else |
| 1012 |
\_ifx\.lasttwo_thisrow\.twozeros |
| 1013 |
\_ifx\.lasttwo_nextrow\.twozeros |
| 1014 |
\.addtocounter\.penaltyii{\.Nii}% |
| 1015 |
\_fi |
| 1016 |
\_fi |
| 1017 |
\_fi |
| 1018 |
\_fi |
| 1019 |
\_fi |
| 1020 |
% |
| 1021 |
% LASTNINE CODE FOR PENALTY 3 |
| 1022 |
% First, add the new bit to the end. |
| 1023 |
\_ea\.xaddto\_ea\.lastnine\_ea{\.newbit}% |
| 1024 |
\_ifnum\.jz<7\_relax% |
| 1025 |
%Not yet on the 7th entry. |
| 1026 |
%Don't do any testing. |
| 1027 |
\_else |
| 1028 |
% 7th entry or later. |
| 1029 |
% Remove the old one, and then test. |
| 1030 |
\.removefirsttoken\.lastnine |
| 1031 |
\_ea\_ifnum\.size=\.jz\_relax% |
| 1032 |
% Last column. Any of the following should count: |
| 1033 |
% 1011101 (\.finderBzero) |
| 1034 |
% 10111010 (\.finderBone) |
| 1035 |
% 101110100 (\.finderBtwo) |
| 1036 |
% 1011101000 (\.finderBthree) |
| 1037 |
% 10111010000 (\.finderB) |
| 1038 |
\_ifx\.lastnine\.finderB |
| 1039 |
\.addpenaltyiii |
| 1040 |
\_else |
| 1041 |
\.removefirsttoken\.lastnine |
| 1042 |
\_ifx\.lastnine\.finderBthree |
| 1043 |
\.addpenaltyiii |
| 1044 |
\_else |
| 1045 |
\.removefirsttoken\.lastnine |
| 1046 |
\_ifx\.lastnine\.finderBtwo |
| 1047 |
\.addpenaltyiii |
| 1048 |
\_else |
| 1049 |
\.removefirsttoken\.lastnine |
| 1050 |
\_ifx\.lastnine\.finderBone |
| 1051 |
\.addpenaltyiii |
| 1052 |
\_else |
| 1053 |
\.removefirsttoken\.lastnine |
| 1054 |
\_ifx\.lastnine\.finderBzero |
| 1055 |
\.addpenaltyiii |
| 1056 |
\_fi |
| 1057 |
\_fi |
| 1058 |
\_fi |
| 1059 |
\_fi |
| 1060 |
\_fi |
| 1061 |
\_else |
| 1062 |
\_ifx\.lastnine\.finderA% %Matches 0000 1011101 |
| 1063 |
\.addpenaltyiii |
| 1064 |
%Also, we record our discovery, so that we can't count this pattern again |
| 1065 |
%if it shows up four columns later as 1011101 0000. |
| 1066 |
% |
| 1067 |
%Set \.ignore_finderBat to \.jz+4. |
| 1068 |
\.a=\.jz\_relax% |
| 1069 |
\_advance\.a by 4% |
| 1070 |
\_xdef\.ignore_finderBat{\_the\.a}% |
| 1071 |
\_else |
| 1072 |
\_ifx\.lastfive\.finderB% %Matches 1011101 0000. |
| 1073 |
\_ea\_ifnum\.ignore_finderBat=\.jz\_relax |
| 1074 |
%This pattern was *not* counted already earlier. |
| 1075 |
\.addpenaltyiii |
| 1076 |
\_fi |
| 1077 |
\_fi |
| 1078 |
\_fi |
| 1079 |
\_fi |
| 1080 |
\_fi |
| 1081 |
% |
| 1082 |
%COUNT 1's FOR PENALTY 4 |
| 1083 |
\_ea\_ifnum\.newbit=1\_relax% |
| 1084 |
\.stepcounter\.totalones |
| 1085 |
\_fi |
| 1086 |
}% end of j-loop |
| 1087 |
}% end of i-loop |
| 1088 |
% |
| 1089 |
%NOW WE ALSO NEED TO RUN DOWN THE COLUMNS TO FINISH CALCULATING PENALTIES 1 AND 3. |
| 1090 |
\.for \.jz=1 to \.size by 1% |
| 1091 |
{\_def\.lastfive{z}% %The z is a dummy, that will be removed before any testing. |
| 1092 |
\.stringoffivefalse |
| 1093 |
\_def\.lastnine{z0000}% %The 0000 stands for the white space to the left. The z is a dummy. |
| 1094 |
\_def\.ignore_finderBat{0}% |
| 1095 |
\.for \.iz=1 to \.size by 1% |
| 1096 |
{\_edef\.newbit{\.matrixentry{#1}{\_the\.iz}{\_the\.jz}}% |
| 1097 |
% |
| 1098 |
% LASTFIVE CODE FOR PENALTY 1 |
| 1099 |
% First, add the new bit to the end. |
| 1100 |
\_ea\.xaddto\_ea\.lastfive\_ea{\.newbit}% |
| 1101 |
\_ifnum\.iz<5\_relax% |
| 1102 |
%Not yet on the 5th entry. |
| 1103 |
%Don't do any testing. |
| 1104 |
\_else |
| 1105 |
% 5th entry or later. |
| 1106 |
% Remove the old one, and then test. |
| 1107 |
\.removefirsttoken\.lastfive |
| 1108 |
\_ifx\.lastfive\.fiveones% |
| 1109 |
\_ifqr_stringoffive |
| 1110 |
%This is a continuation of a previous block of five or more 1's. |
| 1111 |
\.stepcounter\.penaltyi |
| 1112 |
\_else |
| 1113 |
%This is a new string of five 1's. |
| 1114 |
\.addtocounter\.penaltyi{\.Ni}% |
| 1115 |
\_global\.stringoffivetrue |
| 1116 |
\_fi |
| 1117 |
\_else |
| 1118 |
\_ifx\.lastfive\.fivezeros% |
| 1119 |
\_ifqr_stringoffive |
| 1120 |
%This is a continuation of a previous block of five or more 0's. |
| 1121 |
\.stepcounter\.penaltyi |
| 1122 |
\_else |
| 1123 |
%This is a new string of five 0's. |
| 1124 |
\.addtocounter\.penaltyi{\.Ni}% |
| 1125 |
\_global\.stringoffivetrue |
| 1126 |
\_fi |
| 1127 |
\_else |
| 1128 |
%This is not a string of five 1's or five 0's. |
| 1129 |
\_global\.stringoffivefalse |
| 1130 |
\_fi |
| 1131 |
\_fi |
| 1132 |
\_fi |
| 1133 |
% |
| 1134 |
% HAPPILY, WE DON'T NEED TO CALCULATE PENALTY 2 AGAIN. |
| 1135 |
% |
| 1136 |
% LASTNINE CODE FOR PENALTY 3 |
| 1137 |
% First, add the new bit to the end. |
| 1138 |
\_ea\.xaddto\_ea\.lastnine\_ea{\.newbit}% |
| 1139 |
\_ifnum\.iz<7\_relax% |
| 1140 |
%Not yet on the 7th entry. |
| 1141 |
%Don't do any testing. |
| 1142 |
\_else |
| 1143 |
% 7th entry or later. |
| 1144 |
% Remove the old one, and then test. |
| 1145 |
\.removefirsttoken\.lastnine |
| 1146 |
\_ea\_ifnum\.size=\.iz\_relax% |
| 1147 |
% Last column. Any of the following should count: |
| 1148 |
% 1011101 (\.finderBzero) |
| 1149 |
% 10111010 (\.finderBone) |
| 1150 |
% 101110100 (\.finderBtwo) |
| 1151 |
% 1011101000 (\.finderBthree) |
| 1152 |
% 10111010000 (\.finderB) |
| 1153 |
\_ifx\.lastnine\.finderB |
| 1154 |
\.addpenaltyiii |
| 1155 |
\_else |
| 1156 |
\.removefirsttoken\.lastnine |
| 1157 |
\_ifx\.lastnine\.finderBthree |
| 1158 |
\.addpenaltyiii |
| 1159 |
\_else |
| 1160 |
\.removefirsttoken\.lastnine |
| 1161 |
\_ifx\.lastnine\.finderBtwo |
| 1162 |
\.addpenaltyiii |
| 1163 |
\_else |
| 1164 |
\.removefirsttoken\.lastnine |
| 1165 |
\_ifx\.lastnine\.finderBone |
| 1166 |
\.addpenaltyiii |
| 1167 |
\_else |
| 1168 |
\.removefirsttoken\.lastnine |
| 1169 |
\_ifx\.lastnine\.finderBzero |
| 1170 |
\.addpenaltyiii |
| 1171 |
\_fi |
| 1172 |
\_fi |
| 1173 |
\_fi |
| 1174 |
\_fi |
| 1175 |
\_fi |
| 1176 |
\_else |
| 1177 |
\_ifx\.lastnine\.finderA% %Matches 0000 1011101 |
| 1178 |
\.addpenaltyiii |
| 1179 |
%Also, we record our discovery, so that we can't count this pattern again |
| 1180 |
%if it shows up four columns later as 1011101 0000. |
| 1181 |
% |
| 1182 |
%Set \.ignore_finderBat to \.iz+4. |
| 1183 |
\.a=\.iz\_relax% |
| 1184 |
\_advance\.a by 4% |
| 1185 |
\_xdef\.ignore_finderBat{\_the\.a}% |
| 1186 |
\_else |
| 1187 |
\_ifx\.lastfive\.finderB% %Matches 1011101 0000. |
| 1188 |
\_ea\_ifnum\.ignore_finderBat=\.iz\_relax |
| 1189 |
%This pattern was *not* counted already earlier. |
| 1190 |
\.addpenaltyiii |
| 1191 |
\_fi |
| 1192 |
\_fi |
| 1193 |
\_fi |
| 1194 |
\_fi |
| 1195 |
\_fi |
| 1196 |
% |
| 1197 |
}% end of i-loop |
| 1198 |
}% end of j-loop |
| 1199 |
\_egroup |
| 1200 |
% |
| 1201 |
%CALCULATE PENALTY 4 |
| 1202 |
%According to the spec, penalty #4 is computed as |
| 1203 |
% floor( |(i/n^2)-0.5|/0.05 ) |
| 1204 |
% where i is the total number of 1's in the matrix. |
| 1205 |
% This is equal to abs(20*i-10n^2) div n^2. |
| 1206 |
% |
| 1207 |
\.a=\.totalones\_relax |
| 1208 |
\_multiply\.a by 20\_relax |
| 1209 |
\.b=\.size\_relax |
| 1210 |
\_multiply\.b by \.size\_relax |
| 1211 |
\.c=10\_relax |
| 1212 |
\_multiply\.c by \.b\_relax |
| 1213 |
\_advance\.a by -\.c\_relax |
| 1214 |
\_ifnum\.a<0\_relax |
| 1215 |
\_multiply\.a by -1\_relax |
| 1216 |
\_fi |
| 1217 |
\_divide\.a by \.b\_relax |
| 1218 |
\.setcounter\.penaltyiv{\_the\.a}% |
| 1219 |
% |
| 1220 |
%CALCULATE TOTAL PENALTY |
| 1221 |
\.a=\_the\.penaltyi\_relax% |
| 1222 |
\_advance\.a by \_the\.penaltyii\_relax% |
| 1223 |
\_advance\.a by \_the\.penaltyiii\_relax% |
| 1224 |
\_advance\.a by \_the\.penaltyiv\_relax% |
| 1225 |
\_edef\.penalty{\_the\.a}% |
| 1226 |
}% |
| 1227 |
|
| 1228 |
\_def\.removefirsttoken#1{% |
| 1229 |
%Removes the first token from the macro named in #1. |
| 1230 |
\_edef\.argument{(#1)}% |
| 1231 |
\_ea\.removefirsttoken_int\.argument% |
| 1232 |
\_xdef#1{\.removefirsttoken_result}% |
| 1233 |
}% |
| 1234 |
\_def\.removefirsttoken_int(#1#2){% |
| 1235 |
\_def\.removefirsttoken_result{#2}% |
| 1236 |
}% |
| 1237 |
|
| 1238 |
\_def\.writeformatstring#1#2{% |
| 1239 |
% #1 = matrix name |
| 1240 |
% #2 = binary string representing the encoded and masked format information |
| 1241 |
\.setcounter\.i{9}% |
| 1242 |
\.setcounter\.j{1}% |
| 1243 |
\_edef\.argument{{#1}(#2\_relax)}% |
| 1244 |
\_ea\.writeformatA_recursive\.argument |
| 1245 |
% |
| 1246 |
\.setcounter\.i{\.numberofrowsinmatrix{#1}}% |
| 1247 |
\.setcounter\.j{9}% |
| 1248 |
\_ea\.writeformatB_recursive\.argument |
| 1249 |
}% |
| 1250 |
|
| 1251 |
\_def\.writeformatA_recursive#1(#2#3){% |
| 1252 |
% #1 = matrix name |
| 1253 |
% #2 = first bit of string |
| 1254 |
% #3 = rest of bitstream |
| 1255 |
% (qr_i,qr_j) = current (valid) position to write (in counters) |
| 1256 |
\_ifnum#2=1\_relax |
| 1257 |
\.storetomatrix{#1}{\_the\.i}{\_the\.j}{\.blackformat}% |
| 1258 |
\_else |
| 1259 |
\.storetomatrix{#1}{\_the\.i}{\_the\.j}{\.whiteformat}% |
| 1260 |
\_fi |
| 1261 |
% Now the tricky part--moving \.iz and \.jz to their next positions. |
| 1262 |
\_ifnum\.j<9\_relax |
| 1263 |
%If we're not yet in column 9, move right. |
| 1264 |
\.stepcounter\.j |
| 1265 |
\_ifnum\.j=7\_relax |
| 1266 |
%But we skip column 7! |
| 1267 |
\.stepcounter\.j |
| 1268 |
\_fi |
| 1269 |
\_else |
| 1270 |
%If we're in column 9, we move up. |
| 1271 |
\.addtocounter\.i{-1}% |
| 1272 |
\_ifnum\.i=7\_relax |
| 1273 |
%But we skip row 7! |
| 1274 |
\.addtocounter\.i{-1}% |
| 1275 |
\_fi |
| 1276 |
\_fi |
| 1277 |
%N.B. that at the end of time, this will leave us at invalid position (0,9). |
| 1278 |
%That makes for an easy test to know when we are done. |
| 1279 |
\_ifnum\.i<1 |
| 1280 |
\_let\.nexttoken=\_relax |
| 1281 |
\_else |
| 1282 |
\_def\.nexttoken{\.writeformatA_recursive{#1}(#3)}% |
| 1283 |
\_fi |
| 1284 |
\.nexttoken |
| 1285 |
}% |
| 1286 |
|
| 1287 |
\_def\.writeformatB_recursive#1(#2#3){% |
| 1288 |
% #1 = matrix name |
| 1289 |
% #2 = first bit of string |
| 1290 |
% #3 = rest of bitstream |
| 1291 |
% (qr_i,qr_j) = current (valid) position to write (in counters) |
| 1292 |
\_ifnum#2=1\_relax |
| 1293 |
\.storetomatrix{#1}{\_the\.i}{\_the\.j}{\.blackformat}% |
| 1294 |
\_else |
| 1295 |
\.storetomatrix{#1}{\_the\.i}{\_the\.j}{\.whiteformat}% |
| 1296 |
\_fi |
| 1297 |
% Now the tricky part--moving counters i and j to their next positions. |
| 1298 |
\.a=\.size% |
| 1299 |
\_advance\.a by -6\_relax% |
| 1300 |
\_ifnum\.a<\.i\_relax |
| 1301 |
%If we're not yet in row n-6, move up. |
| 1302 |
\.addtocounter\.i{-1}% |
| 1303 |
\_else |
| 1304 |
\_ifnum\.a=\.i\_relax |
| 1305 |
%If we're actually in row n-6, we jump to position (9,n-7). |
| 1306 |
\.setcounter\.i{9}% |
| 1307 |
%Set counter j equal to \.size-7. |
| 1308 |
\_global\.j=\.size\_relax% |
| 1309 |
\_global\_advance\.j by -7\_relax% |
| 1310 |
\_else |
| 1311 |
%Otherwise, we must be in row 9. |
| 1312 |
%In this case, we move right. |
| 1313 |
\.stepcounter\.j |
| 1314 |
\_fi |
| 1315 |
\_fi |
| 1316 |
%N.B. that at the end of time, this will leave us at invalid position (9,n+1). |
| 1317 |
%That makes for an easy test to know when we are done. |
| 1318 |
\_ea\_ifnum\.size<\.j\_relax |
| 1319 |
\_let\.nexttoken=\_relax |
| 1320 |
\_else |
| 1321 |
\_def\.nexttoken{\.writeformatB_recursive{#1}(#3)}% |
| 1322 |
\_fi |
| 1323 |
\.nexttoken |
| 1324 |
}% |
| 1325 |
|
| 1326 |
\_def\.writeversionstring#1#2{% |
| 1327 |
% #1 = matrix name |
| 1328 |
% #2 = binary string representing the encoded version information |
| 1329 |
% |
| 1330 |
% Plot the encoded version string into the matrix. |
| 1331 |
% This is only done for versions 7 and higher. |
| 1332 |
\_ea\_ifnum\.version>6\_relax |
| 1333 |
%Move to position (n-8,6). |
| 1334 |
\.setcounter\.i{\.size}\_relax% |
| 1335 |
\.addtocounter\.i{-8}\_relax% |
| 1336 |
\.setcounter\.j{6}% |
| 1337 |
\_edef\.argument{{#1}(#2\_relax)}% |
| 1338 |
\_ea\.writeversion_recursive\.argument |
| 1339 |
\_fi |
| 1340 |
}% |
| 1341 |
|
| 1342 |
\_def\.writeversion_recursive#1(#2#3){% |
| 1343 |
% #1 = matrix name |
| 1344 |
% #2 = first bit of string |
| 1345 |
% #3 = rest of bitstream |
| 1346 |
% (qr_i,qr_j) = current (valid) position to write (in counters) |
| 1347 |
% |
| 1348 |
% The version information is stored symmetrically in the matrix |
| 1349 |
% In two transposed regions, so we can write both at the same time. |
| 1350 |
% In the comments, we describe what happens in the lower-left region, |
| 1351 |
% not the upper-right. |
| 1352 |
% |
| 1353 |
%Set \.topline equal to n-10. |
| 1354 |
\.a=\.size\_relax% |
| 1355 |
\_advance\.a by -10\_relax% |
| 1356 |
\_edef\.topline{\_the\.a}% |
| 1357 |
% |
| 1358 |
\_ifnum#2=1\_relax |
| 1359 |
\.storetomatrix{#1}{\_the\.i}{\_the\.j}{\.blackformat}% |
| 1360 |
\.storetomatrix{#1}{\_the\.j}{\_the\.i}{\.blackformat}% |
| 1361 |
\_else |
| 1362 |
\.storetomatrix{#1}{\_the\.i}{\_the\.j}{\.whiteformat}% |
| 1363 |
\.storetomatrix{#1}{\_the\.j}{\_the\.i}{\.whiteformat}% |
| 1364 |
\_fi |
| 1365 |
% Now the tricky part--moving counters i and j to their next positions. |
| 1366 |
\.addtocounter\.i{-1}% |
| 1367 |
\_ea\_ifnum\.topline>\.i\_relax |
| 1368 |
%We've overshot the top of the region. |
| 1369 |
%We need to move left one column and down three. |
| 1370 |
\.addtocounter\.j{-1}% |
| 1371 |
\.addtocounter\.i{3}% |
| 1372 |
\_fi |
| 1373 |
%N.B. that at the end of time, this will leave us at invalid position (n-8,0). |
| 1374 |
%That makes for an easy test to know when we are done. |
| 1375 |
\_ifnum\.j<1\_relax |
| 1376 |
\_let\.nexttoken=\_relax |
| 1377 |
\_else |
| 1378 |
\_def\.nexttoken{\.writeversion_recursive{#1}(#3)}% |
| 1379 |
\_fi |
| 1380 |
\.nexttoken |
| 1381 |
}% |
| 1382 |
\_newcount\.hexchars |
| 1383 |
|
| 1384 |
\_def\.encode_binary#1{% |
| 1385 |
% #1 = string of ascii characters, to be converted into bitstream |
| 1386 |
% |
| 1387 |
% We do this one entirely in hex, rather than binary, because we can. |
| 1388 |
% |
| 1389 |
%Now comes the actual data. |
| 1390 |
% |
| 1391 |
{\_endlinechar=-1 \_xdef\.codetext{4\.utfstring{#1}0}}% |
| 1392 |
%The terminator is 0 |
| 1393 |
%\.xaddto\.codetext{0}% %This is '0000' in binary. |
| 1394 |
% |
| 1395 |
%There is no need to pad bits to make a multiple of 8, |
| 1396 |
%because the data length is already 4 + 8 + 8n + 4. |
| 1397 |
% |
| 1398 |
%Now add padding codewords if needed. |
| 1399 |
\.setcounter\.hexchars{0}% |
| 1400 |
\.getstringlength{\.codetext}% |
| 1401 |
\.setcounter\.hexchars{\.stringlength}% |
| 1402 |
%Set \.numpaddingcodewords equal to \.totaldatacodewords - hexchars/2. |
| 1403 |
\.a=-\.hexchars\_relax |
| 1404 |
\_divide\.a by 2\_relax |
| 1405 |
\_advance\.a by \.totaldatacodewords\_relax |
| 1406 |
\_edef\.numpaddingcodewords{\_the\.a}% |
| 1407 |
% |
| 1408 |
\_ea\_ifnum\.numpaddingcodewords<0% |
| 1409 |
\_edef\.ds{ERROR: Too much data! Over by \.numpaddingcodewords bytes.}\_show\.ds |
| 1410 |
\_fi |
| 1411 |
\_ea\_ifnum\.numpaddingcodewords>0% |
| 1412 |
\.for \.iz = 2 to \.numpaddingcodewords by 2% |
| 1413 |
{\.xaddto{\.codetext}{ec11}}% |
| 1414 |
\_ea\_ifodd\.numpaddingcodewords\_relax |
| 1415 |
\.xaddto{\.codetext}{ec}% |
| 1416 |
\_fi |
| 1417 |
\_fi |
| 1418 |
}% |
| 1419 |
|
| 1420 |
\_def\.splitcodetextintoblocks{% |
| 1421 |
\.setcounter\.i{0}% |
| 1422 |
\.for \.jz = 1 to \.numshortblocks by 1% |
| 1423 |
{\.stepcounter\.i |
| 1424 |
\.splitoffblock{\.codetext}{\_the\.i}{\.shortblock_size}% |
| 1425 |
}% |
| 1426 |
\_ea\_ifnum\.numlongblocks>0\_relax |
| 1427 |
\.for \.jz = 1 to \.numlongblocks by 1% |
| 1428 |
{\.stepcounter\.i |
| 1429 |
\.splitoffblock{\.codetext}{\_the\.i}{\.longblock_size}% |
| 1430 |
}% |
| 1431 |
\_fi |
| 1432 |
}% |
| 1433 |
|
| 1434 |
\_def\.splitoffblock#1#2#3{% |
| 1435 |
% #1 = current codetext in hexadecimal |
| 1436 |
% #2 = number to use in csname "\datablock@#2". |
| 1437 |
% #3 = number of bytes to split off |
| 1438 |
\.message{<Splitting off block #2>}% |
| 1439 |
\_ea\_gdef\_csname datablock@#2\_endcsname{}% %This line is important! |
| 1440 |
\.for \.iz = 1 to #3 by 1% |
| 1441 |
{\_edef\.argument{{#2}(#1)}% |
| 1442 |
\_ea\.splitoffblock_int\.argument |
| 1443 |
}% |
| 1444 |
}% |
| 1445 |
|
| 1446 |
\_def\.splitoffblock_int#1(#2#3#4){% |
| 1447 |
% #1 = number to use in csname "\datablock@#1". |
| 1448 |
% #2#3 = next byte to split off |
| 1449 |
% #4 = remaining text |
| 1450 |
% |
| 1451 |
% We add the next byte to "\datablock@#1", |
| 1452 |
% and we remove it from the codetext. |
| 1453 |
\_ea\_xdef\_csname datablock@#1\_endcsname{\_csname datablock@#1\_endcsname#2#3}% |
| 1454 |
\_xdef\.codetext{#4}% |
| 1455 |
}% |
| 1456 |
|
| 1457 |
\_def\.createerrorblocks{% |
| 1458 |
\.for \.ii = 1 to \.numblocks by 1% |
| 1459 |
{\.message{<Making error block \_the\.ii>}% |
| 1460 |
\.FXgenerate_errorbytes{\_csname datablock@\_the\.ii\_endcsname}{\.numeccodewords}% |
| 1461 |
\_ea\_xdef\_csname errorblock@\_the\.ii\_endcsname{\.FXerrorbytes}% |
| 1462 |
}% |
| 1463 |
}% |
| 1464 |
|
| 1465 |
\_def\.interleave{% |
| 1466 |
\.setcounter\.i{0}% |
| 1467 |
\_def\.interleaved_text{}% |
| 1468 |
\.message{<Interleaving datablocks of length \.shortblock_size\_space and \.longblock_size: }% |
| 1469 |
\.for \.ii = 1 to \.shortblock_size by 1% |
| 1470 |
{\.for \.jj = 1 to \.numblocks by 1% |
| 1471 |
{\.writefromblock{datablock}{\_the\.jj}% |
| 1472 |
}% |
| 1473 |
\.message{\_the\.ii,}% |
| 1474 |
}% |
| 1475 |
%The long blocks are numbered \.numshortblocks+1, \.numshortblocks+2, ..., \.numblocks. |
| 1476 |
\.a=\.numshortblocks\_relax |
| 1477 |
\_advance\.a by 1\_relax |
| 1478 |
\.for \.jj = \.a to \.numblocks by 1% |
| 1479 |
{\.writefromblock{datablock}{\_the\.jj}}% |
| 1480 |
\_ea\_ifnum\.numlongblocks>0\_relax% |
| 1481 |
\.message{\.longblock_size.>}% |
| 1482 |
\_else |
| 1483 |
\.message{.>}% |
| 1484 |
\_fi |
| 1485 |
\.message{<Interleaving errorblocks of length \.numeccodewords: }% |
| 1486 |
\.for \.ii = 1 to \.numeccodewords by 1% |
| 1487 |
{\.message{\_the\.ii,}% |
| 1488 |
\.for \.jj = 1 to \.numblocks by 1% |
| 1489 |
{\.writefromblock{errorblock}{\_the\.jj}% |
| 1490 |
}% |
| 1491 |
}% |
| 1492 |
\.message{.><Interleaving complete.>}% |
| 1493 |
}% |
| 1494 |
|
| 1495 |
\_def\.writefromblock#1#2{% |
| 1496 |
% #1 = either 'datablock' or 'errorblock' |
| 1497 |
% #2 = block number, in {1,...,\.numblocks}% |
| 1498 |
\_edef\.argument{(\_csname #1@#2\_endcsname\_relax\_relax\_relax)}% |
| 1499 |
\_ea\.writefromblock_int\.argument |
| 1500 |
\_ea\_xdef\_csname #1@#2\_endcsname{\.writefromblock_remainder}% |
| 1501 |
}% |
| 1502 |
|
| 1503 |
\_def\.writefromblock_int(#1#2#3){% |
| 1504 |
% #1#2 = first byte (in hex) of text, which will be written to \.interleaved_text |
| 1505 |
% #3 = remainder, including \_relax\_relax\_relax terminator. |
| 1506 |
\.xaddto{\.interleaved_text}{#1#2}% |
| 1507 |
\.writefromblock_intint(#3)% |
| 1508 |
}% |
| 1509 |
|
| 1510 |
\_def\.writefromblock_intint(#1\_relax\_relax\_relax){% |
| 1511 |
\_xdef\.writefromblock_remainder{#1}% |
| 1512 |
}% |
| 1513 |
|
| 1514 |
\_newifi\_ifqr_leadingcoeff |
| 1515 |
\_def\.testleadingcoeff(#1#2){% |
| 1516 |
% Tests whether the leading digit of #1#2 is 1. |
| 1517 |
\_ifnum#1=1\_relax |
| 1518 |
\.leadingcoefftrue |
| 1519 |
\_else |
| 1520 |
\.leadingcoefffalse |
| 1521 |
\_fi |
| 1522 |
}% |
| 1523 |
|
| 1524 |
\_def\.polynomialdivide#1#2{% |
| 1525 |
\_edef\.numerator{#1}% |
| 1526 |
\_edef\.denominator{#2}% |
| 1527 |
\.divisiondonefalse% |
| 1528 |
\_ea\_ea\_ea\.oneroundofdivision\_ea\_ea\_ea{\_ea\.numerator\_ea}\_ea{\.denominator}% |
| 1529 |
}% |
| 1530 |
|
| 1531 |
\_def\.empty{}% |
| 1532 |
\_def\.oneroundofdivision#1#2{% |
| 1533 |
% #1 = f(x), of degree n |
| 1534 |
% #2 = g(x), of degree m |
| 1535 |
% Obtains a new polynomial h(x), congruent to f(x) modulo g(x), |
| 1536 |
% but of degree at most n-1. |
| 1537 |
% |
| 1538 |
% If leading coefficient of f(x) is 1, subtracts off g(x) * x^(n-m). |
| 1539 |
% If leading coefficient of f(x) is 0, strips off that leading zero. |
| 1540 |
% |
| 1541 |
\.testleadingcoeff(#1)% |
| 1542 |
\_ifqr_leadingcoeff |
| 1543 |
\.xorbitstrings{#1}{#2}% |
| 1544 |
\_ifqr_xorfailed |
| 1545 |
%If xor failed, that means our #1 was already the remainder! |
| 1546 |
\.divisiondonetrue |
| 1547 |
\_edef\.theremainder{#1}% |
| 1548 |
\_else |
| 1549 |
%xor succeeded. We need to recurse. |
| 1550 |
\_ea\_ea\_ea\_edef\_ea\_ea\_ea\.numerator\_ea\_ea\_ea{\_ea\.stripleadingzero\_ea(\.xorresult)}% |
| 1551 |
\_fi |
| 1552 |
\_else |
| 1553 |
\_ea\_def\_ea\.numerator\_ea{\.stripleadingzero(#1)}% |
| 1554 |
\_ifx\.numerator\.empty |
| 1555 |
\.divisiondonetrue |
| 1556 |
\_def\.theremainder{0}% |
| 1557 |
\_fi |
| 1558 |
\_fi |
| 1559 |
\_ifqr_divisiondone |
| 1560 |
\_relax |
| 1561 |
\_else |
| 1562 |
\_ea\.oneroundofdivision\_ea{\.numerator}{#2}% |
| 1563 |
\_fi |
| 1564 |
}% |
| 1565 |
|
| 1566 |
\_def\.stripleadingzero(0#1){#1}%Strips off a leading zero. |
| 1567 |
|
| 1568 |
\_newifi\_ifqr_xorfailed% This flag will trigger when #2 is longer than #1. |
| 1569 |
|
| 1570 |
\_def\.xorbitstrings#1#2{% |
| 1571 |
% #1 = bitstring |
| 1572 |
% #2 = bitstring no longer than #1 |
| 1573 |
\.xorfailedfalse |
| 1574 |
\_edef\.argument{(,#1\_relax\_relax)(#2\_relax\_relax)}% |
| 1575 |
\_ea\.xorbitstrings_recursive\.argument |
| 1576 |
%\.xorbitstrings_recursive(,#1\_relax\_relax)(#2\_relax\_relax)% |
| 1577 |
}% |
| 1578 |
|
| 1579 |
\_def\.xorbitstrings_recursive(#1,#2#3)(#4#5){% |
| 1580 |
% #1#2#3 is the first bitstring, xor'ed up through #1. |
| 1581 |
% #4#5 is the remaining portion of the second bitstring. |
| 1582 |
\_def\.testii{#2}% |
| 1583 |
\_def\.testiv{#4}% |
| 1584 |
\_ifx\.testii\.relax |
| 1585 |
% #1 contains the whole string. |
| 1586 |
% Now if #4 is also \_relax, that means the two strings started off with equal lengths. |
| 1587 |
% If, however, #4 is not \_relax, that means the second string was longer than the first, a problem. |
| 1588 |
\_ifx\.testiv\.relax |
| 1589 |
%No problem. We are done. |
| 1590 |
\.xorbit_saveresult(#1#2#3)% |
| 1591 |
\_else |
| 1592 |
%Problem! The second string was longer than the first. |
| 1593 |
\.xorfailedtrue |
| 1594 |
\_def\.xorresult{}% |
| 1595 |
\_fi |
| 1596 |
\_else |
| 1597 |
% There is still a bit to manipulate in #2. |
| 1598 |
% Check whether #4 contains anything. |
| 1599 |
\_ifx\.testiv\.relax |
| 1600 |
% No, #4 is empty. We are done. "#2#3" contains the remainder of the first string, |
| 1601 |
% which we append untouched and then strip off the two \_relax-es. |
| 1602 |
\.xorbit_saveresult(#1#2#3)% |
| 1603 |
\_else |
| 1604 |
% Yes, #4 still has something to XOR. Do the task. |
| 1605 |
\_ifnum#2=#4\_relax |
| 1606 |
\.xorbitstrings_recursive(#1% |
| 1607 |
0,#3)(#5)% |
| 1608 |
\_else |
| 1609 |
\.xorbitstrings_recursive(#1% |
| 1610 |
1,#3)(#5)% |
| 1611 |
\_fi |
| 1612 |
\_fi |
| 1613 |
\_fi |
| 1614 |
}% |
| 1615 |
|
| 1616 |
\_def\.xorbit_saveresult(#1\_relax\_relax){% |
| 1617 |
%Strips off the extra '\_relax'es at the end. |
| 1618 |
\_def\.xorresult{#1}% |
| 1619 |
}% |
| 1620 |
|
| 1621 |
\_newifi\_ifqr_divisiondone |
| 1622 |
|
| 1623 |
\_def\.BCHcode#1{% |
| 1624 |
\_edef\.formatinfo{#1}% |
| 1625 |
\_def\.formatinfopadded{\.formatinfo 0000000000}% |
| 1626 |
\_def\.divisor{10100110111}% |
| 1627 |
\.divisiondonefalse |
| 1628 |
\.polynomialdivide{\.formatinfopadded}{\.divisor}% |
| 1629 |
% |
| 1630 |
\.getstringlength{\.theremainder}% |
| 1631 |
%Run loop from stringlength+1 to 10. |
| 1632 |
\.a=\.stringlength\_relax% |
| 1633 |
\_advance\.a by 1\_relax% |
| 1634 |
\.for \.iz = \.a to 10 by 1% |
| 1635 |
{% |
| 1636 |
\_xdef\.theremainder{0\.theremainder}% |
| 1637 |
}% |
| 1638 |
\_edef\.BCHresult{\.formatinfo\.theremainder}% |
| 1639 |
}% |
| 1640 |
|
| 1641 |
\_def\.formatmask{101010000010010}% |
| 1642 |
|
| 1643 |
\_def\.encodeandmaskformat#1{% |
| 1644 |
\.BCHcode{#1}% |
| 1645 |
\.xorbitstrings{\.BCHresult}{\.formatmask}% |
| 1646 |
\_edef\.formatbitstring{\.xorresult}% |
| 1647 |
}% |
| 1648 |
|
| 1649 |
\_def\.Golaycode#1{% |
| 1650 |
% #1 = 6-bit version number |
| 1651 |
\_edef\.versioninfo{#1}% |
| 1652 |
\_def\.versioninfopadded{\.versioninfo 000000000000}% %Append 12 zeros. |
| 1653 |
\_def\.divisor{1111100100101}% |
| 1654 |
\.divisiondonefalse |
| 1655 |
\.polynomialdivide{\.versioninfopadded}{\.divisor}% |
| 1656 |
% |
| 1657 |
\.getstringlength{\.theremainder}% |
| 1658 |
%Run loop from stringlength+1 to 12. |
| 1659 |
\.a=\.stringlength\_relax% |
| 1660 |
\_advance\.a by 1\_relax% |
| 1661 |
\.for \.iz = \.a to 12 by 1% |
| 1662 |
{% |
| 1663 |
\_xdef\.theremainder{0\.theremainder}% |
| 1664 |
}% |
| 1665 |
\_edef\.Golayresult{\.versioninfo\.theremainder}% |
| 1666 |
}% |
| 1667 |
\_def\.Fresult{}% |
| 1668 |
|
| 1669 |
\_def\.xorbitstring#1#2#3{% |
| 1670 |
% #1 = new macro to receive result |
| 1671 |
% #2, #3 = bitstrings to xor. The second can be shorter than the first. |
| 1672 |
\_def\.xor_result{}% |
| 1673 |
\_edef\.argument{(#2\_relax\_relax)(#3\_relax\_relax)}% |
| 1674 |
\_ea\.xorbitstring_recursive\.argument% |
| 1675 |
\_edef#1{\.xor_result}% |
| 1676 |
}% |
| 1677 |
\_def\.xorbitstring_recursive(#1#2)(#3#4){% |
| 1678 |
\_edef\.testi{#1}% |
| 1679 |
\_ifx\.testi\.relax% |
| 1680 |
%Done. |
| 1681 |
\let\.next=\_relax% |
| 1682 |
\_else |
| 1683 |
\_if#1#3\_relax |
| 1684 |
\.xaddto{\.xor_result}{0}% |
| 1685 |
\_else |
| 1686 |
\.xaddto{\.xor_result}{1}% |
| 1687 |
\_fi |
| 1688 |
\_edef\.next{\_noexpand\.xorbitstring_recursive(#2)(#4)}% |
| 1689 |
\_fi |
| 1690 |
\.next |
| 1691 |
} |
| 1692 |
|
| 1693 |
\_def\.Faddchar_raw#1#2{% |
| 1694 |
%Add two hexadecimal digits using bitwise xor |
| 1695 |
\.hextobinary[4]{\.summandA}{#1}% |
| 1696 |
\.hextobinary[4]{\.summandB}{#2}% |
| 1697 |
\.xorbitstring{\.Fresult}{\.summandA}{\.summandB}% |
| 1698 |
\.binarytohex[1]{\.Fresult}{\.Fresult}% |
| 1699 |
}% |
| 1700 |
|
| 1701 |
\_def\.canceltwos#1{% |
| 1702 |
\_edef\.argument{(#1\_relax\_relax)}% |
| 1703 |
\_ea\.canceltwos_int\.argument% |
| 1704 |
}% |
| 1705 |
|
| 1706 |
\_def\.canceltwos_int(#1#2){% |
| 1707 |
\_ea\.canceltwos_recursion(,#1#2)% |
| 1708 |
}% |
| 1709 |
|
| 1710 |
\_def\.canceltwos_recursion(#1,#2#3){% |
| 1711 |
\_def\.testii{#2}% |
| 1712 |
\_ifx\.testii\.relax |
| 1713 |
%Cancelling complete. |
| 1714 |
\.striptworelaxes(#1#2#3)% |
| 1715 |
%Now \.Fresult contains the answer. |
| 1716 |
\_else |
| 1717 |
\_relax |
| 1718 |
\_ifnum#2=2\_relax |
| 1719 |
\.canceltwos_recursion(#10,#3)% |
| 1720 |
\_else |
| 1721 |
\.canceltwos_recursion(#1#2,#3)% |
| 1722 |
\_fi |
| 1723 |
\_fi |
| 1724 |
}% |
| 1725 |
|
| 1726 |
\_def\.striptworelaxes(#1\_relax\_relax){% |
| 1727 |
\_gdef\.Fresult{#1}% |
| 1728 |
}% |
| 1729 |
|
| 1730 |
\.for \.iz = 0 to 15 by 1% |
| 1731 |
{\.decimaltohex[1]{\.tempa}{\_the\.iz}% |
| 1732 |
\.for \.jz = 0 to 15 by 1% |
| 1733 |
{\.decimaltohex[1]{\.tempb}{\_the\.jz}% |
| 1734 |
\.Faddchar_raw\.tempa\.tempb |
| 1735 |
\_ea\_xdef\_csname F@addchar@\.tempa\.tempb\_endcsname{\.Fresult}% |
| 1736 |
}% |
| 1737 |
}% |
| 1738 |
|
| 1739 |
\_def\.Faddchar#1#2{% |
| 1740 |
\_ea\_def\_ea\.Fresult\_ea{\_csname F@addchar@#1#2\_endcsname}% |
| 1741 |
}% |
| 1742 |
|
| 1743 |
\_def\.Faddstrings#1#2{% |
| 1744 |
\_edef\.argument{(,#1\_relax\_relax)(#2\_relax\_relax)}% |
| 1745 |
\_ea\.Faddstrings_recursion\.argument% |
| 1746 |
}% |
| 1747 |
|
| 1748 |
\_def\.Faddstrings_recursion(#1,#2#3)(#4#5){% |
| 1749 |
%Adds two hexadecimal strings, bitwise, from left to right. |
| 1750 |
%The second string is allowed to be shorter than the first. |
| 1751 |
\_def\.testii{#2}% |
| 1752 |
\_def\.testiv{#4}% |
| 1753 |
\_ifx\.testii\.relax |
| 1754 |
%The entire string has been processed. |
| 1755 |
\_gdef\.Fresult{#1}% |
| 1756 |
\_else |
| 1757 |
\_ifx\.testiv\.relax |
| 1758 |
%The second string is over. |
| 1759 |
\.striptworelaxes(#1#2#3)% |
| 1760 |
%Now \.Fresult contains the answer. |
| 1761 |
\_else |
| 1762 |
%We continue to add. |
| 1763 |
\.Faddchar{#2}{#4}% |
| 1764 |
\_edef\.argument{(#1\.Fresult,#3)(#5)}% |
| 1765 |
\_ea\.Faddstrings_recursion\.argument% |
| 1766 |
\_fi |
| 1767 |
\_fi |
| 1768 |
}% |
| 1769 |
\_gdef\.Fstripleadingzero(0#1){\_edef\.Fresult{#1}}% |
| 1770 |
|
| 1771 |
\.i=0% |
| 1772 |
\_def\.poweroftwo{1}% |
| 1773 |
\.for \.iz = 1 to 254 by 1% |
| 1774 |
{\_global\_advance\.i by1% |
| 1775 |
\.a=\.poweroftwo\_relax |
| 1776 |
\_multiply\.a by 2\_relax |
| 1777 |
\_edef\.poweroftwo{\_the\.a}% |
| 1778 |
\.decimaltohex[2]{\.poweroftwo_hex}{\.poweroftwo}% |
| 1779 |
\_ea\_ifnum\.poweroftwo>255\_relax |
| 1780 |
%We need to bitwise add the polynomial represented by 100011101, i.e. 0x11d. |
| 1781 |
\.Faddstrings{\.poweroftwo_hex}{11d}% %Now it should start with 0. |
| 1782 |
\_ea\.Fstripleadingzero\_ea(\.Fresult)% %Now it should be two hex digits. |
| 1783 |
\_edef\.poweroftwo_hex{\.Fresult}% %Save the hex version. |
| 1784 |
\.hextodecimal{\.poweroftwo}{\.Fresult}% |
| 1785 |
\_fi |
| 1786 |
\_xdef\.poweroftwo{\.poweroftwo}% |
| 1787 |
\_ea\_xdef\_csname F@twotothe@\_the\.i\_endcsname{\.poweroftwo_hex}% |
| 1788 |
\_ea\_xdef\_csname F@logtwo@\.poweroftwo_hex\_endcsname{\_the\.i}% |
| 1789 |
}% |
| 1790 |
\_ea\_xdef\_csname F@twotothe@0\_endcsname{01}% |
| 1791 |
\_ea\_xdef\_csname F@logtwo@01\_endcsname{0}% |
| 1792 |
|
| 1793 |
\_def\.Ftwotothe#1{% |
| 1794 |
\_ea\_xdef\_ea\.Fresult\_ea{\_csname F@twotothe@#1\_endcsname}% |
| 1795 |
}% |
| 1796 |
\_def\.Flogtwo#1{% |
| 1797 |
\_ea\_xdef\_ea\.Fresult\_ea{\_csname F@logtwo@#1\_endcsname}% |
| 1798 |
}% |
| 1799 |
|
| 1800 |
\_def\.zerozero{00}% |
| 1801 |
|
| 1802 |
\_def\.Fmultiply#1#2{% |
| 1803 |
% #1 and #2 are two elements of F_256, |
| 1804 |
% given as two-character hexadecimal strings. |
| 1805 |
% Multiply them within F_256, and place the answer in \.Fresult |
| 1806 |
\_edef\.argA{#1}% |
| 1807 |
\_edef\.argB{#2}% |
| 1808 |
\_ifx\.argA\.zerozero |
| 1809 |
\_def\.Fresult{00}% |
| 1810 |
\_else |
| 1811 |
\_ifx\.argB\.zerozero |
| 1812 |
\_def\.Fresult{00}% |
| 1813 |
\_else |
| 1814 |
\_ea\.Flogtwo\_ea{\.argA}% |
| 1815 |
\_edef\.logA{\.Fresult}% |
| 1816 |
\_ea\.Flogtwo\_ea{\.argB}% |
| 1817 |
\_edef\.logB{\.Fresult}% |
| 1818 |
\_ea\.a\_ea=\.logA\_relax% \.a = \logA |
| 1819 |
\_ea\_advance\_ea\.a\.logB\_relax% \advance \.a by \logB |
| 1820 |
\_ifnum\.a>254\_relax% |
| 1821 |
\_advance\.a by -255\_relax |
| 1822 |
\_fi |
| 1823 |
\_ea\.Ftwotothe\_ea{\_the\.a}% |
| 1824 |
% Now \.Fresult contains the product, as desired. |
| 1825 |
\_fi |
| 1826 |
\_fi |
| 1827 |
}% |
| 1828 |
|
| 1829 |
\_newifi\_ifqr_FXleadingcoeff_zero |
| 1830 |
\_def\.FXtestleadingcoeff(#1#2#3){% |
| 1831 |
% Tests whether the leading coefficient of the hex-string #1#2#3 is '00'. |
| 1832 |
\_edef\.FXleadingcoefficient{#1#2}% |
| 1833 |
\.FXleadingcoeff_zerofalse |
| 1834 |
\_ifx\.FXleadingcoefficient\.zerozero |
| 1835 |
\.FXleadingcoeff_zerotrue |
| 1836 |
\_fi |
| 1837 |
}% |
| 1838 |
|
| 1839 |
\_newifi\_ifqr_FXdivisiondone |
| 1840 |
|
| 1841 |
\_newcount\.divisionsremaining %Keep track of how many divisions to go! |
| 1842 |
\_def\.FXpolynomialdivide#1#2{% |
| 1843 |
\_edef\.FXnumerator{#1}% |
| 1844 |
\_edef\.denomin{#2}% |
| 1845 |
\.getstringlength\.FXnumerator |
| 1846 |
\.setcounter\.divisionsremaining{\.stringlength}% |
| 1847 |
\.getstringlength\.denomin |
| 1848 |
\.addtocounter\.divisionsremaining{-\.stringlength}% |
| 1849 |
\.addtocounter\.divisionsremaining{2}% |
| 1850 |
\_divide\.divisionsremaining by 2\_relax %2 hex chars per number |
| 1851 |
\.FXdivisiondonefalse |
| 1852 |
\_ea\_ea\_ea\.FXpolynomialdivide_recursive\_ea\_ea\_ea{\_ea\.FXnumerator\_ea}\_ea{\.denomin}% |
| 1853 |
}% |
| 1854 |
|
| 1855 |
\_def\.FXpolynomialdivide_recursive#1#2{% |
| 1856 |
% #1 = f(x), of degree n |
| 1857 |
% #2 = g(x), of degree m |
| 1858 |
% Obtains a new polynomial h(x), congruent to f(x) modulo g(x), |
| 1859 |
% but of degree at most n-1. |
| 1860 |
% |
| 1861 |
% If leading coefficient of f(x) is 0, strips off that leading zero. |
| 1862 |
% If leading coefficient of f(x) is a, subtracts off a * g(x) * x^(n-m). |
| 1863 |
% N.B. we assume g is monic. |
| 1864 |
% |
| 1865 |
\.FXtestleadingcoeff(#1)% |
| 1866 |
\_ifqr_FXleadingcoeff_zero |
| 1867 |
%Leading coefficient is zero, so remove it. |
| 1868 |
\_ea\_def\_ea\.FXnumerator\_ea{\.FXstripleadingzero(#1)}% |
| 1869 |
\_else% |
| 1870 |
%Leading coefficient is nonzero, and contained in \.FXleadingcoefficient |
| 1871 |
\.FXsubtractphase{#1}{#2}{\.FXleadingcoefficient}% |
| 1872 |
\_ifqr_FXsubtractfailed |
| 1873 |
%If subtraction failed, that means our #1 was already the remainder! |
| 1874 |
\.FXdivisiondonetrue |
| 1875 |
\_edef\.theremainder{#1}% |
| 1876 |
\_else% |
| 1877 |
%xor succeeded. We need to recurse. |
| 1878 |
\_ea\_ea\_ea\_edef\_ea\_ea\_ea\.FXnumerator\_ea\_ea\_ea{\_ea\.FXstripleadingzero\_ea(\.FXsubtraction_result)}% |
| 1879 |
\_fi% |
| 1880 |
\_fi% |
| 1881 |
\.addtocounter\.divisionsremaining{-1}% |
| 1882 |
\_ifnum\.divisionsremaining=0\_relax |
| 1883 |
%Division is done! |
| 1884 |
\.FXdivisiondonetrue |
| 1885 |
\_edef\.theremainder{\.FXnumerator}% |
| 1886 |
\_relax% |
| 1887 |
\_else% |
| 1888 |
\_ea\.FXpolynomialdivide_recursive\_ea{\.FXnumerator}{#2}% |
| 1889 |
\_fi% |
| 1890 |
}% |
| 1891 |
|
| 1892 |
\_def\.FXstripleadingzero(00#1){#1}%Strips off a single leading zero of F_256. |
| 1893 |
|
| 1894 |
\_newifi\_ifqr_FXsubtractfailed% This flag will trigger when #2 is longer than #1. |
| 1895 |
|
| 1896 |
\_def\.FXsubtractphase#1#2#3{% |
| 1897 |
% #1 = bitstring |
| 1898 |
% #2 = bitstring no longer than #1 |
| 1899 |
% #3 = leading coefficient |
| 1900 |
\.FXsubtractfailedfalse |
| 1901 |
\_edef\.argument{(,#1\_relax\_relax\_relax)(#2\_relax\_relax\_relax)(#3)}% |
| 1902 |
\_ea\.FXsubtract_recursive\.argument% |
| 1903 |
}% |
| 1904 |
|
| 1905 |
\_def\.FXsubtract_recursive(#1,#2#3#4)(#5#6#7)(#8){% |
| 1906 |
% This is a recursive way to compute f(x) - a*g(x)*x^k. |
| 1907 |
% #1#2#3#4 is the first bitstring, subtracted up through #1. |
| 1908 |
% Thus #2#3 constitutes the next two-character coefficient. |
| 1909 |
% #5#6#7 is the remaining portion of the second bitstring. |
| 1910 |
% Thus #5#6 constitutes the next two-character coefficient |
| 1911 |
% #8 is the element a of F_256. It should contain two characters. |
| 1912 |
\_def\.testii{#2}% |
| 1913 |
\_def\.testv{#5}% |
| 1914 |
\_ifx\.testii\.relax |
| 1915 |
% #1 contains the whole string. |
| 1916 |
% Now if #5 is also \_relax, that means the two strings started off with equal lengths. |
| 1917 |
% If, however, #5 is not \_relax, that means the second string was longer than the first, a problem. |
| 1918 |
\_ifx\.testv\.relax |
| 1919 |
%No problem. We are done. |
| 1920 |
\.FXsubtract_saveresult(#1#2#3#4)% %We keep the #2#3#4 to be sure we have all three relax-es to strip off. |
| 1921 |
\_else |
| 1922 |
%Problem! The second string was longer than the first. |
| 1923 |
%This usually indicates the end of the long division process. |
| 1924 |
\.FXsubtractfailedtrue |
| 1925 |
\_def\.FXsubtraction_result{}% |
| 1926 |
\_fi |
| 1927 |
\_else |
| 1928 |
% There is still a coefficient to manipulate in #2#3. |
| 1929 |
% Check whether #5 contains anything. |
| 1930 |
\_ifx\.testv\.relax |
| 1931 |
% No, #5 is empty. We are done. "#2#3#4" contains the remainder of the first string, |
| 1932 |
% which we append untouched and then strip off the three \_relax-es. |
| 1933 |
\.FXsubtract_saveresult(#1#2#3#4)% |
| 1934 |
\_else |
| 1935 |
% Yes, #5#6 still has something to XOR. Do the task. |
| 1936 |
\.Fmultiply{#5#6}{#8}% Multiply by the factor 'a'. |
| 1937 |
\.Faddstrings{#2#3}{\.Fresult}% Subtract. (We're in characteristic two, so adding works.) |
| 1938 |
\_edef\.argument{(#1\.Fresult,#4)(#7)(#8)}% |
| 1939 |
\_ea\.FXsubtract_recursive\.argument% |
| 1940 |
\_fi |
| 1941 |
\_fi |
| 1942 |
}% |
| 1943 |
|
| 1944 |
\_def\.FXsubtract_saveresult(#1\_relax\_relax\_relax){% |
| 1945 |
%Strips off the three extra '\_relax'es at the end. |
| 1946 |
\_def\.FXsubtraction_result{#1}% |
| 1947 |
}% |
| 1948 |
|
| 1949 |
\_def\.FXcreategeneratorpolynomial#1{% |
| 1950 |
% #1 = n, the number of error codewords desired. |
| 1951 |
% We need to create \prod_{j=0}^{n-1} (x-2^j). |
| 1952 |
\_edef\.FXgenerator_degree{#1}% |
| 1953 |
\_def\.FXgeneratorpolynomial{01}% Initially, set it equal to 1. |
| 1954 |
\.setcounter\.i{0}% |
| 1955 |
\.FXcreategenerator_recursive% |
| 1956 |
%The result is now stored in \.FXgeneratorpolynomial |
| 1957 |
}% |
| 1958 |
|
| 1959 |
\_def\.FXcreategenerator_recursive{% |
| 1960 |
% \.FXgeneratorpolynomial contains the current polynomial f(x), |
| 1961 |
% which should be a degree-i polynomial |
| 1962 |
% equal to \prod_{j=0}^{i-1} (x-2^j). |
| 1963 |
% (If i=0, then \.FXgeneratorpolynomial should be 01.) |
| 1964 |
% This recursion step should multiply the existing polynomial by (x-2^i), |
| 1965 |
% increment i by 1, and check whether we're done or not. |
| 1966 |
\_edef\.summandA{\.FXgeneratorpolynomial 00}% This is f(x) * x |
| 1967 |
\_edef\.summandB{00\.FXgeneratorpolynomial}% This is f(x), with a 0x^{i+1} in front. |
| 1968 |
\.Ftwotothe{\_the\.i}% |
| 1969 |
\_edef\.theconstant{\.Fresult}% |
| 1970 |
\.FXsubtractphase{\.summandA}{\.summandB}{\.theconstant}% |
| 1971 |
%This calculates \.summandA + \.theconstant * \.summandB |
| 1972 |
%and stores the result in \.FXsubtraction_result |
| 1973 |
\_edef\.FXgeneratorpolynomial{\.FXsubtraction_result}% |
| 1974 |
\.stepcounter\.i |
| 1975 |
\_ea \_ifnum\.FXgenerator_degree=\.i\_relax |
| 1976 |
%We just multiplied by (x-2^{n-1}), so we're done. |
| 1977 |
\_relax |
| 1978 |
\_else% |
| 1979 |
%We need to do this again! |
| 1980 |
\_ea \.FXcreategenerator_recursive |
| 1981 |
\_fi |
| 1982 |
}% |
| 1983 |
|
| 1984 |
\_def\.FXgenerate_errorbytes#1#2{% |
| 1985 |
% #1 = datastream in hex |
| 1986 |
% #2 = number of error correction bytes requested |
| 1987 |
\_edef\.numerrorbytes{#2}% |
| 1988 |
\_ea\.FXcreategeneratorpolynomial\_ea{\.numerrorbytes}% |
| 1989 |
\_edef\.FXnumerator{#1}% |
| 1990 |
\.for \.iz = 1 to \.numerrorbytes by 1% |
| 1991 |
{\.xaddto\.FXnumerator{00}}% %One error byte means two hex codes. |
| 1992 |
\.FXpolynomialdivide{\.FXnumerator}{\.FXgeneratorpolynomial}% |
| 1993 |
\_edef\.FXerrorbytes{\.theremainder}% |
| 1994 |
}% |
| 1995 |
\_newifi\_ifqr_versionmodules |
| 1996 |
|
| 1997 |
\_def\.level_char#1{% |
| 1998 |
\_ea\_ifcase#1 |
| 1999 |
M\_or L\_or H\_or Q\_fi}% |
| 2000 |
|
| 2001 |
\_newifi\_ifqr_versiongoodenough |
| 2002 |
\_def\.choose_bestversion#1{% |
| 2003 |
% \.desiredversion = user-requested version |
| 2004 |
% \.desiredlevel = user-requested error-correction level |
| 2005 |
\_edef\.stringlength{\.utfstringlen{#1}}% |
| 2006 |
% |
| 2007 |
%Run double loop over levels and versions, looking for |
| 2008 |
%the smallest version that can contain our data, |
| 2009 |
%and then choosing the best error-correcting level at that version, |
| 2010 |
%subject to the level being at least as good as the user desires. |
| 2011 |
\_global\.versiongoodenoughfalse% |
| 2012 |
\_gdef\.bestversion{0}% |
| 2013 |
\_gdef\.bestlevel{0}% |
| 2014 |
\_ifnum\.desiredversion=0\_relax |
| 2015 |
\.a=1\_relax |
| 2016 |
\_else |
| 2017 |
\.a=\.desiredversion\_relax |
| 2018 |
\_fi |
| 2019 |
\.for \.iz=\.a to 40 by 1 |
| 2020 |
{\_edef\.version{\_the\.iz}% |
| 2021 |
\_global\.versiongoodenoughfalse |
| 2022 |
\.for \.jz=0 to 3 by 1% |
| 2023 |
{%First, we map {0,1,2,3} to {1,0,4,3}, so that we loop through {M,L,H,Q} |
| 2024 |
%in order of increasing error-correction capabilities. |
| 2025 |
\.a = \.jz\_relax |
| 2026 |
\_divide \.a by 2\_relax |
| 2027 |
\_multiply \.a by 4\_relax |
| 2028 |
\_advance \.a by 1\_relax |
| 2029 |
\_advance \.a by -\.jz\_relax |
| 2030 |
\_edef\.level{\_the\.a}% |
| 2031 |
\_ifnum\.desiredlevel=\.a\_relax |
| 2032 |
\_global\.versiongoodenoughtrue |
| 2033 |
\_fi |
| 2034 |
\_ifqr_versiongoodenough |
| 2035 |
\.calculate_capacity{\.version}{\.level}% |
| 2036 |
\_ea\_ea\_ea\_ifnum\_ea\.truecapacity\_ea<\.stringlength\_relax |
| 2037 |
%Too short |
| 2038 |
\_relax |
| 2039 |
\_else |
| 2040 |
%Long enough! |
| 2041 |
\_xdef\.bestversion{\.version}% |
| 2042 |
\_xdef\.bestlevel{\.level}% |
| 2043 |
\_global\.iz=40% |
| 2044 |
\_fi |
| 2045 |
\_fi |
| 2046 |
}% |
| 2047 |
}% |
| 2048 |
\_edef\.version{\.bestversion}% |
| 2049 |
\_edef\.level{\.bestlevel}% |
| 2050 |
\_ea\_ifnum\.desiredversion>0\_relax |
| 2051 |
\_ifx\.bestversion\.desiredversion\_relax |
| 2052 |
%No change from desired version. |
| 2053 |
\_else |
| 2054 |
%Version was increased |
| 2055 |
\.message{<Requested QR version '\.desiredversion' is too small for desired text.}% |
| 2056 |
\.message{Version increased to '\.bestversion' to fit text.>^^J}% |
| 2057 |
\_fi |
| 2058 |
\_fi |
| 2059 |
\_ifx\.bestlevel\.desiredlevel\_relax |
| 2060 |
%No change in level. |
| 2061 |
\_else |
| 2062 |
\.message{<Error-correction level increased from \.level_char{\.desiredlevel}}% |
| 2063 |
\.message{to \.level_char{\.bestlevel} at no cost.>^^J}% |
| 2064 |
\_fi |
| 2065 |
}% |
| 2066 |
|
| 2067 |
\_def\.calculate_capacity#1#2{% |
| 2068 |
\_edef\.version{#1}% |
| 2069 |
\_edef\.level{#2}% |
| 2070 |
%Calculate \.size, the number of modules per side. |
| 2071 |
% The formula is 4\.version+17. |
| 2072 |
\.a=\.version\_relax |
| 2073 |
\_multiply\.a by 4\_relax |
| 2074 |
\_advance\.a by 17\_relax |
| 2075 |
\_xdef\.size{\_the\.a}% |
| 2076 |
% |
| 2077 |
% Calculate \.k, which governs the number of alignment patterns. |
| 2078 |
% The alignment patterns lie in a kxk square, except for 3 that are replaced by finding patterns. |
| 2079 |
% The formula is 2 + floor( \.version / 7 ), except that k=0 for version 1. |
| 2080 |
\_ea\_ifnum\.version=1\_relax% |
| 2081 |
\_def\.k{0}% |
| 2082 |
\_else% |
| 2083 |
\.a=\.version\_relax |
| 2084 |
\_divide \.a by 7\_relax |
| 2085 |
\_advance\.a by 2\_relax |
| 2086 |
\_edef\.k{\_the\.a}% |
| 2087 |
\_fi% |
| 2088 |
% |
| 2089 |
%Calculate number of function pattern modules. |
| 2090 |
%This consists of the three 8x8 finder patterns, the two timing strips, and the (k^2-3) 5x5 alignment patterns. |
| 2091 |
%The formula is 160+2n+25(k^2-3)-10(k-2), unless k=0 in which case we just have 160+2n. |
| 2092 |
\.a=\.size\_relax |
| 2093 |
\_multiply\.a by 2\_relax |
| 2094 |
\_advance\.a by 160\_relax |
| 2095 |
\_ea\_ifnum\.k=0\_relax\_else |
| 2096 |
%\.k is nonzero, hence at least 2, so we continue to add 25(k^2-3)-10(k-2). |
| 2097 |
\.b=\.k\_relax |
| 2098 |
\_multiply\.b by \.k\_relax |
| 2099 |
\_advance\.b by -3\_relax |
| 2100 |
\_multiply\.b by 25\_relax |
| 2101 |
\_advance\.a by \.b\_relax |
| 2102 |
\.b=\.k\_relax |
| 2103 |
\_advance\.b by -2\_relax |
| 2104 |
\_multiply\.b by 10\_relax |
| 2105 |
\_advance\.a by -\.b\_relax |
| 2106 |
\_fi |
| 2107 |
\_edef\.numfunctionpatternmodules{\_the\.a}% |
| 2108 |
% |
| 2109 |
%Calculate the number of version modules, either 36 or 0. |
| 2110 |
\_ea\_ifnum\.version>6\_relax |
| 2111 |
\.versionmodulestrue |
| 2112 |
\_def\.numversionmodules{36}% |
| 2113 |
\_else |
| 2114 |
\.versionmodulesfalse |
| 2115 |
\_def\.numversionmodules{0}% |
| 2116 |
\_fi |
| 2117 |
% |
| 2118 |
%Now calculate the codeword capacity and remainder bits. |
| 2119 |
%Take n^2 modules, subtract all those dedicated to finder patterns etc., format information, and version information, |
| 2120 |
%and what's left is the number of bits we can play with. |
| 2121 |
%The number of complete bytes is \.numdatacodewords; |
| 2122 |
%the leftover bits are \.numremainderbits. |
| 2123 |
\.a=\.size\_relax |
| 2124 |
\_multiply \.a by \.size\_relax |
| 2125 |
\_advance \.a by -\.numfunctionpatternmodules\_relax |
| 2126 |
\_advance \.a by -31\_relax% % There are 31 format modules. |
| 2127 |
\_advance \.a by -\.numversionmodules\_relax |
| 2128 |
\.b=\.a\_relax |
| 2129 |
\_divide \.a by 8\_relax |
| 2130 |
\_edef\.numdatacodewords{\_the\.a}% |
| 2131 |
\_multiply\.a by 8\_relax |
| 2132 |
\_advance \.b by -\.a\_relax |
| 2133 |
\_edef\.numremainderbits{\_the\.b}% |
| 2134 |
% |
| 2135 |
%The size of the character count indicator also varies by version. |
| 2136 |
%There are only two options, so hardcoding seems easier than expressing these functionally. |
| 2137 |
\_ea\_ifnum\.version<10\_relax |
| 2138 |
\_def\.charactercountbytes_byte{1}% |
| 2139 |
\_def\.charactercountbits_byte{8}% |
| 2140 |
\_else |
| 2141 |
\_def\.charactercountbytes_byte{2}% |
| 2142 |
\_def\.charactercountbits_byte{16}% |
| 2143 |
\_fi |
| 2144 |
% |
| 2145 |
%Now we call on the table, from the QR specification, |
| 2146 |
%of how many blocks to divide the message into, and how many error bytes each block gets. |
| 2147 |
%This affects the true capacity for data, which we store into \.totaldatacodewords. |
| 2148 |
% The following macro sets \.numblocks and \.numeccodewords |
| 2149 |
% based on Table 9 of the QR specification. |
| 2150 |
\.settableix |
| 2151 |
\.a = -\.numblocks\_relax |
| 2152 |
\_multiply \.a by \.numeccodewords\_relax |
| 2153 |
\_advance\.a by \.numdatacodewords\_relax |
| 2154 |
\_edef\.totaldatacodewords{\_the\.a}% |
| 2155 |
\_advance\.a by -\.charactercountbytes_byte\_relax%Subtract character count |
| 2156 |
\_advance\.a by -1\_relax% Subtract 1 byte for the 4-bit mode indicator and the 4-bit terminator at the end. |
| 2157 |
\_edef\.truecapacity{\_the\.a}% |
| 2158 |
} |
| 2159 |
|
| 2160 |
\_def\.setversion#1#2{% |
| 2161 |
% #1 = version number, an integer between 1 and 40 inclusive. |
| 2162 |
% #2 = error-correction level, as an integer between 0 and 3 inclusive. |
| 2163 |
% 0 = 00 = M |
| 2164 |
% 1 = 01 = L |
| 2165 |
% 2 = 10 = H |
| 2166 |
% 3 = 11 = Q |
| 2167 |
% This macro calculates and sets a variety of global macros and/or counters |
| 2168 |
% storing version information that is used later in construction the QR code. |
| 2169 |
% Thus \setversion should be called every time! |
| 2170 |
% |
| 2171 |
\_edef\.version{#1}% |
| 2172 |
\_edef\.level{#2}% |
| 2173 |
% |
| 2174 |
\.calculate_capacity{\.version}{\.level}% |
| 2175 |
%The capacity-check code sets the following: |
| 2176 |
% * \.size |
| 2177 |
% * \.k |
| 2178 |
% * \_ifqr_versionmodules |
| 2179 |
% * \.numversionmodules |
| 2180 |
% * \.numdatacodewords |
| 2181 |
% * \.numremainderbits |
| 2182 |
% * \.charactercountbits_byte |
| 2183 |
% * \.charactercountbytes_byte |
| 2184 |
% * \.numblocks (via \.settableix) |
| 2185 |
% * \.numeccodewords (via \.settableix) |
| 2186 |
% * \.totaldatacodewords |
| 2187 |
% |
| 2188 |
% The alignment patterns' square is 7 modules in from each edge. |
| 2189 |
% They are spaced "as evenly as possible" with an even number of modules between each row/column, |
| 2190 |
% unevenness in division being accommodated by making the first such gap smaller. |
| 2191 |
% The formula seems to be |
| 2192 |
% general distance = 2*round((n-13)/(k-1)/2+0.25) |
| 2193 |
% = 2*floor((n-13)/(k-1)/2+0.75) |
| 2194 |
% = 2*floor( (2*(n-13)/(k-1)+3) / 4 ) |
| 2195 |
% = (((2*(n-13)) div (k-1) + 3 ) div 4 ) * 2 |
| 2196 |
% first distance = leftovers |
| 2197 |
% The 0.25 is to accommodate version 32, which is the only time we round down. |
| 2198 |
% Otherwise a simple 2*ceiling((n-13)/(k-1)/2) would have sufficed. |
| 2199 |
% |
| 2200 |
\.a = \.size\_relax |
| 2201 |
\_advance\.a by -13\_relax |
| 2202 |
\_multiply\.a by 2\_relax |
| 2203 |
\.b = \.k\_relax |
| 2204 |
\_advance \.b by -1\_relax |
| 2205 |
\_divide\.a by \.b\_relax |
| 2206 |
\_advance\.a by 3\_relax |
| 2207 |
\_divide\.a by 4\_relax |
| 2208 |
\_multiply\.a by 2\_relax |
| 2209 |
\_edef\.alignment_generalskip{\_the\.a}% |
| 2210 |
% |
| 2211 |
%Now set \.alignment_firstskip to (\.size-13)-(\.k-2)*\.alignment_generalskip % |
| 2212 |
\.a = \.k\_relax |
| 2213 |
\_advance\.a by -2\_relax |
| 2214 |
\_multiply\.a by -\.alignment_generalskip\_relax |
| 2215 |
\_advance\.a by \.size\_relax |
| 2216 |
\_advance\.a by -13\_relax |
| 2217 |
\_edef\.alignment_firstskip{\_the\.a}% |
| 2218 |
% |
| 2219 |
% |
| 2220 |
% |
| 2221 |
% Our \.totaldatacodewords bytes of data are broken up as evenly as possible |
| 2222 |
% into \.numblocks datablocks; some may be one byte longer than others. |
| 2223 |
% We set \.shortblock_size to floor(\.totaldatacodewords / \.numblocks) |
| 2224 |
% and \.numlongblocks to mod(\.totaldatacodewords , \.numblocks). |
| 2225 |
\.a=\.totaldatacodewords\_relax |
| 2226 |
\_divide\.a by \.numblocks\_relax |
| 2227 |
\_edef\.shortblock_size{\_the\.a}% |
| 2228 |
\_multiply\.a by -\.numblocks\_relax |
| 2229 |
\_advance\.a by \.totaldatacodewords\_relax |
| 2230 |
\_edef\.numlongblocks{\_the\.a}% |
| 2231 |
% |
| 2232 |
%Set \.longblock_size to \.shortblock_size+1. |
| 2233 |
\.a=\.shortblock_size\_relax |
| 2234 |
\_advance\.a by 1\_relax |
| 2235 |
\_edef\.longblock_size{\_the\.a}% |
| 2236 |
% |
| 2237 |
%Set \.numshortblocks to \.numblocks - \.numlongblocks |
| 2238 |
\.b=\.numblocks\_relax |
| 2239 |
\_advance\.b by -\.numlongblocks\_relax |
| 2240 |
\_edef\.numshortblocks{\_the\.b}% |
| 2241 |
}% |
| 2242 |
|
| 2243 |
\_def\.settableix_int(#1,#2){% |
| 2244 |
\_edef\.numblocks{#1}% |
| 2245 |
\_edef\.numeccodewords{#2}% |
| 2246 |
}% |
| 2247 |
|
| 2248 |
\_def\.settableix{% |
| 2249 |
\_ea\_ifcase\.level\_relax |
| 2250 |
%00: Level 'M', medium error correction |
| 2251 |
\_edef\.tempdata{(% |
| 2252 |
\_ifcase\.version\_relax |
| 2253 |
\_relax %There is no version 0. |
| 2254 |
\_or1,10% |
| 2255 |
\_or1,16% |
| 2256 |
\_or1,26% |
| 2257 |
\_or2,18% |
| 2258 |
\_or2,24% |
| 2259 |
\_or4,16% |
| 2260 |
\_or4,18% |
| 2261 |
\_or4,22% |
| 2262 |
\_or5,22% |
| 2263 |
\_or5,26% |
| 2264 |
\_or5,30% |
| 2265 |
\_or8,22% |
| 2266 |
\_or9,22% |
| 2267 |
\_or9,24% |
| 2268 |
\_or10,24% |
| 2269 |
\_or10,28% |
| 2270 |
\_or11,28% |
| 2271 |
\_or13,26% |
| 2272 |
\_or14,26% |
| 2273 |
\_or16,26% |
| 2274 |
\_or17,26% |
| 2275 |
\_or17,28% |
| 2276 |
\_or18,28% |
| 2277 |
\_or20,28% |
| 2278 |
\_or21,28% |
| 2279 |
\_or23,28% |
| 2280 |
\_or25,28% |
| 2281 |
\_or26,28% |
| 2282 |
\_or28,28% |
| 2283 |
\_or29,28% |
| 2284 |
\_or31,28% |
| 2285 |
\_or33,28% |
| 2286 |
\_or35,28% |
| 2287 |
\_or37,28% |
| 2288 |
\_or38,28% |
| 2289 |
\_or40,28% |
| 2290 |
\_or43,28% |
| 2291 |
\_or45,28% |
| 2292 |
\_or47,28% |
| 2293 |
\_or49,28% |
| 2294 |
\_fi)}% |
| 2295 |
\_or |
| 2296 |
%01: Level 'L', low error correction |
| 2297 |
\_edef\.tempdata{% |
| 2298 |
(\_ifcase\.version\_relax |
| 2299 |
\_relax %There is no version 0. |
| 2300 |
\_or 1,7% |
| 2301 |
\_or 1,10% |
| 2302 |
\_or 1,15% |
| 2303 |
\_or 1,20% |
| 2304 |
\_or 1,26% |
| 2305 |
\_or 2,18% |
| 2306 |
\_or 2,20% |
| 2307 |
\_or 2,24% |
| 2308 |
\_or 2,30% |
| 2309 |
\_or 4,18% |
| 2310 |
\_or 4,20% |
| 2311 |
\_or 4,24% |
| 2312 |
\_or 4,26% |
| 2313 |
\_or 4,30% |
| 2314 |
\_or 6,22% |
| 2315 |
\_or 6,24% |
| 2316 |
\_or 6,28% |
| 2317 |
\_or 6,30% |
| 2318 |
\_or 7,28% |
| 2319 |
\_or 8,28% |
| 2320 |
\_or 8,28% |
| 2321 |
\_or 9,28% |
| 2322 |
\_or 9,30% |
| 2323 |
\_or 10,30% |
| 2324 |
\_or 12,26% |
| 2325 |
\_or 12,28% |
| 2326 |
\_or 12,30% |
| 2327 |
\_or 13,30% |
| 2328 |
\_or 14,30% |
| 2329 |
\_or 15,30% |
| 2330 |
\_or 16,30% |
| 2331 |
\_or 17,30% |
| 2332 |
\_or 18,30% |
| 2333 |
\_or 19,30% |
| 2334 |
\_or 19,30% |
| 2335 |
\_or 20,30% |
| 2336 |
\_or 21,30% |
| 2337 |
\_or 22,30% |
| 2338 |
\_or 24,30% |
| 2339 |
\_or 25,30% |
| 2340 |
\_fi)}% |
| 2341 |
\_or |
| 2342 |
%10: Level 'H', high error correction |
| 2343 |
\_edef\.tempdata{(% |
| 2344 |
\_ifcase\.version\_relax |
| 2345 |
\_relax %There is no version 0. |
| 2346 |
\_or1,17% |
| 2347 |
\_or1,28% |
| 2348 |
\_or2,22% |
| 2349 |
\_or4,16% |
| 2350 |
\_or4,22% |
| 2351 |
\_or4,28% |
| 2352 |
\_or5,26% |
| 2353 |
\_or6,26% |
| 2354 |
\_or8,24% |
| 2355 |
\_or8,28% |
| 2356 |
\_or11,24% |
| 2357 |
\_or11,28% |
| 2358 |
\_or16,22% |
| 2359 |
\_or16,24% |
| 2360 |
\_or18,24% |
| 2361 |
\_or16,30% |
| 2362 |
\_or19,28% |
| 2363 |
\_or21,28% |
| 2364 |
\_or25,26% |
| 2365 |
\_or25,28% |
| 2366 |
\_or25,30% |
| 2367 |
\_or34,24% |
| 2368 |
\_or30,30% |
| 2369 |
\_or32,30% |
| 2370 |
\_or35,30% |
| 2371 |
\_or37,30% |
| 2372 |
\_or40,30% |
| 2373 |
\_or42,30% |
| 2374 |
\_or45,30% |
| 2375 |
\_or48,30% |
| 2376 |
\_or51,30% |
| 2377 |
\_or54,30% |
| 2378 |
\_or57,30% |
| 2379 |
\_or60,30% |
| 2380 |
\_or63,30% |
| 2381 |
\_or66,30% |
| 2382 |
\_or70,30% |
| 2383 |
\_or74,30% |
| 2384 |
\_or77,30% |
| 2385 |
\_or81,30% |
| 2386 |
\_fi)}% |
| 2387 |
\_or |
| 2388 |
%11: Level 'Q', quality error correction |
| 2389 |
\_edef\.tempdata{(% |
| 2390 |
\_ifcase\.version\_relax |
| 2391 |
\_relax %There is no version 0. |
| 2392 |
\_or1,13% |
| 2393 |
\_or1,22% |
| 2394 |
\_or2,18% |
| 2395 |
\_or2,26% |
| 2396 |
\_or4,18% |
| 2397 |
\_or4,24% |
| 2398 |
\_or6,18% |
| 2399 |
\_or6,22% |
| 2400 |
\_or8,20% |
| 2401 |
\_or8,24% |
| 2402 |
\_or8,28% |
| 2403 |
\_or10,26% |
| 2404 |
\_or12,24% |
| 2405 |
\_or16,20% |
| 2406 |
\_or12,30% |
| 2407 |
\_or17,24% |
| 2408 |
\_or16,28% |
| 2409 |
\_or18,28% |
| 2410 |
\_or21,26% |
| 2411 |
\_or20,30% |
| 2412 |
\_or23,28% |
| 2413 |
\_or23,30% |
| 2414 |
\_or25,30% |
| 2415 |
\_or27,30% |
| 2416 |
\_or29,30% |
| 2417 |
\_or34,28% |
| 2418 |
\_or34,30% |
| 2419 |
\_or35,30% |
| 2420 |
\_or38,30% |
| 2421 |
\_or40,30% |
| 2422 |
\_or43,30% |
| 2423 |
\_or45,30% |
| 2424 |
\_or48,30% |
| 2425 |
\_or51,30% |
| 2426 |
\_or53,30% |
| 2427 |
\_or56,30% |
| 2428 |
\_or59,30% |
| 2429 |
\_or62,30% |
| 2430 |
\_or65,30% |
| 2431 |
\_or68,30% |
| 2432 |
\_fi)}% |
| 2433 |
\_fi |
| 2434 |
\_ea\.settableix_int\.tempdata |
| 2435 |
}% |
| 2436 |
|
| 2437 |
\_def\.qM{M}\_def\.qz{0}% |
| 2438 |
\_def\.qL{L}\_def\.qi{1}% |
| 2439 |
\_def\.qH{H}\_def\.qii{2}% |
| 2440 |
\_def\.qQ{Q}\_def\.qiii{3}% |
| 2441 |
\_def\.setlevel#1{% |
| 2442 |
\_edef\.level_selected{#1}% |
| 2443 |
\_ifx\.level_selected\.qM |
| 2444 |
\_edef\.desiredlevel{0}% |
| 2445 |
\_fi |
| 2446 |
\_ifx\.level_selected\.qL |
| 2447 |
\_edef\.desiredlevel{1}% |
| 2448 |
\_fi |
| 2449 |
\_ifx\.level_selected\.qH |
| 2450 |
\_edef\.desiredlevel{2}% |
| 2451 |
\_fi |
| 2452 |
\_ifx\.level_selected\.qQ |
| 2453 |
\_edef\.desiredlevel{3}% |
| 2454 |
\_fi |
| 2455 |
\_ifx\.level_selected\.qz |
| 2456 |
\_edef\.desiredlevel{0}% |
| 2457 |
\_fi |
| 2458 |
\_ifx\.level_selected\.qi |
| 2459 |
\_edef\.desiredlevel{1}% |
| 2460 |
\_fi |
| 2461 |
\_ifx\.level_selected\.qii |
| 2462 |
\_edef\.desiredlevel{2}% |
| 2463 |
\_fi |
| 2464 |
\_ifx\.level_selected\.qiii |
| 2465 |
\_edef\.desiredlevel{3}% |
| 2466 |
\_fi |
| 2467 |
}% |
| 2468 |
|
| 2469 |
% key-value pairs (OPmac trick 0069) |
| 2470 |
\_def\.kv#1{\_ifcsname _qr_kv:#1\_endcsname \_csname _qr_kv:#1\_ea\_endcsname |
| 2471 |
\_else \_ea\.kvunknown \_fi |
| 2472 |
} |
| 2473 |
\_def\.kvunknown{???} |
| 2474 |
\_def\.kvscan #1#2=#3,{\_ifx#1,\_else \_sdef{_qr_kv:#1#2}{#3}\_ea\.kvscan\_fi} |
| 2475 |
|
| 2476 |
\_def\.qrsetup#1{\_def\.tmpb{#1,}% |
| 2477 |
\_replstring\.tmpb{ =}{=}\_replstring\.tmpb{= }{=}% |
| 2478 |
\_replstring\.tmpb{tight,}{qr-border=0,}% |
| 2479 |
\_replstring\.tmpb{padding,}{qr-border=1,}% |
| 2480 |
\_replstring\.tmpb{verbose,}{qr-message=1,}% |
| 2481 |
\_replstring\.tmpb{silent,}{qr-message=0,}% |
| 2482 |
\_replstring\.tmpb{draft,}{qr-final=0,}% |
| 2483 |
\_replstring\.tmpb{final,}{qr-final=1,}% |
| 2484 |
\_replstring\.tmpb{nolink,}{qr-link=0,}% |
| 2485 |
\_replstring\.tmpb{link,}{qr-link=1,}% |
| 2486 |
\_ea\.kvscan\.tmpb,=,% |
| 2487 |
\.desiredheight=\.kv{height}\_relax |
| 2488 |
\.setlevel{\.kv{level}}% |
| 2489 |
\_edef\.desiredversion{\.kv{version}}% |
| 2490 |
} |
| 2491 |
\.qrsetup{height=2cm, version=0, level=M, tight, silent, final, nolink} |
| 2492 |
|
| 2493 |
\_def\.setcounter #1#2{\_global#1=#2\_relax} |
| 2494 |
\_def\.stepcounter #1{\_global\_advance#1 by1\_relax} |
| 2495 |
\_def\.addtocounter#1#2{\_global\_advance#1 by#2\_relax} |
| 2496 |
|
| 2497 |
\_def\.qrcode{\_begingroup |
| 2498 |
\_isnextchar[{\.qrcodeA}{\.qrcodeB}% |
| 2499 |
} |
| 2500 |
\_def\.qrcodeA[#1]{\.qrsetup{#1}\_ea\.qrcodeB\_romannumeral-`\.} |
| 2501 |
\_def\.qrcodeB#1{% |
| 2502 |
\_if1\.kv{qr-message}\_let\.message=\_message \_else \_def\.message##1{}\_fi |
| 2503 |
\_if1\.kv{qr-border}\_def\.padd{\_kern4\.modulesize}\_else\_def\.padd{}\_fi |
| 2504 |
\_bgroup |
| 2505 |
\_escapechar=-1 |
| 2506 |
\_xdef\.etext{\_detokenize{#1}}\_gdef\.text{#1}% |
| 2507 |
\_egroup |
| 2508 |
\.qrcode_int |
| 2509 |
\_endgroup |
| 2510 |
} |
| 2511 |
\_def\.qrcode_int{% |
| 2512 |
\.message{<QR code requested for "\.etext" in version |
| 2513 |
\.desiredversion-\.level_char{\.desiredlevel}.>^^J}% |
| 2514 |
%First, choose the version and level. |
| 2515 |
%Recall that \.choose_bestversion sets \.version and \.level. |
| 2516 |
\_ea\.choose_bestversion\_ea{\.etext}% |
| 2517 |
\_if1\.kv{qr-final}% |
| 2518 |
\.setversion{\.version}{\.level}% |
| 2519 |
\.qrcode_int_new |
| 2520 |
\_else |
| 2521 |
\.modulesize=\.desiredheight |
| 2522 |
\_divide\.modulesize by \.size\_relax |
| 2523 |
\_let\.d=\.desiredheight |
| 2524 |
\_vbox{\.padd\_hbox{\.padd\_vbox to\.d{\_hrule\_vss |
| 2525 |
\_hbox to\.d{\_vrule height.7\.d depth.3\.d \_hss ...QR...\_hss\_vrule}% |
| 2526 |
\_vss\_hrule}\.padd}\.padd}% |
| 2527 |
\_fi |
| 2528 |
}% |
| 2529 |
|
| 2530 |
\_nspublic \qrcode \qrsetup ; |
| 2531 |
\_newpublic \_let\qrset=\.qrsetup % for backward compatibility |
| 2532 |
|
| 2533 |
\_def\.qrcode_int_new{% |
| 2534 |
\.qrbeginhook |
| 2535 |
\.createsquareblankmatrix{newqr}{\.size}% |
| 2536 |
\.placefinderpatterns{newqr}% |
| 2537 |
\.placetimingpatterns{newqr}% |
| 2538 |
\.placealignmentpatterns{newqr}% |
| 2539 |
\.placedummyformatpatterns{newqr}% |
| 2540 |
\.placedummyversionpatterns{newqr}% |
| 2541 |
\.message{<Calculating QR code for "\.etext" in |
| 2542 |
version \.version-\.level_char{\.level}.>^^J}% |
| 2543 |
\_ea\.encode_binary\_ea{\.etext}% |
| 2544 |
\.splitcodetextintoblocks |
| 2545 |
\.createerrorblocks |
| 2546 |
\.interleave |
| 2547 |
\.message{<Writing data...}% |
| 2548 |
\.writedata_hex{newqr}{\.interleaved_text}% |
| 2549 |
\.message{done.>^^J}% |
| 2550 |
\.writeremainderbits{newqr}% |
| 2551 |
\.chooseandapplybestmask{newqr}% |
| 2552 |
\.decimaltobinary[2]{\.level_binary}{\.level}% |
| 2553 |
\.decimaltobinary[3]{\.mask_binary}{\.mask_selected}% |
| 2554 |
\_edef\.formatstring{\.level_binary\.mask_binary}% |
| 2555 |
\.message{<Encoding and writing format string...}% |
| 2556 |
\_ea\.encodeandmaskformat\_ea{\.formatstring}% |
| 2557 |
\.writeformatstring{newqr}{\.formatbitstring}% |
| 2558 |
\.message{done.>^^J}% |
| 2559 |
\.message{<Encoding and writing version information...}% |
| 2560 |
\.decimaltobinary[6]{\.version_binary}{\.version}% |
| 2561 |
\.Golaycode{\.version_binary}% |
| 2562 |
\.writeversionstring{newqr}{\.Golayresult}% |
| 2563 |
\.message{done.>^^J}% |
| 2564 |
\.message{<Printing QR code...}% |
| 2565 |
\.matrixtobinary{newqr}% |
| 2566 |
\.restore\.data |
| 2567 |
\.message{done.>^^J}% |
| 2568 |
\.qrendhook |
| 2569 |
}% |
| 2570 |
|
| 2571 |
\_def\.matrixtobinary#1{% |
| 2572 |
\_bgroup |
| 2573 |
\_gdef\.data{}% |
| 2574 |
\_def\.black{1}\_let\.blackfixed=\.black \_let\.blackformat=\.black |
| 2575 |
\_def\.white{0}\_let\.whitefixed=\.white \_let\.whiteformat=\.white |
| 2576 |
\.for \.iz = 1 to \.size by 1 |
| 2577 |
{\.for \.jz = 1 to \.size by 1 |
| 2578 |
{\_xdef\.data{\.data\.matrixentry{#1}{\_the\.iz}{\_the\.jz}}}}% |
| 2579 |
\_xdef\.data{{\.size}{\.data}}% |
| 2580 |
\_egroup |
| 2581 |
} |
| 2582 |
|
| 2583 |
\_def\.restore#1{\_ea\.restoreA#1} |
| 2584 |
\_def\.restoreA#1#2{% |
| 2585 |
\.modulesize=\.desiredheight \_divide\.modulesize by#1 |
| 2586 |
\_if1\.kv{qr-link}\_setbox0=\_fi |
| 2587 |
\_vbox bdir0 \_bgroup\.padd \_offinterlineskip \_baselineskip=\.modulesize |
| 2588 |
\.i=0 \.j=0 \_let\.next=\.restoreB |
| 2589 |
\_hbox\_bgroup\.padd \.restoreB #2% |
| 2590 |
\_if1\.kv{qr-link}\.link{\.etext}{\_box0}\_fi |
| 2591 |
} |
| 2592 |
\_def\.restoreB#1{\_advance \.j by1 |
| 2593 |
\_ifx1#1\_vrule height\.modulesize width\.modulesize\_else \_kern\.modulesize\_fi |
| 2594 |
\_ifnum\.size=\.j \_vrule height\.modulesize width 0pt \.padd\_egroup \_advance\.i by1 |
| 2595 |
\_ifnum\.size=\.i \.padd\_egroup \_let\.next=\_relax \_else \_hbox\_bgroup\.padd \_fi |
| 2596 |
\_fi \.next |
| 2597 |
} |
| 2598 |
|
| 2599 |
\_def\.qrbeginhook{} |
| 2600 |
\_def\.qrendhook{} |
| 2601 |
|
| 2602 |
\_endnamespace |
| 2603 |
\_endcode |
| 2604 |
|
| 2605 |
This macro file is created for \OpTeX/. It is derived from: |
| 2606 |
|
| 2607 |
\begtt |
| 2608 |
% qrcode.tex |
| 2609 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2610 |
% Petr Olsak <[email protected]> Jul. 2015 |
| 2611 |
|
| 2612 |
% This macro qrcode.tex is (roughly speaking) a copy of qrcode.sty |
| 2613 |
% macro by Anders Hendrickson <[email protected]>, see |
| 2614 |
% http://www.ctan.org/tex-archive/macros/latex/contrib/qrcode |
| 2615 |
|
| 2616 |
% The main difference between qrcode.sty and qrcode.tex is, that |
| 2617 |
% the LaTeX ballast was removed from qrcode.sty by Petr Olsak. The result: |
| 2618 |
% The qrcode.tex macro can be used in plain TeX format too. |
| 2619 |
\endtt |
| 2620 |
|
| 2621 |
\secc Usage |
| 2622 |
|
| 2623 |
Load this macro file by `\load[qrcode]` and then the command |
| 2624 |
`\qrcode{<encoded text>}` or `\qrcode[<options>]{<encoded text>}` is ready to use. |
| 2625 |
|
| 2626 |
The parameter <encoded text> is used as is (without expanding). |
| 2627 |
It follows the same rules as in the `\code{<text>}` macro. |
| 2628 |
Use these rules if the encoded text includes a \TeX/ sensitive characters. |
| 2629 |
|
| 2630 |
The characters from all Unicode table are allowed in <encoded text>. |
| 2631 |
|
| 2632 |
\secc Options |
| 2633 |
|
| 2634 |
You can use `\qrsetup{<options>}` for global-like options and |
| 2635 |
`\qrcode[<options>]{<encoded text>}` for local options for one QR code. |
| 2636 |
The `\qrsetup{<options>}` is valid within a group (if exists) or in whole |
| 2637 |
document. |
| 2638 |
|
| 2639 |
Options are separated by comma and they are in two types: single |
| 2640 |
word or key=value format. Default options are: |
| 2641 |
|
| 2642 |
\begtt |
| 2643 |
\qrsetup{height=2cm, version=0, level=M, tight, silent, final, nolink} |
| 2644 |
\endtt |
| 2645 |
|
| 2646 |
The options are the same as described in `qrcode.pdf` at |
| 2647 |
\url{http://www.ctan.org/tex-archive/macros/latex/contrib/qrcode}. |
| 2648 |
In short: |
| 2649 |
|
| 2650 |
\begitems |
| 2651 |
* `height=dimen` ... The height of the QRcode without padding. |
| 2652 |
|
| 2653 |
* `version=number` ... Number 0 to 40 linearly depends on the density of QRcode. |
| 2654 |
The 0 means that the density is automatically selected. |
| 2655 |
|
| 2656 |
* `level=letter` ... L, M, Q o H (low, medium, quality, high) sets the amount |
| 2657 |
of redundancy in the code in order of error recovering. |
| 2658 |
|
| 2659 |
* `tight` ... Code without margins. |
| 2660 |
* `padding` ... 4module blank margins around the code. |
| 2661 |
|
| 2662 |
* `verbose` ... Information about calculating in terminal and in the log. |
| 2663 |
* `silent` ... No information about calculating. |
| 2664 |
|
| 2665 |
* `final` ... The QR code is calculated and printed. |
| 2666 |
* `draft` ... Only empty rectangle in the same size as QR code is printed. |
| 2667 |
|
| 2668 |
* `nolink` ... The QR code is not active hyperlink. |
| 2669 |
* `link` ... The QR code is active hyperlink to <encoded text>. |
| 2670 |
|
| 2671 |
* `qrborder={R G B}` ... The color of the frame around active hypertext space |
| 2672 |
if link option is set. R G B (red green blue) are decimal |
| 2673 |
numbers from 0 to 1. The frame is visible only in |
| 2674 |
pdf viewers. Default: invisible frame. |
| 2675 |
\enditems |
| 2676 |
|
| 2677 |
|
| 2678 |
Example: |
| 2679 |
|
| 2680 |
\begtt |
| 2681 |
\qrsetup{silent} % ... all codes will be silent in the log and terminal. |
| 2682 |
\qrcode [height=3cm, link, padding, qrborder={1 0 0}] {http://petr.olsak.net} |
| 2683 |
% ... 3cm QRcode as hyperlink |
| 2684 |
\endtt |
| 2685 |
|
| 2686 |
Note: |
| 2687 |
|
| 2688 |
The saving/restoring pre-calculated QRcodes isn't supported by default. |
| 2689 |
If you are printing the same QR codes repeatedly, use \setbox/\copy |
| 2690 |
technique. For example: |
| 2691 |
|
| 2692 |
\begtt |
| 2693 |
\newbox\mybox |
| 2694 |
\setbox\mybox=\hbox{\qrcode{encoded text}} |
| 2695 |
\copy\mybox \copy\mybox \copy\mybox etc. |
| 2696 |
\endtt |
| 2697 |
|
| 2698 |
If you have a huge amount of different QR codes, you can use draft/final |
| 2699 |
options. |
| 2700 |
|
| 2701 |
The `\_qr_data` macro is saved after each \qrcode calculation in the format |
| 2702 |
`{size}{111101011...001}` where size is the number of columns or rows in QR |
| 2703 |
square and second parameter includes size^2 ones or zeros which means black |
| 2704 |
or white modules (scanned left to right, top to bottom). Another information |
| 2705 |
can be retrieved from `\_qr_text` macro (encoded text before expanding) and |
| 2706 |
`\_qr_etext` macro (raw encoded text, each character is detokeinized). |
| 2707 |
The macros `\_qr_data`, `\_qr_text` and `\_qr_etext` are saved globally. |
| 2708 |
|