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.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"
17 +
18   /*
19   *      A text pattern is specified as the text (a file or line),
20   *  the upper left anchor point, the right motion vector, the down
# Line 23 | 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 < extern char  *libpath;                  /* library search path */
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  
73 < typedef unsigned char  GLYPH;
73 > #define  TLSTR(l)       ((char *)((l)+1))
74  
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
75   typedef struct {
69        char  **t;                      /* text array */
76          FVECT  right, down;             /* right and down unit vectors */
77          FONT  *f;                       /* our font */
78 +        TLINE  tl;                      /* line list */
79   }  TEXT;
80  
74 extern char  *fgetword();
75
81   TEXT  *gettext();
82  
83 < FONT  *getfont();
83 > TLINE  *tlalloc();
84  
80 static FONT  *fontlist = NULL;          /* our font list */
85  
86 <
83 < text(m, r)
86 > do_text(m, r)
87   register OBJREC  *m;
88   RAY  *r;
89   {
90 <        double  v[3];
90 >        FVECT  v;
91          int  foreground;
92                                  /* get transformed position */
93          if (r->rox != NULL)
# Line 99 | 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 121 | Line 128 | RAY  *r;
128                                          m->oargs.farg[14]);
129                  multcolor(r->pcol, cval);
130          }
131 +        return(0);
132   }
133  
134  
135 + TLINE *
136 + tlalloc(s)                      /* allocate and assign text line */
137 + char  *s;
138 + {
139 +        register int  siz;
140 +        register TLINE  *tl;
141 +
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);
148 +        return(tl);
149 + }
150 +
151 +
152   TEXT *
153   gettext(tm)                     /* get text structure for material */
154   register OBJREC  *tm;
155   {
156   #define  R      (tm->oargs.farg+3)
157   #define  D      (tm->oargs.farg+6)
133        extern char  *strcpy(), *fgets();
158          FVECT  DxR;
159          double  d;
160          FILE  *fp;
161          char  linbuf[512];
162 <        register TEXT  *t;
162 >        TEXT  *t;
163          register int  i;
164 +        register TLINE  *tlp;
165          register char  *s;
166  
167          if ((t = (TEXT *)tm->os) != NULL)
168                  return(t);
169                                                  /* check arguments */
170 <        if (tm->oargs.nsargs - tndx(tm) < 1 ||
146 <                        tm->oargs.nfargs != (tm->otype == PAT_BTEXT ? 11 :
147 <                                        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 <                goto memerr;
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);
184 <        d = DOT(R,R) / DOT(t->down,t->down);
184 >        d = DOT(R,R)/DOT(t->down,t->down);
185          for (i = 0; i < 3; i++)
186                  t->down[i] *= d;
187                                                  /* get text */
188 <        t->t = (char **)malloc(2*sizeof(char **));
163 <        if (t->t == NULL)
164 <                goto memerr;
188 >        tlp = &t->tl;
189          if (tm->oargs.nsargs - tndx(tm) > 1) {  /* single line */
190                  s = linbuf;
191                  for (i = tndx(tm)+1; i < tm->oargs.nsargs; i++) {
# Line 170 | Line 194 | register OBJREC  *tm;
194                          *s++ = ' ';
195                  }
196                  *--s = '\0';
197 <                t->t[0] = savqstr(linbuf);
198 <                t->t[1] = NULL;
197 >                tlp->next = tlalloc(linbuf);
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\"",
184 <                                        s);
207 >                        sprintf(errmsg, "cannot open text file \"%s\"", s);
208                          error(SYSTEM, errmsg);
209                  }
210 <                for (i=0; fgets(linbuf,sizeof(linbuf),fp)!=NULL; i++) {
210 >                while (fgets(linbuf, sizeof(linbuf), fp) != NULL) {
211                          s = linbuf + strlen(linbuf) - 1;
212                          if (*s == '\n')
213                                  *s = '\0';
214 <                        t->t=(char **)realloc((char *)t->t,
215 <                                        (i+2)*sizeof(char **));
193 <                        if (t->t == NULL)
194 <                                goto memerr;
195 <                        t->t[i] = savqstr(linbuf);
214 >                        tlp->next = tlalloc(linbuf);
215 >                        tlp = tlp->next;
216                  }
197                t->t[i] = NULL;
217                  fclose(fp);
218          }
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);
205 memerr:
206        error(SYSTEM, "out of memory in gettext");
240   #undef  R
241   #undef  D
242   }
# Line 213 | Line 246 | freetext(m)                    /* free text structures associated with
246   OBJREC  *m;
247   {
248          register TEXT  *tp;
249 <        register int  i;
249 >        register TLINE  *tlp;
250  
251          tp = (TEXT *)m->os;
252          if (tp == NULL)
253                  return;
254 <        for (i = 0; tp->t[i] != NULL; i++)
255 <                freeqstr(tp->t[i]);
256 <        free((char *)tp->t);
257 <        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  
264  
265   intext(p, m)                    /* check to see if p is in text glyph */
266   FVECT  p;
267 < register OBJREC  *m;
267 > OBJREC  *m;
268   {
269          register TEXT  *tp;
270 <        register int  i;
271 <        double  v[3], y, x;
272 <        int  col, lno;
270 >        register TLINE  *tlp;
271 >        FVECT  v;
272 >        double  y, x;
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 */
292 <        tp = gettext(m);
293 <        for (i = 0; i < lno; i++)
294 <                if (tp->t[i] == NULL)
295 <                        return(0);
252 <        if (col >= strlen(tp->t[i]))
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 (--i < 0)
294 >                        break;
295 >        if (tlp == NULL || h >= tlp->width)
296                  return(0);
297 <        return(inglyph(x, y, tp->f->fg[tp->t[i][col]]));
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  
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
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 <        register GLYPH  *p0, *p1;
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;
320 <        n = *gl++;                      /* get # of vertices */
321 <        p0 = gl + 2*(n-1);              /* connect last to first */
322 <        p1 = gl;
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