4 * Copyright (C) 2007,2009 by Aiwota Programmer
5 * aiwotaprog@tetteke.tk
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <pygobject.h>
26 #include <pygtk/pygtk.h>
29 /* ------------ types from other modules ------------ */
30 static PyTypeObject * _PyPangoAttrList_Type;
31 #define PyPangoAttrList_Type (*_PyPangoAttrList_Type)
32 static PyTypeObject * _PyPangoContext_Type;
33 #define PyPangoContext_Type (*_PyPangoContext_Type)
36 void get_char_width(PangoContext *context, const char* text,
37 int len, PangoAttrList *attrs, double* widths)
42 while (p - text < len) {
43 const char* const next = g_utf8_next_char(p);
44 GList *list = pango_itemize(context, p, 0, next-p, attrs, NULL);
49 PangoItem *item = list_s->data;
50 PangoAnalysis *analysis = &item->analysis;
51 PangoGlyphString *glyphs = pango_glyph_string_new();
54 pango_shape(p + item->offset, item->length, analysis, glyphs);
56 for (i = 0; i < glyphs->num_glyphs; i++)
59 pango_units_to_double(glyphs->glyphs[i].geometry.width);
61 widths[idx++] = width;
63 pango_item_free(item);
64 pango_glyph_string_free(glyphs);
65 list_s = list_s->next;
74 _wrap_get_char_width(PyObject *self, PyObject *args, PyObject *kwargs)
76 static char *kwlist[] = { "context", "text", "attrs", NULL};
77 PyObject *py_context, *py_attrs;
78 PangoContext *context;
79 PangoAttrList *attrs = NULL;
83 gdouble *logical_widths;
86 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os#O", kwlist,
87 &py_context, &text, &length, &py_attrs))
90 if (pygobject_check(py_context, &PyPangoContext_Type))
91 context = (PangoContext*) pygobject_get(py_context);
94 PyErr_SetString(PyExc_TypeError, "context must be a PangoContext");
98 if (pygobject_check(py_attrs, &PyPangoAttrList_Type))
99 /*if (pyg_boxed_check(py_attrs, PANGO_TYPE_ATTR_LIST))*/
101 attrs = pyg_boxed_get(py_attrs, PangoAttrList);
103 else if (py_attrs != Py_None)
105 PyErr_SetString(PyExc_TypeError,
106 "attrlist must be a PangoAttrList or None");
111 slen = g_utf8_strlen(text, length);
113 logical_widths = g_new(double, slen);
114 get_char_width(context, text, length, attrs, logical_widths);
116 ret = PyTuple_New(slen);
117 for (i = 0; i < slen; i++)
119 PyObject *item = PyFloat_FromDouble(logical_widths[i]);
120 PyTuple_SetItem(ret, i, item);
122 g_free(logical_widths);
127 static PyMethodDef pythread_view_extend_functions[] = {
128 { "get_char_width", (PyCFunction)_wrap_get_char_width,
129 METH_VARARGS|METH_KEYWORDS, NULL },
130 { NULL, NULL, 0, NULL }
133 /* initialise stuff extension classes */
134 PyMODINIT_FUNC initthread_view_extend()
138 if ((module = PyImport_ImportModule("pango")) != NULL)
140 _PyPangoContext_Type = (PyTypeObject *)
141 PyObject_GetAttrString(module, "Context");
142 if (_PyPangoContext_Type == NULL)
144 PyErr_SetString(PyExc_ImportError,
145 "cannot import name Context from pango");
149 _PyPangoAttrList_Type = (PyTypeObject *)
150 PyObject_GetAttrString(module, "AttrList");
151 if (_PyPangoAttrList_Type == NULL)
153 PyErr_SetString(PyExc_ImportError,
154 "cannot import name AttrList from pango");
159 PyErr_SetString(PyExc_ImportError, "could not import pango");
163 Py_InitModule("thread_view_extend", pythread_view_extend_functions);