qrmanual
Version:
Implementação de QR Code v2-L (25×25, correção L) em TypeScript
36 lines (35 loc) • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderSVG = renderSVG;
exports.generateQRCodeSVG = generateQRCodeSVG;
const matrix_1 = require("./matrix");
function renderSVG(matrix, options = {}) {
var _a, _b, _c, _d;
const n = matrix.length;
const scale = Math.floor((_a = options.scale) !== null && _a !== void 0 ? _a : 10);
const margin = Math.floor((_b = options.margin) !== null && _b !== void 0 ? _b : 4);
const bg = (_c = options.backgroundColor) !== null && _c !== void 0 ? _c : '#fff';
const fg = (_d = options.foregroundColor) !== null && _d !== void 0 ? _d : '#000';
const total = n + margin * 2;
const px = total * scale;
let svg = `<?xml version="1.0" encoding="UTF-8"?>`;
svg += `<svg xmlns="http://www.w3.org/2000/svg"`;
svg += ` width="${px}" height="${px}"`;
svg += ` shape-rendering="crispEdges">`;
svg += `<rect width="${px}" height="${px}" fill="${bg}" />`;
for (let y = 0; y < n; y++) {
for (let x = 0; x < n; x++) {
if (matrix[y][x] === 1) {
svg += `<rect x="${(x + margin) * scale}" y="${(y + margin) * scale}"`;
svg += ` width="${scale}" height="${scale}" fill="${fg}" />`;
}
}
}
svg += `</svg>`;
return svg;
}
function generateQRCodeSVG(payload, options) {
const codewords = require('./encoder').encodePayloadToCodewords(payload);
const matrix = (0, matrix_1.buildMatrix)(codewords);
return renderSVG(matrix, options);
}