--- ray/src/common/font.c 1995/05/25 15:14:03 2.10 +++ ray/src/common/font.c 2003/06/07 12:50:20 2.15 @@ -1,13 +1,12 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: font.c,v 2.15 2003/06/07 12:50:20 schorsch Exp $"; #endif - /* * Polygonal font handling routines */ +#include "copyright.h" + #include "standard.h" #include "font.h" @@ -15,7 +14,7 @@ static char SCCSid[] = "$SunId$ LBL"; #define galloc(nv) (GLYPH *)malloc(sizeof(GLYPH)+2*sizeof(GORD)*(nv)) -extern char *fgetword(), *getlibpath(); +int retainfonts = 0; /* retain loaded fonts? */ static FONT *fontlist = NULL; /* list of loaded fonts */ @@ -24,21 +23,21 @@ FONT * getfont(fname) /* return font fname */ char *fname; { - char buf[16]; FILE *fp; char *pathname, *err; unsigned wsum, hsum, ngly; - int gn, ngv; - register int gv; + int gn, ngv, gv; register GLYPH *g; GORD *gp; register FONT *f; for (f = fontlist; f != NULL; f = f->next) - if (!strcmp(f->name, fname)) + if (!strcmp(f->name, fname)) { + f->nref++; return(f); + } /* load the font file */ - if ((pathname = getpath(fname, getlibpath(), R_OK)) == NULL) { + if ((pathname = getpath(fname, getrlibpath(), R_OK)) == NULL) { sprintf(errmsg, "cannot find font file \"%s\"", fname); error(USER, errmsg); } @@ -46,16 +45,16 @@ char *fname; if (f == NULL) goto memerr; f->name = savestr(fname); + f->nref = 1; if ((fp = fopen(pathname, "r")) == NULL) { sprintf(errmsg, "cannot open font file \"%s\"", pathname); error(SYSTEM, errmsg); } - wsum = hsum = ngly = 0; - while (fgetword(buf,sizeof(buf),fp) != NULL) { /* get each glyph */ - if (!isint(buf)) + wsum = hsum = ngly = 0; /* get each glyph */ + while ((ngv = fgetval(fp, 'i', (char *)&gn)) != EOF) { + if (ngv == 0) goto nonint; - gn = atoi(buf); if (gn < 1 || gn > 255) { err = "illegal"; goto fonterr; @@ -64,8 +63,8 @@ char *fname; err = "duplicate"; goto fonterr; } - if (fgetword(buf,sizeof(buf),fp) == NULL || !isint(buf) || - (ngv = atoi(buf)) < 0 || ngv > 32000) { + if (fgetval(fp, 'i', (char *)&ngv) <= 0 || + ngv < 0 || ngv > 32000) { err = "bad # vertices for"; goto fonterr; } @@ -77,9 +76,8 @@ char *fname; ngv *= 2; gp = gvlist(g); while (ngv--) { - if (fgetword(buf,sizeof(buf),fp) == NULL || - !isint(buf) || - (gv = atoi(buf)) < 0 || gv > 255) { + if (fgetval(fp, 'i', (char *)&gv) <= 0 || + gv < 0 || gv > 255) { err = "bad vertex for"; goto fonterr; } @@ -119,26 +117,30 @@ fonterr: error(USER, errmsg); memerr: error(SYSTEM, "out of memory in fontglyph"); + return NULL; /* pro forma return */ } -freefont(fname) /* free a font (free all if fname==NULL) */ -char *fname; +void +freefont(fnt) /* release a font (free all if NULL) */ +FONT *fnt; { FONT head; register FONT *fl, *f; register int i; - + /* check reference count */ + if (fnt != NULL && (fnt->nref-- > 1 | retainfonts)) + return; head.next = fontlist; fl = &head; while ((f = fl->next) != NULL) - if (fname == NULL || !strcmp(fname, f->name)) { + if ((fnt == NULL | fnt == f)) { fl->next = f->next; for (i = 0; i < 256; i++) if (f->fg[i] != NULL) - free((char *)f->fg[i]); + free((void *)f->fg[i]); freestr(f->name); - free((char *)f); + free((void *)f); } else fl = f; fontlist = head.next;