utils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.isSupportPath2d = exports.getMarginSize = exports.getImageSettings = exports.generatePath = exports.excavateModules = exports.SPEC_MARGIN_SIZE = exports.ERROR_LEVEL_MAP = exports.DEFAULT_SIZE = exports.DEFAULT_NEED_MARGIN = exports.DEFAULT_MINVERSION = exports.DEFAULT_MARGIN_SIZE = exports.DEFAULT_LEVEL = exports.DEFAULT_IMG_SCALE = exports.DEFAULT_FRONT_COLOR = exports.DEFAULT_BACKGROUND_COLOR = void 0;
  6. var _qrcodegen = require("./libs/qrcodegen");
  7. // Part logic is from `qrcode.react`. (ISC License)
  8. // https://github.com/zpao/qrcode.react
  9. // ==========================================================
  10. // =================== ERROR_LEVEL ==========================
  11. var ERROR_LEVEL_MAP = exports.ERROR_LEVEL_MAP = {
  12. L: _qrcodegen.Ecc.LOW,
  13. M: _qrcodegen.Ecc.MEDIUM,
  14. Q: _qrcodegen.Ecc.QUARTILE,
  15. H: _qrcodegen.Ecc.HIGH
  16. };
  17. // =================== DEFAULT_VALUE ==========================
  18. var DEFAULT_SIZE = exports.DEFAULT_SIZE = 128;
  19. var DEFAULT_LEVEL = exports.DEFAULT_LEVEL = 'L';
  20. var DEFAULT_BACKGROUND_COLOR = exports.DEFAULT_BACKGROUND_COLOR = '#FFFFFF';
  21. var DEFAULT_FRONT_COLOR = exports.DEFAULT_FRONT_COLOR = '#000000';
  22. var DEFAULT_NEED_MARGIN = exports.DEFAULT_NEED_MARGIN = false;
  23. var DEFAULT_MINVERSION = exports.DEFAULT_MINVERSION = 1;
  24. var SPEC_MARGIN_SIZE = exports.SPEC_MARGIN_SIZE = 4;
  25. var DEFAULT_MARGIN_SIZE = exports.DEFAULT_MARGIN_SIZE = 0;
  26. var DEFAULT_IMG_SCALE = exports.DEFAULT_IMG_SCALE = 0.1;
  27. // =================== UTILS ==========================
  28. /**
  29. * Generate a path string from modules
  30. * @param modules
  31. * @param margin
  32. * @returns
  33. */
  34. var generatePath = exports.generatePath = function generatePath(modules) {
  35. var margin = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  36. var ops = [];
  37. modules.forEach(function (row, y) {
  38. var start = null;
  39. row.forEach(function (cell, x) {
  40. if (!cell && start !== null) {
  41. ops.push("M".concat(start + margin, " ").concat(y + margin, "h").concat(x - start, "v1H").concat(start + margin, "z"));
  42. start = null;
  43. return;
  44. }
  45. if (x === row.length - 1) {
  46. if (!cell) {
  47. return;
  48. }
  49. if (start === null) {
  50. ops.push("M".concat(x + margin, ",").concat(y + margin, " h1v1H").concat(x + margin, "z"));
  51. } else {
  52. ops.push("M".concat(start + margin, ",").concat(y + margin, " h").concat(x + 1 - start, "v1H").concat(start + margin, "z"));
  53. }
  54. return;
  55. }
  56. if (cell && start === null) {
  57. start = x;
  58. }
  59. });
  60. });
  61. return ops.join('');
  62. };
  63. /**
  64. * Excavate modules
  65. * @param modules
  66. * @param excavation
  67. * @returns
  68. */
  69. var excavateModules = exports.excavateModules = function excavateModules(modules, excavation) {
  70. return modules.slice().map(function (row, y) {
  71. if (y < excavation.y || y >= excavation.y + excavation.h) {
  72. return row;
  73. }
  74. return row.map(function (cell, x) {
  75. if (x < excavation.x || x >= excavation.x + excavation.w) {
  76. return cell;
  77. }
  78. return false;
  79. });
  80. });
  81. };
  82. /**
  83. * Get image settings
  84. * @param cells The modules of the QR code
  85. * @param size The size of the QR code
  86. * @param margin
  87. * @param imageSettings
  88. * @returns
  89. */
  90. var getImageSettings = exports.getImageSettings = function getImageSettings(cells, size, margin, imageSettings) {
  91. if (imageSettings == null) {
  92. return null;
  93. }
  94. var numCells = cells.length + margin * 2;
  95. var defaultSize = Math.floor(size * DEFAULT_IMG_SCALE);
  96. var scale = numCells / size;
  97. var w = (imageSettings.width || defaultSize) * scale;
  98. var h = (imageSettings.height || defaultSize) * scale;
  99. var x = imageSettings.x == null ? cells.length / 2 - w / 2 : imageSettings.x * scale;
  100. var y = imageSettings.y == null ? cells.length / 2 - h / 2 : imageSettings.y * scale;
  101. var opacity = imageSettings.opacity == null ? 1 : imageSettings.opacity;
  102. var excavation = null;
  103. if (imageSettings.excavate) {
  104. var floorX = Math.floor(x);
  105. var floorY = Math.floor(y);
  106. var ceilW = Math.ceil(w + x - floorX);
  107. var ceilH = Math.ceil(h + y - floorY);
  108. excavation = {
  109. x: floorX,
  110. y: floorY,
  111. w: ceilW,
  112. h: ceilH
  113. };
  114. }
  115. var crossOrigin = imageSettings.crossOrigin;
  116. return {
  117. x: x,
  118. y: y,
  119. h: h,
  120. w: w,
  121. excavation: excavation,
  122. opacity: opacity,
  123. crossOrigin: crossOrigin
  124. };
  125. };
  126. /**
  127. * Get margin size
  128. * @param needMargin Whether need margin
  129. * @param marginSize Custom margin size
  130. * @returns
  131. */
  132. var getMarginSize = exports.getMarginSize = function getMarginSize(needMargin, marginSize) {
  133. if (marginSize != null) {
  134. return Math.max(Math.floor(marginSize), 0);
  135. }
  136. return needMargin ? SPEC_MARGIN_SIZE : DEFAULT_MARGIN_SIZE;
  137. };
  138. /**
  139. * Check if Path2D is supported
  140. */
  141. var isSupportPath2d = exports.isSupportPath2d = function () {
  142. try {
  143. new Path2D().addPath(new Path2D());
  144. } catch (_unused) {
  145. return false;
  146. }
  147. return true;
  148. }();