Skip to content
Merged
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
65 changes: 55 additions & 10 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#define PY3K 0
#endif

#if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >=3)
#define PY33 1
#else
#define PY33 0
#endif

/* Must define Py_TYPE for Python 2.5 or older */
#ifndef Py_TYPE
# define Py_TYPE(o) ((o)->ob_type)
Expand Down Expand Up @@ -2536,7 +2542,6 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
{
float x;
float y;
const UniChar* text;
int n;
PyObject* family;
float size;
Expand All @@ -2546,6 +2551,11 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
CTFontRef font;
CGColorRef color;
CGFloat descent;
#if PY33
const char* text;
#else
const UniChar* text;
#endif

CFStringRef keys[2];
CFTypeRef values[2];
Expand All @@ -2556,7 +2566,19 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
PyErr_SetString(PyExc_RuntimeError, "CGContextRef is NULL");
return NULL;
}

#if PY33
if(!PyArg_ParseTuple(args, "ffs#Ofssf",
&x,
&y,
&text,
&n,
&family,
&size,
&weight,
&italic,
&angle)) return NULL;
CFStringRef s = CFStringCreateWithCString(kCFAllocatorDefault, text, kCFStringEncodingUTF8);
#else
if(!PyArg_ParseTuple(args, "ffu#Ofssf",
&x,
&y,
Expand All @@ -2567,6 +2589,8 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
&weight,
&italic,
&angle)) return NULL;
CFStringRef s = CFStringCreateWithCharacters(kCFAllocatorDefault, text, n);
#endif

font = setfont(cr, family, size, weight, italic);

Expand All @@ -2588,8 +2612,6 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
CGColorRelease(color);
CFRelease(font);

CFStringRef s = CFStringCreateWithCharacters(kCFAllocatorDefault, text, n);

CFAttributedStringRef string = CFAttributedStringCreate(kCFAllocatorDefault,
s,
attributes);
Expand Down Expand Up @@ -2631,13 +2653,18 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
static PyObject*
GraphicsContext_get_text_width_height_descent(GraphicsContext* self, PyObject* args)
{
const UniChar* text;
int n;
PyObject* family;
float size;
const char* weight;
const char* italic;

#if PY33
const char* text;
#else
const UniChar* text;
#endif

CGFloat ascent;
CGFloat descent;
double width;
Expand All @@ -2652,9 +2679,25 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
return NULL;
}

#if PY33
if(!PyArg_ParseTuple(args, "s#Ofss",
&text,
&n,
&family,
&size,
&weight,
&italic)) return NULL;
CFStringRef s = CFStringCreateWithCString(kCFAllocatorDefault, text, kCFStringEncodingUTF8);
#else
if(!PyArg_ParseTuple(args, "u#Ofss",
&text, &n, &family, &size, &weight, &italic))
return NULL;
&text,
&n,
&family,
&size,
&weight,
&italic)) return NULL;
CFStringRef s = CFStringCreateWithCharacters(kCFAllocatorDefault, text, n);
#endif

font = setfont(cr, family, size, weight, italic);

Expand All @@ -2671,8 +2714,6 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
&kCFTypeDictionaryValueCallBacks);
CFRelease(font);

CFStringRef s = CFStringCreateWithCharacters(kCFAllocatorDefault, text, n);

CFAttributedStringRef string = CFAttributedStringCreate(kCFAllocatorDefault,
s,
attributes);
Expand All @@ -2697,7 +2738,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
return Py_BuildValue("fff", width, rect.size.height, descent);
}

#else
#else // Text drawing for OSX versions <10.5

static PyObject*
GraphicsContext_draw_text (GraphicsContext* self, PyObject* args)
Expand Down Expand Up @@ -4900,7 +4941,11 @@ -(void)save_figure:(id)sender
unsigned int n = [filename length];
unichar* buffer = malloc(n*sizeof(unichar));
[filename getCharacters: buffer];
#if PY33
PyObject* string = PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, buffer, n);
#else
PyObject* string = PyUnicode_FromUnicode(buffer, n);
#endif
free(buffer);
return string;
}
Expand Down