40f17010b466a9597c6d92abe8abb6f00ecbacefe27e4dfd67f6edcb6696fc7a.json raw

   1  {"ast":null,"code":"const Utils = require('./utils');\nfunction getColorAttrib(color, attrib) {\n  const alpha = color.a / 255;\n  const str = attrib + '=\"' + color.hex + '\"';\n  return alpha < 1 ? str + ' ' + attrib + '-opacity=\"' + alpha.toFixed(2).slice(1) + '\"' : str;\n}\nfunction svgCmd(cmd, x, y) {\n  let str = cmd + x;\n  if (typeof y !== 'undefined') str += ' ' + y;\n  return str;\n}\nfunction qrToPath(data, size, margin) {\n  let path = '';\n  let moveBy = 0;\n  let newRow = false;\n  let lineLength = 0;\n  for (let i = 0; i < data.length; i++) {\n    const col = Math.floor(i % size);\n    const row = Math.floor(i / size);\n    if (!col && !newRow) newRow = true;\n    if (data[i]) {\n      lineLength++;\n      if (!(i > 0 && col > 0 && data[i - 1])) {\n        path += newRow ? svgCmd('M', col + margin, 0.5 + row + margin) : svgCmd('m', moveBy, 0);\n        moveBy = 0;\n        newRow = false;\n      }\n      if (!(col + 1 < size && data[i + 1])) {\n        path += svgCmd('h', lineLength);\n        lineLength = 0;\n      }\n    } else {\n      moveBy++;\n    }\n  }\n  return path;\n}\nexports.render = function render(qrData, options, cb) {\n  const opts = Utils.getOptions(options);\n  const size = qrData.modules.size;\n  const data = qrData.modules.data;\n  const qrcodesize = size + opts.margin * 2;\n  const bg = !opts.color.light.a ? '' : '<path ' + getColorAttrib(opts.color.light, 'fill') + ' d=\"M0 0h' + qrcodesize + 'v' + qrcodesize + 'H0z\"/>';\n  const path = '<path ' + getColorAttrib(opts.color.dark, 'stroke') + ' d=\"' + qrToPath(data, size, opts.margin) + '\"/>';\n  const viewBox = 'viewBox=\"' + '0 0 ' + qrcodesize + ' ' + qrcodesize + '\"';\n  const width = !opts.width ? '' : 'width=\"' + opts.width + '\" height=\"' + opts.width + '\" ';\n  const svgTag = '<svg xmlns=\"http://www.w3.org/2000/svg\" ' + width + viewBox + ' shape-rendering=\"crispEdges\">' + bg + path + '</svg>\\n';\n  if (typeof cb === 'function') {\n    cb(null, svgTag);\n  }\n  return svgTag;\n};","map":null,"metadata":{},"sourceType":"script","externalDependencies":[]}