BrowserSvgCodeWriter.js 5.8 KB

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