Last active
May 30, 2016 08:06
-
-
Save pkra/c60098af5c1d8c37473416caad0418f6 to your computer and use it in GitHub Desktop.
An example module for wrapping PNG generation around mathjax-node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mjAPI = require("../lib/main.js"); | |
var svg2png = require('svg2png'); | |
function createPNG(result, callback){ | |
var sourceBuffer = new Buffer(result.svg, "utf-8"); | |
svg2png(sourceBuffer).then(function(buffer){ | |
result.png = "data:image/png;base64," + buffer.toString('base64'); | |
return callback(result); | |
}) | |
}; | |
exports.math2png = function(options, callback){ | |
var mjConfig = Object.assign({}, options.config); | |
console.log("**Note**: options.config can only be assigned once"); | |
var typesetOptions = Object.assign({}, options.typeset); | |
// make sure SVG output will be generated and disable mml and html | |
typesetOptions.svg = true; | |
typesetOptions.mml = false; | |
typesetOptions.html = false; | |
mjAPI.config(mjConfig); | |
mjAPI.typeset(typesetOptions, function(result){ | |
if (result.errors) callback(result); | |
createPNG(result, callback); | |
}); | |
} | |
// Usage: | |
// | |
// var math2png = require("math2png.js").math2png; | |
// var options = { | |
// config: { | |
// }, | |
// typeset: { | |
// math: "x^2", | |
// format: "TeX" | |
// } | |
// } | |
// math2png(options, function(result){ | |
// console.log(result.png); | |
// console.log("style='" + result.style + "width:" + result.width + "; height:" + result.height +";'"); | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment