Bump version to 0.9.1.
[python/dscho.git] / Modules / fmmodule.c
blobdf5f27ba67f277c4d1838571f7daf6a24b7ef3f8
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Font Manager module */
13 #include "Python.h"
15 #include <gl.h>
16 #include <device.h>
17 #include <fmclient.h>
20 /* Font Handle object implementation */
22 typedef struct {
23 PyObject_HEAD
24 fmfonthandle fh_fh;
25 } fhobject;
27 staticforward PyTypeObject Fhtype;
29 #define is_fhobject(v) ((v)->ob_type == &Fhtype)
31 static PyObject *
32 newfhobject(fmfonthandle fh)
34 fhobject *fhp;
35 if (fh == NULL) {
36 PyErr_SetString(PyExc_RuntimeError,
37 "error creating new font handle");
38 return NULL;
40 fhp = PyObject_New(fhobject, &Fhtype);
41 if (fhp == NULL)
42 return NULL;
43 fhp->fh_fh = fh;
44 return (PyObject *)fhp;
47 /* Font Handle methods */
49 static PyObject *
50 fh_scalefont(fhobject *self, PyObject *args)
52 double size;
53 if (!PyArg_Parse(args, "d", &size))
54 return NULL;
55 return newfhobject(fmscalefont(self->fh_fh, size));
58 /* XXX fmmakefont */
60 static PyObject *
61 fh_setfont(fhobject *self, PyObject *args)
63 if (!PyArg_NoArgs(args))
64 return NULL;
65 fmsetfont(self->fh_fh);
66 Py_INCREF(Py_None);
67 return Py_None;
70 static PyObject *
71 fh_getfontname(fhobject *self, PyObject *args)
73 char fontname[256];
74 int len;
75 if (!PyArg_NoArgs(args))
76 return NULL;
77 len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
78 if (len < 0) {
79 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
80 return NULL;
82 return PyString_FromStringAndSize(fontname, len);
85 static PyObject *
86 fh_getcomment(fhobject *self, PyObject *args)
88 char comment[256];
89 int len;
90 if (!PyArg_NoArgs(args))
91 return NULL;
92 len = fmgetcomment(self->fh_fh, sizeof comment, comment);
93 if (len < 0) {
94 PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
95 return NULL;
97 return PyString_FromStringAndSize(comment, len);
100 static PyObject *
101 fh_getfontinfo(fhobject *self, PyObject *args)
103 fmfontinfo info;
104 if (!PyArg_NoArgs(args))
105 return NULL;
106 if (fmgetfontinfo(self->fh_fh, &info) < 0) {
107 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
108 return NULL;
110 return Py_BuildValue("(llllllll)",
111 info.printermatched,
112 info.fixed_width,
113 info.xorig,
114 info.yorig,
115 info.xsize,
116 info.ysize,
117 info.height,
118 info.nglyphs);
121 #if 0
122 static PyObject *
123 fh_getwholemetrics(fhobject *self, PyObject *args)
126 #endif
128 static PyObject *
129 fh_getstrwidth(fhobject *self, PyObject *args)
131 char *str;
132 if (!PyArg_Parse(args, "s", &str))
133 return NULL;
134 return PyInt_FromLong(fmgetstrwidth(self->fh_fh, str));
137 static PyMethodDef fh_methods[] = {
138 {"scalefont", (PyCFunction)fh_scalefont},
139 {"setfont", (PyCFunction)fh_setfont},
140 {"getfontname", (PyCFunction)fh_getfontname},
141 {"getcomment", (PyCFunction)fh_getcomment},
142 {"getfontinfo", (PyCFunction)fh_getfontinfo},
143 #if 0
144 {"getwholemetrics", (PyCFunction)fh_getwholemetrics},
145 #endif
146 {"getstrwidth", (PyCFunction)fh_getstrwidth},
147 {NULL, NULL} /* sentinel */
150 static PyObject *
151 fh_getattr(fhobject *fhp, char *name)
153 return Py_FindMethod(fh_methods, (PyObject *)fhp, name);
156 static void
157 fh_dealloc(fhobject *fhp)
159 fmfreefont(fhp->fh_fh);
160 PyObject_Del(fhp);
163 static PyTypeObject Fhtype = {
164 PyObject_HEAD_INIT(&PyType_Type)
165 0, /*ob_size*/
166 "font handle", /*tp_name*/
167 sizeof(fhobject), /*tp_size*/
168 0, /*tp_itemsize*/
169 /* methods */
170 (destructor)fh_dealloc, /*tp_dealloc*/
171 0, /*tp_print*/
172 (getattrfunc)fh_getattr, /*tp_getattr*/
173 0, /*tp_setattr*/
174 0, /*tp_compare*/
175 0, /*tp_repr*/
179 /* Font Manager functions */
181 static PyObject *
182 fm_init(PyObject *self, PyObject *args)
184 if (!PyArg_NoArgs(args))
185 return NULL;
186 fminit();
187 Py_INCREF(Py_None);
188 return Py_None;
191 static PyObject *
192 fm_findfont(PyObject *self, PyObject *args)
194 char *str;
195 if (!PyArg_Parse(args, "s", &str))
196 return NULL;
197 return newfhobject(fmfindfont(str));
200 static PyObject *
201 fm_prstr(PyObject *self, PyObject *args)
203 char *str;
204 if (!PyArg_Parse(args, "s", &str))
205 return NULL;
206 fmprstr(str);
207 Py_INCREF(Py_None);
208 return Py_None;
211 /* XXX This uses a global variable as temporary! Not re-entrant! */
213 static PyObject *fontlist;
215 static void
216 clientproc(char *fontname)
218 int err;
219 PyObject *v;
220 if (fontlist == NULL)
221 return;
222 v = PyString_FromString(fontname);
223 if (v == NULL)
224 err = -1;
225 else {
226 err = PyList_Append(fontlist, v);
227 Py_DECREF(v);
229 if (err != 0) {
230 Py_DECREF(fontlist);
231 fontlist = NULL;
235 static PyObject *
236 fm_enumerate(PyObject *self, PyObject *args)
238 PyObject *res;
239 if (!PyArg_NoArgs(args))
240 return NULL;
241 fontlist = PyList_New(0);
242 if (fontlist == NULL)
243 return NULL;
244 fmenumerate(clientproc);
245 res = fontlist;
246 fontlist = NULL;
247 return res;
250 static PyObject *
251 fm_setpath(PyObject *self, PyObject *args)
253 char *str;
254 if (!PyArg_Parse(args, "s", &str))
255 return NULL;
256 fmsetpath(str);
257 Py_INCREF(Py_None);
258 return Py_None;
261 static PyObject *
262 fm_fontpath(PyObject *self, PyObject *args)
264 if (!PyArg_NoArgs(args))
265 return NULL;
266 return PyString_FromString(fmfontpath());
269 static PyMethodDef fm_methods[] = {
270 {"init", fm_init},
271 {"findfont", fm_findfont},
272 {"enumerate", fm_enumerate},
273 {"prstr", fm_prstr},
274 {"setpath", fm_setpath},
275 {"fontpath", fm_fontpath},
276 {NULL, NULL} /* sentinel */
280 void
281 initfm(void)
283 Py_InitModule("fm", fm_methods);
284 fminit();