Skip to content

Commit

Permalink
UTF16 to UTF8
Browse files Browse the repository at this point in the history
1. utf16 to utf8: Convert from UTF-16, allows to encode Hanji, Kanji, emoji, etc.
1. clear build dir.
  • Loading branch information
scopewu committed Apr 14, 2018
1 parent 85ac699 commit e2d2572
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build:es": "cross-env NODE_ENV=es rollup -c",
"build:umd": "cross-env NODE_ENV=development rollup -c",
"build:umd:min": "cross-env NODE_ENV=production rollup -c",
"build": "npm run build:es && npm run build:umd && npm run build:umd:min",
"build": "npm run clean && npm run build:es && npm run build:umd && npm run build:umd:min",
"lint": "eslint --ext .js,.vue src",
"prepublish": "npm run build"
},
Expand Down
8 changes: 6 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const banner =
'\n * © 2017-' + new Date().getFullYear() + ' @scopewu' +
'\n * MIT License.' +
'\n */'
const sourcemap = false
const indent = false

const config = {
input: 'src/index.js',
Expand Down Expand Up @@ -64,7 +66,8 @@ if (env === 'es') {
file: 'dist/qrcode.vue.esm.js',
format: 'es',
name: 'QrcodeVue',
sourcemap: false,
sourcemap,
indent,
banner
}
}
Expand All @@ -74,7 +77,8 @@ if (env === 'development') {
file: 'dist/qrcode.vue.js',
format: 'umd',
name: 'QrcodeVue',
sourcemap: false,
sourcemap,
indent,
banner
}
}
Expand Down
37 changes: 36 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,41 @@ function getBackingStorePixelRatio(ctx) {
)
}

/**
* Encode UTF16 to UTF8.
* See: http://jonisalonen.com/2012/from-utf-16-to-utf-8-in-javascript/
* @param str {string}
* @returns {string}
*/
function toUTF8String(str) {
let utf8Str = ''
for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i)
if (charCode < 0x0080) {
utf8Str += String.fromCharCode(charCode)
} else if (charCode < 0x0800) {
utf8Str += String.fromCharCode(0xc0 | (charCode >> 6))
utf8Str += String.fromCharCode(0x80 | (charCode & 0x3f))
} else if (charCode < 0xd800 || charCode >= 0xe000) {
utf8Str += String.fromCharCode(0xe0 | (charCode >> 12))
utf8Str += String.fromCharCode(0x80 | ((charCode >> 6) & 0x3f))
utf8Str += String.fromCharCode(0x80 | (charCode & 0x3f))
} else {
// surrogate pair
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))
utf8Str += String.fromCharCode(0xf0 | (charCode >> 18))
utf8Str += String.fromCharCode(0x80 | ((charCode >> 12) & 0x3f))
utf8Str += String.fromCharCode(0x80 | ((charCode >> 6) & 0x3f))
utf8Str += String.fromCharCode(0x80 | (charCode & 0x3f))
}
}
return utf8Str
}

const QrcodeVue = {
render(createElement) {
const {className, value, level, background, foreground, size} = this
Expand Down Expand Up @@ -71,7 +106,7 @@ const QrcodeVue = {

// We'll use type===-1 to force QRCode to automatically pick the best type
const qrCode = new QRCode(-1, ErrorCorrectLevel[level])
qrCode.addData(value)
qrCode.addData(toUTF8String(value))
qrCode.make()

const canvas = this.$refs['qrcode-vue']
Expand Down

0 comments on commit e2d2572

Please sign in to comment.