22 *
33 * mj-single-svg.js
44 *
5- * Implements an API to MathJax in node .js so that MathJax can be
6- * used server-side to generate SVG, MathML , or images (the latter
7- * requires an external library, batik, to do the svg to png
8- * conversion). This API converts single math expressions to SVG,
9- * will giving control over the input format, the font caching, and
10- * a number of other features.
5+ * Implements an API to MathJax in Node .js so that MathJax can be
6+ * used server-side to generate HTML+CSS, SVG , or MathML.
7+ *
8+ * This API converts single math expressions while giving control
9+ * over the input format, the SVG font caching, and a number of other
10+ * features.
1111 *
1212 * ----------------------------------------------------------------------
1313 *
14- * Copyright (c) 2014 The MathJax Consortium
14+ * Copyright (c) 2014--2016 The MathJax Consortium
1515 *
1616 * Licensed under the Apache License, Version 2.0 (the "License");
1717 * you may not use this file except in compliance with the License.
@@ -30,11 +30,8 @@ var http = require('http');
3030var fs = require ( 'fs' ) ;
3131var path = require ( 'path' ) ;
3232var url = require ( 'url' ) ;
33- var fmt = require ( 'util' ) . format ;
3433var jsdom = require ( 'jsdom' ) . jsdom ;
35- var execFile = require ( 'child_process' ) . execFile ;
3634var speech = require ( 'speech-rule-engine' ) ;
37- var os = require ( 'os' ) ;
3835
3936require ( './patch/jsdom.js' ) . patch ( jsdom ) ; // Fix some bugs in jsdom
4037
@@ -60,9 +57,6 @@ var defaults = {
6057 css : false , // return CSS for HTML output?
6158 mml : false , // return mml output?
6259 svg : false , // return svg output?
63- img : false , // return img tag for remote image?
64- png : false , // return png image (as data: URL)?
65- dpi : 144 , // dpi for png image
6660
6761 speakText : false , // add spoken annotations to svg output?
6862 speakRuleset : "mathspeak" , // set speech ruleset (default (chromevox rules), mathspeak)
@@ -88,12 +82,10 @@ var STATE = {
8882// The MathJaxPath is normaized against file:/// so that Windows paths are correct
8983//
9084var MathJaxPath = url . resolve ( "file:///" , "file:" + require . resolve ( 'mathjax/unpacked/MathJax' ) ) ;
91- var BatikRasterizerPath = path . resolve ( __dirname , '..' , 'batik/batik-rasterizer.jar' ) ;
9285var MathJaxConfig ; // configuration for when starting MathJax
9386var MathJax ; // filled in once MathJax is loaded
9487var serverState = STATE . STOPPED ; // nothing loaded yet
9588var timer ; // used to reset MathJax if it runs too long
96- var tmpfile = os . tmpdir ( ) + "/mj-single-svg" + process . pid ; // file name prefix to use for temp files
9789
9890var document , window , content , html ; // the DOM elements
9991
@@ -593,10 +585,10 @@ function GetHTML(result) {
593585}
594586
595587//
596- // Create SVG output and IMG output , if requested
588+ // Create SVG output, if requested
597589//
598590function GetSVG ( result ) {
599- if ( ! data . svg && ! data . png && ! data . img ) return ;
591+ if ( ! data . svg ) return ;
600592 var jax = MathJax . Hub . getAllJax ( ) [ 0 ] ; if ( ! jax ) return ;
601593 var script = jax . SourceElement ( ) ,
602594 svg = script . previousSibling . getElementsByTagName ( "svg" ) [ 0 ] ;
@@ -623,67 +615,15 @@ function GetSVG(result) {
623615 var svgdata = svg . outerHTML . replace ( / > < ( [ ^ / ] ) / g, ">\n<$1" )
624616 . replace ( / ( < \/ [ a - z ] * > ) (? = < \/ ) / g, "$1\n" )
625617 . replace ( / ( < (?: u s e | i m a g e ) [ ^ > ] * ) ( h r e f = ) / g, ' $1xlink:$2' ) ;
626- //
627- // The file version includes the xml and DOCTYPE comments
628- //
629- var svgfile = [
630- '<?xml version="1.0" standalone="no"?>' ,
631- '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' ,
632- svgdata
633- ] . join ( "\n" ) ;
634618
635619 //
636620 // Add the requested data to the results
637621 //
638- if ( data . svg ) result . svg = svgdata ;
639- if ( data . png ) result . svgfile = svgfile ;
640- if ( data . img ) {
641- if ( data . svg ) result . svg = svgfile ;
642- result . img = [
643- '<img src="file.svg" style="' ,
644- svg . style . cssText ,
645- " width:" , svg . getAttribute ( "width" ) , "; height:" , svg . getAttribute ( "height" ) ,
646- ';"' ,
647- ( data . speakText ? ' alt="' + result . speakText + '"' : "" ) ,
648- ' />'
649- ] . join ( "" ) ;
650- }
651-
652- //
653- // Add metadata to the results
654- //
655- if ( data . svg ) {
656- result . svg = svgdata ;
657- result . width = svg . getAttribute ( "width" ) ;
658- result . height = svg . getAttribute ( "height" ) ;
659- result . style = svg . style . cssText ;
660- }
661-
662- }
622+ result . svg = svgdata ;
623+ result . width = svg . getAttribute ( "width" ) ;
624+ result . height = svg . getAttribute ( "height" ) ;
625+ result . style = svg . style . cssText ;
663626
664- //
665- // Create the PNG file asynchronously, reporting errors.
666- //
667- function GetPNG ( result ) {
668- var svgfile = result . svgfile ; delete result . svgfile ;
669- if ( data . png ) {
670- var batikCommands = [ '-jar' , BatikRasterizerPath , '-dpi' , data . dpi , tmpfile + '.svg' ] ;
671- var synch = MathJax . Callback ( function ( ) { } ) ; // for synchronization with MathJax
672- var check = function ( err ) { if ( err ) { AddError ( err . message ) ; synch ( ) ; return true } }
673- var tmpSVG = tmpfile + ".svg" , tmpPNG = tmpfile + ".png" ;
674- fs . writeFile ( tmpSVG , svgfile , function ( err ) {
675- if ( check ( err ) ) return ;
676- execFile ( 'java' , batikCommands , function ( err , stdout , stderr ) {
677- if ( check ( err ) ) { fs . unlinkSync ( tmpSVG ) ; return }
678- fs . readFile ( tmpPNG , null , function ( err , buffer ) {
679- result . png = "data:image/png;base64," + ( buffer || "" ) . toString ( 'base64' ) ;
680- fs . unlinkSync ( tmpSVG ) ; fs . unlinkSync ( tmpPNG ) ;
681- check ( err ) ; synch ( ) ;
682- } ) ;
683- } ) ;
684- } ) ;
685- return synch ; // This keeps the queue from continuing until the readFile() is complete
686- }
687627}
688628
689629/********************************************************************/
@@ -735,7 +675,7 @@ function StartQueue() {
735675
736676 //
737677 // Set up a timeout timer to restart MathJax if it runs too long,
738- // Then push the Typeset call, the MathML, speech, SVG, and PNG calls,
678+ // Then push the Typeset call, the MathML, speech, and SVG calls,
739679 // and our TypesetDone routine
740680 //
741681 timer = setTimeout ( RestartMathJax , data . timeout ) ;
@@ -749,7 +689,6 @@ function StartQueue() {
749689 $$ ( GetHTML , result ) ,
750690 $$ ( RerenderSVG , result ) ,
751691 $$ ( GetSVG , result ) ,
752- $$ ( GetPNG , result ) ,
753692 $$ ( ReturnResult , result )
754693 ) ;
755694}
@@ -836,7 +775,7 @@ function SetRenderer(renderer) {
836775}
837776
838777function RerenderSVG ( result ) {
839- if ( data . html && ( data . svg || data . png || data . img ) ) {
778+ if ( data . html && data . svg ) {
840779 timer = setTimeout ( RestartMathJax , data . timeout ) ;
841780 var queue = MathJax . Callback . Queue ( ) , $$ = window . Array ;
842781 return queue . Push (
0 commit comments