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

Commit e32ef4c

Browse files
authored
Merge pull request #5 from pkra/scale
[feature] scale option
2 parents 572d2e4 + b21db65 commit e32ef4c

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ to install mathjax-node-svg2png and its dependencies.
1414

1515
## Use
1616

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

19-
png: false, // enable PNG generation
19+
png: false // enable PNG generation
20+
scale: 1 // scaling factor to apply during conversion
2021

21-
Similarly, mathjax-node's `result` object is extended with a new key `png` containing the resulting data-uri string.
22+
Similarly, mathjax-node's `result` object is extended with new keys `png` (containing the resulting data-uri string) and `pngWidth` (PNG width in pixel).
23+
24+
png: // PNG results
25+
pngWidth: // width (in pixel)

lib/main.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,32 @@ var svg2png = require('svg2png');
3434
// input `data` extends mathjax-node input `data`.
3535
// Additional values are:
3636
//
37-
// png: false, // enable PNG generation
37+
// png: false // enable PNG generation
38+
// scale: 1, // scaling factor to apply during conversion
3839

3940
// `result` data extends the mathjax-node `result` data.
4041
// Additional values are
4142
//
42-
// png: // PNG as data-uri
43+
// png // data URI in base64
44+
// pngWidth // PNG width (in pixel)
45+
//
4346

4447
exports.typeset = function(data, callback) {
4548
if (data.png) data.svg = true;
4649
mathjax.typeset(data, function(result) {
4750
if (result.error) callback(result);
48-
if (data.png) convert(result, callback);
51+
if (data.png) convert(result, data, callback);
4952
});
5053
};
5154

52-
var convert = function(result, callback) {
55+
var convert = function(result, data, callback) {
5356
var sourceBuffer = new Buffer(result.svg, "utf-8");
57+
var scale = data.scale || 1;
58+
// NOTE magic constant, vaguely matches ~16pt Times
59+
const EXTOPX = data.ex || 6;
60+
result.pngWidth = result.width.substring(0, result.width.length - 2) * EXTOPX * scale;
5461
var returnBuffer = svg2png.sync(sourceBuffer, {
55-
width: result.width,
56-
height: result.height
62+
width: result.pngWidth
5763
});
5864
result.png = "data:image/png;base64," + returnBuffer.toString('base64');
5965
callback(result);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mathjax-node-svg2png",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "Extend mathjax-node using svg2png",
55
"main": "./lib/main.js",
66
"scripts": {

test/helper.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Helpers (for debugging)
2+
3+
var fs = require("fs");
4+
// writePNG
5+
// USE: require('./helper.js').writePNG(result.png.data,'out.png');
6+
exports.writePNG = function (string, filename) {
7+
var raw = string.substring(22);
8+
fs.writeFile(filename, raw, 'base64', function(err) {
9+
if (err) console.log('Error writing to disk' + err);
10+
});
11+
}

test/scale.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var tape = require('tape');
2+
var typeset = require("../lib/main.js").typeset;
3+
tape('scaling: basic check', function(t) {
4+
t.plan(1);
5+
var opt1 = {math: '\\sin(x)', format:'TeX', png:true};
6+
var opt2 = {math: '\\sin(x)', format:'TeX', png:true, scale: 3};
7+
var result1 = 0;
8+
var result2 = 0;
9+
typeset(opt1, function (result) {
10+
result1 = result.pngWidth;
11+
});
12+
typeset(opt2, function (result) {
13+
result2 = result.pngWidth;
14+
t.equal(result1*3, result2, 'Scale applied')
15+
});
16+
});

0 commit comments

Comments
 (0)