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.4 by greg, Sat Dec 15 15:03:40 1990 UTC vs.
Revision 2.23 by schorsch, Sun Jul 27 22:12:03 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1990 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  
73 < typedef struct font {
74 <        GLYPH  *fg[256];                /* font glyphs */
75 <        char  *name;                    /* font file name */
76 <        struct font  *next;             /* next font in list */
77 < }  FONT;
73 > typedef struct {
74 >        FVECT  right, down;             /* right and down unit vectors */
75 >        FONT  *f;                       /* our font */
76 >        TLINE  tl;                      /* line list */
77 > }  TEXT;
78  
79 < extern GLYPH  *getglyph();
79 > TEXT  *gettext();
80  
81 < extern FONT  *getfont();
81 > TLINE  *tlalloc();
82  
72 static FONT  *fontlist = NULL;          /* our font list */
83  
84 <
75 < text(m, r)
84 > do_text(m, r)
85   register OBJREC  *m;
86   RAY  *r;
87   {
88 <        double  v[3], y, x;
80 <        int  col, lno;
88 >        FVECT  v;
89          int  foreground;
90 <        GLYPH  *g;
83 <        register double  *ap;
84 <
85 <        if (m->oargs.nsargs - tndx(m) < 1 ||
86 <                        m->oargs.nfargs != (m->otype == PAT_BTEXT ? 11 :
87 <                                        m->otype == PAT_CTEXT ? 15 : 9))
88 <                objerror(m, USER, "bad # arguments");
89 <
90 <                                /* first, discover position in text */
91 <        ap = m->oargs.farg;
90 >                                /* get transformed position */
91          if (r->rox != NULL)
92                  multp3(v, r->rop, r->rox->b.xfm);
93          else
94                  VCOPY(v, r->rop);
95 <        v[0] -= ap[0];
96 <        v[1] -= ap[1];
98 <        v[2] -= ap[2];
99 <        col = x = DOT(v, ap+3) / DOT(ap+3, ap+3);
100 <        lno = y = DOT(v, ap+6) / DOT(ap+6, ap+6);
101 <        x -= col;
102 <        y = (lno+1) - y;
103 <                                /* get the font character, check it */
104 <        if ((g = getglyph(m, lno, col)) == NULL)
105 <                foreground = 0;
106 <        else
107 <                foreground = inglyph(x, y, g);
95 >                                /* check if we are within a text glyph */
96 >        foreground = intext(v, m);
97                                  /* modify */
98          if (m->otype == MIX_TEXT) {
99                  OBJECT  omod;
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 <                raymixture(r, omod, OVOID, 1.0);
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, ap[9]);
114 >                        scalecolor(r->pcol, m->oargs.farg[9]);
115                  else
116 <                        scalecolor(r->pcol, ap[10]);
116 >                        scalecolor(r->pcol, m->oargs.farg[10]);
117          } else { /* PAT_CTEXT */
118                  COLOR  cval;
119                  if (foreground)
120 <                        setcolor(cval, ap[9], ap[10], ap[11]);
120 >                        setcolor(cval, m->oargs.farg[9],
121 >                                        m->oargs.farg[10],
122 >                                        m->oargs.farg[11]);
123                  else
124 <                        setcolor(cval, ap[12], ap[13], ap[14]);
124 >                        setcolor(cval, m->oargs.farg[12],
125 >                                        m->oargs.farg[13],
126 >                                        m->oargs.farg[14]);
127                  multcolor(r->pcol, cval);
128          }
129 +        return(0);
130   }
131  
132  
133 < GLYPH *
134 < getglyph(tm, lno, col)          /* get a glyph from a text description */
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;
138 int  lno;
139 int  col;
153   {
154 <        extern char  *strcpy(), *fgets();
154 > #define  R      (tm->oargs.farg+3)
155 > #define  D      (tm->oargs.farg+6)
156 >        FVECT  DxR;
157 >        double  d;
158          FILE  *fp;
159          char  linbuf[512];
160 +        TEXT  *t;
161          register int  i;
162 <        register char  **txt;
162 >        register TLINE  *tlp;
163          register char  *s;
164  
165 <        if (lno < 0 || col < 0)
166 <                return(NULL);
167 <        if (tm->os == NULL) {
168 <                txt = (char **)malloc(2*sizeof(char **));
169 <                if (txt == NULL)
170 <                        goto memerr;
171 <                if (tm->oargs.nsargs - tndx(tm) > 1) {  /* single line */
172 <                        s = linbuf;
173 <                        for (i = tndx(tm)+1; i < tm->oargs.nsargs; i++) {
174 <                                strcpy(s, tm->oargs.sarg[i]);
175 <                                s += strlen(s);
176 <                                *s++ = ' ';
177 <                        }
178 <                        *--s = '\0';
179 <                        txt[0] = savqstr(linbuf);
180 <                        txt[1] = NULL;
181 <                } else {                                /* text file */
182 <                        if ((s = getpath(tm->oargs.sarg[tndx(tm)],
183 <                                        libpath, R_OK)) == NULL) {
184 <                                sprintf(errmsg, "cannot find text file \"%s\"",
185 <                                                tm->oargs.sarg[tndx(tm)]);
186 <                                error(USER, errmsg);
187 <                        }
188 <                        if ((fp = fopen(s, "r")) == NULL) {
189 <                                sprintf(errmsg, "cannot open text file \"%s\"",
190 <                                                s);
191 <                                error(SYSTEM, errmsg);
192 <                        }
176 <                        for (i=0; fgets(linbuf,sizeof(linbuf),fp)!=NULL; i++) {
177 <                                s = linbuf + strlen(linbuf) - 1;
178 <                                if (*s == '\n')
179 <                                        *s = '\0';
180 <                                txt=(char **)realloc(txt,(i+2)*sizeof(char **));
181 <                                if (txt == NULL)
182 <                                        goto memerr;
183 <                                txt[i] = savqstr(linbuf);
184 <                        }
185 <                        txt[i] = NULL;
186 <                        fclose(fp);
165 >        if ((t = (TEXT *)tm->os) != NULL)
166 >                return(t);
167 >                                                /* check arguments */
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(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);
183 >        for (i = 0; i < 3; i++)
184 >                t->down[i] *= d;
185 >                                                /* get text */
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++) {
190 >                        strcpy(s, tm->oargs.sarg[i]);
191 >                        s += strlen(s);
192 >                        *s++ = ' ';
193                  }
194 <                tm->os = (char *)txt;
194 >                *--s = '\0';
195 >                tlp->next = tlalloc(linbuf);
196 >                tlp = tlp->next;
197 >        } else {                                /* text file */
198 >                if ((s = getpath(tm->oargs.sarg[tndx(tm)],
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\"", s);
206 >                        error(SYSTEM, errmsg);
207 >                }
208 >                while (fgets(linbuf, sizeof(linbuf), fp) != NULL) {
209 >                        s = linbuf + strlen(linbuf) - 1;
210 >                        if (*s == '\n')
211 >                                *s = '\0';
212 >                        tlp->next = tlalloc(linbuf);
213 >                        tlp = tlp->next;
214 >                }
215 >                fclose(fp);
216          }
217 <        txt = (char **)tm->os;
218 <        for (i = 0; i < lno; i++)
219 <                if (txt[i] == NULL)
220 <                        break;
221 <        if ((s = txt[i]) == NULL || col >= strlen(s))
222 <                return(NULL);
223 <        else
224 <                return(getfont(tm->oargs.sarg[fndx(tm)])->fg[s[col]]);
225 < memerr:
226 <        error(SYSTEM, "out of memory in getglyph");
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);
238 > #undef  R
239 > #undef  D
240   }
241  
242  
243 < FONT *
244 < getfont(fname)                          /* return font fname */
205 < char  *fname;
243 > freetext(m)                     /* free text structures associated with m */
244 > OBJREC  *m;
245   {
246 <        FILE  *fp;
247 <        char  *pathname, *err;
209 <        int  gn, ngv, gv;
210 <        register GLYPH  *g;
211 <        register FONT  *f;
246 >        register TEXT  *tp;
247 >        register TLINE  *tlp;
248  
249 <        for (f = fontlist; f != NULL; f = f->next)
250 <                if (!strcmp(f->name, fname))
251 <                        return(f);
252 <                                                /* load the font file */
253 <        if ((pathname = getpath(fname, libpath, R_OK)) == NULL) {
254 <                sprintf(errmsg, "cannot find font file \"%s\"", fname);
255 <                error(USER, errmsg);
249 >        tp = (TEXT *)m->os;
250 >        if (tp == NULL)
251 >                return;
252 >        while ((tlp = tp->tl.next) != NULL) {
253 >                tp->tl.next = tlp->next;
254 >                free((void *)tlp->spc);
255 >                free((void *)tlp);
256          }
257 <        f = (FONT *)calloc(1, sizeof(FONT));
258 <        if (f == NULL)
259 <                goto memerr;
224 <        f->name = savestr(fname);
225 <        if ((fp = fopen(pathname, "r")) == NULL) {
226 <                sprintf(errmsg, "cannot open font file \"%s\"",
227 <                                pathname);
228 <                error(SYSTEM, errmsg);
229 <        }
230 <        while (fscanf(fp, "%d", &gn) == 1) {    /* get each glyph */
231 <                if (gn < 0 || gn > 255) {
232 <                        err = "illegal";
233 <                        goto fonterr;
234 <                }
235 <                if (f->fg[gn] != NULL) {
236 <                        err = "duplicate";
237 <                        goto fonterr;
238 <                }
239 <                if (fscanf(fp, "%d", &ngv) != 1 ||
240 <                                ngv < 0 || ngv > 255) {
241 <                        err = "bad # vertices for";
242 <                        goto fonterr;
243 <                }
244 <                g = (GLYPH *)malloc((2*ngv+1)*sizeof(GLYPH));
245 <                if (g == NULL)
246 <                        goto memerr;
247 <                f->fg[gn] = g;
248 <                *g++ = ngv;
249 <                ngv *= 2;
250 <                while (ngv--) {
251 <                        if (fscanf(fp, "%d", &gv) != 1 ||
252 <                                        gv < 0 || gv > 255) {
253 <                                err = "bad vertex for";
254 <                                goto fonterr;
255 <                        }
256 <                        *g++ = gv;
257 <                }
258 <        }
259 <        fclose(fp);
260 <        f->next = fontlist;
261 <        return(fontlist = f);
262 < fonterr:
263 <        sprintf(errmsg, "%s character (%d) in font file \"%s\"",
264 <                        err, gn, pathname);
265 <        error(USER, errmsg);
266 < memerr:
267 <        error(SYSTEM, "out of memory in fontglyph");
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 + OBJREC  *m;
266 + {
267 +        register TEXT  *tp;
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 +        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)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 +        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 +
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 (x < 0.0 || y < 0.0)
312 >        if (gl == NULL)
313                  return(0);
314 <        xlb = x *= 255.0;               /* get glyph coordinates */
315 <        ylb = y *= 255.0;
316 <        n = *gl++;                      /* get # of vertices */
317 <        p0 = gl + 2*(n-1);              /* connect last to first */
318 <        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