Simplified toSvgString() in JavaScript code.

This commit is contained in:
Project Nayuki 2018-06-23 16:18:47 +00:00
parent 1a5207ece3
commit 236efbeccd
1 changed files with 10 additions and 17 deletions

View File

@ -169,27 +169,20 @@ var qrcodegen = new function() {
this.toSvgString = function(border) {
if (border < 0)
throw "Border must be non-negative";
var result = '<?xml version="1.0" encoding="UTF-8"?>\n';
result += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n';
result += '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 ' +
(size + border * 2) + ' ' + (size + border * 2) + '" stroke="none">\n';
result += '\t<rect width="100%" height="100%" fill="#FFFFFF"/>\n';
result += '\t<path d="';
var head = true;
var parts = [];
for (var y = -border; y < size + border; y++) {
for (var x = -border; x < size + border; x++) {
if (this.getModule(x, y)) {
if (head)
head = false;
else
result += " ";
result += "M" + (x + border) + "," + (y + border) + "h1v1h-1z";
}
if (this.getModule(x, y))
parts.push("M" + (x + border) + "," + (y + border) + "h1v1h-1z");
}
}
result += '" fill="#000000"/>\n';
result += '</svg>\n';
return result;
return '<?xml version="1.0" encoding="UTF-8"?>\n' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n' +
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 ' +
(size + border * 2) + ' ' + (size + border * 2) + '" stroke="none">\n' +
'\t<rect width="100%" height="100%" fill="#FFFFFF"/>\n' +
'\t<path d="' + parts.join(" ") + '" fill="#000000"/>\n' +
'</svg>\n';
};