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

Skip to content

Commit 047d077

Browse files
committed
Fix text alignment in PDF backend. (At suggestion of Stan West).
svn path=/trunk/matplotlib/; revision=6296
1 parent 839434c commit 047d077

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

lib/matplotlib/backends/backend_pdf.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,14 +1268,15 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
12681268
lastx, lasty = x, y
12691269
output(Op.grestore)
12701270

1271-
def _setup_textpos(self, x, y, angle, oldx=0, oldy=0, oldangle=0):
1271+
def _setup_textpos(self, x, y, descent, angle, oldx=0, oldy=0, olddescent=0, oldangle=0):
12721272
if angle == oldangle == 0:
1273-
self.file.output(x - oldx, y - oldy, Op.textpos)
1273+
self.file.output(x - oldx, (y + descent) - (oldy + olddescent), Op.textpos)
12741274
else:
12751275
angle = angle / 180.0 * pi
12761276
self.file.output( cos(angle), sin(angle),
12771277
-sin(angle), cos(angle),
12781278
x, y, Op.textmatrix)
1279+
self.file.output(0, descent, Op.textpos)
12791280

12801281
def draw_mathtext(self, gc, x, y, s, prop, angle):
12811282
# TODO: fix positioning and encoding
@@ -1305,7 +1306,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
13051306
fonttype = global_fonttype
13061307

13071308
if fonttype == 42 or num <= 255:
1308-
self._setup_textpos(ox, oy, 0, oldx, oldy)
1309+
self._setup_textpos(ox, oy, 0, 0, oldx, oldy)
13091310
oldx, oldy = ox, oy
13101311
if (fontname, fontsize) != prev_font:
13111312
fontsize *= self.dpi/72.0
@@ -1398,7 +1399,7 @@ def draw_tex(self, gc, x, y, s, prop, angle):
13981399
self.file.output(elt[1], elt[2], Op.selectfont)
13991400
elif elt[0] == 'text':
14001401
curx, cury = mytrans.transform((elt[1], elt[2]))
1401-
self._setup_textpos(curx, cury, angle, oldx, oldy)
1402+
self._setup_textpos(curx, cury, 0, angle, oldx, oldy)
14021403
oldx, oldy = curx, cury
14031404
if len(elt[3]) == 1:
14041405
self.file.output(elt[3][0], Op.show)
@@ -1447,13 +1448,13 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
14471448
if rcParams['pdf.use14corefonts']:
14481449
font = self._get_font_afm(prop)
14491450
l, b, w, h = font.get_str_bbox(s)
1450-
y -= b * fontsize / 1000
1451+
descent = -b * fontsize / 1000
14511452
fonttype = 42
14521453
else:
14531454
font = self._get_font_ttf(prop)
14541455
self.track_characters(font, s)
14551456
font.set_text(s, 0.0, flags=LOAD_NO_HINTING)
1456-
y += font.get_descent() / 64.0
1457+
descent = font.get_descent() / 64.0
14571458

14581459
fonttype = rcParams['pdf.fonttype']
14591460

@@ -1493,7 +1494,7 @@ def draw_text_simple():
14931494
self.file.fontName(prop),
14941495
fontsize,
14951496
Op.selectfont)
1496-
self._setup_textpos(x, y, angle)
1497+
self._setup_textpos(x, y, descent, angle)
14971498
self.file.output(self.encode_string(s, fonttype), Op.show, Op.end_text)
14981499

14991500
def draw_text_woven(chunks):
@@ -1514,6 +1515,7 @@ def draw_text_woven(chunks):
15141515
# output all the 2-byte characters.
15151516
for mode in (1, 2):
15161517
newx = oldx = 0
1518+
olddescent = 0
15171519
# Output a 1-byte character chunk
15181520
if mode == 1:
15191521
self.file.output(Op.begin_text,
@@ -1523,9 +1525,10 @@ def draw_text_woven(chunks):
15231525

15241526
for chunk_type, chunk in chunks:
15251527
if mode == 1 and chunk_type == 1:
1526-
self._setup_textpos(newx, 0, 0, oldx, 0, 0)
1528+
self._setup_textpos(newx, 0, descent, 0, oldx, 0, olddescent, 0)
15271529
self.file.output(self.encode_string(chunk, fonttype), Op.show)
15281530
oldx = newx
1531+
olddescent = descent
15291532

15301533
lastgind = None
15311534
for c in chunk:

0 commit comments

Comments
 (0)