Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 29 | |
| 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 | |
| 45 | typedef struct { |
| 46 | OB_HEAD |
| 47 | fmfonthandle fh_fh; |
| 48 | } fhobject; |
| 49 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 50 | staticforward typeobject Fhtype; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 51 | |
| 52 | #define is_fhobject(v) ((v)->ob_type == &Fhtype) |
| 53 | |
| 54 | static object * |
| 55 | newfhobject(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 | |
| 72 | static object * |
| 73 | fh_scalefont(self, args) |
| 74 | fhobject *self; |
| 75 | object *args; |
| 76 | { |
| 77 | double size; |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 78 | if (!getargs(args, "d", &size)) |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 79 | return NULL; |
| 80 | return newfhobject(fmscalefont(self->fh_fh, size)); |
| 81 | } |
| 82 | |
| 83 | /* XXX fmmakefont */ |
| 84 | |
| 85 | static object * |
| 86 | fh_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 | |
| 97 | static object * |
| 98 | fh_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 | |
| 114 | static object * |
| 115 | fh_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 | |
| 131 | static object * |
| 132 | fh_getfontinfo(self, args) |
| 133 | fhobject *self; |
| 134 | object *args; |
| 135 | { |
| 136 | fmfontinfo info; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 137 | 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 Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 143 | 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 Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | #if 0 |
| 155 | static object * |
| 156 | fh_getwholemetrics(self, args) |
| 157 | fhobject *self; |
| 158 | object *args; |
| 159 | { |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | static object * |
| 164 | fh_getstrwidth(self, args) |
| 165 | fhobject *self; |
| 166 | object *args; |
| 167 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 168 | char *str; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 169 | if (!getstrarg(args, &str)) |
| 170 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 171 | return newintobject(fmgetstrwidth(self->fh_fh, str)); |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static struct methodlist fh_methods[] = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | {"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 Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 180 | #if 0 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 181 | {"getwholemetrics", (method)fh_getwholemetrics}, |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 182 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 183 | {"getstrwidth", (method)fh_getstrwidth}, |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 184 | {NULL, NULL} /* sentinel */ |
| 185 | }; |
| 186 | |
| 187 | static object * |
| 188 | fh_getattr(fhp, name) |
| 189 | fhobject *fhp; |
| 190 | char *name; |
| 191 | { |
| 192 | return findmethod(fh_methods, (object *)fhp, name); |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | fh_dealloc(fhp) |
| 197 | fhobject *fhp; |
| 198 | { |
| 199 | fmfreefont(fhp->fh_fh); |
| 200 | DEL(fhp); |
| 201 | } |
| 202 | |
| 203 | static typeobject Fhtype = { |
| 204 | OB_HEAD_INIT(&Typetype) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 205 | 0, /*ob_size*/ |
| 206 | "font handle", /*tp_name*/ |
| 207 | sizeof(fhobject), /*tp_size*/ |
| 208 | 0, /*tp_itemsize*/ |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 209 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 210 | (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 Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | |
| 219 | /* Font Manager functions */ |
| 220 | |
| 221 | static object * |
| 222 | fm_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 | |
| 232 | static object * |
| 233 | fm_findfont(self, args) |
| 234 | object *self, *args; |
| 235 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 236 | char *str; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 237 | if (!getstrarg(args, &str)) |
| 238 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 239 | return newfhobject(fmfindfont(str)); |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static object * |
| 243 | fm_prstr(self, args) |
| 244 | object *self, *args; |
| 245 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 246 | char *str; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 247 | if (!getstrarg(args, &str)) |
| 248 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 249 | fmprstr(str); |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 250 | INCREF(None); |
| 251 | return None; |
| 252 | } |
| 253 | |
| 254 | /* XXX This uses a global variable as temporary! Not re-entrant! */ |
| 255 | |
| 256 | static object *fontlist; |
| 257 | |
| 258 | static void |
| 259 | clientproc(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 | |
| 279 | static object * |
| 280 | fm_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 | |
| 295 | static object * |
| 296 | fm_setpath(self, args) |
| 297 | object *self, *args; |
| 298 | { |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 299 | char *str; |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 300 | if (!getstrarg(args, &str)) |
| 301 | return NULL; |
Guido van Rossum | ef0a00e | 1992-01-27 16:51:30 +0000 | [diff] [blame] | 302 | fmsetpath(str); |
Guido van Rossum | a0c191e | 1991-04-03 19:02:31 +0000 | [diff] [blame] | 303 | INCREF(None); |
| 304 | return None; |
| 305 | } |
| 306 | |
| 307 | static object * |
| 308 | fm_fontpath(self, args) |
| 309 | object *self, *args; |
| 310 | { |
| 311 | if (!getnoarg(args)) |
| 312 | return NULL; |
| 313 | return newstringobject(fmfontpath()); |
| 314 | } |
| 315 | |
| 316 | static 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 | |
| 327 | void |
| 328 | initfm() |
| 329 | { |
| 330 | initmodule("fm", fm_methods); |
| 331 | fminit(); |
| 332 | } |