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.1 by greg, Sat Jun 6 07:38:40 1992 UTC vs.
Revision 2.24 by greg, Sat Nov 20 22:39:01 2021 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 + int     retainfonts = 0;                /* retain loaded fonts? */
20  
18 extern char  *libpath;                  /* list of library directories */
19
21   static FONT     *fontlist = NULL;       /* list of loaded fonts */
22  
23  
24   FONT *
25 < getfont(fname)                          /* return font fname */
26 < char  *fname;
25 > getfont(                        /* return font fname */
26 >        char  *fname
27 > )
28   {
29 <        char  buf[16];
29 >        char  embuf[512];
30          FILE  *fp;
31 <        char  *pathname, *err;
32 <        int  gn, ngv;
33 <        register int  gv;
34 <        register GLYPH  *g;
31 >        char  *pathname, *err = NULL;
32 >        unsigned  wsum, hsum, ngly;
33 >        int  gn, ngv, gv;
34 >        GLYPH   *g;
35          GORD  *gp;
36 <        register FONT  *f;
36 >        FONT  *f;
37  
38          for (f = fontlist; f != NULL; f = f->next)
39 <                if (!strcmp(f->name, fname))
39 >                if (!strcmp(f->name, fname)) {
40 >                        f->nref++;
41                          return(f);
42 +                }
43                                                  /* load the font file */
44 <        if ((pathname = getpath(fname, libpath, R_OK)) == NULL) {
45 <                sprintf(errmsg, "cannot find font file \"%s\"", fname);
46 <                error(USER, errmsg);
44 >        if ((pathname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
45 >                sprintf(embuf, "cannot find font file \"%s\"\n", fname);
46 >                eputs(embuf);
47 >                return(NULL);
48          }
49 +        if ((fp = fopen(pathname, "r")) == NULL) {
50 +                sprintf(embuf, "cannot open font file \"%s\"\n", pathname);
51 +                eputs(embuf);
52 +                return(NULL);
53 +        }
54          f = (FONT *)calloc(1, sizeof(FONT));
55          if (f == NULL)
56                  goto memerr;
57 <        f->name = savestr(fname);
58 <        if ((fp = fopen(pathname, "r")) == NULL) {
59 <                sprintf(errmsg, "cannot open font file \"%s\"",
60 <                                pathname);
61 <                error(SYSTEM, errmsg);
52 <        }
53 <        while (fgetword(buf,sizeof(buf),fp) != NULL) {  /* get each glyph */
54 <                if (!isint(buf))
57 >        strcpy(f->name, fname);
58 >        f->nref = 1;
59 >        wsum = hsum = ngly = 0;                 /* get each glyph */
60 >        while ((ngv = fgetval(fp, 'i', (char *)&gn)) != EOF) {
61 >                if (ngv == 0)
62                          goto nonint;
63 <                gn = atoi(buf);
57 <                if (gn < 0 || gn > 255) {
63 >                if (gn < 1 || gn > 255) {
64                          err = "illegal";
65                          goto fonterr;
66                  }
# Line 62 | Line 68 | char  *fname;
68                          err = "duplicate";
69                          goto fonterr;
70                  }
71 <                if (fgetword(buf,sizeof(buf),fp) == NULL || !isint(buf) ||
72 <                                (ngv = atoi(buf)) < 0 || ngv > 32000) {
71 >                if (fgetval(fp, 'i', (char *)&ngv) <= 0 ||
72 >                                ngv < 0 || ngv > 32000) {
73                          err = "bad # vertices for";
74                          goto fonterr;
75                  }
# Line 71 | Line 77 | char  *fname;
77                  if (g == NULL)
78                          goto memerr;
79                  g->nverts = ngv;
80 <                g->start = g->width = 128;
80 >                g->left = g->right = g->top = g->bottom = 128;
81                  ngv *= 2;
82                  gp = gvlist(g);
83                  while (ngv--) {
84 <                        if (fgetword(buf,sizeof(buf),fp) == NULL ||
85 <                                        !isint(buf) ||
80 <                                        (gv = atoi(buf)) < 0 || gv > 255) {
84 >                        if (fgetval(fp, 'i', (char *)&gv) <= 0 ||
85 >                                        gv < 0 || gv > 255) {
86                                  err = "bad vertex for";
87                                  goto fonterr;
88                          }
89                          *gp++ = gv;
90 <                        if (ngv & 1)            /* follow x limits */
91 <                                if (gv < g->start)
92 <                                        g->start = gv;
93 <                                else if (gv > g->width)
94 <                                        g->width = gv;
90 >                        if (ngv & 1) {          /* follow x limits */
91 >                                if (gv < g->left)
92 >                                        g->left = gv;
93 >                                else if (gv > g->right)
94 >                                        g->right = gv;
95 >                        } else {                /* follow y limits */
96 >                                if (gv < g->bottom)
97 >                                        g->bottom = gv;
98 >                                else if (gv > g->top)
99 >                                        g->top = gv;
100 >                        }
101                  }
102 <                g->width -= g->start;
102 >                if (g->right - g->left && g->top - g->bottom) {
103 >                        ngly++;
104 >                        wsum += g->right - g->left;
105 >                        hsum += g->top - g->bottom;
106 >                }
107                  f->fg[gn] = g;
108          }
109          fclose(fp);
110 +        if (ngly) {
111 +                f->mwidth = wsum / ngly;
112 +                f->mheight = hsum / ngly;
113 +        }
114          f->next = fontlist;
115          return(fontlist = f);
116   nonint:
117 <        sprintf(errmsg, "non-integer in font file \"%s\"", pathname);
118 <        error(USER, errmsg);
117 >        sprintf(embuf, "non-integer in font file \"%s\"\n", pathname);
118 >        eputs(embuf);
119 >        fclose(fp);
120 >        return(NULL);
121   fonterr:
122 <        sprintf(errmsg, "%s character (%d) in font file \"%s\"",
122 >        sprintf(embuf, "%s character (%d) in font file \"%s\"\n",
123                          err, gn, pathname);
124 <        error(USER, errmsg);
124 >        eputs(embuf);
125 >        fclose(fp);
126 >        return(NULL);
127   memerr:
128 <        error(SYSTEM, "out of memory in fontglyph");
128 >        eputs("out of memory in getfont()\n");
129 >        fclose(fp);
130 >        return(NULL);
131 > }
132 >
133 >
134 > void
135 > freefont(                       /* release a font (free all if NULL) */
136 >        FONT *fnt
137 > )
138 > {
139 >        FONT  head;
140 >        FONT  *fl, *f;
141 >        int  i;
142 >                                        /* check reference count */
143 >        if (fnt != NULL && ((fnt->nref-- > 1) | retainfonts))
144 >                return;
145 >        head.next = fontlist;
146 >        fl = &head;
147 >        while ((f = fl->next) != NULL)
148 >                if ((fnt == NULL) | (fnt == f)) {
149 >                        fl->next = f->next;
150 >                        for (i = 0; i < 256; i++)
151 >                                if (f->fg[i] != NULL)
152 >                                        free((void *)f->fg[i]);
153 >                        free((void *)f);
154 >                } else
155 >                        fl = f;
156 >        fontlist = head.next;
157 > }
158 >
159 >
160 > int
161 > uniftext(                       /* uniformly space text line */
162 >        short   *sp,            /* returned character spacing */
163 >        char  *tp,              /* text line */
164 >        FONT  *f                /* font */
165 > )
166 > {
167 >        int  linelen;
168 >
169 >        linelen = *sp++ = 0;
170 >        while (*tp)
171 >                if (f->fg[*tp++&0xff] == NULL)
172 >                        *sp++ = 0;
173 >                else
174 >                        linelen += *sp++ = 255;
175 >        return(linelen);
176 > }
177 >
178 >
179 > int
180 > squeeztext(             /* squeeze text line */
181 >        short  *sp,                     /* returned character spacing */
182 >        char  *tp,                      /* text line */
183 >        FONT  *f,                       /* font */
184 >        int  cis                        /* intercharacter spacing */
185 > )
186 > {
187 >        int  linelen;
188 >        GLYPH   *gp;
189 >
190 >        linelen = 0;
191 >        gp = NULL;
192 >        while (*tp && (gp = f->fg[*tp++&0xff]) == NULL)
193 >                *sp++ = 0;
194 >        cis /= 2;
195 >        *sp = cis;
196 >        while (gp != NULL) {
197 >                if (gp->nverts) {               /* regular character */
198 >                        linelen += *sp++ += cis - gp->left;
199 >                        *sp = gp->right + cis;
200 >                } else {                        /* space */
201 >                        linelen += *sp++;
202 >                        *sp = f->mwidth;
203 >                }
204 >                gp = NULL;
205 >                while (*tp && (gp = f->fg[*tp++&0xff]) == NULL) {
206 >                        linelen += *sp++;
207 >                        *sp = 0;
208 >                }
209 >        }
210 >        linelen += *sp += cis;
211 >        return(linelen);
212 > }
213 >
214 >
215 > int
216 > proptext(               /* space line proportionally */
217 >        short  *sp,                     /* returned character spacing */
218 >        char  *tp,                      /* text line */
219 >        FONT  *f,                       /* font */
220 >        int  cis,                       /* target intercharacter spacing */
221 >        int  nsi                        /* minimum number of spaces for indent */
222 > )
223 > {
224 >        char  *end, *tab = NULL;
225 >        GLYPH  *gp;
226 >        short  *nsp;
227 >        int  alen, len, width;
228 >                                        /* start by squeezing it */
229 >        squeeztext(sp, tp, f, cis);
230 >                                        /* now, realign spacing */
231 >        width = *sp++;
232 >        while (*tp) {
233 >                len = alen = 0;
234 >                nsp = sp;
235 >                for (end = tp; *end; end = tab) {
236 >                        tab = end + 1;
237 >                        alen += *nsp++;
238 >                        if (f->fg[*end&0xff]) {
239 >                                while ((gp = f->fg[*tab&0xff]) != NULL &&
240 >                                                gp->nverts == 0) { /* tab in */
241 >                                        alen += *nsp++;
242 >                                        tab++;
243 >                                }
244 >                                len += tab - end;
245 >                        }
246 >                        if (nsi && tab - end > nsi)
247 >                                break;
248 >                }
249 >                len *= f->mwidth + cis;         /* compute target length */
250 >                width += len;
251 >                len -= alen;                    /* necessary adjustment */
252 >                while (sp < nsp) {
253 >                        alen = len/(nsp-sp);
254 >                        *sp++ += alen;
255 >                        len -= alen;
256 >                }
257 >                tp = tab;
258 >        }
259 >        return(width);
260   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines