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转换成图片方法_第1张图片

其对应的MathML描述应该是:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
  <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">
    <mtr>
      <mtd>
        <mrow class="MJX-TeXAtom-ORD">
          <mi mathvariant="normal">T</mi>
          <mi mathvariant="normal">i</mi>
          <mi mathvariant="normal">m</mi>
          <mi mathvariant="normal">e</mi>
        </mrow>
        <mo>=</mo>
        <mrow class="MJX-TeXAtom-ORD">
          <mi mathvariant="normal">R</mi>
          <mi mathvariant="normal">T</mi>
          <mi mathvariant="normal">T</mi>
        </mrow>
        <mo>×<!-- × --></mo>
        <mrow>
          <mo>⌈</mo>
          <mi>l</mi>
          <mi>o</mi>
          <msub>
            <mi>g</mi>
            <mn>2</mn>
          </msub>
          <mrow>
            <mo>(</mo>
            <mfrac>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="normal">N</mi>
              </mrow>
              <mrow class="MJX-TeXAtom-ORD">
                <mi mathvariant="normal">i</mi>
                <mi mathvariant="normal">n</mi>
                <mi mathvariant="normal">i</mi>
                <mi mathvariant="normal">t</mi>
                <mi mathvariant="normal">i</mi>
                <mi mathvariant="normal">a</mi>
                <mi mathvariant="normal">l</mi>
                <mtext> </mtext>
                <mi mathvariant="normal">c</mi>
                <mi mathvariant="normal">w</mi>
                <mi mathvariant="normal">n</mi>
                <mi mathvariant="normal">d</mi>
              </mrow>
            </mfrac>
            <mo>)</mo>
          </mrow>
          <mo>⌉</mo>
        </mrow>
      </mtd>
    </mtr>
  </mtable>
</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

可以得到如下图片


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


你可能感兴趣的:(脚本,编辑器,MathML)