1111#define PY3K 0
1212#endif
1313
14+ #if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >=3)
15+ #define PY33 1
16+ #else
17+ #define PY33 0
18+ #endif
19+
1420/* Must define Py_TYPE for Python 2.5 or older */
1521#ifndef Py_TYPE
1622# define Py_TYPE (o ) ((o)->ob_type)
@@ -2584,7 +2590,6 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
25842590{
25852591 float x;
25862592 float y;
2587- const UniChar* text;
25882593 int n;
25892594 PyObject* family;
25902595 float size;
@@ -2594,6 +2599,11 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
25942599 CTFontRef font;
25952600 CGColorRef color;
25962601 CGFloat descent;
2602+ #if PY33
2603+ const char * text;
2604+ #else
2605+ const UniChar* text;
2606+ #endif
25972607
25982608 CFStringRef keys[2 ];
25992609 CFTypeRef values[2 ];
@@ -2604,7 +2614,19 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
26042614 PyErr_SetString (PyExc_RuntimeError, " CGContextRef is NULL" );
26052615 return NULL ;
26062616 }
2607-
2617+ #if PY33
2618+ if (!PyArg_ParseTuple (args, " ffs#Ofssf" ,
2619+ &x,
2620+ &y,
2621+ &text,
2622+ &n,
2623+ &family,
2624+ &size,
2625+ &weight,
2626+ &italic,
2627+ &angle)) return NULL ;
2628+ CFStringRef s = CFStringCreateWithCString (kCFAllocatorDefault , text, kCFStringEncodingUTF8 );
2629+ #else
26082630 if (!PyArg_ParseTuple (args, " ffu#Ofssf" ,
26092631 &x,
26102632 &y,
@@ -2615,6 +2637,8 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
26152637 &weight,
26162638 &italic,
26172639 &angle)) return NULL ;
2640+ CFStringRef s = CFStringCreateWithCharacters (kCFAllocatorDefault , text, n);
2641+ #endif
26182642
26192643 font = setfont (cr, family, size, weight, italic);
26202644
@@ -2636,8 +2660,6 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
26362660 CGColorRelease (color);
26372661 CFRelease (font);
26382662
2639- CFStringRef s = CFStringCreateWithCharacters (kCFAllocatorDefault , text, n);
2640-
26412663 CFAttributedStringRef string = CFAttributedStringCreate (kCFAllocatorDefault ,
26422664 s,
26432665 attributes);
@@ -2679,13 +2701,18 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
26792701static PyObject*
26802702GraphicsContext_get_text_width_height_descent (GraphicsContext* self, PyObject* args)
26812703{
2682- const UniChar* text;
26832704 int n;
26842705 PyObject* family;
26852706 float size;
26862707 const char * weight;
26872708 const char * italic;
26882709
2710+ #if PY33
2711+ const char * text;
2712+ #else
2713+ const UniChar* text;
2714+ #endif
2715+
26892716 CGFloat ascent;
26902717 CGFloat descent;
26912718 double width;
@@ -2700,9 +2727,25 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
27002727 return NULL ;
27012728 }
27022729
2730+ #if PY33
2731+ if (!PyArg_ParseTuple (args, " s#Ofss" ,
2732+ &text,
2733+ &n,
2734+ &family,
2735+ &size,
2736+ &weight,
2737+ &italic)) return NULL ;
2738+ CFStringRef s = CFStringCreateWithCString (kCFAllocatorDefault , text, kCFStringEncodingUTF8 );
2739+ #else
27032740 if (!PyArg_ParseTuple (args, " u#Ofss" ,
2704- &text, &n, &family, &size, &weight, &italic))
2705- return NULL ;
2741+ &text,
2742+ &n,
2743+ &family,
2744+ &size,
2745+ &weight,
2746+ &italic)) return NULL ;
2747+ CFStringRef s = CFStringCreateWithCharacters (kCFAllocatorDefault , text, n);
2748+ #endif
27062749
27072750 font = setfont (cr, family, size, weight, italic);
27082751
@@ -2719,8 +2762,6 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
27192762 &kCFTypeDictionaryValueCallBacks );
27202763 CFRelease (font);
27212764
2722- CFStringRef s = CFStringCreateWithCharacters (kCFAllocatorDefault , text, n);
2723-
27242765 CFAttributedStringRef string = CFAttributedStringCreate (kCFAllocatorDefault ,
27252766 s,
27262767 attributes);
@@ -2745,7 +2786,7 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
27452786 return Py_BuildValue (" fff" , width, rect.size .height , descent);
27462787}
27472788
2748- #else
2789+ #else // Text drawing for OSX versions <10.5
27492790
27502791static PyObject*
27512792GraphicsContext_draw_text (GraphicsContext* self, PyObject* args)
@@ -4948,7 +4989,11 @@ -(void)save_figure:(id)sender
49484989 unsigned int n = [filename length ];
49494990 unichar * buffer = malloc (n*sizeof (unichar ));
49504991 [filename getCharacters: buffer];
4992+ #if PY33
4993+ PyObject* string = PyUnicode_FromKindAndData (PyUnicode_2BYTE_KIND, buffer, n);
4994+ #else
49514995 PyObject* string = PyUnicode_FromUnicode (buffer, n);
4996+ #endif
49524997 free (buffer);
49534998 return string;
49544999 }
0 commit comments