Hi all,
I just wanted to get others some insight about what I did in
mathtext2. Who knows - it might turn out to be useful to someone
Also, any comments/thoughts are appreciated.
A TeX math expression:
$\sum_{i = 0}^{\infty}$
gets translated to a pure Python list, consisting of Python builtins:
[u'\\sum', u'_', [u'i', u' ', u'=', u' ', u'0'], u'^', [u'\\infty']]
This gets fed to a token parser that produces the TeX equivalent of
hboxes. Every unicode character (including "\sum" - u'\u2211') gets
translated to a TexCharClass instance. TexCharClass (I'm not very good
at naming things is a class that completely handles rendering of a
single character. It does this based on the information available from
the font file (TrueType). More about fonts can be found in the
excellent FreeType lib docs.
http://www.freetype.org/freetype2/documentation.html
A combination like a_b^c (or a^c_b) gets translated to a Scripted
instance, which, again, handles the rendering of the sub/supercript
and the nucleus. Similar for a fraction: a Fraction instance has a
numerator and denumerator, which are used for rendering.
The above python list
[u'\\sum', u'_', [u'i', u' ', u'=', u' ', u'0'], u'^', [u'\\infty']]
and any other sublist, like
[u'i', u' ', u'=', u' ', u'0']
get translated to a Hbox instance.
TexCharClass, Scripted, Fraction, Hbox (but also Line, Kern) are all
sublclasses of Renderer, a class that defines an __init__ method and a
render method. The data attributes of a Renderer instance (initialized
to 0 by the Renderer's __init__ method) are xmin, xmax, ymin, ymax,
bearingx, bearingy, width, height - for now. Basically, these are
taken from the FreeType implementation of glyphs. In other words,
every instance of Renderer, that is: an instance of a subclass of
Renderer (Renderer is a virtual class) behaves like a glyph.
http://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html
Unfortunately, although FreeType allows drawing of vertical text, I
haven't implemented it yet. This would be also a base for Vbox, so,
for example, \frac 1 2 could be translated to Vbox([u'1', Line(width,
height), u'2']). Supporting vertical text involves adding additional
attributes to the classes like, vertical bearingx, vertical bearingy
(see the FreeType docs).
Cheers,
Edin