@depup/qrcode 1.5.4-depup.0

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 (42) hide show
  1. package/README.md +33 -0
  2. package/bin/qrcode +159 -0
  3. package/changes.json +18 -0
  4. package/helper/to-sjis-browser.js +2 -0
  5. package/helper/to-sjis.js +105 -0
  6. package/lib/browser.js +76 -0
  7. package/lib/can-promise.js +7 -0
  8. package/lib/core/alignment-pattern.js +83 -0
  9. package/lib/core/alphanumeric-data.js +59 -0
  10. package/lib/core/bit-buffer.js +37 -0
  11. package/lib/core/bit-matrix.js +65 -0
  12. package/lib/core/byte-data.js +30 -0
  13. package/lib/core/error-correction-code.js +135 -0
  14. package/lib/core/error-correction-level.js +50 -0
  15. package/lib/core/finder-pattern.js +22 -0
  16. package/lib/core/format-info.js +29 -0
  17. package/lib/core/galois-field.js +69 -0
  18. package/lib/core/kanji-data.js +54 -0
  19. package/lib/core/mask-pattern.js +234 -0
  20. package/lib/core/mode.js +167 -0
  21. package/lib/core/numeric-data.js +43 -0
  22. package/lib/core/polynomial.js +62 -0
  23. package/lib/core/qrcode.js +495 -0
  24. package/lib/core/reed-solomon-encoder.js +56 -0
  25. package/lib/core/regex.js +31 -0
  26. package/lib/core/segments.js +330 -0
  27. package/lib/core/utils.js +63 -0
  28. package/lib/core/version-check.js +9 -0
  29. package/lib/core/version.js +163 -0
  30. package/lib/index.js +12 -0
  31. package/lib/renderer/canvas.js +63 -0
  32. package/lib/renderer/png.js +78 -0
  33. package/lib/renderer/svg-tag.js +81 -0
  34. package/lib/renderer/svg.js +19 -0
  35. package/lib/renderer/terminal/terminal-small.js +85 -0
  36. package/lib/renderer/terminal/terminal.js +49 -0
  37. package/lib/renderer/terminal.js +9 -0
  38. package/lib/renderer/utf8.js +71 -0
  39. package/lib/renderer/utils.js +99 -0
  40. package/lib/server.js +138 -0
  41. package/license +10 -0
  42. package/package.json +102 -0
