2 /* Font Manager module */
11 /* Font Handle object implementation */
18 staticforward 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_Parse(args
, "d", &size
))
46 return newfhobject(fmscalefont(self
->fh_fh
, size
));
52 fh_setfont(fhobject
*self
, PyObject
*args
)
54 if (!PyArg_NoArgs(args
))
56 fmsetfont(self
->fh_fh
);
62 fh_getfontname(fhobject
*self
, PyObject
*args
)
66 if (!PyArg_NoArgs(args
))
68 len
= fmgetfontname(self
->fh_fh
, sizeof fontname
, fontname
);
70 PyErr_SetString(PyExc_RuntimeError
, "error in fmgetfontname");
73 return PyString_FromStringAndSize(fontname
, len
);
77 fh_getcomment(fhobject
*self
, PyObject
*args
)
81 if (!PyArg_NoArgs(args
))
83 len
= fmgetcomment(self
->fh_fh
, sizeof comment
, comment
);
85 PyErr_SetString(PyExc_RuntimeError
, "error in fmgetcomment");
88 return PyString_FromStringAndSize(comment
, len
);
92 fh_getfontinfo(fhobject
*self
, PyObject
*args
)
95 if (!PyArg_NoArgs(args
))
97 if (fmgetfontinfo(self
->fh_fh
, &info
) < 0) {
98 PyErr_SetString(PyExc_RuntimeError
, "error in fmgetfontinfo");
101 return Py_BuildValue("(llllllll)",
114 fh_getwholemetrics(fhobject
*self
, PyObject
*args
)
120 fh_getstrwidth(fhobject
*self
, PyObject
*args
)
123 if (!PyArg_Parse(args
, "s", &str
))
125 return PyInt_FromLong(fmgetstrwidth(self
->fh_fh
, str
));
128 static 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
},
135 {"getwholemetrics", (PyCFunction
)fh_getwholemetrics
},
137 {"getstrwidth", (PyCFunction
)fh_getstrwidth
},
138 {NULL
, NULL
} /* sentinel */
142 fh_getattr(fhobject
*fhp
, char *name
)
144 return Py_FindMethod(fh_methods
, (PyObject
*)fhp
, name
);
148 fh_dealloc(fhobject
*fhp
)
150 fmfreefont(fhp
->fh_fh
);
154 static PyTypeObject Fhtype
= {
155 PyObject_HEAD_INIT(&PyType_Type
)
157 "font handle", /*tp_name*/
158 sizeof(fhobject
), /*tp_size*/
161 (destructor
)fh_dealloc
, /*tp_dealloc*/
163 (getattrfunc
)fh_getattr
, /*tp_getattr*/
170 /* Font Manager functions */
173 fm_init(PyObject
*self
, PyObject
*args
)
175 if (!PyArg_NoArgs(args
))
183 fm_findfont(PyObject
*self
, PyObject
*args
)
186 if (!PyArg_Parse(args
, "s", &str
))
188 return newfhobject(fmfindfont(str
));
192 fm_prstr(PyObject
*self
, PyObject
*args
)
195 if (!PyArg_Parse(args
, "s", &str
))
202 /* XXX This uses a global variable as temporary! Not re-entrant! */
204 static PyObject
*fontlist
;
207 clientproc(char *fontname
)
211 if (fontlist
== NULL
)
213 v
= PyString_FromString(fontname
);
217 err
= PyList_Append(fontlist
, v
);
227 fm_enumerate(PyObject
*self
, PyObject
*args
)
230 if (!PyArg_NoArgs(args
))
232 fontlist
= PyList_New(0);
233 if (fontlist
== NULL
)
235 fmenumerate(clientproc
);
242 fm_setpath(PyObject
*self
, PyObject
*args
)
245 if (!PyArg_Parse(args
, "s", &str
))
253 fm_fontpath(PyObject
*self
, PyObject
*args
)
255 if (!PyArg_NoArgs(args
))
257 return PyString_FromString(fmfontpath());
260 static PyMethodDef fm_methods
[] = {
262 {"findfont", fm_findfont
},
263 {"enumerate", fm_enumerate
},
265 {"setpath", fm_setpath
},
266 {"fontpath", fm_fontpath
},
267 {NULL
, NULL
} /* sentinel */
274 Py_InitModule("fm", fm_methods
);