Skip to content

Commit

Permalink
feat: enable ppi setting on png cli export (vega#8854)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidanthoff authored and BradyJ27 committed Oct 19, 2023
1 parent a95ef2a commit 93bf5b7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions bin/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ To load data, you may need to set a base directory:
if (type === 'vega') {
args.boolean('p').alias('p', 'pretty').describe('p', 'Output human readable/pretty spec.');
}
else if (type === 'png') {
args.number('ppi').describe('ppi', 'Resolution in ppi.');
}

return args.help().version().argv;
};
5 changes: 4 additions & 1 deletion bin/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ module.exports = (type, callback, opt) => {
// set output image scale factor
const scale = arg.scale || undefined;

// Allows for other ppi settings than 72 for png files
const ppi = arg.ppi || 72;

// use a seeded random number generator, if specified
if (typeof arg.seed !== 'undefined') {
if (Number.isNaN(arg.seed)) throw 'Illegal seed value: must be a valid number.';
Expand All @@ -55,7 +58,7 @@ module.exports = (type, callback, opt) => {
renderer: 'none' // no primary renderer needed
}).finalize(); // clear any timers, etc

return (type === 'svg' ? view.toSVG(scale) : view.toCanvas(scale, opt)).then(_ => callback(_, arg));
return (type === 'svg' ? view.toSVG(scale) : view.toCanvas(scale * ppi / 72, opt)).then(_ => callback(_, arg));
}

// read input from file or stdin
Expand Down
2 changes: 1 addition & 1 deletion bin/vl2png
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ const render = require('./render');
render('png', function(canvas, arg) {
const file = arg._[1] || null;
const out = file ? createWriteStream(file) : process.stdout;
const stream = canvas.createPNGStream();
const stream = canvas.createPNGStream({ resolution: arg.ppi || undefined });
stream.on('data', chunk => { out.write(chunk); });
});

0 comments on commit 93bf5b7

Please sign in to comment.