svg.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6. var _merge = require("../help/merge.js");
  7. var _merge2 = _interopRequireDefault(_merge);
  8. var _shared = require("./shared.js");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  11. var svgns = "http://www.w3.org/2000/svg";
  12. var SVGRenderer = function () {
  13. function SVGRenderer(svg, encodings, options) {
  14. _classCallCheck(this, SVGRenderer);
  15. this.svg = svg;
  16. this.encodings = encodings;
  17. this.options = options;
  18. this.document = options.xmlDocument || document;
  19. }
  20. _createClass(SVGRenderer, [{
  21. key: "render",
  22. value: function render() {
  23. var currentX = this.options.marginLeft;
  24. this.prepareSVG();
  25. for (var i = 0; i < this.encodings.length; i++) {
  26. var encoding = this.encodings[i];
  27. var encodingOptions = (0, _merge2.default)(this.options, encoding.options);
  28. var group = this.createGroup(currentX, encodingOptions.marginTop, this.svg);
  29. this.setGroupOptions(group, encodingOptions);
  30. this.drawSvgBarcode(group, encodingOptions, encoding);
  31. this.drawSVGText(group, encodingOptions, encoding);
  32. currentX += encoding.width;
  33. }
  34. }
  35. }, {
  36. key: "prepareSVG",
  37. value: function prepareSVG() {
  38. // Clear the SVG
  39. while (this.svg.firstChild) {
  40. this.svg.removeChild(this.svg.firstChild);
  41. }
  42. (0, _shared.calculateEncodingAttributes)(this.encodings, this.options);
  43. var totalWidth = (0, _shared.getTotalWidthOfEncodings)(this.encodings);
  44. var maxHeight = (0, _shared.getMaximumHeightOfEncodings)(this.encodings);
  45. var width = totalWidth + this.options.marginLeft + this.options.marginRight;
  46. this.setSvgAttributes(width, maxHeight);
  47. if (this.options.background) {
  48. this.drawRect(0, 0, width, maxHeight, this.svg).setAttribute("style", "fill:" + this.options.background + ";");
  49. }
  50. }
  51. }, {
  52. key: "drawSvgBarcode",
  53. value: function drawSvgBarcode(parent, options, encoding) {
  54. var binary = encoding.data;
  55. // Creates the barcode out of the encoded binary
  56. var yFrom;
  57. if (options.textPosition == "top") {
  58. yFrom = options.fontSize + options.textMargin;
  59. } else {
  60. yFrom = 0;
  61. }
  62. var barWidth = 0;
  63. var x = 0;
  64. for (var b = 0; b < binary.length; b++) {
  65. x = b * options.width + encoding.barcodePadding;
  66. if (binary[b] === "1") {
  67. barWidth++;
  68. } else if (barWidth > 0) {
  69. this.drawRect(x - options.width * barWidth, yFrom, options.width * barWidth, options.height, parent);
  70. barWidth = 0;
  71. }
  72. }
  73. // Last draw is needed since the barcode ends with 1
  74. if (barWidth > 0) {
  75. this.drawRect(x - options.width * (barWidth - 1), yFrom, options.width * barWidth, options.height, parent);
  76. }
  77. }
  78. }, {
  79. key: "drawSVGText",
  80. value: function drawSVGText(parent, options, encoding) {
  81. var textElem = this.document.createElementNS(svgns, 'text');
  82. // Draw the text if displayValue is set
  83. if (options.displayValue) {
  84. var x, y;
  85. textElem.setAttribute("style", "font:" + options.fontOptions + " " + options.fontSize + "px " + options.font);
  86. if (options.textPosition == "top") {
  87. y = options.fontSize - options.textMargin;
  88. } else {
  89. y = options.height + options.textMargin + options.fontSize;
  90. }
  91. // Draw the text in the correct X depending on the textAlign option
  92. if (options.textAlign == "left" || encoding.barcodePadding > 0) {
  93. x = 0;
  94. textElem.setAttribute("text-anchor", "start");
  95. } else if (options.textAlign == "right") {
  96. x = encoding.width - 1;
  97. textElem.setAttribute("text-anchor", "end");
  98. }
  99. // In all other cases, center the text
  100. else {
  101. x = encoding.width / 2;
  102. textElem.setAttribute("text-anchor", "middle");
  103. }
  104. textElem.setAttribute("x", x);
  105. textElem.setAttribute("y", y);
  106. textElem.appendChild(this.document.createTextNode(encoding.text));
  107. parent.appendChild(textElem);
  108. }
  109. }
  110. }, {
  111. key: "setSvgAttributes",
  112. value: function setSvgAttributes(width, height) {
  113. var svg = this.svg;
  114. svg.setAttribute("width", width + "px");
  115. svg.setAttribute("height", height + "px");
  116. svg.setAttribute("x", "0px");
  117. svg.setAttribute("y", "0px");
  118. svg.setAttribute("viewBox", "0 0 " + width + " " + height);
  119. svg.setAttribute("xmlns", svgns);
  120. svg.setAttribute("version", "1.1");
  121. svg.setAttribute("style", "transform: translate(0,0)");
  122. }
  123. }, {
  124. key: "createGroup",
  125. value: function createGroup(x, y, parent) {
  126. var group = this.document.createElementNS(svgns, 'g');
  127. group.setAttribute("transform", "translate(" + x + ", " + y + ")");
  128. parent.appendChild(group);
  129. return group;
  130. }
  131. }, {
  132. key: "setGroupOptions",
  133. value: function setGroupOptions(group, options) {
  134. group.setAttribute("style", "fill:" + options.lineColor + ";");
  135. }
  136. }, {
  137. key: "drawRect",
  138. value: function drawRect(x, y, width, height, parent) {
  139. var rect = this.document.createElementNS(svgns, 'rect');
  140. rect.setAttribute("x", x);
  141. rect.setAttribute("y", y);
  142. rect.setAttribute("width", width);
  143. rect.setAttribute("height", height);
  144. parent.appendChild(rect);
  145. return rect;
  146. }
  147. }]);
  148. return SVGRenderer;
  149. }();
  150. exports.default = SVGRenderer;