Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF 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 | |
| 38 | typedef struct { |
| 39 | OB_HEAD |
| 40 | fmfonthandle fh_fh; |
| 41 | } fhobject; |
| 42 | |
| 43 | extern typeobject Fhtype; /* Really static, forward */ |
| 44 | |
| 45 | #define is_fhobject(v) ((v)->ob_type == &Fhtype) |
| 46 | |
| 47 | static object * |
| 48 | newfhobject(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 | |
| 65 | static object * |
| 66 | fh_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 | |
| 78 | static object * |
| 79 | fh_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 | |
| 90 | static object * |
| 91 | fh_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 | |
| 107 | static object * |
| 108 | fh_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 | |
| 124 | static object * |
| 125 | fh_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 |
| 156 | static object * |
| 157 | fh_getwholemetrics(self, args) |
| 158 | fhobject *self; |
| 159 | object *args; |
| 160 | { |
| 161 | } |
| 162 | #endif |
| 163 | |
| 164 | static object * |
| 165 | fh_getstrwidth(self, args) |
| 166 | fhobject *self; |
| 167 | object *args; |
| 168 | { |
| 169 | object *str; |
| 170 | if (!getstrarg(args, &str)) |
| 171 | return NULL; |
| 172 | return newintobject(fmgetstrwidth(self->fh_fh, getstringvalue(str))); |
| 173 | } |
| 174 | |
| 175 | static 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 | |
| 188 | static object * |
| 189 | fh_getattr(fhp, name) |
| 190 | fhobject *fhp; |
| 191 | char *name; |
| 192 | { |
| 193 | return findmethod(fh_methods, (object *)fhp, name); |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | fh_dealloc(fhp) |
| 198 | fhobject *fhp; |
| 199 | { |
| 200 | fmfreefont(fhp->fh_fh); |
| 201 | DEL(fhp); |
| 202 | } |
| 203 | |
| 204 | static 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 | |
| 222 | static object * |
| 223 | fm_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 | |
| 233 | static object * |
| 234 | fm_findfont(self, args) |
| 235 | object *self, *args; |
| 236 | { |
| 237 | object *str; |
| 238 | if (!getstrarg(args, &str)) |
| 239 | return NULL; |
| 240 | return newfhobject(fmfindfont(getstringvalue(str))); |
| 241 | } |
| 242 | |
| 243 | static object * |
| 244 | fm_prstr(self, args) |
| 245 | object *self, *args; |
| 246 | { |
| 247 | object *str; |
| 248 | if (!getstrarg(args, &str)) |
| 249 | return NULL; |
| 250 | fmprstr(getstringvalue(str)); |
| 251 | INCREF(None); |
| 252 | return None; |
| 253 | } |
| 254 | |
| 255 | /* XXX This uses a global variable as temporary! Not re-entrant! */ |
| 256 | |
| 257 | static object *fontlist; |
| 258 | |
| 259 | static void |
| 260 | clientproc(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 | |
| 280 | static object * |
| 281 | fm_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 | |
| 296 | static object * |
| 297 | fm_setpath(self, args) |
| 298 | object *self, *args; |
| 299 | { |
| 300 | object *str; |
| 301 | if (!getstrarg(args, &str)) |
| 302 | return NULL; |
| 303 | fmsetpath(getstringvalue(str)); |
| 304 | INCREF(None); |
| 305 | return None; |
| 306 | } |
| 307 | |
| 308 | static object * |
| 309 | fm_fontpath(self, args) |
| 310 | object *self, *args; |
| 311 | { |
| 312 | if (!getnoarg(args)) |
| 313 | return NULL; |
| 314 | return newstringobject(fmfontpath()); |
| 315 | } |
| 316 | |
| 317 | static 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 | |
| 328 | void |
| 329 | initfm() |
| 330 | { |
| 331 | initmodule("fm", fm_methods); |
| 332 | fminit(); |
| 333 | } |