pdfmake 0.3.0-beta.6 → 0.3.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE +1 -1
  3. package/build/pdfmake.js +36523 -36424
  4. package/build/pdfmake.js.map +1 -1
  5. package/build/pdfmake.min.js +2 -2
  6. package/build/pdfmake.min.js.map +1 -1
  7. package/package.json +21 -20
  8. package/src/DocMeasure.js +3 -0
  9. package/src/Printer.js +1 -0
  10. package/src/TableProcessor.js +4 -0
  11. package/src/TextDecorator.js +1 -1
  12. package/src/qrEnc.js +5 -3
  13. package/js/3rd-party/svg-to-pdfkit/source.js +0 -3628
  14. package/js/3rd-party/svg-to-pdfkit.js +0 -7
  15. package/js/DocMeasure.js +0 -624
  16. package/js/DocPreprocessor.js +0 -238
  17. package/js/DocumentContext.js +0 -265
  18. package/js/ElementWriter.js +0 -331
  19. package/js/LayoutBuilder.js +0 -694
  20. package/js/Line.js +0 -105
  21. package/js/OutputDocument.js +0 -76
  22. package/js/OutputDocumentServer.js +0 -27
  23. package/js/PDFDocument.js +0 -144
  24. package/js/PageElementWriter.js +0 -140
  25. package/js/PageSize.js +0 -74
  26. package/js/Printer.js +0 -290
  27. package/js/Renderer.js +0 -375
  28. package/js/SVGMeasure.js +0 -69
  29. package/js/StyleContextStack.js +0 -181
  30. package/js/TableProcessor.js +0 -508
  31. package/js/TextBreaker.js +0 -139
  32. package/js/TextDecorator.js +0 -143
  33. package/js/TextInlines.js +0 -206
  34. package/js/URLResolver.js +0 -73
  35. package/js/base.js +0 -52
  36. package/js/browser-extensions/OutputDocumentBrowser.js +0 -118
  37. package/js/browser-extensions/URLBrowserResolver.js +0 -76
  38. package/js/browser-extensions/fonts/Roboto.js +0 -38
  39. package/js/browser-extensions/index.js +0 -53
  40. package/js/browser-extensions/pdfMake.js +0 -15
  41. package/js/browser-extensions/standard-fonts/Courier.js +0 -38
  42. package/js/browser-extensions/standard-fonts/Helvetica.js +0 -38
  43. package/js/browser-extensions/standard-fonts/Symbol.js +0 -23
  44. package/js/browser-extensions/standard-fonts/Times.js +0 -38
  45. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +0 -23
  46. package/js/browser-extensions/virtual-fs-cjs.js +0 -3
  47. package/js/columnCalculator.js +0 -129
  48. package/js/helpers/node.js +0 -98
  49. package/js/helpers/tools.js +0 -40
  50. package/js/helpers/variableType.js +0 -47
  51. package/js/index.js +0 -15
  52. package/js/qrEnc.js +0 -720
  53. package/js/standardPageSizes.js +0 -56
  54. package/js/tableLayouts.js +0 -98
  55. package/js/virtual-fs.js +0 -60
