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

Skip to content

Commit f3e854e

Browse files
committed
Calculate text size and descent correctly
1 parent 8db6f68 commit f3e854e

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

lib/matplotlib/backends/backend_macosx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
172172
size = self.points_to_pixels(points)
173173
width, height, descent = self.gc.get_text_width_height_descent(
174174
six.text_type(s), family, size, weight, style)
175-
return width, height, 0.0*descent
175+
return width, height, descent
176176

177177
def flipy(self):
178178
return False

src/_macosx.m

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#ifndef COMPILING_FOR_10_5
4848
static int ngc = 0; /* The number of graphics contexts in use */
4949

50+
#include <Carbon/Carbon.h>
5051

5152
/* For drawing Unicode strings with ATSUI */
5253
static ATSUStyle style = NULL;
@@ -2620,7 +2621,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
26202621
#endif
26212622
CFRelease(string);
26222623
}
2623-
if (font == NULL)
2624+
if (!font)
26242625
{
26252626
PyErr_SetString(PyExc_ValueError, "Could not load font");
26262627
}
@@ -2761,11 +2762,13 @@ static CGFloat _get_device_scale(CGContextRef cr)
27612762
const UniChar* text;
27622763
#endif
27632764

2764-
CGFloat ascent;
2765-
CGFloat descent;
2766-
double width;
2765+
float descent;
2766+
float width;
2767+
float height;
2768+
27672769
CGRect rect;
27682770

2771+
CGPoint point;
27692772
CTFontRef font;
27702773

27712774
CGContextRef cr = self->cr;
@@ -2830,12 +2833,15 @@ static CGFloat _get_device_scale(CGContextRef cr)
28302833
return NULL;
28312834
}
28322835

2833-
width = CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
2836+
point = CGContextGetTextPosition(cr);
28342837
rect = CTLineGetImageBounds(line, cr);
2835-
28362838
CFRelease(line);
28372839

2838-
return Py_BuildValue("fff", width, rect.size.height, descent);
2840+
width = rect.size.width;
2841+
height = rect.size.height;
2842+
descent = point.y - rect.origin.y;
2843+
2844+
return Py_BuildValue("fff", width, height, descent);
28392845
}
28402846

28412847
#else // Text drawing for OSX versions <10.5
@@ -2948,6 +2954,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
29482954
const char* italic;
29492955

29502956
ATSFontRef atsfont;
2957+
Rect rect;
29512958

29522959
CGContextRef cr = self->cr;
29532960
if (!cr)
@@ -3016,23 +3023,20 @@ static CGFloat _get_device_scale(CGContextRef cr)
30163023
return NULL;
30173024
}
30183025

3019-
ATSUTextMeasurement before;
3020-
ATSUTextMeasurement after;
3021-
ATSUTextMeasurement ascent;
3022-
ATSUTextMeasurement descent;
3023-
status = ATSUGetUnjustifiedBounds(layout,
3024-
kATSUFromTextBeginning, kATSUToTextEnd,
3025-
&before, &after, &ascent, &descent);
3026+
status = ATSUMeasureTextImage(layout,
3027+
kATSUFromTextBeginning, kATSUToTextEnd,
3028+
0, 0, &rect);
30263029
if (status!=noErr)
30273030
{
3028-
PyErr_SetString(PyExc_RuntimeError, "ATSUGetUnjustifiedBounds failed");
3031+
PyErr_SetString(PyExc_RuntimeError, "ATSUMeasureTextImage failed");
30293032
return NULL;
30303033
}
30313034

3032-
const float width = FixedToFloat(after-before);
3033-
const float height = FixedToFloat(ascent-descent);
3035+
const float width = rect.right-rect.left;
3036+
const float height = rect.bottom-rect.top;
3037+
const float descent = rect.bottom;
30343038

3035-
return Py_BuildValue("fff", width, height, FixedToFloat(descent));
3039+
return Py_BuildValue("fff", width, height, descent);
30363040
}
30373041
#endif
30383042

0 commit comments

Comments
 (0)