From: <md...@us...> - 2007-11-26 14:29:52
|
Revision: 4438 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4438&view=rev Author: mdboom Date: 2007-11-26 06:29:49 -0800 (Mon, 26 Nov 2007) Log Message: ----------- Minor speed improvements in mathtext. Removing trailing whitespace. Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/mathtext.py Modified: trunk/matplotlib/lib/matplotlib/mathtext.py =================================================================== --- trunk/matplotlib/lib/matplotlib/mathtext.py 2007-11-26 14:10:11 UTC (rev 4437) +++ trunk/matplotlib/lib/matplotlib/mathtext.py 2007-11-26 14:29:49 UTC (rev 4438) @@ -210,7 +210,7 @@ class MathtextBackendBbox(MathtextBackend): """A backend whose only purpose is to get a precise bounding box. Only required for the Agg backend.""" - + def __init__(self, real_backend): MathtextBackend.__init__(self) self.bbox = [0, 0, 0, 0] @@ -221,7 +221,7 @@ min(self.bbox[1], y1), max(self.bbox[2], x2), max(self.bbox[3], y2)] - + def render_glyph(self, ox, oy, info): self._update_bbox(ox + info.metrics.xmin, oy - info.metrics.ymax, @@ -253,14 +253,14 @@ self.real_backend.fonts_object = self.fonts_object self.real_backend.ox = self.bbox[0] self.real_backend.oy = self.bbox[1] - + class MathtextBackendAggRender(MathtextBackend): def __init__(self): self.ox = 0 self.oy = 0 self.image = None MathtextBackend.__init__(self) - + def set_canvas_size(self, w, h, d): MathtextBackend.set_canvas_size(self, w, h, d) self.image = FT2Image(ceil(w), ceil(h + d)) @@ -286,11 +286,11 @@ def MathtextBackendAgg(): return MathtextBackendBbox(MathtextBackendAggRender()) - + class MathtextBackendBitmapRender(MathtextBackendAggRender): def get_results(self, box): return self.image - + def MathtextBackendBitmap(): return MathtextBackendBbox(MathtextBackendBitmapRender()) @@ -312,7 +312,7 @@ """ % locals() self.lastfont = postscript_name, fontsize self.pswriter.write(ps) - + ps = """%(ox)f %(oy)f moveto /%(symbol_name)s glyphshow\n """ % locals() @@ -428,7 +428,7 @@ """Fix any cyclical references before the object is about to be destroyed.""" self.used_characters = None - + def get_kern(self, font1, sym1, fontsize1, font2, sym2, fontsize2, dpi): """ @@ -739,7 +739,7 @@ fontmap = {} use_cmex = True - + def __init__(self, *args, **kwargs): # This must come first so the backend's owner is set correctly if rcParams['mathtext.fallback_to_cm']: @@ -760,7 +760,7 @@ def _map_virtual_font(self, fontname, font_class, uniindex): return fontname, uniindex - + def _get_glyph(self, fontname, font_class, sym, fontsize): found_symbol = False @@ -769,7 +769,7 @@ if uniindex is not None: fontname = 'ex' found_symbol = True - + if not found_symbol: try: uniindex = get_unicode_index(sym) @@ -782,7 +782,7 @@ fontname, uniindex = self._map_virtual_font( fontname, font_class, uniindex) - + # Only characters in the "Letter" class should be italicized in 'it' # mode. Greek capital letters should be Roman. if found_symbol: @@ -832,13 +832,16 @@ return [(fontname, sym)] class StixFonts(UnicodeFonts): + """ + A font handling class for the STIX fonts + """ _fontmap = { 'rm' : 'STIXGeneral', 'it' : 'STIXGeneralItalic', 'bf' : 'STIXGeneralBol', 'nonunirm' : 'STIXNonUni', 'nonuniit' : 'STIXNonUniIta', 'nonunibf' : 'STIXNonUniBol', - + 0 : 'STIXGeneral', 1 : 'STIXSiz1Sym', 2 : 'STIXSiz2Sym', @@ -851,7 +854,6 @@ cm_fallback = False def __init__(self, *args, **kwargs): - self._sans = kwargs.pop("sans", False) TruetypeFonts.__init__(self, *args, **kwargs) if not len(self.fontmap): for key, name in self._fontmap.iteritems(): @@ -893,14 +895,14 @@ # This will generate a dummy character uniindex = 0x1 fontname = 'it' - + # Handle private use area glyphs if (fontname in ('it', 'rm', 'bf') and uniindex >= 0xe000 and uniindex <= 0xf8ff): fontname = 'nonuni' + fontname return fontname, uniindex - + _size_alternatives = {} def get_sized_alternatives_for_symbol(self, fontname, sym): alternatives = self._size_alternatives.get(sym) @@ -921,7 +923,14 @@ self._size_alternatives[sym] = alternatives return alternatives - + +class StixSansFonts(StixFonts): + """ + A font handling class for the STIX fonts (using sans-serif + characters by default). + """ + _sans = True + class StandardPsFonts(Fonts): """ Use the standard postscript fonts for rendering to backend_ps @@ -1087,7 +1096,8 @@ # Note that (as TeX) y increases downward, unlike many other parts of # matplotlib. -# How much text shrinks when going to the next-smallest level +# How much text shrinks when going to the next-smallest level. GROW_FACTOR +# must be the inverse of SHRINK_FACTOR. SHRINK_FACTOR = 0.7 GROW_FACTOR = 1.0 / SHRINK_FACTOR # The number of different sizes of chars to use, beyond which they will not @@ -1162,10 +1172,16 @@ pass class Vbox(Box): + """ + A box with only height (zero width). + """ def __init__(self, height, depth): Box.__init__(self, 0., height, depth) class Hbox(Box): + """ + A box with only width (zero height and depth). + """ def __init__(self, width): Box.__init__(self, width, 0., 0.) @@ -1243,8 +1259,9 @@ self.depth *= GROW_FACTOR class Accent(Char): - """The font metrics need to be dealt with differently for accents, since they - are already offset correctly from the baseline in TrueType fonts.""" + """The font metrics need to be dealt with differently for accents, + since they are already offset correctly from the baseline in + TrueType fonts.""" def _update_metrics(self): metrics = self._metrics = self.font_output.get_metrics( self.font, self.font_class, self.c, self.fontsize, self.dpi) @@ -1743,7 +1760,7 @@ self.cur_s += 1 self.max_push = max(self.cur_s, self.max_push) clamp = self.clamp - + for p in box.children: if isinstance(p, Char): p.render(self.cur_h + self.off_h, self.cur_v + self.off_v) @@ -1866,7 +1883,7 @@ empty = Empty() empty.setParseAction(raise_error) return empty - + class Parser(object): _binary_operators = Set(r''' + * @@ -1924,7 +1941,7 @@ _dropsub_symbols = Set(r'''\int \oint'''.split()) _fontnames = Set("rm cal it tt sf bf default bb frak circled scr".split()) - + _function_names = Set(""" arccos csc ker min arcsin deg lg Pr arctan det lim sec arg dim liminf sin cos exp limsup sinh cosh gcd ln sup cot hom log tan @@ -1937,7 +1954,7 @@ _leftDelim = Set(r"( [ { \lfloor \langle \lceil".split()) _rightDelim = Set(r") ] } \rfloor \rangle \rceil".split()) - + def __init__(self): # All forward declarations are here font = Forward().setParseAction(self.font).setName("font") @@ -1949,7 +1966,7 @@ self._expression = Forward().setParseAction(self.finish).setName("finish") float = Regex(r"-?[0-9]+\.?[0-9]*") - + lbrace = Literal('{').suppress() rbrace = Literal('}').suppress() start_group = (Optional(latexfont) + lbrace) @@ -1995,7 +2012,7 @@ c_over_c =(Suppress(bslash) + oneOf(self._char_over_chars.keys()) ).setParseAction(self.char_over_chars) - + accent = Group( Suppress(bslash) + accent @@ -2057,7 +2074,7 @@ ) | Error("Expected symbol or group") simple <<(space - | customspace + | customspace | font | subsuper ) @@ -2107,11 +2124,11 @@ + (Suppress(math_delim) | Error("Expected end of math '$'")) + non_math - ) + ) ) + StringEnd() self._expression.enablePackrat() - + self.clear() def clear(self): @@ -2158,7 +2175,7 @@ self.font_class = name self._font = name font = property(_get_font, _set_font) - + def get_state(self): return self._state_stack[-1] @@ -2216,7 +2233,7 @@ def customspace(self, s, loc, toks): return [self._make_space(float(toks[1]))] - + def symbol(self, s, loc, toks): # print "symbol", toks c = toks[0] @@ -2242,7 +2259,7 @@ # (in multiples of underline height) r'AA' : ( ('rm', 'A', 1.0), (None, '\circ', 0.5), 0.0), } - + def char_over_chars(self, s, loc, toks): sym = toks[0] state = self.get_state() @@ -2253,7 +2270,7 @@ self._char_over_chars.get(sym, (None, None, 0.0)) if under_desc is None: raise ParseFatalException("Error parsing symbol") - + over_state = state.copy() if over_desc[0] is not None: over_state.font = over_desc[0] @@ -2267,19 +2284,19 @@ under = Char(under_desc[1], under_state) width = max(over.width, under.width) - + over_centered = HCentered([over]) over_centered.hpack(width, 'exactly') under_centered = HCentered([under]) under_centered.hpack(width, 'exactly') - + return Vlist([ over_centered, Vbox(0., thickness * space), under_centered ]) - + _accent_map = { r'hat' : r'\circumflexaccent', r'breve' : r'\combiningbreve', @@ -2603,7 +2620,7 @@ return is width, height, fonts """ _parser = None - + _backend_mapping = { 'Bitmap': MathtextBackendBitmap, 'Agg' : MathtextBackendAgg, @@ -2613,6 +2630,13 @@ 'Cairo' : MathtextBackendCairo } + _font_type_mapping = { + 'cm' : BakomaFonts, + 'stix' : StixFonts, + 'stixsans' : StixSansFonts, + 'custom' : UnicodeFonts + } + def __init__(self, output): self._output = output self._cache = {} @@ -2620,7 +2644,7 @@ def parse(self, s, dpi = 72, prop = None): if prop is None: prop = FontProperties() - + cacheKey = (s, dpi, hash(prop)) result = self._cache.get(cacheKey) if result is not None: @@ -2631,16 +2655,13 @@ else: backend = self._backend_mapping[self._output]() fontset = rcParams['mathtext.fontset'] - if fontset == 'cm': - font_output = BakomaFonts(prop, backend) - elif fontset == 'stix': - font_output = StixFonts(prop, backend) - elif fontset == 'stixsans': - font_output = StixFonts(prop, backend, sans=True) - elif fontset == 'custom': - font_output = UnicodeFonts(prop, backend) + fontset_class = self._font_type_mapping.get(fontset) + if fontset_class is not None: + font_output = fontset_class(prop, backend) else: - raise ValueError("mathtext.fontset must be either 'cm', 'stix', 'stixsans', or 'custom'") + raise ValueError( + "mathtext.fontset must be either 'cm', 'stix', " + "'stixsans', or 'custom'") fontsize = prop.get_size_in_points() @@ -2648,7 +2669,7 @@ # with each request. if self._parser is None: self.__class__._parser = Parser() - + box = self._parser.parse(s, font_output, fontsize, dpi) font_output.set_canvas_size(box.width, box.height, box.depth) result = font_output.get_results(box) @@ -2660,5 +2681,5 @@ font_output.destroy() font_output.mathtext_backend.fonts_object = None font_output.mathtext_backend = None - + return result This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |