Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/get_options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var BITMAP_OPTIONS = {
parse_url: false,
ec_level: 'M',
size: 5,
margin: 4,
customize: null
};

var VECTOR_OPTIONS = {
parse_url: false,
ec_level: 'M',
margin: 1,
size: 0
};

function get_options(options, force_type) {
if (typeof options === 'string') {
options = { 'ec_level': options }
} else {
options = options || {};
}
var _options = {
type: String(force_type || options.type || 'png').toLowerCase()
};

var defaults = _options.type == 'png' ? BITMAP_OPTIONS : VECTOR_OPTIONS;

for (var k in defaults) {
_options[k] = k in options ? options[k] : defaults[k];
}

return _options;
}

module.exports = get_options
43 changes: 2 additions & 41 deletions lib/qr.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,7 @@ var vector = require('./vector');

var fn_noop = function() {};

var BITMAP_OPTIONS = {
parse_url: false,
ec_level: 'M',
size: 5,
margin: 4,
customize: null
};

var VECTOR_OPTIONS = {
parse_url: false,
ec_level: 'M',
margin: 1,
size: 0
};

function get_options(options, force_type) {
if (typeof options === 'string') {
options = { 'ec_level': options }
} else {
options = options || {};
}
var _options = {
type: String(force_type || options.type || 'png').toLowerCase()
};

var defaults = _options.type == 'png' ? BITMAP_OPTIONS : VECTOR_OPTIONS;

for (var k in defaults) {
_options[k] = k in options ? options[k] : defaults[k];
}

return _options;
}
var get_options = require('./get_options')

function qr_image(text, options) {
options = get_options(options);
Expand Down Expand Up @@ -106,16 +74,9 @@ function qr_image_sync(text, options) {
return result;
}

function svg_object(text, options) {
options = get_options(options, 'svg');

var matrix = QR(text, options.ec_level);
return vector.svg_object(matrix, options.margin);
}

module.exports = {
matrix: QR,
image: qr_image,
imageSync: qr_image_sync,
svgObject: svg_object
svgObject: vector.svg_object
};
12 changes: 11 additions & 1 deletion lib/vector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"use strict";

var QR = require('./qr-base').QR;
var get_options = require('./get_options')

function matrix2path(matrix) {
var N = matrix.length;
var filled = [];
Expand Down Expand Up @@ -252,9 +255,16 @@ function PDF(matrix, stream, margin) {
stream.push(null);
}

function svg_object(text, options) {
options = get_options(options, 'svg');

var matrix = QR(text, options.ec_level);
return SVG_object(matrix, options.margin);
}

module.exports = {
svg: SVG,
eps: EPS,
pdf: PDF,
svg_object: SVG_object
svg_object: svg_object
}
Binary file added qr_sync.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ qr.image(text, { type: 'eps', ec_level: ec_level}).pipe(file('qr.eps'));
qr.image(text, { type: 'pdf', ec_level: ec_level}).pipe(file('qr.pdf'));

fs.writeFileSync('qr_sync.png', qr.imageSync(text));

// vector only test
var vector = require('./../lib/vector')
var result = vector.svg_object(text)

console.log('vector.svg_object test',
result.size === 31 ? 'passed' : 'failed',
result.path === 'M1 1h7v7h-7zM10 1h7v1h1v1h-1v2h1v1h-2v-1h-1v-1h1v-1h-1v1h-1v-2h-2v1h-1v1h-1v5h-1v-6h1zM19 1h3v1h-3zM23 1h7v7h-7zM2 2v5h5v-5zM24 2v5h5v-5zM3 3h3v3h-3zM25 3h3v3h-3zM12 4h2v2h-1v1h-1v1h-1v-2h1zM21 4h1v2h-1zM19 5h1v1h-1zM13 7h1v2h-1zM15 7h1v2h-1zM17 7h1v2h-1zM19 7h1v1h-1zM21 7h1v1h-1zM20 8h1v1h1v1h1v-1h5v1h-2v2h-2v1h-2v2h-4v1h-1v1h1v4h1v2h-1v-1h-1v-2h-2v1h-2v-1h-1v-1h2v-1h2v1h1v-1h-1v-1h-1v-1h1v-1h1v-1h-2v-1h5v1h1v-3h-2v-1h1zM1 9h1v2h-1zM3 9h5v1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1h-1zM9 29h1v1h-1zM14 29h2v1h-2zM17 29h1v1h-1zM20 29h1v1h-1z'
? 'passed'
: 'failed'
)