Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3 | Amsterdam, The Netherlands. |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 43 | staticforward typeobject Fhtype; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 71 | if (!getargs(args, "d", &size)) |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 137 | return mkvalue("(llllllll)", |
| 138 | info.printermatched, |
| 139 | info.fixed_width, |
| 140 | info.xorig, |
| 141 | info.yorig, |
| 142 | info.xsize, |
| 143 | info.ysize, |
| 144 | info.height, |
| 145 | info.nglyphs); |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | #if 0 |
| 149 | static object * |
| 150 | fh_getwholemetrics(self, args) |
| 151 | fhobject *self; |
| 152 | object *args; |
| 153 | { |
| 154 | } |
| 155 | #endif |
| 156 | |
| 157 | static object * |
| 158 | fh_getstrwidth(self, args) |
| 159 | fhobject *self; |
| 160 | object *args; |
| 161 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 162 | char *str; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 163 | if (!getstrarg(args, &str)) |
| 164 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 165 | return newintobject(fmgetstrwidth(self->fh_fh, str)); |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static struct methodlist fh_methods[] = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 169 | {"scalefont", (method)fh_scalefont}, |
| 170 | {"setfont", (method)fh_setfont}, |
| 171 | {"getfontname", (method)fh_getfontname}, |
| 172 | {"getcomment", (method)fh_getcomment}, |
| 173 | {"getfontinfo", (method)fh_getfontinfo}, |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 174 | #if 0 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | {"getwholemetrics", (method)fh_getwholemetrics}, |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 176 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 177 | {"getstrwidth", (method)fh_getstrwidth}, |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 178 | {NULL, NULL} /* sentinel */ |
| 179 | }; |
| 180 | |
| 181 | static object * |
| 182 | fh_getattr(fhp, name) |
| 183 | fhobject *fhp; |
| 184 | char *name; |
| 185 | { |
| 186 | return findmethod(fh_methods, (object *)fhp, name); |
| 187 | } |
| 188 | |
| 189 | static void |
| 190 | fh_dealloc(fhp) |
| 191 | fhobject *fhp; |
| 192 | { |
| 193 | fmfreefont(fhp->fh_fh); |
| 194 | DEL(fhp); |
| 195 | } |
| 196 | |
| 197 | static typeobject Fhtype = { |
| 198 | OB_HEAD_INIT(&Typetype) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 199 | 0, /*ob_size*/ |
| 200 | "font handle", /*tp_name*/ |
| 201 | sizeof(fhobject), /*tp_size*/ |
| 202 | 0, /*tp_itemsize*/ |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 203 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 204 | (destructor)fh_dealloc, /*tp_dealloc*/ |
| 205 | 0, /*tp_print*/ |
| 206 | (getattrfunc)fh_getattr, /*tp_getattr*/ |
| 207 | 0, /*tp_setattr*/ |
| 208 | 0, /*tp_compare*/ |
| 209 | 0, /*tp_repr*/ |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | |
| 213 | /* Font Manager functions */ |
| 214 | |
| 215 | static object * |
| 216 | fm_init(self, args) |
| 217 | object *self, *args; |
| 218 | { |
| 219 | if (!getnoarg(args)) |
| 220 | return NULL; |
| 221 | fminit(); |
| 222 | INCREF(None); |
| 223 | return None; |
| 224 | } |
| 225 | |
| 226 | static object * |
| 227 | fm_findfont(self, args) |
| 228 | object *self, *args; |
| 229 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 230 | char *str; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 231 | if (!getstrarg(args, &str)) |
| 232 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 233 | return newfhobject(fmfindfont(str)); |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static object * |
| 237 | fm_prstr(self, args) |
| 238 | object *self, *args; |
| 239 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 240 | char *str; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 241 | if (!getstrarg(args, &str)) |
| 242 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 243 | fmprstr(str); |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 244 | INCREF(None); |
| 245 | return None; |
| 246 | } |
| 247 | |
| 248 | /* XXX This uses a global variable as temporary! Not re-entrant! */ |
| 249 | |
| 250 | static object *fontlist; |
| 251 | |
| 252 | static void |
| 253 | clientproc(fontname) |
| 254 | char *fontname; |
| 255 | { |
| 256 | int err; |
| 257 | object *v; |
| 258 | if (fontlist == NULL) |
| 259 | return; |
| 260 | v = newstringobject(fontname); |
| 261 | if (v == NULL) |
| 262 | err = -1; |
| 263 | else { |
| 264 | err = addlistitem(fontlist, v); |
| 265 | DECREF(v); |
| 266 | } |
| 267 | if (err != 0) { |
| 268 | DECREF(fontlist); |
| 269 | fontlist = NULL; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static object * |
| 274 | fm_enumerate(self, args) |
| 275 | object *self, *args; |
| 276 | { |
| 277 | object *res; |
| 278 | if (!getnoarg(args)) |
| 279 | return NULL; |
| 280 | fontlist = newlistobject(0); |
| 281 | if (fontlist == NULL) |
| 282 | return NULL; |
| 283 | fmenumerate(clientproc); |
| 284 | res = fontlist; |
| 285 | fontlist = NULL; |
| 286 | return res; |
| 287 | } |
| 288 | |
| 289 | static object * |
| 290 | fm_setpath(self, args) |
| 291 | object *self, *args; |
| 292 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 293 | char *str; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 294 | if (!getstrarg(args, &str)) |
| 295 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 296 | fmsetpath(str); |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 297 | INCREF(None); |
| 298 | return None; |
| 299 | } |
| 300 | |
| 301 | static object * |
| 302 | fm_fontpath(self, args) |
| 303 | object *self, *args; |
| 304 | { |
| 305 | if (!getnoarg(args)) |
| 306 | return NULL; |
| 307 | return newstringobject(fmfontpath()); |
| 308 | } |
| 309 | |
| 310 | static struct methodlist fm_methods[] = { |
| 311 | {"init", fm_init}, |
| 312 | {"findfont", fm_findfont}, |
| 313 | {"enumerate", fm_enumerate}, |
| 314 | {"prstr", fm_prstr}, |
| 315 | {"setpath", fm_setpath}, |
| 316 | {"fontpath", fm_fontpath}, |
| 317 | {NULL, NULL} /* sentinel */ |
| 318 | }; |
| 319 | |
| 320 | |
| 321 | void |
| 322 | initfm() |
| 323 | { |
| 324 | initmodule("fm", fm_methods); |
| 325 | fminit(); |
| 326 | } |