pdfmake 0.3.0-beta.7 → 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 (49) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/build/pdfmake.js +2654 -2525
  3. package/build/pdfmake.js.map +1 -1
  4. package/build/pdfmake.min.js +2 -2
  5. package/build/pdfmake.min.js.map +1 -1
  6. package/package.json +1 -1
  7. package/js/3rd-party/svg-to-pdfkit/source.js +0 -3626
  8. package/js/3rd-party/svg-to-pdfkit.js +0 -7
  9. package/js/DocMeasure.js +0 -627
  10. package/js/DocPreprocessor.js +0 -238
  11. package/js/DocumentContext.js +0 -265
  12. package/js/ElementWriter.js +0 -331
  13. package/js/LayoutBuilder.js +0 -694
  14. package/js/Line.js +0 -105
  15. package/js/OutputDocument.js +0 -76
  16. package/js/OutputDocumentServer.js +0 -27
  17. package/js/PDFDocument.js +0 -144
  18. package/js/PageElementWriter.js +0 -140
  19. package/js/PageSize.js +0 -74
  20. package/js/Printer.js +0 -291
  21. package/js/Renderer.js +0 -375
  22. package/js/SVGMeasure.js +0 -69
  23. package/js/StyleContextStack.js +0 -180
  24. package/js/TableProcessor.js +0 -511
  25. package/js/TextBreaker.js +0 -139
  26. package/js/TextDecorator.js +0 -143
  27. package/js/TextInlines.js +0 -206
  28. package/js/URLResolver.js +0 -73
  29. package/js/base.js +0 -52
  30. package/js/browser-extensions/OutputDocumentBrowser.js +0 -118
  31. package/js/browser-extensions/URLBrowserResolver.js +0 -76
  32. package/js/browser-extensions/fonts/Roboto.js +0 -38
  33. package/js/browser-extensions/index.js +0 -53
  34. package/js/browser-extensions/pdfMake.js +0 -15
  35. package/js/browser-extensions/standard-fonts/Courier.js +0 -38
  36. package/js/browser-extensions/standard-fonts/Helvetica.js +0 -38
  37. package/js/browser-extensions/standard-fonts/Symbol.js +0 -23
  38. package/js/browser-extensions/standard-fonts/Times.js +0 -38
  39. package/js/browser-extensions/standard-fonts/ZapfDingbats.js +0 -23
  40. package/js/browser-extensions/virtual-fs-cjs.js +0 -3
  41. package/js/columnCalculator.js +0 -129
  42. package/js/helpers/node.js +0 -98
  43. package/js/helpers/tools.js +0 -40
  44. package/js/helpers/variableType.js +0 -47
  45. package/js/index.js +0 -15
  46. package/js/qrEnc.js +0 -721
  47. package/js/standardPageSizes.js +0 -56
  48. package/js/tableLayouts.js +0 -98
  49. package/js/virtual-fs.js +0 -60
