@@ -232,14 +232,18 @@ class BakomaTrueTypeFonts(Fonts):
232232 None : 'cmmi10' ,
233233 }
234234
235- def __init__ (self ):
235+ def __init__ (self , useSVG = False ):
236236 self .glyphd = {}
237237 self .fonts = dict (
238238 [ (name , FT2Font (os .path .join (self .basepath , name ) + '.ttf' ))
239239 for name in self .fnames ])
240240
241241 for font in self .fonts .values ():
242242 font .clear ()
243+ if useSVG :
244+ self .svg_glyphs = [] # a list of "glyphs" we need to render this thing in SVG
245+ else : pass
246+ self .usingSVG = useSVG
243247
244248 def get_metrics (self , font , sym , fontsize , dpi ):
245249 cmfont , metrics , glyph , offset = \
@@ -299,9 +303,22 @@ def render(self, ox, oy, font, sym, fontsize, dpi):
299303 cmfont , metrics , glyph , offset = \
300304 self ._get_info (font , sym , fontsize , dpi )
301305
302- cmfont .draw_glyph_to_bitmap (
303- int (ox ), int (self .height - oy - metrics .ymax ), glyph )
304-
306+ if not self .usingSVG :
307+ cmfont .draw_glyph_to_bitmap (
308+ int (ox ), int (self .height - oy - metrics .ymax ), glyph )
309+ else :
310+ oy += offset - 512 / 2048. * 10.
311+ basename = self .fontmap [font ]
312+ if latex_to_bakoma .has_key (sym ):
313+ basename , num = latex_to_bakoma [sym ]
314+ num = self .fonts [basename ].get_charmap ()[num ]
315+ elif len (sym ) == 1 :
316+ num = ord (sym )
317+ else :
318+ num = 0
319+ print >> sys .stderr , 'unrecognized symbol "%s"' % sym
320+ self .svg_glyphs .append ((basename , fontsize , num , ox , oy , metrics ))
321+
305322
306323class BakomaPSFonts (Fonts ):
307324 """
@@ -960,6 +977,57 @@ def math_parse_s_ft2font(s, dpi, fontsize, angle=0):
960977
961978math_parse_s_ft2font .cache = {}
962979
980+ def math_parse_s_ft2font_svg (s , dpi , fontsize , angle = 0 ):
981+ """
982+ Parse the math expression s, return the (bbox, fonts) tuple needed
983+ to render it.
984+
985+ fontsize must be in points
986+
987+ return is width, height, fonts
988+ """
989+
990+ major , minor1 , minor2 , tmp , tmp = sys .version_info
991+ if major == 2 and minor1 == 2 :
992+ print >> sys .stderr , 'mathtext broken on python2.2. We hope to get this fixed soon'
993+ sys .exit ()
994+ cacheKey = (s , dpi , fontsize , angle )
995+ s = s [1 :- 1 ] # strip the $ from front and back
996+ if math_parse_s_ft2font_svg .cache .has_key (cacheKey ):
997+ w , h , svg_glyphs = math_parse_s_ft2font_svg .cache [cacheKey ]
998+ return w , h , svg_glyphs
999+
1000+ bakomaFonts = BakomaTrueTypeFonts (useSVG = True )
1001+ Element .fonts = bakomaFonts
1002+ handler .clear ()
1003+ expression .parseString ( s )
1004+
1005+ handler .expr .set_size_info (fontsize , dpi )
1006+
1007+ # set the origin once to allow w, h compution
1008+ handler .expr .set_origin (0 , 0 )
1009+ xmin = min ([e .xmin () for e in handler .symbols ])
1010+ xmax = max ([e .xmax () for e in handler .symbols ])
1011+ ymin = min ([e .ymin () for e in handler .symbols ])
1012+ ymax = max ([e .ymax () for e in handler .symbols ])
1013+
1014+ # now set the true origin - doesn't affect with and height
1015+ w , h = xmax - xmin , ymax - ymin
1016+ # a small pad for the canvas size
1017+ w += 2
1018+ h += 2
1019+
1020+ handler .expr .set_origin (0 , h - ymax )
1021+
1022+ Element .fonts .set_canvas_size (w ,h )
1023+ handler .expr .render ()
1024+ handler .clear ()
1025+
1026+ math_parse_s_ft2font_svg .cache [cacheKey ] = w , h , bakomaFonts .svg_glyphs
1027+ return w , h , bakomaFonts .svg_glyphs
1028+
1029+ math_parse_s_ft2font_svg .cache = {}
1030+
9631031
9641032def math_parse_s_ps (s , dpi , fontsize ):
9651033 """
0 commit comments