Skip to content

Commit

Permalink
fix: adds patch system to apply stdout fixes
Browse files Browse the repository at this point in the history
Updates install script to apply patches to Zint library for saving
stdout data to a buffer, converting it to a string, and returning it
as part of encodedData.
  • Loading branch information
jshor committed Dec 11, 2019
1 parent babb7d6 commit d0ac5bc
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 37 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ install:
- yarn global add windows-build-tools
- yarn
- yarn package-binary
- yarn publish-binary
# - yarn publish-binary

test_script:
# Output useful info for debugging.
Expand All @@ -20,7 +20,7 @@ platform:

environment:
matrix:
- NODE_VERSION: '9'
# - NODE_VERSION: '9'
- NODE_VERSION: '10'
- NODE_VERSION: '11'
- NODE_VERSION: '12'
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"node-pre-gyp": "^0.14.0",
"pngjs": "3.3.0",
"pngjs-image": "^0.11.6",
"replace-in-file": "^4.2.0"
"replace-in-file": "^4.2.0",
"rimraf": "^3.0.0"
},
"devDependencies": {
"aws-sdk": "2.108.0",
Expand All @@ -53,6 +54,6 @@
"build": "node-pre-gyp rebuild --target_arch=x64",
"package-binary": "node-pre-gyp rebuild package --target_arch=x64",
"publish-binary": "node-pre-gyp publish --target_arch=x64",
"install": "node ./scripts/install.js && node-pre-gyp install --fallback-to-build"
"install": "node ./scripts/install.js"
}
}
63 changes: 57 additions & 6 deletions scripts/install.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,64 @@
const clone = require('git-clone')
const replace = require('replace-in-file')
const rimraf = require('rimraf')
const path = require('path')

console.log('Cloning zint library...')

