Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4ed18ca

Browse files
committed
Merge pull request #2015 from mdboom/usetex-baseline-fixes
Wrong text baseline with usetex.
2 parents b5c340a + 28f33ed commit 4ed18ca

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
153153
ox, oy, width, height, descent, font_image, used_characters = \
154154
self.mathtext_parser.parse(s, self.dpi, prop)
155155

156-
xd = descent * np.sin(angle / (180.0 * np.pi))
157-
yd = descent * np.cos(angle / (180.0 * np.pi))
156+
xd = descent * np.sin(np.deg2rad(angle))
157+
yd = descent * np.cos(np.deg2rad(angle))
158158
x = np.round(x + ox - xd)
159159
y = np.round(y - oy + yd)
160160
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):
232232
Z = texmanager.get_grey(s, size, self.dpi)
233233
Z = np.array(Z * 255.0, np.uint8)
234234

235+
w, h, d = self.get_text_width_height_descent(s, prop, ismath)
236+
xd = d * np.sin(np.deg2rad(angle))
237+
yd = d * np.cos(np.deg2rad(angle))
238+
x = np.round(x - xd)
239+
y = np.round(y + yd)
240+
235241
self._renderer.draw_text_image(Z, x, y, angle, gc)
236242

237243
def get_canvas_width_height(self):

lib/matplotlib/backends/backend_ps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,11 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
666666
corr = 0#w/2*(fontsize-10)/10
667667
if rcParams['text.latex.preview']:
668668
# use baseline alignment!
669-
pos = _nums_to_str(x-corr, y+bl)
669+
pos = _nums_to_str(x-corr, y)
670670
self.psfrag.append(r'\psfrag{%s}[Bl][Bl][1][%f]{\fontsize{%f}{%f}%s}'%(thetext, angle, fontsize, fontsize*1.25, tex))
671671
else:
672672
# stick to the bottom alignment, but this may give incorrect baseline some times.
673-
pos = _nums_to_str(x-corr, y)
673+
pos = _nums_to_str(x-corr, y-bl)
674674
self.psfrag.append(r'\psfrag{%s}[bl][bl][1][%f]{\fontsize{%f}{%f}%s}'%(thetext, angle, fontsize, fontsize*1.25, tex))
675675

676676
ps = """\

lib/matplotlib/dviread.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import matplotlib
2525
import matplotlib.cbook as mpl_cbook
2626
from matplotlib.compat import subprocess
27+
from matplotlib import rcParams
2728
import numpy as np
2829
import struct
2930
import sys
31+
import os
3032

3133
if sys.version_info[0] >= 3:
3234
def ord(x):
@@ -52,6 +54,18 @@ def __init__(self, filename, dpi):
5254
self.dpi = dpi
5355
self.fonts = {}
5456
self.state = _dvistate.pre
57+
self.baseline = self._get_baseline(filename)
58+
59+
def _get_baseline(self, filename):
60+
if rcParams['text.latex.preview']:
61+
base, ext = os.path.splitext(filename)
62+
baseline_filename = base + ".baseline"
63+
if os.path.exists(baseline_filename):
64+
with open(baseline_filename, 'rb') as fd:
65+
l = fd.read().split()
66+
height, depth, width = l
67+
return float(depth)
68+
return None
5569

5670
def __iter__(self):
5771
"""
@@ -105,17 +119,22 @@ def _output(self):
105119
# special case for ease of debugging: output raw dvi coordinates
106120
return mpl_cbook.Bunch(text=self.text, boxes=self.boxes,
107121
width=maxx-minx, height=maxy_pure-miny,
108-
descent=maxy-maxy_pure)
122+
descent=descent)
109123

110124
d = self.dpi / (72.27 * 2**16) # from TeX's "scaled points" to dpi units
111-
text = [ ((x-minx)*d, (maxy-y)*d, f, g, w*d)
125+
if self.baseline is None:
126+
descent = (maxy - maxy_pure) * d
127+
else:
128+
descent = self.baseline
129+
130+
text = [ ((x-minx)*d, (maxy-y)*d - descent, f, g, w*d)
112131
for (x,y,f,g,w) in self.text ]
113-
boxes = [ ((x-minx)*d, (maxy-y)*d, h*d, w*d) for (x,y,h,w) in self.boxes ]
132+
boxes = [ ((x-minx)*d, (maxy-y)*d - descent, h*d, w*d) for (x,y,h,w) in self.boxes ]
114133

115134
return mpl_cbook.Bunch(text=text, boxes=boxes,
116135
width=(maxx-minx)*d,
117136
height=(maxy_pure-miny)*d,
118-
descent=(maxy-maxy_pure)*d)
137+
descent=descent)
119138

120139
def _read(self):
121140
"""
Binary file not shown.

lib/matplotlib/tests/baseline_images/test_text/text_alignment.svg

Lines changed: 3 additions & 3 deletions
Loading

0 commit comments

Comments
 (0)