utils.js 4.1 KB

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