slitaz-forge diff pro/lib/html/qrcode.js @ rev 380
chksrc: handle TARBALL miss
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Thu Dec 12 11:17:16 2013 +0000 (2013-12-12) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/pro/lib/html/qrcode.js Thu Dec 12 11:17:16 2013 +0000 1.3 @@ -0,0 +1,733 @@ 1.4 +/* qr.js -- QR code generator in Javascript (revision 2011-01-19) 1.5 + * Written by Kang Seonghoon <public+qrjs@mearie.org>. 1.6 + * 1.7 + * This source code is in the public domain; if your jurisdiction does not 1.8 + * recognize the public domain the terms of Creative Commons CC0 license 1.9 + * apply. In the other words, you can always do what you want. 1.10 + */ 1.11 + 1.12 +var QRCode = (function(){ 1.13 + 1.14 +/* Quick overview: QR code composed of 2D array of modules (a rectangular 1.15 + * area that conveys one bit of information); some modules are fixed to help 1.16 + * the recognition of the code, and remaining data modules are further divided 1.17 + * into 8-bit code words which are augumented by Reed-Solomon error correcting 1.18 + * codes (ECC). There could be multiple ECCs, in the case the code is so large 1.19 + * that it is helpful to split the raw data into several chunks. 1.20 + * 1.21 + * The number of modules is determined by the code's "version", ranging from 1 1.22 + * (21x21) to 40 (177x177). How many ECC bits are used is determined by the 1.23 + * ECC level (L/M/Q/H). The number and size (and thus the order of generator 1.24 + * polynomial) of ECCs depend to the version and ECC level. 1.25 + */ 1.26 + 1.27 +// per-version information (cf. JIS X 0510:2004 pp. 30--36, 71) 1.28 +// 1.29 +// [0]: the degree of generator polynomial by ECC levels 1.30 +// [1]: # of code blocks by ECC levels 1.31 +// [2]: left-top positions of alignment patterns 1.32 +// 1.33 +// the number in this table (in particular, [0]) does not exactly match with 1.34 +// the numbers in the specficiation. see augumenteccs below for the reason. 1.35 +var VERSIONS = [ 1.36 + null, 1.37 + [[10, 7,17,13], [ 1, 1, 1, 1], []], 1.38 + [[16,10,28,22], [ 1, 1, 1, 1], [4,16]], 1.39 + [[26,15,22,18], [ 1, 1, 2, 2], [4,20]], 1.40 + [[18,20,16,26], [ 2, 1, 4, 2], [4,24]], 1.41 + [[24,26,22,18], [ 2, 1, 4, 4], [4,28]], 1.42 + [[16,18,28,24], [ 4, 2, 4, 4], [4,32]], 1.43 + [[18,20,26,18], [ 4, 2, 5, 6], [4,20,36]], 1.44 + [[22,24,26,22], [ 4, 2, 6, 6], [4,22,40]], 1.45 + [[22,30,24,20], [ 5, 2, 8, 8], [4,24,44]], 1.46 + [[26,18,28,24], [ 5, 4, 8, 8], [4,26,48]], 1.47 + [[30,20,24,28], [ 5, 4,11, 8], [4,28,52]], 1.48 + [[22,24,28,26], [ 8, 4,11,10], [4,30,56]], 1.49 + [[22,26,22,24], [ 9, 4,16,12], [4,32,60]], 1.50 + [[24,30,24,20], [ 9, 4,16,16], [4,24,44,64]], 1.51 + [[24,22,24,30], [10, 6,18,12], [4,24,46,68]], 1.52 + [[28,24,30,24], [10, 6,16,17], [4,24,48,72]], 1.53 + [[28,28,28,28], [11, 6,19,16], [4,28,52,76]], 1.54 + [[26,30,28,28], [13, 6,21,18], [4,28,54,80]], 1.55 + [[26,28,26,26], [14, 7,25,21], [4,28,56,84]], 1.56 + [[26,28,28,30], [16, 8,25,20], [4,32,60,88]], 1.57 + [[26,28,30,28], [17, 8,25,23], [4,26,48,70,92]], 1.58 + [[28,28,24,30], [17, 9,34,23], [4,24,48,72,96]], 1.59 + [[28,30,30,30], [18, 9,30,25], [4,28,52,76,100]], 1.60 + [[28,30,30,30], [20,10,32,27], [4,26,52,78,104]], 1.61 + [[28,26,30,30], [21,12,35,29], [4,30,56,82,108]], 1.62 + [[28,28,30,28], [23,12,37,34], [4,28,56,84,112]], 1.63 + [[28,30,30,30], [25,12,40,34], [4,32,60,88,116]], 1.64 + [[28,30,30,30], [26,13,42,35], [4,24,48,72,96,120]], 1.65 + [[28,30,30,30], [28,14,45,38], [4,28,52,76,100,124]], 1.66 + [[28,30,30,30], [29,15,48,40], [4,24,50,76,102,128]], 1.67 + [[28,30,30,30], [31,16,51,43], [4,28,54,80,106,132]], 1.68 + [[28,30,30,30], [33,17,54,45], [4,32,58,84,110,136]], 1.69 + [[28,30,30,30], [35,18,57,48], [4,28,56,84,112,140]], 1.70 + [[28,30,30,30], [37,19,60,51], [4,32,60,88,116,144]], 1.71 + [[28,30,30,30], [38,19,63,53], [4,28,52,76,100,124,148]], 1.72 + [[28,30,30,30], [40,20,66,56], [4,22,48,74,100,126,152]], 1.73 + [[28,30,30,30], [43,21,70,59], [4,26,52,78,104,130,156]], 1.74 + [[28,30,30,30], [45,22,74,62], [4,30,56,82,108,134,160]], 1.75 + [[28,30,30,30], [47,24,77,65], [4,24,52,80,108,136,164]], 1.76 + [[28,30,30,30], [49,25,81,68], [4,28,56,84,112,140,168]]]; 1.77 + 1.78 +// mode constants (cf. Table 2 in JIS X 0510:2004 p. 16) 1.79 +var MODE_TERMINATOR = 0; 1.80 +var MODE_NUMERIC = 1, MODE_ALPHANUMERIC = 2, MODE_OCTET = 4, MODE_KANJI = 8; 1.81 + 1.82 +// validation regexps 1.83 +var NUMERIC_REGEXP = /^\d*$/; 1.84 +var ALPHANUMERIC_REGEXP = /^[A-Za-z0-9 $%*+\-./:]*$/; 1.85 +var ALPHANUMERIC_OUT_REGEXP = /^[A-Z0-9 $%*+\-./:]*$/; 1.86 + 1.87 +// ECC levels (cf. Table 22 in JIS X 0510:2004 p. 45) 1.88 +var ECCLEVEL_L = 1, ECCLEVEL_M = 0, ECCLEVEL_Q = 3, ECCLEVEL_H = 2; 1.89 + 1.90 +// GF(2^8)-to-integer mapping with a reducing polynomial x^8+x^4+x^3+x^2+1 1.91 +// invariant: GF256_MAP[GF256_INVMAP[i]] == i for all i in [1,256) 1.92 +var GF256_MAP = [], GF256_INVMAP = [-1]; 1.93 +for (var i = 0, v = 1; i < 255; ++i) { 1.94 + GF256_MAP.push(v); 1.95 + GF256_INVMAP[v] = i; 1.96 + v = (v * 2) ^ (v >= 128 ? 0x11d : 0); 1.97 +} 1.98 + 1.99 +// generator polynomials up to degree 30 1.100 +// (should match with polynomials in JIS X 0510:2004 Appendix A) 1.101 +// 1.102 +// generator polynomial of degree K is product of (x-\alpha^0), (x-\alpha^1), 1.103 +// ..., (x-\alpha^(K-1)). by convention, we omit the K-th coefficient (always 1) 1.104 +// from the result; also other coefficients are written in terms of the exponent 1.105 +// to \alpha to avoid the redundant calculation. (see also calculateecc below.) 1.106 +var GF256_GENPOLY = [[]]; 1.107 +for (var i = 0; i < 30; ++i) { 1.108 + var prevpoly = GF256_GENPOLY[i], poly = []; 1.109 + for (var j = 0; j <= i; ++j) { 1.110 + var a = (j < i ? GF256_MAP[prevpoly[j]] : 0); 1.111 + var b = GF256_MAP[(i + (prevpoly[j-1] || 0)) % 255]; 1.112 + poly.push(GF256_INVMAP[a ^ b]); 1.113 + } 1.114 + GF256_GENPOLY.push(poly); 1.115 +} 1.116 + 1.117 +// alphanumeric character mapping (cf. Table 5 in JIS X 0510:2004 p. 19) 1.118 +var ALPHANUMERIC_MAP = {}; 1.119 +for (var i = 0; i < 45; ++i) { 1.120 + ALPHANUMERIC_MAP['0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'.charAt(i)] = i; 1.121 +} 1.122 + 1.123 +// mask functions in terms of row # and column # 1.124 +// (cf. Table 20 in JIS X 0510:2004 p. 42) 1.125 +var MASKFUNCS = [ 1.126 + function(i,j) { return (i+j) % 2 == 0; }, 1.127 + function(i,j) { return i % 2 == 0; }, 1.128 + function(i,j) { return j % 3 == 0; }, 1.129 + function(i,j) { return (i+j) % 3 == 0; }, 1.130 + function(i,j) { return (((i/2)|0) + ((j/3)|0)) % 2 == 0; }, 1.131 + function(i,j) { return (i*j) % 2 + (i*j) % 3 == 0; }, 1.132 + function(i,j) { return ((i*j) % 2 + (i*j) % 3) % 2 == 0; }, 1.133 + function(i,j) { return ((i+j) % 2 + (i*j) % 3) % 2 == 0; }]; 1.134 + 1.135 +// returns true when the version information has to be embeded. 1.136 +var needsverinfo = function(ver) { return ver > 6; }; 1.137 + 1.138 +// returns the size of entire QR code for given version. 1.139 +var getsizebyver = function(ver) { return 4 * ver + 17; }; 1.140 + 1.141 +// returns the number of bits available for code words in this version. 1.142 +var nfullbits = function(ver) { 1.143 + /* 1.144 + * |<--------------- n --------------->| 1.145 + * | |<----- n-17 ---->| | 1.146 + * +-------+ ///+-------+ ---- 1.147 + * | | ///| | ^ 1.148 + * | 9x9 | @@@@@ ///| 9x8 | | 1.149 + * | | # # # @5x5@ # # # | | | 1.150 + * +-------+ @@@@@ +-------+ | 1.151 + * # ---| 1.152 + * ^ | 1.153 + * # | 1.154 + * @@@@@ @@@@@ @@@@@ | n 1.155 + * @5x5@ @5x5@ @5x5@ n-17 1.156 + * @@@@@ @@@@@ @@@@@ | | 1.157 + * # | | 1.158 + * ////// v | 1.159 + * //////# ---| 1.160 + * +-------+ @@@@@ @@@@@ | 1.161 + * | | @5x5@ @5x5@ | 1.162 + * | 8x9 | @@@@@ @@@@@ | 1.163 + * | | v 1.164 + * +-------+ ---- 1.165 + * 1.166 + * when the entire code has n^2 modules and there are m^2-3 alignment 1.167 + * patterns, we have: 1.168 + * - 225 (= 9x9 + 9x8 + 8x9) modules for finder patterns and 1.169 + * format information; 1.170 + * - 2n-34 (= 2(n-17)) modules for timing patterns; 1.171 + * - 36 (= 3x6 + 6x3) modules for version information, if any; 1.172 + * - 25m^2-75 (= (m^2-3)(5x5)) modules for alignment patterns 1.173 + * if any, but 10m-20 (= 2(m-2)x5) of them overlaps with 1.174 + * timing patterns. 1.175 + */ 1.176 + var v = VERSIONS[ver]; 1.177 + var nbits = 16*ver*ver + 128*ver + 64; // finder, timing and format info. 1.178 + if (needsverinfo(ver)) nbits -= 36; // version information 1.179 + if (v[2].length) { // alignment patterns 1.180 + nbits -= 25 * v[2].length * v[2].length - 10 * v[2].length - 55; 1.181 + } 1.182 + return nbits; 1.183 +}; 1.184 + 1.185 +// returns the number of bits available for data portions (i.e. excludes ECC 1.186 +// bits but includes mode and length bits) in this version and ECC level. 1.187 +var ndatabits = function(ver, ecclevel) { 1.188 + var nbits = nfullbits(ver) & ~7; // no sub-octet code words 1.189 + var v = VERSIONS[ver]; 1.190 + nbits -= 8 * v[0][ecclevel] * v[1][ecclevel]; // ecc bits 1.191 + return nbits; 1.192 +} 1.193 + 1.194 +// returns the number of bits required for the length of data. 1.195 +// (cf. Table 3 in JIS X 0510:2004 p. 16) 1.196 +var ndatalenbits = function(ver, mode) { 1.197 + switch (mode) { 1.198 + case MODE_NUMERIC: return (ver < 10 ? 10 : ver < 27 ? 12 : 14); 1.199 + case MODE_ALPHANUMERIC: return (ver < 10 ? 9 : ver < 27 ? 11 : 13); 1.200 + case MODE_OCTET: return (ver < 10 ? 8 : 16); 1.201 + case MODE_KANJI: return (ver < 10 ? 8 : ver < 27 ? 10 : 12); 1.202 + } 1.203 +}; 1.204 + 1.205 +// returns the maximum length of data possible in given configuration. 1.206 +var getmaxdatalen = function(ver, mode, ecclevel) { 1.207 + var nbits = ndatabits(ver, ecclevel) - 4 - ndatalenbits(ver, mode); // 4 for mode bits 1.208 + switch (mode) { 1.209 + case MODE_NUMERIC: 1.210 + return ((nbits/10) | 0) * 3 + (nbits%10 < 4 ? 0 : nbits%10 < 7 ? 1 : 2); 1.211 + case MODE_ALPHANUMERIC: 1.212 + return ((nbits/11) | 0) * 2 + (nbits%11 < 6 ? 0 : 1); 1.213 + case MODE_OCTET: 1.214 + return (nbits/8) | 0; 1.215 + case MODE_KANJI: 1.216 + return (nbits/13) | 0; 1.217 + } 1.218 +}; 1.219 + 1.220 +// checks if the given data can be encoded in given mode, and returns 1.221 +// the converted data for the further processing if possible. otherwise 1.222 +// returns null. 1.223 +// 1.224 +// this function does not check the length of data; it is a duty of 1.225 +// encode function below (as it depends on the version and ECC level too). 1.226 +var validatedata = function(mode, data) { 1.227 + switch (mode) { 1.228 + case MODE_NUMERIC: 1.229 + if (!data.match(NUMERIC_REGEXP)) return null; 1.230 + return data; 1.231 + 1.232 + case MODE_ALPHANUMERIC: 1.233 + if (!data.match(ALPHANUMERIC_REGEXP)) return null; 1.234 + return data.toUpperCase(); 1.235 + 1.236 + case MODE_OCTET: 1.237 + if (typeof data === 'string') { // encode as utf-8 string 1.238 + var newdata = []; 1.239 + for (var i = 0; i < data.length; ++i) { 1.240 + var ch = data.charCodeAt(i); 1.241 + if (ch < 0x80) { 1.242 + newdata.push(ch); 1.243 + } else if (ch < 0x800) { 1.244 + newdata.push(0xc0 | (ch >> 6), 1.245 + 0x80 | (ch & 0x3f)); 1.246 + } else if (ch < 0x10000) { 1.247 + newdata.push(0xe0 | (ch >> 12), 1.248 + 0x80 | ((ch >> 6) & 0x3f), 1.249 + 0x80 | (ch & 0x3f)); 1.250 + } else { 1.251 + newdata.push(0xf0 | (ch >> 18), 1.252 + 0x80 | ((ch >> 12) & 0x3f), 1.253 + 0x80 | ((ch >> 6) & 0x3f), 1.254 + 0x80 | (ch & 0x3f)); 1.255 + } 1.256 + } 1.257 + return newdata; 1.258 + } else { 1.259 + return data; 1.260 + } 1.261 + } 1.262 +}; 1.263 + 1.264 +// returns the code words (sans ECC bits) for given data and configurations. 1.265 +// requires data to be preprocessed by validatedata. no length check is 1.266 +// performed, and everything has to be checked before calling this function. 1.267 +var encode = function(ver, mode, data, maxbuflen) { 1.268 + var buf = []; 1.269 + var bits = 0, remaining = 8; 1.270 + var datalen = data.length; 1.271 + 1.272 + // this function is intentionally no-op when n=0. 1.273 + var pack = function(x, n) { 1.274 + if (n >= remaining) { 1.275 + buf.push(bits | (x >> (n -= remaining))); 1.276 + while (n >= 8) buf.push((x >> (n -= 8)) & 255); 1.277 + bits = 0; 1.278 + remaining = 8; 1.279 + } 1.280 + if (n > 0) bits |= (x & ((1 << n) - 1)) << (remaining -= n); 1.281 + }; 1.282 + 1.283 + var nlenbits = ndatalenbits(ver, mode); 1.284 + pack(mode, 4); 1.285 + pack(datalen, nlenbits); 1.286 + 1.287 + switch (mode) { 1.288 + case MODE_NUMERIC: 1.289 + for (var i = 2; i < datalen; i += 3) { 1.290 + pack(parseInt(data.substring(i-2,i+1), 10), 10); 1.291 + } 1.292 + pack(parseInt(data.substring(i-2), 10), [0,4,7][datalen%3]); 1.293 + break; 1.294 + 1.295 + case MODE_ALPHANUMERIC: 1.296 + for (var i = 1; i < datalen; i += 2) { 1.297 + pack(ALPHANUMERIC_MAP[data.charAt(i-1)] * 45 + 1.298 + ALPHANUMERIC_MAP[data.charAt(i)], 11); 1.299 + } 1.300 + if (datalen % 2 == 1) { 1.301 + pack(ALPHANUMERIC_MAP[data.charAt(i-1)], 6); 1.302 + } 1.303 + break; 1.304 + 1.305 + case MODE_OCTET: 1.306 + for (var i = 0; i < datalen; ++i) { 1.307 + pack(data[i], 8); 1.308 + } 1.309 + break; 1.310 + }; 1.311 + 1.312 + // final bits. it is possible that adding terminator causes the buffer 1.313 + // to overflow, but then the buffer truncated to the maximum size will 1.314 + // be valid as the truncated terminator mode bits and padding is 1.315 + // identical in appearance (cf. JIS X 0510:2004 sec 8.4.8). 1.316 + pack(MODE_TERMINATOR, 4); 1.317 + if (remaining < 8) buf.push(bits); 1.318 + 1.319 + // the padding to fill up the remaining space. we should not add any 1.320 + // words when the overflow already occurred. 1.321 + while (buf.length + 1 < maxbuflen) buf.push(0xec, 0x11); 1.322 + if (buf.length < maxbuflen) buf.push(0xec); 1.323 + return buf; 1.324 +}; 1.325 + 1.326 +// calculates ECC code words for given code words and generator polynomial. 1.327 +// 1.328 +// this is quite similar to CRC calculation as both Reed-Solomon and CRC use 1.329 +// the certain kind of cyclic codes, which is effectively the division of 1.330 +// zero-augumented polynomial by the generator polynomial. the only difference 1.331 +// is that Reed-Solomon uses GF(2^8), instead of CRC's GF(2), and Reed-Solomon 1.332 +// uses the different generator polynomial than CRC's. 1.333 +var calculateecc = function(poly, genpoly) { 1.334 + var modulus = poly.slice(0); 1.335 + var polylen = poly.length, genpolylen = genpoly.length; 1.336 + for (var i = 0; i < genpolylen; ++i) modulus.push(0); 1.337 + for (var i = 0; i < polylen; ) { 1.338 + var quotient = GF256_INVMAP[modulus[i++]]; 1.339 + if (quotient >= 0) { 1.340 + for (var j = 0; j < genpolylen; ++j) { 1.341 + modulus[i+j] ^= GF256_MAP[(quotient + genpoly[j]) % 255]; 1.342 + } 1.343 + } 1.344 + } 1.345 + return modulus.slice(polylen); 1.346 +}; 1.347 + 1.348 +// auguments ECC code words to given code words. the resulting words are 1.349 +// ready to be encoded in the matrix. 1.350 +// 1.351 +// the much of actual augumenting procedure follows JIS X 0510:2004 sec 8.7. 1.352 +// the code is simplified using the fact that the size of each code & ECC 1.353 +// blocks is almost same; for example, when we have 4 blocks and 46 data words 1.354 +// the number of code words in those blocks are 11, 11, 12, 12 respectively. 1.355 +var augumenteccs = function(poly, nblocks, genpoly) { 1.356 + var subsizes = []; 1.357 + var subsize = (poly.length / nblocks) | 0, subsize0 = 0; 1.358 + var pivot = nblocks - poly.length % nblocks; 1.359 + for (var i = 0; i < pivot; ++i) { 1.360 + subsizes.push(subsize0); 1.361 + subsize0 += subsize; 1.362 + } 1.363 + for (var i = pivot; i < nblocks; ++i) { 1.364 + subsizes.push(subsize0); 1.365 + subsize0 += subsize+1; 1.366 + } 1.367 + subsizes.push(subsize0); 1.368 + 1.369 + var eccs = []; 1.370 + for (var i = 0; i < nblocks; ++i) { 1.371 + eccs.push(calculateecc(poly.slice(subsizes[i], subsizes[i+1]), genpoly)); 1.372 + } 1.373 + 1.374 + var result = []; 1.375 + var nitemsperblock = (poly.length / nblocks) | 0; 1.376 + for (var i = 0; i < nitemsperblock; ++i) { 1.377 + for (var j = 0; j < nblocks; ++j) { 1.378 + result.push(poly[subsizes[j] + i]); 1.379 + } 1.380 + } 1.381 + for (var j = pivot; j < nblocks; ++j) { 1.382 + result.push(poly[subsizes[j+1] - 1]); 1.383 + } 1.384 + for (var i = 0; i < genpoly.length; ++i) { 1.385 + for (var j = 0; j < nblocks; ++j) { 1.386 + result.push(eccs[j][i]); 1.387 + } 1.388 + } 1.389 + return result; 1.390 +}; 1.391 + 1.392 +// auguments BCH(p+q,q) code to the polynomial over GF(2), given the proper 1.393 +// genpoly. the both input and output are in binary numbers, and unlike 1.394 +// calculateecc genpoly should include the 1 bit for the highest degree. 1.395 +// 1.396 +// actual polynomials used for this procedure are as follows: 1.397 +// - p=10, q=5, genpoly=x^10+x^8+x^5+x^4+x^2+x+1 (JIS X 0510:2004 Appendix C) 1.398 +// - p=18, q=6, genpoly=x^12+x^11+x^10+x^9+x^8+x^5+x^2+1 (ibid. Appendix D) 1.399 +var augumentbch = function(poly, p, genpoly, q) { 1.400 + var modulus = poly << q; 1.401 + for (var i = p - 1; i >= 0; --i) { 1.402 + if ((modulus >> (q+i)) & 1) modulus ^= genpoly << i; 1.403 + } 1.404 + return (poly << q) | modulus; 1.405 +}; 1.406 + 1.407 +// creates the base matrix for given version. it returns two matrices, one of 1.408 +// them is the actual one and the another represents the "reserved" portion 1.409 +// (e.g. finder and timing patterns) of the matrix. 1.410 +// 1.411 +// some entries in the matrix may be undefined, rather than 0 or 1. this is 1.412 +// intentional (no initialization needed!), and putdata below will fill 1.413 +// the remaining ones. 1.414 +var makebasematrix = function(ver) { 1.415 + var v = VERSIONS[ver], n = getsizebyver(ver); 1.416 + var matrix = [], reserved = []; 1.417 + for (var i = 0; i < n; ++i) { 1.418 + matrix.push([]); 1.419 + reserved.push([]); 1.420 + } 1.421 + 1.422 + var blit = function(y, x, h, w, bits) { 1.423 + for (var i = 0; i < h; ++i) { 1.424 + for (var j = 0; j < w; ++j) { 1.425 + matrix[y+i][x+j] = (bits[i] >> j) & 1; 1.426 + reserved[y+i][x+j] = 1; 1.427 + } 1.428 + } 1.429 + }; 1.430 + 1.431 + // finder patterns and a part of timing patterns 1.432 + // will also mark the format information area (not yet written) as reserved. 1.433 + blit(0, 0, 9, 9, [0x7f, 0x41, 0x5d, 0x5d, 0x5d, 0x41, 0x17f, 0x00, 0x40]); 1.434 + blit(n-8, 0, 8, 9, [0x100, 0x7f, 0x41, 0x5d, 0x5d, 0x5d, 0x41, 0x7f]); 1.435 + blit(0, n-8, 9, 8, [0xfe, 0x82, 0xba, 0xba, 0xba, 0x82, 0xfe, 0x00, 0x00]); 1.436 + 1.437 + // the rest of timing patterns 1.438 + for (var i = 9; i < n-8; ++i) { 1.439 + matrix[6][i] = matrix[i][6] = ~i & 1; 1.440 + reserved[6][i] = reserved[i][6] = 1; 1.441 + } 1.442 + 1.443 + // alignment patterns 1.444 + var aligns = v[2], m = aligns.length; 1.445 + for (var i = 0; i < m; ++i) { 1.446 + var minj = (i==0 || i==m-1 ? 1 : 0), maxj = (i==0 ? m-1 : m); 1.447 + for (var j = minj; j < maxj; ++j) { 1.448 + blit(aligns[i], aligns[j], 5, 5, [0x1f, 0x11, 0x15, 0x11, 0x1f]); 1.449 + } 1.450 + } 1.451 + 1.452 + // version information 1.453 + if (needsverinfo(ver)) { 1.454 + var code = augumentbch(ver, 6, 0x1f25, 12); 1.455 + var k = 0; 1.456 + for (var i = 0; i < 6; ++i) { 1.457 + for (var j = 0; j < 3; ++j) { 1.458 + matrix[i][(n-11)+j] = matrix[(n-11)+j][i] = (code >> k++) & 1; 1.459 + reserved[i][(n-11)+j] = reserved[(n-11)+j][i] = 1; 1.460 + } 1.461 + } 1.462 + } 1.463 + 1.464 + return {matrix: matrix, reserved: reserved}; 1.465 +}; 1.466 + 1.467 +// fills the data portion (i.e. unmarked in reserved) of the matrix with given 1.468 +// code words. the size of code words should be no more than available bits, 1.469 +// and remaining bits are padded to 0 (cf. JIS X 0510:2004 sec 8.7.3). 1.470 +var putdata = function(matrix, reserved, buf) { 1.471 + var n = matrix.length; 1.472 + var k = 0, dir = -1; 1.473 + for (var i = n-1; i >= 0; i -= 2) { 1.474 + if (i == 6) --i; // skip the entire timing pattern column 1.475 + var jj = (dir < 0 ? n-1 : 0); 1.476 + for (var j = 0; j < n; ++j) { 1.477 + for (var ii = i; ii > i-2; --ii) { 1.478 + if (!reserved[jj][ii]) { 1.479 + // may overflow, but (undefined >> x) 1.480 + // is 0 so it will auto-pad to zero. 1.481 + matrix[jj][ii] = (buf[k >> 3] >> (~k&7)) & 1; 1.482 + ++k; 1.483 + } 1.484 + } 1.485 + jj += dir; 1.486 + } 1.487 + dir = -dir; 1.488 + } 1.489 + return matrix; 1.490 +}; 1.491 + 1.492 +// XOR-masks the data portion of the matrix. repeating the call with the same 1.493 +// arguments will revert the prior call (convenient in the matrix evaluation). 1.494 +var maskdata = function(matrix, reserved, mask) { 1.495 + var maskf = MASKFUNCS[mask]; 1.496 + var n = matrix.length; 1.497 + for (var i = 0; i < n; ++i) { 1.498 + for (var j = 0; j < n; ++j) { 1.499 + if (!reserved[i][j]) matrix[i][j] ^= maskf(i,j); 1.500 + } 1.501 + } 1.502 + return matrix; 1.503 +} 1.504 + 1.505 +// puts the format information. 1.506 +var putformatinfo = function(matrix, reserved, ecclevel, mask) { 1.507 + var n = matrix.length; 1.508 + var code = augumentbch((ecclevel << 3) | mask, 5, 0x537, 10) ^ 0x5412; 1.509 + for (var i = 0; i < 15; ++i) { 1.510 + var r = [0,1,2,3,4,5,7,8,n-7,n-6,n-5,n-4,n-3,n-2,n-1][i]; 1.511 + var c = [n-1,n-2,n-3,n-4,n-5,n-6,n-7,n-8,7,5,4,3,2,1,0][i]; 1.512 + matrix[r][8] = matrix[8][c] = (code >> i) & 1; 1.513 + // we don't have to mark those bits reserved; always done 1.514 + // in makebasematrix above. 1.515 + } 1.516 + return matrix; 1.517 +}; 1.518 + 1.519 +// evaluates the resulting matrix and returns the score (lower is better). 1.520 +// (cf. JIS X 0510:2004 sec 8.8.2) 1.521 +// 1.522 +// the evaluation procedure tries to avoid the problematic patterns naturally 1.523 +// occuring from the original matrix. for example, it penaltizes the patterns 1.524 +// which just look like the finder pattern which will confuse the decoder. 1.525 +// we choose the mask which results in the lowest score among 8 possible ones. 1.526 +// 1.527 +// note: zxing seems to use the same procedure and in many cases its choice 1.528 +// agrees to ours, but sometimes it does not. practically it doesn't matter. 1.529 +var evaluatematrix = function(matrix) { 1.530 + // N1+(k-5) points for each consecutive row of k same-colored modules, where k >= 5. no overlapping row counts. 1.531 + var PENALTY_CONSECUTIVE = 3; 1.532 + // N2 points for each 2x2 block of same-colored modules. Overlapping block does count. 1.533 + var PENALTY_TWOBYTWO = 3; 1.534 + // N3 points for each pattern with >4W:1B:1W:3B:1W:1B or 1.535 + // 1B:1W:3B:1W:1B:>4W, or their multiples (e.g. highly unlikely, but 13W:3B:3W:9B:3W:3B counts). 1.536 + var PENALTY_FINDERLIKE = 40; 1.537 + // N4*k points for every (5*k)% deviation from 50% black density. 1.538 + // i.e. k=1 for 55~60% and 40~45%, k=2 for 60~65% and 35~40%, etc. 1.539 + var PENALTY_DENSITY = 10; 1.540 + 1.541 + var evaluategroup = function(groups) { // assumes [W,B,W,B,W,...,B,W] 1.542 + var score = 0; 1.543 + for (var i = 0; i < groups.length; ++i) { 1.544 + if (groups[i] >= 5) score += PENALTY_CONSECUTIVE + (groups[i]-5); 1.545 + } 1.546 + for (var i = 5; i < groups.length; i += 2) { 1.547 + var p = groups[i]; 1.548 + if (groups[i-1] == p && groups[i-2] == 3*p && groups[i-3] == p && 1.549 + groups[i-4] == p && (groups[i-5] >= 4*p || groups[i+1] >= 4*p)) { 1.550 + // this part differs from zxing... 1.551 + score += PENALTY_FINDERLIKE; 1.552 + } 1.553 + } 1.554 + return score; 1.555 + }; 1.556 + 1.557 + var n = matrix.length; 1.558 + var score = 0, nblacks = 0; 1.559 + for (var i = 0; i < n; ++i) { 1.560 + var row = matrix[i]; 1.561 + var groups; 1.562 + 1.563 + // evaluate the current row 1.564 + groups = [0]; // the first empty group of white 1.565 + for (var j = 0; j < n; ) { 1.566 + var k; 1.567 + for (k = 0; j < n && row[j]; ++k) ++j; 1.568 + groups.push(k); 1.569 + for (k = 0; j < n && !row[j]; ++k) ++j; 1.570 + groups.push(k); 1.571 + } 1.572 + score += evaluategroup(groups); 1.573 + 1.574 + // evaluate the current column 1.575 + groups = [0]; 1.576 + for (var j = 0; j < n; ) { 1.577 + var k; 1.578 + for (k = 0; j < n && matrix[j][i]; ++k) ++j; 1.579 + groups.push(k); 1.580 + for (k = 0; j < n && !matrix[j][i]; ++k) ++j; 1.581 + groups.push(k); 1.582 + } 1.583 + score += evaluategroup(groups); 1.584 + 1.585 + // check the 2x2 box and calculate the density 1.586 + var nextrow = matrix[i+1] || []; 1.587 + nblacks += row[0]; 1.588 + for (var j = 1; j < n; ++j) { 1.589 + var p = row[j]; 1.590 + nblacks += p; 1.591 + // at least comparison with next row should be strict... 1.592 + if (row[j-1] == p && nextrow[j] === p && nextrow[j-1] === p) { 1.593 + score += PENALTY_TWOBYTWO; 1.594 + } 1.595 + } 1.596 + } 1.597 + 1.598 + score += PENALTY_DENSITY * ((Math.abs(nblacks / n / n - 0.5) / 0.05) | 0); 1.599 + return score; 1.600 +}; 1.601 + 1.602 +// returns the fully encoded QR code matrix which contains given data. 1.603 +// it also chooses the best mask automatically when mask is -1. 1.604 +var generate = function(data, ver, mode, ecclevel, mask) { 1.605 + var v = VERSIONS[ver]; 1.606 + var buf = encode(ver, mode, data, ndatabits(ver, ecclevel) >> 3); 1.607 + buf = augumenteccs(buf, v[1][ecclevel], GF256_GENPOLY[v[0][ecclevel]]); 1.608 + 1.609 + var result = makebasematrix(ver); 1.610 + var matrix = result.matrix, reserved = result.reserved; 1.611 + putdata(matrix, reserved, buf); 1.612 + 1.613 + if (mask < 0) { 1.614 + // find the best mask 1.615 + maskdata(matrix, reserved, 0); 1.616 + putformatinfo(matrix, reserved, ecclevel, 0); 1.617 + var bestmask = 0, bestscore = evaluatematrix(matrix); 1.618 + maskdata(matrix, reserved, 0); 1.619 + for (mask = 1; mask < 8; ++mask) { 1.620 + maskdata(matrix, reserved, mask); 1.621 + putformatinfo(matrix, reserved, ecclevel, mask); 1.622 + var score = evaluatematrix(matrix); 1.623 + if (bestscore > score) { 1.624 + bestscore = score; 1.625 + bestmask = mask; 1.626 + } 1.627 + maskdata(matrix, reserved, mask); 1.628 + } 1.629 + mask = bestmask; 1.630 + } 1.631 + 1.632 + maskdata(matrix, reserved, mask); 1.633 + putformatinfo(matrix, reserved, ecclevel, mask); 1.634 + return matrix; 1.635 +}; 1.636 + 1.637 +// the public interface is trivial; the options available are as follows: 1.638 +// 1.639 +// - version: an integer in [1,40]. when omitted (or -1) the smallest possible 1.640 +// version is chosen. 1.641 +// - mode: one of 'numeric', 'alphanumeric', 'octet'. when omitted the smallest 1.642 +// possible mode is chosen. 1.643 +// - ecclevel: one of 'L', 'M', 'Q', 'H'. defaults to 'L'. 1.644 +// - mask: an integer in [0,7]. when omitted (or -1) the best mask is chosen. 1.645 +// 1.646 +// for generate{HTML,PNG}: 1.647 +// 1.648 +// - modulesize: a number. this is a size of each modules in pixels, and 1.649 +// defaults to 5px. 1.650 +// - margin: a number. this is a size of margin in *modules*, and defaults to 1.651 +// 4 (white modules). the specficiation mandates the margin no less than 4 1.652 +// modules, so it is better not to alter this value unless you know what 1.653 +// you're doing. 1.654 +var QRCode = { 1.655 + 'generate': function(data, options) { 1.656 + var MODES = {'numeric': MODE_NUMERIC, 'alphanumeric': MODE_ALPHANUMERIC, 1.657 + 'octet': MODE_OCTET}; 1.658 + var ECCLEVELS = {'L': ECCLEVEL_L, 'M': ECCLEVEL_M, 'Q': ECCLEVEL_Q, 1.659 + 'H': ECCLEVEL_H}; 1.660 + 1.661 + options = options || {}; 1.662 + var ver = options.version || -1; 1.663 + var ecclevel = ECCLEVELS[(options.ecclevel || 'L').toUpperCase()]; 1.664 + var mode = options.mode ? MODES[options.mode.toLowerCase()] : -1; 1.665 + var mask = 'mask' in options ? options.mask : -1; 1.666 + 1.667 + if (mode < 0) { 1.668 + if (typeof data === 'string') { 1.669 + if (data.match(NUMERIC_REGEXP)) { 1.670 + mode = MODE_NUMERIC; 1.671 + } else if (data.match(ALPHANUMERIC_OUT_REGEXP)) { 1.672 + // while encode supports case-insensitive 1.673 + // encoding, we restrict the data to be 1.674 + // uppercased when auto-selecting the mode. 1.675 + mode = MODE_ALPHANUMERIC; 1.676 + } else { 1.677 + mode = MODE_OCTET; 1.678 + } 1.679 + } else { 1.680 + mode = MODE_OCTET; 1.681 + } 1.682 + } else if (!(mode == MODE_NUMERIC || mode == MODE_ALPHANUMERIC || 1.683 + mode == MODE_OCTET)) { 1.684 + throw 'invalid or unsupported mode'; 1.685 + } 1.686 + 1.687 + data = validatedata(mode, data); 1.688 + if (data === null) throw 'invalid data format'; 1.689 + 1.690 + if (ecclevel < 0 || ecclevel > 3) throw 'invalid ECC level'; 1.691 + 1.692 + if (ver < 0) { 1.693 + for (ver = 1; ver <= 40; ++ver) { 1.694 + if (data.length <= getmaxdatalen(ver, mode, ecclevel)) break; 1.695 + } 1.696 + if (ver > 40) throw 'too large data'; 1.697 + } else if (ver < 1 || ver > 40) { 1.698 + throw 'invalid version'; 1.699 + } 1.700 + 1.701 + if (mask != -1 && (mask < 0 || mask > 8)) throw 'invalid mask'; 1.702 + 1.703 + return generate(data, ver, mode, ecclevel, mask); 1.704 + }, 1.705 + 1.706 + 1.707 + 'generatePNG': function(data, options) { 1.708 + options = options || {}; 1.709 + var matrix = QRCode['generate'](data, options); 1.710 + var modsize = Math.max(options.modulesize || 5, 0.5); 1.711 + var margin = Math.max(options.margin || 4, 0.0); 1.712 + var n = matrix.length; 1.713 + var size = modsize * (n + 2 * margin); 1.714 + 1.715 + var canvas = document.createElement('canvas'), context; 1.716 + canvas.width = canvas.height = size; 1.717 + context = canvas.getContext('2d'); 1.718 + if (!context) throw 'canvas support is needed for PNG output'; 1.719 + 1.720 + context.fillStyle = '#fff'; 1.721 + context.fillRect(0, 0, size, size); 1.722 + context.fillStyle = '#000'; 1.723 + for (var i = 0; i < n; ++i) { 1.724 + for (var j = 0; j < n; ++j) { 1.725 + if (matrix[i][j]) { 1.726 + context.fillRect(modsize * (margin + j), modsize * (margin + i), modsize, modsize); 1.727 + } 1.728 + } 1.729 + } 1.730 + //context.fillText('evaluation: ' + evaluatematrix(matrix), 10, 10); 1.731 + return canvas.toDataURL(); 1.732 + } 1.733 +}; 1.734 + 1.735 +return QRCode; 1.736 +})();