Skip to content

Commit

Permalink
fix(binding): updates lib to properly convert bitmap chars
Browse files Browse the repository at this point in the history
Updates getBitmap to convert unsigned int to v8 int for bitmap array.
Updates unit tests to test QRCode and Code128 for quick verification.
Updates CI files to build for Node 9-13 on each of win, osx, and linux.
  • Loading branch information
jshor committed Dec 10, 2019
1 parent fa01451 commit 51c0990
Show file tree
Hide file tree
Showing 83 changed files with 64,317 additions and 40,888 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ language: node_js

os:
- linux
# - osx
- osx

node_js:
- $NODE_VERSION
Expand All @@ -14,6 +14,7 @@ install:
- yarn

script:
- yarn test
- yarn package-binary
- yarn publish-binary

Expand All @@ -23,7 +24,7 @@ after_success:
env:
matrix:
- NODE_VERSION="9"
# - NODE_VERSION="10"
# - NODE_VERSION="11"
# - NODE_VERSION="12"
- NODE_VERSION="10"
- NODE_VERSION="11"
- NODE_VERSION="12"
- NODE_VERSION="13"
22 changes: 17 additions & 5 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
image: Visual Studio 2017

install:
- ps: Install-Product node $NODE_VERSION
# - npm remove node-gyp
- ps: Install-Product node $env:NODE_VERSION $env:platform
- yarn global add windows-build-tools
# - yarn test
- yarn
- yarn package-binary
- yarn publish-binary

test_script:
# Output useful info for debugging.
- node --version
- npm --version
# run tests
- yarn test

platform:
- x86
- x64

