[SignalingServer] Optimize dependent modules
[platform/framework/web/wrtjs.git] / device_home / node_modules / qrcode / lib / renderer / svg-tag.js
1 var Utils = require('./utils')
2
3 function getColorAttrib (color, attrib) {
4   var alpha = color.a / 255
5   var str = attrib + '="' + color.hex + '"'
6
7   return alpha < 1
8     ? str + ' ' + attrib + '-opacity="' + alpha.toFixed(2).slice(1) + '"'
9     : str
10 }
11
12 function svgCmd (cmd, x, y) {
13   var str = cmd + x
14   if (typeof y !== 'undefined') str += ' ' + y
15
16   return str
17 }
18
19 function qrToPath (data, size, margin) {
20   var path = ''
21   var moveBy = 0
22   var newRow = false
23   var lineLength = 0
24
25   for (var i = 0; i < data.length; i++) {
26     var col = Math.floor(i % size)
27     var row = Math.floor(i / size)
28
29     if (!col && !newRow) newRow = true
30
31     if (data[i]) {
32       lineLength++
33
34       if (!(i > 0 && col > 0 && data[i - 1])) {
35         path += newRow
36           ? svgCmd('M', col + margin, 0.5 + row + margin)
37           : svgCmd('m', moveBy, 0)
38
39         moveBy = 0
40         newRow = false
41       }
42
43       if (!(col + 1 < size && data[i + 1])) {
44         path += svgCmd('h', lineLength)
45         lineLength = 0
46       }
47     } else {
48       moveBy++
49     }
50   }
51
52   return path
53 }
54
55 exports.render = function render (qrData, options, cb) {
56   var opts = Utils.getOptions(options)
57   var size = qrData.modules.size
58   var data = qrData.modules.data
59   var qrcodesize = size + opts.margin * 2
60
61   var bg = !opts.color.light.a
62     ? ''
63     : '<path ' + getColorAttrib(opts.color.light, 'fill') +
64       ' d="M0 0h' + qrcodesize + 'v' + qrcodesize + 'H0z"/>'
65
66   var path =
67     '<path ' + getColorAttrib(opts.color.dark, 'stroke') +
68     ' d="' + qrToPath(data, size, opts.margin) + '"/>'
69
70   var viewBox = 'viewBox="' + '0 0 ' + qrcodesize + ' ' + qrcodesize + '"'
71
72   var width = !opts.width ? '' : 'width="' + opts.width + '" height="' + opts.width + '" '
73
74   var svgTag = '<svg xmlns="http://www.w3.org/2000/svg" ' + width + viewBox + ' shape-rendering="crispEdges">' + bg + path + '</svg>\n'
75
76   if (typeof cb === 'function') {
77     cb(null, svgTag)
78   }
79
80   return svgTag
81 }