ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/font.c
(Generate patch)

Comparing ray/src/common/font.c (file contents):
Revision 2.3 by greg, Wed Jun 24 17:52:58 1992 UTC vs.
Revision 2.19 by greg, Fri Mar 26 23:04:23 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Polygonal font handling routines
6   */
7  
8 < #include "standard.h"
8 > #include "copyright.h"
9  
10 + #include <stdlib.h>
11 +
12 + #include "paths.h"
13 + #include "rtio.h"
14 + #include "rterror.h"
15   #include "font.h"
16  
17   #define galloc(nv)      (GLYPH *)malloc(sizeof(GLYPH)+2*sizeof(GORD)*(nv))
18  
19  
20 < extern char  *libpath;                  /* list of library directories */
20 > int     retainfonts = 0;                /* retain loaded fonts? */
21  
22   static FONT     *fontlist = NULL;       /* list of loaded fonts */
23  
# Line 24 | Line 26 | FONT *
26   getfont(fname)                          /* return font fname */
27   char  *fname;
28   {
27        char  buf[16];
29          FILE  *fp;
30 <        char  *pathname, *err;
30 >        char  *pathname, *err = NULL;
31          unsigned  wsum, hsum, ngly;
32 <        int  gn, ngv;
33 <        register int  gv;
33 <        register GLYPH  *g;
32 >        int  gn, ngv, gv;
33 >        register GLYPH  *g;
34          GORD  *gp;
35          register FONT  *f;
36  
37          for (f = fontlist; f != NULL; f = f->next)
38 <                if (!strcmp(f->name, fname))
38 >                if (!strcmp(f->name, fname)) {
39 >                        f->nref++;
40                          return(f);
41 +                }
42                                                  /* load the font file */
43 <        if ((pathname = getpath(fname, libpath, R_OK)) == NULL) {
43 >        if ((pathname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
44                  sprintf(errmsg, "cannot find font file \"%s\"", fname);
45                  error(USER, errmsg);
46          }
# Line 46 | Line 48 | char  *fname;
48          if (f == NULL)
49                  goto memerr;
50          f->name = savestr(fname);
51 +        f->nref = 1;
52          if ((fp = fopen(pathname, "r")) == NULL) {
53                  sprintf(errmsg, "cannot open font file \"%s\"",
54                                  pathname);
55                  error(SYSTEM, errmsg);
56          }
57 <        wsum = hsum = ngly = 0;
58 <        while (fgetword(buf,sizeof(buf),fp) != NULL) {  /* get each glyph */
59 <                if (!isint(buf))
57 >        wsum = hsum = ngly = 0;                 /* get each glyph */
58 >        while ((ngv = fgetval(fp, 'i', (char *)&gn)) != EOF) {
59 >                if (ngv == 0)
60                          goto nonint;
58                gn = atoi(buf);
61                  if (gn < 1 || gn > 255) {
62                          err = "illegal";
63                          goto fonterr;
# Line 64 | Line 66 | char  *fname;
66                          err = "duplicate";
67                          goto fonterr;
68                  }
69 <                if (fgetword(buf,sizeof(buf),fp) == NULL || !isint(buf) ||
70 <                                (ngv = atoi(buf)) < 0 || ngv > 32000) {
69 >                if (fgetval(fp, 'i', (char *)&ngv) <= 0 ||
70 >                                ngv < 0 || ngv > 32000) {
71                          err = "bad # vertices for";
72                          goto fonterr;
73                  }
# Line 77 | Line 79 | char  *fname;
79                  ngv *= 2;
80                  gp = gvlist(g);
81                  while (ngv--) {
82 <                        if (fgetword(buf,sizeof(buf),fp) == NULL ||
83 <                                        !isint(buf) ||
82 <                                        (gv = atoi(buf)) < 0 || gv > 255) {
82 >                        if (fgetval(fp, 'i', (char *)&gv) <= 0 ||
83 >                                        gv < 0 || gv > 255) {
84                                  err = "bad vertex for";
85                                  goto fonterr;
86                          }
# Line 119 | Line 120 | fonterr:
120          error(USER, errmsg);
121   memerr:
122          error(SYSTEM, "out of memory in fontglyph");
123 +        return NULL; /* pro forma return */
124   }
125  
126  
127 + void
128 + freefont(fnt)                   /* release a font (free all if NULL) */
129 + FONT *fnt;
130 + {
131 +        FONT  head;
132 +        register FONT  *fl, *f;
133 +        register int  i;
134 +                                        /* check reference count */
135 +        if (fnt != NULL && ((fnt->nref-- > 1) | retainfonts))
136 +                return;
137 +        head.next = fontlist;
138 +        fl = &head;
139 +        while ((f = fl->next) != NULL)
140 +                if ((fnt == NULL) | (fnt == f)) {
141 +                        fl->next = f->next;
142 +                        for (i = 0; i < 256; i++)
143 +                                if (f->fg[i] != NULL)
144 +                                        free((void *)f->fg[i]);
145 +                        freestr(f->name);
146 +                        free((void *)f);
147 +                } else
148 +                        fl = f;
149 +        fontlist = head.next;
150 + }
151 +
152 +
153   int
154   uniftext(sp, tp, f)                     /* uniformly space text line */
155 < register short  *sp;            /* returned character spacing */
155 > register short  *sp;            /* returned character spacing */
156   register char  *tp;             /* text line */
157   register FONT  *f;              /* font */
158   {
# Line 135 | Line 163 | register FONT  *f;             /* font */
163                  if (f->fg[*tp++&0xff] == NULL)
164                          *sp++ = 0;
165                  else
166 <                        linelen += *sp++ = 256;
166 >                        linelen += *sp++ = 255;
167          return(linelen);
168   }
169  
# Line 148 | Line 176 | FONT  *f;                      /* font */
176   int  cis;                       /* intercharacter spacing */
177   {
178          int  linelen;
179 <        register GLYPH  *gp;
179 >        register GLYPH  *gp;
180  
181 +        linelen = 0;
182          gp = NULL;
183          while (*tp && (gp = f->fg[*tp++&0xff]) == NULL)
184                  *sp++ = 0;
185          cis /= 2;
186 <        linelen = *sp = cis;
186 >        *sp = cis;
187          while (gp != NULL) {
188                  if (gp->nverts) {               /* regular character */
189                          linelen += *sp++ += cis - gp->left;
# Line 182 | Line 211 | FONT  *f;                      /* font */
211   int  cis;                       /* target intercharacter spacing */
212   int  nsi;                       /* minimum number of spaces for indent */
213   {
214 <        register char  *end, *tab;
214 >        register char  *end, *tab = NULL;
215          GLYPH  *gp;
216          short  *nsp;
217          int  alen, len, width;
218                                          /* start by squeezing it */
219          squeeztext(sp, tp, f, cis);
220                                          /* now, realign spacing */
221 <        len = 0;
193 <        width = alen = *sp++;
221 >        width = *sp++;
222          while (*tp) {
223 +                len = alen = 0;
224                  nsp = sp;
225                  for (end = tp; *end; end = tab) {
226                          tab = end + 1;
# Line 204 | Line 233 | int  nsi;                      /* minimum number of spaces for indent */
233                                  }
234                                  len += tab - end;
235                          }
236 <                        if (tab - end > nsi)
236 >                        if (nsi && tab - end > nsi)
237                                  break;
238                  }
239                  len *= f->mwidth + cis;         /* compute target length */
# Line 215 | Line 244 | int  nsi;                      /* minimum number of spaces for indent */
244                          *sp++ += alen;
245                          len -= alen;
246                  }
218                len = 0;
219                alen = 0;
247                  tp = tab;
248          }
249          return(width);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines