"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _createClass=function(){function t(t,e){for(var i=0;i<e.length;i++){var r=e[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,i,r){return i&&t(e.prototype,i),r&&t(e,r),e}}(),_merge=require("../help/merge.js"),_merge2=_interopRequireDefault(_merge),_shared=require("./shared.js");function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}var svgns="http://www.w3.org/2000/svg",SVGRenderer=function(){function t(e,i,r){_classCallCheck(this,t),this.svg=e,this.encodings=i,this.options=r,this.document=r.xmlDocument||document}return _createClass(t,[{key:"render",value:function(){var t=this.options.marginLeft;this.prepareSVG();for(var e=0;e<this.encodings.length;e++){var i=this.encodings[e],r=(0,_merge2.default)(this.options,i.options),s=this.createGroup(t,r.marginTop,this.svg);this.setGroupOptions(s,r),this.drawSvgBarcode(s,r,i),this.drawSVGText(s,r,i),t+=i.width}}},{key:"prepareSVG",value:function(){for(;this.svg.firstChild;)this.svg.removeChild(this.svg.firstChild);(0,_shared.calculateEncodingAttributes)(this.encodings,this.options);var t=(0,_shared.getTotalWidthOfEncodings)(this.encodings),e=(0,_shared.getMaximumHeightOfEncodings)(this.encodings),i=t+this.options.marginLeft+this.options.marginRight;this.setSvgAttributes(i,e),this.options.background&&this.drawRect(0,0,i,e,this.svg).setAttribute("style","fill:"+this.options.background+";")}},{key:"drawSvgBarcode",value:function(t,e,i){var r,s=i.data;r="top"==e.textPosition?e.fontSize+e.textMargin:0;for(var n=0,o=0,a=0;a<s.length;a++)o=a*e.width+i.barcodePadding,"1"===s[a]?n++:n>0&&(this.drawRect(o-e.width*n,r,e.width*n,e.height,t),n=0);n>0&&this.drawRect(o-e.width*(n-1),r,e.width*n,e.height,t)}},{key:"drawSVGText",value:function(t,e,i){var r,s,n=this.document.createElementNS(svgns,"text");e.displayValue&&(n.setAttribute("style","font:"+e.fontOptions+" "+e.fontSize+"px "+e.font),s="top"==e.textPosition?e.fontSize-e.textMargin:e.height+e.textMargin+e.fontSize,"left"==e.textAlign||i.barcodePadding>0?(r=0,n.setAttribute("text-anchor","start")):"right"==e.textAlign?(r=i.width-1,n.setAttribute("text-anchor","end")):(r=i.width/2,n.setAttribute("text-anchor","middle")),n.setAttribute("x",r),n.setAttribute("y",s),n.appendChild(this.document.createTextNode(i.text)),t.appendChild(n))}},{key:"setSvgAttributes",value:function(t,e){var i=this.svg;i.setAttribute("width",t+"px"),i.setAttribute("height",e+"px"),i.setAttribute("x","0px"),i.setAttribute("y","0px"),i.setAttribute("viewBox","0 0 "+t+" "+e),i.setAttribute("xmlns",svgns),i.setAttribute("version","1.1"),i.setAttribute("style","transform: translate(0,0)")}},{key:"createGroup",value:function(t,e,i){var r=this.document.createElementNS(svgns,"g");return r.setAttribute("transform","translate("+t+", "+e+")"),i.appendChild(r),r}},{key:"setGroupOptions",value:function(t,e){t.setAttribute("style","fill:"+e.lineColor+";")}},{key:"drawRect",value:function(t,e,i,r,s){var n=this.document.createElementNS(svgns,"rect");return n.setAttribute("x",t),n.setAttribute("y",e),n.setAttribute("width",i),n.setAttribute("height",r),s.appendChild(n),n}}]),t}();exports.default=SVGRenderer;