2
2
*
3
3
* mj-single-svg.js
4
4
*
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.
11
11
*
12
12
* ----------------------------------------------------------------------
13
13
*
14
- * Copyright (c) 2014 The MathJax Consortium
14
+ * Copyright (c) 2014--2016 The MathJax Consortium
15
15
*
16
16
* Licensed under the Apache License, Version 2.0 (the "License");
17
17
* you may not use this file except in compliance with the License.
@@ -30,11 +30,8 @@ var http = require('http');
30
30
var fs = require ( 'fs' ) ;
31
31
var path = require ( 'path' ) ;
32
32
var url = require ( 'url' ) ;
33
- var fmt = require ( 'util' ) . format ;
34
33
var jsdom = require ( 'jsdom' ) . jsdom ;
35
- var execFile = require ( 'child_process' ) . execFile ;
36
34
var speech = require ( 'speech-rule-engine' ) ;
37
- var os = require ( 'os' ) ;
38
35
39
36
require ( './patch/jsdom.js' ) . patch ( jsdom ) ; // Fix some bugs in jsdom
40
37
@@ -60,9 +57,6 @@ var defaults = {
60
57
css : false , // return CSS for HTML output?
61
58
mml : false , // return mml output?
62
59
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
66
60
67
61
speakText : false , // add spoken annotations to svg output?
68
62
speakRuleset : "mathspeak" , // set speech ruleset (default (chromevox rules), mathspeak)
@@ -88,12 +82,10 @@ var STATE = {
88
82
// The MathJaxPath is normaized against file:/// so that Windows paths are correct
89
83
//
90
84
var MathJaxPath = url . resolve ( "file:///" , "file:" + require . resolve ( 'mathjax/unpacked/MathJax' ) ) ;
91
- var BatikRasterizerPath = path . resolve ( __dirname , '..' , 'batik/batik-rasterizer.jar' ) ;
92
85
var MathJaxConfig ; // configuration for when starting MathJax
93
86
var MathJax ; // filled in once MathJax is loaded
94
87
var serverState = STATE . STOPPED ; // nothing loaded yet
95
88
var 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
97
89
98
90
var document , window , content , html ; // the DOM elements
99
91
@@ -593,10 +585,10 @@ function GetHTML(result) {
593
585
}
594
586
595
587
//
596
- // Create SVG output and IMG output , if requested
588
+ // Create SVG output, if requested
597
589
//
598
590
function GetSVG ( result ) {
599
- if ( ! data . svg && ! data . png && ! data . img ) return ;
591
+ if ( ! data . svg ) return ;
600
592
var jax = MathJax . Hub . getAllJax ( ) [ 0 ] ; if ( ! jax ) return ;
601
593
var script = jax . SourceElement ( ) ,
602
594
svg = script . previousSibling . getElementsByTagName ( "svg" ) [ 0 ] ;
@@ -623,67 +615,15 @@ function GetSVG(result) {
623
615
var svgdata = svg . outerHTML . replace ( / > < ( [ ^ / ] ) / g, ">\n<$1" )
624
616
. replace ( / ( < \/ [ a - z ] * > ) (? = < \/ ) / g, "$1\n" )
625
617
. 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" ) ;
634
618
635
619
//
636
620
// Add the requested data to the results
637
621
//
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 ;
663
626
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
- }
687
627
}
688
628
689
629
/********************************************************************/
@@ -735,7 +675,7 @@ function StartQueue() {
735
675
736
676
//
737
677
// 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,
739
679
// and our TypesetDone routine
740
680
//
741
681
timer = setTimeout ( RestartMathJax , data . timeout ) ;
@@ -749,7 +689,6 @@ function StartQueue() {
749
689
$$ ( GetHTML , result ) ,
750
690
$$ ( RerenderSVG , result ) ,
751
691
$$ ( GetSVG , result ) ,
752
- $$ ( GetPNG , result ) ,
753
692
$$ ( ReturnResult , result )
754
693
) ;
755
694
}
@@ -836,7 +775,7 @@ function SetRenderer(renderer) {
836
775
}
837
776
838
777
function RerenderSVG ( result ) {
839
- if ( data . html && ( data . svg || data . png || data . img ) ) {
778
+ if ( data . html && data . svg ) {
840
779
timer = setTimeout ( RestartMathJax , data . timeout ) ;
841
780
var queue = MathJax . Callback . Queue ( ) , $$ = window . Array ;
842
781
return queue . Push (
0 commit comments