environment:
matrix:
- NODE_VERSION: '9 x64'
- NODE_VERSION: '13 x64'
- NODE_VERSION: '9'
- NODE_VERSION: '10'
- NODE_VERSION: '11'
- NODE_VERSION: '12'
- NODE_VERSION: '13'
22 changes: 22 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"targets": [
{
"target_name": "<(module_name)",
"defines": ["NO_PNG"],
"cflags": ["-Wno-sign-compare"],
"xcode_settings": {
"OTHER_CFLAGS": ["-Wno-sign-compare"]
},
"sources": [
"src/lib/zint/2of5.c",
"src/lib/zint/auspost.c",
Expand Down Expand Up @@ -35,6 +40,22 @@
"src/lib/zint/svg.c",
"src/lib/zint/telepen.c",
"src/lib/zint/upcean.c",
"src/lib/zint/bmp.c",
"src/lib/zint/codablock.c",
"src/lib/zint/dotcode.c",
"src/lib/zint/eci.c",
"src/lib/zint/emf.c",
"src/lib/zint/gb2312.c",
"src/lib/zint/general_field.c",
"src/lib/zint/gif.c",
"src/lib/zint/hanxin.c",
"src/lib/zint/mailmark.c",
"src/lib/zint/pcx.c",
"src/lib/zint/raster.c",
"src/lib/zint/sjis.c",
"src/lib/zint/tif.c",
"src/lib/zint/ultra.c",
"src/lib/zint/vector.c",
"src/lib/binding.cpp"
],
"include_dirs": [
Expand All @@ -50,6 +71,7 @@
},
{
"target_name": "action_after_build",
"cflags": ["-Wno-sign-compare"],
"type": "none",
"dependencies": [ "<(module_name)" ],
"copies": [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"module_path": "./lib/binding/bin/",
"remote_path": "./builds/v{version}/{platform}/{arch}/{node_abi}",
"package_name": "{module_name}.tar.gz",
"host": "https://symbology.dev.s3-us-east-2.amazonaws.com"
"host": "https://symbologyjs.s3-us-east-2.amazonaws.com"
},
"dependencies": {
"nan": "^2.14.0",
Expand Down
4 changes: 4 additions & 0 deletions src/__tests__/fixtures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"qrPng": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACoAAAA8CAYAAAAQTCjdAAAAAklEQVR4AewaftIAAAJQSURBVNXB0Y1bRxREwdODF0DnHxHD6QzawoUgeHbHNLniz6tSw4MfkBkNG5nRMGQ2DT+yuImL32Re0nAkc9QwZDYyL2kYi5u4+KLhSOYlDW9pOJLZLG7i4i/JjIYhs2n4iMVNXHyIzGgYMh+1uImLL2Te0jBkhsxoGDJPybxkcRMXvzV8RMOQGQ1DZtPwlsVNXD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GQFgRpQZAWBGlBkBYEaUGQFgRpQZAWBGlBkBYEaUGQFgRpQZAWBGlBkBYEaUGQFgRpQZAWBGlBkBYEaUGQFgRpQZAWBGlBkBYEaUGQFgRpQZAWBGlBkBYEaUGQFgRpQZAWBGlBkBYEaUGQFgRpQZAWBGlBkBYEaUGQFgRpQQEfSH9YkBYEacELMQ11uJnmpg6XaR7V4YtpqMNlmkd1eEsv3tw0X0xDHW6moQ7U4Ytpvpjmizrob+GNTUMd/pdpqMPNNNRB33jxxurwqA4/Mg11+LFpburwtl7oH01DHW6muUxDHT5NQx0e1eFmGurwll7oP6nDp2mow7fqoF8EfWsa6vAj01AH/UDQb01DHW6m+dY0TMM0l2ku0+gXL/RoGurwr9ThZhrqoAcFfPCmpvmiDkzzqA6XaW7q8Gga6vBpmps6vK0CPpD+sCAtCNKCIC0I0oK/AOPQeeK2eBuxAAAAAElFTkSuQmCC"
}
57 changes: 31 additions & 26 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var chai = require('chai');
var sinon = require('sinon');
var mocha = require('mocha');
var fs = require('fs');
var fixtures = require('./fixtures.json');
var library = require('../');
var regex = require('./helpers/regex');
var createSymbology = require('./helpers/createSymbologyStub');
Expand All @@ -17,13 +18,7 @@ function getSymbol(obj) {
symbology: obj.symbology || library.Barcode.CODE128,
foregroundColor: obj.foregroundColor || 'fff000',
backgroundColor: obj.backgroundColor || '000000',
fileName: obj.fileName || 'out.png',
scale: obj.scale || 1.0,
option1: obj.option1 || -1,
option2: obj.option2 || -1,
option3: obj.option3 || -1,
showHumanReadableText: obj.showHumanReadableText || 1
// input_mode: BINARY_MODE,
fileName: obj.fileName || 'out.bmp'
};
}

Expand All @@ -44,7 +39,7 @@ describe('the symbology library', function() {
var filePath = 'testfile.png';

beforeEach(function() {
sandbox.stub(symbology, 'createFile').callsFake(createSymbology);
sandbox.stub(symbology, 'createStream').callsFake(createSymbology);
});

it('should return a status code and a message', function() {
Expand Down Expand Up @@ -76,7 +71,7 @@ describe('the symbology library', function() {
var filePath = 'testfile.svg';

beforeEach(function() {
sandbox.stub(symbology, 'createFile').callsFake(createSymbology);
sandbox.stub(symbology, 'createStream').callsFake(createSymbology);
});

it('should return a zero status code and render an SVG file', function() {
Expand Down Expand Up @@ -109,23 +104,7 @@ describe('the symbology library', function() {
.then(function(data) {
expect(data.code).to.be.a('number');
expect(data.message).to.be.a('string');
// expect(data.data).to.match(regex.svg);
});
});
});

describe('the createStream function when `outputType` is not specified', function() {
beforeEach(function() {
sandbox.stub(symbology, 'createStream').callsFake(createSymbology);
});

it('should return default to rendering a png', function () {
return library
.createStream(getSymbol(), '12345')
.then(function(data) {
expect(data.code).to.be.a('number');
expect(data.message).to.be.a('string');
expect(data.data).to.match(regex.base64);
// expect(data.data).to.match(regex.svg);
});
});
});
Expand Down Expand Up @@ -158,4 +137,30 @@ describe('the symbology library', function() {
});
});
});

describe('rendering PNG images', function() {
it('should render a Code 128 PNG image', function() {
return library
.createStream(getSymbol({
symbology: library.Barcode.CODE128
}), '12345', library.Output.PNG)
.then(function(data) {
expect(data.code).to.be.a('number');
expect(data.message).to.be.a('string');
expect(data.data).to.equal(fixtures.code128Png);
});
});

it('should render a QRCode PNG image', function() {
return library
.createStream(getSymbol({
symbology: library.Barcode.QRCODE
}), '12345', library.Output.PNG)
.then(function(data) {
expect(data.code).to.be.a('number');
expect(data.message).to.be.a('string');
expect(data.data).to.equal(fixtures.qrPng);
});
});
})
});
2 changes: 1 addition & 1 deletion src/enums/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
PNG: 'png',
PNG: 'bmp',
SVG: 'svg',
EPS: 'eps'
}
21 changes: 12 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var defaultSymbol = {
outputOptions: -1,
foregroundColor: '000000',
backgroundColor: 'ffffff',
fileName: 'out.png',
fileName: 'out.bmp',
scale: 1.0,
option1: -1,
option2: -1,
Expand Down Expand Up @@ -116,7 +116,7 @@ function blobToBase64Png(image) {
function pngRender(bitmap, width, height) {
var image = PNGImage.createImage(width, height);
var i = 0;

for(var y = 0; y<height; y++) {
for(var x = 0; x<width; x++) {
image.setAt(x, y, {
Expand Down Expand Up @@ -145,18 +145,19 @@ function pngRender(bitmap, width, height) {
*/
function theCreateStram(symbol, barcodeData, outputType) {
outputType = outputType || exp.Output.PNG
symbol.fileName = symbol.fileName || 'out.' + outputType
symbol.fileName = symbol.fileName.replace(/\.png$/g, '.bmp') || 'out.' + outputType

if (outputType !== exp.Output.PNG) {
// if (outputType !== exp.Output.PNG) {
symbol.outputOptions = 8; // force buffer to write to rendered_data
}
// }

var res = createSymbology(symbol, barcodeData, 'createStream');

if(res.code <= 2) {
if (typeof res.encodedData === 'string') {
res.encodedData = res.encodedData.replace(/\r?\n?[^\r\n]*$/, '')
}
// console.log('I HAVE DATA: ', res.encodedData)
// if (typeof res.encodedData === 'string') {
// res.encodedData = res.encodedData.replace(/\r?\n?[^\r\n]*$/, '')
// }

return Promise.resolve({
data: res.encodedData,
Expand Down Expand Up @@ -203,6 +204,8 @@ exp.createStream = function(symbol, barcodeData, outputType) {
* @returns {Promise<Object>} object with resulting props (see docs)
*/
exp.createFile = function(symbol, barcodeData) {
const originalFileName = symbol.fileName
symbol.fileName = symbol.fileName.replace(/\.png$/g, '.bmp')
const outputType = exp.Output[symbol.fileName.split('.').pop().toUpperCase()] || exp.Output.PNG

return theCreateStram(symbol, barcodeData, outputType)
Expand All @@ -211,7 +214,7 @@ exp.createFile = function(symbol, barcodeData) {
var image = pngRender(res.data, res.width, res.height);

return new Promise((resolve, reject) => {
image.writeImage(symbol.fileName, function(err) {
image.writeImage(originalFileName, function(err) {
if (err) {
reject(err)
} else {
Expand Down
37 changes: 15 additions & 22 deletions src/lib/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,60 +22,55 @@ namespace symbology {
* Returns a bitmap (of type V8 Array) of the image in memory.
*/
Local<Object> getBitmap(Isolate* isolate, zint_symbol *symbol) {
v8::Local<v8::Context> context = isolate->GetCurrentContext();

int matrix_size = symbol->bitmap_width * symbol->bitmap_height * 3;
int i;

v8::Local<v8::Object> outArr = Object::New(isolate);
// Local<v8::Array> outArr = v8::Array::New(isolate, matrix_size);
Local<v8::Array> outArr = v8::Array::New(isolate, matrix_size);

for (i = 0; i < matrix_size; i++) {
// outArr->Set(i, v8::Integer::NewFromUnsigned(isolate, symbol->bitmap[i]));
// Nan::Set(outArr, Nan::New<int>i, 1);
Nan::Set(outArr,
Nan::New<String>(std::to_string(i)).ToLocalChecked(),
v8::Integer::New(isolate, symbol->bitmap[i]));
// outArr[i] = symbol->bitmap[i];
(void)outArr->Set(context, i, v8::Integer::NewFromUnsigned(isolate, symbol->bitmap[i]));
}
return outArr;
}

/**
* Renders symbology and returns an object with PNG bitmap data, EPS binary data, or SVG XML.
* Renders symbology and returns an object with PNG bitmap data, EPS, or SVG XML.
*/
Local<Object> createStreamHandle(Isolate* isolate, zint_symbol *symbol, uint8_t *data, char *str) {
int status_code = ZBarcode_Encode_and_Print(symbol, data, 0, 0);
int status_code = ZBarcode_Encode_and_Buffer(symbol, data, 0, 0);

v8::Local<v8::Object> obj = Object::New(isolate);

if(status_code <= 2) {
// the barcode creation was successful; parse the result
int fileNameLength = strlen(symbol->outfile);

if(fileNameLength > 4) {
// check if the file at least is 4 chars long so we can parse the last three and check it as an extension
const char *fileExt = &symbol->outfile[fileNameLength - 3];

if(strcmp("png", fileExt) == 0) {
// obj->Set(String::NewFromUtf8(isolate, "encodedData"), getBitmap(isolate, symbol));
if(strcmp("bmp", fileExt) == 0) {
// parse the buffer as a bitmap array and store it in `encodedData`
Nan::Set(obj, Nan::New<String>("encodedData").ToLocalChecked(), getBitmap(isolate, symbol));
}

if(strcmp("svg", fileExt) == 0 || strcmp("eps", fileExt) == 0) {
// parse the result as a normal string and store it in `encodedData`
// obj->Set(String::NewFromUtf8(isolate, "encodedData"), String::NewFromUtf8(isolate, symbol->rendered_data));
Nan::Set(obj, Nan::New<String>("encodedData").ToLocalChecked(), Nan::New<String>(symbol->rendered_data).ToLocalChecked());
// Nan::Set(obj, Nan::New<String>("encodedData").ToLocalChecked(), Nan::New<String>(symbol->encoded_data).ToLocalChecked());
}
}

// set the buffered bitmap dimensions
Nan::Set(obj, Nan::New<String>("width").ToLocalChecked(), v8::Integer::New(isolate, symbol->bitmap_width));
Nan::Set(obj, Nan::New<String>("height").ToLocalChecked(), v8::Integer::New(isolate, symbol->bitmap_height));

// obj->Set(String::NewFromUtf8(isolate, "width"), v8::Integer::New(isolate, symbol->bitmap_width));
// obj->Set(String::NewFromUtf8(isolate, "height"), v8::Integer::New(isolate, symbol->bitmap_height));
}

// set the informational params (message and status code)
Nan::Set(obj, Nan::New<String>("message").ToLocalChecked(), Nan::New<String>(symbol->errtxt).ToLocalChecked());
Nan::Set(obj, Nan::New<String>("code").ToLocalChecked(), v8::Integer::New(isolate, status_code));

// obj->Set(String::NewFromUtf8(isolate, "message"), String::NewFromUtf8(isolate, symbol->errtxt));
// obj->Set(String::NewFromUtf8(isolate, "code"), v8::Integer::New(isolate, status_code));

return obj;
}
Expand Down Expand Up @@ -156,16 +151,14 @@ namespace symbology {

obj = createStreamHandle(isolate, symbol, (unsigned char*)*data, (char*)*data);

printf("SYMBOLOGY --- %i", symbol->symbology);

ZBarcode_Delete(symbol);
args.GetReturnValue().Set(obj);
}

void Init(v8::Local<v8::Object> exports) {
v8::Local<v8::Context> context = exports->CreationContext();

exports->Set(context,
(void)exports->Set(context,
Nan::New("createStream").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(createStream)
->GetFunction(context)
Expand Down

0 comments on commit 51c0990

Please sign in to comment.