MathML转换成图片方法

1. MathML介绍

MathML是什么就不解释了,请参考:

1. IBM:https://www.ibm.com/developerworks/cn/xml/x-mathml3/

2.Wiki百科:http://zh.wikipedia.org/zh/%E6%95%B0%E5%AD%A6%E7%BD%AE%E6%A0%87%E8%AF%AD%E8%A8%80

2.需求

最近在翻译《High Performance Browser Networking》时,里面有很多的公式采用的是MathML编辑的,但是CSDN这个编辑器不支持MathML,因此复杂的公式无法在博客中展现,研究了一下,发现有一个开源工具jeuclid可以支持将mathML公式转换成各种格式的图片,且支持多种配置,使用起来比较方便。

比如下面这个公式(因为CSDN不支持MathML,只好采用截图)

其对应的MathML描述应该是:

 

[html][/html] view plaincopyprint?

  1. <math xmlns=”http://www.w3.org/1998/Math/MathML” display=”block”>
  2.   <mtable columnalign=”right left right left right left right left right left right left” rowspacing=”.5em” columnspacing=”0.278em 2em 0.278em 2em 0.278em 2em 0.278em 2em 0.278em 2em 0.278em”>
  3.     <mtr>
  4.       <mtd>
  5.         <mrow class=”MJX-TeXAtom-ORD”>
  6.           <mi mathvariant=”normal”>T</mi>
  7.           <mi mathvariant=”normal”>i</mi>
  8.           <mi mathvariant=”normal”>m</mi>
  9.           <mi mathvariant=”normal”>e</mi>
  10.         </mrow>
  11.         <mo>=</mo>
  12.         <mrow class=”MJX-TeXAtom-ORD”>
  13.           <mi mathvariant=”normal”>R</mi>
  14.           <mi mathvariant=”normal”>T</mi>
  15.           <mi mathvariant=”normal”>T</mi>
  16.         </mrow>
  17.         <mo>×<!– × –></mo>
  18.         <mrow>
  19.           <mo>⌈</mo>
  20.           <mi>l</mi>
  21.           <mi>o</mi>
  22.           <msub>
  23.             <mi>g</mi>
  24.             <mn>2</mn>
  25.           </msub>
  26.           <mrow>
  27.             <mo>(</mo>
  28.             <mfrac>
  29.               <mrow class=”MJX-TeXAtom-ORD”>
  30.                 <mi mathvariant=”normal”>N</mi>
  31.               </mrow>
  32.               <mrow class=”MJX-TeXAtom-ORD”>
  33.                 <mi mathvariant=”normal”>i</mi>
  34.                 <mi mathvariant=”normal”>n</mi>
  35.                 <mi mathvariant=”normal”>i</mi>
  36.                 <mi mathvariant=”normal”>t</mi>
  37.                 <mi mathvariant=”normal”>i</mi>
  38.                 <mi mathvariant=”normal”>a</mi>
  39.                 <mi mathvariant=”normal”>l</mi>
  40.                 <mtext> </mtext>
  41.                 <mi mathvariant=”normal”>c</mi>
  42.                 <mi mathvariant=”normal”>w</mi>
  43.                 <mi mathvariant=”normal”>n</mi>
  44.                 <mi mathvariant=”normal”>d</mi>
  45.               </mrow>
  46.             </mfrac>
  47.             <mo>)</mo>
  48.           </mrow>
  49.           <mo>⌉</mo>
  50.         </mrow>
  51.       </mtd>
  52.     </mtr>
  53.   </mtable>
  54. </math>

 

3. jeuclid的使用方法

jeuclid是一个apache授权的工具包,里面包含了几个工具,包括渲染器,转换器等,我只使用了其转换器,其余的没有仔细研究,有兴趣或者有需求可以自己去研究。

在jeuclid的目录下有一个mml2xxx.bat脚本,需要转换就可以使用这个脚本。这个脚本的使用参数如下

 

usage: mml2xxx <source file(s)> <target file/directory> [options]
source is the path to the source file (MathML or ODF format)
target is the path to the target file / directory
If multiple source files are given, target must be a directory
-antiAlias <boolean> anti-alias mode [default: true]
-antiAliasMinSize <float> minimum font size for which anti-alias
is turned on [default: 10.0]
-backgroundColor <color> default background color
(mathbackground)
-debug <boolean> debug mode – if on, elements will have
borders drawn around them [default:
false]
-display <BLOCK|INLINE> display style [default: BLOCK]
-fontsDoublestruck <list> list of font families for Double-Struck
[default: MSBM10, Mathematica7, Caslon
Open Face, Caslon Openface, Cloister
Open Face, Academy Engraved LET,
Colonna MT, Imprint MT Shadow]
-fontsFraktur <list> list of font families for Fraktur
[default: EUFM10, Mathematica6,
FetteFraktur, Fette Fraktur, Euclid
Fraktur, Lucida Blackletter, Blackmoor
LET]
-fontSize <float> font size used for the output
(mathsize) [default: 12.0]
-fontsMonospaced <list> list of font families for Monospaced
[default: Andale Mono, Courier, Courier
Mono, Courier New, Lucida Sans
Typewriter, DejaVu Sans Mono,
DejaVuSansMono, Bitstream Vera Sans
Mono, Luxi Mono, FreeMono, monospaced]
-fontsSansSerif <list> list of font families for Sans-Serif
[default: Verdana, Helvetica, Arial,
Arial Unicode MS, Lucida Sans Unicode,
Lucida Sans, Lucida Grande, DejaVu
Sans, DejaVuSans, Bitstream Vera Sans,
Luxi Sans, FreeSans, sansserif]
-fontsScript <list> list of font families for Script
[default: EUSM10, cmsy10, Math5,
Mathematica5, Savoye LET, Brush Script
MT, Zapfino, Apple Chancery, Edwardian
Script ITC, Lucida Handwriting,
Monotype Corsiva, Santa Fe LET]
-fontsSerif <list> list of font families for Serif
[default: Constantina, Cambria, Times,
Times New Roman, Lucida Bright, DejaVu
Serif, DejaVuSerif, Bitstream Vera
Serif, Luxi Serif, FreeSerif, serif]
-foregroundColor <color> default foreground color (mathcolor)
[default: black]
-mfracKeepScriptLevel <boolean> if true, <mfrac> element will NEVER
increase children’s scriptlevel (in
violation of the spec) [default: false]
-outFileType <arg> output file mime type [default: derived
from the target file’s extention];
available values are:
application/postscript image/x-png
application/x-shockwave-flash
image/x-portable-pixmap
image/vnd.wap.wbmp image/x-gif
image/svg+xml image/jpeg image/png
image/x-raw image/gif image/bmp
image/x-emf application/pdf
-scriptLevel <integer> script level [default: 0]
-scriptMinSize <float> font size to be used for smallest
script [default: 8.0]
-scriptSizeMult <float> script size multiplier [default: 0.71]
Example: mml2xxx a.mml a.png -backgroundColor white

 

 

将上面的MathML保存成一个XML文件(文件后缀名其实可以任意),执行脚本

mml2xxx.bat 1.xml 1.png -fontSize 36.0 -backgroundColor gray

可以得到如下图片

效果还不错,更多的效果,等待你自己去发现。

标签