Skip to content

Commit

Permalink
Fix Issue matplotlib#2060: Text bbox alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed May 28, 2013
1 parent 2af443e commit fc41bb9
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 169 deletions.
5 changes: 1 addition & 4 deletions lib/matplotlib/offsetbox.py
Expand Up @@ -718,13 +718,10 @@ def get_extent(self, renderer):
_, h_, d_ = renderer.get_text_width_height_descent(
"lp", self._text._fontproperties, ismath=False)

bbox, info = self._text._get_layout(renderer)
bbox, info, d = self._text._get_layout(renderer)
w, h = bbox.width, bbox.height

line = info[-1][0] # last line
_, hh, dd = renderer.get_text_width_height_descent(
line, self._text._fontproperties, ismath=ismath)
d = dd # the baseline of the last line

self._baseline_transform.clear()

Expand Down
Binary file modified lib/matplotlib/tests/baseline_images/test_legend/fancy.pdf
Binary file not shown.
Binary file modified lib/matplotlib/tests/baseline_images/test_legend/fancy.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
306 changes: 153 additions & 153 deletions lib/matplotlib/tests/baseline_images/test_legend/fancy.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
112 changes: 112 additions & 0 deletions lib/matplotlib/tests/baseline_images/test_text/text_alignment.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion lib/matplotlib/tests/test_text.py
Expand Up @@ -166,7 +166,8 @@ def test_alignment():
x = 0.1
for rotation in (0, 30):
for alignment in ('top', 'bottom', 'baseline', 'center'):
ax.text(x, 0.5, alignment + " Tj", va=alignment, rotation=rotation)
ax.text(x, 0.5, alignment + " Tj", va=alignment, rotation=rotation,
bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))
ax.text(x, 1.0, r'$\sum_{i=0}^{j}$', va=alignment, rotation=rotation)
x += 0.1

Expand Down
21 changes: 10 additions & 11 deletions lib/matplotlib/text.py
Expand Up @@ -118,10 +118,12 @@ def _get_textbox(text, renderer):
theta = np.deg2rad(text.get_rotation())
tr = mtransforms.Affine2D().rotate(-theta)

for t, wh, x, y in text._get_layout(renderer)[1]:
_, parts, d = text._get_layout(renderer)

for t, wh, x, y in parts:
w, h = wh

xt1, yt1 = tr.transform_point((x, y))
xt1, yt1 = tr.transform_point((x, y - d))

This comment has been minimized.

Copy link
@leejjoon

leejjoon May 29, 2013

d needs to be subtracted after the transform.

xt1, yt1 = tr.transform_point((x, y))
yt1 -= d

Currently, the frame is still wrong if we use something angle=90.

xt2, yt2 = xt1 + w, yt1 + h

projected_xs.extend([xt1, xt2])
Expand Down Expand Up @@ -443,7 +445,7 @@ def get_text_width_height_descent(*kl, **kwargs):

xs, ys = xys[:, 0], xys[:, 1]

ret = bbox, zip(lines, whs, xs, ys)
ret = bbox, zip(lines, whs, xs, ys), d
self.cached[key] = ret
return ret

Expand Down Expand Up @@ -509,8 +511,7 @@ def update_bbox_position_size(self, renderer):
posx, posy = trans.transform_point((posx, posy))

x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
self._bbox_patch.set_bounds(0., 0.,
w_box, h_box)
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
theta = np.deg2rad(self.get_rotation())
tr = mtransforms.Affine2D().rotate(theta)
tr = tr.translate(posx + x_box, posy + y_box)
Expand All @@ -526,8 +527,7 @@ def _draw_bbox(self, renderer, posx, posy):
"""

x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
self._bbox_patch.set_bounds(0., 0.,
w_box, h_box)
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
theta = np.deg2rad(self.get_rotation())
tr = mtransforms.Affine2D().rotate(theta)
tr = tr.translate(posx + x_box, posy + y_box)
Expand All @@ -550,7 +550,7 @@ def draw(self, renderer):

renderer.open_group('text', self.get_gid())

bbox, info = self._get_layout(renderer)
bbox, info, descent = self._get_layout(renderer)
trans = self.get_transform()

# don't use self.get_position here, which refers to text position
Expand Down Expand Up @@ -768,7 +768,7 @@ def get_window_extent(self, renderer=None, dpi=None):
if self._renderer is None:
raise RuntimeError('Cannot get window extent w/o renderer')

bbox, info = self._get_layout(self._renderer)
bbox, info, descent = self._get_layout(self._renderer)
x, y = self.get_position()
x, y = self.get_transform().transform_point((x, y))
bbox = bbox.translated(x, y)
Expand Down Expand Up @@ -1997,8 +1997,7 @@ def update_bbox_position_size(self, renderer):
posx, posy = self._x, self._y

x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
self._bbox_patch.set_bounds(0., 0.,
w_box, h_box)
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
theta = np.deg2rad(self.get_rotation())
tr = mtransforms.Affine2D().rotate(theta)
tr = tr.translate(posx + x_box, posy + y_box)
Expand Down

0 comments on commit fc41bb9

Please sign in to comment.