diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index a28b87720da2..418fc5bb73ab 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -153,8 +153,8 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): ox, oy, width, height, descent, font_image, used_characters = \ self.mathtext_parser.parse(s, self.dpi, prop) - xd = descent * np.sin(angle / (180.0 * np.pi)) - yd = descent * np.cos(angle / (180.0 * np.pi)) + xd = descent * np.sin(np.deg2rad(angle)) + yd = descent * np.cos(np.deg2rad(angle)) x = np.round(x + ox - xd) y = np.round(y - oy + yd) self._renderer.draw_text_image(font_image, x, y + 1, angle, gc) @@ -232,6 +232,12 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): Z = texmanager.get_grey(s, size, self.dpi) Z = np.array(Z * 255.0, np.uint8) + w, h, d = self.get_text_width_height_descent(s, prop, ismath) + xd = d * np.sin(np.deg2rad(angle)) + yd = d * np.cos(np.deg2rad(angle)) + x = np.round(x - xd) + y = np.round(y + yd) + self._renderer.draw_text_image(Z, x, y, angle, gc) def get_canvas_width_height(self): diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 91b4770a9844..ffbea7ef95f8 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -666,11 +666,11 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): corr = 0#w/2*(fontsize-10)/10 if rcParams['text.latex.preview']: # use baseline alignment! - pos = _nums_to_str(x-corr, y+bl) + pos = _nums_to_str(x-corr, y) self.psfrag.append(r'\psfrag{%s}[Bl][Bl][1][%f]{\fontsize{%f}{%f}%s}'%(thetext, angle, fontsize, fontsize*1.25, tex)) else: # stick to the bottom alignment, but this may give incorrect baseline some times. - pos = _nums_to_str(x-corr, y) + pos = _nums_to_str(x-corr, y-bl) self.psfrag.append(r'\psfrag{%s}[bl][bl][1][%f]{\fontsize{%f}{%f}%s}'%(thetext, angle, fontsize, fontsize*1.25, tex)) ps = """\ diff --git a/lib/matplotlib/dviread.py b/lib/matplotlib/dviread.py index c5cee227ea84..3e334635e4c9 100644 --- a/lib/matplotlib/dviread.py +++ b/lib/matplotlib/dviread.py @@ -24,9 +24,11 @@ import matplotlib import matplotlib.cbook as mpl_cbook from matplotlib.compat import subprocess +from matplotlib import rcParams import numpy as np import struct import sys +import os if sys.version_info[0] >= 3: def ord(x): @@ -52,6 +54,18 @@ def __init__(self, filename, dpi): self.dpi = dpi self.fonts = {} self.state = _dvistate.pre + self.baseline = self._get_baseline(filename) + + def _get_baseline(self, filename): + if rcParams['text.latex.preview']: + base, ext = os.path.splitext(filename) + baseline_filename = base + ".baseline" + if os.path.exists(baseline_filename): + with open(baseline_filename, 'rb') as fd: + l = fd.read().split() + height, depth, width = l + return float(depth) + return None def __iter__(self): """ @@ -105,17 +119,22 @@ def _output(self): # special case for ease of debugging: output raw dvi coordinates return mpl_cbook.Bunch(text=self.text, boxes=self.boxes, width=maxx-minx, height=maxy_pure-miny, - descent=maxy-maxy_pure) + descent=descent) d = self.dpi / (72.27 * 2**16) # from TeX's "scaled points" to dpi units - text = [ ((x-minx)*d, (maxy-y)*d, f, g, w*d) + if self.baseline is None: + descent = (maxy - maxy_pure) * d + else: + descent = self.baseline + + text = [ ((x-minx)*d, (maxy-y)*d - descent, f, g, w*d) for (x,y,f,g,w) in self.text ] - boxes = [ ((x-minx)*d, (maxy-y)*d, h*d, w*d) for (x,y,h,w) in self.boxes ] + boxes = [ ((x-minx)*d, (maxy-y)*d - descent, h*d, w*d) for (x,y,h,w) in self.boxes ] return mpl_cbook.Bunch(text=text, boxes=boxes, width=(maxx-minx)*d, height=(maxy_pure-miny)*d, - descent=(maxy-maxy_pure)*d) + descent=descent) def _read(self): """ diff --git a/lib/matplotlib/tests/baseline_images/test_text/text_alignment.pdf b/lib/matplotlib/tests/baseline_images/test_text/text_alignment.pdf index 0aa048676cd3..ba57f505acfa 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_text/text_alignment.pdf and b/lib/matplotlib/tests/baseline_images/test_text/text_alignment.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_text/text_alignment.png b/lib/matplotlib/tests/baseline_images/test_text/text_alignment.png index d2914f939889..692ea126db5b 100644 Binary files a/lib/matplotlib/tests/baseline_images/test_text/text_alignment.png and b/lib/matplotlib/tests/baseline_images/test_text/text_alignment.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_text/text_alignment.svg b/lib/matplotlib/tests/baseline_images/test_text/text_alignment.svg index bce05461ab5e..e2721ec908c5 100644 --- a/lib/matplotlib/tests/baseline_images/test_text/text_alignment.svg +++ b/lib/matplotlib/tests/baseline_images/test_text/text_alignment.svg @@ -5,7 +5,7 @@ @@ -31,12 +31,12 @@ z +L518.4 273.6" style="fill:none;stroke:#0000ff;stroke-linecap:square;"/> +L518.4 158.4" style="fill:none;stroke:#008000;stroke-linecap:square;"/>