From 93bf5b72abcc946f044b3f35c82b8d397ef6f5c4 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Thu, 27 Apr 2023 12:36:30 -0700 Subject: [PATCH] feat: enable ppi setting on png cli export (#8854) --- bin/args.js | 3 +++ bin/render.js | 5 ++++- bin/vl2png | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bin/args.js b/bin/args.js index da2f1356a67..6de2fb20c86 100644 --- a/bin/args.js +++ b/bin/args.js @@ -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; }; diff --git a/bin/render.js b/bin/render.js index ec1ffc22332..a01bb3762e8 100644 --- a/bin/render.js +++ b/bin/render.js @@ -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.'; @@ -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 diff --git a/bin/vl2png b/bin/vl2png index bffe2b9914e..219c8b883b8 100755 --- a/bin/vl2png +++ b/bin/vl2png @@ -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); }); });