0
0
mirror of https://github.com/python/cpython.git synced 2024-11-30 01:16:51 +01:00
cpython/Modules/fmmodule.c

263 lines
4.9 KiB
C
Raw Normal View History

1991-04-03 21:02:31 +02:00
/* Font Manager module */
#include "Python.h"
1991-04-03 21:02:31 +02:00
#include <gl.h>
#include <device.h>
#include <fmclient.h>
/* Font Handle object implementation */
typedef struct {
PyObject_HEAD
1991-04-03 21:02:31 +02:00
fmfonthandle fh_fh;
} fhobject;
static PyTypeObject Fhtype;
1991-04-03 21:02:31 +02:00
#define is_fhobject(v) ((v)->ob_type == &Fhtype)
static PyObject *
newfhobject(fmfonthandle fh)
1991-04-03 21:02:31 +02:00
{
fhobject *fhp;
if (fh == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"error creating new font handle");
1991-04-03 21:02:31 +02:00
return NULL;
}
fhp = PyObject_New(fhobject, &Fhtype);
1991-04-03 21:02:31 +02:00
if (fhp == NULL)
return NULL;
fhp->fh_fh = fh;
return (PyObject *)fhp;
1991-04-03 21:02:31 +02:00
}
/* Font Handle methods */
static PyObject *
fh_scalefont(fhobject *self, PyObject *args)
1991-04-03 21:02:31 +02:00
{
double size;
if (!PyArg_Parse(args, "d", &size))
1991-04-03 21:02:31 +02:00
return NULL;
return newfhobject(fmscalefont(self->fh_fh, size));
}
/* XXX fmmakefont */
static PyObject *
fh_setfont(fhobject *self)
1991-04-03 21:02:31 +02:00
{
fmsetfont(self->fh_fh);
Py_INCREF(Py_None);
return Py_None;
1991-04-03 21:02:31 +02:00
}
static PyObject *
fh_getfontname(fhobject *self)
1991-04-03 21:02:31 +02:00
{
char fontname[256];
int len;
len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
if (len < 0) {
PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
1991-04-03 21:02:31 +02:00
return NULL;
}
return PyString_FromStringAndSize(fontname, len);
1991-04-03 21:02:31 +02:00
}
static PyObject *
fh_getcomment(fhobject *self)
1991-04-03 21:02:31 +02:00
{
char comment[256];
int len;
len = fmgetcomment(self->fh_fh, sizeof comment, comment);
if (len < 0) {
PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
1991-04-03 21:02:31 +02:00
return NULL;
}
return PyString_FromStringAndSize(comment, len);
1991-04-03 21:02:31 +02:00
}
static PyObject *
fh_getfontinfo(fhobject *self)
1991-04-03 21:02:31 +02:00
{
fmfontinfo info;
if (fmgetfontinfo(self->fh_fh, &info) < 0) {
PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
1991-04-03 21:02:31 +02:00
return NULL;
}
return Py_BuildValue("(llllllll)",
info.printermatched,
info.fixed_width,
info.xorig,
info.yorig,
info.xsize,
info.ysize,
info.height,
info.nglyphs);
1991-04-03 21:02:31 +02:00
}
#if 0
static PyObject *
fh_getwholemetrics(fhobject *self, PyObject *args)
1991-04-03 21:02:31 +02:00
{
}
#endif
static PyObject *
fh_getstrwidth(fhobject *self, PyObject *args)
1991-04-03 21:02:31 +02:00
{
1992-01-27 17:51:30 +01:00
char *str;
if (!PyArg_Parse(args, "s", &str))
1991-04-03 21:02:31 +02:00
return NULL;
return PyInt_FromLong(fmgetstrwidth(self->fh_fh, str));
1991-04-03 21:02:31 +02:00
}
static PyMethodDef fh_methods[] = {
{"scalefont", (PyCFunction)fh_scalefont, METH_OLDARGS},
{"setfont", (PyCFunction)fh_setfont, METH_NOARGS},
{"getfontname", (PyCFunction)fh_getfontname, METH_NOARGS},
{"getcomment", (PyCFunction)fh_getcomment, METH_NOARGS},
{"getfontinfo", (PyCFunction)fh_getfontinfo, METH_NOARGS},
1991-04-03 21:02:31 +02:00
#if 0
{"getwholemetrics", (PyCFunction)fh_getwholemetrics, METH_OLDARGS},
1991-04-03 21:02:31 +02:00
#endif
{"getstrwidth", (PyCFunction)fh_getstrwidth, METH_OLDARGS},
1991-04-03 21:02:31 +02:00
{NULL, NULL} /* sentinel */
};
static PyObject *
fh_getattr(fhobject *fhp, char *name)
1991-04-03 21:02:31 +02:00
{
return Py_FindMethod(fh_methods, (PyObject *)fhp, name);
1991-04-03 21:02:31 +02:00
}
static void
fh_dealloc(fhobject *fhp)
1991-04-03 21:02:31 +02:00
{
fmfreefont(fhp->fh_fh);
PyObject_Del(fhp);
1991-04-03 21:02:31 +02:00
}
static PyTypeObject Fhtype = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/
"fm.font handle", /*tp_name*/
sizeof(fhobject), /*tp_size*/
0, /*tp_itemsize*/
1991-04-03 21:02:31 +02:00
/* methods */
(destructor)fh_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)fh_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
1991-04-03 21:02:31 +02:00
};
/* Font Manager functions */
static PyObject *
fm_init(PyObject *self)
1991-04-03 21:02:31 +02:00
{
fminit();
Py_INCREF(Py_None);
return Py_None;
1991-04-03 21:02:31 +02:00
}
static PyObject *
2000-07-12 12:43:11 +02:00
fm_findfont(PyObject *self, PyObject *args)
1991-04-03 21:02:31 +02:00
{
1992-01-27 17:51:30 +01:00
char *str;
if (!PyArg_Parse(args, "s", &str))
1991-04-03 21:02:31 +02:00
return NULL;
1992-01-27 17:51:30 +01:00
return newfhobject(fmfindfont(str));
1991-04-03 21:02:31 +02:00
}
static PyObject *
2000-07-12 12:43:11 +02:00
fm_prstr(PyObject *self, PyObject *args)
1991-04-03 21:02:31 +02:00
{
1992-01-27 17:51:30 +01:00
char *str;
if (!PyArg_Parse(args, "s", &str))
1991-04-03 21:02:31 +02:00
return NULL;
1992-01-27 17:51:30 +01:00
fmprstr(str);
Py_INCREF(Py_None);
return Py_None;
1991-04-03 21:02:31 +02:00
}
/* XXX This uses a global variable as temporary! Not re-entrant! */
static PyObject *fontlist;
1991-04-03 21:02:31 +02:00
static void
clientproc(char *fontname)
1991-04-03 21:02:31 +02:00
{
int err;
PyObject *v;
1991-04-03 21:02:31 +02:00
if (fontlist == NULL)
return;
v = PyString_FromString(fontname);
1991-04-03 21:02:31 +02:00
if (v == NULL)
err = -1;
else {
err = PyList_Append(fontlist, v);
Py_DECREF(v);
1991-04-03 21:02:31 +02:00
}
if (err != 0) {
Py_DECREF(fontlist);
1991-04-03 21:02:31 +02:00
fontlist = NULL;
}
}
static PyObject *
fm_enumerate(PyObject *self)
1991-04-03 21:02:31 +02:00
{
PyObject *res;
fontlist = PyList_New(0);
1991-04-03 21:02:31 +02:00
if (fontlist == NULL)
return NULL;
fmenumerate(clientproc);
res = fontlist;
fontlist = NULL;
return res;
}
static PyObject *
fm_setpath(PyObject *self, PyObject *args)
1991-04-03 21:02:31 +02:00
{
1992-01-27 17:51:30 +01:00
char *str;
if (!PyArg_Parse(args, "s", &str))
1991-04-03 21:02:31 +02:00
return NULL;
1992-01-27 17:51:30 +01:00
fmsetpath(str);
Py_INCREF(Py_None);
return Py_None;
1991-04-03 21:02:31 +02:00
}
static PyObject *
fm_fontpath(PyObject *self)
1991-04-03 21:02:31 +02:00
{
return PyString_FromString(fmfontpath());
1991-04-03 21:02:31 +02:00
}
static PyMethodDef fm_methods[] = {
{"init", fm_init, METH_NOARGS},
{"findfont", fm_findfont, METH_OLDARGS},
{"enumerate", fm_enumerate, METH_NOARGS},
{"prstr", fm_prstr, METH_OLDARGS},
{"setpath", fm_setpath, METH_OLDARGS},
{"fontpath", fm_fontpath, METH_NOARGS},
1991-04-03 21:02:31 +02:00
{NULL, NULL} /* sentinel */
};
void
initfm(void)
1991-04-03 21:02:31 +02:00
{
Py_InitModule("fm", fm_methods);
1991-04-03 21:02:31 +02:00
fminit();
}