1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Font Manager module */
20 /* Font Handle object implementation */
27 staticforward PyTypeObject Fhtype
;
29 #define is_fhobject(v) ((v)->ob_type == &Fhtype)
32 newfhobject(fmfonthandle fh
)
36 PyErr_SetString(PyExc_RuntimeError
,
37 "error creating new font handle");
40 fhp
= PyObject_New(fhobject
, &Fhtype
);
44 return (PyObject
*)fhp
;
47 /* Font Handle methods */
50 fh_scalefont(fhobject
*self
, PyObject
*args
)
53 if (!PyArg_Parse(args
, "d", &size
))
55 return newfhobject(fmscalefont(self
->fh_fh
, size
));
61 fh_setfont(fhobject
*self
, PyObject
*args
)
63 if (!PyArg_NoArgs(args
))
65 fmsetfont(self
->fh_fh
);
71 fh_getfontname(fhobject
*self
, PyObject
*args
)
75 if (!PyArg_NoArgs(args
))
77 len
= fmgetfontname(self
->fh_fh
, sizeof fontname
, fontname
);
79 PyErr_SetString(PyExc_RuntimeError
, "error in fmgetfontname");
82 return PyString_FromStringAndSize(fontname
, len
);
86 fh_getcomment(fhobject
*self
, PyObject
*args
)
90 if (!PyArg_NoArgs(args
))
92 len
= fmgetcomment(self
->fh_fh
, sizeof comment
, comment
);
94 PyErr_SetString(PyExc_RuntimeError
, "error in fmgetcomment");
97 return PyString_FromStringAndSize(comment
, len
);
101 fh_getfontinfo(fhobject
*self
, PyObject
*args
)
104 if (!PyArg_NoArgs(args
))
106 if (fmgetfontinfo(self
->fh_fh
, &info
) < 0) {
107 PyErr_SetString(PyExc_RuntimeError
, "error in fmgetfontinfo");
110 return Py_BuildValue("(llllllll)",
123 fh_getwholemetrics(fhobject
*self
, PyObject
*args
)
129 fh_getstrwidth(fhobject
*self
, PyObject
*args
)
132 if (!PyArg_Parse(args
, "s", &str
))
134 return PyInt_FromLong(fmgetstrwidth(self
->fh_fh
, str
));
137 static PyMethodDef fh_methods
[] = {
138 {"scalefont", (PyCFunction
)fh_scalefont
},
139 {"setfont", (PyCFunction
)fh_setfont
},
140 {"getfontname", (PyCFunction
)fh_getfontname
},
141 {"getcomment", (PyCFunction
)fh_getcomment
},
142 {"getfontinfo", (PyCFunction
)fh_getfontinfo
},
144 {"getwholemetrics", (PyCFunction
)fh_getwholemetrics
},
146 {"getstrwidth", (PyCFunction
)fh_getstrwidth
},
147 {NULL
, NULL
} /* sentinel */
151 fh_getattr(fhobject
*fhp
, char *name
)
153 return Py_FindMethod(fh_methods
, (PyObject
*)fhp
, name
);
157 fh_dealloc(fhobject
*fhp
)
159 fmfreefont(fhp
->fh_fh
);
163 static PyTypeObject Fhtype
= {
164 PyObject_HEAD_INIT(&PyType_Type
)
166 "font handle", /*tp_name*/
167 sizeof(fhobject
), /*tp_size*/
170 (destructor
)fh_dealloc
, /*tp_dealloc*/
172 (getattrfunc
)fh_getattr
, /*tp_getattr*/
179 /* Font Manager functions */
182 fm_init(PyObject
*self
, PyObject
*args
)
184 if (!PyArg_NoArgs(args
))
192 fm_findfont(PyObject
*self
, PyObject
*args
)
195 if (!PyArg_Parse(args
, "s", &str
))
197 return newfhobject(fmfindfont(str
));
201 fm_prstr(PyObject
*self
, PyObject
*args
)
204 if (!PyArg_Parse(args
, "s", &str
))
211 /* XXX This uses a global variable as temporary! Not re-entrant! */
213 static PyObject
*fontlist
;
216 clientproc(char *fontname
)
220 if (fontlist
== NULL
)
222 v
= PyString_FromString(fontname
);
226 err
= PyList_Append(fontlist
, v
);
236 fm_enumerate(PyObject
*self
, PyObject
*args
)
239 if (!PyArg_NoArgs(args
))
241 fontlist
= PyList_New(0);
242 if (fontlist
== NULL
)
244 fmenumerate(clientproc
);
251 fm_setpath(PyObject
*self
, PyObject
*args
)
254 if (!PyArg_Parse(args
, "s", &str
))
262 fm_fontpath(PyObject
*self
, PyObject
*args
)
264 if (!PyArg_NoArgs(args
))
266 return PyString_FromString(fmfontpath());
269 static PyMethodDef fm_methods
[] = {
271 {"findfont", fm_findfont
},
272 {"enumerate", fm_enumerate
},
274 {"setpath", fm_setpath
},
275 {"fontpath", fm_fontpath
},
276 {NULL
, NULL
} /* sentinel */
283 Py_InitModule("fm", fm_methods
);