blob: 3a741070c0b2813715bc0b23ee17105ad44481cc [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 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000014 PyObject_HEAD
15 fmfonthandle fh_fh;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000016} fhobject;
17
Jeremy Hylton938ace62002-07-17 16:30:39 +000018static PyTypeObject Fhtype;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000019
Antoine Pitrouc83ea132010-05-09 14:46:46 +000020#define is_fhobject(v) ((v)->ob_type == &Fhtype)
Guido van Rossuma0c191e1991-04-03 19:02:31 +000021
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000025 fhobject *fhp;
26 if (fh == NULL) {
27 PyErr_SetString(PyExc_RuntimeError,
28 "error creating new font handle");
29 return NULL;
30 }
31 fhp = PyObject_New(fhobject, &Fhtype);
32 if (fhp == NULL)
33 return NULL;
34 fhp->fh_fh = fh;
35 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000043 double size;
44 if (!PyArg_ParseTuple(args, "d", &size))
45 return NULL;
46 return newfhobject(fmscalefont(self->fh_fh, size));
Guido van Rossuma0c191e1991-04-03 19:02:31 +000047}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000054 fmsetfont(self->fh_fh);
55 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000062 char fontname[256];
63 int len;
64 len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
65 if (len < 0) {
66 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
67 return NULL;
68 }
69 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000075 char comment[256];
76 int len;
77 len = fmgetcomment(self->fh_fh, sizeof comment, comment);
78 if (len < 0) {
79 PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
80 return NULL;
81 }
82 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000088 fmfontinfo info;
89 if (fmgetfontinfo(self->fh_fh, &info) < 0) {
90 PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
91 return NULL;
92 }
93 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000114 char *str;
115 if (!PyArg_ParseTuple(args, "s", &str))
116 return NULL;
117 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[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 {"scalefont", (PyCFunction)fh_scalefont, METH_VARARGS},
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
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000127 {"getwholemetrics", (PyCFunction)fh_getwholemetrics, METH_VARARGS},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000128#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000129 {"getstrwidth", (PyCFunction)fh_getstrwidth, METH_VARARGS},
130 {NULL, NULL} /* sentinel */
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000131};
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000142 fmfreefont(fhp->fh_fh);
143 PyObject_Del(fhp);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000144}
145
Roger E. Masseed648a21997-01-17 16:08:55 +0000146static PyTypeObject Fhtype = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 PyObject_HEAD_INIT(&PyType_Type)
148 0, /*ob_size*/
149 "fm.font handle", /*tp_name*/
150 sizeof(fhobject), /*tp_size*/
151 0, /*tp_itemsize*/
152 /* methods */
153 (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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 fminit();
168 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000175 char *str;
176 if (!PyArg_ParseTuple(args, "s", &str))
177 return NULL;
178 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000184 char *str;
185 if (!PyArg_ParseTuple(args, "s", &str))
186 return NULL;
187 fmprstr(str);
188 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 int err;
200 PyObject *v;
201 if (fontlist == NULL)
202 return;
203 v = PyString_FromString(fontname);
204 if (v == NULL)
205 err = -1;
206 else {
207 err = PyList_Append(fontlist, v);
208 Py_DECREF(v);
209 }
210 if (err != 0) {
211 Py_DECREF(fontlist);
212 fontlist = NULL;
213 }
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000214}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000219 PyObject *res;
220 fontlist = PyList_New(0);
221 if (fontlist == NULL)
222 return NULL;
223 fmenumerate(clientproc);
224 res = fontlist;
225 fontlist = NULL;
226 return res;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000227}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000232 char *str;
233 if (!PyArg_ParseTuple(args, "s", &str))
234 return NULL;
235 fmsetpath(str);
236 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +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[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247 {"init", fm_init, METH_NOARGS},
248 {"findfont", fm_findfont, METH_VARARGS},
249 {"enumerate", fm_enumerate, METH_NOARGS},
250 {"prstr", fm_prstr, METH_VARARGS},
251 {"setpath", fm_setpath, METH_VARARGS},
252 {"fontpath", fm_fontpath, METH_NOARGS},
253 {NULL, NULL} /* sentinel */
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000254};
255
256
257void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000258initfm(void)
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000259{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000260
Brett Cannon75ba4652008-05-15 03:23:17 +0000261 if (PyErr_WarnPy3k("the fm module has been removed in "
262 "Python 3.0", 2) < 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000263 return;
264
265 Py_InitModule("fm", fm_methods);
266 if (m == NULL)
Brett Cannon75ba4652008-05-15 03:23:17 +0000267 return;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000268 fminit();
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000269}