Skip to content

Commit

Permalink
a little change of multibyte support. (issue#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuhikoarase committed Mar 29, 2017
1 parent 1067b0a commit a7ec489
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 48 deletions.
2 changes: 1 addition & 1 deletion js/package.json
@@ -1,6 +1,6 @@
{
"name": "qrcode-generator",
"version": "1.1.0",
"version": "1.2.0",
"description": "QR Code Generator implementation in JavaScript.",
"author": "Kazuhiko Arase",
"main": "qrcode.js",
Expand Down
1 change: 1 addition & 0 deletions js/qrcode.d.ts
Expand Up @@ -18,6 +18,7 @@
interface QRCodeFactory {
(typeNumber: number, errorCorrectionLevel: string) : QRCode;
stringToBytes(s: string) : number[];
stringToBytesFuncs : { [encoding : string] : (s: string) => number[] };
createStringToBytes(unicodeData: string, numChars: number) :
(s : string) => number[];
}
Expand Down
63 changes: 56 additions & 7 deletions js/qrcode.js

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions js/qrcode_SJIS.js

Large diffs are not rendered by default.

35 changes: 1 addition & 34 deletions js/qrcode_UTF8.js
Expand Up @@ -21,39 +21,6 @@
// overwrite qrcode.stringToBytes
//---------------------------------------------------------------------

qrcode.stringToBytes = function(s) {
// http://stackoverflow.com/questions/18729405/how-to-convert-utf8-string-to-byte-array
function toUTF8Array(str) {
var utf8 = [];
for (var i=0; i < str.length; i++) {
var charcode = str.charCodeAt(i);
if (charcode < 0x80) utf8.push(charcode);
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6),
0x80 | (charcode & 0x3f));
}
else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(0xe0 | (charcode >> 12),
0x80 | ((charcode>>6) & 0x3f),
0x80 | (charcode & 0x3f));
}
// surrogate pair
else {
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode = 0x10000 + (((charcode & 0x3ff)<<10)
| (str.charCodeAt(i) & 0x3ff));
utf8.push(0xf0 | (charcode >>18),
0x80 | ((charcode>>12) & 0x3f),
0x80 | ((charcode>>6) & 0x3f),
0x80 | (charcode & 0x3f));
}
}
return utf8;
}
return toUTF8Array(s);
};
qrcode.stringToBytes = qrcode.stringToBytesFuncs['UTF-8'];

}(qrcode);
7 changes: 7 additions & 0 deletions js/sample.html
Expand Up @@ -4,6 +4,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=Utf-8">
<script type="text/javascript" src="qrcode.js"></script>
<!-- SJIS Support (optional) -->
<script type="text/javascript" src="qrcode_SJIS.js"></script>
<!--
<script type="text/javascript" src="qrcode_SJIS.js"></script>
-->
Expand Down Expand Up @@ -32,6 +33,12 @@
<option value="Byte" selected>Byte</option>
<option value="Kanji">Kanji</option>
</select>
<span>Mutibyte:</span>
<select name="mb">
<option value="default">None</option>
<option value="SJIS">SJIS</option>
<option value="UTF-8" selected>UTF-8</option>
</select>
<br/>
<textarea name="msg" rows="10" cols="40">here comes qr!</textarea>
<br/>
Expand Down
8 changes: 6 additions & 2 deletions js/sample.js
Expand Up @@ -3,7 +3,10 @@ var draw_qrcode = function(text, typeNumber, errorCorrectionLevel) {
document.write(create_qrcode(text, typeNumber, errorCorrectionLevel) );
};

var create_qrcode = function(text, typeNumber, errorCorrectionLevel, mode) {
var create_qrcode = function(text, typeNumber, errorCorrectionLevel, mode,
mb) {

qrcode.stringToBytes = qrcode.stringToBytesFuncs[mb];

var qr = qrcode(typeNumber || 4, errorCorrectionLevel || 'M');
qr.addData(text, mode);
Expand All @@ -21,5 +24,6 @@ var update_qrcode = function() {
var t = form.elements['t'].value;
var e = form.elements['e'].value;
var m = form.elements['m'].value;
document.getElementById('qr').innerHTML = create_qrcode(text, t, e, m);
var mb = form.elements['mb'].value;
document.getElementById('qr').innerHTML = create_qrcode(text, t, e, m, mb);
};

0 comments on commit a7ec489

Please sign in to comment.