From: <md...@us...> - 2007-07-25 15:40:14
|
Revision: 3612 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3612&view=rev Author: mdboom Date: 2007-07-25 08:34:52 -0700 (Wed, 25 Jul 2007) Log Message: ----------- Fix for get_xheight/get_underline_thickness. Update examples to use new math syntax. Modified Paths: -------------- branches/mathtext_mgd/examples/mathtext_demo.py branches/mathtext_mgd/lib/matplotlib/mathtext.py Modified: branches/mathtext_mgd/examples/mathtext_demo.py =================================================================== --- branches/mathtext_mgd/examples/mathtext_demo.py 2007-07-25 14:43:13 UTC (rev 3611) +++ branches/mathtext_mgd/examples/mathtext_demo.py 2007-07-25 15:34:52 UTC (rev 3612) @@ -17,8 +17,8 @@ grid(True) xlabel(r'$\Delta_i^j$', fontsize=20) ylabel(r'$\Delta_{i+1}^j$', fontsize=20) -tex = r'$\cal{R}\prod_{i=\alpha_{i+1}}^\infty a_i\rm{sin}(2 \pi f x_i)$' -text(1, 1.6, tex, fontsize=20) +tex = r'$\mathcal{R}\prod_{i=\alpha_{i+1}}^\infty a_i\sin(2 \pi f x_i)$' +text(1, 1.6, tex, fontsize=20, va='bottom') #title(r'$\Delta_i^j \hspace{0.4} \rm{versus} \hspace{0.4} \Delta_{i+1}^j$', fontsize=20) savefig('mathtext_demo') Modified: branches/mathtext_mgd/lib/matplotlib/mathtext.py =================================================================== --- branches/mathtext_mgd/lib/matplotlib/mathtext.py 2007-07-25 14:43:13 UTC (rev 3611) +++ branches/mathtext_mgd/lib/matplotlib/mathtext.py 2007-07-25 15:34:52 UTC (rev 3612) @@ -699,7 +699,6 @@ basename, font, metrics, symbol_name, num, glyph, offset = \ self._get_info(font, sym, fontsize, dpi) - font.draw_rect(0, 0, self.width - 1, self.height - 1) font.draw_glyph_to_bitmap( int(ox), int(oy - metrics.ymax), glyph) @@ -711,16 +710,16 @@ def get_used_characters(self): return self.used_characters - def get_xheight(self, font, fontsize): + def get_xheight(self, font, fontsize, dpi): basename, cached_font = self._get_font(font) - cached_font.font.set_size(fontsize) + cached_font.font.set_size(fontsize, dpi) pclt = cached_font.font.get_sfnt_table('pclt') xHeight = pclt['xHeight'] / 64.0 return xHeight - def get_underline_thickness(self, font, fontsize): + def get_underline_thickness(self, font, fontsize, dpi): basename, cached_font = self._get_font(font) - cached_font.font.set_size(fontsize) + cached_font.font.set_size(fontsize, dpi) return max(1.0, cached_font.font.underline_thickness / 64.0) def get_kern(self, fontleft, symleft, fontsizeleft, @@ -944,11 +943,11 @@ ps = "%f %f %f %f rectfill" % (x1, self.height - y2, x2 - x1, y2 - y1) self.pswriter.write(ps) - def get_xheight(self, font, fontsize): + def get_xheight(self, font, fontsize, dpi): basename, cached_font = self._get_font(font) return cached_font.get_xheight() * 0.001 * fontsize - def get_underline_thickness(self, font, fontsize): + def get_underline_thickness(self, font, fontsize, dpi): basename, cached_font = self._get_font(font) return cached_font.get_underline_thickness() * 0.001 * fontsize @@ -1367,7 +1366,7 @@ """Convenience class to create a horizontal rule.""" def __init__(self, state): thickness = state.font_output.get_underline_thickness( - state.font, state.fontsize) + state.font, state.fontsize, state.dpi) height = depth = thickness * 0.5 Rule.__init__(self, None, height, depth, state) @@ -1375,7 +1374,7 @@ """Convenience class to create a vertical rule.""" def __init__(self, state): thickness = state.font_output.get_underline_thickness( - state.font, state.fontsize) + state.font, state.fontsize, state.dpi) Rule.__init__(self, thickness, None, None, state) class Glue(Node): @@ -1950,6 +1949,7 @@ return [box] def symbol(self, s, loc, toks): + # print "symbol", toks c = toks[0] if c in self._spaced_symbols: return [Hlist([self._make_space(0.3), @@ -1982,7 +1982,7 @@ assert(len(toks)==1) state = self.get_state() thickness = state.font_output.get_underline_thickness( - state.font, state.fontsize) + state.font, state.fontsize, state.dpi) accent, sym = toks[0] accent = Accent(self._accent_map[accent], self.get_state()) centered = HCentered([accent]) @@ -2067,11 +2067,12 @@ sub = next2 else: raise ParseFatalException("Subscript/superscript string is too long.") - + state = self.get_state() rule_thickness = state.font_output.get_underline_thickness( - state.font, state.fontsize) - xHeight = state.font_output.get_xheight(state.font, state.fontsize) + state.font, state.fontsize, state.dpi) + xHeight = state.font_output.get_xheight( + state.font, state.fontsize, state.dpi) if self.is_overunder(nucleus): vlist = [] @@ -2155,7 +2156,7 @@ cden.hpack(width, 'exactly') state = self.get_state() thickness = state.font_output.get_underline_thickness( - state.font, state.fontsize) + state.font, state.fontsize, state.dpi) space = thickness * 3.0 vlist = Vlist([cnum, FixedGlue(thickness * 2.0), @@ -2243,7 +2244,7 @@ pswriter = list() font_output.set_canvas_size(w, h, pswriter) ship(2, 2, box) - + if self.output == 'SVG': # The empty list at the end is for lines svg_elements = Bunch(svg_glyphs=font_output.svg_glyphs, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <md...@us...> - 2007-07-25 20:19:29
|
Revision: 3614 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3614&view=rev Author: mdboom Date: 2007-07-25 13:19:25 -0700 (Wed, 25 Jul 2007) Log Message: ----------- Support auto-sized delimiters (e.g. \left(\frac{3}{2}\right)). Commit mathtext_examples.py Modified Paths: -------------- branches/mathtext_mgd/lib/matplotlib/mathtext.py Added Paths: ----------- branches/mathtext_mgd/examples/mathtext_examples.py Added: branches/mathtext_mgd/examples/mathtext_examples.py =================================================================== --- branches/mathtext_mgd/examples/mathtext_examples.py (rev 0) +++ branches/mathtext_mgd/examples/mathtext_examples.py 2007-07-25 20:19:25 UTC (rev 3614) @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +import os, sys + +stests = [ + r'Kerning: AVA $AVA$', + r'$x y$', + r'$x+y\ x=y\ x<y\ x:y\ x,y\ x@y$', + r'$100\%y\ x*y\ x/y x\$y$', + r'$x\leftarrow y\ x\forall y$', + r'$x \sf x \bf x {\cal X} \rm x$', + r'$\{ \rm braces \}$', + r'$\left[\left\lfloor\frac{5}{\frac{3}{4}} y\right)\right]$', + r'$\left(x\right)$', + r'$\sin(x)$', + r'$x_2$', + r'$x^2$', + r'$x^2_y$', + r'$x_y^2$', + r'$\prod_{i=\alpha_{i+1}}^\infty$', + r'$x = \frac{x+\frac{5}{2}}{\frac{y+3}{8}}$', + r'$dz/dt \/ = \/ \gamma x^2 \/ + \/ {\rm sin}(2\pi y+\phi)$', + r'Foo: $\alpha_{i+1}^j \/ = \/ {\rm sin}(2\pi f_j t_i) e^{-5 t_i/\tau}$', + r'$\mathcal{R}\prod_{i=\alpha_{i+1}}^\infty a_i \sin(2 \pi f x_i)$', +# r'$\bigodot \bigoplus {\sf R} a_i{\rm sin}(2 \pi f x_i)$', + r'Variable $i$ is good', + r'$\Delta_i^j$', + r'$\Delta^j_{i+1}$', + r'$\ddot{o}\acute{e}\grave{e}\hat{O}\breve{\imath}\tilde{n}\vec{q}$', + r'$_i$', + r"$\arccos((x^i))$", + r"$\gamma = \frac{x=\frac{6}{8}}{y} \delta$", + r'$\"o\ddot o \'e\`e\~n\.x\^y$', + + ] + +from pylab import * + +if '--latex' in sys.argv: + fd = open("mathtext_examples.ltx", "w") + fd.write("\\documentclass{article}\n") + fd.write("\\begin{document}\n") + fd.write("\\begin{enumerate}\n") + + for i, s in enumerate(stests): + fd.write("\\item %s\n" % s) + + fd.write("\\end{enumerate}\n") + fd.write("\\end{document}\n") + fd.close() + + os.system("pdflatex mathtext_examples.ltx") +else: + for i, s in enumerate(stests): + print s + plot([1,2,3], 'r') + x = arange(0.0, 3.0, 0.1) + + grid(True) + text(1, 1.6, s, fontsize=20) + + savefig('mathtext_example%02d' % i) + figure() + Property changes on: branches/mathtext_mgd/examples/mathtext_examples.py ___________________________________________________________________ Name: svn:executable + * Modified: branches/mathtext_mgd/lib/matplotlib/mathtext.py =================================================================== --- branches/mathtext_mgd/lib/matplotlib/mathtext.py 2007-07-25 16:17:22 UTC (rev 3613) +++ branches/mathtext_mgd/lib/matplotlib/mathtext.py 2007-07-25 20:19:25 UTC (rev 3614) @@ -739,12 +739,6 @@ Use the Bakoma postscript fonts for rendering to backend_ps """ - def _get_offset(self, basename, cached_font, glyph, fontsize, dpi): - head = cached_font.font.get_sfnt_table("head") - if basename.lower() == 'cmex10': - return -(head['yMin']+512)/head['unitsPerEm']*10. - return 0. - def set_canvas_size(self, w, h, pswriter): 'Dimension the drawing canvas; may be a noop' self.width = w @@ -756,7 +750,7 @@ self._get_info(font, sym, fontsize, dpi) oy = self.height - oy if basename.lower() == 'cmex10': - oy += offset - 512/2048.*10. + oy += offset ps = """/%(basename)s findfont %(fontsize)s scalefont @@ -1067,7 +1061,7 @@ self._update_metrics() def __internal_repr__(self): - return repr(self.c) + return '`%s`' % self.c def _update_metrics(self): metrics = self._metrics = self.font_output.get_metrics( @@ -1097,7 +1091,7 @@ if self.size < NUM_SIZE_LEVELS: self.fontsize *= SHRINK_FACTOR self._update_metrics() - + 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.""" @@ -1305,7 +1299,7 @@ p = self.list_head while p is not None: if isinstance(p, Char): - raise RuntimeError("Internal error in mathtext") + raise RuntimeError("Internal mathtext error: Char node found in Vlist.") elif isinstance(p, Box): x += d + p.height d = p.depth @@ -1504,6 +1498,19 @@ self.super = None Hlist.__init__(self, []) +class AutoSizedDelim(Hlist): + def __init__(self, c, height, depth, state): + char = Char(c, state) + shift = 0. + if char.height + char.depth < height + depth: + factor = (height + depth) / (char.height + char.depth) + fontsize = char.fontsize * factor + state = state.copy() + state.fontsize = fontsize + char = Char(c, state) + shift = (depth - char.depth) + Hlist.__init__(self, [char]) + self.shift_amount = shift class Ship(object): """Once the boxes have been set up, this sends them to output. @@ -1612,7 +1619,7 @@ while p: if isinstance(p, Char): - raise RuntimeError("Internal error in mathtext") + raise RuntimeError("Internal mathtext error: Char node found in vlist") elif isinstance(p, List): if p.list_head is None: self.cur_v += p.height + p.depth @@ -1663,7 +1670,7 @@ ############################################################################## # PARSER -class Parser: +class Parser(object): _binary_operators = Set(r''' + - * \pm \sqcap \rhd @@ -1707,7 +1714,7 @@ _spaced_symbols = _binary_operators | _relation_symbols | _arrow_symbols - _delimiter_symbols = Set(r', ; . !'.split()) + _punctuation_symbols = Set(r', ; . !'.split()) def __init__(self): # All forward declarations are here @@ -1716,6 +1723,7 @@ subsuper = Forward().setParseAction(self.subsuperscript).setName("subsuper") placeable = Forward().setName("placeable") simple = Forward().setName("simple") + autoDelim = Forward().setParseAction(self.auto_sized_delimiter) self._expression = Forward().setParseAction(self.finish).setName("finish") lbrace = Literal('{').suppress() @@ -1724,42 +1732,9 @@ start_group.setParseAction(self.start_group) end_group = rbrace end_group.setParseAction(self.end_group) - lbrack = Literal('[') - rbrack = Literal(']') - lparen = Literal('(') - rparen = Literal(')') - grouping =(lbrack - | rbrack - | lparen - | rparen) bslash = Literal('\\') - langle = Literal('<') - rangle = Literal('>') - equals = Literal('=') - relation =(langle - | rangle - | equals) - - colon = Literal(':') - comma = Literal(',') - period = Literal('.') - semicolon = Literal(';') - exclamation = Literal('!') - punctuation =(colon - | comma - | period - | semicolon) - - at = Literal('@') - percent = Literal('%') - ampersand = Literal('&') - misc =(exclamation - | at - | percent - | ampersand) - accent = oneOf("hat check dot breve acute ddot grave tilde bar vec " "\" ` ' ~ . ^") @@ -1794,7 +1769,7 @@ symbol = Regex("(" + ")|(".join( [ - r"\\[a-zA-Z0-9]+(?!{)", + r"\\(?!right)(?!left)[a-zA-Z0-9]+(?!{)", r"[a-zA-Z0-9 ]", r"[+\-*/]", r"[<>=]", @@ -1845,7 +1820,8 @@ simple <<(space | font - | subsuper) + | subsuper + ) subsuperop =(Literal("_") | Literal("^") @@ -1861,8 +1837,22 @@ | placeable ) + ambiDelim = oneOf(r"| \| / \backslash \uparrow \downarrow \updownarrow \Uparrow \Downarrow \Updownarrow") + leftDelim = oneOf(r"( [ { \lfloor \langle \lceil") + rightDelim = oneOf(r") ] } \rfloot \rangle \rceil") + + autoDelim <<(Suppress(Literal(r"\left")) + + (leftDelim | ambiDelim) + + Group( + autoDelim + ^ OneOrMore(simple)) + + Suppress(Literal(r"\right")) + + (rightDelim | ambiDelim) + ) + math = OneOrMore( - simple + autoDelim + | simple ).setParseAction(self.math).setName("math") math_delim =(~bslash @@ -1955,7 +1945,7 @@ return [Hlist([self._make_space(0.3), Char(c, self.get_state()), self._make_space(0.3)])] - elif c in self._delimiter_symbols: + elif c in self._punctuation_symbols: return [Hlist([Char(c, self.get_state()), self._make_space(0.3)])] return [Char(toks[0], self.get_state())] @@ -2173,8 +2163,17 @@ hlist = Hlist([vlist, FixedGlue(thickness * 2.)]) return [hlist] + def auto_sized_delimiter(self, s, loc, toks): + front, middle, back = toks + state = self.get_state() + height = max([x.height for x in middle]) + depth = max([x.depth for x in middle]) + hlist = Hlist( + [AutoSizedDelim(front, height, depth, state)] + + middle.asList() + + [AutoSizedDelim(back, height, depth, state)]) + return hlist - #### ############################################################################## This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <md...@us...> - 2007-07-26 13:26:29
|
Revision: 3615 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3615&view=rev Author: mdboom Date: 2007-07-26 06:26:26 -0700 (Thu, 26 Jul 2007) Log Message: ----------- Important bugfix (many expressions were silently failing) Modified Paths: -------------- branches/mathtext_mgd/examples/mathtext_examples.py branches/mathtext_mgd/lib/matplotlib/_mathtext_data.py branches/mathtext_mgd/lib/matplotlib/mathtext.py Modified: branches/mathtext_mgd/examples/mathtext_examples.py =================================================================== --- branches/mathtext_mgd/examples/mathtext_examples.py 2007-07-25 20:19:25 UTC (rev 3614) +++ branches/mathtext_mgd/examples/mathtext_examples.py 2007-07-26 13:26:26 UTC (rev 3615) @@ -52,7 +52,7 @@ os.system("pdflatex mathtext_examples.ltx") else: for i, s in enumerate(stests): - print s + print "%02d: %s" % (i, s) plot([1,2,3], 'r') x = arange(0.0, 3.0, 0.1) Modified: branches/mathtext_mgd/lib/matplotlib/_mathtext_data.py =================================================================== --- branches/mathtext_mgd/lib/matplotlib/_mathtext_data.py 2007-07-25 20:19:25 UTC (rev 3614) +++ branches/mathtext_mgd/lib/matplotlib/_mathtext_data.py 2007-07-26 13:26:26 UTC (rev 3615) @@ -38,8 +38,10 @@ r'\SQRT' : ('cmex10', 53), r'\leftbrace' : ('cmex10', 92), r'{' : ('cmex10', 92), + r'\{' : ('cmex10', 92), r'\rightbrace' : ('cmex10', 130), r'}' : ('cmex10', 130), + r'\}' : ('cmex10', 130), r'\leftangle' : ('cmex10', 97), r'\rightangle' : ('cmex10', 64), r'\Leftparen' : ('cmex10', 112), Modified: branches/mathtext_mgd/lib/matplotlib/mathtext.py =================================================================== --- branches/mathtext_mgd/lib/matplotlib/mathtext.py 2007-07-25 20:19:25 UTC (rev 3614) +++ branches/mathtext_mgd/lib/matplotlib/mathtext.py 2007-07-26 13:26:26 UTC (rev 3615) @@ -59,17 +59,10 @@ ^ use raw strings - The $ symbols must be the first and last symbols in the string. Eg, - you cannot do + Math and non-math can be interpresed in the same string. E.g., r'My label $x_i$'. - but you can change fonts, as in - - r'$\rm{My label} x_i$' - - to achieve the same effect. - A large set of the TeX symbols are provided. Subscripting and superscripting are supported, as well as the over/under style of subscripting with \sum, \int, etc. @@ -1714,7 +1707,7 @@ _spaced_symbols = _binary_operators | _relation_symbols | _arrow_symbols - _punctuation_symbols = Set(r', ; . !'.split()) + _punctuation_symbols = Set(r', ; . ! \ldotp \cdotp'.split()) def __init__(self): # All forward declarations are here @@ -1769,15 +1762,14 @@ symbol = Regex("(" + ")|(".join( [ - r"\\(?!right)(?!left)[a-zA-Z0-9]+(?!{)", + r"\\(?!left[^a-z])(?!right[^a-z])[a-zA-Z0-9]+(?!{)", r"[a-zA-Z0-9 ]", r"[+\-*/]", r"[<>=]", r"[:,.;!]", r"[!@%&]", r"[[\]()]", - r"\\\$", - r"\\\%" + r"\\[$%{}]", ]) + ")" ).setParseAction(self.symbol).leaveWhitespace() @@ -1828,7 +1820,7 @@ ) subsuper << Group( - ( placeable + ( Optional(placeable) + OneOrMore( subsuperop + placeable @@ -1837,7 +1829,9 @@ | placeable ) - ambiDelim = oneOf(r"| \| / \backslash \uparrow \downarrow \updownarrow \Uparrow \Downarrow \Updownarrow") + ambiDelim = oneOf(r"""| \| / \backslash \uparrow \downarrow + \updownarrow \Uparrow \Downarrow + \Updownarrow""") leftDelim = oneOf(r"( [ { \lfloor \langle \lceil") rightDelim = oneOf(r") ] } \rfloot \rangle \rceil") @@ -1861,9 +1855,9 @@ non_math = Regex(r"(?:[^$]|(?:\\\$))*" ).setParseAction(self.non_math).setName("non_math").leaveWhitespace() - self._expression <<( + self._expression <<( non_math - + ZeroOrMore( + + OneOrMore( Suppress(math_delim) + math + Suppress(math_delim) @@ -1908,6 +1902,7 @@ return [self._expr] def math(self, s, loc, toks): + #~ print "math", toks hlist = Hlist(toks) self.pop_state() return [hlist] @@ -2164,6 +2159,7 @@ return [hlist] def auto_sized_delimiter(self, s, loc, toks): + #~ print "auto_sized_delimiter", toks front, middle, back = toks state = self.get_state() height = max([x.height for x in middle]) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <md...@us...> - 2007-07-26 13:46:59
|
Revision: 3616 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3616&view=rev Author: mdboom Date: 2007-07-26 06:46:56 -0700 (Thu, 26 Jul 2007) Log Message: ----------- Committing in preparation for merge Modified Paths: -------------- branches/mathtext_mgd/examples/mathtext_examples.py branches/mathtext_mgd/lib/matplotlib/mathtext.py Modified: branches/mathtext_mgd/examples/mathtext_examples.py =================================================================== --- branches/mathtext_mgd/examples/mathtext_examples.py 2007-07-26 13:26:26 UTC (rev 3615) +++ branches/mathtext_mgd/examples/mathtext_examples.py 2007-07-26 13:46:56 UTC (rev 3616) @@ -10,7 +10,7 @@ r'$x\leftarrow y\ x\forall y$', r'$x \sf x \bf x {\cal X} \rm x$', r'$\{ \rm braces \}$', - r'$\left[\left\lfloor\frac{5}{\frac{3}{4}} y\right)\right]$', + r'$\left[\left\lfloor\frac{5}{\frac{\left(3\right)}{4}} y\right)\right]$', r'$\left(x\right)$', r'$\sin(x)$', r'$x_2$', Modified: branches/mathtext_mgd/lib/matplotlib/mathtext.py =================================================================== --- branches/mathtext_mgd/lib/matplotlib/mathtext.py 2007-07-26 13:26:26 UTC (rev 3615) +++ branches/mathtext_mgd/lib/matplotlib/mathtext.py 2007-07-26 13:46:56 UTC (rev 3616) @@ -1728,24 +1728,16 @@ bslash = Literal('\\') - accent = oneOf("hat check dot breve acute ddot grave tilde bar vec " - "\" ` ' ~ . ^") + accent = oneOf("hat check dot breve acute ddot grave tilde bar " + "vec \" ` ' ~ . ^") - function = oneOf("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 coth inf max tanh") + function = oneOf("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 coth inf max " + "tanh") number = Combine(Word(nums) + Optional(Literal('.')) + Optional( Word(nums) )) - plus = Literal('+') - minus = Literal('-') - times = Literal('*') - div = Literal('/') - binop =(plus - | minus - | times - | div) - fontname = oneOf("rm cal it tt sf bf") latex2efont = oneOf("mathrm mathcal mathit mathtt mathsf mathbf") @@ -1784,7 +1776,9 @@ group = Group( start_group - + OneOrMore(simple) + + OneOrMore( + autoDelim + | simple) + end_group ).setParseAction(self.group).setName("group") @@ -1833,8 +1827,7 @@ \updownarrow \Uparrow \Downarrow \Updownarrow""") leftDelim = oneOf(r"( [ { \lfloor \langle \lceil") - rightDelim = oneOf(r") ] } \rfloot \rangle \rceil") - + rightDelim = oneOf(r") ] } \rfloor \rangle \rceil") autoDelim <<(Suppress(Literal(r"\left")) + (leftDelim | ambiDelim) + Group( This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |