svg.js 4.3 KB

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