Skip to content

Commit

Permalink
Add utf8WithoutBOM option
Browse files Browse the repository at this point in the history
Add `utf8WithoutBOM` option
  • Loading branch information
ushelp committed Nov 29, 2021
1 parent a2dc6e6 commit e2ddd20
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
25 changes: 14 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* NodeJS QRCode generator. Can save image or svg to file, get standard base64 image data url text or get SVG serialized text. Cross-browser QRCode generator for pure javascript. Support Dot style, Logo, Background image, Colorful, Title etc. settings. support binary mode.(Running without DOM on server side)
*
* Version 4.4.0
* Version 4.4.1
*
* @author [ [email protected] ]
*
Expand Down Expand Up @@ -35,7 +35,7 @@ const {
} = jsdom;
const win = new JSDOM().window;

function QR8bitByte(data, binary) {
function QR8bitByte(data, binary, utf8WithoutBOM) {
this.mode = QRMode.MODE_8BIT_BYTE;
this.data = data;
this.parsedData = [];
Expand Down Expand Up @@ -70,7 +70,7 @@ function QR8bitByte(data, binary) {

this.parsedData = Array.prototype.concat.apply([], this.parsedData);

if (this.parsedData.length != this.data.length) {
if (!utf8WithoutBOM && this.parsedData.length != this.data.length) {
this.parsedData.unshift(191);
this.parsedData.unshift(187);
this.parsedData.unshift(239);
Expand Down Expand Up @@ -98,8 +98,8 @@ function QRCodeModel(typeNumber, errorCorrectLevel) {
}

QRCodeModel.prototype = {
addData: function(data, binary) {
var newData = new QR8bitByte(data, binary);
addData: function(data, binary, utf8WithoutBOM) {
var newData = new QR8bitByte(data, binary, utf8WithoutBOM);
this.dataList.push(newData);
this.dataCache = null;
},
Expand Down Expand Up @@ -1461,7 +1461,7 @@ Drawing.prototype.draw = function(oQRCode) {
// console.error(e);
t.reject(e);
}
img.originalSrc=_htOption.logo;
img.originalSrc = _htOption.logo;
img.src = _htOption.logo;
// if (img.complete) {
// img.onload = null;
Expand Down Expand Up @@ -1548,14 +1548,14 @@ Drawing.prototype.makeImage = function() {
if (this._htOption.onRenderingStart) {
this._htOption.onRenderingStart(this._htOption);
}

if (this._htOption.format == 'PNG') {
// dataUrl = this._canvas.toDataURL()
t.resolve(this._canvas.createPNGStream());
} else {
t.resolve(this._canvas.createJPEGStream());
}

}
};

Expand Down Expand Up @@ -1660,7 +1660,10 @@ function QRCode(vOption) {
version: 0, // The symbol versions of QR Code range from Version 1 to Version 40. default 0 means automatically choose the closest version based on the text length.

// ==== binary(hex) data mode
binary: false // Whether it is binary mode, default is text mode.
binary: false, // Whether it is binary mode, default is text mode.

// UTF-8 without BOM
utf8WithoutBOM: true
};
if (typeof vOption === 'string') {
vOption = {
Expand Down Expand Up @@ -1776,7 +1779,7 @@ function QRCode(vOption) {

this._oQRCode = null;
this._oQRCode = new QRCodeModel(_getTypeNumber(this._htOption.text, this._htOption), this._htOption.correctLevel);
this._oQRCode.addData(this._htOption.text, this._htOption.binary);
this._oQRCode.addData(this._htOption.text, this._htOption.binary, this._htOption.utf8WithoutBOM);
this._oQRCode.make();
}

Expand Down Expand Up @@ -1828,7 +1831,7 @@ QRCode.prototype.saveSVG = function(saveOptions) {
// Get Base64 or SVG text
QRCode.prototype._toData = function(drawer, makeType) {
var defOptions = {
makeType: makeType?makeType:'URL'
makeType: makeType ? makeType : 'URL'
}
this._htOption._drawer = drawer;

Expand Down
4 changes: 2 additions & 2 deletions index.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easyqrcodejs-nodejs",
"version": "4.4.0",
"version": "4.4.1",
"description": "NodeJS QRCode generator. Can save image or svg to file, get standard base64 image data url text or get SVG serialized text. Cross-browser QRCode generator for pure javascript. Support Dot style, Logo, Background image, Colorful, Title etc. settings. support binary mode.(Running without DOM on server side)",
"main": "index.min.js",
"scripts": {},
Expand Down
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,13 @@ var qrcode = new QRCode(options);

// ===== Binary(hex) data mode
/*
binary: false // Whether it is binary mode, default is text mode.
binary: false, // Whether it is binary mode, default is text mode.
*/

// ===== UTF-8 without BOM
/*
utf8WithoutBOM: true
*/
}
```

Expand Down Expand Up @@ -333,6 +338,8 @@ var qrcode = new QRCode(options);
| **quality** | N | Number | `0.75` | An object specifying the quality (0 to 1). (**JPGs only**) |
| Version options| --- | ---|---|---|
| **version** | N | Number | `0` | The symbol versions of QR Code range from Version `1` to Version `40`. default 0 means automatically choose the closest version based on the text length. [Information capacity and versions of QR Codes](https://www.qrcode.com/en/about/version.html) **NOTE**: If you set a value less than the minimum version available for text, the minimum version is automatically used. |
| UTF-8 options| --- | ---|---|---|
| **utf8WithoutBOM** | N | Boolean | `true` | Use UTF-8 without BOM. set to `false` value will use BOM in UFT-8.|
| Binary(hex) data model options| --- | ---|---|---|
| **binary** | N | Boolean | `false` | Whether it is binary mode, default is text mode. |

Expand Down

0 comments on commit e2ddd20

Please sign in to comment.