cpython/Modules/fmmodule.c

332 lines
6.7 KiB
C
Raw Normal View History

1991-04-03 15:02:31 -04:00
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
1991-04-03 15:02:31 -04:00
All Rights Reserved
1996-10-25 11:44:06 -03:00
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
1991-04-03 15:02:31 -04:00
provided that the above copyright notice appear in all copies and that
1996-10-25 11:44:06 -03:00
both that copyright notice and this permission notice appear in
1991-04-03 15:02:31 -04:00
supporting documentation, and that the names of Stichting Mathematisch
1996-10-25 11:44:06 -03:00
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
1991-04-03 15:02:31 -04:00
1996-10-25 11:44:06 -03:00
While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
1991-04-03 15:02:31 -04:00
******************************************************************/
/* Font Manager module */
#include "Python.h"
1991-04-03 15:02:31 -04:00
#include <gl.h>
#include <device.h>
#include <fmclient.h>
/* Font Handle object implementation */
typedef struct {
PyObject_HEAD
1991-04-03 15:02:31 -04:00
fmfonthandle fh_fh;
} fhobject;
staticforward PyTypeObject Fhtype;
1991-04-03 15:02:31 -04:00
#define is_fhobject(v) ((v)->ob_type == &Fhtype)
static PyObject *
1991-04-03 15:02:31 -04:00
newfhobject(fh)
fmfonthandle fh;
{
fhobject *fhp;
if (fh == NULL) {
PyErr_SetString(PyExc_RuntimeError,
"error creating new font handle");
1991-04-03 15:02:31 -04:00
return NULL;
}
fhp = PyObject_New(fhobject, &Fhtype);
1991-04-03 15:02:31 -04:00
if (fhp == NULL)
return NULL;
fhp->fh_fh = fh;
return (PyObject *)fhp;
1991-04-03 15:02:31 -04:00
}
/* Font Handle methods */
static PyObject *
1991-04-03 15:02:31 -04:00
fh_scalefont(self, args)
fhobject *self;
PyObject *args;
1991-04-03 15:02:31 -04:00
{
double size;
if (!PyArg_Parse(args, "d", &size))
1991-04-03 15:02:31 -04:00
return NULL;
return newfhobject(fmscalefont(self->fh_fh, size));
}
/* XXX fmmakefont */
static PyObject *
1991-04-03 15:02:31 -04:00
fh_setfont(self, args)
fhobject *self;
PyObject *args;
1991-04-03 15:02:31 -04:00
{
if (!PyArg_NoArgs(args))
1991-04-03 15:02:31 -04:00
return NULL;
fmsetfont(self->fh_fh);
Py_INCREF(Py_None);
return Py_None;
1991-04-03 15:02:31 -04:00
}
static PyObject *
1991-04-03 15:02:31 -04:00
fh_getfontname(self, args)
fhobject *self;
PyObject *args;
1991-04-03 15:02:31 -04:00
{
char fontname[256];
int len;
if (!PyArg_NoArgs(args))
1991-04-03 15:02:31 -04:00
return NULL;
len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
if (len < 0) {
PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
1991-04-03 15:02:31 -04:00
return NULL;
}
return PyString_FromStringAndSize(fontname, len);
1991-04-03 15:02:31 -04:00
}
static PyObject *
1991-04-03 15:02:31 -04:00
fh_getcomment(self, args)
fhobject *self;
PyObject *args;
1991-04-03 15:02:31 -04:00
{
char comment[256];
int len;
if (!PyArg_NoArgs(args))
1991-04-03 15:02:31 -04:00
return NULL;
len = fmgetcomment(self->fh_fh, sizeof comment, comment);
if (len < 0) {
PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
1991-04-03 15:02:31 -04:00
return NULL;
}
return PyString_FromStringAndSize(comment, len);
1991-04-03 15:02:31 -04:00
}
static PyObject *
1991-04-03 15:02:31 -04:00
fh_getfontinfo(self, args)
fhobject *self;
PyObject *args;
1991-04-03 15:02:31 -04:00
{
fmfontinfo info;
if (!PyArg_NoArgs(args))
1991-04-03 15:02:31 -04:00
return NULL;
if (fmgetfontinfo(self->fh_fh, &info) < 0) {
PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
1991-04-03 15:02:31 -04: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 15:02:31 -04:00
}
#if 0
static PyObject *
1991-04-03 15:02:31 -04:00
fh_getwholemetrics(self, args)
fhobject *self;
PyObject *args;
1991-04-03 15:02:31 -04:00
{
}
#endif
static PyObject *
1991-04-03 15:02:31 -04:00
fh_getstrwidth(self, args)
fhobject *self;
PyObject *args;
1991-04-03 15:02:31 -04:00
{
1992-01-27 12:51:30 -04:00
char *str;
if (!PyArg_Parse(args, "s", &str))
1991-04-03 15:02:31 -04:00
return NULL;
return PyInt_FromLong(fmgetstrwidth(self->fh_fh, str));
1991-04-03 15:02:31 -04:00
}
static PyMethodDef fh_methods[] = {
{"scalefont", (PyCFunction)fh_scalefont},
{"setfont", (PyCFunction)fh_setfont},
{"getfontname", (PyCFunction)fh_getfontname},
{"getcomment", (PyCFunction)fh_getcomment},
{"getfontinfo", (PyCFunction)fh_getfontinfo},
1991-04-03 15:02:31 -04:00
#if 0
{"getwholemetrics", (PyCFunction)fh_getwholemetrics},
1991-04-03 15:02:31 -04:00
#endif
{"getstrwidth", (PyCFunction)fh_getstrwidth},
1991-04-03 15:02:31 -04:00
{NULL, NULL} /* sentinel */
};
static PyObject *
1991-04-03 15:02:31 -04:00
fh_getattr(fhp, name)
fhobject *fhp;
char *name;
1991-04-03 15:02:31 -04:00
{
return Py_FindMethod(fh_methods, (PyObject *)fhp, name);
1991-04-03 15:02:31 -04:00
}
static void
fh_dealloc(fhp)
fhobject *fhp;
{
fmfreefont(fhp->fh_fh);
PyObject_Del(fhp);
1991-04-03 15:02:31 -04:00
}
static PyTypeObject Fhtype = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/
"font handle", /*tp_name*/
sizeof(fhobject), /*tp_size*/
0, /*tp_itemsize*/
1991-04-03 15:02:31 -04: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 15:02:31 -04:00
};
/* Font Manager functions */
static PyObject *
1991-04-03 15:02:31 -04:00
fm_init(self, args)
PyObject *self, *args;
1991-04-03 15:02:31 -04:00
{
if (!PyArg_NoArgs(args))
1991-04-03 15:02:31 -04:00
return NULL;
fminit();
Py_INCREF(Py_None);
return Py_None;
1991-04-03 15:02:31 -04:00
}
static PyObject *
1991-04-03 15:02:31 -04:00
fm_findfont(self, args)
PyObject *self, *args;
1991-04-03 15:02:31 -04:00
{
1992-01-27 12:51:30 -04:00
char *str;
if (!PyArg_Parse(args, "s", &str))
1991-04-03 15:02:31 -04:00
return NULL;
1992-01-27 12:51:30 -04:00
return newfhobject(fmfindfont(str));
1991-04-03 15:02:31 -04:00
}
static PyObject *
1991-04-03 15:02:31 -04:00
fm_prstr(self, args)
PyObject *self, *args;
1991-04-03 15:02:31 -04:00
{
1992-01-27 12:51:30 -04:00
char *str;
if (!PyArg_Parse(args, "s", &str))
1991-04-03 15:02:31 -04:00
return NULL;
1992-01-27 12:51:30 -04:00
fmprstr(str);
Py_INCREF(Py_None);
return Py_None;
1991-04-03 15:02:31 -04:00
}
/* XXX This uses a global variable as temporary! Not re-entrant! */
static PyObject *fontlist;
1991-04-03 15:02:31 -04:00
static void
clientproc(fontname)
char *fontname;
{
int err;
PyObject *v;
1991-04-03 15:02:31 -04:00
if (fontlist == NULL)
return;
v = PyString_FromString(fontname);
1991-04-03 15:02:31 -04:00
if (v == NULL)
err = -1;
else {
err = PyList_Append(fontlist, v);
Py_DECREF(v);
1991-04-03 15:02:31 -04:00
}
if (err != 0) {
Py_DECREF(fontlist);
1991-04-03 15:02:31 -04:00
fontlist = NULL;
}
}
static PyObject *
1991-04-03 15:02:31 -04:00
fm_enumerate(self, args)
PyObject *self, *args;
1991-04-03 15:02:31 -04:00
{
PyObject *res;
if (!PyArg_NoArgs(args))
1991-04-03 15:02:31 -04:00
return NULL;
fontlist = PyList_New(0);
1991-04-03 15:02:31 -04:00
if (fontlist == NULL)
return NULL;
fmenumerate(clientproc);
res = fontlist;
fontlist = NULL;
return res;
}
static PyObject *
1991-04-03 15:02:31 -04:00
fm_setpath(self, args)
PyObject *self, *args;
1991-04-03 15:02:31 -04:00
{
1992-01-27 12:51:30 -04:00
char *str;
if (!PyArg_Parse(args, "s", &str))
1991-04-03 15:02:31 -04:00
return NULL;
1992-01-27 12:51:30 -04:00
fmsetpath(str);
Py_INCREF(Py_None);
return Py_None;
1991-04-03 15:02:31 -04:00
}
static PyObject *
1991-04-03 15:02:31 -04:00
fm_fontpath(self, args)
PyObject *self, *args;
1991-04-03 15:02:31 -04:00
{
if (!PyArg_NoArgs(args))
1991-04-03 15:02:31 -04:00
return NULL;
return PyString_FromString(fmfontpath());
1991-04-03 15:02:31 -04:00
}
static PyMethodDef fm_methods[] = {
1991-04-03 15:02:31 -04:00
{"init", fm_init},
{"findfont", fm_findfont},
{"enumerate", fm_enumerate},
{"prstr", fm_prstr},
{"setpath", fm_setpath},
{"fontpath", fm_fontpath},
{NULL, NULL} /* sentinel */
};
void
initfm()
{
Py_InitModule("fm", fm_methods);
1991-04-03 15:02:31 -04:00
fminit();
}