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.10 by greg, Wed Oct 23 13:43:49 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  
58 extern char  *libpath;                  /* library search path */
59
60 typedef unsigned char  GLYPH;
61
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
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  
# Line 78 | Line 76 | typedef struct {
76          TLINE  tl;                      /* line list */
77   }  TEXT;
78  
81 extern char  *fgetword();
82
79   TEXT  *gettext();
80  
81   TLINE  *tlalloc();
82  
87 FONT  *getfont();
83  
84 < static FONT  *fontlist = NULL;          /* our font list */
90 <
91 <
92 < text(m, r)
84 > do_text(m, r)
85   register OBJREC  *m;
86   RAY  *r;
87   {
# Line 108 | 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 130 | Line 126 | RAY  *r;
126                                          m->oargs.farg[14]);
127                  multcolor(r->pcol, cval);
128          }
129 +        return(0);
130   }
131  
132  
# Line 137 | Line 134 | TLINE *
134   tlalloc(s)                      /* allocate and assign text line */
135   char  *s;
136   {
137 <        extern char  *strcpy();
137 >        register int  siz;
138          register TLINE  *tl;
139  
140 <        tl = (TLINE *)malloc(sizeof(TLINE)+1+strlen(s));
141 <        if (tl == NULL)
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);
# Line 155 | Line 153 | register OBJREC  *tm;
153   {
154   #define  R      (tm->oargs.farg+3)
155   #define  D      (tm->oargs.farg+6)
158        extern char  *strcpy(), *fgets();
156          FVECT  DxR;
157          double  d;
158          FILE  *fp;
# Line 168 | Line 165 | register OBJREC  *tm;
165          if ((t = (TEXT *)tm->os) != NULL)
166                  return(t);
167                                                  /* check arguments */
168 <        if (tm->oargs.nsargs - tndx(tm) < 1 ||
172 <                        tm->oargs.nfargs != (tm->otype == PAT_BTEXT ? 11 :
173 <                                        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                  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);
# Line 198 | Line 196 | register OBJREC  *tm;
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\"",
208 <                                        s);
205 >                        sprintf(errmsg, "cannot open text file \"%s\"", s);
206                          error(SYSTEM, errmsg);
207                  }
208                  while (fgets(linbuf, sizeof(linbuf), fp) != NULL) {
# Line 220 | Line 217 | register OBJREC  *tm;
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);
# Line 237 | Line 249 | OBJREC  *m;
249          tp = (TEXT *)m->os;
250          if (tp == NULL)
251                  return;
252 <        for (tlp = tp->tl.next; tlp != NULL; tlp = tlp->next);
253 <                free((char *)tlp);
254 <        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  
# Line 252 | Line 268 | OBJREC  *m;
268          register TLINE  *tlp;
269          FVECT  v;
270          double  y, x;
271 <        int  col;
256 <        register int  lno;
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 */
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 (--lno < 0)
291 >                if (--i < 0)
292                          break;
293 <        if (tlp == NULL || col >= strlen(TLSTR(tlp)))
293 >        if (tlp == NULL || h >= tlp->width)
294                  return(0);
295 <        return(inglyph(x, y, tp->f->fg[TLSTR(tlp)[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  
278 FONT *
279 getfont(fname)                          /* return font fname */
280 char  *fname;
281 {
282        char  buf[16];
283        FILE  *fp;
284        char  *pathname, *err;
285        int  gn, ngv, gv;
286        register GLYPH  *g;
287        register FONT  *f;
288
289        for (f = fontlist; f != NULL; f = f->next)
290                if (!strcmp(f->name, fname))
291                        return(f);
292                                                /* load the font file */
293        if ((pathname = getpath(fname, libpath, R_OK)) == NULL) {
294                sprintf(errmsg, "cannot find font file \"%s\"", fname);
295                error(USER, errmsg);
296        }
297        f = (FONT *)calloc(1, sizeof(FONT));
298        if (f == NULL)
299                goto memerr;
300        f->name = savestr(fname);
301        if ((fp = fopen(pathname, "r")) == NULL) {
302                sprintf(errmsg, "cannot open font file \"%s\"",
303                                pathname);
304                error(SYSTEM, errmsg);
305        }
306        while (fgetword(buf,sizeof(buf),fp) != NULL) {  /* get each glyph */
307                if (!isint(buf))
308                        goto nonint;
309                gn = atoi(buf);
310                if (gn < 0 || gn > 255) {
311                        err = "illegal";
312                        goto fonterr;
313                }
314                if (f->fg[gn] != NULL) {
315                        err = "duplicate";
316                        goto fonterr;
317                }
318                if (fgetword(buf,sizeof(buf),fp) == NULL || !isint(buf) ||
319                                (ngv = atoi(buf)) < 0 || ngv > 255) {
320                        err = "bad # vertices for";
321                        goto fonterr;
322                }
323                g = (GLYPH *)malloc((2*ngv+1)*sizeof(GLYPH));
324                if (g == NULL)
325                        goto memerr;
326                f->fg[gn] = g;
327                *g++ = ngv;
328                ngv *= 2;
329                while (ngv--) {
330                        if (fgetword(buf,sizeof(buf),fp) == NULL ||
331                                        !isint(buf) ||
332                                        (gv = atoi(buf)) < 0 || gv > 255) {
333                                err = "bad vertex for";
334                                goto fonterr;
335                        }
336                        *g++ = gv;
337                }
338        }
339        fclose(fp);
340        f->next = fontlist;
341        return(fontlist = f);
342 nonint:
343        sprintf(errmsg, "non-integer in font file \"%s\"", pathname);
344        error(USER, errmsg);
345 fonterr:
346        sprintf(errmsg, "%s character (%d) in font file \"%s\"",
347                        err, gn, pathname);
348        error(USER, errmsg);
349 memerr:
350        error(SYSTEM, "out of memory in fontglyph");
351 }
352
353
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