blob: 4875d9a2d5f61a674edfb3da84adac263cfc449f [file] [log] [blame]
Guido van Rossuma0c191e1991-04-03 19:02:31 +00001/***********************************************************
Guido van Rossumb6775db1994-08-01 11:34:53 +00002Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Guido van Rossume5372401993-03-16 12:15:04 +00003Amsterdam, The Netherlands.
Guido van Rossuma0c191e1991-04-03 19:02:31 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Font Manager module */
26
27#include "allobjects.h"
28
29#include "modsupport.h"
30
31#include <gl.h>
32#include <device.h>
33#include <fmclient.h>
34
35
36/* Font Handle object implementation */
37
38typedef struct {
39 OB_HEAD
40 fmfonthandle fh_fh;
41} fhobject;
42
Guido van Rossumb6775db1994-08-01 11:34:53 +000043staticforward typeobject Fhtype;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000044
45#define is_fhobject(v) ((v)->ob_type == &Fhtype)
46
47static object *
48newfhobject(fh)
49 fmfonthandle fh;
50{
51 fhobject *fhp;
52 if (fh == NULL) {
53 err_setstr(RuntimeError, "error creating new font handle");
54 return NULL;
55 }
56 fhp = NEWOBJ(fhobject, &Fhtype);
57 if (fhp == NULL)
58 return NULL;
59 fhp->fh_fh = fh;
60 return (object *)fhp;
61}
62
63/* Font Handle methods */
64
65static object *
66fh_scalefont(self, args)
67 fhobject *self;
68 object *args;
69{
70 double size;
Guido van Rossum234f9421993-06-17 12:35:49 +000071 if (!getargs(args, "d", &size))
Guido van Rossuma0c191e1991-04-03 19:02:31 +000072 return NULL;
73 return newfhobject(fmscalefont(self->fh_fh, size));
74}
75
76/* XXX fmmakefont */
77
78static object *
79fh_setfont(self, args)
80 fhobject *self;
81 object *args;
82{
83 if (!getnoarg(args))
84 return NULL;
85 fmsetfont(self->fh_fh);
86 INCREF(None);
87 return None;
88}
89
90static object *
91fh_getfontname(self, args)
92 fhobject *self;
93 object *args;
94{
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;
103 }
104 return newsizedstringobject(fontname, len);
105}
106
107static object *
108fh_getcomment(self, args)
109 fhobject *self;
110 object *args;
111{
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;
120 }
121 return newsizedstringobject(comment, len);
122}
123
124static object *
125fh_getfontinfo(self, args)
126 fhobject *self;
127 object *args;
128{
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;
136 }
Guido van Rossume5372401993-03-16 12:15:04 +0000137 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);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000146}
147
148#if 0
149static object *
150fh_getwholemetrics(self, args)
151 fhobject *self;
152 object *args;
153{
154}
155#endif
156
157static object *
158fh_getstrwidth(self, args)
159 fhobject *self;
160 object *args;
161{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000162 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000163 if (!getstrarg(args, &str))
164 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000165 return newintobject(fmgetstrwidth(self->fh_fh, str));
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000166}
167
168static struct methodlist fh_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000169 {"scalefont", (method)fh_scalefont},
170 {"setfont", (method)fh_setfont},
171 {"getfontname", (method)fh_getfontname},
172 {"getcomment", (method)fh_getcomment},
173 {"getfontinfo", (method)fh_getfontinfo},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000174#if 0
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175 {"getwholemetrics", (method)fh_getwholemetrics},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000176#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000177 {"getstrwidth", (method)fh_getstrwidth},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000178 {NULL, NULL} /* sentinel */
179};
180
181static object *
182fh_getattr(fhp, name)
183 fhobject *fhp;
184 char *name;
185{
186 return findmethod(fh_methods, (object *)fhp, name);
187}
188
189static void
190fh_dealloc(fhp)
191 fhobject *fhp;
192{
193 fmfreefont(fhp->fh_fh);
194 DEL(fhp);
195}
196
197static typeobject Fhtype = {
198 OB_HEAD_INIT(&Typetype)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199 0, /*ob_size*/
200 "font handle", /*tp_name*/
201 sizeof(fhobject), /*tp_size*/
202 0, /*tp_itemsize*/
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000203 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204 (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*/
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000210};
211
212
213/* Font Manager functions */
214
215static object *
216fm_init(self, args)
217 object *self, *args;
218{
219 if (!getnoarg(args))
220 return NULL;
221 fminit();
222 INCREF(None);
223 return None;
224}
225
226static object *
227fm_findfont(self, args)
228 object *self, *args;
229{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000230 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000231 if (!getstrarg(args, &str))
232 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000233 return newfhobject(fmfindfont(str));
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000234}
235
236static object *
237fm_prstr(self, args)
238 object *self, *args;
239{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000240 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000241 if (!getstrarg(args, &str))
242 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000243 fmprstr(str);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000244 INCREF(None);
245 return None;
246}
247
248/* XXX This uses a global variable as temporary! Not re-entrant! */
249
250static object *fontlist;
251
252static void
253clientproc(fontname)
254 char *fontname;
255{
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);
266 }
267 if (err != 0) {
268 DECREF(fontlist);
269 fontlist = NULL;
270 }
271}
272
273static object *
274fm_enumerate(self, args)
275 object *self, *args;
276{
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;
287}
288
289static object *
290fm_setpath(self, args)
291 object *self, *args;
292{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000293 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000294 if (!getstrarg(args, &str))
295 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000296 fmsetpath(str);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000297 INCREF(None);
298 return None;
299}
300
301static object *
302fm_fontpath(self, args)
303 object *self, *args;
304{
305 if (!getnoarg(args))
306 return NULL;
307 return newstringobject(fmfontpath());
308}
309
310static 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 */
318};
319
320
321void
322initfm()
323{
324 initmodule("fm", fm_methods);
325 fminit();
326}