Skip to content

Commit

Permalink
Restore utf8 support from davidshimjs/qrcodejs#6.
Browse files Browse the repository at this point in the history
  • Loading branch information
leMaik committed Jun 19, 2022
1 parent f2e6a18 commit d36f0bc
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions lib/qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,48 @@
function QR8bitByte(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;
return this.parsedData.length;
},

write: function (buffer) {
for (var i = 0; i < this.data.length; i++) {
// not JIS ...
buffer.put(this.data.charCodeAt(i), 8);
for (var i = 0, l = this.parsedData.length; i < l; i++) {
buffer.put(this.parsedData[i], 8);
}
},
};
Expand Down

0 comments on commit d36f0bc

Please sign in to comment.