summaryrefslogtreecommitdiffstats
path: root/Modules/fmmodule.c
blob: 4875d9a2d5f61a674edfb3da84adac263cfc449f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/***********************************************************
Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Amsterdam, The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.

STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

/* Font Manager module */

#include "allobjects.h"

#include "modsupport.h"

#include <gl.h>
#include <device.h>
#include <fmclient.h>


/* Font Handle object implementation */

typedef struct {
	OB_HEAD
	fmfonthandle fh_fh;
} fhobject;

staticforward typeobject Fhtype;

#define is_fhobject(v)		((v)->ob_type == &Fhtype)

static object *
newfhobject(fh)
	fmfonthandle fh;
{
	fhobject *fhp;
	if (fh == NULL) {
		err_setstr(RuntimeError, "error creating new font handle");
		return NULL;
	}
	fhp = NEWOBJ(fhobject, &Fhtype);
	if (fhp == NULL)
		return NULL;
	fhp->fh_fh = fh;
	return (object *)fhp;
}

/* Font Handle methods */

static object *
fh_scalefont(self, args)
	fhobject *self;
	object *args;
{
	double size;
	if (!getargs(args, "d", &size))
		return NULL;
	return newfhobject(fmscalefont(self->fh_fh, size));
}

/* XXX fmmakefont */

static object *
fh_setfont(self, args)
	fhobject *self;
	object *args;
{
	if (!getnoarg(args))
		return NULL;
	fmsetfont(self->fh_fh);
	INCREF(None);
	return None;
}

static object *
fh_getfontname(self, args)
	fhobject *self;
	object *args;
{
	char fontname[256];
	int len;
	if (!getnoarg(args))
		return NULL;
	len = fmgetfontname(self->fh_fh, sizeof fontname, fontname);
	if (len < 0) {
		err_setstr(RuntimeError, "error in fmgetfontname");
		return NULL;
	}
	return newsizedstringobject(fontname, len);
}

static object *
fh_getcomment(self, args)
	fhobject *self;
	object *args;
{
	char comment[256];
	int len;
	if (!getnoarg(args))
		return NULL;
	len = fmgetcomment(self->fh_fh, sizeof comment, comment);
	if (len < 0) {
		err_setstr(RuntimeError, "error in fmgetcomment");
		return NULL;
	}
	return newsizedstringobject(comment, len);
}

static object *
fh_getfontinfo(self, args)
	fhobject *self;
	object *args;
{
	fmfontinfo info;
	object *v;
	if (!getnoarg(args))
		return NULL;
	if (fmgetfontinfo(self->fh_fh, &info) < 0) {
		err_setstr(RuntimeError, "error in fmgetfontinfo");
		return NULL;
	}
	return mkvalue("(llllllll)",
		       info.printermatched,
		       info.fixed_width,
		       info.xorig,
		       info.yorig,
		       info.xsize,
		       info.ysize,
		       info.height,
		       info.nglyphs);
}

#if 0
static object *
fh_getwholemetrics(self, args)
	fhobject *self;
	object *args;
{
}
#endif

static object *
fh_getstrwidth(self, args)
	fhobject *self;
	object *args;
{
	char *str;
	if (!getstrarg(args, &str))
		return NULL;
	return newintobject(fmgetstrwidth(self->fh_fh, str));
}

static struct methodlist fh_methods[] = {
	{"scalefont",	(method)fh_scalefont},
	{"setfont",	(method)fh_setfont},
	{"getfontname",	(method)fh_getfontname},
	{"getcomment",	(method)fh_getcomment},
	{"getfontinfo",	(method)fh_getfontinfo},
#if 0
	{"getwholemetrics",	(method)fh_getwholemetrics},
#endif
	{"getstrwidth",	(method)fh_getstrwidth},
	{NULL,		NULL}		/* sentinel */
};

static object *
fh_getattr(fhp, name)
	fhobject *fhp;
	char *name;
{
	return findmethod(fh_methods, (object *)fhp, name);
}

static void
fh_dealloc(fhp)
	fhobject *fhp;
{
	fmfreefont(fhp->fh_fh);
	DEL(fhp);
}

static typeobject Fhtype = {
	OB_HEAD_INIT(&Typetype)
	0,				/*ob_size*/
	"font handle",			/*tp_name*/
	sizeof(fhobject),		/*tp_size*/
	0,				/*tp_itemsize*/
	/* methods */
	(destructor)fh_dealloc,		/*tp_dealloc*/
	0,				/*tp_print*/
	(getattrfunc)fh_getattr,	/*tp_getattr*/
	0,				/*tp_setattr*/
	0,				/*tp_compare*/
	0,				/*tp_repr*/
};


/* Font Manager functions */

static object *
fm_init(self, args)
	object *self, *args;
{
	if (!getnoarg(args))
		return NULL;
	fminit();
	INCREF(None);
	return None;
}

static object *
fm_findfont(self, args)
	object *self, *args;
{
	char *str;
	if (!getstrarg(args, &str))
		return NULL;
	return newfhobject(fmfindfont(str));
}

static object *
fm_prstr(self, args)
	object *self, *args;
{
	char *str;
	if (!getstrarg(args, &str))
		return NULL;
	fmprstr(str);
	INCREF(None);
	return None;
}

/* XXX This uses a global variable as temporary! Not re-entrant! */

static object *fontlist;

static void
clientproc(fontname)
	char *fontname;
{
	int err;
	object *v;
	if (fontlist == NULL)
		return;
	v = newstringobject(fontname);
	if (v == NULL)
		err = -1;
	else {
		err = addlistitem(fontlist, v);
		DECREF(v);
	}
	if (err != 0) {
		DECREF(fontlist);
		fontlist = NULL;
	}
}

static object *
fm_enumerate(self, args)
	object *self, *args;
{
	object *res;
	if (!getnoarg(args))
		return NULL;
	fontlist = newlistobject(0);
	if (fontlist == NULL)
		return NULL;
	fmenumerate(clientproc);
	res = fontlist;
	fontlist = NULL;
	return res;
}

static object *
fm_setpath(self, args)
	object *self, *args;
{
	char *str;
	if (!getstrarg(args, &str))
		return NULL;
	fmsetpath(str);
	INCREF(None);
	return None;
}

static object *
fm_fontpath(self, args)
	object *self, *args;
{
	if (!getnoarg(args))
		return NULL;
	return newstringobject(fmfontpath());
}

static struct methodlist fm_methods[] = {
	{"init",	fm_init},
	{"findfont",	fm_findfont},
	{"enumerate",	fm_enumerate},
	{"prstr",	fm_prstr},
	{"setpath",	fm_setpath},
	{"fontpath",	fm_fontpath},
	{NULL,		NULL}		/* sentinel */
};


void
initfm()
{
	initmodule("fm", fm_methods);
	fminit();
}