home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Modules / fmmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  5.1 KB  |  277 lines

  1.  
  2. /* Font Manager module */
  3.  
  4. #include "Python.h"
  5.  
  6. #include <gl.h>
  7. #include <device.h>
  8. #include <fmclient.h>
  9.  
  10.  
  11. /* Font Handle object implementation */
  12.  
  13. typedef struct {
  14.     PyObject_HEAD
  15.     fmfonthandle fh_fh;
  16. } fhobject;
  17.  
  18. staticforward PyTypeObject Fhtype;
  19.  
  20. #define is_fhobject(v)        ((v)->ob_type == &Fhtype)
  21.  
  22. static PyObject *
  23. newfhobject(fmfonthandle fh)
  24. {
  25.     fhobject *fhp;
  26.     if (fh == NULL) {
  27.         PyErr_SetString(PyExc_RuntimeError,
  28.                 "error creating new font handle");
  29.         return NULL;
  30.     }
  31.     fhp = PyObject_New(fhobject, &Fhtype);
  32.     if (fhp == NULL)
  33.         return NULL;
  34.     fhp->fh_fh = fh;
  35.     return (PyObject *)fhp;
  36. }
  37.  
  38. /* Font Handle methods */
  39.  
  40. static PyObject *
  41. fh_scalefont(fhobject *self, PyObject *args)
  42. {
  43.     double size;
  44.     if (!PyArg_Parse(args, "d", &size))
  45.         return NULL;
  46.     return newfhobject(fmscalefont(self->fh_fh, size));
  47. }
  48.  
  49. /* XXX fmmakefont */
  50.  
  51. static PyObject *
  52. fh_setfont(fhobject *self, PyObject *args)
  53. {
  54.     if (!PyArg_NoArgs(args))
  55.         return NULL;
  56.     fmsetfont(self->fh_fh);
  57.     Py_INCREF(Py_None);
  58.     return Py_None;
  59. }
  60.  
  61. static PyObject *
  62. fh_getfontname(fhobject *self, PyObject *args)
  63. {
  64.     char fontname[256];
  65.     int len;
  66.     if (!PyArg_NoArgs(args))
  67.         return NULL;
  68.     len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
  69.     if (len < 0) {
  70.         PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
  71.         return NULL;
  72.     }
  73.     return PyString_FromStringAndSize(fontname, len);
  74. }
  75.  
  76. static PyObject *
  77. fh_getcomment(fhobject *self, PyObject *args)
  78. {
  79.     char comment[256];
  80.     int len;
  81.     if (!PyArg_NoArgs(args))
  82.         return NULL;
  83.     len = fmgetcomment(self->fh_fh, sizeof comment, comment);
  84.     if (len < 0) {
  85.         PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
  86.         return NULL;
  87.     }
  88.     return PyString_FromStringAndSize(comment, len);
  89. }
  90.  
  91. static PyObject *
  92. fh_getfontinfo(fhobject *self, PyObject *args)
  93. {
  94.     fmfontinfo info;
  95.     if (!PyArg_NoArgs(args))
  96.         return NULL;
  97.     if (fmgetfontinfo(self->fh_fh, &info) < 0) {
  98.         PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
  99.         return NULL;
  100.     }
  101.     return Py_BuildValue("(llllllll)",
  102.                  info.printermatched,
  103.                  info.fixed_width,
  104.                  info.xorig,
  105.                  info.yorig,
  106.                  info.xsize,
  107.                  info.ysize,
  108.                  info.height,
  109.                  info.nglyphs);
  110. }
  111.  
  112. #if 0
  113. static PyObject *
  114. fh_getwholemetrics(fhobject *self, PyObject *args)
  115. {
  116. }
  117. #endif
  118.  
  119. static PyObject *
  120. fh_getstrwidth(fhobject *self, PyObject *args)
  121. {
  122.     char *str;
  123.     if (!PyArg_Parse(args, "s", &str))
  124.         return NULL;
  125.     return PyInt_FromLong(fmgetstrwidth(self->fh_fh, str));
  126. }
  127.  
  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},
  134. #if 0
  135.     {"getwholemetrics",    (PyCFunction)fh_getwholemetrics},
  136. #endif
  137.     {"getstrwidth",    (PyCFunction)fh_getstrwidth},
  138.     {NULL,        NULL}        /* sentinel */
  139. };
  140.  
  141. static PyObject *
  142. fh_getattr(fhobject *fhp, char *name)
  143. {
  144.     return Py_FindMethod(fh_methods, (PyObject *)fhp, name);
  145. }
  146.  
  147. static void
  148. fh_dealloc(fhobject *fhp)
  149. {
  150.     fmfreefont(fhp->fh_fh);
  151.     PyObject_Del(fhp);
  152. }
  153.  
  154. static PyTypeObject Fhtype = {
  155.     PyObject_HEAD_INIT(&PyType_Type)
  156.     0,                /*ob_size*/
  157.     "font handle",            /*tp_name*/
  158.     sizeof(fhobject),        /*tp_size*/
  159.     0,                /*tp_itemsize*/
  160.     /* methods */
  161.     (destructor)fh_dealloc,        /*tp_dealloc*/
  162.     0,                /*tp_print*/
  163.     (getattrfunc)fh_getattr,    /*tp_getattr*/
  164.     0,                /*tp_setattr*/
  165.     0,                /*tp_compare*/
  166.     0,                /*tp_repr*/
  167. };
  168.  
  169.  
  170. /* Font Manager functions */
  171.  
  172. static PyObject *
  173. fm_init(PyObject *self, PyObject *args)
  174. {
  175.     if (!PyArg_NoArgs(args))
  176.         return NULL;
  177.     fminit();
  178.     Py_INCREF(Py_None);
  179.     return Py_None;
  180. }
  181.  
  182. static PyObject *
  183. fm_findfont(PyObject *self, PyObject *args)
  184. {
  185.     char *str;
  186.     if (!PyArg_Parse(args, "s", &str))
  187.         return NULL;
  188.     return newfhobject(fmfindfont(str));
  189. }
  190.  
  191. static PyObject *
  192. fm_prstr(PyObject *self, PyObject *args)
  193. {
  194.     char *str;
  195.     if (!PyArg_Parse(args, "s", &str))
  196.         return NULL;
  197.     fmprstr(str);
  198.     Py_INCREF(Py_None);
  199.     return Py_None;
  200. }
  201.  
  202. /* XXX This uses a global variable as temporary! Not re-entrant! */
  203.  
  204. static PyObject *fontlist;
  205.  
  206. static void
  207. clientproc(char *fontname)
  208. {
  209.     int err;
  210.     PyObject *v;
  211.     if (fontlist == NULL)
  212.         return;
  213.     v = PyString_FromString(fontname);
  214.     if (v == NULL)
  215.         err = -1;
  216.     else {
  217.         err = PyList_Append(fontlist, v);
  218.         Py_DECREF(v);
  219.     }
  220.     if (err != 0) {
  221.         Py_DECREF(fontlist);
  222.         fontlist = NULL;
  223.     }
  224. }
  225.  
  226. static PyObject *
  227. fm_enumerate(PyObject *self, PyObject *args)
  228. {
  229.     PyObject *res;
  230.     if (!PyArg_NoArgs(args))
  231.         return NULL;
  232.     fontlist = PyList_New(0);
  233.     if (fontlist == NULL)
  234.         return NULL;
  235.     fmenumerate(clientproc);
  236.     res = fontlist;
  237.     fontlist = NULL;
  238.     return res;
  239. }
  240.  
  241. static PyObject *
  242. fm_setpath(PyObject *self, PyObject *args)
  243. {
  244.     char *str;
  245.     if (!PyArg_Parse(args, "s", &str))
  246.         return NULL;
  247.     fmsetpath(str);
  248.     Py_INCREF(Py_None);
  249.     return Py_None;
  250. }
  251.  
  252. static PyObject *
  253. fm_fontpath(PyObject *self, PyObject *args)
  254. {
  255.     if (!PyArg_NoArgs(args))
  256.         return NULL;
  257.     return PyString_FromString(fmfontpath());
  258. }
  259.  
  260. static PyMethodDef fm_methods[] = {
  261.     {"init",    fm_init},
  262.     {"findfont",    fm_findfont},
  263.     {"enumerate",    fm_enumerate},
  264.     {"prstr",    fm_prstr},
  265.     {"setpath",    fm_setpath},
  266.     {"fontpath",    fm_fontpath},
  267.     {NULL,        NULL}        /* sentinel */
  268. };
  269.  
  270.  
  271. void
  272. initfm(void)
  273. {
  274.     Py_InitModule("fm", fm_methods);
  275.     fminit();
  276. }
  277.