Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5 from pkra/scale
Browse files Browse the repository at this point in the history
[feature] scale option
  • Loading branch information
pkra authored Apr 29, 2017
2 parents 572d2e4 + b21db65 commit e32ef4c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ to install mathjax-node-svg2png and its dependencies.

## Use

This module is used like mathjax-node, extending the input `data` object with a new option
This module is used like mathjax-node, extending the input `data` object with new options

png: false, // enable PNG generation
png: false // enable PNG generation
scale: 1 // scaling factor to apply during conversion

Similarly, mathjax-node's `result` object is extended with a new key `png` containing the resulting data-uri string.
Similarly, mathjax-node's `result` object is extended with new keys `png` (containing the resulting data-uri string) and `pngWidth` (PNG width in pixel).

png: // PNG results
pngWidth: // width (in pixel)
18 changes: 12 additions & 6 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,32 @@ var svg2png = require('svg2png');
// input `data` extends mathjax-node input `data`.
// Additional values are:
//
// png: false, // enable PNG generation
// png: false // enable PNG generation
// scale: 1, // scaling factor to apply during conversion

// `result` data extends the mathjax-node `result` data.
// Additional values are
//
// png: // PNG as data-uri
// png // data URI in base64
// pngWidth // PNG width (in pixel)
//

exports.typeset = function(data, callback) {
if (data.png) data.svg = true;
mathjax.typeset(data, function(result) {
if (result.error) callback(result);
if (data.png) convert(result, callback);
if (data.png) convert(result, data, callback);
});
};

var convert = function(result, callback) {
var convert = function(result, data, callback) {
var sourceBuffer = new Buffer(result.svg, "utf-8");
var scale = data.scale || 1;
// NOTE magic constant, vaguely matches ~16pt Times
const EXTOPX = data.ex || 6;
result.pngWidth = result.width.substring(0, result.width.length - 2) * EXTOPX * scale;
var returnBuffer = svg2png.sync(sourceBuffer, {
width: result.width,
height: result.height
width: result.pngWidth
});
result.png = "data:image/png;base64," + returnBuffer.toString('base64');
callback(result);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mathjax-node-svg2png",
"version": "1.0.2",
"version": "1.1.0",
"description": "Extend mathjax-node using svg2png",
"main": "./lib/main.js",
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Helpers (for debugging)

var fs = require("fs");
// writePNG
// USE: require('./helper.js').writePNG(result.png.data,'out.png');
exports.writePNG = function (string, filename) {
var raw = string.substring(22);
fs.writeFile(filename, raw, 'base64', function(err) {
if (err) console.log('Error writing to disk' + err);
});
}
16 changes: 16 additions & 0 deletions test/scale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var tape = require('tape');
var typeset = require("../lib/main.js").typeset;
tape('scaling: basic check', function(t) {
t.plan(1);
var opt1 = {math: '\\sin(x)', format:'TeX', png:true};
var opt2 = {math: '\\sin(x)', format:'TeX', png:true, scale: 3};
var result1 = 0;
var result2 = 0;
typeset(opt1, function (result) {
result1 = result.pngWidth;
});
typeset(opt2, function (result) {
result2 = result.pngWidth;
t.equal(result1*3, result2, 'Scale applied')
});
});

0 comments on commit e32ef4c

Please sign in to comment.