Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Resolving Issue #1737 - MacOSX backend unicode problems in python 3.3 #1753

Merged
merged 5 commits into from

5 participants

@bingjeff

This is to resolve an issue with the macosx backend when using Python 3.3 that the text printed during plots is only half of the string. I have resolved my issues, but I would request that someone knowledgable in objective-c and python take a look.

bingjeff added some commits
@bingjeff bingjeff Fix of macosx text rendering truncation with edit
of _macosx to overcome change in Py_UNICODE size
that occurred in Python3.3
f6382fe
@bingjeff bingjeff Rollback of error introduced in
GraphicsContext_draw_image from previous commit.
6433af0
@bingjeff

The Travis build failed initially, but it builds on my machine OSX 10.7.5 using matplotlib at tag v1.2.0, Python 3.3.0 and Numpy 1.7.

@dmcdougall dmcdougall was assigned
@pelson
Collaborator

@dmcdougall / @mdehoon - are either of you in a position to try this out?

@pelson
Collaborator

@mdehoon wrote:

@bingjeff : Thanks for providing the pull request. It seems to work fine for Python 3.3, but it breaks unicode handling for Python 2. For example, try running unicode_demo.py in examples/pylab_examples. So I think we need to add #ifdef's to GraphicsContext_draw_text to keep using the old code for Python 2.

@bingjeff

@pelson and @mdehoon I finally wrapped everything in #ifdefs hopefully this works now?

@mdboom
Owner

Just a note -- I think this is important enough that we should backport this to v1.2.x before or after merging.

@mdboom
Owner

What's the status of this? I don't have a Mac handy -- if someone can confirm this works on "not Damon's" Mac, I think this looks good to merge.

@cimarronm
Collaborator

This patch works for me

@mdboom mdboom merged commit 016d120 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 17, 2013
  1. @bingjeff

    Fix of macosx text rendering truncation with edit

    bingjeff authored
    of _macosx to overcome change in Py_UNICODE size
    that occurred in Python3.3
Commits on Feb 19, 2013
  1. @bingjeff

    Rollback of error introduced in

    bingjeff authored
    GraphicsContext_draw_image from previous commit.
Commits on Apr 18, 2013
  1. @bingjeff
  2. @bingjeff
  3. @bingjeff
This page is out of date. Refresh to see the latest.
Showing with 55 additions and 10 deletions.
  1. +55 −10 src/_macosx.m
View
65 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;
}
Something went wrong with that request. Please try again.