The 0.5 release happened on 2/15, not on 2/14. :-)
[python/dscho.git] / Modules / fmmodule.c
blobdd5b39787827cb7e7693ce89c5a30e1d72053ce0
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Font Manager module */
34 #include "Python.h"
36 #include <gl.h>
37 #include <device.h>
38 #include <fmclient.h>
41 /* Font Handle object implementation */
43 typedef struct {
44 PyObject_HEAD
45 fmfonthandle fh_fh;
46 } fhobject;
48 staticforward PyTypeObject Fhtype;
50 #define is_fhobject(v) ((v)->ob_type == &Fhtype)
52 static PyObject *
53 newfhobject(fh)
54 fmfonthandle fh;
56 fhobject *fhp;
57 if (fh == NULL) {
58 PyErr_SetString(PyExc_RuntimeError,
59 "error creating new font handle");
60 return NULL;
62 fhp = PyObject_NEW(fhobject, &Fhtype);
63 if (fhp == NULL)
64 return NULL;
65 fhp->fh_fh = fh;
66 return (PyObject *)fhp;
69 /* Font Handle methods */
71 static PyObject *
72 fh_scalefont(self, args)
73 fhobject *self;
74 PyObject *args;
76 double size;
77 if (!PyArg_Parse(args, "d", &size))
78 return NULL;
79 return newfhobject(fmscalefont(self->fh_fh, size));
82 /* XXX fmmakefont */
84 static PyObject *
85 fh_setfont(self, args)
86 fhobject *self;
87 PyObject *args;
89 if (!PyArg_NoArgs(args))
90 return NULL;
91 fmsetfont(self->fh_fh);
92 Py_INCREF(Py_None);
93 return Py_None;
96 static PyObject *
97 fh_getfontname(self, args)
98 fhobject *self;
99 PyObject *args;
101 char fontname[256];
102 int len;
103 if (!PyArg_NoArgs(args))
104 return NULL;
105 len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
106 if (len < 0) {
107 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
108 return NULL;
110 return PyString_FromStringAndSize(fontname, len);
113 static PyObject *
114 fh_getcomment(self, args)
115 fhobject *self;
116 PyObject *args;
118 char comment[256];
119 int len;
120 if (!PyArg_NoArgs(args))
121 return NULL;
122 len = fmgetcomment(self->fh_fh, sizeof comment, comment);
123 if (len < 0) {
124 PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
125 return NULL;
127 return PyString_FromStringAndSize(comment, len);
130 static PyObject *
131 fh_getfontinfo(self, args)
132 fhobject *self;
133 PyObject *args;
135 fmfontinfo info;
136 if (!PyArg_NoArgs(args))
137 return NULL;
138 if (fmgetfontinfo(self->fh_fh, &info) < 0) {
139 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
140 return NULL;
142 return Py_BuildValue("(llllllll)",
143 info.printermatched,
144 info.fixed_width,
145 info.xorig,
146 info.yorig,
147 info.xsize,
148 info.ysize,
149 info.height,
150 info.nglyphs);
153 #if 0
154 static PyObject *
155 fh_getwholemetrics(self, args)
156 fhobject *self;
157 PyObject *args;
160 #endif
162 static PyObject *
163 fh_getstrwidth(self, args)
164 fhobject *self;
165 PyObject *args;
167 char *str;
168 if (!PyArg_Parse(args, "s", &str))
169 return NULL;
170 return PyInt_FromLong(fmgetstrwidth(self->fh_fh, str));
173 static PyMethodDef fh_methods[] = {
174 {"scalefont", (PyCFunction)fh_scalefont},
175 {"setfont", (PyCFunction)fh_setfont},
176 {"getfontname", (PyCFunction)fh_getfontname},
177 {"getcomment", (PyCFunction)fh_getcomment},
178 {"getfontinfo", (PyCFunction)fh_getfontinfo},
179 #if 0
180 {"getwholemetrics", (PyCFunction)fh_getwholemetrics},
181 #endif
182 {"getstrwidth", (PyCFunction)fh_getstrwidth},
183 {NULL, NULL} /* sentinel */
186 static PyObject *
187 fh_getattr(fhp, name)
188 fhobject *fhp;
189 char *name;
191 return Py_FindMethod(fh_methods, (PyObject *)fhp, name);
194 static void
195 fh_dealloc(fhp)
196 fhobject *fhp;
198 fmfreefont(fhp->fh_fh);
199 PyMem_DEL(fhp);
202 static PyTypeObject Fhtype = {
203 PyObject_HEAD_INIT(&PyType_Type)
204 0, /*ob_size*/
205 "font handle", /*tp_name*/
206 sizeof(fhobject), /*tp_size*/
207 0, /*tp_itemsize*/
208 /* methods */
209 (destructor)fh_dealloc, /*tp_dealloc*/
210 0, /*tp_print*/
211 (getattrfunc)fh_getattr, /*tp_getattr*/
212 0, /*tp_setattr*/
213 0, /*tp_compare*/
214 0, /*tp_repr*/
218 /* Font Manager functions */
220 static PyObject *
221 fm_init(self, args)
222 PyObject *self, *args;
224 if (!PyArg_NoArgs(args))
225 return NULL;
226 fminit();
227 Py_INCREF(Py_None);
228 return Py_None;
231 static PyObject *
232 fm_findfont(self, args)
233 PyObject *self, *args;
235 char *str;
236 if (!PyArg_Parse(args, "s", &str))
237 return NULL;
238 return newfhobject(fmfindfont(str));
241 static PyObject *
242 fm_prstr(self, args)
243 PyObject *self, *args;
245 char *str;
246 if (!PyArg_Parse(args, "s", &str))
247 return NULL;
248 fmprstr(str);
249 Py_INCREF(Py_None);
250 return Py_None;
253 /* XXX This uses a global variable as temporary! Not re-entrant! */
255 static PyObject *fontlist;
257 static void
258 clientproc(fontname)
259 char *fontname;
261 int err;
262 PyObject *v;
263 if (fontlist == NULL)
264 return;
265 v = PyString_FromString(fontname);
266 if (v == NULL)
267 err = -1;
268 else {
269 err = PyList_Append(fontlist, v);
270 Py_DECREF(v);
272 if (err != 0) {
273 Py_DECREF(fontlist);
274 fontlist = NULL;
278 static PyObject *
279 fm_enumerate(self, args)
280 PyObject *self, *args;
282 PyObject *res;
283 if (!PyArg_NoArgs(args))
284 return NULL;
285 fontlist = PyList_New(0);
286 if (fontlist == NULL)
287 return NULL;
288 fmenumerate(clientproc);
289 res = fontlist;
290 fontlist = NULL;
291 return res;
294 static PyObject *
295 fm_setpath(self, args)
296 PyObject *self, *args;
298 char *str;
299 if (!PyArg_Parse(args, "s", &str))
300 return NULL;
301 fmsetpath(str);
302 Py_INCREF(Py_None);
303 return Py_None;
306 static PyObject *
307 fm_fontpath(self, args)
308 PyObject *self, *args;
310 if (!PyArg_NoArgs(args))
311 return NULL;
312 return PyString_FromString(fmfontpath());
315 static PyMethodDef fm_methods[] = {
316 {"init", fm_init},
317 {"findfont", fm_findfont},
318 {"enumerate", fm_enumerate},
319 {"prstr", fm_prstr},
320 {"setpath", fm_setpath},
321 {"fontpath", fm_fontpath},
322 {NULL, NULL} /* sentinel */
326 void
327 initfm()
329 Py_InitModule("fm", fm_methods);
330 fminit();