diff --git a/src/_macosx.m b/src/_macosx.m index 7fd5f9afa4aa..568c9650e932 100644 --- a/src/_macosx.m +++ b/src/_macosx.m @@ -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) @@ -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; @@ -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]; @@ -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, @@ -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); @@ -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); @@ -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; @@ -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); @@ -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); @@ -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) @@ -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; }