Skip to content

Commit

Permalink
Add svgObject method. Closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyten committed Aug 22, 2014
1 parent 6e76607 commit ab3a5e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
23 changes: 19 additions & 4 deletions lib/qr.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ var VECTOR_OPTIONS = {
margin: 1
};

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

var defaults = _options.type == 'png' ? BITMAP_OPTIONS : VECTOR_OPTIONS;
Expand All @@ -47,14 +47,21 @@ function qr_image(text, options) {
stream._read = fn_noop;

switch (options.type) {
case 'svgpath':
case 'svg':
case 'pdf':
case 'eps':
process.nextTick(function() {
vector[options.type](matrix, stream, options.margin);
});
break;
case 'svgpath':
// deprecated, use svg_object method
process.nextTick(function() {
var obj = vector.svg_object(matrix, options.margin);
stream.push(obj.path);
stream.push(null);
});
break;
case 'png':
default:
process.nextTick(function() {
Expand Down Expand Up @@ -91,8 +98,16 @@ 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
imageSync: qr_image_sync,
svgObject: svg_object
};
15 changes: 11 additions & 4 deletions lib/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,16 @@ function pushSVGPath(matrix, stream, margin) {
});
}

function SVGpath(matrix, stream, margin) {
function SVG_object(matrix, margin) {
var stream = [];
pushSVGPath(matrix, stream, margin);
stream.push(null);

var result = {
size: matrix.length + 2 * margin,
path: stream.filter(Boolean).join('')
}

return result;
}

function SVG(matrix, stream, margin) {
Expand Down Expand Up @@ -241,8 +248,8 @@ function PDF(matrix, stream, margin) {
}

module.exports = {
svgpath: SVGpath,
svg: SVG,
eps: EPS,
pdf: PDF
pdf: PDF,
svg_object: SVG_object
}

0 comments on commit ab3a5e0

Please sign in to comment.