2 /* Font Manager module */
11 /* Font Handle object implementation */
18 static PyTypeObject Fhtype
;
20 #define is_fhobject(v) ((v)->ob_type == &Fhtype)
23 newfhobject(fmfonthandle fh
)
27 PyErr_SetString(PyExc_RuntimeError
,
28 "error creating new font handle");
31 fhp
= PyObject_New(fhobject
, &Fhtype
);
35 return (PyObject
*)fhp
;
38 /* Font Handle methods */
41 fh_scalefont(fhobject
*self
, PyObject
*args
)
44 if (!PyArg_ParseTuple(args
, "d", &size
))
46 return newfhobject(fmscalefont(self
->fh_fh
, size
));
52 fh_setfont(fhobject
*self
)
54 fmsetfont(self
->fh_fh
);
60 fh_getfontname(fhobject
*self
)
64 len
= fmgetfontname(self
->fh_fh
, sizeof fontname
, fontname
);
66 PyErr_SetString(PyExc_RuntimeError
, "error in fmgetfontname");
69 return PyString_FromStringAndSize(fontname
, len
);
73 fh_getcomment(fhobject
*self
)
77 len
= fmgetcomment(self
->fh_fh
, sizeof comment
, comment
);
79 PyErr_SetString(PyExc_RuntimeError
, "error in fmgetcomment");
82 return PyString_FromStringAndSize(comment
, len
);
86 fh_getfontinfo(fhobject
*self
)
89 if (fmgetfontinfo(self
->fh_fh
, &info
) < 0) {
90 PyErr_SetString(PyExc_RuntimeError
, "error in fmgetfontinfo");
93 return Py_BuildValue("(llllllll)",
106 fh_getwholemetrics(fhobject
*self
, PyObject
*args
)
112 fh_getstrwidth(fhobject
*self
, PyObject
*args
)
115 if (!PyArg_ParseTuple(args
, "s", &str
))
117 return PyInt_FromLong(fmgetstrwidth(self
->fh_fh
, str
));
120 static PyMethodDef fh_methods
[] = {
121 {"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
},
127 {"getwholemetrics", (PyCFunction
)fh_getwholemetrics
, METH_VARARGS
},
129 {"getstrwidth", (PyCFunction
)fh_getstrwidth
, METH_VARARGS
},
130 {NULL
, NULL
} /* sentinel */
134 fh_getattr(fhobject
*fhp
, char *name
)
136 return Py_FindMethod(fh_methods
, (PyObject
*)fhp
, name
);
140 fh_dealloc(fhobject
*fhp
)
142 fmfreefont(fhp
->fh_fh
);
146 static PyTypeObject Fhtype
= {
147 PyObject_HEAD_INIT(&PyType_Type
)
149 "fm.font handle", /*tp_name*/
150 sizeof(fhobject
), /*tp_size*/
153 (destructor
)fh_dealloc
, /*tp_dealloc*/
155 (getattrfunc
)fh_getattr
, /*tp_getattr*/
162 /* Font Manager functions */
165 fm_init(PyObject
*self
)
173 fm_findfont(PyObject
*self
, PyObject
*args
)
176 if (!PyArg_ParseTuple(args
, "s", &str
))
178 return newfhobject(fmfindfont(str
));
182 fm_prstr(PyObject
*self
, PyObject
*args
)
185 if (!PyArg_ParseTuple(args
, "s", &str
))
192 /* XXX This uses a global variable as temporary! Not re-entrant! */
194 static PyObject
*fontlist
;
197 clientproc(char *fontname
)
201 if (fontlist
== NULL
)
203 v
= PyString_FromString(fontname
);
207 err
= PyList_Append(fontlist
, v
);
217 fm_enumerate(PyObject
*self
)
220 fontlist
= PyList_New(0);
221 if (fontlist
== NULL
)
223 fmenumerate(clientproc
);
230 fm_setpath(PyObject
*self
, PyObject
*args
)
233 if (!PyArg_ParseTuple(args
, "s", &str
))
241 fm_fontpath(PyObject
*self
)
243 return PyString_FromString(fmfontpath());
246 static PyMethodDef fm_methods
[] = {
247 {"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 */
261 if (PyErr_WarnPy3k("the fm module has been removed in "
262 "Python 3.0", 2) < 0)
265 Py_InitModule("fm", fm_methods
);