blob: 01cf26823e3df046f2f18fad4cbc96b7748a3ba5 [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;
137 object *v;
138 if (!getnoarg(args))
139 return NULL;
140 if (fmgetfontinfo(self->fh_fh, &info) < 0) {
141 err_setstr(RuntimeError, "error in fmgetfontinfo");
142 return NULL;
143 }
Guido van Rossume5372401993-03-16 12:15:04 +0000144 return mkvalue("(llllllll)",
145 info.printermatched,
146 info.fixed_width,
147 info.xorig,
148 info.yorig,
149 info.xsize,
150 info.ysize,
151 info.height,
152 info.nglyphs);
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000153}
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[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000176 {"scalefont", (method)fh_scalefont},
177 {"setfont", (method)fh_setfont},
178 {"getfontname", (method)fh_getfontname},
179 {"getcomment", (method)fh_getcomment},
180 {"getfontinfo", (method)fh_getfontinfo},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000181#if 0
Guido van Rossumb6775db1994-08-01 11:34:53 +0000182 {"getwholemetrics", (method)fh_getwholemetrics},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000183#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000184 {"getstrwidth", (method)fh_getstrwidth},
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000185 {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)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206 0, /*ob_size*/
207 "font handle", /*tp_name*/
208 sizeof(fhobject), /*tp_size*/
209 0, /*tp_itemsize*/
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000210 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211 (destructor)fh_dealloc, /*tp_dealloc*/
212 0, /*tp_print*/
213 (getattrfunc)fh_getattr, /*tp_getattr*/
214 0, /*tp_setattr*/
215 0, /*tp_compare*/
216 0, /*tp_repr*/
Guido van Rossuma0c191e1991-04-03 19:02:31 +0000217};
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}