pdfmake-acroforms 0.3.4

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