Updated for 2.1b2 distribution.
[python/dscho.git] / Modules / fmmodule.c
blob065aed60045984329279dfc644f87918616546a6
2 /* Font Manager module */
4 #include "Python.h"
6 #include <gl.h>
7 #include <device.h>
8 #include <fmclient.h>
11 /* Font Handle object implementation */
13 typedef struct {
14 PyObject_HEAD
15 fmfonthandle fh_fh;
16 } fhobject;
18 staticforward PyTypeObject Fhtype;
20 #define is_fhobject(v) ((v)->ob_type == &Fhtype)
22 static PyObject *
23 newfhobject(fmfonthandle fh)
25 fhobject *fhp;
26 if (fh == NULL) {
27 PyErr_SetString(PyExc_RuntimeError,
28 "error creating new font handle");
29 return NULL;
31 fhp = PyObject_New(fhobject, &Fhtype);
32 if (fhp == NULL)
33 return NULL;
34 fhp->fh_fh = fh;
35 return (PyObject *)fhp;
38 /* Font Handle methods */
40 static PyObject *
41 fh_scalefont(fhobject *self, PyObject *args)
43 double size;
44 if (!PyArg_Parse(args, "d", &size))
45 return NULL;
46 return newfhobject(fmscalefont(self->fh_fh, size));
49 /* XXX fmmakefont */
51 static PyObject *
52 fh_setfont(fhobject *self, PyObject *args)
54 if (!PyArg_NoArgs(args))
55 return NULL;
56 fmsetfont(self->fh_fh);
57 Py_INCREF(Py_None);
58 return Py_None;
61 static PyObject *
62 fh_getfontname(fhobject *self, PyObject *args)
64 char fontname[256];
65 int len;
66 if (!PyArg_NoArgs(args))
67 return NULL;
68 len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
69 if (len < 0) {
70 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
71 return NULL;
73 return PyString_FromStringAndSize(fontname, len);
76 static PyObject *
77 fh_getcomment(fhobject *self, PyObject *args)
79 char comment[256];
80 int len;
81 if (!PyArg_NoArgs(args))
82 return NULL;
83 len = fmgetcomment(self->fh_fh, sizeof comment, comment);
84 if (len < 0) {
85 PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
86 return NULL;
88 return PyString_FromStringAndSize(comment, len);
91 static PyObject *
92 fh_getfontinfo(fhobject *self, PyObject *args)
94 fmfontinfo info;
95 if (!PyArg_NoArgs(args))
96 return NULL;
97 if (fmgetfontinfo(self->fh_fh, &info) < 0) {
98 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
99 return NULL;
101 return Py_BuildValue("(llllllll)",
102 info.printermatched,
103 info.fixed_width,
104 info.xorig,
105 info.yorig,
106 info.xsize,
107 info.ysize,
108 info.height,
109 info.nglyphs);
112 #if 0
113 static PyObject *
114 fh_getwholemetrics(fhobject *self, PyObject *args)
117 #endif
119 static PyObject *
120 fh_getstrwidth(fhobject *self, PyObject *args)
122 char *str;
123 if (!PyArg_Parse(args, "s", &str))
124 return NULL;
125 return PyInt_FromLong(fmgetstrwidth(self->fh_fh, str));
128 static PyMethodDef fh_methods[] = {
129 {"scalefont", (PyCFunction)fh_scalefont},
130 {"setfont", (PyCFunction)fh_setfont},
131 {"getfontname", (PyCFunction)fh_getfontname},
132 {"getcomment", (PyCFunction)fh_getcomment},
133 {"getfontinfo", (PyCFunction)fh_getfontinfo},
134 #if 0
135 {"getwholemetrics", (PyCFunction)fh_getwholemetrics},
136 #endif
137 {"getstrwidth", (PyCFunction)fh_getstrwidth},
138 {NULL, NULL} /* sentinel */
141 static PyObject *
142 fh_getattr(fhobject *fhp, char *name)
144 return Py_FindMethod(fh_methods, (PyObject *)fhp, name);
147 static void
148 fh_dealloc(fhobject *fhp)
150 fmfreefont(fhp->fh_fh);
151 PyObject_Del(fhp);
154 static PyTypeObject Fhtype = {
155 PyObject_HEAD_INIT(&PyType_Type)
156 0, /*ob_size*/
157 "font handle", /*tp_name*/
158 sizeof(fhobject), /*tp_size*/
159 0, /*tp_itemsize*/
160 /* methods */
161 (destructor)fh_dealloc, /*tp_dealloc*/
162 0, /*tp_print*/
163 (getattrfunc)fh_getattr, /*tp_getattr*/
164 0, /*tp_setattr*/
165 0, /*tp_compare*/
166 0, /*tp_repr*/
170 /* Font Manager functions */
172 static PyObject *
173 fm_init(PyObject *self, PyObject *args)
175 if (!PyArg_NoArgs(args))
176 return NULL;
177 fminit();
178 Py_INCREF(Py_None);
179 return Py_None;
182 static PyObject *
183 fm_findfont(PyObject *self, PyObject *args)
185 char *str;
186 if (!PyArg_Parse(args, "s", &str))
187 return NULL;
188 return newfhobject(fmfindfont(str));
191 static PyObject *
192 fm_prstr(PyObject *self, PyObject *args)
194 char *str;
195 if (!PyArg_Parse(args, "s", &str))
196 return NULL;
197 fmprstr(str);
198 Py_INCREF(Py_None);
199 return Py_None;
202 /* XXX This uses a global variable as temporary! Not re-entrant! */
204 static PyObject *fontlist;
206 static void
207 clientproc(char *fontname)
209 int err;
210 PyObject *v;
211 if (fontlist == NULL)
212 return;
213 v = PyString_FromString(fontname);
214 if (v == NULL)
215 err = -1;
216 else {
217 err = PyList_Append(fontlist, v);
218 Py_DECREF(v);
220 if (err != 0) {
221 Py_DECREF(fontlist);
222 fontlist = NULL;
226 static PyObject *
227 fm_enumerate(PyObject *self, PyObject *args)
229 PyObject *res;
230 if (!PyArg_NoArgs(args))
231 return NULL;
232 fontlist = PyList_New(0);
233 if (fontlist == NULL)
234 return NULL;
235 fmenumerate(clientproc);
236 res = fontlist;
237 fontlist = NULL;
238 return res;
241 static PyObject *
242 fm_setpath(PyObject *self, PyObject *args)
244 char *str;
245 if (!PyArg_Parse(args, "s", &str))
246 return NULL;
247 fmsetpath(str);
248 Py_INCREF(Py_None);
249 return Py_None;
252 static PyObject *
253 fm_fontpath(PyObject *self, PyObject *args)
255 if (!PyArg_NoArgs(args))
256 return NULL;
257 return PyString_FromString(fmfontpath());
260 static PyMethodDef fm_methods[] = {
261 {"init", fm_init},
262 {"findfont", fm_findfont},
263 {"enumerate", fm_enumerate},
264 {"prstr", fm_prstr},
265 {"setpath", fm_setpath},
266 {"fontpath", fm_fontpath},
267 {NULL, NULL} /* sentinel */
271 void
272 initfm(void)
274 Py_InitModule("fm", fm_methods);
275 fminit();