utils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var toSJISFunction
  2. var CODEWORDS_COUNT = [
  3. 0, // Not used
  4. 26, 44, 70, 100, 134, 172, 196, 242, 292, 346,
  5. 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085,
  6. 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185,
  7. 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706
  8. ]
  9. /**
  10. * Returns the QR Code size for the specified version
  11. *
  12. * @param {Number} version QR Code version
  13. * @return {Number} size of QR code
  14. */
  15. exports.getSymbolSize = function getSymbolSize (version) {
  16. if (!version) throw new Error('"version" cannot be null or undefined')
  17. if (version < 1 || version > 40) throw new Error('"version" should be in range from 1 to 40')
  18. return version * 4 + 17
  19. }
  20. /**
  21. * Returns the total number of codewords used to store data and EC information.
  22. *
  23. * @param {Number} version QR Code version
  24. * @return {Number} Data length in bits
  25. */
  26. exports.getSymbolTotalCodewords = function getSymbolTotalCodewords (version) {
  27. return CODEWORDS_COUNT[version]
  28. }
  29. /**
  30. * Encode data with Bose-Chaudhuri-Hocquenghem
  31. *
  32. * @param {Number} data Value to encode
  33. * @return {Number} Encoded value
  34. */
  35. exports.getBCHDigit = function (data) {
  36. var digit = 0
  37. while (data !== 0) {
  38. digit++
  39. data >>>= 1
  40. }
  41. return digit
  42. }
  43. exports.setToSJISFunction = function setToSJISFunction (f) {
  44. if (typeof f !== 'function') {
  45. throw new Error('"toSJISFunc" is not a valid function.')
  46. }
  47. toSJISFunction = f
  48. }
  49. exports.isKanjiModeEnabled = function () {
  50. return typeof toSJISFunction !== 'undefined'
  51. }
  52. exports.toSJIS = function toSJIS (kanji) {
  53. return toSJISFunction(kanji)
  54. }