Skip to content

Commit

Permalink
feat: Support JPG and PDF as additional output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Jan 30, 2022
1 parent e994823 commit 5e67b16
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Read the documentation about the [nomnoml source format](https://github.com/skan

## Command-Line Usage

The `nomnoml` script generates a PNG or SVG image from the nomnoml source text.
The `nomnoml` script generates a png, jpg, svg or pdf image from the nomnoml source text.
Both file names and standard input and output are supported as parameters.
If generating the image fails, exit code 1 is returned to the caller.

Expand All @@ -45,11 +45,11 @@ $ nomnoml --help
-V, --version output the version number
-i, --input <path> file with nomnoml source to read from
-o, --output <path> file for the image output to write to
-f, --format <format> output format (png or svg)
-f, --format <format> output format (png, jpg, svg, pdf)
-w, --width <pixels> width of the canvas to draw on
-H, --height <pixels> height of the canvas to draw on
The default output format is PNG. The default canvas size is 640x480 pixels.
The default output format is png. The default canvas size is 640x480 pixels.
If the input file is omitted, the source is read from the standard input.
If the output file is omitted, the image is written to the standard output.
Expand Down Expand Up @@ -81,7 +81,8 @@ object with the following supported properties:
* `resultType`: type of the object to resolve the promise with if the
`output` parameter is not provided ('buffer' or 'stream'; the former
is default)
* `format`: output image format ('png' or 'svg'; the former is default)
* `format`: output image format ('png', 'jpg', 'svg' or 'pdf'; the first
is default)
* `width`: canvas width in pixels (640 by default)
* `height`: canvas height in pixels (480 by default)

Expand Down
4 changes: 2 additions & 2 deletions bin/nomnoml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ commander
.usage('[option]')
.option('-i, --input <path>', 'file with nomnoml source to read from')
.option('-o, --output <path>', 'file for the image output to write to')
.option('-f, --format <format>', 'output format (png or svg)')
.option('-f, --format <format>', 'output format (png, jpg, svg, pdf)')
.option('-w, --width <pixels>', 'width of the canvas to draw on', parseInt)
.option('-H, --height <pixels>', 'height of the canvas to draw on', parseInt)
.on('--help', function () {
console.log(' The default output format is PNG. The default canvas size is 640x480 pixels.');
console.log(' The default output format is png. The default canvas size is 640x480 pixels.');
console.log(' If the input file is omitted, the source is read from the standard input.');
console.log(' If the output file is omitted, the image is written to the standard output.');
console.log();
Expand Down
7 changes: 5 additions & 2 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const { PassThrough } = require('stream');

const mimeTypes = {
png: 'image/png',
svg: 'image/svg+xml'
svg: 'image/svg+xml',
jpg: 'image/jpeg',
pdf: 'application/pdf'
};

// Expands file import directives recursively. A single file can be imported
Expand Down Expand Up @@ -66,7 +68,8 @@ module.exports = function (options) {
} = options;
format = format.toLowerCase();

const canvas = createCanvas(width, height, format === 'svg' ? 'svg' : undefined);
const canvas = createCanvas(width, height,
format === 'svg' ? 'svg' : format === 'pdf' ? 'pdf' : undefined);
draw(canvas, preprocessInputText(
inputText, inputFile && dirname(inputFile)));

Expand Down
43 changes: 42 additions & 1 deletion test/generate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ exports.when = {
.then(async function (result) {
test.ok(true, 'succeeds');
const buffer = await readStream(result);
test.equal(137, buffer[0], 'returns 137 in a PNG stream');
test.equal('P'.charCodeAt(0), buffer[1], 'returns "P" in a PNG stream');
test.equal('N'.charCodeAt(0), buffer[2], 'returns "N" in a PNG stream');
test.equal('G'.charCodeAt(0), buffer[3], 'returns "G" in a PNG stream');
Expand All @@ -127,6 +126,27 @@ exports.when = {
});
},

'called with a string expecting a JPG stream': function (test) {
generateDiagram({
input: '[nomnoml]is->[awesome]',
format: 'jpg',
resultType: 'stream'
})
.then(async function (result) {
test.ok(true, 'succeeds');
const buffer = await readStream(result);
test.equal('J'.charCodeAt(0), buffer[6], 'returns "J" in a JPG stream');
test.equal('F'.charCodeAt(0), buffer[7], 'returns "F" in a JPG stream');
test.equal('I'.charCodeAt(0), buffer[8], 'returns "I" in a JPG stream');
test.equal('F'.charCodeAt(0), buffer[9], 'returns "F" in a JPG stream');
test.done();
})
.catch(function (error) {
test.ok(false, error);
test.done();
});
},

'called with a string expecting an SVG stream': function (test) {
generateDiagram({
input: '[nomnoml]is->[awesome]',
Expand All @@ -149,6 +169,27 @@ exports.when = {
});
},

'called with a string expecting an PDF stream': function (test) {
generateDiagram({
input: '[nomnoml]is->[awesome]',
format: 'pdf',
resultType: 'stream'
})
.then(async function (result) {
test.ok(true, 'succeeds');
const buffer = await readStream(result);
test.equal('%'.charCodeAt(0), buffer[0], 'returns "%" in a PDF stream');
test.equal('P'.charCodeAt(0), buffer[1], 'returns "P" in a PDF stream');
test.equal('D'.charCodeAt(0), buffer[2], 'returns "D" in a PDF stream');
test.equal('F'.charCodeAt(0), buffer[3], 'returns "F" in a PDF stream');
test.done();
})
.catch(function (error) {
test.ok(false, error);
test.done();
});
},

'called with input and output streams': function (test) {
const name = join(__dirname, 'piracy');
generateDiagram({
Expand Down

0 comments on commit 5e67b16

Please sign in to comment.