blob: 2f6031a8c177dda65eadf61809be8234a597b34f [file] [log] [blame]
Guido van Rossuma0c191e1991-04-03 19:02:31 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossuma0c191e1991-04-03 19:02:31 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossuma0c191e1991-04-03 19:02:31 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossuma0c191e1991-04-03 19:02:31 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossuma0c191e1991-04-03 19:02:31 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossuma0c191e1991-04-03 19:02:31 +000029
30******************************************************************/
31
32/* Font Manager module */
33
34#include "allobjects.h"
35
36#include "modsupport.h"
37
38#include <gl.h>
39#include <device.h>
40#include <fmclient.h>
41
42
43/* Font Handle object implementation */
44
45typedef struct {
46 OB_HEAD
47 fmfonthandle fh_fh;
48} fhobject;
49
Guido van Rossumb6775db1994-08-01 11:34:53 +000050staticforward typeobject Fhtype;
Guido van Rossuma0c191e1991-04-03 19:02:31 +000051
52#define is_fhobject(v) ((v)->ob_type == &Fhtype)
53
54static object *
55newfhobject(fh)
56 fmfonthandle fh;
57{
58 fhobject *fhp;
59 if (fh == NULL) {
60 err_setstr(RuntimeError, "error creating new font handle");
61 return NULL;
62 }
63 fhp = NEWOBJ(fhobject, &Fhtype);
64 if (fhp == NULL)
65 return NULL;
66 fhp->fh_fh = fh;
67 return (object *)fhp;
68}
69
70/* Font Handle methods */
71
72static object *
73fh_scalefont(self, args)
74 fhobject *self;
75 object *args;
76{
77 double size;
Guido van Rossum234f9421993-06-17 12:35:49 +000078 if (!getargs(args, "d", &size))
Guido van Rossuma0c191e1991-04-03 19:02:31 +000079 return NULL;
80 return newfhobject(fmscalefont(self->fh_fh, size));
81}
82
83/* XXX fmmakefont */
84
85static object *
86fh_setfont(self, args)
87 fhobject *self;
88 object *args;
89{
90 if (!getnoarg(args))
91 return NULL;
92 fmsetfont(self->fh_fh);
93 INCREF(None);
94 return None;
95}
96
97static object *
98fh_getfontname(self, args)
99 fhobject *self;
100 object *args;
101{
102 char fontname[256];
103 int len;
104 if (!getnoarg(args))
105 return NULL;
106 len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
107 if (len < 0) {
108 err_setstr(RuntimeError, "error in fmgetfontname");
109 return NULL;
110 }
111 return newsizedstringobject(fontname, len);
112}
113
114static object *
115fh_getcomment(self, args)
116 fhobject *self;
117 object *args;
118{
119 char comment[256];
120 int len;
121 if (!getnoarg(args))
122 return NULL;
123 len = fmgetcomment(self->fh_fh, sizeof comment, comment);
124 if (len < 0) {
125 err_setstr(RuntimeError, "error in fmgetcomment");
126 return NULL;
127 }
128 return newsizedstringobject(comment, len);
129}
130
131static object *
132fh_getfontinfo(self, args)
133 fhobject *self;
134 object *args;
135{
136 fmfontinfo info;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000137 if (!getnoarg(args))
138 return NULL;
139 if (fmgetfontinfo(self->fh_fh, &info) < 0) {
140 err_setstr(RuntimeError, "error in fmgetfontinfo");
141 return NULL;
142 }
Guido van Rossume5372401993-03-16 12:15:04 +0000143 return mkvalue("(llllllll)",
144 info.printermatched,
145 info.fixed_width,
146 info.xorig,
147 info.yorig,
148 info.xsize,
149 info.ysize,
150 info.height,
151 info.nglyphs);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000152}
153
154#if 0
155static object *
156fh_getwholemetrics(self, args)
157 fhobject *self;
158 object *args;
159{
160}
161#endif
162
163static object *
164fh_getstrwidth(self, args)
165 fhobject *self;
166 object *args;
167{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000168 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000169 if (!getstrarg(args, &str))
170 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000171 return newintobject(fmgetstrwidth(self->fh_fh, str));
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000172}
173
174static struct methodlist fh_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000175 {"scalefont", (method)fh_scalefont},
176 {"setfont", (method)fh_setfont},
177 {"getfontname", (method)fh_getfontname},
178 {"getcomment", (method)fh_getcomment},
179 {"getfontinfo", (method)fh_getfontinfo},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000180#if 0
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181 {"getwholemetrics", (method)fh_getwholemetrics},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000182#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183 {"getstrwidth", (method)fh_getstrwidth},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000184 {NULL, NULL} /* sentinel */
185};
186
187static object *
188fh_getattr(fhp, name)
189 fhobject *fhp;
190 char *name;
191{
192 return findmethod(fh_methods, (object *)fhp, name);
193}
194
195static void
196fh_dealloc(fhp)
197 fhobject *fhp;
198{
199 fmfreefont(fhp->fh_fh);
200 DEL(fhp);
201}
202
203static typeobject Fhtype = {
204 OB_HEAD_INIT(&Typetype)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000205 0, /*ob_size*/
206 "font handle", /*tp_name*/
207 sizeof(fhobject), /*tp_size*/
208 0, /*tp_itemsize*/
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000209 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000210 (destructor)fh_dealloc, /*tp_dealloc*/
211 0, /*tp_print*/
212 (getattrfunc)fh_getattr, /*tp_getattr*/
213 0, /*tp_setattr*/
214 0, /*tp_compare*/
215 0, /*tp_repr*/
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000216};
217
218
219/* Font Manager functions */
220
221static object *
222fm_init(self, args)
223 object *self, *args;
224{
225 if (!getnoarg(args))
226 return NULL;
227 fminit();
228 INCREF(None);
229 return None;
230}
231
232static object *
233fm_findfont(self, args)
234 object *self, *args;
235{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000236 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000237 if (!getstrarg(args, &str))
238 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000239 return newfhobject(fmfindfont(str));
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000240}
241
242static object *
243fm_prstr(self, args)
244 object *self, *args;
245{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000246 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000247 if (!getstrarg(args, &str))
248 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000249 fmprstr(str);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000250 INCREF(None);
251 return None;
252}
253
254/* XXX This uses a global variable as temporary! Not re-entrant! */
255
256static object *fontlist;
257
258static void
259clientproc(fontname)
260 char *fontname;
261{
262 int err;
263 object *v;
264 if (fontlist == NULL)
265 return;
266 v = newstringobject(fontname);
267 if (v == NULL)
268 err = -1;
269 else {
270 err = addlistitem(fontlist, v);
271 DECREF(v);
272 }
273 if (err != 0) {
274 DECREF(fontlist);
275 fontlist = NULL;
276 }
277}
278
279static object *
280fm_enumerate(self, args)
281 object *self, *args;
282{
283 object *res;
284 if (!getnoarg(args))
285 return NULL;
286 fontlist = newlistobject(0);
287 if (fontlist == NULL)
288 return NULL;
289 fmenumerate(clientproc);
290 res = fontlist;
291 fontlist = NULL;
292 return res;
293}
294
295static object *
296fm_setpath(self, args)
297 object *self, *args;
298{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000299 char *str;
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000300 if (!getstrarg(args, &str))
301 return NULL;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000302 fmsetpath(str);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000303 INCREF(None);
304 return None;
305}
306
307static object *
308fm_fontpath(self, args)
309 object *self, *args;
310{
311 if (!getnoarg(args))
312 return NULL;
313 return newstringobject(fmfontpath());
314}
315
316static struct methodlist fm_methods[] = {
317 {"init", fm_init},
318 {"findfont", fm_findfont},
319 {"enumerate", fm_enumerate},
320 {"prstr", fm_prstr},
321 {"setpath", fm_setpath},
322 {"fontpath", fm_fontpath},
323 {NULL, NULL} /* sentinel */
324};
325
326
327void
328initfm()
329{
330 initmodule("fm", fm_methods);
331 fminit();
332}