From 2e923d4db82b764a1f07628477106f78e8dab4a7 Mon Sep 17 00:00:00 2001 From: Steve Chaplin <> Date: Sun, 19 Sep 2010 11:13:07 +0800 Subject: Improve/simplify unicode text support - read string and encode into utf8 using PyArg_ParseTuple. context.c pycairo_select_font_face pycairo_show_text pycairo_text_extents pycairo_text_path font.c toy_font_face_new scaled_font_text_extents --- doc/reference/context.rst | 2 +- src/context.c | 57 +++++++++++++---------------------------------- src/font.c | 30 +++++++------------------ src/wscript | 18 ++++++++++----- wscript | 1 + 5 files changed, 37 insertions(+), 71 deletions(-) diff --git a/doc/reference/context.rst b/doc/reference/context.rst index dfcd78a..4a2f17f 100644 --- a/doc/reference/context.rst +++ b/doc/reference/context.rst @@ -1409,7 +1409,7 @@ safely be changed, without loosing the current state. Use .. method:: text_path(text) :param text: text - :type text: string + :type text: str Adds closed paths for text to the current path. The generated path if filled, achieves an effect similar to that of :meth:`Context.show_text`. diff --git a/src/context.c b/src/context.c index 60cb0d7..9bee5d4 100644 --- a/src/context.c +++ b/src/context.c @@ -783,23 +783,16 @@ pycairo_scale (PycairoContext *o, PyObject *args) { static PyObject * pycairo_select_font_face (PycairoContext *o, PyObject *args) { - PyObject *obj; + const char *utf8; cairo_font_slant_t slant = CAIRO_FONT_SLANT_NORMAL; cairo_font_weight_t weight = CAIRO_FONT_WEIGHT_NORMAL; - if (!PyArg_ParseTuple(args, "U|ii:Context.select_font_face", - &obj, &slant, &weight)) - return NULL; - - PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 == NULL) - return NULL; - const char *utf8 = PyBytes_AS_STRING(pyUTF8); - if (utf8 == NULL) + if (!PyArg_ParseTuple (args, "es|ii:Context.select_font_face", + "utf-8", &utf8, &slant, &weight)) return NULL; cairo_select_font_face (o->ctx, utf8, slant, weight); - Py_XDECREF(pyUTF8); + PyMem_Free((void *)utf8); RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx); Py_RETURN_NONE; } @@ -1099,22 +1092,16 @@ pycairo_show_page (PycairoContext *o) { static PyObject * pycairo_show_text (PycairoContext *o, PyObject *args) { - PyObject *obj; - - if (!PyArg_ParseTuple(args, "U:Context.show_text", &obj)) - return NULL; + const char *utf8; - PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 == NULL) - return NULL; - const char *utf8 = PyBytes_AS_STRING(pyUTF8); - if (utf8 == NULL) + if (!PyArg_ParseTuple (args, "es:Context.show_text", "utf-8", &utf8)) return NULL; Py_BEGIN_ALLOW_THREADS; cairo_show_text (o->ctx, utf8); Py_END_ALLOW_THREADS; - Py_XDECREF(pyUTF8); + + PyMem_Free((void *)utf8); RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx); Py_RETURN_NONE; } @@ -1147,21 +1134,14 @@ pycairo_stroke_preserve (PycairoContext *o) { static PyObject * pycairo_text_extents (PycairoContext *o, PyObject *args) { - PyObject *obj; - - if (!PyArg_ParseTuple(args, "U:Context.text_extents", &obj)) - return NULL; + cairo_text_extents_t extents; + const char *utf8; - PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 == NULL) - return NULL; - const char *utf8 = PyBytes_AS_STRING(pyUTF8); - if (utf8 == NULL) + if (!PyArg_ParseTuple (args, "es:Context.text_extents", "utf-8", &utf8)) return NULL; - cairo_text_extents_t extents; cairo_text_extents (o->ctx, utf8, &extents); - Py_XDECREF(pyUTF8); + PyMem_Free((void *)utf8); RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx); return Py_BuildValue("(dddddd)", extents.x_bearing, extents.y_bearing, extents.width, extents.height, extents.x_advance, @@ -1170,20 +1150,13 @@ pycairo_text_extents (PycairoContext *o, PyObject *args) { static PyObject * pycairo_text_path (PycairoContext *o, PyObject *args) { - PyObject *obj; + const char *utf8; - if (!PyArg_ParseTuple(args, "U:Context.text_path", &obj)) - return NULL; - - PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 == NULL) - return NULL; - const char *utf8 = PyBytes_AS_STRING(pyUTF8); - if (utf8 == NULL) + if (!PyArg_ParseTuple (args, "es:Context.text_path", "utf-8", &utf8)) return NULL; cairo_text_path (o->ctx, utf8); - Py_XDECREF(pyUTF8); + PyMem_Free((void *)utf8); RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx); Py_RETURN_NONE; } diff --git a/src/font.c b/src/font.c index 10d90e7..6c7ca4e 100644 --- a/src/font.c +++ b/src/font.c @@ -140,24 +140,17 @@ PyTypeObject PycairoFontFace_Type = { static PyObject * toy_font_face_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyObject *obj; + const char *utf8; cairo_font_slant_t slant = CAIRO_FONT_SLANT_NORMAL; cairo_font_weight_t weight = CAIRO_FONT_WEIGHT_NORMAL; - if (!PyArg_ParseTuple(args, "U|ii:ToyFontFace.__new__", - &obj, &slant, &weight)) - return NULL; - - PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 == NULL) - return NULL; - const char *utf8 = PyBytes_AS_STRING(pyUTF8); - if (utf8 == NULL) + if (!PyArg_ParseTuple (args, "es|ii:ToyFontFace.__new__", + "utf-8", &utf8, &slant, &weight)) return NULL; PyObject *o = PycairoFontFace_FromFontFace ( cairo_toy_font_face_create (utf8, slant, weight)); - Py_XDECREF(pyUTF8); + PyMem_Free((void *)utf8); return o; } @@ -311,21 +304,14 @@ scaled_font_get_scale_matrix (PycairoScaledFont *o) { static PyObject * scaled_font_text_extents (PycairoScaledFont *o, PyObject *args) { - PyObject *obj; - - if (!PyArg_ParseTuple(args, "U:ScaledFont.text_extents", &obj)) - return NULL; + const char *utf8; + cairo_text_extents_t extents; - PyObject *pyUTF8 = PyUnicode_AsUTF8String(obj); - if (pyUTF8 == NULL) - return NULL; - const char *utf8 = PyBytes_AS_STRING(pyUTF8); - if (utf8 == NULL) + if (!PyArg_ParseTuple (args, "es:ScaledFont.text_extents", "utf-8", &utf8)) return NULL; - cairo_text_extents_t extents; cairo_scaled_font_text_extents (o->scaled_font, utf8, &extents); - Py_XDECREF(pyUTF8); + PyMem_Free((void *)utf8); RETURN_NULL_IF_CAIRO_SCALED_FONT_ERROR(o->scaled_font); return Py_BuildValue("(dddddd)", extents.x_bearing, extents.y_bearing, extents.width, extents.height, extents.x_advance, diff --git a/src/wscript b/src/wscript index ca51a88..d30c6ad 100644 --- a/src/wscript +++ b/src/wscript @@ -10,7 +10,6 @@ def build(ctx): pycairoLibDir = os.path.join(ctx.env['LIBDIR'], 'python'+ctx.env['PYTHON_VERSION'], 'site-packages', 'cairo') - # .py files ctx.new_task_gen( features = 'py', @@ -20,11 +19,18 @@ def build(ctx): # C extension module ctx.new_task_gen( - features = 'cc cshlib pyext', - source = 'cairomodule.c context.c font.c path.c pattern.c matrix.c surface.c', - target = '_cairo', - includes = '.', - uselib = 'CAIRO', + features = 'cc cshlib pyext', + source = ['cairomodule.c', + 'context.c', + 'font.c', + 'path.c', + 'pattern.c', + 'matrix.c', + 'surface.c', + ], + target = '_cairo', + includes = '.', + uselib = 'CAIRO', install_path = pycairoLibDir, ) diff --git a/wscript b/wscript index a55ae55..cf66711 100644 --- a/wscript +++ b/wscript @@ -50,6 +50,7 @@ def configure(ctx): ctx.write_config_header('src/config.h') + print("Configuration:") print("%-40s : %s" % ('PREFIX', env['PREFIX'])) print("%-40s : %s" % ('LIBDIR', env['LIBDIR'])) -- cgit v1.2.3