BrowserSvgCodeWriter.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BrowserSvgCodeWriter = void 0;
  4. var EncodeHintType_1 = require("../core/EncodeHintType");
  5. var Encoder_1 = require("../core/qrcode/encoder/Encoder");
  6. var ErrorCorrectionLevel_1 = require("../core/qrcode/decoder/ErrorCorrectionLevel");
  7. var IllegalArgumentException_1 = require("../core/IllegalArgumentException");
  8. var IllegalStateException_1 = require("../core/IllegalStateException");
  9. /**
  10. * @deprecated Moving to @zxing/browser
  11. */
  12. var BrowserSvgCodeWriter = /** @class */ (function () {
  13. /**
  14. * Constructs. 😉
  15. */
  16. function BrowserSvgCodeWriter(containerElement) {
  17. if (typeof containerElement === 'string') {
  18. this.containerElement = document.getElementById(containerElement);
  19. }
  20. else {
  21. this.containerElement = containerElement;
  22. }
  23. }
  24. /**
  25. * Writes the QR code to a SVG and renders it in the container.
  26. */
  27. BrowserSvgCodeWriter.prototype.write = function (contents, width, height, hints) {
  28. if (hints === void 0) { hints = null; }
  29. if (contents.length === 0) {
  30. throw new IllegalArgumentException_1.default('Found empty contents');
  31. }
  32. if (width < 0 || height < 0) {
  33. throw new IllegalArgumentException_1.default('Requested dimensions are too small: ' + width + 'x' + height);
  34. }
  35. var quietZone = hints && hints.get(EncodeHintType_1.default.MARGIN) !== undefined
  36. ? Number.parseInt(hints.get(EncodeHintType_1.default.MARGIN).toString(), 10)
  37. : BrowserSvgCodeWriter.QUIET_ZONE_SIZE;
  38. var code = this.encode(hints, contents);
  39. return this.renderResult(code, width, height, quietZone);
  40. };
  41. /**
  42. * Encodes the content to a Barcode type.
  43. */
  44. BrowserSvgCodeWriter.prototype.encode = function (hints, contents) {
  45. var errorCorrectionLevel = ErrorCorrectionLevel_1.default.L;
  46. if (hints && hints.get(EncodeHintType_1.default.ERROR_CORRECTION) !== undefined) {
  47. errorCorrectionLevel = ErrorCorrectionLevel_1.default.fromString(hints.get(EncodeHintType_1.default.ERROR_CORRECTION).toString());
  48. }
  49. var code = Encoder_1.default.encode(contents, errorCorrectionLevel, hints);
  50. return code;
  51. };
  52. /**
  53. * Renders the SVG in the container.
  54. *
  55. * @note the input matrix uses 0 == white, 1 == black. The output matrix uses 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
  56. */
  57. BrowserSvgCodeWriter.prototype.renderResult = function (code, width /*int*/, height /*int*/, quietZone /*int*/) {
  58. // if (this.format && format != this.format) {
  59. // throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format)
  60. // }
  61. var input = code.getMatrix();
  62. if (input === null) {
  63. throw new IllegalStateException_1.default();
  64. }
  65. var inputWidth = input.getWidth();
  66. var inputHeight = input.getHeight();
  67. var qrWidth = inputWidth + (quietZone * 2);
  68. var qrHeight = inputHeight + (quietZone * 2);
  69. var outputWidth = Math.max(width, qrWidth);
  70. var outputHeight = Math.max(height, qrHeight);
  71. var multiple = Math.min(Math.floor(outputWidth / qrWidth), Math.floor(outputHeight / qrHeight));
  72. // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  73. // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  74. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  75. // handle all the padding from 100x100 (the actual QR) up to 200x160.
  76. var leftPadding = Math.floor((outputWidth - (inputWidth * multiple)) / 2);
  77. var topPadding = Math.floor((outputHeight - (inputHeight * multiple)) / 2);
  78. var svgElement = this.createSVGElement(outputWidth, outputHeight);
  79. var placeholder = this.createSvgPathPlaceholderElement(width, height);
  80. svgElement.append(placeholder);
  81. this.containerElement.appendChild(svgElement);
  82. // 2D loop
  83. for (var inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
  84. // Write the contents of this row of the barcode
  85. for (var inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
  86. if (input.get(inputX, inputY) === 1) {
  87. var svgRectElement = this.createSvgRectElement(outputX, outputY, multiple, multiple);
  88. svgElement.appendChild(svgRectElement);
  89. }
  90. }
  91. }
  92. return svgElement;
  93. };
  94. /**
  95. * Creates a SVG element.
  96. */
  97. BrowserSvgCodeWriter.prototype.createSVGElement = function (w, h) {
  98. var el = document.createElementNS(BrowserSvgCodeWriter.SVG_NS, 'svg');
  99. el.setAttributeNS(null, 'width', h.toString());
  100. el.setAttributeNS(null, 'height', w.toString());
  101. return el;
  102. };
  103. /**
  104. * Creates a SVG rect.
  105. */
  106. BrowserSvgCodeWriter.prototype.createSvgPathPlaceholderElement = function (w, h) {
  107. var el = document.createElementNS(BrowserSvgCodeWriter.SVG_NS, 'path');
  108. el.setAttributeNS(null, 'd', "M0 0h" + w + "v" + h + "H0z");
  109. el.setAttributeNS(null, 'fill', 'none');
  110. return el;
  111. };
  112. /**
  113. * Creates a SVG rect.
  114. */
  115. BrowserSvgCodeWriter.prototype.createSvgRectElement = function (x, y, w, h) {
  116. var el = document.createElementNS(BrowserSvgCodeWriter.SVG_NS, 'rect');
  117. el.setAttributeNS(null, 'x', x.toString());
  118. el.setAttributeNS(null, 'y', y.toString());
  119. el.setAttributeNS(null, 'height', w.toString());
  120. el.setAttributeNS(null, 'width', h.toString());
  121. el.setAttributeNS(null, 'fill', '#000000');
  122. return el;
  123. };
  124. /**
  125. * Default quiet zone in pixels.
  126. */
  127. BrowserSvgCodeWriter.QUIET_ZONE_SIZE = 4;
  128. /**
  129. * SVG markup NameSpace
  130. */
  131. BrowserSvgCodeWriter.SVG_NS = 'http://www.w3.org/2000/svg';
  132. return BrowserSvgCodeWriter;
  133. }());
  134. exports.BrowserSvgCodeWriter = BrowserSvgCodeWriter;