improve treatment of multi-line replies, ignore empty lines
[python/dscho.git] / Modules / fmmodule.c
blob5fa78bd30632a6be4002110d4977cd38ef993e44
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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 not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Font Manager module */
27 #include "allobjects.h"
29 #include "modsupport.h"
31 #include <gl.h>
32 #include <device.h>
33 #include <fmclient.h>
36 /* Font Handle object implementation */
38 typedef struct {
39 OB_HEAD
40 fmfonthandle fh_fh;
41 } fhobject;
43 staticforward typeobject Fhtype;
45 #define is_fhobject(v) ((v)->ob_type == &Fhtype)
47 static object *
48 newfhobject(fh)
49 fmfonthandle fh;
51 fhobject *fhp;
52 if (fh == NULL) {
53 err_setstr(RuntimeError, "error creating new font handle");
54 return NULL;
56 fhp = NEWOBJ(fhobject, &Fhtype);
57 if (fhp == NULL)
58 return NULL;
59 fhp->fh_fh = fh;
60 return (object *)fhp;
63 /* Font Handle methods */
65 static object *
66 fh_scalefont(self, args)
67 fhobject *self;
68 object *args;
70 double size;
71 if (!getargs(args, "d", &size))
72 return NULL;
73 return newfhobject(fmscalefont(self->fh_fh, size));
76 /* XXX fmmakefont */
78 static object *
79 fh_setfont(self, args)
80 fhobject *self;
81 object *args;
83 if (!getnoarg(args))
84 return NULL;
85 fmsetfont(self->fh_fh);
86 INCREF(None);
87 return None;
90 static object *
91 fh_getfontname(self, args)
92 fhobject *self;
93 object *args;
95 char fontname[256];
96 int len;
97 if (!getnoarg(args))
98 return NULL;
99 len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
100 if (len < 0) {
101 err_setstr(RuntimeError, "error in fmgetfontname");
102 return NULL;
104 return newsizedstringobject(fontname, len);
107 static object *
108 fh_getcomment(self, args)
109 fhobject *self;
110 object *args;
112 char comment[256];
113 int len;
114 if (!getnoarg(args))
115 return NULL;
116 len = fmgetcomment(self->fh_fh, sizeof comment, comment);
117 if (len < 0) {
118 err_setstr(RuntimeError, "error in fmgetcomment");
119 return NULL;
121 return newsizedstringobject(comment, len);
124 static object *
125 fh_getfontinfo(self, args)
126 fhobject *self;
127 object *args;
129 fmfontinfo info;
130 object *v;
131 if (!getnoarg(args))
132 return NULL;
133 if (fmgetfontinfo(self->fh_fh, &info) < 0) {
134 err_setstr(RuntimeError, "error in fmgetfontinfo");
135 return NULL;
137 return mkvalue("(llllllll)",
138 info.printermatched,
139 info.fixed_width,
140 info.xorig,
141 info.yorig,
142 info.xsize,
143 info.ysize,
144 info.height,
145 info.nglyphs);
148 #if 0
149 static object *
150 fh_getwholemetrics(self, args)
151 fhobject *self;
152 object *args;
155 #endif
157 static object *
158 fh_getstrwidth(self, args)
159 fhobject *self;
160 object *args;
162 char *str;
163 if (!getstrarg(args, &str))
164 return NULL;
165 return newintobject(fmgetstrwidth(self->fh_fh, str));
168 static struct methodlist fh_methods[] = {
169 {"scalefont", (method)fh_scalefont},
170 {"setfont", (method)fh_setfont},
171 {"getfontname", (method)fh_getfontname},
172 {"getcomment", (method)fh_getcomment},
173 {"getfontinfo", (method)fh_getfontinfo},
174 #if 0
175 {"getwholemetrics", (method)fh_getwholemetrics},
176 #endif
177 {"getstrwidth", (method)fh_getstrwidth},
178 {NULL, NULL} /* sentinel */
181 static object *
182 fh_getattr(fhp, name)
183 fhobject *fhp;
184 char *name;
186 return findmethod(fh_methods, (object *)fhp, name);
189 static void
190 fh_dealloc(fhp)
191 fhobject *fhp;
193 fmfreefont(fhp->fh_fh);
194 DEL(fhp);
197 static typeobject Fhtype = {
198 OB_HEAD_INIT(&Typetype)
199 0, /*ob_size*/
200 "font handle", /*tp_name*/
201 sizeof(fhobject), /*tp_size*/
202 0, /*tp_itemsize*/
203 /* methods */
204 (destructor)fh_dealloc, /*tp_dealloc*/
205 0, /*tp_print*/
206 (getattrfunc)fh_getattr, /*tp_getattr*/
207 0, /*tp_setattr*/
208 0, /*tp_compare*/
209 0, /*tp_repr*/
213 /* Font Manager functions */
215 static object *
216 fm_init(self, args)
217 object *self, *args;
219 if (!getnoarg(args))
220 return NULL;
221 fminit();
222 INCREF(None);
223 return None;
226 static object *
227 fm_findfont(self, args)
228 object *self, *args;
230 char *str;
231 if (!getstrarg(args, &str))
232 return NULL;
233 return newfhobject(fmfindfont(str));
236 static object *
237 fm_prstr(self, args)
238 object *self, *args;
240 char *str;
241 if (!getstrarg(args, &str))
242 return NULL;
243 fmprstr(str);
244 INCREF(None);
245 return None;
248 /* XXX This uses a global variable as temporary! Not re-entrant! */
250 static object *fontlist;
252 static void
253 clientproc(fontname)
254 char *fontname;
256 int err;
257 object *v;
258 if (fontlist == NULL)
259 return;
260 v = newstringobject(fontname);
261 if (v == NULL)
262 err = -1;
263 else {
264 err = addlistitem(fontlist, v);
265 DECREF(v);
267 if (err != 0) {
268 DECREF(fontlist);
269 fontlist = NULL;
273 static object *
274 fm_enumerate(self, args)
275 object *self, *args;
277 object *res;
278 if (!getnoarg(args))
279 return NULL;
280 fontlist = newlistobject(0);
281 if (fontlist == NULL)
282 return NULL;
283 fmenumerate(clientproc);
284 res = fontlist;
285 fontlist = NULL;
286 return res;
289 static object *
290 fm_setpath(self, args)
291 object *self, *args;
293 char *str;
294 if (!getstrarg(args, &str))
295 return NULL;
296 fmsetpath(str);
297 INCREF(None);
298 return None;
301 static object *
302 fm_fontpath(self, args)
303 object *self, *args;
305 if (!getnoarg(args))
306 return NULL;
307 return newstringobject(fmfontpath());
310 static struct methodlist fm_methods[] = {
311 {"init", fm_init},
312 {"findfont", fm_findfont},
313 {"enumerate", fm_enumerate},
314 {"prstr", fm_prstr},
315 {"setpath", fm_setpath},
316 {"fontpath", fm_fontpath},
317 {NULL, NULL} /* sentinel */
321 void
322 initfm()
324 initmodule("fm", fm_methods);
325 fminit();