package/js/qrEnc.js DELETED
@@ -1,721 +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
- return matrix;
487
- };
488
-
489
- // evaluates the resulting matrix and returns the score (lower is better).
490
- // (cf. JIS X 0510:2004 sec 8.8.2)
491
- //
492
- // the evaluation procedure tries to avoid the problematic patterns naturally
493
- // occuring from the original matrix. for example, it penaltizes the patterns
494
- // which just look like the finder pattern which will confuse the decoder.
495
- // we choose the mask which results in the lowest score among 8 possible ones.
496
- //
497
- // note: zxing seems to use the same procedure and in many cases its choice
498
- // agrees to ours, but sometimes it does not. practically it doesn't matter.
499
- var evaluatematrix = function (matrix) {
500
- // N1+(k-5) points for each consecutive row of k same-colored modules,
501
- // where k >= 5. no overlapping row counts.
502
- var PENALTY_CONSECUTIVE = 3;
503
- // N2 points for each 2x2 block of same-colored modules.
504
- // overlapping block does count.
505
- var PENALTY_TWOBYTWO = 3;
506
- // N3 points for each pattern with >4W:1B:1W:3B:1W:1B or
507
- // 1B:1W:3B:1W:1B:>4W, or their multiples (e.g. highly unlikely,
508
- // but 13W:3B:3W:9B:3W:3B counts).
509
- var PENALTY_FINDERLIKE = 40;
510
- // N4*k points for every (5*k)% deviation from 50% black density.
511
- // i.e. k=1 for 55~60% and 40~45%, k=2 for 60~65% and 35~40%, etc.
512
- var PENALTY_DENSITY = 10;
513
- var evaluategroup = function (groups) {
514
- // assumes [W,B,W,B,W,...,B,W]
515
- var score = 0;
516
- for (var i = 0; i < groups.length; ++i) {
517
- if (groups[i] >= 5) score += PENALTY_CONSECUTIVE + (groups[i] - 5);
518
- }
519
- for (var i = 5; i < groups.length; i += 2) {
520
- var p = groups[i];
521
- 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)) {
522
- // this part differs from zxing...
523
- score += PENALTY_FINDERLIKE;
524
- }
525
- }
526
- return score;
527
- };
528
- var n = matrix.length;
529
- var score = 0,
530
- nblacks = 0;
531
- for (var i = 0; i < n; ++i) {
532
- var row = matrix[i];
533
- var groups;
534
-
535
- // evaluate the current row
536
- groups = [0]; // the first empty group of white
537
- for (var j = 0; j < n;) {
538
- var k;
539
- for (k = 0; j < n && row[j]; ++k) ++j;
540
- groups.push(k);
541
- for (k = 0; j < n && !row[j]; ++k) ++j;
542
- groups.push(k);
543
- }
544
- score += evaluategroup(groups);
545
-
546
- // evaluate the current column
547
- groups = [0];
548
- for (var j = 0; j < n;) {
549
- var k;
550
- for (k = 0; j < n && matrix[j][i]; ++k) ++j;
551
- groups.push(k);
552
- for (k = 0; j < n && !matrix[j][i]; ++k) ++j;
553
- groups.push(k);
554
- }
555
- score += evaluategroup(groups);
556
-
557
- // check the 2x2 box and calculate the density
558
- var nextrow = matrix[i + 1] || [];
559
- nblacks += row[0];
560
- for (var j = 1; j < n; ++j) {
561
- var p = row[j];
562
- nblacks += p;
563
- // at least comparison with next row should be strict...
564
- if (row[j - 1] == p && nextrow[j] === p && nextrow[j - 1] === p) {
565
- score += PENALTY_TWOBYTWO;
566
- }
567
- }
568
- }
569
- score += PENALTY_DENSITY * (Math.abs(nblacks / n / n - 0.5) / 0.05 | 0);
570
- return score;
571
- };
572
-
573
- // returns the fully encoded QR code matrix which contains given data.
574
- // it also chooses the best mask automatically when mask is -1.
575
- var generate = function (data, ver, mode, ecclevel, mask) {
576
- var v = VERSIONS[ver];
577
- var buf = encode(ver, mode, data, ndatabits(ver, ecclevel) >> 3);
578
- buf = augumenteccs(buf, v[1][ecclevel], GF256_GENPOLY[v[0][ecclevel]]);
579
- var result = makebasematrix(ver);
580
- var matrix = result.matrix,
581
- reserved = result.reserved;
582
- putdata(matrix, reserved, buf);
583
- if (mask < 0) {
584
- // find the best mask
585
- maskdata(matrix, reserved, 0);
586
- putformatinfo(matrix, reserved, ecclevel, 0);
587
- var bestmask = 0,
588
- bestscore = evaluatematrix(matrix);
589
- maskdata(matrix, reserved, 0);
590
- for (mask = 1; mask < 8; ++mask) {
591
- maskdata(matrix, reserved, mask);
592
- putformatinfo(matrix, reserved, ecclevel, mask);
593
- var score = evaluatematrix(matrix);
594
- if (bestscore > score) {
595
- bestscore = score;
596
- bestmask = mask;
597
- }
598
- maskdata(matrix, reserved, mask);
599
- }
600
- mask = bestmask;
601
- }
602
- maskdata(matrix, reserved, mask);
603
- putformatinfo(matrix, reserved, ecclevel, mask);
604
- return matrix;
605
- };
606
-
607
- // the public interface is trivial; the options available are as follows:
608
- //
609
- // - version: an integer in [1,40]. when omitted (or -1) the smallest possible
610
- // version is chosen.
611
- // - mode: one of 'numeric', 'alphanumeric', 'octet'. when omitted the smallest
612
- // possible mode is chosen.
613
- // - eccLevel: one of 'L', 'M', 'Q', 'H'. defaults to 'L'.
614
- // - mask: an integer in [0,7]. when omitted (or -1) the best mask is chosen.
615
- //
616
-
617
- function generateFrame(data, options) {
618
- var MODES = {
619
- 'numeric': MODE_NUMERIC,
620
- 'alphanumeric': MODE_ALPHANUMERIC,
621
- 'octet': MODE_OCTET
622
- };
623
- var ECCLEVELS = {
624
- 'L': ECCLEVEL_L,
625
- 'M': ECCLEVEL_M,
626
- 'Q': ECCLEVEL_Q,
627
- 'H': ECCLEVEL_H
628
- };
629
- options = options || {};
630
- var ver = options.version || -1;
631
- var ecclevel = ECCLEVELS[(options.eccLevel || 'L').toUpperCase()];
632
- var mode = options.mode ? MODES[options.mode.toLowerCase()] : -1;
633
- var mask = 'mask' in options ? options.mask : -1;
634
- if (mode < 0) {
635
- if (typeof data === 'string') {
636
- if (data.match(NUMERIC_REGEXP)) {
637
- mode = MODE_NUMERIC;
638
- } else if (data.match(ALPHANUMERIC_OUT_REGEXP)) {
639
- // while encode supports case-insensitive encoding, we restrict the data to be uppercased when auto-selecting the mode.
640
- mode = MODE_ALPHANUMERIC;
641
- } else {
642
- mode = MODE_OCTET;
643
- }
644
- } else {
645
- mode = MODE_OCTET;
646
- }
647
- } else if (!(mode == MODE_NUMERIC || mode == MODE_ALPHANUMERIC || mode == MODE_OCTET)) {
648
- throw 'invalid or unsupported mode';
649
- }
650
- data = validatedata(mode, data);
651
- if (data === null) throw 'invalid data format';
652
- if (ecclevel < 0 || ecclevel > 3) throw 'invalid ECC level';
653
- if (ver < 0) {
654
- for (ver = 1; ver <= 40; ++ver) {
655
- if (data.length <= getmaxdatalen(ver, mode, ecclevel)) break;
656
- }
657
- if (ver > 40) throw 'too large data for the Qr format';
658
- } else if (ver < 1 || ver > 40) {
659
- throw 'invalid Qr version! should be between 1 and 40';
660
- }
661
- if (mask != -1 && (mask < 0 || mask > 8)) throw 'invalid mask';
662
- //console.log('version:', ver, 'mode:', mode, 'ECC:', ecclevel, 'mask:', mask )
663
- return generate(data, ver, mode, ecclevel, mask);
664
- }
665
-
666
- // options
667
- // - modulesize: a number. this is a size of each modules in pixels, and
668
- // defaults to 5px.
669
- // - margin: a number. this is a size of margin in *modules*, and defaults to
670
- // 4 (white modules). the specficiation mandates the margin no less than 4
671
- // modules, so it is better not to alter this value unless you know what
672
- // you're doing.
673
- function buildCanvas(data, options) {
674
- var canvas = [];
675
- var background = options.background || '#fff';
676
- var foreground = options.foreground || '#000';
677
- var padding = options.padding || 0;
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 + modSize * padding * 2;
683
- var paddingXY = modSize * padding;
684
- canvas.push({
685
- type: 'rect',
686
- x: 0,
687
- y: 0,
688
- w: size,
689
- h: size,
690
- lineWidth: 0,
691
- color: background
692
- });
693
- for (var i = 0; i < n; ++i) {
694
- for (var j = 0; j < n; ++j) {
695
- if (matrix[i][j]) {
696
- canvas.push({
697
- type: 'rect',
698
- x: modSize * j + paddingXY,
699
- y: modSize * i + paddingXY,
700
- w: modSize,
701
- h: modSize,
702
- lineWidth: 0,
703
- color: foreground
704
- });
705
- }
706
- }
707
- }
708
- return {
709
- canvas: canvas,
710
- size: size
711
- };
712
- }
713
- function measure(node) {
714
- var cd = buildCanvas(node.qr, node);
715
- node._canvas = cd.canvas;
716
- node._width = node._height = node._minWidth = node._maxWidth = node._minHeight = node._maxHeight = cd.size;
717
- return node;
718
- }
719
- var _default = exports.default = {
720
- measure: measure
721
- };