summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Chaplin <>2010-09-15 22:11:39 +0800
committerSteve Chaplin <>2010-09-15 22:11:39 +0800
commit79950d982973947e412c44fde63327b6982bfc1a (patch)
tree7b1ff435d0f690ba23027b59788caddb9984c866
parent6a0a09a7c37a54cce3b890f37965868e94539ed8 (diff)
downloadpy2cairo-79950d982973947e412c44fde63327b6982bfc1a.tar.gz
Read string or unicode 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
-rw-r--r--src/context.c103
-rw-r--r--src/font.c54
2 files changed, 33 insertions, 124 deletions
diff --git a/src/context.c b/src/context.c
index f6e76c7..d580260 100644
--- a/src/context.c
+++ b/src/context.c
@@ -794,34 +794,16 @@ pycairo_scale (PycairoContext *o, PyObject *args) {
static PyObject *
pycairo_select_font_face (PycairoContext *o, PyObject *args) {
- PyObject *obj;
- PyObject *pyUTF8 = NULL;
- const char *utf8family = NULL;
+ 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, "O!|ii:Context.select_font_face",
- &PyBaseString_Type, &obj, &slant, &weight))
+ if (!PyArg_ParseTuple (args, "et|ii:Context.select_font_face",
+ "utf-8", &utf8, &slant, &weight))
return NULL;
- /* accept str and unicode family, auto convert to utf8 as required */
- if (PyString_Check(obj)) {
- /* A plain ASCII string is also a valid UTF-8 string */
- utf8family = PyString_AS_STRING(obj);
- } else if (PyUnicode_Check(obj)) {
- pyUTF8 = PyUnicode_AsUTF8String(obj);
- if (pyUTF8 != NULL) {
- utf8family = PyString_AS_STRING(pyUTF8);
- }
- } else {
- PyErr_SetString(PyExc_TypeError,
- "Context.select_font_face: family must be str or unicode");
- }
- if (utf8family == NULL)
- return NULL;
-
- cairo_select_font_face (o->ctx, utf8family, slant, weight);
- Py_XDECREF(pyUTF8);
+ cairo_select_font_face (o->ctx, utf8, slant, weight);
+ PyMem_Free(utf8);
RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
Py_RETURN_NONE;
}
@@ -1120,30 +1102,17 @@ pycairo_show_page (PycairoContext *o) {
}
static PyObject *
-pycairo_show_text (PycairoContext *o, PyObject *obj) {
- PyObject *pyUTF8 = NULL;
- const char *utf8 = NULL;
+pycairo_show_text (PycairoContext *o, PyObject *args) {
+ const char *utf8;
- /* accept str and unicode text, auto convert to utf8 as required */
- if (PyString_Check(obj)) {
- /* A plain ASCII string is also a valid UTF-8 string */
- utf8 = PyString_AS_STRING(obj);
- } else if (PyUnicode_Check(obj)) {
- pyUTF8 = PyUnicode_AsUTF8String(obj);
- if (pyUTF8 != NULL) {
- utf8 = PyString_AS_STRING(pyUTF8);
- }
- } else {
- PyErr_SetString(PyExc_TypeError,
- "Context.show_text: text must be str or unicode");
- }
- if (utf8 == NULL)
+ if (!PyArg_ParseTuple (args, "et: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(utf8);
RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
Py_RETURN_NONE;
}
@@ -1175,29 +1144,15 @@ pycairo_stroke_preserve (PycairoContext *o) {
}
static PyObject *
-pycairo_text_extents (PycairoContext *o, PyObject *obj) {
+pycairo_text_extents (PycairoContext *o, PyObject *args) {
cairo_text_extents_t extents;
- PyObject *pyUTF8 = NULL;
- const char *utf8 = NULL;
-
- /* accept str and unicode text, auto convert to utf8 as required */
- if (PyString_Check(obj)) {
- /* A plain ASCII string is also a valid UTF-8 string */
- utf8 = PyString_AS_STRING(obj);
- } else if (PyUnicode_Check(obj)) {
- pyUTF8 = PyUnicode_AsUTF8String(obj);
- if (pyUTF8 != NULL) {
- utf8 = PyString_AS_STRING(pyUTF8);
- }
- } else {
- PyErr_SetString(PyExc_TypeError,
- "Context.text_extents: text must be str or unicode");
- }
- if (utf8 == NULL)
+ const char *utf8;
+
+ if (!PyArg_ParseTuple (args, "et:Context.text_extents", "utf-8", &utf8))
return NULL;
cairo_text_extents (o->ctx, utf8, &extents);
- Py_XDECREF(pyUTF8);
+ PyMem_Free(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,
@@ -1205,28 +1160,14 @@ pycairo_text_extents (PycairoContext *o, PyObject *obj) {
}
static PyObject *
-pycairo_text_path (PycairoContext *o, PyObject *obj) {
- PyObject *pyUTF8 = NULL;
- const char *utf8 = NULL;
+pycairo_text_path (PycairoContext *o, PyObject *args) {
+ const char *utf8;
- /* accept str and unicode text, auto convert to utf8 as required */
- if (PyString_Check(obj)) {
- /* A plain ASCII string is also a valid UTF-8 string */
- utf8 = PyString_AS_STRING(obj);
- } else if (PyUnicode_Check(obj)) {
- pyUTF8 = PyUnicode_AsUTF8String(obj);
- if (pyUTF8 != NULL) {
- utf8 = PyString_AS_STRING(pyUTF8);
- }
- } else {
- PyErr_SetString(PyExc_TypeError,
- "Context.text_path: text must be str or unicode");
- }
- if (utf8 == NULL)
+ if (!PyArg_ParseTuple (args, "et:Context.text_path", "utf-8", &utf8))
return NULL;
cairo_text_path (o->ctx, utf8);
- Py_XDECREF(pyUTF8);
+ PyMem_Free(utf8);
RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
Py_RETURN_NONE;
}
@@ -1384,12 +1325,12 @@ static PyMethodDef pycairo_methods[] = {
{"set_tolerance", (PyCFunction)pycairo_set_tolerance, METH_VARARGS},
{"show_glyphs", (PyCFunction)pycairo_show_glyphs, METH_VARARGS},
{"show_page", (PyCFunction)pycairo_show_page, METH_NOARGS},
- {"show_text", (PyCFunction)pycairo_show_text, METH_O},
+ {"show_text", (PyCFunction)pycairo_show_text, METH_VARARGS},
{"stroke", (PyCFunction)pycairo_stroke, METH_NOARGS},
{"stroke_extents", (PyCFunction)pycairo_stroke_extents, METH_NOARGS},
{"stroke_preserve", (PyCFunction)pycairo_stroke_preserve, METH_NOARGS},
- {"text_extents", (PyCFunction)pycairo_text_extents, METH_O},
- {"text_path", (PyCFunction)pycairo_text_path, METH_O},
+ {"text_extents", (PyCFunction)pycairo_text_extents, METH_VARARGS},
+ {"text_path", (PyCFunction)pycairo_text_path, METH_VARARGS},
{"transform", (PyCFunction)pycairo_transform, METH_VARARGS},
{"translate", (PyCFunction)pycairo_translate, METH_VARARGS},
{"user_to_device", (PyCFunction)pycairo_user_to_device, METH_VARARGS},
diff --git a/src/font.c b/src/font.c
index 3fcc9e4..711076c 100644
--- a/src/font.c
+++ b/src/font.c
@@ -149,35 +149,17 @@ PyTypeObject PycairoFontFace_Type = {
static PyObject *
toy_font_face_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
- PyObject *obj;
- PyObject *pyUTF8 = NULL;
- const char *utf8family = NULL;
+ 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, "O!|ii:ToyFontFace.__new__",
- &PyBaseString_Type, &obj, &slant, &weight))
- return NULL;
-
- /* accept str and unicode family, auto convert to utf8 as required */
- if (PyString_Check(obj)) {
- /* A plain ASCII string is also a valid UTF-8 string */
- utf8family = PyString_AS_STRING(obj);
- } else if (PyUnicode_Check(obj)) {
- pyUTF8 = PyUnicode_AsUTF8String(obj);
- if (pyUTF8 != NULL) {
- utf8family = PyString_AS_STRING(pyUTF8);
- }
- } else {
- PyErr_SetString(PyExc_TypeError,
- "ToyFontFace.__new__: family must be str or unicode");
- }
- if (utf8family == NULL)
+ if (!PyArg_ParseTuple (args, "et|ii:ToyFontFace.__new__",
+ "utf-8", &utf8, &slant, &weight))
return NULL;
PyObject *o = PycairoFontFace_FromFontFace (
- cairo_toy_font_face_create (utf8family, slant, weight));
- Py_XDECREF(pyUTF8);
+ cairo_toy_font_face_create (utf8, slant, weight));
+ PyMem_Free(utf8);
return o;
}
@@ -328,29 +310,15 @@ scaled_font_get_scale_matrix (PycairoScaledFont *o) {
}
static PyObject *
-scaled_font_text_extents (PycairoScaledFont *o, PyObject *obj) {
+scaled_font_text_extents (PycairoScaledFont *o, PyObject *args) {
+ const char *utf8;
cairo_text_extents_t extents;
- PyObject *pyUTF8 = NULL;
- const char *utf8 = NULL;
-
- /* accept str and unicode text, auto convert to utf8 as required */
- if (PyString_Check(obj)) {
- /* A plain ASCII string is also a valid UTF-8 string */
- utf8 = PyString_AS_STRING(obj);
- } else if (PyUnicode_Check(obj)) {
- pyUTF8 = PyUnicode_AsUTF8String(obj);
- if (pyUTF8 != NULL) {
- utf8 = PyString_AS_STRING(pyUTF8);
- }
- } else {
- PyErr_SetString(PyExc_TypeError,
- "ScaledFont.text_extents: text must be str or unicode");
- }
- if (utf8 == NULL)
+
+ if (!PyArg_ParseTuple (args, "et:ScaledFont.text_extents", "utf-8", &utf8))
return NULL;
cairo_scaled_font_text_extents (o->scaled_font, utf8, &extents);
- Py_XDECREF(pyUTF8);
+ PyMem_Free(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,
@@ -373,7 +341,7 @@ static PyMethodDef scaled_font_methods[] = {
{"extents", (PyCFunction)scaled_font_extents, METH_NOARGS},
{"get_font_face", (PyCFunction)scaled_font_get_font_face, METH_NOARGS},
{"get_scale_matrix", (PyCFunction)scaled_font_get_scale_matrix, METH_VARARGS},
- {"text_extents", (PyCFunction)scaled_font_text_extents, METH_O},
+ {"text_extents", (PyCFunction)scaled_font_text_extents, METH_VARARGS},
{NULL, NULL, 0, NULL},
};