blob: 78a58772c2ac0f7bcba411ee4dfdad98ff96610c [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
Jeremy Hylton938ace62002-07-17 16:30:39 +000018static 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 *
Neal Norwitz50905b52002-03-31 14:57:24 +000052fh_setfont(fhobject *self)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000053{
Guido van Rossuma0c191e1991-04-03 19:02:31 +000054 fmsetfont(self->fh_fh);
Roger E. Masseed648a21997-01-17 16:08:55 +000055 Py_INCREF(Py_None);
56 return Py_None;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000057}
58
Roger E. Masseed648a21997-01-17 16:08:55 +000059static PyObject *
Neal Norwitz50905b52002-03-31 14:57:24 +000060fh_getfontname(fhobject *self)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000061{
62 char fontname[256];
63 int len;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000064 len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
65 if (len < 0) {
Roger E. Masseed648a21997-01-17 16:08:55 +000066 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
Guido van Rossuma0c191e1991-04-03 19:02:31 +000067 return NULL;
68 }
Roger E. Masseed648a21997-01-17 16:08:55 +000069 return PyString_FromStringAndSize(fontname, len);
Guido van Rossuma0c191e1991-04-03 19:02:31 +000070}
71
Roger E. Masseed648a21997-01-17 16:08:55 +000072static PyObject *
Neal Norwitz50905b52002-03-31 14:57:24 +000073fh_getcomment(fhobject *self)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000074{
75 char comment[256];
76 int len;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000077 len = fmgetcomment(self->fh_fh, sizeof comment, comment);
78 if (len < 0) {
Roger E. Masseed648a21997-01-17 16:08:55 +000079 PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
Guido van Rossuma0c191e1991-04-03 19:02:31 +000080 return NULL;
81 }
Roger E. Masseed648a21997-01-17 16:08:55 +000082 return PyString_FromStringAndSize(comment, len);
Guido van Rossuma0c191e1991-04-03 19:02:31 +000083}
84
Roger E. Masseed648a21997-01-17 16:08:55 +000085static PyObject *
Neal Norwitz50905b52002-03-31 14:57:24 +000086fh_getfontinfo(fhobject *self)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000087{
88 fmfontinfo info;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000089 if (fmgetfontinfo(self->fh_fh, &info) < 0) {
Roger E. Masseed648a21997-01-17 16:08:55 +000090 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
Guido van Rossuma0c191e1991-04-03 19:02:31 +000091 return NULL;
92 }
Roger E. Masseed648a21997-01-17 16:08:55 +000093 return Py_BuildValue("(llllllll)",
94 info.printermatched,
95 info.fixed_width,
96 info.xorig,
97 info.yorig,
98 info.xsize,
99 info.ysize,
100 info.height,
101 info.nglyphs);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000102}
103
104#if 0
Roger E. Masseed648a21997-01-17 16:08:55 +0000105static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000106fh_getwholemetrics(fhobject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000107{
108}
109#endif
110
Roger E. Masseed648a21997-01-17 16:08:55 +0000111static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000112fh_getstrwidth(fhobject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000113{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000114 char *str;
Roger E. Masseed648a21997-01-17 16:08:55 +0000115 if (!PyArg_Parse(args, "s", &str))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000116 return NULL;
Roger E. Masseed648a21997-01-17 16:08:55 +0000117 return PyInt_FromLong(fmgetstrwidth(self->fh_fh, str));
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000118}
119
Roger E. Masseed648a21997-01-17 16:08:55 +0000120static PyMethodDef fh_methods[] = {
Neal Norwitz50905b52002-03-31 14:57:24 +0000121 {"scalefont", (PyCFunction)fh_scalefont, METH_OLDARGS},
122 {"setfont", (PyCFunction)fh_setfont, METH_NOARGS},
123 {"getfontname", (PyCFunction)fh_getfontname, METH_NOARGS},
124 {"getcomment", (PyCFunction)fh_getcomment, METH_NOARGS},
125 {"getfontinfo", (PyCFunction)fh_getfontinfo, METH_NOARGS},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000126#if 0
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000127 {"getwholemetrics", (PyCFunction)fh_getwholemetrics, METH_OLDARGS},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000128#endif
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000129 {"getstrwidth", (PyCFunction)fh_getstrwidth, METH_OLDARGS},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000130 {NULL, NULL} /* sentinel */
131};
132
Roger E. Masseed648a21997-01-17 16:08:55 +0000133static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000134fh_getattr(fhobject *fhp, char *name)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000135{
Roger E. Masseed648a21997-01-17 16:08:55 +0000136 return Py_FindMethod(fh_methods, (PyObject *)fhp, name);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000137}
138
139static void
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000140fh_dealloc(fhobject *fhp)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000141{
142 fmfreefont(fhp->fh_fh);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000143 PyObject_Del(fhp);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000144}
145
Roger E. Masseed648a21997-01-17 16:08:55 +0000146static PyTypeObject Fhtype = {
147 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000149 "fm.font handle", /*tp_name*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150 sizeof(fhobject), /*tp_size*/
151 0, /*tp_itemsize*/
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000152 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153 (destructor)fh_dealloc, /*tp_dealloc*/
154 0, /*tp_print*/
155 (getattrfunc)fh_getattr, /*tp_getattr*/
156 0, /*tp_setattr*/
157 0, /*tp_compare*/
158 0, /*tp_repr*/
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000159};
160
161
162/* Font Manager functions */
163
Roger E. Masseed648a21997-01-17 16:08:55 +0000164static PyObject *
Neal Norwitz50905b52002-03-31 14:57:24 +0000165fm_init(PyObject *self)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000166{
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000167 fminit();
Roger E. Masseed648a21997-01-17 16:08:55 +0000168 Py_INCREF(Py_None);
169 return Py_None;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000170}
171
Roger E. Masseed648a21997-01-17 16:08:55 +0000172static PyObject *
Sjoerd Mullender0ae52842000-07-12 10:43:11 +0000173fm_findfont(PyObject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000174{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000175 char *str;
Roger E. Masseed648a21997-01-17 16:08:55 +0000176 if (!PyArg_Parse(args, "s", &str))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000177 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000178 return newfhobject(fmfindfont(str));
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000179}
180
Roger E. Masseed648a21997-01-17 16:08:55 +0000181static PyObject *
Sjoerd Mullender0ae52842000-07-12 10:43:11 +0000182fm_prstr(PyObject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000183{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000184 char *str;
Roger E. Masseed648a21997-01-17 16:08:55 +0000185 if (!PyArg_Parse(args, "s", &str))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000186 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000187 fmprstr(str);
Roger E. Masseed648a21997-01-17 16:08:55 +0000188 Py_INCREF(Py_None);
189 return Py_None;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000190}
191
192/* XXX This uses a global variable as temporary! Not re-entrant! */
193
Roger E. Masseed648a21997-01-17 16:08:55 +0000194static PyObject *fontlist;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000195
196static void
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000197clientproc(char *fontname)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000198{
199 int err;
Roger E. Masseed648a21997-01-17 16:08:55 +0000200 PyObject *v;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000201 if (fontlist == NULL)
202 return;
Roger E. Masseed648a21997-01-17 16:08:55 +0000203 v = PyString_FromString(fontname);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000204 if (v == NULL)
205 err = -1;
206 else {
Roger E. Masseed648a21997-01-17 16:08:55 +0000207 err = PyList_Append(fontlist, v);
208 Py_DECREF(v);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000209 }
210 if (err != 0) {
Roger E. Masseed648a21997-01-17 16:08:55 +0000211 Py_DECREF(fontlist);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000212 fontlist = NULL;
213 }
214}
215
Roger E. Masseed648a21997-01-17 16:08:55 +0000216static PyObject *
Neal Norwitz50905b52002-03-31 14:57:24 +0000217fm_enumerate(PyObject *self)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000218{
Roger E. Masseed648a21997-01-17 16:08:55 +0000219 PyObject *res;
Roger E. Masseed648a21997-01-17 16:08:55 +0000220 fontlist = PyList_New(0);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000221 if (fontlist == NULL)
222 return NULL;
223 fmenumerate(clientproc);
224 res = fontlist;
225 fontlist = NULL;
226 return res;
227}
228
Roger E. Masseed648a21997-01-17 16:08:55 +0000229static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000230fm_setpath(PyObject *self, PyObject *args)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000231{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000232 char *str;
Roger E. Masseed648a21997-01-17 16:08:55 +0000233 if (!PyArg_Parse(args, "s", &str))
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000234 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000235 fmsetpath(str);
Roger E. Masseed648a21997-01-17 16:08:55 +0000236 Py_INCREF(Py_None);
237 return Py_None;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000238}
239
Roger E. Masseed648a21997-01-17 16:08:55 +0000240static PyObject *
Neal Norwitz50905b52002-03-31 14:57:24 +0000241fm_fontpath(PyObject *self)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000242{
Roger E. Masseed648a21997-01-17 16:08:55 +0000243 return PyString_FromString(fmfontpath());
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000244}
245
Roger E. Masseed648a21997-01-17 16:08:55 +0000246static PyMethodDef fm_methods[] = {
Neal Norwitz50905b52002-03-31 14:57:24 +0000247 {"init", fm_init, METH_NOARGS},
248 {"findfont", fm_findfont, METH_OLDARGS},
249 {"enumerate", fm_enumerate, METH_NOARGS},
250 {"prstr", fm_prstr, METH_OLDARGS},
251 {"setpath", fm_setpath, METH_OLDARGS},
252 {"fontpath", fm_fontpath, METH_NOARGS},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000253 {NULL, NULL} /* sentinel */
254};
255
256
257void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000258initfm(void)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000259{
Roger E. Masseed648a21997-01-17 16:08:55 +0000260 Py_InitModule("fm", fm_methods);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000261 fminit();
262}