Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shorten SVG inline syntax #445

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 54 additions & 60 deletions src/renderers/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ class SVGRenderer{
var currentX = this.options.marginLeft;

this.prepareSVG();

// Create Background IF not (SVG default) `transparent`
if(!this.options.background.match(/^(transparent|#[0-9a-f]{3}0|#[0-9a-f]{6}0{2})$/im)){
this.drawRect(0, 0, "100%", "100%", this.svg).setAttribute("fill", this.options.background);
}

for(let i = 0; i < this.encodings.length; i++){
var encoding = this.encodings[i];
var encodingOptions = merge(this.options, encoding.options);

var group = this.createGroup(currentX, encodingOptions.marginTop, this.svg);

this.setGroupOptions(group, encodingOptions);
// Set color IF not (SVG default) `black`
if(!encodingOptions.lineColor.match(/^(black|#0{3}f?|#0{6}(?:f{2})?)$/im)){
group.setAttribute("fill", encodingOptions.lineColor);
}

this.drawSvgBarcode(group, encodingOptions, encoding);
this.drawSVGText(group, encodingOptions, encoding);
Expand All @@ -32,22 +41,14 @@ class SVGRenderer{

prepareSVG(){
// Clear the SVG
while (this.svg.firstChild) {
this.svg.removeChild(this.svg.firstChild);
}
this.svg.replaceChildren();

calculateEncodingAttributes(this.encodings, this.options);
var totalWidth = getTotalWidthOfEncodings(this.encodings);
var maxHeight = getMaximumHeightOfEncodings(this.encodings);

var width = totalWidth + this.options.marginLeft + this.options.marginRight;
this.setSvgAttributes(width, maxHeight);

if(this.options.background){
this.drawRect(0, 0, width, maxHeight, this.svg).setAttribute(
"style", "fill:" + this.options.background + ";"
);
}
}

drawSvgBarcode(parent, options, encoding){
Expand Down Expand Up @@ -83,82 +84,75 @@ class SVGRenderer{
}

drawSVGText(parent, options, encoding){
var textElem = this.document.createElementNS(svgns, 'text');

// Draw the text if displayValue is set
if(options.displayValue){
var x, y;
if(!options.displayValue){
return;
}

textElem.setAttribute("style",
"font:" + options.fontOptions + " " + options.fontSize + "px " + options.font
);
var textElem = this.document.createElementNS(svgns, "text");
var x, y;

if(options.textPosition == "top"){
y = options.fontSize - options.textMargin;
}
else{
y = options.height + options.textMargin + options.fontSize;
}
if(options.textPosition == "top"){
y = options.fontSize - options.textMargin;
}
else{
y = options.height + options.textMargin + options.fontSize;
}

// Draw the text in the correct X depending on the textAlign option
if(options.textAlign == "left" || encoding.barcodePadding > 0){
x = 0;
textElem.setAttribute("text-anchor", "start");
}
else if(options.textAlign == "right"){
x = encoding.width - 1;
textElem.setAttribute("text-anchor", "end");
}
// In all other cases, center the text
else{
x = encoding.width / 2;
textElem.setAttribute("text-anchor", "middle");
}
// Draw the text in the correct X depending on the textAlign option
if(options.textAlign == "left" || encoding.barcodePadding > 0){
x = 0;
textElem.setAttribute("text-anchor", "start");
}
else if(options.textAlign == "right"){
x = encoding.width - 1;
textElem.setAttribute("text-anchor", "end");
}
// In all other cases, center the text
else{
x = encoding.width / 2;
textElem.setAttribute("text-anchor", "middle");
}

textElem.setAttribute("x", x);
textElem.setAttribute("y", y);
textElem.setAttribute("x", x);
textElem.setAttribute("y", y);

textElem.appendChild(this.document.createTextNode(encoding.text));
textElem.setAttribute("style", "font:" +
(options.fontOptions + " " + options.fontSize + "px " + options.font).trim()
);

parent.appendChild(textElem);
}
textElem.appendChild(this.document.createTextNode(encoding.text));

parent.appendChild(textElem);
}


setSvgAttributes(width, height){
var svg = this.svg;
svg.setAttribute("width", width + "px");
svg.setAttribute("height", height + "px");
svg.setAttribute("x", "0px");
svg.setAttribute("y", "0px");
svg.setAttribute("viewBox", "0 0 " + width + " " + height);

svg.setAttribute("xmlns", svgns);
svg.setAttribute("version", "1.1");
svg.setAttribute("viewBox", "0 0 " + width + " " + height);

svg.setAttribute("style", "transform: translate(0,0)");
svg.setAttribute("width", width);
svg.setAttribute("height", height);
}

createGroup(x, y, parent){
var group = this.document.createElementNS(svgns, 'g');
group.setAttribute("transform", "translate(" + x + ", " + y + ")");
var group = this.document.createElementNS(svgns, "g");

group.setAttribute("transform", "translate(" + x + "," + y + ")");

parent.appendChild(group);

return group;
}

setGroupOptions(group, options){
group.setAttribute("style",
"fill:" + options.lineColor + ";"
);
}

drawRect(x, y, width, height, parent){
var rect = this.document.createElementNS(svgns, 'rect');
var rect = this.document.createElementNS(svgns, "rect");

if(x){rect.setAttribute("x", x);}
if(y){rect.setAttribute("y", y);}

rect.setAttribute("x", x);
rect.setAttribute("y", y);
rect.setAttribute("width", width);
rect.setAttribute("height", height);

Expand Down