Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
size = self.points_to_pixels(points)
width, height, descent = self.gc.get_text_width_height_descent(
six.text_type(s), family, size, weight, style)
return width, height, 0.0*descent
return width, height, descent

def flipy(self):
return False
Expand Down
40 changes: 22 additions & 18 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#ifndef COMPILING_FOR_10_5
static int ngc = 0; /* The number of graphics contexts in use */

#include <Carbon/Carbon.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this put a min OSX version constraint in? I don't follow mac at all, but have a vague notion that carbon is 'new'.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Carbon is the old api. Cocoa is the new one https://en.wikipedia.org/wiki/Carbon_(API) so I don't think so


/* For drawing Unicode strings with ATSUI */
static ATSUStyle style = NULL;
Expand Down Expand Up @@ -2620,7 +2621,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
#endif
CFRelease(string);
}
if (font == NULL)
if (!font)
{
PyErr_SetString(PyExc_ValueError, "Could not load font");
}
Expand Down Expand Up @@ -2761,11 +2762,13 @@ static CGFloat _get_device_scale(CGContextRef cr)
const UniChar* text;
#endif

CGFloat ascent;
CGFloat descent;
double width;
float descent;
float width;
float height;

CGRect rect;

CGPoint point;
CTFontRef font;

CGContextRef cr = self->cr;
Expand Down Expand Up @@ -2830,12 +2833,15 @@ static CGFloat _get_device_scale(CGContextRef cr)
return NULL;
}

width = CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
point = CGContextGetTextPosition(cr);
rect = CTLineGetImageBounds(line, cr);

CFRelease(line);

return Py_BuildValue("fff", width, rect.size.height, descent);
width = rect.size.width;
height = rect.size.height;
descent = point.y - rect.origin.y;

return Py_BuildValue("fff", width, height, descent);
}

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

ATSFontRef atsfont;
Rect rect;

CGContextRef cr = self->cr;
if (!cr)
Expand Down Expand Up @@ -3016,23 +3023,20 @@ static CGFloat _get_device_scale(CGContextRef cr)
return NULL;
}

ATSUTextMeasurement before;
ATSUTextMeasurement after;
ATSUTextMeasurement ascent;
ATSUTextMeasurement descent;
status = ATSUGetUnjustifiedBounds(layout,
kATSUFromTextBeginning, kATSUToTextEnd,
&before, &after, &ascent, &descent);
status = ATSUMeasureTextImage(layout,
kATSUFromTextBeginning, kATSUToTextEnd,
0, 0, &rect);
if (status!=noErr)
{
PyErr_SetString(PyExc_RuntimeError, "ATSUGetUnjustifiedBounds failed");
PyErr_SetString(PyExc_RuntimeError, "ATSUMeasureTextImage failed");
return NULL;
}

const float width = FixedToFloat(after-before);
const float height = FixedToFloat(ascent-descent);
const float width = rect.right-rect.left;
const float height = rect.bottom-rect.top;
const float descent = rect.bottom;

return Py_BuildValue("fff", width, height, FixedToFloat(descent));
return Py_BuildValue("fff", width, height, descent);
}
#endif

Expand Down