blob: 065aed60045984329279dfc644f87918616546a6 [file] [log] [blame]
Guido van Rossuma0c191e1991-04-03 19:02:31 +00001
2/* Font Manager module */
3
Roger E. Masseed648a21997-01-17 16:08:55 +00004#include "Python.h"
Guido van Rossuma0c191e1991-04-03 19:02:31 +00005
6#include <gl.h>
7#include <device.h>
8#include <fmclient.h>
9
10
11/* Font Handle object implementation */
12
13typedef struct {
Roger E. Masseed648a21997-01-17 16:08:55 +000014 PyObject_HEAD
Guido van Rossuma0c191e1991-04-03 19:02:31 +000015 fmfonthandle fh_fh;
16} fhobject;
17
Roger E. Masseed648a21997-01-17 16:08:55 +000018staticforward PyTypeObject Fhtype;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000019
20#define is_fhobject(v) ((v)->ob_type == &Fhtype)
21
Roger E. Masseed648a21997-01-17 16:08:55 +000022static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000023newfhobject(fmfonthandle fh)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000024{
25 fhobject *fhp;
26 if (fh == NULL) {
Roger E. Masseed648a21997-01-17 16:08:55 +000027 PyErr_SetString(PyExc_RuntimeError,
28 "error creating new font handle");
Guido van Rossuma0c191e1991-04-03 19:02:31 +000029 return NULL;
30 }
Guido van Rossumb18618d2000-05-03 23:44:39 +000031 fhp = PyObject_New(fhobject, &Fhtype);
Guido van Rossuma0c191e1991-04-03 19:02:31 +000032 if (fhp == NULL)
33 return NULL;
34 fhp->fh_fh = fh;
Roger E. Masseed648a21997-01-17 16:08:55 +000035 return (PyObject *)fhp;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000036}
37
38/* Font Handle methods */
39
Roger E. Masseed648a21997-01-17 16:08:55 +000040static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000041fh_scalefont(fhobject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000042{
43 double size;
Roger E. Masseed648a21997-01-17 16:08:55 +000044 if (!PyArg_Parse(args, "d", &size))
Guido van Rossuma0c191e1991-04-03 19:02:31 +000045 return NULL;
46 return newfhobject(fmscalefont(self->fh_fh, size));
47}
48
49/* XXX fmmakefont */
50
Roger E. Masseed648a21997-01-17 16:08:55 +000051static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000052fh_setfont(fhobject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000053{
Roger E. Masseed648a21997-01-17 16:08:55 +000054 if (!PyArg_NoArgs(args))
Guido van Rossuma0c191e1991-04-03 19:02:31 +000055 return NULL;
56 fmsetfont(self->fh_fh);
Roger E. Masseed648a21997-01-17 16:08:55 +000057 Py_INCREF(Py_None);
58 return Py_None;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000059}
60
Roger E. Masseed648a21997-01-17 16:08:55 +000061static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000062fh_getfontname(fhobject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000063{
64 char fontname[256];
65 int len;
Roger E. Masseed648a21997-01-17 16:08:55 +000066 if (!PyArg_NoArgs(args))
Guido van Rossuma0c191e1991-04-03 19:02:31 +000067 return NULL;
68 len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
69 if (len < 0) {
Roger E. Masseed648a21997-01-17 16:08:55 +000070 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
Guido van Rossuma0c191e1991-04-03 19:02:31 +000071 return NULL;
72 }
Roger E. Masseed648a21997-01-17 16:08:55 +000073 return PyString_FromStringAndSize(fontname, len);
Guido van Rossuma0c191e1991-04-03 19:02:31 +000074}
75
Roger E. Masseed648a21997-01-17 16:08:55 +000076static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000077fh_getcomment(fhobject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000078{
79 char comment[256];
80 int len;
Roger E. Masseed648a21997-01-17 16:08:55 +000081 if (!PyArg_NoArgs(args))
Guido van Rossuma0c191e1991-04-03 19:02:31 +000082 return NULL;
83 len = fmgetcomment(self->fh_fh, sizeof comment, comment);
84 if (len < 0) {
Roger E. Masseed648a21997-01-17 16:08:55 +000085 PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
Guido van Rossuma0c191e1991-04-03 19:02:31 +000086 return NULL;
87 }
Roger E. Masseed648a21997-01-17 16:08:55 +000088 return PyString_FromStringAndSize(comment, len);
Guido van Rossuma0c191e1991-04-03 19:02:31 +000089}
90
Roger E. Masseed648a21997-01-17 16:08:55 +000091static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000092fh_getfontinfo(fhobject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000093{
94 fmfontinfo info;
Roger E. Masseed648a21997-01-17 16:08:55 +000095 if (!PyArg_NoArgs(args))
Guido van Rossuma0c191e1991-04-03 19:02:31 +000096 return NULL;
97 if (fmgetfontinfo(self->fh_fh, &info) < 0) {
Roger E. Masseed648a21997-01-17 16:08:55 +000098 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
Guido van Rossuma0c191e1991-04-03 19:02:31 +000099 return NULL;
100 }
Roger E. Masseed648a21997-01-17 16:08:55 +0000101 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);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000110}
111
112#if 0
Roger E. Masseed648a21997-01-17 16:08:55 +0000113static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000114fh_getwholemetrics(fhobject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000115{
116}
117#endif
118
Roger E. Masseed648a21997-01-17 16:08:55 +0000119static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000120fh_getstrwidth(fhobject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000121{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000122 char *str;
Roger E. Masseed648a21997-01-17 16:08:55 +0000123 if (!PyArg_Parse(args, "s", &str))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000124 return NULL;
Roger E. Masseed648a21997-01-17 16:08:55 +0000125 return PyInt_FromLong(fmgetstrwidth(self->fh_fh, str));
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000126}
127
Roger E. Masseed648a21997-01-17 16:08:55 +0000128static 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},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000134#if 0
Roger E. Masseed648a21997-01-17 16:08:55 +0000135 {"getwholemetrics", (PyCFunction)fh_getwholemetrics},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000136#endif
Roger E. Masseed648a21997-01-17 16:08:55 +0000137 {"getstrwidth", (PyCFunction)fh_getstrwidth},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000138 {NULL, NULL} /* sentinel */
139};
140
Roger E. Masseed648a21997-01-17 16:08:55 +0000141static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000142fh_getattr(fhobject *fhp, char *name)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000143{
Roger E. Masseed648a21997-01-17 16:08:55 +0000144 return Py_FindMethod(fh_methods, (PyObject *)fhp, name);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000145}
146
147static void
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000148fh_dealloc(fhobject *fhp)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000149{
150 fmfreefont(fhp->fh_fh);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000151 PyObject_Del(fhp);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000152}
153
Roger E. Masseed648a21997-01-17 16:08:55 +0000154static PyTypeObject Fhtype = {
155 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000156 0, /*ob_size*/
157 "font handle", /*tp_name*/
158 sizeof(fhobject), /*tp_size*/
159 0, /*tp_itemsize*/
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000160 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161 (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*/
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000167};
168
169
170/* Font Manager functions */
171
Roger E. Masseed648a21997-01-17 16:08:55 +0000172static PyObject *
Sjoerd Mullender0ae52842000-07-12 10:43:11 +0000173fm_init(PyObject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000174{
Roger E. Masseed648a21997-01-17 16:08:55 +0000175 if (!PyArg_NoArgs(args))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000176 return NULL;
177 fminit();
Roger E. Masseed648a21997-01-17 16:08:55 +0000178 Py_INCREF(Py_None);
179 return Py_None;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000180}
181
Roger E. Masseed648a21997-01-17 16:08:55 +0000182static PyObject *
Sjoerd Mullender0ae52842000-07-12 10:43:11 +0000183fm_findfont(PyObject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000184{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000185 char *str;
Roger E. Masseed648a21997-01-17 16:08:55 +0000186 if (!PyArg_Parse(args, "s", &str))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000187 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000188 return newfhobject(fmfindfont(str));
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000189}
190
Roger E. Masseed648a21997-01-17 16:08:55 +0000191static PyObject *
Sjoerd Mullender0ae52842000-07-12 10:43:11 +0000192fm_prstr(PyObject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000193{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000194 char *str;
Roger E. Masseed648a21997-01-17 16:08:55 +0000195 if (!PyArg_Parse(args, "s", &str))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000196 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000197 fmprstr(str);
Roger E. Masseed648a21997-01-17 16:08:55 +0000198 Py_INCREF(Py_None);
199 return Py_None;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000200}
201
202/* XXX This uses a global variable as temporary! Not re-entrant! */
203
Roger E. Masseed648a21997-01-17 16:08:55 +0000204static PyObject *fontlist;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000205
206static void
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000207clientproc(char *fontname)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000208{
209 int err;
Roger E. Masseed648a21997-01-17 16:08:55 +0000210 PyObject *v;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000211 if (fontlist == NULL)
212 return;
Roger E. Masseed648a21997-01-17 16:08:55 +0000213 v = PyString_FromString(fontname);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000214 if (v == NULL)
215 err = -1;
216 else {
Roger E. Masseed648a21997-01-17 16:08:55 +0000217 err = PyList_Append(fontlist, v);
218 Py_DECREF(v);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000219 }
220 if (err != 0) {
Roger E. Masseed648a21997-01-17 16:08:55 +0000221 Py_DECREF(fontlist);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000222 fontlist = NULL;
223 }
224}
225
Roger E. Masseed648a21997-01-17 16:08:55 +0000226static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000227fm_enumerate(PyObject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000228{
Roger E. Masseed648a21997-01-17 16:08:55 +0000229 PyObject *res;
230 if (!PyArg_NoArgs(args))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000231 return NULL;
Roger E. Masseed648a21997-01-17 16:08:55 +0000232 fontlist = PyList_New(0);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000233 if (fontlist == NULL)
234 return NULL;
235 fmenumerate(clientproc);
236 res = fontlist;
237 fontlist = NULL;
238 return res;
239}
240
Roger E. Masseed648a21997-01-17 16:08:55 +0000241static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000242fm_setpath(PyObject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000243{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000244 char *str;
Roger E. Masseed648a21997-01-17 16:08:55 +0000245 if (!PyArg_Parse(args, "s", &str))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000246 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000247 fmsetpath(str);
Roger E. Masseed648a21997-01-17 16:08:55 +0000248 Py_INCREF(Py_None);
249 return Py_None;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000250}
251
Roger E. Masseed648a21997-01-17 16:08:55 +0000252static PyObject *
Sjoerd Mullender0ae52842000-07-12 10:43:11 +0000253fm_fontpath(PyObject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000254{
Roger E. Masseed648a21997-01-17 16:08:55 +0000255 if (!PyArg_NoArgs(args))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000256 return NULL;
Roger E. Masseed648a21997-01-17 16:08:55 +0000257 return PyString_FromString(fmfontpath());
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000258}
259
Roger E. Masseed648a21997-01-17 16:08:55 +0000260static PyMethodDef fm_methods[] = {
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000261 {"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 */
268};
269
270
271void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000272initfm(void)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000273{
Roger E. Masseed648a21997-01-17 16:08:55 +0000274 Py_InitModule("fm", fm_methods);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000275 fminit();
276}