From c82f0e72f8534bbed1fdcccd9941d304084d782b Mon Sep 17 00:00:00 2001 From: CPunisher <1343316114@qq.com> Date: Fri, 15 May 2020 20:10:30 +0800 Subject: [PATCH] Unicode supporting --- src/utils/qrcode.js | 62 +++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/src/utils/qrcode.js b/src/utils/qrcode.js index 15a5356..a98102a 100644 --- a/src/utils/qrcode.js +++ b/src/utils/qrcode.js @@ -13,29 +13,53 @@ // http://www.denso-wave.com/qrcode/faqpatent-e.html // //--------------------------------------------------------------------- - -//--------------------------------------------------------------------- -// QR8bitByte -//--------------------------------------------------------------------- -/* eslint-disable */ - function QR8bitByte(data) { - this.mode = QRMode.MODE_8BIT_BYTE; - this.data = data; + this.mode = QRMode.MODE_8BIT_BYTE; + this.data = data; + this.parsedData = []; + + // Added to support UTF-8 Characters + for (var i = 0, l = this.data.length; i < l; i++) { + var byteArray = []; + var code = this.data.charCodeAt(i); + + if (code > 0x10000) { + byteArray[0] = 0xF0 | ((code & 0x1C0000) >>> 18); + byteArray[1] = 0x80 | ((code & 0x3F000) >>> 12); + byteArray[2] = 0x80 | ((code & 0xFC0) >>> 6); + byteArray[3] = 0x80 | (code & 0x3F); + } else if (code > 0x800) { + byteArray[0] = 0xE0 | ((code & 0xF000) >>> 12); + byteArray[1] = 0x80 | ((code & 0xFC0) >>> 6); + byteArray[2] = 0x80 | (code & 0x3F); + } else if (code > 0x80) { + byteArray[0] = 0xC0 | ((code & 0x7C0) >>> 6); + byteArray[1] = 0x80 | (code & 0x3F); + } else { + byteArray[0] = code; + } + + this.parsedData.push(byteArray); + } + + this.parsedData = Array.prototype.concat.apply([], this.parsedData); + + if (this.parsedData.length != this.data.length) { + this.parsedData.unshift(191); + this.parsedData.unshift(187); + this.parsedData.unshift(239); + } } QR8bitByte.prototype = { - - getLength : function(buffer) { - return this.data.length; - }, - - write : function(buffer) { - for (let i = 0; i < this.data.length; i++) { - // not JIS ... - buffer.put(this.data.charCodeAt(i), 8); - } - } + getLength: function (buffer) { + return this.parsedData.length; + }, + write: function (buffer) { + for (var i = 0, l = this.parsedData.length; i < l; i++) { + buffer.put(this.parsedData[i], 8); + } + } }; //---------------------------------------------------------------------