/*\ title: $:/plugins/tiddlywiki/qrcode/makeqr.js type: application/javascript module-type: macro Macro to convert a string into a QR Code \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; /* Information about this macro */ var qrcode = require("$:/plugins/tiddlywiki/qrcode/qrcode.js"); var QRCODE_GENERATION_ERROR_PREFIX = '', QRCODE_GENERATION_ERROR_SUFFIX = ''; exports.name = "makeqr"; exports.params = [ {name: "text"}, {name: "size"}, {name: "errorCorrectLevel"}, {name: "fallback"} ]; /* Run the macro */ exports.run = function(text,size,errorCorrectLevel,fallback) { var result; try { result = generateQrCode(text,{size: size, errorCorrectLevel: errorCorrectLevel}); } catch (ex) { console.log("makeqr error: " + ex); result = fallback || ("data:image/svg+xml," + encodeURI(QRCODE_GENERATION_ERROR_PREFIX + ex + QRCODE_GENERATION_ERROR_SUFFIX)); } return result || ""; }; function generateQrCode(text,options) { options = options || {}; var typeNumber = options.typeNumber || 4, errorCorrectLevel = options.errorCorrectLevel || "M", size = options.size || 500, qr; try { qr = qrcode(typeNumber,errorCorrectLevel); qr.addData(text); qr.make(); } catch (e) { if(typeNumber >= 40) { throw new Error("Text too long to encode"); } else { return generateQrCode(text, { size: size, errorCorrectLevel: errorCorrectLevel, typeNumber: typeNumber + 1 }); } } var cellsize = parseInt(size / qr.getModuleCount()), margin = parseInt((size - qr.getModuleCount() * cellsize) / 2); return qr.createImgTag(cellsize, margin, size); } })();