From 5ccc34518d520d687f2469ffe0c67b9700e50290 Mon Sep 17 00:00:00 2001
From: Peter Krautzberger
Date: Fri, 28 Apr 2017 14:25:10 +0200
Subject: [PATCH 1/2] [feature] scale option * add config.scale * add
result.pngWidth (in pixel) * add test/helper.js for debugging
---
README.md | 10 +++++++---
lib/main.js | 18 ++++++++++++------
package.json | 2 +-
test/helper.js | 11 +++++++++++
test/scale.js | 16 ++++++++++++++++
5 files changed, 47 insertions(+), 10 deletions(-)
create mode 100644 test/helper.js
create mode 100644 test/scale.js
diff --git a/README.md b/README.md
index a251513..3d366c5 100644
--- a/README.md
+++ b/README.md
@@ -12,8 +12,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)
diff --git a/lib/main.js b/lib/main.js
index 3114763..b9ae80c 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -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 = 38;
+ result.pngWidth = result.width.substring(0, result.width.length - 2)*38*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);
diff --git a/package.json b/package.json
index e8925c5..4bc4f2e 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/test/helper.js b/test/helper.js
new file mode 100644
index 0000000..903f25c
--- /dev/null
+++ b/test/helper.js
@@ -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);
+ });
+}
\ No newline at end of file
diff --git a/test/scale.js b/test/scale.js
new file mode 100644
index 0000000..1b70d1e
--- /dev/null
+++ b/test/scale.js
@@ -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')
+ });
+});
From 92680ed7d9303507034139f1949ad16a54a9def8 Mon Sep 17 00:00:00 2001
From: Peter Krautzberger
Date: Sat, 29 Apr 2017 14:19:12 +0200
Subject: [PATCH 2/2] fix ex-to-px constant
---
lib/main.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lib/main.js b/lib/main.js
index b9ae80c..6ffabcc 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -56,8 +56,8 @@ 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 = 38;
- result.pngWidth = result.width.substring(0, result.width.length - 2)*38*scale;
+ 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
});