website rev 1161

Add qrcode pseudo link
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Mar 25 17:11:35 2013 +0100 (2013-03-25)
parents da74b9b2fcaf
children 64894689b692
files lib/html/footer.html lib/js/qrcode.js
line diff
     1.1 --- a/lib/html/footer.html	Mon Mar 11 19:37:20 2013 +0000
     1.2 +++ b/lib/html/footer.html	Mon Mar 25 17:11:35 2013 +0100
     1.3 @@ -1,4 +1,7 @@
     1.4  <!-- Footer -->
     1.5 +
     1.6 +<script type="text/javascript" src="lib/js/qrcode.js"></script>
     1.7 +
     1.8  <div id="footer">
     1.9  	Copyright &copy; <span class="year"></span>
    1.10  	<a href="http://www.slitaz.org/">SliTaz</a> - Network:
    1.11 @@ -9,7 +12,8 @@
    1.12  	<a href="http://bugs.slitaz.org">Bugs</a>
    1.13  	<a href="http://hg.slitaz.org/">Hg</a>
    1.14  	<p>
    1.15 -		SliTaz @
    1.16 +		<img src="#" alt="SliTaz @" onmouseover="this.title = location.href"
    1.17 +		 onclick="this.src = QRCode.generatePNG(location.href, {ecclevel: 'H'})" />
    1.18  		<a href="http://twitter.com/slitaz">Twitter</a>
    1.19  		<a href="http://www.facebook.com/slitaz">Facebook</a>
    1.20  		<a href="http://distrowatch.com/table.php?distribution=slitaz">Distrowatch</a>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/lib/js/qrcode.js	Mon Mar 25 17:11:35 2013 +0100
     2.3 @@ -0,0 +1,733 @@
     2.4 +/* qr.js -- QR code generator in Javascript (revision 2011-01-19)
     2.5 + * Written by Kang Seonghoon <public+qrjs@mearie.org>.
     2.6 + *
     2.7 + * This source code is in the public domain; if your jurisdiction does not
     2.8 + * recognize the public domain the terms of Creative Commons CC0 license
     2.9 + * apply. In the other words, you can always do what you want.
    2.10 + */
    2.11 +
    2.12 +var QRCode = (function(){
    2.13 +
    2.14 +/* Quick overview: QR code composed of 2D array of modules (a rectangular
    2.15 + * area that conveys one bit of information); some modules are fixed to help
    2.16 + * the recognition of the code, and remaining data modules are further divided
    2.17 + * into 8-bit code words which are augumented by Reed-Solomon error correcting
    2.18 + * codes (ECC). There could be multiple ECCs, in the case the code is so large
    2.19 + * that it is helpful to split the raw data into several chunks.
    2.20 + *
    2.21 + * The number of modules is determined by the code's "version", ranging from 1
    2.22 + * (21x21) to 40 (177x177). How many ECC bits are used is determined by the
    2.23 + * ECC level (L/M/Q/H). The number and size (and thus the order of generator
    2.24 + * polynomial) of ECCs depend to the version and ECC level.
    2.25 + */
    2.26 +
    2.27 +// per-version information (cf. JIS X 0510:2004 pp. 30--36, 71)
    2.28 +//
    2.29 +// [0]: the degree of generator polynomial by ECC levels
    2.30 +// [1]: # of code blocks by ECC levels
    2.31 +// [2]: left-top positions of alignment patterns
    2.32 +//
    2.33 +// the number in this table (in particular, [0]) does not exactly match with
    2.34 +// the numbers in the specficiation. see augumenteccs below for the reason.
    2.35 +var VERSIONS = [
    2.36 +	null,
    2.37 +	[[10, 7,17,13], [ 1, 1, 1, 1], []],
    2.38 +	[[16,10,28,22], [ 1, 1, 1, 1], [4,16]],
    2.39 +	[[26,15,22,18], [ 1, 1, 2, 2], [4,20]],
    2.40 +	[[18,20,16,26], [ 2, 1, 4, 2], [4,24]],
    2.41 +	[[24,26,22,18], [ 2, 1, 4, 4], [4,28]],
    2.42 +	[[16,18,28,24], [ 4, 2, 4, 4], [4,32]],
    2.43 +	[[18,20,26,18], [ 4, 2, 5, 6], [4,20,36]],
    2.44 +	[[22,24,26,22], [ 4, 2, 6, 6], [4,22,40]],
    2.45 +	[[22,30,24,20], [ 5, 2, 8, 8], [4,24,44]],
    2.46 +	[[26,18,28,24], [ 5, 4, 8, 8], [4,26,48]],
    2.47 +	[[30,20,24,28], [ 5, 4,11, 8], [4,28,52]],
    2.48 +	[[22,24,28,26], [ 8, 4,11,10], [4,30,56]],
    2.49 +	[[22,26,22,24], [ 9, 4,16,12], [4,32,60]],
    2.50 +	[[24,30,24,20], [ 9, 4,16,16], [4,24,44,64]],
    2.51 +	[[24,22,24,30], [10, 6,18,12], [4,24,46,68]],
    2.52 +	[[28,24,30,24], [10, 6,16,17], [4,24,48,72]],
    2.53 +	[[28,28,28,28], [11, 6,19,16], [4,28,52,76]],
    2.54 +	[[26,30,28,28], [13, 6,21,18], [4,28,54,80]],
    2.55 +	[[26,28,26,26], [14, 7,25,21], [4,28,56,84]],
    2.56 +	[[26,28,28,30], [16, 8,25,20], [4,32,60,88]],
    2.57 +	[[26,28,30,28], [17, 8,25,23], [4,26,48,70,92]],
    2.58 +	[[28,28,24,30], [17, 9,34,23], [4,24,48,72,96]],
    2.59 +	[[28,30,30,30], [18, 9,30,25], [4,28,52,76,100]],
    2.60 +	[[28,30,30,30], [20,10,32,27], [4,26,52,78,104]],
    2.61 +	[[28,26,30,30], [21,12,35,29], [4,30,56,82,108]],
    2.62 +	[[28,28,30,28], [23,12,37,34], [4,28,56,84,112]],
    2.63 +	[[28,30,30,30], [25,12,40,34], [4,32,60,88,116]],
    2.64 +	[[28,30,30,30], [26,13,42,35], [4,24,48,72,96,120]],
    2.65 +	[[28,30,30,30], [28,14,45,38], [4,28,52,76,100,124]],
    2.66 +	[[28,30,30,30], [29,15,48,40], [4,24,50,76,102,128]],
    2.67 +	[[28,30,30,30], [31,16,51,43], [4,28,54,80,106,132]],
    2.68 +	[[28,30,30,30], [33,17,54,45], [4,32,58,84,110,136]],
    2.69 +	[[28,30,30,30], [35,18,57,48], [4,28,56,84,112,140]],
    2.70 +	[[28,30,30,30], [37,19,60,51], [4,32,60,88,116,144]],
    2.71 +	[[28,30,30,30], [38,19,63,53], [4,28,52,76,100,124,148]],
    2.72 +	[[28,30,30,30], [40,20,66,56], [4,22,48,74,100,126,152]],
    2.73 +	[[28,30,30,30], [43,21,70,59], [4,26,52,78,104,130,156]],
    2.74 +	[[28,30,30,30], [45,22,74,62], [4,30,56,82,108,134,160]],
    2.75 +	[[28,30,30,30], [47,24,77,65], [4,24,52,80,108,136,164]],
    2.76 +	[[28,30,30,30], [49,25,81,68], [4,28,56,84,112,140,168]]];
    2.77 +
    2.78 +// mode constants (cf. Table 2 in JIS X 0510:2004 p. 16)
    2.79 +var MODE_TERMINATOR = 0;
    2.80 +var MODE_NUMERIC = 1, MODE_ALPHANUMERIC = 2, MODE_OCTET = 4, MODE_KANJI = 8;
    2.81 +
    2.82 +// validation regexps
    2.83 +var NUMERIC_REGEXP = /^\d*$/;
    2.84 +var ALPHANUMERIC_REGEXP = /^[A-Za-z0-9 $%*+\-./:]*$/;
    2.85 +var ALPHANUMERIC_OUT_REGEXP = /^[A-Z0-9 $%*+\-./:]*$/;
    2.86 +
    2.87 +// ECC levels (cf. Table 22 in JIS X 0510:2004 p. 45)
    2.88 +var ECCLEVEL_L = 1, ECCLEVEL_M = 0, ECCLEVEL_Q = 3, ECCLEVEL_H = 2;
    2.89 +
    2.90 +// GF(2^8)-to-integer mapping with a reducing polynomial x^8+x^4+x^3+x^2+1
    2.91 +// invariant: GF256_MAP[GF256_INVMAP[i]] == i for all i in [1,256)
    2.92 +var GF256_MAP = [], GF256_INVMAP = [-1];
    2.93 +for (var i = 0, v = 1; i < 255; ++i) {
    2.94 +	GF256_MAP.push(v);
    2.95 +	GF256_INVMAP[v] = i;
    2.96 +	v = (v * 2) ^ (v >= 128 ? 0x11d : 0);
    2.97 +}
    2.98 +
    2.99 +// generator polynomials up to degree 30
   2.100 +// (should match with polynomials in JIS X 0510:2004 Appendix A)
   2.101 +//
   2.102 +// generator polynomial of degree K is product of (x-\alpha^0), (x-\alpha^1),
   2.103 +// ..., (x-\alpha^(K-1)). by convention, we omit the K-th coefficient (always 1)
   2.104 +// from the result; also other coefficients are written in terms of the exponent
   2.105 +// to \alpha to avoid the redundant calculation. (see also calculateecc below.)
   2.106 +var GF256_GENPOLY = [[]];
   2.107 +for (var i = 0; i < 30; ++i) {
   2.108 +	var prevpoly = GF256_GENPOLY[i], poly = [];
   2.109 +	for (var j = 0; j <= i; ++j) {
   2.110 +		var a = (j < i ? GF256_MAP[prevpoly[j]] : 0);
   2.111 +		var b = GF256_MAP[(i + (prevpoly[j-1] || 0)) % 255];
   2.112 +		poly.push(GF256_INVMAP[a ^ b]);
   2.113 +	}
   2.114 +	GF256_GENPOLY.push(poly);
   2.115 +}
   2.116 +
   2.117 +// alphanumeric character mapping (cf. Table 5 in JIS X 0510:2004 p. 19)
   2.118 +var ALPHANUMERIC_MAP = {};
   2.119 +for (var i = 0; i < 45; ++i) {
   2.120 +	ALPHANUMERIC_MAP['0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'.charAt(i)] = i;
   2.121 +}
   2.122 +
   2.123 +// mask functions in terms of row # and column #
   2.124 +// (cf. Table 20 in JIS X 0510:2004 p. 42)
   2.125 +var MASKFUNCS = [
   2.126 +	function(i,j) { return (i+j) % 2 == 0; },
   2.127 +	function(i,j) { return i % 2 == 0; },
   2.128 +	function(i,j) { return j % 3 == 0; },
   2.129 +	function(i,j) { return (i+j) % 3 == 0; },
   2.130 +	function(i,j) { return (((i/2)|0) + ((j/3)|0)) % 2 == 0; },
   2.131 +	function(i,j) { return (i*j) % 2 + (i*j) % 3 == 0; },
   2.132 +	function(i,j) { return ((i*j) % 2 + (i*j) % 3) % 2 == 0; },
   2.133 +	function(i,j) { return ((i+j) % 2 + (i*j) % 3) % 2 == 0; }];
   2.134 +
   2.135 +// returns true when the version information has to be embeded.
   2.136 +var needsverinfo = function(ver) { return ver > 6; };
   2.137 +
   2.138 +// returns the size of entire QR code for given version.
   2.139 +var getsizebyver = function(ver) { return 4 * ver + 17; };
   2.140 +
   2.141 +// returns the number of bits available for code words in this version.
   2.142 +var nfullbits = function(ver) {
   2.143 +	/*
   2.144 +	 * |<--------------- n --------------->|
   2.145 +	 * |        |<----- n-17 ---->|        |
   2.146 +	 * +-------+                ///+-------+ ----
   2.147 +	 * |       |                ///|       |    ^
   2.148 +	 * |  9x9  |       @@@@@    ///|  9x8  |    |
   2.149 +	 * |       | # # # @5x5@ # # # |       |    |
   2.150 +	 * +-------+       @@@@@       +-------+    |
   2.151 +	 *       #                               ---|
   2.152 +	 *                                        ^ |
   2.153 +	 *       #                                |
   2.154 +	 *     @@@@@       @@@@@       @@@@@      | n
   2.155 +	 *     @5x5@       @5x5@       @5x5@   n-17
   2.156 +	 *     @@@@@       @@@@@       @@@@@      | |
   2.157 +	 *       #                                | |
   2.158 +	 * //////                                 v |
   2.159 +	 * //////#                               ---|
   2.160 +	 * +-------+       @@@@@       @@@@@        |
   2.161 +	 * |       |       @5x5@       @5x5@        |
   2.162 +	 * |  8x9  |       @@@@@       @@@@@        |
   2.163 +	 * |       |                                v
   2.164 +	 * +-------+                             ----
   2.165 +	 *
   2.166 +	 * when the entire code has n^2 modules and there are m^2-3 alignment
   2.167 +	 * patterns, we have:
   2.168 +	 * - 225 (= 9x9 + 9x8 + 8x9) modules for finder patterns and
   2.169 +	 *   format information;
   2.170 +	 * - 2n-34 (= 2(n-17)) modules for timing patterns;
   2.171 +	 * - 36 (= 3x6 + 6x3) modules for version information, if any;
   2.172 +	 * - 25m^2-75 (= (m^2-3)(5x5)) modules for alignment patterns
   2.173 +	 *   if any, but 10m-20 (= 2(m-2)x5) of them overlaps with
   2.174 +	 *   timing patterns.
   2.175 +	 */
   2.176 +	var v = VERSIONS[ver];
   2.177 +	var nbits = 16*ver*ver + 128*ver + 64; // finder, timing and format info.
   2.178 +	if (needsverinfo(ver)) nbits -= 36; // version information
   2.179 +	if (v[2].length) { // alignment patterns
   2.180 +		nbits -= 25 * v[2].length * v[2].length - 10 * v[2].length - 55;
   2.181 +	}
   2.182 +	return nbits;
   2.183 +};
   2.184 +
   2.185 +// returns the number of bits available for data portions (i.e. excludes ECC
   2.186 +// bits but includes mode and length bits) in this version and ECC level.
   2.187 +var ndatabits = function(ver, ecclevel) {
   2.188 +	var nbits = nfullbits(ver) & ~7; // no sub-octet code words
   2.189 +	var v = VERSIONS[ver];
   2.190 +	nbits -= 8 * v[0][ecclevel] * v[1][ecclevel]; // ecc bits
   2.191 +	return nbits;
   2.192 +}
   2.193 +
   2.194 +// returns the number of bits required for the length of data.
   2.195 +// (cf. Table 3 in JIS X 0510:2004 p. 16)
   2.196 +var ndatalenbits = function(ver, mode) {
   2.197 +	switch (mode) {
   2.198 +	case MODE_NUMERIC: return (ver < 10 ? 10 : ver < 27 ? 12 : 14);
   2.199 +	case MODE_ALPHANUMERIC: return (ver < 10 ? 9 : ver < 27 ? 11 : 13);
   2.200 +	case MODE_OCTET: return (ver < 10 ? 8 : 16);
   2.201 +	case MODE_KANJI: return (ver < 10 ? 8 : ver < 27 ? 10 : 12);
   2.202 +	}
   2.203 +};
   2.204 +
   2.205 +// returns the maximum length of data possible in given configuration.
   2.206 +var getmaxdatalen = function(ver, mode, ecclevel) {
   2.207 +	var nbits = ndatabits(ver, ecclevel) - 4 - ndatalenbits(ver, mode); // 4 for mode bits
   2.208 +	switch (mode) {
   2.209 +	case MODE_NUMERIC:
   2.210 +		return ((nbits/10) | 0) * 3 + (nbits%10 < 4 ? 0 : nbits%10 < 7 ? 1 : 2);
   2.211 +	case MODE_ALPHANUMERIC:
   2.212 +		return ((nbits/11) | 0) * 2 + (nbits%11 < 6 ? 0 : 1);
   2.213 +	case MODE_OCTET:
   2.214 +		return (nbits/8) | 0;
   2.215 +	case MODE_KANJI:
   2.216 +		return (nbits/13) | 0;
   2.217 +	}
   2.218 +};
   2.219 +
   2.220 +// checks if the given data can be encoded in given mode, and returns
   2.221 +// the converted data for the further processing if possible. otherwise
   2.222 +// returns null.
   2.223 +//
   2.224 +// this function does not check the length of data; it is a duty of
   2.225 +// encode function below (as it depends on the version and ECC level too).
   2.226 +var validatedata = function(mode, data) {
   2.227 +	switch (mode) {
   2.228 +	case MODE_NUMERIC:
   2.229 +		if (!data.match(NUMERIC_REGEXP)) return null;
   2.230 +		return data;
   2.231 +
   2.232 +	case MODE_ALPHANUMERIC:
   2.233 +		if (!data.match(ALPHANUMERIC_REGEXP)) return null;
   2.234 +		return data.toUpperCase();
   2.235 +
   2.236 +	case MODE_OCTET:
   2.237 +		if (typeof data === 'string') { // encode as utf-8 string
   2.238 +			var newdata = [];
   2.239 +			for (var i = 0; i < data.length; ++i) {
   2.240 +				var ch = data.charCodeAt(i);
   2.241 +				if (ch < 0x80) {
   2.242 +					newdata.push(ch);
   2.243 +				} else if (ch < 0x800) {
   2.244 +					newdata.push(0xc0 | (ch >> 6),
   2.245 +						0x80 | (ch & 0x3f));
   2.246 +				} else if (ch < 0x10000) {
   2.247 +					newdata.push(0xe0 | (ch >> 12),
   2.248 +						0x80 | ((ch >> 6) & 0x3f),
   2.249 +						0x80 | (ch & 0x3f));
   2.250 +				} else {
   2.251 +					newdata.push(0xf0 | (ch >> 18),
   2.252 +						0x80 | ((ch >> 12) & 0x3f),
   2.253 +						0x80 | ((ch >> 6) & 0x3f),
   2.254 +						0x80 | (ch & 0x3f));
   2.255 +				}
   2.256 +			}
   2.257 +			return newdata;
   2.258 +		} else {
   2.259 +			return data;
   2.260 +		}
   2.261 +	}
   2.262 +};
   2.263 +
   2.264 +// returns the code words (sans ECC bits) for given data and configurations.
   2.265 +// requires data to be preprocessed by validatedata. no length check is
   2.266 +// performed, and everything has to be checked before calling this function.
   2.267 +var encode = function(ver, mode, data, maxbuflen) {
   2.268 +	var buf = [];
   2.269 +	var bits = 0, remaining = 8;
   2.270 +	var datalen = data.length;
   2.271 +
   2.272 +	// this function is intentionally no-op when n=0.
   2.273 +	var pack = function(x, n) {
   2.274 +		if (n >= remaining) {
   2.275 +			buf.push(bits | (x >> (n -= remaining)));
   2.276 +			while (n >= 8) buf.push((x >> (n -= 8)) & 255);
   2.277 +			bits = 0;
   2.278 +			remaining = 8;
   2.279 +		}
   2.280 +		if (n > 0) bits |= (x & ((1 << n) - 1)) << (remaining -= n);
   2.281 +	};
   2.282 +
   2.283 +	var nlenbits = ndatalenbits(ver, mode);
   2.284 +	pack(mode, 4);
   2.285 +	pack(datalen, nlenbits);
   2.286 +
   2.287 +	switch (mode) {
   2.288 +	case MODE_NUMERIC:
   2.289 +		for (var i = 2; i < datalen; i += 3) {
   2.290 +			pack(parseInt(data.substring(i-2,i+1), 10), 10);
   2.291 +		}
   2.292 +		pack(parseInt(data.substring(i-2), 10), [0,4,7][datalen%3]);
   2.293 +		break;
   2.294 +
   2.295 +	case MODE_ALPHANUMERIC:
   2.296 +		for (var i = 1; i < datalen; i += 2) {
   2.297 +			pack(ALPHANUMERIC_MAP[data.charAt(i-1)] * 45 +
   2.298 +				ALPHANUMERIC_MAP[data.charAt(i)], 11);
   2.299 +		}
   2.300 +		if (datalen % 2 == 1) {
   2.301 +			pack(ALPHANUMERIC_MAP[data.charAt(i-1)], 6);
   2.302 +		}
   2.303 +		break;
   2.304 +
   2.305 +	case MODE_OCTET:
   2.306 +		for (var i = 0; i < datalen; ++i) {
   2.307 +			pack(data[i], 8);
   2.308 +		}
   2.309 +		break;
   2.310 +	};
   2.311 +
   2.312 +	// final bits. it is possible that adding terminator causes the buffer
   2.313 +	// to overflow, but then the buffer truncated to the maximum size will
   2.314 +	// be valid as the truncated terminator mode bits and padding is
   2.315 +	// identical in appearance (cf. JIS X 0510:2004 sec 8.4.8).
   2.316 +	pack(MODE_TERMINATOR, 4);
   2.317 +	if (remaining < 8) buf.push(bits);
   2.318 +
   2.319 +	// the padding to fill up the remaining space. we should not add any
   2.320 +	// words when the overflow already occurred.
   2.321 +	while (buf.length + 1 < maxbuflen) buf.push(0xec, 0x11);
   2.322 +	if (buf.length < maxbuflen) buf.push(0xec);
   2.323 +	return buf;
   2.324 +};
   2.325 +
   2.326 +// calculates ECC code words for given code words and generator polynomial.
   2.327 +//
   2.328 +// this is quite similar to CRC calculation as both Reed-Solomon and CRC use
   2.329 +// the certain kind of cyclic codes, which is effectively the division of
   2.330 +// zero-augumented polynomial by the generator polynomial. the only difference
   2.331 +// is that Reed-Solomon uses GF(2^8), instead of CRC's GF(2), and Reed-Solomon
   2.332 +// uses the different generator polynomial than CRC's.
   2.333 +var calculateecc = function(poly, genpoly) {
   2.334 +	var modulus = poly.slice(0);
   2.335 +	var polylen = poly.length, genpolylen = genpoly.length;
   2.336 +	for (var i = 0; i < genpolylen; ++i) modulus.push(0);
   2.337 +	for (var i = 0; i < polylen; ) {
   2.338 +		var quotient = GF256_INVMAP[modulus[i++]];
   2.339 +		if (quotient >= 0) {
   2.340 +			for (var j = 0; j < genpolylen; ++j) {
   2.341 +				modulus[i+j] ^= GF256_MAP[(quotient + genpoly[j]) % 255];
   2.342 +			}
   2.343 +		}
   2.344 +	}
   2.345 +	return modulus.slice(polylen);
   2.346 +};
   2.347 +
   2.348 +// auguments ECC code words to given code words. the resulting words are
   2.349 +// ready to be encoded in the matrix.
   2.350 +//
   2.351 +// the much of actual augumenting procedure follows JIS X 0510:2004 sec 8.7.
   2.352 +// the code is simplified using the fact that the size of each code & ECC
   2.353 +// blocks is almost same; for example, when we have 4 blocks and 46 data words
   2.354 +// the number of code words in those blocks are 11, 11, 12, 12 respectively.
   2.355 +var augumenteccs = function(poly, nblocks, genpoly) {
   2.356 +	var subsizes = [];
   2.357 +	var subsize = (poly.length / nblocks) | 0, subsize0 = 0;
   2.358 +	var pivot = nblocks - poly.length % nblocks;
   2.359 +	for (var i = 0; i < pivot; ++i) {
   2.360 +		subsizes.push(subsize0);
   2.361 +		subsize0 += subsize;
   2.362 +	}
   2.363 +	for (var i = pivot; i < nblocks; ++i) {
   2.364 +		subsizes.push(subsize0);
   2.365 +		subsize0 += subsize+1;
   2.366 +	}
   2.367 +	subsizes.push(subsize0);
   2.368 +
   2.369 +	var eccs = [];
   2.370 +	for (var i = 0; i < nblocks; ++i) {
   2.371 +		eccs.push(calculateecc(poly.slice(subsizes[i], subsizes[i+1]), genpoly));
   2.372 +	}
   2.373 +
   2.374 +	var result = [];
   2.375 +	var nitemsperblock = (poly.length / nblocks) | 0;
   2.376 +	for (var i = 0; i < nitemsperblock; ++i) {
   2.377 +		for (var j = 0; j < nblocks; ++j) {
   2.378 +			result.push(poly[subsizes[j] + i]);
   2.379 +		}
   2.380 +	}
   2.381 +	for (var j = pivot; j < nblocks; ++j) {
   2.382 +		result.push(poly[subsizes[j+1] - 1]);
   2.383 +	}
   2.384 +	for (var i = 0; i < genpoly.length; ++i) {
   2.385 +		for (var j = 0; j < nblocks; ++j) {
   2.386 +			result.push(eccs[j][i]);
   2.387 +		}
   2.388 +	}
   2.389 +	return result;
   2.390 +};
   2.391 +
   2.392 +// auguments BCH(p+q,q) code to the polynomial over GF(2), given the proper
   2.393 +// genpoly. the both input and output are in binary numbers, and unlike
   2.394 +// calculateecc genpoly should include the 1 bit for the highest degree.
   2.395 +//
   2.396 +// actual polynomials used for this procedure are as follows:
   2.397 +// - p=10, q=5, genpoly=x^10+x^8+x^5+x^4+x^2+x+1 (JIS X 0510:2004 Appendix C)
   2.398 +// - p=18, q=6, genpoly=x^12+x^11+x^10+x^9+x^8+x^5+x^2+1 (ibid. Appendix D)
   2.399 +var augumentbch = function(poly, p, genpoly, q) {
   2.400 +	var modulus = poly << q;
   2.401 +	for (var i = p - 1; i >= 0; --i) {
   2.402 +		if ((modulus >> (q+i)) & 1) modulus ^= genpoly << i;
   2.403 +	}
   2.404 +	return (poly << q) | modulus;
   2.405 +};
   2.406 +
   2.407 +// creates the base matrix for given version. it returns two matrices, one of
   2.408 +// them is the actual one and the another represents the "reserved" portion
   2.409 +// (e.g. finder and timing patterns) of the matrix.
   2.410 +//
   2.411 +// some entries in the matrix may be undefined, rather than 0 or 1. this is
   2.412 +// intentional (no initialization needed!), and putdata below will fill
   2.413 +// the remaining ones.
   2.414 +var makebasematrix = function(ver) {
   2.415 +	var v = VERSIONS[ver], n = getsizebyver(ver);
   2.416 +	var matrix = [], reserved = [];
   2.417 +	for (var i = 0; i < n; ++i) {
   2.418 +		matrix.push([]);
   2.419 +		reserved.push([]);
   2.420 +	}
   2.421 +
   2.422 +	var blit = function(y, x, h, w, bits) {
   2.423 +		for (var i = 0; i < h; ++i) {
   2.424 +			for (var j = 0; j < w; ++j) {
   2.425 +				matrix[y+i][x+j] = (bits[i] >> j) & 1;
   2.426 +				reserved[y+i][x+j] = 1;
   2.427 +			}
   2.428 +		}
   2.429 +	};
   2.430 +
   2.431 +	// finder patterns and a part of timing patterns
   2.432 +	// will also mark the format information area (not yet written) as reserved.
   2.433 +	blit(0, 0, 9, 9, [0x7f, 0x41, 0x5d, 0x5d, 0x5d, 0x41, 0x17f, 0x00, 0x40]);
   2.434 +	blit(n-8, 0, 8, 9, [0x100, 0x7f, 0x41, 0x5d, 0x5d, 0x5d, 0x41, 0x7f]);
   2.435 +	blit(0, n-8, 9, 8, [0xfe, 0x82, 0xba, 0xba, 0xba, 0x82, 0xfe, 0x00, 0x00]);
   2.436 +
   2.437 +	// the rest of timing patterns
   2.438 +	for (var i = 9; i < n-8; ++i) {
   2.439 +		matrix[6][i] = matrix[i][6] = ~i & 1;
   2.440 +		reserved[6][i] = reserved[i][6] = 1;
   2.441 +	}
   2.442 +
   2.443 +	// alignment patterns
   2.444 +	var aligns = v[2], m = aligns.length;
   2.445 +	for (var i = 0; i < m; ++i) {
   2.446 +		var minj = (i==0 || i==m-1 ? 1 : 0), maxj = (i==0 ? m-1 : m);
   2.447 +		for (var j = minj; j < maxj; ++j) {
   2.448 +			blit(aligns[i], aligns[j], 5, 5, [0x1f, 0x11, 0x15, 0x11, 0x1f]);
   2.449 +		}
   2.450 +	}
   2.451 +
   2.452 +	// version information
   2.453 +	if (needsverinfo(ver)) {
   2.454 +		var code = augumentbch(ver, 6, 0x1f25, 12);
   2.455 +		var k = 0;
   2.456 +		for (var i = 0; i < 6; ++i) {
   2.457 +			for (var j = 0; j < 3; ++j) {
   2.458 +				matrix[i][(n-11)+j] = matrix[(n-11)+j][i] = (code >> k++) & 1;
   2.459 +				reserved[i][(n-11)+j] = reserved[(n-11)+j][i] = 1;
   2.460 +			}
   2.461 +		}
   2.462 +	}
   2.463 +
   2.464 +	return {matrix: matrix, reserved: reserved};
   2.465 +};
   2.466 +
   2.467 +// fills the data portion (i.e. unmarked in reserved) of the matrix with given
   2.468 +// code words. the size of code words should be no more than available bits,
   2.469 +// and remaining bits are padded to 0 (cf. JIS X 0510:2004 sec 8.7.3).
   2.470 +var putdata = function(matrix, reserved, buf) {
   2.471 +	var n = matrix.length;
   2.472 +	var k = 0, dir = -1;
   2.473 +	for (var i = n-1; i >= 0; i -= 2) {
   2.474 +		if (i == 6) --i; // skip the entire timing pattern column
   2.475 +		var jj = (dir < 0 ? n-1 : 0);
   2.476 +		for (var j = 0; j < n; ++j) {
   2.477 +			for (var ii = i; ii > i-2; --ii) {
   2.478 +				if (!reserved[jj][ii]) {
   2.479 +					// may overflow, but (undefined >> x)
   2.480 +					// is 0 so it will auto-pad to zero.
   2.481 +					matrix[jj][ii] = (buf[k >> 3] >> (~k&7)) & 1;
   2.482 +					++k;
   2.483 +				}
   2.484 +			}
   2.485 +			jj += dir;
   2.486 +		}
   2.487 +		dir = -dir;
   2.488 +	}
   2.489 +	return matrix;
   2.490 +};
   2.491 +
   2.492 +// XOR-masks the data portion of the matrix. repeating the call with the same
   2.493 +// arguments will revert the prior call (convenient in the matrix evaluation).
   2.494 +var maskdata = function(matrix, reserved, mask) {
   2.495 +	var maskf = MASKFUNCS[mask];
   2.496 +	var n = matrix.length;
   2.497 +	for (var i = 0; i < n; ++i) {
   2.498 +		for (var j = 0; j < n; ++j) {
   2.499 +			if (!reserved[i][j]) matrix[i][j] ^= maskf(i,j);
   2.500 +		}
   2.501 +	}
   2.502 +	return matrix;
   2.503 +}
   2.504 +
   2.505 +// puts the format information.
   2.506 +var putformatinfo = function(matrix, reserved, ecclevel, mask) {
   2.507 +	var n = matrix.length;
   2.508 +	var code = augumentbch((ecclevel << 3) | mask, 5, 0x537, 10) ^ 0x5412;
   2.509 +	for (var i = 0; i < 15; ++i) {
   2.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];
   2.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];
   2.512 +		matrix[r][8] = matrix[8][c] = (code >> i) & 1;
   2.513 +		// we don't have to mark those bits reserved; always done
   2.514 +		// in makebasematrix above.
   2.515 +	}
   2.516 +	return matrix;
   2.517 +};
   2.518 +
   2.519 +// evaluates the resulting matrix and returns the score (lower is better).
   2.520 +// (cf. JIS X 0510:2004 sec 8.8.2)
   2.521 +//
   2.522 +// the evaluation procedure tries to avoid the problematic patterns naturally
   2.523 +// occuring from the original matrix. for example, it penaltizes the patterns
   2.524 +// which just look like the finder pattern which will confuse the decoder.
   2.525 +// we choose the mask which results in the lowest score among 8 possible ones.
   2.526 +//
   2.527 +// note: zxing seems to use the same procedure and in many cases its choice
   2.528 +// agrees to ours, but sometimes it does not. practically it doesn't matter.
   2.529 +var evaluatematrix = function(matrix) {
   2.530 +	// N1+(k-5) points for each consecutive row of k same-colored modules, where k >= 5. no overlapping row counts.
   2.531 +	var PENALTY_CONSECUTIVE = 3;
   2.532 +	// N2 points for each 2x2 block of same-colored modules. Overlapping block does count.
   2.533 +	var PENALTY_TWOBYTWO = 3;
   2.534 +	// N3 points for each pattern with >4W:1B:1W:3B:1W:1B or
   2.535 +	// 1B:1W:3B:1W:1B:>4W, or their multiples (e.g. highly unlikely, but 13W:3B:3W:9B:3W:3B counts).
   2.536 +	var PENALTY_FINDERLIKE = 40;
   2.537 +	// N4*k points for every (5*k)% deviation from 50% black density.
   2.538 +	// i.e. k=1 for 55~60% and 40~45%, k=2 for 60~65% and 35~40%, etc.
   2.539 +	var PENALTY_DENSITY = 10;
   2.540 +
   2.541 +	var evaluategroup = function(groups) { // assumes [W,B,W,B,W,...,B,W]
   2.542 +		var score = 0;
   2.543 +		for (var i = 0; i < groups.length; ++i) {
   2.544 +			if (groups[i] >= 5) score += PENALTY_CONSECUTIVE + (groups[i]-5);
   2.545 +		}
   2.546 +		for (var i = 5; i < groups.length; i += 2) {
   2.547 +			var p = groups[i];
   2.548 +			if (groups[i-1] == p && groups[i-2] == 3*p && groups[i-3] == p &&
   2.549 +					groups[i-4] == p && (groups[i-5] >= 4*p || groups[i+1] >= 4*p)) {
   2.550 +				// this part differs from zxing...
   2.551 +				score += PENALTY_FINDERLIKE;
   2.552 +			}
   2.553 +		}
   2.554 +		return score;
   2.555 +	};
   2.556 +
   2.557 +	var n = matrix.length;
   2.558 +	var score = 0, nblacks = 0;
   2.559 +	for (var i = 0; i < n; ++i) {
   2.560 +		var row = matrix[i];
   2.561 +		var groups;
   2.562 +
   2.563 +		// evaluate the current row
   2.564 +		groups = [0]; // the first empty group of white
   2.565 +		for (var j = 0; j < n; ) {
   2.566 +			var k;
   2.567 +			for (k = 0; j < n && row[j]; ++k) ++j;
   2.568 +			groups.push(k);
   2.569 +			for (k = 0; j < n && !row[j]; ++k) ++j;
   2.570 +			groups.push(k);
   2.571 +		}
   2.572 +		score += evaluategroup(groups);
   2.573 +
   2.574 +		// evaluate the current column
   2.575 +		groups = [0];
   2.576 +		for (var j = 0; j < n; ) {
   2.577 +			var k;
   2.578 +			for (k = 0; j < n && matrix[j][i]; ++k) ++j;
   2.579 +			groups.push(k);
   2.580 +			for (k = 0; j < n && !matrix[j][i]; ++k) ++j;
   2.581 +			groups.push(k);
   2.582 +		}
   2.583 +		score += evaluategroup(groups);
   2.584 +
   2.585 +		// check the 2x2 box and calculate the density
   2.586 +		var nextrow = matrix[i+1] || [];
   2.587 +		nblacks += row[0];
   2.588 +		for (var j = 1; j < n; ++j) {
   2.589 +			var p = row[j];
   2.590 +			nblacks += p;
   2.591 +			// at least comparison with next row should be strict...
   2.592 +			if (row[j-1] == p && nextrow[j] === p && nextrow[j-1] === p) {
   2.593 +				score += PENALTY_TWOBYTWO;
   2.594 +			}
   2.595 +		}
   2.596 +	}
   2.597 +
   2.598 +	score += PENALTY_DENSITY * ((Math.abs(nblacks / n / n - 0.5) / 0.05) | 0);
   2.599 +	return score;
   2.600 +};
   2.601 +
   2.602 +// returns the fully encoded QR code matrix which contains given data.
   2.603 +// it also chooses the best mask automatically when mask is -1.
   2.604 +var generate = function(data, ver, mode, ecclevel, mask) {
   2.605 +	var v = VERSIONS[ver];
   2.606 +	var buf = encode(ver, mode, data, ndatabits(ver, ecclevel) >> 3);
   2.607 +	buf = augumenteccs(buf, v[1][ecclevel], GF256_GENPOLY[v[0][ecclevel]]);
   2.608 +
   2.609 +	var result = makebasematrix(ver);
   2.610 +	var matrix = result.matrix, reserved = result.reserved;
   2.611 +	putdata(matrix, reserved, buf);
   2.612 +
   2.613 +	if (mask < 0) {
   2.614 +		// find the best mask
   2.615 +		maskdata(matrix, reserved, 0);
   2.616 +		putformatinfo(matrix, reserved, ecclevel, 0);
   2.617 +		var bestmask = 0, bestscore = evaluatematrix(matrix);
   2.618 +		maskdata(matrix, reserved, 0);
   2.619 +		for (mask = 1; mask < 8; ++mask) {
   2.620 +			maskdata(matrix, reserved, mask);
   2.621 +			putformatinfo(matrix, reserved, ecclevel, mask);
   2.622 +			var score = evaluatematrix(matrix);
   2.623 +			if (bestscore > score) {
   2.624 +				bestscore = score;
   2.625 +				bestmask = mask;
   2.626 +			}
   2.627 +			maskdata(matrix, reserved, mask);
   2.628 +		}
   2.629 +		mask = bestmask;
   2.630 +	}
   2.631 +
   2.632 +	maskdata(matrix, reserved, mask);
   2.633 +	putformatinfo(matrix, reserved, ecclevel, mask);
   2.634 +	return matrix;
   2.635 +};
   2.636 +
   2.637 +// the public interface is trivial; the options available are as follows:
   2.638 +//
   2.639 +// - version: an integer in [1,40]. when omitted (or -1) the smallest possible
   2.640 +//   version is chosen.
   2.641 +// - mode: one of 'numeric', 'alphanumeric', 'octet'. when omitted the smallest
   2.642 +//   possible mode is chosen.
   2.643 +// - ecclevel: one of 'L', 'M', 'Q', 'H'. defaults to 'L'.
   2.644 +// - mask: an integer in [0,7]. when omitted (or -1) the best mask is chosen.
   2.645 +//
   2.646 +// for generate{HTML,PNG}:
   2.647 +//
   2.648 +// - modulesize: a number. this is a size of each modules in pixels, and
   2.649 +//   defaults to 5px.
   2.650 +// - margin: a number. this is a size of margin in *modules*, and defaults to
   2.651 +//   4 (white modules). the specficiation mandates the margin no less than 4
   2.652 +//   modules, so it is better not to alter this value unless you know what
   2.653 +//   you're doing.
   2.654 +var QRCode = {
   2.655 +	'generate': function(data, options) {
   2.656 +		var MODES = {'numeric': MODE_NUMERIC, 'alphanumeric': MODE_ALPHANUMERIC,
   2.657 +			'octet': MODE_OCTET};
   2.658 +		var ECCLEVELS = {'L': ECCLEVEL_L, 'M': ECCLEVEL_M, 'Q': ECCLEVEL_Q,
   2.659 +			'H': ECCLEVEL_H};
   2.660 +
   2.661 +		options = options || {};
   2.662 +		var ver = options.version || -1;
   2.663 +		var ecclevel = ECCLEVELS[(options.ecclevel || 'L').toUpperCase()];
   2.664 +		var mode = options.mode ? MODES[options.mode.toLowerCase()] : -1;
   2.665 +		var mask = 'mask' in options ? options.mask : -1;
   2.666 +
   2.667 +		if (mode < 0) {
   2.668 +			if (typeof data === 'string') {
   2.669 +				if (data.match(NUMERIC_REGEXP)) {
   2.670 +					mode = MODE_NUMERIC;
   2.671 +				} else if (data.match(ALPHANUMERIC_OUT_REGEXP)) {
   2.672 +					// while encode supports case-insensitive
   2.673 +					// encoding, we restrict the data to be
   2.674 +					// uppercased when auto-selecting the mode.
   2.675 +					mode = MODE_ALPHANUMERIC;
   2.676 +				} else {
   2.677 +					mode = MODE_OCTET;
   2.678 +				}
   2.679 +			} else {
   2.680 +				mode = MODE_OCTET;
   2.681 +			}
   2.682 +		} else if (!(mode == MODE_NUMERIC || mode == MODE_ALPHANUMERIC ||
   2.683 +				mode == MODE_OCTET)) {
   2.684 +			throw 'invalid or unsupported mode';
   2.685 +		}
   2.686 +
   2.687 +		data = validatedata(mode, data);
   2.688 +		if (data === null) throw 'invalid data format';
   2.689 +
   2.690 +		if (ecclevel < 0 || ecclevel > 3) throw 'invalid ECC level';
   2.691 +
   2.692 +		if (ver < 0) {
   2.693 +			for (ver = 1; ver <= 40; ++ver) {
   2.694 +				if (data.length <= getmaxdatalen(ver, mode, ecclevel)) break;
   2.695 +			}
   2.696 +			if (ver > 40) throw 'too large data';
   2.697 +		} else if (ver < 1 || ver > 40) {
   2.698 +			throw 'invalid version';
   2.699 +		}
   2.700 +
   2.701 +		if (mask != -1 && (mask < 0 || mask > 8)) throw 'invalid mask';
   2.702 +
   2.703 +		return generate(data, ver, mode, ecclevel, mask);
   2.704 +	},
   2.705 +
   2.706 +
   2.707 +	'generatePNG': function(data, options) {
   2.708 +		options = options || {};
   2.709 +		var matrix = QRCode['generate'](data, options);
   2.710 +		var modsize = Math.max(options.modulesize || 5, 0.5);
   2.711 +		var margin = Math.max(options.margin || 4, 0.0);
   2.712 +		var n = matrix.length;
   2.713 +		var size = modsize * (n + 2 * margin);
   2.714 +
   2.715 +		var canvas = document.createElement('canvas'), context;
   2.716 +		canvas.width = canvas.height = size;
   2.717 +		context = canvas.getContext('2d');
   2.718 +		if (!context) throw 'canvas support is needed for PNG output';
   2.719 +
   2.720 +		context.fillStyle = '#fff';
   2.721 +		context.fillRect(0, 0, size, size);
   2.722 +		context.fillStyle = '#000';
   2.723 +		for (var i = 0; i < n; ++i) {
   2.724 +			for (var j = 0; j < n; ++j) {
   2.725 +				if (matrix[i][j]) {
   2.726 +					context.fillRect(modsize * (margin + j), modsize * (margin + i), modsize, modsize);
   2.727 +				}
   2.728 +			}
   2.729 +		}
   2.730 +		//context.fillText('evaluation: ' + evaluatematrix(matrix), 10, 10);
   2.731 +		return canvas.toDataURL();
   2.732 +	}
   2.733 +};
   2.734 +
   2.735 +return QRCode;
   2.736 +})();