clone('https://github.com/woo-j/zint.git', '.zint', () => {
replace.sync({
files: '.zint/**/*.c',
const patches = [
{
files: '.zint/**/zint.h',
from: /struct zint_symbol \{/g,
to: `
struct zint_symbol {
char rendered_data[100000000];
`
},
{
files: '.zint/**/*.{c,h}',
from: /\<malloc\.h\>/g,
to: '<stdlib.h>'
},
{
files: '.zint/**/{svg,ps}.{c,h}',
from: /([a-z]+) = stdout;\n/g,
to: `#ifndef _WIN32
pipe(p);
#endif
$1 = fdopen(p[1], "w");
`
},
{
files: '.zint/**/{svg,ps}.{c,h}',
from: /int ([a-z]+)_plot([^\n]+)/g,
to: `
int pipe(int fd[2]);
int close(int fildes);
int read(int fildes, void *buf, unsigned nbytes);
int $1_plot$2
int p[2];`
},
{
files: '.zint/**/{svg,ps}.{c,h}',
from: /(fflush\([a-z]+\);)/g,
to: `$1
close(p[1]);
read(p[0], symbol->rendered_data, 100000000);`
}
]

console.log('Removing any existing .zint directory...')

rimraf(path.join(__dirname, '../.zint'), () => {
console.log('Cloning zint...', path.join(__dirname, '.zint'))

clone('https://github.com/woo-j/zint.git', '.zint', () => {
console.log('Successfully cloned. Applying code patches...')

patches.forEach(patch => replace.sync(patch))

console.log('Done.')
})
console.log('Finished cloning zint.')
})
})

3 changes: 2 additions & 1 deletion src/__tests__/fixtures.json

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ describe('the symbology library', function() {
});
});

describe('rendering SVG images', function() {
it('should render a MaxiCode SVG image', function() {
return library
.createStream(getSymbol({
symbology: library.Barcode.MAXICODE,
foregroundColor: 'ff00ff',
backgroundColor: '00ff00'
}), '12345', library.Output.SVG)
.then(function(data) {
expect(data.code).to.be.a('number');
expect(data.message).to.be.a('string');
expect(data.data).to.equal(fixtures.maxicodeSvg);
});
});
});

describe('rendering PNG images', function() {
it('should render a Code 128 PNG image', function() {
return library
Expand Down
21 changes: 19 additions & 2 deletions src/binding/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <typeinfo>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <nan.h>
#include "../../.zint/backend/zint.h"

Expand Down Expand Up @@ -39,7 +40,13 @@ namespace symbology {
* 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_Buffer(symbol, data, 0, 0);
int status_code;

if ((symbol->output_options & BARCODE_STDOUT) != 0) {
status_code = ZBarcode_Encode_and_Print(symbol, data, 0, 0);
} else {
status_code = ZBarcode_Encode_and_Buffer(symbol, data, 0, 0);
}

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

Expand All @@ -57,9 +64,19 @@ namespace symbology {
}

if(strcmp("svg", fileExt) == 0 || strcmp("eps", fileExt) == 0) {
// char buff[10000];

// close(symbol->file_pointer[1]);
// read(symbol->file_pointer[0], buff, 10000);

// strcpy(symbol->rendered_data, buff);

// strcpy(symbol->rendered_data, buff);
// parse the result as a normal string and store it in `encodedData`
// obj->Set(String::NewFromUtf8(isolate, "encodedData"), String::NewFromUtf8(isolate, symbol->rendered_data));
printf("ENCODED: %s", *symbol->encoded_data);
// printf("ENCODED: %s", symbol->rendered_data);
// printf("THE RESULT -- %s --END", 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());
}
}
Expand Down
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: 'bmp',
PNG: 'png',
SVG: 'svg',
EPS: 'eps'
}
49 changes: 27 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ function createSymbology(symbologyStruct, barcodeData, fnName) {
symbologyStruct.outputOptions,
symbologyStruct.backgroundColor,
symbologyStruct.foregroundColor,
symbologyStruct.fileName,
// indicate to the library that we want BMP instead of PNG
symbologyStruct.fileName.replace(/\.png$/g, '.bmp'),
symbologyStruct.scale,
symbologyStruct.option1,
symbologyStruct.option2,
Expand Down Expand Up @@ -137,28 +138,27 @@ function pngRender(bitmap, width, height) {
* If PNG, it returns the stream as a base64 string.
*
* @note The file will be created in memory and then passed to the returned object.
*
*
* @private
* @param {Symbol} symbol - Symbol struct
* @param {String} barcodeData - data to encode
* @param {String} outputType - 'png', 'svg', or 'eps' (default: 'png')
* @param {Boolean} isStdout - if true, forces the buffer to write to rendered_data
* @returns {Promise<Object>} object with resulting props (see docs)
*/
function theCreateStram(symbol, barcodeData, outputType) {
outputType = outputType || exp.Output.PNG
symbol.fileName = symbol.fileName.replace(/\.png$/g, '.bmp') || 'out.' + outputType
function callManagedLibrary(symbol, barcodeData, outputType, isStdout) {
if (!Object.values(exp.Output).includes(outputType)) {
throw new Error(`Invalid output type: ${outputType}`)
}

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

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

if(res.code <= 2) {
// 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,
message: res.message,
Expand All @@ -173,8 +173,16 @@ function theCreateStram(symbol, barcodeData, outputType) {
});
};

/**
* Creates an in-memory of a PNG, SVG or EPS file in the specified fileName path.
*
* @param {Symbol} symbol - symbol struct
* @param {String} barcodeData - data to encode
* @param {String} outputType - `png`, `eps`, or `svg`.
* @returns {Promise<Object>} object with resulting props (see docs)
*/
exp.createStream = function(symbol, barcodeData, outputType) {
return theCreateStram(symbol, barcodeData, outputType)
return callManagedLibrary(symbol, barcodeData, outputType, true)
.then((res) => {
if (outputType === exp.Output.PNG) {
var pngData = pngRender(res.data, res.width, res.height);
Expand All @@ -195,26 +203,23 @@ exp.createStream = function(symbol, barcodeData, outputType) {
}

/**
* Creates a stream of a PNG, SVG or EPS file in the specified fileName path.
*
* @note: Zint will render the file type based on the extension of the given file path.
* Creates a file of a PNG, SVG or EPS file in the specified fileName path.
*
* @param {Symbol} symbol - symbol struct
* @param {String} barcodeData - data to encode
* @param {String} outputType - `png`, `eps`, or `svg`.
* @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
const outputType = symbol.fileName.match(/\.([a-z]+)$/).pop()

return theCreateStram(symbol, barcodeData, outputType)
return callManagedLibrary(symbol, barcodeData, outputType)
.then((res) => {
if (outputType === exp.Output.PNG) {
var image = pngRender(res.data, res.width, res.height);

return new Promise((resolve, reject) => {
image.writeImage(originalFileName, function(err) {
image.writeImage(symbol.fileName, function(err) {
if (err) {
reject(err)
} else {
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ glob@^7.0.5:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.1.4:
glob@^7.1.3, glob@^7.1.4:
version "7.1.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
Expand Down Expand Up @@ -1327,6 +1327,13 @@ rimraf@^2.6.1:
dependencies:
glob "^7.0.5"

rimraf@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.0.tgz#614176d4b3010b75e5c390eb0ee96f6dc0cebb9b"
integrity sha512-NDGVxTsjqfunkds7CqsOiEnxln4Bo7Nddl3XhS4pXg5OzwkLqJ971ZVAAnB+DDLnF76N+VnDEiBHaVV8I06SUg==
dependencies:
glob "^7.1.3"

safe-buffer@^5.0.1, safe-buffer@^5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
Expand Down

0 comments on commit d0ac5bc

Please sign in to comment.