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

Various fixes #9

Merged
merged 6 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ to install mathjax-node-svg2png and its dependencies.

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

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

Similarly, mathjax-node's `result` object is extended with new keys `png` (containing the resulting data-uri string) and `pngWidth` (PNG width in pixel).
Expand Down
11 changes: 7 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
* SOFTWARE.
*/

var mathjax = require("mathjax-node");
var mathjax = require('mathjax-node');
var svg2png = require('svg2png');

// input `data` extends mathjax-node input `data`.
// Additional values are:
//
// png: false // enable PNG generation
// png: true // enable PNG generation
// scale: 1, // scaling factor to apply during conversion

// `result` data extends the mathjax-node `result` data.
Expand All @@ -45,23 +45,26 @@ var svg2png = require('svg2png');
//

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

var convert = function(result, data, callback) {
var sourceBuffer = new Buffer(result.svg, "utf-8");
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.pngWidth
});
result.png = "data:image/png;base64," + returnBuffer.toString('base64');
result.png = 'data:image/png;base64,' + returnBuffer.toString('base64');
callback(result);
};

Expand Down
5 changes: 3 additions & 2 deletions test/png.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var tape = require('tape');
var typeset = require("../lib/main.js").typeset;
tape('basic test: check PNG generation', function(t) {
t.plan(2);
var options = {math: '\\sin(x)', format:'TeX', png:true};
t.plan(3);
var options = {math: '\\sin(x)', format:'TeX', svg: false, png:true};
typeset(options, function (result) {
t.equal(result.png.indexOf("data:image/png;base64"), 0, 'PNG data-uri header');
t.ok(result.png.length > 100, 'PNG data length');
t.equal(options.svg, false, 'Invariant options');
});
});
12 changes: 12 additions & 0 deletions test/svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var tape = require('tape');
var typeset = require("../lib/main.js").typeset;
tape('basic test: check svg generation', function(t) {
t.plan(4);
var options = {math: '\\cos(x)', format:'TeX', svg: true};
typeset(options, function (result) {
t.ok(true, 'Callback was invoked');
t.equal(typeof result.svg, 'string', 'Content was generated')
t.ok(result.svg.match(/^<svg .*>/i), 'Content is SVG format');
t.equal(options.svg, true, 'Invariant options');
});
});