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

Comparing ray/src/rt/text.c (file contents):
Revision 1.7 by greg, Mon Aug 5 09:32:12 1991 UTC vs.
Revision 2.22 by greg, Tue May 13 17:58:33 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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   *  text.c - functions for text patterns and mixtures.
9 *
10 *     11/12/86
6   */
7  
8 + #include "copyright.h"
9 +
10   #include  "ray.h"
11  
12   #include  "otypes.h"
13  
14 + #include  "font.h"
15 +
16   /*
17   *      A text pattern is specified as the text (a file or line),
18   *  the upper left anchor point, the right motion vector, the down
# Line 23 | Line 22 | static char SCCSid[] = "$SunId$ LBL";
22   *      modifier brighttext id
23   *      2 fontfile textfile
24   *      0
25 < *      11
25 > *      11+
26   *              Ax Ay Az
27   *              Rx Ry Rz
28   *              Dx Dy Dz
29   *              foreground background
30 + *              [spacing]
31   *
32   *  For a single line, we use:
33   *
34   *      modifier brighttext id
35   *      N+2 fontfile . This is a line with N words...
36   *      0
37 < *      11
37 > *      11+
38   *              Ax Ay Az
39   *              Rx Ry Rz
40   *              Dx Dy Dz
41   *              foreground background
42 + *              [spacing]
43   *
44   *  Colortext is identical, except colors are given rather than
45 < *  brightnesses.  Mixtext has foreground and background modifiers:
45 > *  brightnesses.
46   *
47 + *  Mixtext has foreground and background modifiers:
48 + *
49   *      modifier mixtext id
50   *      4+ foremod backmod fontfile text..
51   *      0
52 < *      9
52 > *      9+
53   *              Ax Ay Az
54   *              Rx Ry Rz
55   *              Dx Dy Dz
56 + *              [spacing]
57   */
58  
59   #define  fndx(m)        ((m)->otype==MIX_TEXT ? 2 : 0)
60   #define  tndx(m)        ((m)->otype==MIX_TEXT ? 3 : 1)
61 + #define  sndx(m)        ((m)->otype==PAT_BTEXT ? 11 : \
62 +                                (m)->otype==PAT_CTEXT ? 15 : 9)
63  
64 < extern char  *libpath;                  /* library search path */
64 > typedef struct tline {
65 >        struct tline  *next;            /* pointer to next line */
66 >        short  *spc;                    /* character spacing */
67 >        int  width;                     /* total line width */
68 >                                        /* followed by the string */
69 > }  TLINE;
70  
71 < typedef unsigned char  GLYPH;
71 > #define  TLSTR(l)       ((char *)((l)+1))
72  
62 typedef struct font {
63        GLYPH  *fg[256];                /* font glyphs */
64        char  *name;                    /* font file name */
65        struct font  *next;             /* next font in list */
66 }  FONT;
67
73   typedef struct {
69        char  **t;                      /* text array */
74          FVECT  right, down;             /* right and down unit vectors */
75          FONT  *f;                       /* our font */
76 +        TLINE  tl;                      /* line list */
77   }  TEXT;
78  
74 extern char  *fgetword();
75
79   TEXT  *gettext();
80  
81 < FONT  *getfont();
81 > TLINE  *tlalloc();
82  
80 static FONT  *fontlist = NULL;          /* our font list */
83  
84 <
83 < text(m, r)
84 > do_text(m, r)
85   register OBJREC  *m;
86   RAY  *r;
87   {
88 <        double  v[3];
88 >        FVECT  v;
89          int  foreground;
90                                  /* get transformed position */
91          if (r->rox != NULL)
# Line 99 | Line 100 | RAY  *r;
100                  char  *modname = m->oargs.sarg[foreground ? 0 : 1];
101                  if (!strcmp(modname, VOIDID))
102                          omod = OVOID;
103 <                else if ((omod = modifier(modname)) == OVOID) {
103 >                else if ((omod = lastmod(objndx(m), modname)) == OVOID) {
104                          sprintf(errmsg, "undefined modifier \"%s\"", modname);
105                          objerror(m, USER, errmsg);
106                  }
107 <                raytexture(r, omod);
107 >                if (rayshade(r, omod)) {
108 >                        if (m->omod != OVOID)
109 >                                objerror(m, USER, "inappropriate modifier");
110 >                        return(1);
111 >                }
112          } else if (m->otype == PAT_BTEXT) {
113                  if (foreground)
114                          scalecolor(r->pcol, m->oargs.farg[9]);
# Line 121 | Line 126 | RAY  *r;
126                                          m->oargs.farg[14]);
127                  multcolor(r->pcol, cval);
128          }
129 +        return(0);
130   }
131  
132  
133 + TLINE *
134 + tlalloc(s)                      /* allocate and assign text line */
135 + char  *s;
136 + {
137 +        register int  siz;
138 +        register TLINE  *tl;
139 +
140 +        siz = strlen(s) + 1;
141 +        if ((tl=(TLINE *)malloc(sizeof(TLINE)+siz)) == NULL ||
142 +                        (tl->spc=(short *)malloc(siz*sizeof(short))) == NULL)
143 +                error(SYSTEM, "out of memory in tlalloc");
144 +        tl->next = NULL;
145 +        strcpy(TLSTR(tl), s);
146 +        return(tl);
147 + }
148 +
149 +
150   TEXT *
151   gettext(tm)                     /* get text structure for material */
152   register OBJREC  *tm;
153   {
154   #define  R      (tm->oargs.farg+3)
155   #define  D      (tm->oargs.farg+6)
133        extern char  *strcpy(), *fgets();
156          FVECT  DxR;
157          double  d;
158          FILE  *fp;
159          char  linbuf[512];
160 <        register TEXT  *t;
160 >        TEXT  *t;
161          register int  i;
162 +        register TLINE  *tlp;
163          register char  *s;
164  
165          if ((t = (TEXT *)tm->os) != NULL)
166                  return(t);
167                                                  /* check arguments */
168 <        if (tm->oargs.nsargs - tndx(tm) < 1 ||
146 <                        tm->oargs.nfargs != (tm->otype == PAT_BTEXT ? 11 :
147 <                                        tm->otype == PAT_CTEXT ? 15 : 9))
168 >        if (tm->oargs.nsargs - tndx(tm) < 1 || tm->oargs.nfargs < sndx(tm))
169                  objerror(tm, USER, "bad # arguments");
170          if ((t = (TEXT *)malloc(sizeof(TEXT))) == NULL)
171 <                goto memerr;
171 >                error(SYSTEM, "out of memory in gettext");
172                                                  /* compute vectors */
173          fcross(DxR, D, R);
174          fcross(t->right, DxR, D);
175 <        d = DOT(D,D) / DOT(t->right,t->right);
175 >        d = DOT(t->right,t->right);
176 >        if (d <= FTINY*FTINY*FTINY*FTINY)
177 >                objerror(tm, USER, "illegal motion vector");
178 >        d = DOT(D,D)/d;
179          for (i = 0; i < 3; i++)
180                  t->right[i] *= d;
181          fcross(t->down, R, DxR);
182 <        d = DOT(R,R) / DOT(t->down,t->down);
182 >        d = DOT(R,R)/DOT(t->down,t->down);
183          for (i = 0; i < 3; i++)
184                  t->down[i] *= d;
185                                                  /* get text */
186 <        t->t = (char **)malloc(2*sizeof(char **));
163 <        if (t->t == NULL)
164 <                goto memerr;
186 >        tlp = &t->tl;
187          if (tm->oargs.nsargs - tndx(tm) > 1) {  /* single line */
188                  s = linbuf;
189                  for (i = tndx(tm)+1; i < tm->oargs.nsargs; i++) {
# Line 170 | Line 192 | register OBJREC  *tm;
192                          *s++ = ' ';
193                  }
194                  *--s = '\0';
195 <                t->t[0] = savqstr(linbuf);
196 <                t->t[1] = NULL;
195 >                tlp->next = tlalloc(linbuf);
196 >                tlp = tlp->next;
197          } else {                                /* text file */
198                  if ((s = getpath(tm->oargs.sarg[tndx(tm)],
199 <                                libpath, R_OK)) == NULL) {
199 >                                getrlibpath(), R_OK)) == NULL) {
200                          sprintf(errmsg, "cannot find text file \"%s\"",
201                                          tm->oargs.sarg[tndx(tm)]);
202                          error(USER, errmsg);
203                  }
204                  if ((fp = fopen(s, "r")) == NULL) {
205 <                        sprintf(errmsg, "cannot open text file \"%s\"",
184 <                                        s);
205 >                        sprintf(errmsg, "cannot open text file \"%s\"", s);
206                          error(SYSTEM, errmsg);
207                  }
208 <                for (i=0; fgets(linbuf,sizeof(linbuf),fp)!=NULL; i++) {
208 >                while (fgets(linbuf, sizeof(linbuf), fp) != NULL) {
209                          s = linbuf + strlen(linbuf) - 1;
210                          if (*s == '\n')
211                                  *s = '\0';
212 <                        t->t=(char **)realloc((char *)t->t,
213 <                                        (i+2)*sizeof(char **));
193 <                        if (t->t == NULL)
194 <                                goto memerr;
195 <                        t->t[i] = savqstr(linbuf);
212 >                        tlp->next = tlalloc(linbuf);
213 >                        tlp = tlp->next;
214                  }
197                t->t[i] = NULL;
215                  fclose(fp);
216          }
217 +        tlp->next = NULL;
218                                                  /* get the font */
219          t->f = getfont(tm->oargs.sarg[fndx(tm)]);
220 +                                                /* compute character spacing */
221 +        i = sndx(tm);
222 +        d = i < tm->oargs.nfargs ? tm->oargs.farg[i] : 0.0;
223 +        i = d * 255.0;
224 +        t->tl.width = 0;
225 +        for (tlp = t->tl.next; tlp != NULL; tlp = tlp->next) {
226 +                if (i < 0)
227 +                        tlp->width = squeeztext(tlp->spc, TLSTR(tlp), t->f, -i);
228 +                else if (i > 0)
229 +                        tlp->width = proptext(tlp->spc, TLSTR(tlp), t->f, i, 3);
230 +                else
231 +                        tlp->width = uniftext(tlp->spc, TLSTR(tlp), t->f);
232 +                if (tlp->width > t->tl.width)
233 +                        t->tl.width = tlp->width;
234 +        }
235                                                  /* we're done */
236          tm->os = (char *)t;
237          return(t);
205 memerr:
206        error(SYSTEM, "out of memory in gettext");
238   #undef  R
239   #undef  D
240   }
# Line 213 | Line 244 | freetext(m)                    /* free text structures associated with
244   OBJREC  *m;
245   {
246          register TEXT  *tp;
247 <        register int  i;
247 >        register TLINE  *tlp;
248  
249          tp = (TEXT *)m->os;
250          if (tp == NULL)
251                  return;
252 <        for (i = 0; tp->t[i] != NULL; i++)
253 <                freeqstr(tp->t[i]);
254 <        free((char *)tp->t);
255 <        free((char *)tp);
252 >        while ((tlp = tp->tl.next) != NULL) {
253 >                tp->tl.next = tlp->next;
254 >                free((void *)tlp->spc);
255 >                free((void *)tlp);
256 >        }
257 >        freefont(tp->f);        /* release font reference */
258 >        free((void *)tp);
259          m->os = NULL;
260   }
261  
262  
263   intext(p, m)                    /* check to see if p is in text glyph */
264   FVECT  p;
265 < register OBJREC  *m;
265 > OBJREC  *m;
266   {
267          register TEXT  *tp;
268 <        register int  i;
269 <        double  v[3], y, x;
270 <        int  col, lno;
268 >        register TLINE  *tlp;
269 >        FVECT  v;
270 >        double  y, x;
271 >        register int  i, h;
272                                  /* first, compute position in text */
273 +        tp = gettext(m);
274          v[0] = p[0] - m->oargs.farg[0];
275          v[1] = p[1] - m->oargs.farg[1];
276          v[2] = p[2] - m->oargs.farg[2];
277 <        col = x = DOT(v, tp->right);
278 <        lno = y = DOT(v, tp->down);
277 >        x = DOT(v, tp->right);
278 >        i = sndx(m);
279 >        if (i < m->oargs.nfargs)
280 >                x *= tp->f->mwidth + 255.*fabs(m->oargs.farg[i]);
281 >        else
282 >                x *= 255.;
283 >        h = x;
284 >        i = y = DOT(v, tp->down);
285          if (x < 0.0 || y < 0.0)
286                  return(0);
287 <        x -= (double)col;
288 <        y = (lno+1) - y;
289 <                                /* get the font character */
290 <        tp = gettext(m);
291 <        for (i = 0; i < lno; i++)
292 <                if (tp->t[i] == NULL)
293 <                        return(0);
252 <        if (col >= strlen(tp->t[i]))
287 >        x -= (double)h;
288 >        y = ((i+1) - y)*255.;
289 >                                /* find the line position */
290 >        for (tlp = tp->tl.next; tlp != NULL; tlp = tlp->next)
291 >                if (--i < 0)
292 >                        break;
293 >        if (tlp == NULL || h >= tlp->width)
294                  return(0);
295 <        return(inglyph(x, y, tp->f->fg[tp->t[i][col]]));
295 >        for (i = 0; (h -= tlp->spc[i]) >= 0; i++)
296 >                if (h < 255 && inglyph(h+x, y,
297 >                                tp->f->fg[TLSTR(tlp)[i]&0xff]))
298 >                        return(1);
299 >        return(0);
300   }
301  
302  
258 FONT *
259 getfont(fname)                          /* return font fname */
260 char  *fname;
261 {
262        char  buf[16];
263        FILE  *fp;
264        char  *pathname, *err;
265        int  gn, ngv, gv;
266        register GLYPH  *g;
267        register FONT  *f;
268
269        for (f = fontlist; f != NULL; f = f->next)
270                if (!strcmp(f->name, fname))
271                        return(f);
272                                                /* load the font file */
273        if ((pathname = getpath(fname, libpath, R_OK)) == NULL) {
274                sprintf(errmsg, "cannot find font file \"%s\"", fname);
275                error(USER, errmsg);
276        }
277        f = (FONT *)calloc(1, sizeof(FONT));
278        if (f == NULL)
279                goto memerr;
280        f->name = savestr(fname);
281        if ((fp = fopen(pathname, "r")) == NULL) {
282                sprintf(errmsg, "cannot open font file \"%s\"",
283                                pathname);
284                error(SYSTEM, errmsg);
285        }
286        while (fgetword(buf,sizeof(buf),fp) != NULL) {  /* get each glyph */
287                if (!isint(buf))
288                        goto nonint;
289                gn = atoi(buf);
290                if (gn < 0 || gn > 255) {
291                        err = "illegal";
292                        goto fonterr;
293                }
294                if (f->fg[gn] != NULL) {
295                        err = "duplicate";
296                        goto fonterr;
297                }
298                if (fgetword(buf,sizeof(buf),fp) == NULL || !isint(buf) ||
299                                (ngv = atoi(buf)) < 0 || ngv > 255) {
300                        err = "bad # vertices for";
301                        goto fonterr;
302                }
303                g = (GLYPH *)malloc((2*ngv+1)*sizeof(GLYPH));
304                if (g == NULL)
305                        goto memerr;
306                f->fg[gn] = g;
307                *g++ = ngv;
308                ngv *= 2;
309                while (ngv--) {
310                        if (fgetword(buf,sizeof(buf),fp) == NULL ||
311                                        !isint(buf) ||
312                                        (gv = atoi(buf)) < 0 || gv > 255) {
313                                err = "bad vertex for";
314                                goto fonterr;
315                        }
316                        *g++ = gv;
317                }
318        }
319        fclose(fp);
320        f->next = fontlist;
321        return(fontlist = f);
322 nonint:
323        sprintf(errmsg, "non-integer in font file \"%s\"", pathname);
324        error(USER, errmsg);
325 fonterr:
326        sprintf(errmsg, "%s character (%d) in font file \"%s\"",
327                        err, gn, pathname);
328        error(USER, errmsg);
329 memerr:
330        error(SYSTEM, "out of memory in fontglyph");
331 }
332
333
303   inglyph(x, y, gl)               /* (x,y) within font glyph gl? */
304 < double  x, y;
305 < GLYPH  *gl;
304 > double  x, y;           /* real coordinates in range [0,255) */
305 > register GLYPH  *gl;
306   {
307          int  n, ncross;
308          int  xlb, ylb;
309 <        register GLYPH  *p0, *p1;
309 >        int  tv;
310 >        register GORD  *p0, *p1;
311  
312          if (gl == NULL)
313                  return(0);
314 <        x *= 256.0;                     /* get glyph coordinates */
315 <        y *= 256.0;
316 <        xlb = x + 0.5;
317 <        ylb = y + 0.5;
318 <        n = *gl++;                      /* get # of vertices */
319 <        p0 = gl + 2*(n-1);              /* connect last to first */
320 <        p1 = gl;
314 >        xlb = x;
315 >        ylb = y;
316 >        if (gl->left > xlb || gl->right <= xlb ||       /* check extent */
317 >                        gl->bottom > ylb || gl->top <= ylb)
318 >                return(0);
319 >        xlb = xlb<<1 | 1;               /* add 1/2 to test points... */
320 >        ylb = ylb<<1 | 1;               /* ...so no equal comparisons */
321 >        n = gl->nverts;                 /* get # of vertices */
322 >        p0 = gvlist(gl) + 2*(n-1);      /* connect last to first */
323 >        p1 = gvlist(gl);
324          ncross = 0;
325                                          /* positive x axis cross test */
326          while (n--) {
327 <                if ((p0[1] > ylb) ^ (p1[1] > ylb))
328 <                        if (p0[0] > xlb && p1[0] > xlb)
327 >                if ((p0[1]<<1 > ylb) ^ (p1[1]<<1 > ylb)) {
328 >                        tv = p0[0]<<1 > xlb | (p1[0]<<1 > xlb) << 1;
329 >                        if (tv == 03)
330                                  ncross++;
331 <                        else if (p0[0] > xlb || p1[0] > xlb)
331 >                        else if (tv)
332                                  ncross += (p1[1] > p0[1]) ^
333                                                  ((p0[1]-y)*(p1[0]-x) >
334                                                  (p0[0]-x)*(p1[1]-y));
335 +                }
336                  p0 = p1;
337                  p1 += 2;
338          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines