blob: f3df6ce2e3d275238cb9d95046c09b7831e77390 [file] [log] [blame]
Guido van Rossuma0c191e1991-04-03 19:02:31 +00001/***********************************************************
Guido van Rossumbab9d031992-04-05 14:26:55 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossuma0c191e1991-04-03 19:02:31 +00003Netherlands.
4
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
43extern typeobject Fhtype; /* Really static, forward */
44
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;
71 if (!getdoublearg(args, &size))
72 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 }
137 v = newtupleobject(8);
138 if (v == NULL)
139 return NULL;
140#define SET(i, member) settupleitem(v, i, newintobject(info.member))
141 SET(0, printermatched);
142 SET(1, fixed_width);
143 SET(2, xorig);
144 SET(3, yorig);
145 SET(4, xsize);
146 SET(5, ysize);
147 SET(6, height);
148 SET(7, nglyphs);
149#undef SET
150 if (err_occurred())
151 return NULL;
152 return v;
153}
154
155#if 0
156static object *
157fh_getwholemetrics(self, args)
158 fhobject *self;
159 object *args;
160{
161}
162#endif
163
164static object *
165fh_getstrwidth(self, args)
166 fhobject *self;
167 object *args;
168{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000169 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000170 if (!getstrarg(args, &str))
171 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000172 return newintobject(fmgetstrwidth(self->fh_fh, str));
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000173}
174
175static struct methodlist fh_methods[] = {
176 {"scalefont", fh_scalefont},
177 {"setfont", fh_setfont},
178 {"getfontname", fh_getfontname},
179 {"getcomment", fh_getcomment},
180 {"getfontinfo", fh_getfontinfo},
181#if 0
182 {"getwholemetrics", fh_getwholemetrics},
183#endif
184 {"getstrwidth", fh_getstrwidth},
185 {NULL, NULL} /* sentinel */
186};
187
188static object *
189fh_getattr(fhp, name)
190 fhobject *fhp;
191 char *name;
192{
193 return findmethod(fh_methods, (object *)fhp, name);
194}
195
196static void
197fh_dealloc(fhp)
198 fhobject *fhp;
199{
200 fmfreefont(fhp->fh_fh);
201 DEL(fhp);
202}
203
204static typeobject Fhtype = {
205 OB_HEAD_INIT(&Typetype)
206 0, /*ob_size*/
207 "font handle", /*tp_name*/
208 sizeof(fhobject), /*tp_size*/
209 0, /*tp_itemsize*/
210 /* methods */
211 fh_dealloc, /*tp_dealloc*/
212 0, /*tp_print*/
213 fh_getattr, /*tp_getattr*/
214 0, /*tp_setattr*/
215 0, /*tp_compare*/
216 0, /*tp_repr*/
217};
218
219
220/* Font Manager functions */
221
222static object *
223fm_init(self, args)
224 object *self, *args;
225{
226 if (!getnoarg(args))
227 return NULL;
228 fminit();
229 INCREF(None);
230 return None;
231}
232
233static object *
234fm_findfont(self, args)
235 object *self, *args;
236{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000237 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000238 if (!getstrarg(args, &str))
239 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000240 return newfhobject(fmfindfont(str));
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000241}
242
243static object *
244fm_prstr(self, args)
245 object *self, *args;
246{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000247 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000248 if (!getstrarg(args, &str))
249 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000250 fmprstr(str);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000251 INCREF(None);
252 return None;
253}
254
255/* XXX This uses a global variable as temporary! Not re-entrant! */
256
257static object *fontlist;
258
259static void
260clientproc(fontname)
261 char *fontname;
262{
263 int err;
264 object *v;
265 if (fontlist == NULL)
266 return;
267 v = newstringobject(fontname);
268 if (v == NULL)
269 err = -1;
270 else {
271 err = addlistitem(fontlist, v);
272 DECREF(v);
273 }
274 if (err != 0) {
275 DECREF(fontlist);
276 fontlist = NULL;
277 }
278}
279
280static object *
281fm_enumerate(self, args)
282 object *self, *args;
283{
284 object *res;
285 if (!getnoarg(args))
286 return NULL;
287 fontlist = newlistobject(0);
288 if (fontlist == NULL)
289 return NULL;
290 fmenumerate(clientproc);
291 res = fontlist;
292 fontlist = NULL;
293 return res;
294}
295
296static object *
297fm_setpath(self, args)
298 object *self, *args;
299{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000300 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000301 if (!getstrarg(args, &str))
302 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000303 fmsetpath(str);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000304 INCREF(None);
305 return None;
306}
307
308static object *
309fm_fontpath(self, args)
310 object *self, *args;
311{
312 if (!getnoarg(args))
313 return NULL;
314 return newstringobject(fmfontpath());
315}
316
317static struct methodlist fm_methods[] = {
318 {"init", fm_init},
319 {"findfont", fm_findfont},
320 {"enumerate", fm_enumerate},
321 {"prstr", fm_prstr},
322 {"setpath", fm_setpath},
323 {"fontpath", fm_fontpath},
324 {NULL, NULL} /* sentinel */
325};
326
327
328void
329initfm()
330{
331 initmodule("fm", fm_methods);
332 fminit();
333}