Skip to content

Commit

Permalink
Add sync version
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Jan 10, 2016
1 parent 3248e40 commit bf6233a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ svg2png(sourceBuffer, { width: 300, height: 400 })

This is especially useful for images without `width` or `height`s. You can even specify just one of them and (if the image has an appropriate `viewBox`) the other will be set to scale.

## Sync variant

There's also a sync variant, for use in your shell scripts:

```js
const outputBuffer = svg2png.sync(sourceBuffer, optionalWidthAndOrHeight);
```

## How the conversion is done

svg2png is built on the latest in [PhantomJS](http:https://phantomjs.org/) technology to render your SVGs using a headless WebKit instance. I have found this to produce much more accurate renderings than other solutions like GraphicsMagick or Inkscape. Plus, it's easy to install cross-platform due to the excellent [phantomjs](https://npmjs.org/package/phantomjs) npm package—you don't even need to have PhantomJS in your `PATH`.
Expand Down
10 changes: 6 additions & 4 deletions lib/svg2png.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ module.exports = (sourceBuffer, resize) => {
return cp.promise.then(processResult);
};

// TODO: not sure what execFileSync returns?!
// module.exports.sync = (sourceBuffer, resize) => {
// return processResult(childProcess.execFileSync(phantomJsCmd, getPhantomJSArgs(sourceBuffer, resize)));
// }
module.exports.sync = (sourceBuffer, resize) => {
const result = childProcess.spawnSync(phantomjsCmd, getPhantomJSArgs(resize), {
input: sourceBuffer.toString("base64")
});
return processResult(result);
}

function getPhantomJSArgs(resize) {
return [
Expand Down
31 changes: 28 additions & 3 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,38 @@ function successTest(test, index) {
});
}

function successTestSync(test, index) {
specify(test.name, () => {
const input = fs.readFileSync(relative(`inputs/${test.file}`));
const expected = fs.readFileSync(relative(`success-tests/${index}.png`));

const output = svg2png.sync(input, test.resize);
output.should.deep.equal(expected);
});
}

function failureTest(test) {
specify(`(negative test) ${test.name}`, () => {
specify(test.name, () => {
const input = fs.readFileSync(relative(`inputs/${test.file}`));

return svg2png(input, test.resize).should.be.rejectedWith(/width or height/i);
});
}

successTests.forEach(successTest);
failureTests.forEach(failureTest);
function failureTestSync(test) {
specify(test.name, () => {
const input = fs.readFileSync(relative(`inputs/${test.file}`));

(() => svg2png.sync(input, test.resize)).should.throw(/width or height/i);
});
}

describe("async", () => {
describe("should fulfill", () => successTests.forEach(successTest));
describe("should reject", () => failureTests.forEach(failureTest));
});

describe("sync", () => {
describe("should return", () => successTests.forEach(successTestSync));
describe("should throw", () => failureTests.forEach(failureTestSync));
});
1 change: 0 additions & 1 deletion test/success-tests/rebaseline.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ function relative(relPath) {
return path.resolve(__dirname, relPath);
}

// TODO do this all sync maybe
tests.forEach((test, index) => {
fs.readFile(relative(`../inputs/${test.file}`))
.then(input => svg2png(input, test.resize))
Expand Down

0 comments on commit bf6233a

Please sign in to comment.