@@ -0,0 +1,167 @@
1
+ const VersionCheck = require('./version-check')
2
+ const Regex = require('./regex')
3
+
4
+ /**
5
+ * Numeric mode encodes data from the decimal digit set (0 - 9)
6
+ * (byte values 30HEX to 39HEX).
7
+ * Normally, 3 data characters are represented by 10 bits.
8
+ *
9
+ * @type {Object}
10
+ */
11
+ exports.NUMERIC = {
12
+ id: 'Numeric',
13
+ bit: 1 << 0,
14
+ ccBits: [10, 12, 14]
15
+ }
16
+
17
+ /**
18
+ * Alphanumeric mode encodes data from a set of 45 characters,
19
+ * i.e. 10 numeric digits (0 - 9),
20
+ * 26 alphabetic characters (A - Z),
21
+ * and 9 symbols (SP, $, %, *, +, -, ., /, :).
22
+ * Normally, two input characters are represented by 11 bits.
23
+ *
24
+ * @type {Object}
25
+ */
26
+ exports.ALPHANUMERIC = {
27
+ id: 'Alphanumeric',
28
+ bit: 1 << 1,
29
+ ccBits: [9, 11, 13]
30
+ }
31
+
32
+ /**
33
+ * In byte mode, data is encoded at 8 bits per character.
34
+ *
35
+ * @type {Object}
36
+ */
37
+ exports.BYTE = {
38
+ id: 'Byte',
39
+ bit: 1 << 2,
40
+ ccBits: [8, 16, 16]
41
+ }
42
+
43
+ /**
44
+ * The Kanji mode efficiently encodes Kanji characters in accordance with
45
+ * the Shift JIS system based on JIS X 0208.
46
+ * The Shift JIS values are shifted from the JIS X 0208 values.
47
+ * JIS X 0208 gives details of the shift coded representation.
48
+ * Each two-byte character value is compacted to a 13-bit binary codeword.
49
+ *
50
+ * @type {Object}
51
+ */
52
+ exports.KANJI = {
53
+ id: 'Kanji',
54
+ bit: 1 << 3,
55
+ ccBits: [8, 10, 12]
56
+ }
57
+
58
+ /**
59
+ * Mixed mode will contain a sequences of data in a combination of any of
60
+ * the modes described above
61
+ *
62
+ * @type {Object}
63
+ */
64
+ exports.MIXED = {
65
+ bit: -1
66
+ }
67
+
68
+ /**
69
+ * Returns the number of bits needed to store the data length
70
+ * according to QR Code specifications.
71
+ *
72
+ * @param {Mode} mode Data mode
73
+ * @param {Number} version QR Code version
74
+ * @return {Number} Number of bits
75
+ */
76
+ exports.getCharCountIndicator = function getCharCountIndicator (mode, version) {
77
+ if (!mode.ccBits) throw new Error('Invalid mode: ' + mode)
78
+
79
+ if (!VersionCheck.isValid(version)) {
80
+ throw new Error('Invalid version: ' + version)
81
+ }
82
+
83
+ if (version >= 1 && version < 10) return mode.ccBits[0]
84
+ else if (version < 27) return mode.ccBits[1]
85
+ return mode.ccBits[2]
86
+ }
87
+
88
+ /**
89
+ * Returns the most efficient mode to store the specified data
90
+ *
91
+ * @param {String} dataStr Input data string
92
+ * @return {Mode} Best mode
93
+ */
94
+ exports.getBestModeForData = function getBestModeForData (dataStr) {
95
+ if (Regex.testNumeric(dataStr)) return exports.NUMERIC
96
+ else if (Regex.testAlphanumeric(dataStr)) return exports.ALPHANUMERIC
97
+ else if (Regex.testKanji(dataStr)) return exports.KANJI
98
+ else return exports.BYTE
99
+ }
100
+
101
+ /**
102
+ * Return mode name as string
103
+ *
104
+ * @param {Mode} mode Mode object
105
+ * @returns {String} Mode name
106
+ */
107
+ exports.toString = function toString (mode) {
108
+ if (mode && mode.id) return mode.id
109
+ throw new Error('Invalid mode')
110
+ }
111
+
112
+ /**
113
+ * Check if input param is a valid mode object
114
+ *
115
+ * @param {Mode} mode Mode object
116
+ * @returns {Boolean} True if valid mode, false otherwise
117
+ */
118
+ exports.isValid = function isValid (mode) {
119
+ return mode && mode.bit && mode.ccBits
120
+ }
121
+
122
+ /**
123
+ * Get mode object from its name
124
+ *
125
+ * @param {String} string Mode name
126
+ * @returns {Mode} Mode object
127
+ */
128
+ function fromString (string) {
129
+ if (typeof string !== 'string') {
130
+ throw new Error('Param is not a string')
131
+ }
132
+
133
+ const lcStr = string.toLowerCase()
134
+
135
+ switch (lcStr) {
136
+ case 'numeric':
137
+ return exports.NUMERIC
138
+ case 'alphanumeric':
139
+ return exports.ALPHANUMERIC
140
+ case 'kanji':
141
+ return exports.KANJI
142
+ case 'byte':
143
+ return exports.BYTE
144
+ default:
145
+ throw new Error('Unknown mode: ' + string)
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Returns mode from a value.
151
+ * If value is not a valid mode, returns defaultValue
152
+ *
153
+ * @param {Mode|String} value Encoding mode
154
+ * @param {Mode} defaultValue Fallback value
155
+ * @return {Mode} Encoding mode
156
+ */
157
+ exports.from = function from (value, defaultValue) {
158
+ if (exports.isValid(value)) {
159
+ return value
160
+ }
161
+
162
+ try {
163
+ return fromString(value)
164
+ } catch (e) {
165
+ return defaultValue
166
+ }
167
+ }
@@ -0,0 +1,43 @@
1
+ const Mode = require('./mode')
2
+
3
+ function NumericData (data) {
4
+ this.mode = Mode.NUMERIC
5
+ this.data = data.toString()
6
+ }
7
+
8
+ NumericData.getBitsLength = function getBitsLength (length) {
9
+ return 10 * Math.floor(length / 3) + ((length % 3) ? ((length % 3) * 3 + 1) : 0)
10
+ }
11
+
12
+ NumericData.prototype.getLength = function getLength () {
13
+ return this.data.length
14
+ }
15
+
16
+ NumericData.prototype.getBitsLength = function getBitsLength () {
17
+ return NumericData.getBitsLength(this.data.length)
18
+ }
19
+
20
+ NumericData.prototype.write = function write (bitBuffer) {
21
+ let i, group, value
22
+
23
+ // The input data string is divided into groups of three digits,
24
+ // and each group is converted to its 10-bit binary equivalent.
25
+ for (i = 0; i + 3 <= this.data.length; i += 3) {
26
+ group = this.data.substr(i, 3)
27
+ value = parseInt(group, 10)
28
+
29
+ bitBuffer.put(value, 10)
30
+ }
31
+
32
+ // If the number of input digits is not an exact multiple of three,
33
+ // the final one or two digits are converted to 4 or 7 bits respectively.
34
+ const remainingNum = this.data.length - i
35
+ if (remainingNum > 0) {
36
+ group = this.data.substr(i)
37
+ value = parseInt(group, 10)
38
+
39
+ bitBuffer.put(value, remainingNum * 3 + 1)
40
+ }
41
+ }
42
+
43
+ module.exports = NumericData
@@ -0,0 +1,62 @@
1
+ const GF = require('./galois-field')
2
+
3
+ /**
4
+ * Multiplies two polynomials inside Galois Field
5
+ *
6
+ * @param {Uint8Array} p1 Polynomial
7
+ * @param {Uint8Array} p2 Polynomial
8
+ * @return {Uint8Array} Product of p1 and p2
9
+ */
10
+ exports.mul = function mul (p1, p2) {
11
+ const coeff = new Uint8Array(p1.length + p2.length - 1)
12
+
13
+ for (let i = 0; i < p1.length; i++) {
14
+ for (let j = 0; j < p2.length; j++) {
15
+ coeff[i + j] ^= GF.mul(p1[i], p2[j])
16
+ }
17
+ }
18
+
19
+ return coeff
20
+ }
21
+
22
+ /**
23
+ * Calculate the remainder of polynomials division
24
+ *
25
+ * @param {Uint8Array} divident Polynomial
26
+ * @param {Uint8Array} divisor Polynomial
27
+ * @return {Uint8Array} Remainder
28
+ */
29
+ exports.mod = function mod (divident, divisor) {
30
+ let result = new Uint8Array(divident)
31
+
32
+ while ((result.length - divisor.length) >= 0) {
33
+ const coeff = result[0]
34
+
35
+ for (let i = 0; i < divisor.length; i++) {
36
+ result[i] ^= GF.mul(divisor[i], coeff)
37
+ }
38
+
39
+ // remove all zeros from buffer head
40
+ let offset = 0
41
+ while (offset < result.length && result[offset] === 0) offset++
42
+ result = result.slice(offset)
43
+ }
44
+
45
+ return result
46
+ }
47
+
48
+ /**
49
+ * Generate an irreducible generator polynomial of specified degree
50
+ * (used by Reed-Solomon encoder)
51
+ *
52
+ * @param {Number} degree Degree of the generator polynomial
53
+ * @return {Uint8Array} Buffer containing polynomial coefficients
54
+ */
55
+ exports.generateECPolynomial = function generateECPolynomial (degree) {
56
+ let poly = new Uint8Array([1])
57
+ for (let i = 0; i < degree; i++) {
58
+ poly = exports.mul(poly, new Uint8Array([1, GF.exp(i)]))
59
+ }
60
+
61
+ return poly
62
+ }