home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Modules / fmmodule.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  7KB  |  332 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Font Manager module */
  33.  
  34. #include "Python.h"
  35.  
  36. #include <gl.h>
  37. #include <device.h>
  38. #include <fmclient.h>
  39.  
  40.  
  41. /* Font Handle object implementation */
  42.  
  43. typedef struct {
  44.     PyObject_HEAD
  45.     fmfonthandle fh_fh;
  46. } fhobject;
  47.  
  48. staticforward PyTypeObject Fhtype;
  49.  
  50. #define is_fhobject(v)        ((v)->ob_type == &Fhtype)
  51.  
  52. static PyObject *
  53. newfhobject(fh)
  54.     fmfonthandle fh;
  55. {
  56.     fhobject *fhp;
  57.     if (fh == NULL) {
  58.         PyErr_SetString(PyExc_RuntimeError,
  59.                 "error creating new font handle");
  60.         return NULL;
  61.     }
  62.     fhp = PyObject_NEW(fhobject, &Fhtype);
  63.     if (fhp == NULL)
  64.         return NULL;
  65.     fhp->fh_fh = fh;
  66.     return (PyObject *)fhp;
  67. }
  68.  
  69. /* Font Handle methods */
  70.  
  71. static PyObject *
  72. fh_scalefont(self, args)
  73.     fhobject *self;
  74. PyObject *args;
  75. {
  76.     double size;
  77.     if (!PyArg_Parse(args, "d", &size))
  78.         return NULL;
  79.     return newfhobject(fmscalefont(self->fh_fh, size));
  80. }
  81.  
  82. /* XXX fmmakefont */
  83.  
  84. static PyObject *
  85. fh_setfont(self, args)
  86.     fhobject *self;
  87. PyObject *args;
  88. {
  89.     if (!PyArg_NoArgs(args))
  90.         return NULL;
  91.     fmsetfont(self->fh_fh);
  92.     Py_INCREF(Py_None);
  93.     return Py_None;
  94. }
  95.  
  96. static PyObject *
  97. fh_getfontname(self, args)
  98.     fhobject *self;
  99. PyObject *args;
  100. {
  101.     char fontname[256];
  102.     int len;
  103.     if (!PyArg_NoArgs(args))
  104.         return NULL;
  105.     len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
  106.     if (len < 0) {
  107.         PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontname");
  108.         return NULL;
  109.     }
  110.     return PyString_FromStringAndSize(fontname, len);
  111. }
  112.  
  113. static PyObject *
  114. fh_getcomment(self, args)
  115.     fhobject *self;
  116. PyObject *args;
  117. {
  118.     char comment[256];
  119.     int len;
  120.     if (!PyArg_NoArgs(args))
  121.         return NULL;
  122.     len = fmgetcomment(self->fh_fh, sizeof comment, comment);
  123.     if (len < 0) {
  124.         PyErr_SetString(PyExc_RuntimeError, "error in fmgetcomment");
  125.         return NULL;
  126.     }
  127.     return PyString_FromStringAndSize(comment, len);
  128. }
  129.  
  130. static PyObject *
  131. fh_getfontinfo(self, args)
  132.     fhobject *self;
  133. PyObject *args;
  134. {
  135.     fmfontinfo info;
  136.     if (!PyArg_NoArgs(args))
  137.         return NULL;
  138.     if (fmgetfontinfo(self->fh_fh, &info) < 0) {
  139.         PyErr_SetString(PyExc_RuntimeError, "error in fmgetfontinfo");
  140.         return NULL;
  141.     }
  142.     return Py_BuildValue("(llllllll)",
  143.                  info.printermatched,
  144.                  info.fixed_width,
  145.                  info.xorig,
  146.                  info.yorig,
  147.                  info.xsize,
  148.                  info.ysize,
  149.                  info.height,
  150.                  info.nglyphs);
  151. }
  152.  
  153. #if 0
  154. static PyObject *
  155. fh_getwholemetrics(self, args)
  156.     fhobject *self;
  157. PyObject *args;
  158. {
  159. }
  160. #endif
  161.  
  162. static PyObject *
  163. fh_getstrwidth(self, args)
  164.     fhobject *self;
  165. PyObject *args;
  166. {
  167.     char *str;
  168.     if (!PyArg_Parse(args, "s", &str))
  169.         return NULL;
  170.     return PyInt_FromLong(fmgetstrwidth(self->fh_fh, str));
  171. }
  172.  
  173. static PyMethodDef fh_methods[] = {
  174.     {"scalefont",    (PyCFunction)fh_scalefont},
  175.     {"setfont",    (PyCFunction)fh_setfont},
  176.     {"getfontname",    (PyCFunction)fh_getfontname},
  177.     {"getcomment",    (PyCFunction)fh_getcomment},
  178.     {"getfontinfo",    (PyCFunction)fh_getfontinfo},
  179. #if 0
  180.     {"getwholemetrics",    (PyCFunction)fh_getwholemetrics},
  181. #endif
  182.     {"getstrwidth",    (PyCFunction)fh_getstrwidth},
  183.     {NULL,        NULL}        /* sentinel */
  184. };
  185.  
  186. static PyObject *
  187. fh_getattr(fhp, name)
  188.     fhobject *fhp;
  189. char *name;
  190. {
  191.     return Py_FindMethod(fh_methods, (PyObject *)fhp, name);
  192. }
  193.  
  194. static void
  195. fh_dealloc(fhp)
  196.     fhobject *fhp;
  197. {
  198.     fmfreefont(fhp->fh_fh);
  199.     PyMem_DEL(fhp);
  200. }
  201.  
  202. static PyTypeObject Fhtype = {
  203.     PyObject_HEAD_INIT(&PyType_Type)
  204.     0,                /*ob_size*/
  205.     "font handle",            /*tp_name*/
  206.     sizeof(fhobject),        /*tp_size*/
  207.     0,                /*tp_itemsize*/
  208.     /* methods */
  209.     (destructor)fh_dealloc,        /*tp_dealloc*/
  210.     0,                /*tp_print*/
  211.     (getattrfunc)fh_getattr,    /*tp_getattr*/
  212.     0,                /*tp_setattr*/
  213.     0,                /*tp_compare*/
  214.     0,                /*tp_repr*/
  215. };
  216.  
  217.  
  218. /* Font Manager functions */
  219.  
  220. static PyObject *
  221. fm_init(self, args)
  222.     PyObject *self, *args;
  223. {
  224.     if (!PyArg_NoArgs(args))
  225.         return NULL;
  226.     fminit();
  227.     Py_INCREF(Py_None);
  228.     return Py_None;
  229. }
  230.  
  231. static PyObject *
  232. fm_findfont(self, args)
  233.     PyObject *self, *args;
  234. {
  235.     char *str;
  236.     if (!PyArg_Parse(args, "s", &str))
  237.         return NULL;
  238.     return newfhobject(fmfindfont(str));
  239. }
  240.  
  241. static PyObject *
  242. fm_prstr(self, args)
  243.     PyObject *self, *args;
  244. {
  245.     char *str;
  246.     if (!PyArg_Parse(args, "s", &str))
  247.         return NULL;
  248.     fmprstr(str);
  249.     Py_INCREF(Py_None);
  250.     return Py_None;
  251. }
  252.  
  253. /* XXX This uses a global variable as temporary! Not re-entrant! */
  254.  
  255. static PyObject *fontlist;
  256.  
  257. static void
  258. clientproc(fontname)
  259.     char *fontname;
  260. {
  261.     int err;
  262.     PyObject *v;
  263.     if (fontlist == NULL)
  264.         return;
  265.     v = PyString_FromString(fontname);
  266.     if (v == NULL)
  267.         err = -1;
  268.     else {
  269.         err = PyList_Append(fontlist, v);
  270.         Py_DECREF(v);
  271.     }
  272.     if (err != 0) {
  273.         Py_DECREF(fontlist);
  274.         fontlist = NULL;
  275.     }
  276. }
  277.  
  278. static PyObject *
  279. fm_enumerate(self, args)
  280.     PyObject *self, *args;
  281. {
  282.     PyObject *res;
  283.     if (!PyArg_NoArgs(args))
  284.         return NULL;
  285.     fontlist = PyList_New(0);
  286.     if (fontlist == NULL)
  287.         return NULL;
  288.     fmenumerate(clientproc);
  289.     res = fontlist;
  290.     fontlist = NULL;
  291.     return res;
  292. }
  293.  
  294. static PyObject *
  295. fm_setpath(self, args)
  296.     PyObject *self, *args;
  297. {
  298.     char *str;
  299.     if (!PyArg_Parse(args, "s", &str))
  300.         return NULL;
  301.     fmsetpath(str);
  302.     Py_INCREF(Py_None);
  303.     return Py_None;
  304. }
  305.  
  306. static PyObject *
  307. fm_fontpath(self, args)
  308.     PyObject *self, *args;
  309. {
  310.     if (!PyArg_NoArgs(args))
  311.         return NULL;
  312.     return PyString_FromString(fmfontpath());
  313. }
  314.  
  315. static PyMethodDef fm_methods[] = {
  316.     {"init",    fm_init},
  317.     {"findfont",    fm_findfont},
  318.     {"enumerate",    fm_enumerate},
  319.     {"prstr",    fm_prstr},
  320.     {"setpath",    fm_setpath},
  321.     {"fontpath",    fm_fontpath},
  322.     {NULL,        NULL}        /* sentinel */
  323. };
  324.  
  325.  
  326. void
  327. initfm()
  328. {
  329.     Py_InitModule("fm", fm_methods);
  330.     fminit();
  331. }
  332.