package/js/qrEnc.js DELETED
@@ -1,720 +0,0 @@
1
- /*eslint no-unused-vars: ["error", {"args": "none"}]*/
2
- /*eslint no-redeclare: "off"*/
3
- /*eslint no-throw-literal: "off"*/
4
-
5
- 'use strict';
6
-
7
- /* qr.js -- QR code generator in Javascript (revision 2011-01-19)
8
- * Written by Kang Seonghoon <public+qrjs@mearie.org>.
9
- *
10
- * This source code is in the public domain; if your jurisdiction does not
11
- * recognize the public domain the terms of Creative Commons CC0 license
12
- * apply. In the other words, you can always do what you want.
13
- */
14
-
15
- // per-version information (cf. JIS X 0510:2004 pp. 30--36, 71)
16
- //
17
- // [0]: the degree of generator polynomial by ECC levels
18
- // [1]: # of code blocks by ECC levels
19
- // [2]: left-top positions of alignment patterns
20
- //
21
- // the number in this table (in particular, [0]) does not exactly match with
22
- // the numbers in the specficiation. see augumenteccs below for the reason.
23
- exports.__esModule = true;
24
- exports.default = void 0;
25
- var VERSIONS = [null, [[10, 7, 17, 13], [1, 1, 1, 1], []], [[16, 10, 28, 22], [1, 1, 1, 1], [4, 16]], [[26, 15, 22, 18], [1, 1, 2, 2], [4, 20]], [[18, 20, 16, 26], [2, 1, 4, 2], [4, 24]], [[24, 26, 22, 18], [2, 1, 4, 4], [4, 28]], [[16, 18, 28, 24], [4, 2, 4, 4], [4, 32]], [[18, 20, 26, 18], [4, 2, 5, 6], [4, 20, 36]], [[22, 24, 26, 22], [4, 2, 6, 6], [4, 22, 40]], [[22, 30, 24, 20], [5, 2, 8, 8], [4, 24, 44]], [[26, 18, 28, 24], [5, 4, 8, 8], [4, 26, 48]], [[30, 20, 24, 28], [5, 4, 11, 8], [4, 28, 52]], [[22, 24, 28, 26], [8, 4, 11, 10], [4, 30, 56]], [[22, 26, 22, 24], [9, 4, 16, 12], [4, 32, 60]], [[24, 30, 24, 20], [9, 4, 16, 16], [4, 24, 44, 64]], [[24, 22, 24, 30], [10, 6, 18, 12], [4, 24, 46, 68]], [[28, 24, 30, 24], [10, 6, 16, 17], [4, 24, 48, 72]], [[28, 28, 28, 28], [11, 6, 19, 16], [4, 28, 52, 76]], [[26, 30, 28, 28], [13, 6, 21, 18], [4, 28, 54, 80]], [[26, 28, 26, 26], [14, 7, 25, 21], [4, 28, 56, 84]], [[26, 28, 28, 30], [16, 8, 25, 20], [4, 32, 60, 88]], [[26, 28, 30, 28], [17, 8, 25, 23], [4, 26, 48, 70, 92]], [[28, 28, 24, 30], [17, 9, 34, 23], [4, 24, 48, 72, 96]], [[28, 30, 30, 30], [18, 9, 30, 25], [4, 28, 52, 76, 100]], [[28, 30, 30, 30], [20, 10, 32, 27], [4, 26, 52, 78, 104]], [[28, 26, 30, 30], [21, 12, 35, 29], [4, 30, 56, 82, 108]], [[28, 28, 30, 28], [23, 12, 37, 34], [4, 28, 56, 84, 112]], [[28, 30, 30, 30], [25, 12, 40, 34], [4, 32, 60, 88, 116]], [[28, 30, 30, 30], [26, 13, 42, 35], [4, 24, 48, 72, 96, 120]], [[28, 30, 30, 30], [28, 14, 45, 38], [4, 28, 52, 76, 100, 124]], [[28, 30, 30, 30], [29, 15, 48, 40], [4, 24, 50, 76, 102, 128]], [[28, 30, 30, 30], [31, 16, 51, 43], [4, 28, 54, 80, 106, 132]], [[28, 30, 30, 30], [33, 17, 54, 45], [4, 32, 58, 84, 110, 136]], [[28, 30, 30, 30], [35, 18, 57, 48], [4, 28, 56, 84, 112, 140]], [[28, 30, 30, 30], [37, 19, 60, 51], [4, 32, 60, 88, 116, 144]], [[28, 30, 30, 30], [38, 19, 63, 53], [4, 28, 52, 76, 100, 124, 148]], [[28, 30, 30, 30], [40, 20, 66, 56], [4, 22, 48, 74, 100, 126, 152]], [[28, 30, 30, 30], [43, 21, 70, 59], [4, 26, 52, 78, 104, 130, 156]], [[28, 30, 30, 30], [45, 22, 74, 62], [4, 30, 56, 82, 108, 134, 160]], [[28, 30, 30, 30], [47, 24, 77, 65], [4, 24, 52, 80, 108, 136, 164]], [[28, 30, 30, 30], [49, 25, 81, 68], [4, 28, 56, 84, 112, 140, 168]]];
26
-
27
- // mode constants (cf. Table 2 in JIS X 0510:2004 p. 16)
28
- var MODE_TERMINATOR = 0;
29
- var MODE_NUMERIC = 1,
30
- MODE_ALPHANUMERIC = 2,
31
- MODE_OCTET = 4,
32
- MODE_KANJI = 8;
33
-
34
- // validation regexps
35
- var NUMERIC_REGEXP = /^\d*$/;
36
- var ALPHANUMERIC_REGEXP = /^[A-Za-z0-9 $%*+\-./:]*$/;
37
- var ALPHANUMERIC_OUT_REGEXP = /^[A-Z0-9 $%*+\-./:]*$/;
38
-
39
- // ECC levels (cf. Table 22 in JIS X 0510:2004 p. 45)
40
- var ECCLEVEL_L = 1,
41
- ECCLEVEL_M = 0,
42
- ECCLEVEL_Q = 3,
43
- ECCLEVEL_H = 2;
44
-
45
- // GF(2^8)-to-integer mapping with a reducing polynomial x^8+x^4+x^3+x^2+1
46
- // invariant: GF256_MAP[GF256_INVMAP[i]] == i for all i in [1,256)
47
- var GF256_MAP = [],
48
- GF256_INVMAP = [-1];
49
- for (var i = 0, v = 1; i < 255; ++i) {
50
- GF256_MAP.push(v);
51
- GF256_INVMAP[v] = i;
52
- v = v * 2 ^ (v >= 128 ? 0x11d : 0);
53
- }
54
-
55
- // generator polynomials up to degree 30
56
- // (should match with polynomials in JIS X 0510:2004 Appendix A)
57
- //
58
- // generator polynomial of degree K is product of (x-\alpha^0), (x-\alpha^1),
59
- // ..., (x-\alpha^(K-1)). by convention, we omit the K-th coefficient (always 1)
60
- // from the result; also other coefficients are written in terms of the exponent
61
- // to \alpha to avoid the redundant calculation. (see also calculateecc below.)
62
- var GF256_GENPOLY = [[]];
63
- for (var i = 0; i < 30; ++i) {
64
- var prevpoly = GF256_GENPOLY[i],
65
- poly = [];
66
- for (var j = 0; j <= i; ++j) {
67
- var a = j < i ? GF256_MAP[prevpoly[j]] : 0;
68
- var b = GF256_MAP[(i + (prevpoly[j - 1] || 0)) % 255];
69
- poly.push(GF256_INVMAP[a ^ b]);
70
- }
71
- GF256_GENPOLY.push(poly);
72
- }
73
-
74
- // alphanumeric character mapping (cf. Table 5 in JIS X 0510:2004 p. 19)
75
- var ALPHANUMERIC_MAP = {};
76
- for (var i = 0; i < 45; ++i) {
77
- ALPHANUMERIC_MAP['0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'.charAt(i)] = i;
78
- }
79
-
80
- // mask functions in terms of row # and column #
81
- // (cf. Table 20 in JIS X 0510:2004 p. 42)
82
- /*jshint unused: false */
83
- var MASKFUNCS = [function (i, j) {
84
- return (i + j) % 2 === 0;
85
- }, function (i, j) {
86
- return i % 2 === 0;
87
- }, function (i, j) {
88
- return j % 3 === 0;
89
- }, function (i, j) {
90
- return (i + j) % 3 === 0;
91
- }, function (i, j) {
92
- return ((i / 2 | 0) + (j / 3 | 0)) % 2 === 0;
93
- }, function (i, j) {
94
- return i * j % 2 + i * j % 3 === 0;
95
- }, function (i, j) {
96
- return (i * j % 2 + i * j % 3) % 2 === 0;
97
- }, function (i, j) {
98
- return ((i + j) % 2 + i * j % 3) % 2 === 0;
99
- }];
100
-
101
- // returns true when the version information has to be embeded.
102
- var needsverinfo = function (ver) {
103
- return ver > 6;
104
- };
105
-
106
- // returns the size of entire QR code for given version.
107
- var getsizebyver = function (ver) {
108
- return 4 * ver + 17;
109
- };
110
-
111
- // returns the number of bits available for code words in this version.
112
- var nfullbits = function (ver) {
113
- /*
114
- * |<--------------- n --------------->|
115
- * | |<----- n-17 ---->| |
116
- * +-------+ ///+-------+ ----
117
- * | | ///| | ^
118
- * | 9x9 | @@@@@ ///| 9x8 | |
119
- * | | # # # @5x5@ # # # | | |
120
- * +-------+ @@@@@ +-------+ |
121
- * # ---|
122
- * ^ |
123
- * # |
124
- * @@@@@ @@@@@ @@@@@ | n
125
- * @5x5@ @5x5@ @5x5@ n-17
126
- * @@@@@ @@@@@ @@@@@ | |
127
- * # | |
128
- * ////// v |
129
- * //////# ---|
130
- * +-------+ @@@@@ @@@@@ |
131
- * | | @5x5@ @5x5@ |
132
- * | 8x9 | @@@@@ @@@@@ |
133
- * | | v
134
- * +-------+ ----
135
- *
136
- * when the entire code has n^2 modules and there are m^2-3 alignment
137
- * patterns, we have:
138
- * - 225 (= 9x9 + 9x8 + 8x9) modules for finder patterns and
139
- * format information;
140
- * - 2n-34 (= 2(n-17)) modules for timing patterns;
141
- * - 36 (= 3x6 + 6x3) modules for version information, if any;
142
- * - 25m^2-75 (= (m^2-3)(5x5)) modules for alignment patterns
143
- * if any, but 10m-20 (= 2(m-2)x5) of them overlaps with
144
- * timing patterns.
145
- */
146
- var v = VERSIONS[ver];
147
- var nbits = 16 * ver * ver + 128 * ver + 64; // finder, timing and format info.
148
- if (needsverinfo(ver)) nbits -= 36; // version information
149
- if (v[2].length) {
150
- // alignment patterns
151
- nbits -= 25 * v[2].length * v[2].length - 10 * v[2].length - 55;
152
- }
153
- return nbits;
154
- };
155
-
156
- // returns the number of bits available for data portions (i.e. excludes ECC
157
- // bits but includes mode and length bits) in this version and ECC level.
158
- var ndatabits = function (ver, ecclevel) {
159
- var nbits = nfullbits(ver) & ~7; // no sub-octet code words
160
- var v = VERSIONS[ver];
161
- nbits -= 8 * v[0][ecclevel] * v[1][ecclevel]; // ecc bits
162
- return nbits;
163
- };
164
-
165
- // returns the number of bits required for the length of data.
166
- // (cf. Table 3 in JIS X 0510:2004 p. 16)
167
- var ndatalenbits = function (ver, mode) {
168
- switch (mode) {
169
- case MODE_NUMERIC:
170
- return ver < 10 ? 10 : ver < 27 ? 12 : 14;
171
- case MODE_ALPHANUMERIC:
172
- return ver < 10 ? 9 : ver < 27 ? 11 : 13;
173
- case MODE_OCTET:
174
- return ver < 10 ? 8 : 16;
175
- case MODE_KANJI:
176
- return ver < 10 ? 8 : ver < 27 ? 10 : 12;
177
- }
178
- };
179
-
180
- // returns the maximum length of data possible in given configuration.
181
- var getmaxdatalen = function (ver, mode, ecclevel) {
182
- var nbits = ndatabits(ver, ecclevel) - 4 - ndatalenbits(ver, mode); // 4 for mode bits
183
- switch (mode) {
184
- case MODE_NUMERIC:
185
- return (nbits / 10 | 0) * 3 + (nbits % 10 < 4 ? 0 : nbits % 10 < 7 ? 1 : 2);
186
- case MODE_ALPHANUMERIC:
187
- return (nbits / 11 | 0) * 2 + (nbits % 11 < 6 ? 0 : 1);
188
- case MODE_OCTET:
189
- return nbits / 8 | 0;
190
- case MODE_KANJI:
191
- return nbits / 13 | 0;
192
- }
193
- };
194
-
195
- // checks if the given data can be encoded in given mode, and returns
196
- // the converted data for the further processing if possible. otherwise
197
- // returns null.
198
- //
199
- // this function does not check the length of data; it is a duty of
200
- // encode function below (as it depends on the version and ECC level too).
201
- var validatedata = function (mode, data) {
202
- switch (mode) {
203
- case MODE_NUMERIC:
204
- if (!data.match(NUMERIC_REGEXP)) return null;
205
- return data;
206
- case MODE_ALPHANUMERIC:
207
- if (!data.match(ALPHANUMERIC_REGEXP)) return null;
208
- return data.toUpperCase();
209
- case MODE_OCTET:
210
- if (typeof data === 'string') {
211
- // encode as utf-8 string
212
- var newdata = [];
213
- for (var i = 0; i < data.length; ++i) {
214
- var ch = data.charCodeAt(i);
215
- if (ch < 0x80) {
216
- newdata.push(ch);
217
- } else if (ch < 0x800) {
218
- newdata.push(0xc0 | ch >> 6, 0x80 | ch & 0x3f);
219
- } else if (ch < 0x10000) {
220
- newdata.push(0xe0 | ch >> 12, 0x80 | ch >> 6 & 0x3f, 0x80 | ch & 0x3f);
221
- } else {
222
- newdata.push(0xf0 | ch >> 18, 0x80 | ch >> 12 & 0x3f, 0x80 | ch >> 6 & 0x3f, 0x80 | ch & 0x3f);
223
- }
224
- }
225
- return newdata;
226
- } else {
227
- return data;
228
- }
229
- }
230
- };
231
-
232
- // returns the code words (sans ECC bits) for given data and configurations.
233
- // requires data to be preprocessed by validatedata. no length check is
234
- // performed, and everything has to be checked before calling this function.
235
- var encode = function (ver, mode, data, maxbuflen) {
236
- var buf = [];
237
- var bits = 0,
238
- remaining = 8;
239
- var datalen = data.length;
240
-
241
- // this function is intentionally no-op when n=0.
242
- var pack = function (x, n) {
243
- if (n >= remaining) {
244
- buf.push(bits | x >> (n -= remaining));
245
- while (n >= 8) buf.push(x >> (n -= 8) & 255);
246
- bits = 0;
247
- remaining = 8;
248
- }
249
- if (n > 0) bits |= (x & (1 << n) - 1) << (remaining -= n);
250
- };
251
- var nlenbits = ndatalenbits(ver, mode);
252
- pack(mode, 4);
253
- pack(datalen, nlenbits);
254
- switch (mode) {
255
- case MODE_NUMERIC:
256
- for (var i = 2; i < datalen; i += 3) {
257
- pack(parseInt(data.substring(i - 2, i + 1), 10), 10);
258
- }
259
- pack(parseInt(data.substring(i - 2), 10), [0, 4, 7][datalen % 3]);
260
- break;
261
- case MODE_ALPHANUMERIC:
262
- for (var i = 1; i < datalen; i += 2) {
263
- pack(ALPHANUMERIC_MAP[data.charAt(i - 1)] * 45 + ALPHANUMERIC_MAP[data.charAt(i)], 11);
264
- }
265
- if (datalen % 2 == 1) {
266
- pack(ALPHANUMERIC_MAP[data.charAt(i - 1)], 6);
267
- }
268
- break;
269
- case MODE_OCTET:
270
- for (var i = 0; i < datalen; ++i) {
271
- pack(data[i], 8);
272
- }
273
- break;
274
- }
275
-
276
- // final bits. it is possible that adding terminator causes the buffer
277
- // to overflow, but then the buffer truncated to the maximum size will
278
- // be valid as the truncated terminator mode bits and padding is
279
- // identical in appearance (cf. JIS X 0510:2004 sec 8.4.8).
280
- pack(MODE_TERMINATOR, 4);
281
- if (remaining < 8) buf.push(bits);
282
-
283
- // the padding to fill up the remaining space. we should not add any
284
- // words when the overflow already occurred.
285
- while (buf.length + 1 < maxbuflen) buf.push(0xec, 0x11);
286
- if (buf.length < maxbuflen) buf.push(0xec);
287
- return buf;
288
- };
289
-
290
- // calculates ECC code words for given code words and generator polynomial.
291
- //
292
- // this is quite similar to CRC calculation as both Reed-Solomon and CRC use
293
- // the certain kind of cyclic codes, which is effectively the division of
294
- // zero-augumented polynomial by the generator polynomial. the only difference
295
- // is that Reed-Solomon uses GF(2^8), instead of CRC's GF(2), and Reed-Solomon
296
- // uses the different generator polynomial than CRC's.
297
- var calculateecc = function (poly, genpoly) {
298
- var modulus = poly.slice(0);
299
- var polylen = poly.length,
300
- genpolylen = genpoly.length;
301
- for (var i = 0; i < genpolylen; ++i) modulus.push(0);
302
- for (var i = 0; i < polylen;) {
303
- var quotient = GF256_INVMAP[modulus[i++]];
304
- if (quotient >= 0) {
305
- for (var j = 0; j < genpolylen; ++j) {
306
- modulus[i + j] ^= GF256_MAP[(quotient + genpoly[j]) % 255];
307
- }
308
- }
309
- }
310
- return modulus.slice(polylen);
311
- };
312
-
313
- // auguments ECC code words to given code words. the resulting words are
314
- // ready to be encoded in the matrix.
315
- //
316
- // the much of actual augumenting procedure follows JIS X 0510:2004 sec 8.7.
317
- // the code is simplified using the fact that the size of each code & ECC
318
- // blocks is almost same; for example, when we have 4 blocks and 46 data words
319
- // the number of code words in those blocks are 11, 11, 12, 12 respectively.
320
- var augumenteccs = function (poly, nblocks, genpoly) {
321
- var subsizes = [];
322
- var subsize = poly.length / nblocks | 0,
323
- subsize0 = 0;
324
- var pivot = nblocks - poly.length % nblocks;
325
- for (var i = 0; i < pivot; ++i) {
326
- subsizes.push(subsize0);
327
- subsize0 += subsize;
328
- }
329
- for (var i = pivot; i < nblocks; ++i) {
330
- subsizes.push(subsize0);
331
- subsize0 += subsize + 1;
332
- }
333
- subsizes.push(subsize0);
334
- var eccs = [];
335
- for (var i = 0; i < nblocks; ++i) {
336
- eccs.push(calculateecc(poly.slice(subsizes[i], subsizes[i + 1]), genpoly));
337
- }
338
- var result = [];
339
- var nitemsperblock = poly.length / nblocks | 0;
340
- for (var i = 0; i < nitemsperblock; ++i) {
341
- for (var j = 0; j < nblocks; ++j) {
342
- result.push(poly[subsizes[j] + i]);
343
- }
344
- }
345
- for (var j = pivot; j < nblocks; ++j) {
346
- result.push(poly[subsizes[j + 1] - 1]);
347
- }
348
- for (var i = 0; i < genpoly.length; ++i) {
349
- for (var j = 0; j < nblocks; ++j) {
350
- result.push(eccs[j][i]);
351
- }
352
- }
353
- return result;
354
- };
355
-
356
- // auguments BCH(p+q,q) code to the polynomial over GF(2), given the proper
357
- // genpoly. the both input and output are in binary numbers, and unlike
358
- // calculateecc genpoly should include the 1 bit for the highest degree.
359
- //
360
- // actual polynomials used for this procedure are as follows:
361
- // - p=10, q=5, genpoly=x^10+x^8+x^5+x^4+x^2+x+1 (JIS X 0510:2004 Appendix C)
362
- // - p=18, q=6, genpoly=x^12+x^11+x^10+x^9+x^8+x^5+x^2+1 (ibid. Appendix D)
363
- var augumentbch = function (poly, p, genpoly, q) {
364
- var modulus = poly << q;
365
- for (var i = p - 1; i >= 0; --i) {
366
- if (modulus >> q + i & 1) modulus ^= genpoly << i;
367
- }
368
- return poly << q | modulus;
369
- };
370
-
371
- // creates the base matrix for given version. it returns two matrices, one of
372
- // them is the actual one and the another represents the "reserved" portion
373
- // (e.g. finder and timing patterns) of the matrix.
374
- //
375
- // some entries in the matrix may be undefined, rather than 0 or 1. this is
376
- // intentional (no initialization needed!), and putdata below will fill
377
- // the remaining ones.
378
- var makebasematrix = function (ver) {
379
- var v = VERSIONS[ver],
380
- n = getsizebyver(ver);
381
- var matrix = [],
382
- reserved = [];
383
- for (var i = 0; i < n; ++i) {
384
- matrix.push([]);
385
- reserved.push([]);
386
- }
387
- var blit = function (y, x, h, w, bits) {
388
- for (var i = 0; i < h; ++i) {
389
- for (var j = 0; j < w; ++j) {
390
- matrix[y + i][x + j] = bits[i] >> j & 1;
391
- reserved[y + i][x + j] = 1;
392
- }
393
- }
394
- };
395
-
396
- // finder patterns and a part of timing patterns
397
- // will also mark the format information area (not yet written) as reserved.
398
- blit(0, 0, 9, 9, [0x7f, 0x41, 0x5d, 0x5d, 0x5d, 0x41, 0x17f, 0x00, 0x40]);
399
- blit(n - 8, 0, 8, 9, [0x100, 0x7f, 0x41, 0x5d, 0x5d, 0x5d, 0x41, 0x7f]);
400
- blit(0, n - 8, 9, 8, [0xfe, 0x82, 0xba, 0xba, 0xba, 0x82, 0xfe, 0x00, 0x00]);
401
-
402
- // the rest of timing patterns
403
- for (var i = 9; i < n - 8; ++i) {
404
- matrix[6][i] = matrix[i][6] = ~i & 1;
405
- reserved[6][i] = reserved[i][6] = 1;
406
- }
407
-
408
- // alignment patterns
409
- var aligns = v[2],
410
- m = aligns.length;
411
- for (var i = 0; i < m; ++i) {
412
- var minj = i === 0 || i === m - 1 ? 1 : 0,
413
- maxj = i === 0 ? m - 1 : m;
414
- for (var j = minj; j < maxj; ++j) {
415
- blit(aligns[i], aligns[j], 5, 5, [0x1f, 0x11, 0x15, 0x11, 0x1f]);
416
- }
417
- }
418
-
419
- // version information
420
- if (needsverinfo(ver)) {
421
- var code = augumentbch(ver, 6, 0x1f25, 12);
422
- var k = 0;
423
- for (var i = 0; i < 6; ++i) {
424
- for (var j = 0; j < 3; ++j) {
425
- matrix[i][n - 11 + j] = matrix[n - 11 + j][i] = code >> k++ & 1;
426
- reserved[i][n - 11 + j] = reserved[n - 11 + j][i] = 1;
427
- }
428
- }
429
- }
430
- return {
431
- matrix: matrix,
432
- reserved: reserved
433
- };
434
- };
435
-
436
- // fills the data portion (i.e. unmarked in reserved) of the matrix with given
437
- // code words. the size of code words should be no more than available bits,
438
- // and remaining bits are padded to 0 (cf. JIS X 0510:2004 sec 8.7.3).
439
- var putdata = function (matrix, reserved, buf) {
440
- var n = matrix.length;
441
- var k = 0,
442
- dir = -1;
443
- for (var i = n - 1; i >= 0; i -= 2) {
444
- if (i == 6) --i; // skip the entire timing pattern column
445
- var jj = dir < 0 ? n - 1 : 0;
446
- for (var j = 0; j < n; ++j) {
447
- for (var ii = i; ii > i - 2; --ii) {
448
- if (!reserved[jj][ii]) {
449
- // may overflow, but (undefined >> x)
450
- // is 0 so it will auto-pad to zero.
451
- matrix[jj][ii] = buf[k >> 3] >> (~k & 7) & 1;
452
- ++k;
453
- }
454
- }
455
- jj += dir;
456
- }
457
- dir = -dir;
458
- }
459
- return matrix;
460
- };
461
-
462
- // XOR-masks the data portion of the matrix. repeating the call with the same
463
- // arguments will revert the prior call (convenient in the matrix evaluation).
464
- var maskdata = function (matrix, reserved, mask) {
465
- var maskf = MASKFUNCS[mask];
466
- var n = matrix.length;
467
- for (var i = 0; i < n; ++i) {
468
- for (var j = 0; j < n; ++j) {
469
- if (!reserved[i][j]) matrix[i][j] ^= maskf(i, j);
470
- }
471
- }
472
- return matrix;
473
- };
474
-
475
- // puts the format information.
476
- var putformatinfo = function (matrix, reserved, ecclevel, mask) {
477
- var n = matrix.length;
478
- var code = augumentbch(ecclevel << 3 | mask, 5, 0x537, 10) ^ 0x5412;
479
- for (var i = 0; i < 15; ++i) {
480
- 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];
481
- 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];
482
- matrix[r][8] = matrix[8][c] = code >> i & 1;
483
- // we don't have to mark those bits reserved; always done
484
- // in makebasematrix above.
485
- }
486
-
487
- return matrix;
488
- };
489
-
490
- // evaluates the resulting matrix and returns the score (lower is better).
491
- // (cf. JIS X 0510:2004 sec 8.8.2)
492
- //
493
- // the evaluation procedure tries to avoid the problematic patterns naturally
494
- // occuring from the original matrix. for example, it penaltizes the patterns
495
- // which just look like the finder pattern which will confuse the decoder.
496
- // we choose the mask which results in the lowest score among 8 possible ones.
497
- //
498
- // note: zxing seems to use the same procedure and in many cases its choice
499
- // agrees to ours, but sometimes it does not. practically it doesn't matter.
500
- var evaluatematrix = function (matrix) {
501
- // N1+(k-5) points for each consecutive row of k same-colored modules,
502
- // where k >= 5. no overlapping row counts.
503
- var PENALTY_CONSECUTIVE = 3;
504
- // N2 points for each 2x2 block of same-colored modules.
505
- // overlapping block does count.
506
- var PENALTY_TWOBYTWO = 3;
507
- // N3 points for each pattern with >4W:1B:1W:3B:1W:1B or
508
- // 1B:1W:3B:1W:1B:>4W, or their multiples (e.g. highly unlikely,
509
- // but 13W:3B:3W:9B:3W:3B counts).
510
- var PENALTY_FINDERLIKE = 40;
511
- // N4*k points for every (5*k)% deviation from 50% black density.
512
- // i.e. k=1 for 55~60% and 40~45%, k=2 for 60~65% and 35~40%, etc.
513
- var PENALTY_DENSITY = 10;
514
- var evaluategroup = function (groups) {
515
- // assumes [W,B,W,B,W,...,B,W]
516
- var score = 0;
517
- for (var i = 0; i < groups.length; ++i) {
518
- if (groups[i] >= 5) score += PENALTY_CONSECUTIVE + (groups[i] - 5);
519
- }
520
- for (var i = 5; i < groups.length; i += 2) {
521
- var p = groups[i];
522
- if (groups[i - 1] == p && groups[i - 2] == 3 * p && groups[i - 3] == p && groups[i - 4] == p && (groups[i - 5] >= 4 * p || groups[i + 1] >= 4 * p)) {
523
- // this part differs from zxing...
524
- score += PENALTY_FINDERLIKE;
525
- }
526
- }
527
- return score;
528
- };
529
- var n = matrix.length;
530
- var score = 0,
531
- nblacks = 0;
532
- for (var i = 0; i < n; ++i) {
533
- var row = matrix[i];
534
- var groups;
535
-
536
- // evaluate the current row
537
- groups = [0]; // the first empty group of white
538
- for (var j = 0; j < n;) {
539
- var k;
540
- for (k = 0; j < n && row[j]; ++k) ++j;
541
- groups.push(k);
542
- for (k = 0; j < n && !row[j]; ++k) ++j;
543
- groups.push(k);
544
- }
545
- score += evaluategroup(groups);
546
-
547
- // evaluate the current column
548
- groups = [0];
549
- for (var j = 0; j < n;) {
550
- var k;
551
- for (k = 0; j < n && matrix[j][i]; ++k) ++j;
552
- groups.push(k);
553
- for (k = 0; j < n && !matrix[j][i]; ++k) ++j;
554
- groups.push(k);
555
- }
556
- score += evaluategroup(groups);
557
-
558
- // check the 2x2 box and calculate the density
559
- var nextrow = matrix[i + 1] || [];
560
- nblacks += row[0];
561
- for (var j = 1; j < n; ++j) {
562
- var p = row[j];
563
- nblacks += p;
564
- // at least comparison with next row should be strict...
565
- if (row[j - 1] == p && nextrow[j] === p && nextrow[j - 1] === p) {
566
- score += PENALTY_TWOBYTWO;
567
- }
568
- }
569
- }
570
- score += PENALTY_DENSITY * (Math.abs(nblacks / n / n - 0.5) / 0.05 | 0);
571
- return score;
572
- };
573
-
574
- // returns the fully encoded QR code matrix which contains given data.
575
- // it also chooses the best mask automatically when mask is -1.
576
- var generate = function (data, ver, mode, ecclevel, mask) {
577
- var v = VERSIONS[ver];
578
- var buf = encode(ver, mode, data, ndatabits(ver, ecclevel) >> 3);
579
- buf = augumenteccs(buf, v[1][ecclevel], GF256_GENPOLY[v[0][ecclevel]]);
580
- var result = makebasematrix(ver);
581
- var matrix = result.matrix,
582
- reserved = result.reserved;
583
- putdata(matrix, reserved, buf);
584
- if (mask < 0) {
585
- // find the best mask
586
- maskdata(matrix, reserved, 0);
587
- putformatinfo(matrix, reserved, ecclevel, 0);
588
- var bestmask = 0,
589
- bestscore = evaluatematrix(matrix);
590
- maskdata(matrix, reserved, 0);
591
- for (mask = 1; mask < 8; ++mask) {
592
- maskdata(matrix, reserved, mask);
593
- putformatinfo(matrix, reserved, ecclevel, mask);
594
- var score = evaluatematrix(matrix);
595
- if (bestscore > score) {
596
- bestscore = score;
597
- bestmask = mask;
598
- }
599
- maskdata(matrix, reserved, mask);
600
- }
601
- mask = bestmask;
602
- }
603
- maskdata(matrix, reserved, mask);
604
- putformatinfo(matrix, reserved, ecclevel, mask);
605
- return matrix;
606
- };
607
-
608
- // the public interface is trivial; the options available are as follows:
609
- //
610
- // - version: an integer in [1,40]. when omitted (or -1) the smallest possible
611
- // version is chosen.
612
- // - mode: one of 'numeric', 'alphanumeric', 'octet'. when omitted the smallest
613
- // possible mode is chosen.
614
- // - eccLevel: one of 'L', 'M', 'Q', 'H'. defaults to 'L'.
615
- // - mask: an integer in [0,7]. when omitted (or -1) the best mask is chosen.
616
- //
617
-
618
- function generateFrame(data, options) {
619
- var MODES = {
620
- 'numeric': MODE_NUMERIC,
621
- 'alphanumeric': MODE_ALPHANUMERIC,
622
- 'octet': MODE_OCTET
623
- };
624
- var ECCLEVELS = {
625
- 'L': ECCLEVEL_L,
626
- 'M': ECCLEVEL_M,
627
- 'Q': ECCLEVEL_Q,
628
- 'H': ECCLEVEL_H
629
- };
630
- options = options || {};
631
- var ver = options.version || -1;
632
- var ecclevel = ECCLEVELS[(options.eccLevel || 'L').toUpperCase()];
633
- var mode = options.mode ? MODES[options.mode.toLowerCase()] : -1;
634
- var mask = 'mask' in options ? options.mask : -1;
635
- if (mode < 0) {
636
- if (typeof data === 'string') {
637
- if (data.match(NUMERIC_REGEXP)) {
638
- mode = MODE_NUMERIC;
639
- } else if (data.match(ALPHANUMERIC_OUT_REGEXP)) {
640
- // while encode supports case-insensitive encoding, we restrict the data to be uppercased when auto-selecting the mode.
641
- mode = MODE_ALPHANUMERIC;
642
- } else {
643
- mode = MODE_OCTET;
644
- }
645
- } else {
646
- mode = MODE_OCTET;
647
- }
648
- } else if (!(mode == MODE_NUMERIC || mode == MODE_ALPHANUMERIC || mode == MODE_OCTET)) {
649
- throw 'invalid or unsupported mode';
650
- }
651
- data = validatedata(mode, data);
652
- if (data === null) throw 'invalid data format';
653
- if (ecclevel < 0 || ecclevel > 3) throw 'invalid ECC level';
654
- if (ver < 0) {
655
- for (ver = 1; ver <= 40; ++ver) {
656
- if (data.length <= getmaxdatalen(ver, mode, ecclevel)) break;
657
- }
658
- if (ver > 40) throw 'too large data for the Qr format';
659
- } else if (ver < 1 || ver > 40) {
660
- throw 'invalid Qr version! should be between 1 and 40';
661
- }
662
- if (mask != -1 && (mask < 0 || mask > 8)) throw 'invalid mask';
663
- //console.log('version:', ver, 'mode:', mode, 'ECC:', ecclevel, 'mask:', mask )
664
- return generate(data, ver, mode, ecclevel, mask);
665
- }
666
-
667
- // options
668
- // - modulesize: a number. this is a size of each modules in pixels, and
669
- // defaults to 5px.
670
- // - margin: a number. this is a size of margin in *modules*, and defaults to
671
- // 4 (white modules). the specficiation mandates the margin no less than 4
672
- // modules, so it is better not to alter this value unless you know what
673
- // you're doing.
674
- function buildCanvas(data, options) {
675
- var canvas = [];
676
- var background = options.background || '#fff';
677
- var foreground = options.foreground || '#000';
678
- //var margin = options.margin || 4;
679
- var matrix = generateFrame(data, options);
680
- var n = matrix.length;
681
- var modSize = Math.floor(options.fit ? options.fit / n : 5);
682
- var size = n * modSize;
683
- canvas.push({
684
- type: 'rect',
685
- x: 0,
686
- y: 0,
687
- w: size,
688
- h: size,
689
- lineWidth: 0,
690
- color: background
691
- });
692
- for (var i = 0; i < n; ++i) {
693
- for (var j = 0; j < n; ++j) {
694
- if (matrix[i][j]) {
695
- canvas.push({
696
- type: 'rect',
697
- x: modSize * j,
698
- y: modSize * i,
699
- w: modSize,
700
- h: modSize,
701
- lineWidth: 0,
702
- color: foreground
703
- });
704
- }
705
- }
706
- }
707
- return {
708
- canvas: canvas,
709
- size: size
710
- };
711
- }
712
- function measure(node) {
713
- var cd = buildCanvas(node.qr, node);
714
- node._canvas = cd.canvas;
715
- node._width = node._height = node._minWidth = node._maxWidth = node._minHeight = node._maxHeight = cd.size;
716
- return node;
717
- }
718
- var _default = exports.default = {
719
- measure: measure
720
- };