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.1 by greg, Thu Feb 2 10:41:47 1989 UTC vs.
Revision 2.24 by greg, Thu Feb 12 18:55:50 2004 UTC

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