node-qrcode-lite/lib/renderer/svg.js
Vincenzo Greco 77064f5e5e Refactor/core ()
* Split core lib into multiple files

* Refactor data encoding methods

* Refactor data masking process

* Improve qr code generation process

* Increase minimum required node version to 0.10

* Add linter

* Add tests and tests coverage

* Update travis config to fix compilation issues

* Add examples folder

* Add missing license tag in package.json

* Update build script and add sourcemap support

* Publish only strictly needed files on npm

* Update readme
2016-12-16 23:45:08 +01:00

44 lines
1.6 KiB
JavaScript

exports.renderBits = function renderBits (bits, width, options) {
if (typeof bits === 'undefined' || !(bits instanceof Array)) {
throw new Error('"bits" must be a valid Array')
}
if (typeof bits === 'undefined' || isNaN(width)) {
throw new Error('"width" must be a valid number')
}
var dotsize = options.scale || 4
var margin = options.margin || 20
var qrcodesize = width * dotsize + margin * 2
var lightColor = options.lightColor || '#ffffff'
var darkColor = options.darkColor || '#000000'
var xmlStr = '<?xml version="1.0" encoding="utf-8"?>\n'
xmlStr += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'
xmlStr += '<svg version="1.1" baseProfile="full"'
xmlStr += ' width="' + qrcodesize + '" height="' + qrcodesize + '"'
xmlStr += ' viewBox="0 0 ' + qrcodesize + ' ' + qrcodesize + '"'
xmlStr += ' xmlns="http://www.w3.org/2000/svg"'
xmlStr += ' xmlns:xlink="http://www.w3.org/1999/xlink"'
xmlStr += ' xmlns:ev="http://www.w3.org/2001/xml-events">\n'
xmlStr += '<rect x="0" y="0" width="' + qrcodesize + '" height="' + qrcodesize + '" fill="' + lightColor + '" />\n'
xmlStr += '<defs><rect id="p" width="' + dotsize + '" height="' + dotsize + '" /></defs>\n'
xmlStr += '<g fill="' + darkColor + '">\n'
xmlStr = bits.reduce(function (xml, bit, index) {
if (!bit) return xml
var x = margin + (index % width) * dotsize
var y = margin + Math.floor(index / width) * dotsize
xml += '<use x="' + x + '" y="' + y + '" xlink:href="#p" />\n'
return xml
}, xmlStr)
xmlStr += '</g>\n'
xmlStr += '</svg>'
return xmlStr
}