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 2.2 by greg, Sat Jun 6 07:40:23 1992 UTC vs.
Revision 2.24 by greg, Thu Feb 12 18:55:50 2004 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  "paths.h"
13 +
14   #include  "otypes.h"
15  
16   #include  "font.h"
# Line 25 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24   *      modifier brighttext id
25   *      2 fontfile textfile
26   *      0
27 < *      11
27 > *      11+
28   *              Ax Ay Az
29   *              Rx Ry Rz
30   *              Dx Dy Dz
31   *              foreground background
32 + *              [spacing]
33   *
34   *  For a single line, we use:
35   *
36   *      modifier brighttext id
37   *      N+2 fontfile . This is a line with N words...
38   *      0
39 < *      11
39 > *      11+
40   *              Ax Ay Az
41   *              Rx Ry Rz
42   *              Dx Dy Dz
43   *              foreground background
44 + *              [spacing]
45   *
46   *  Colortext is identical, except colors are given rather than
47 < *  brightnesses.  Mixtext has foreground and background modifiers:
47 > *  brightnesses.
48   *
49 + *  Mixtext has foreground and background modifiers:
50 + *
51   *      modifier mixtext id
52   *      4+ foremod backmod fontfile text..
53   *      0
54 < *      9
54 > *      9+
55   *              Ax Ay Az
56   *              Rx Ry Rz
57   *              Dx Dy Dz
58 + *              [spacing]
59   */
60  
61   #define  fndx(m)        ((m)->otype==MIX_TEXT ? 2 : 0)
62   #define  tndx(m)        ((m)->otype==MIX_TEXT ? 3 : 1)
63 + #define  sndx(m)        ((m)->otype==PAT_BTEXT ? 11 : \
64 +                                (m)->otype==PAT_CTEXT ? 15 : 9)
65  
66   typedef struct tline {
67          struct tline  *next;            /* pointer to next line */
68 +        short  *spc;                    /* character spacing */
69 +        int  width;                     /* total line width */
70                                          /* followed by the string */
71   }  TLINE;
72  
# Line 70 | Line 78 | typedef struct {
78          TLINE  tl;                      /* line list */
79   }  TEXT;
80  
73 extern char  *fgetword();
74
81   TEXT  *gettext();
82  
83   TLINE  *tlalloc();
84  
85  
86 < text(m, r)
86 > do_text(m, r)
87   register OBJREC  *m;
88   RAY  *r;
89   {
# Line 96 | Line 102 | RAY  *r;
102                  char  *modname = m->oargs.sarg[foreground ? 0 : 1];
103                  if (!strcmp(modname, VOIDID))
104                          omod = OVOID;
105 <                else if ((omod = modifier(modname)) == OVOID) {
105 >                else if ((omod = lastmod(objndx(m), modname)) == OVOID) {
106                          sprintf(errmsg, "undefined modifier \"%s\"", modname);
107                          objerror(m, USER, errmsg);
108                  }
109 <                raytexture(r, omod);
109 >                if (rayshade(r, omod)) {
110 >                        if (m->omod != OVOID)
111 >                                objerror(m, USER, "inappropriate modifier");
112 >                        return(1);
113 >                }
114          } else if (m->otype == PAT_BTEXT) {
115                  if (foreground)
116                          scalecolor(r->pcol, m->oargs.farg[9]);
# Line 118 | Line 128 | RAY  *r;
128                                          m->oargs.farg[14]);
129                  multcolor(r->pcol, cval);
130          }
131 +        return(0);
132   }
133  
134  
# Line 125 | Line 136 | TLINE *
136   tlalloc(s)                      /* allocate and assign text line */
137   char  *s;
138   {
139 <        extern char  *strcpy();
139 >        register int  siz;
140          register TLINE  *tl;
141  
142 <        tl = (TLINE *)malloc(sizeof(TLINE)+1+strlen(s));
143 <        if (tl == NULL)
142 >        siz = strlen(s) + 1;
143 >        if ((tl=(TLINE *)malloc(sizeof(TLINE)+siz)) == NULL ||
144 >                        (tl->spc=(short *)malloc(siz*sizeof(short))) == NULL)
145                  error(SYSTEM, "out of memory in tlalloc");
146          tl->next = NULL;
147          strcpy(TLSTR(tl), s);
# Line 143 | Line 155 | register OBJREC  *tm;
155   {
156   #define  R      (tm->oargs.farg+3)
157   #define  D      (tm->oargs.farg+6)
146        extern char  *strcpy(), *fgets();
158          FVECT  DxR;
159          double  d;
160          FILE  *fp;
# Line 156 | Line 167 | register OBJREC  *tm;
167          if ((t = (TEXT *)tm->os) != NULL)
168                  return(t);
169                                                  /* check arguments */
170 <        if (tm->oargs.nsargs - tndx(tm) < 1 ||
160 <                        tm->oargs.nfargs != (tm->otype == PAT_BTEXT ? 11 :
161 <                                        tm->otype == PAT_CTEXT ? 15 : 9))
170 >        if (tm->oargs.nsargs - tndx(tm) < 1 || tm->oargs.nfargs < sndx(tm))
171                  objerror(tm, USER, "bad # arguments");
172          if ((t = (TEXT *)malloc(sizeof(TEXT))) == NULL)
173                  error(SYSTEM, "out of memory in gettext");
174                                                  /* compute vectors */
175          fcross(DxR, D, R);
176          fcross(t->right, DxR, D);
177 <        d = DOT(D,D)/DOT(t->right,t->right);
177 >        d = DOT(t->right,t->right);
178 >        if (d <= FTINY*FTINY*FTINY*FTINY)
179 >                objerror(tm, USER, "illegal motion vector");
180 >        d = DOT(D,D)/d;
181          for (i = 0; i < 3; i++)
182                  t->right[i] *= d;
183          fcross(t->down, R, DxR);
# Line 186 | Line 198 | register OBJREC  *tm;
198                  tlp = tlp->next;
199          } else {                                /* text file */
200                  if ((s = getpath(tm->oargs.sarg[tndx(tm)],
201 <                                libpath, R_OK)) == NULL) {
201 >                                getrlibpath(), R_OK)) == NULL) {
202                          sprintf(errmsg, "cannot find text file \"%s\"",
203                                          tm->oargs.sarg[tndx(tm)]);
204                          error(USER, errmsg);
205                  }
206                  if ((fp = fopen(s, "r")) == NULL) {
207 <                        sprintf(errmsg, "cannot open text file \"%s\"",
196 <                                        s);
207 >                        sprintf(errmsg, "cannot open text file \"%s\"", s);
208                          error(SYSTEM, errmsg);
209                  }
210                  while (fgets(linbuf, sizeof(linbuf), fp) != NULL) {
# Line 208 | Line 219 | register OBJREC  *tm;
219          tlp->next = NULL;
220                                                  /* get the font */
221          t->f = getfont(tm->oargs.sarg[fndx(tm)]);
222 +                                                /* compute character spacing */
223 +        i = sndx(tm);
224 +        d = i < tm->oargs.nfargs ? tm->oargs.farg[i] : 0.0;
225 +        i = d * 255.0;
226 +        t->tl.width = 0;
227 +        for (tlp = t->tl.next; tlp != NULL; tlp = tlp->next) {
228 +                if (i < 0)
229 +                        tlp->width = squeeztext(tlp->spc, TLSTR(tlp), t->f, -i);
230 +                else if (i > 0)
231 +                        tlp->width = proptext(tlp->spc, TLSTR(tlp), t->f, i, 3);
232 +                else
233 +                        tlp->width = uniftext(tlp->spc, TLSTR(tlp), t->f);
234 +                if (tlp->width > t->tl.width)
235 +                        t->tl.width = tlp->width;
236 +        }
237                                                  /* we're done */
238          tm->os = (char *)t;
239          return(t);
# Line 225 | Line 251 | OBJREC  *m;
251          tp = (TEXT *)m->os;
252          if (tp == NULL)
253                  return;
254 <        for (tlp = tp->tl.next; tlp != NULL; tlp = tlp->next);
255 <                free((char *)tlp);
256 <        free((char *)tp);
254 >        while ((tlp = tp->tl.next) != NULL) {
255 >                tp->tl.next = tlp->next;
256 >                free((void *)tlp->spc);
257 >                free((void *)tlp);
258 >        }
259 >        freefont(tp->f);        /* release font reference */
260 >        free((void *)tp);
261          m->os = NULL;
262   }
263  
# Line 240 | Line 270 | OBJREC  *m;
270          register TLINE  *tlp;
271          FVECT  v;
272          double  y, x;
273 <        int  col;
244 <        register int  lno;
273 >        register int  i, h;
274                                  /* first, compute position in text */
275          tp = gettext(m);
276          v[0] = p[0] - m->oargs.farg[0];
277          v[1] = p[1] - m->oargs.farg[1];
278          v[2] = p[2] - m->oargs.farg[2];
279 <        col = x = DOT(v, tp->right);
280 <        lno = y = DOT(v, tp->down);
279 >        x = DOT(v, tp->right);
280 >        i = sndx(m);
281 >        if (i < m->oargs.nfargs)
282 >                x *= tp->f->mwidth + 255.*fabs(m->oargs.farg[i]);
283 >        else
284 >                x *= 255.;
285 >        h = x;
286 >        i = y = DOT(v, tp->down);
287          if (x < 0.0 || y < 0.0)
288                  return(0);
289 <        x -= (double)col;
290 <        y = (lno+1) - y;
291 <                                /* get the font character */
289 >        x -= (double)h;
290 >        y = ((i+1) - y)*255.;
291 >                                /* find the line position */
292          for (tlp = tp->tl.next; tlp != NULL; tlp = tlp->next)
293 <                if (--lno < 0)
293 >                if (--i < 0)
294                          break;
295 <        if (tlp == NULL || col >= strlen(TLSTR(tlp)))
295 >        if (tlp == NULL || h >= tlp->width)
296                  return(0);
297 <        return(inglyph(x, y, tp->f->fg[TLSTR(tlp)[col]&0xff]));
297 >        for (i = 0; (h -= tlp->spc[i]) >= 0; i++)
298 >                if (h < 255 && inglyph(h+x, y,
299 >                                tp->f->fg[TLSTR(tlp)[i]&0xff]))
300 >                        return(1);
301 >        return(0);
302   }
303  
304  
305   inglyph(x, y, gl)               /* (x,y) within font glyph gl? */
306 < double  x, y;
307 < GLYPH  *gl;
306 > double  x, y;           /* real coordinates in range [0,255) */
307 > register GLYPH  *gl;
308   {
309          int  n, ncross;
310          int  xlb, ylb;
311 +        int  tv;
312          register GORD  *p0, *p1;
313  
314          if (gl == NULL)
315                  return(0);
316 <        x *= 256.0;                     /* get glyph coordinates */
317 <        y *= 256.0;
318 <        xlb = x + 0.5;
319 <        ylb = y + 0.5;
316 >        xlb = x;
317 >        ylb = y;
318 >        if (gl->left > xlb || gl->right <= xlb ||       /* check extent */
319 >                        gl->bottom > ylb || gl->top <= ylb)
320 >                return(0);
321 >        xlb = xlb<<1 | 1;               /* add 1/2 to test points... */
322 >        ylb = ylb<<1 | 1;               /* ...so no equal comparisons */
323          n = gl->nverts;                 /* get # of vertices */
324          p0 = gvlist(gl) + 2*(n-1);      /* connect last to first */
325          p1 = gvlist(gl);
326          ncross = 0;
327                                          /* positive x axis cross test */
328          while (n--) {
329 <                if ((p0[1] > ylb) ^ (p1[1] > ylb))
330 <                        if (p0[0] > xlb && p1[0] > xlb)
329 >                if ((p0[1]<<1 > ylb) ^ (p1[1]<<1 > ylb)) {
330 >                        tv = (p0[0]<<1 > xlb) | ((p1[0]<<1 > xlb) << 1);
331 >                        if (tv == 03)
332                                  ncross++;
333 <                        else if (p0[0] > xlb || p1[0] > xlb)
333 >                        else if (tv)
334                                  ncross += (p1[1] > p0[1]) ^
335                                                  ((p0[1]-y)*(p1[0]-x) >
336                                                  (p0[0]-x)*(p1[1]-y));
337 +                }
338                  p0 = p1;
339                  p1 += 2;
340          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines