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.20 by greg, Sat Feb 22 02:07:29 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.
6 + */
7 +
8 + /* ====================================================================
9 + * The Radiance Software License, Version 1.0
10   *
11 < *     11/12/86
11 > * Copyright (c) 1990 - 2002 The Regents of the University of California,
12 > * through Lawrence Berkeley National Laboratory.   All rights reserved.
13 > *
14 > * Redistribution and use in source and binary forms, with or without
15 > * modification, are permitted provided that the following conditions
16 > * are met:
17 > *
18 > * 1. Redistributions of source code must retain the above copyright
19 > *         notice, this list of conditions and the following disclaimer.
20 > *
21 > * 2. Redistributions in binary form must reproduce the above copyright
22 > *       notice, this list of conditions and the following disclaimer in
23 > *       the documentation and/or other materials provided with the
24 > *       distribution.
25 > *
26 > * 3. The end-user documentation included with the redistribution,
27 > *           if any, must include the following acknowledgment:
28 > *             "This product includes Radiance software
29 > *                 (http://radsite.lbl.gov/)
30 > *                 developed by the Lawrence Berkeley National Laboratory
31 > *               (http://www.lbl.gov/)."
32 > *       Alternately, this acknowledgment may appear in the software itself,
33 > *       if and wherever such third-party acknowledgments normally appear.
34 > *
35 > * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36 > *       and "The Regents of the University of California" must
37 > *       not be used to endorse or promote products derived from this
38 > *       software without prior written permission. For written
39 > *       permission, please contact [email protected].
40 > *
41 > * 5. Products derived from this software may not be called "Radiance",
42 > *       nor may "Radiance" appear in their name, without prior written
43 > *       permission of Lawrence Berkeley National Laboratory.
44 > *
45 > * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 > * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 > * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 > * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49 > * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 > * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 > * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 > * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 > * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 > * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 > * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 > * SUCH DAMAGE.
57 > * ====================================================================
58 > *
59 > * This software consists of voluntary contributions made by many
60 > * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
61 > * information on Lawrence Berkeley National Laboratory, please see
62 > * <http://www.lbl.gov/>.
63   */
64  
65   #include  "ray.h"
66  
67   #include  "otypes.h"
68  
69 + #include  "font.h"
70 +
71   /*
72   *      A text pattern is specified as the text (a file or line),
73   *  the upper left anchor point, the right motion vector, the down
# Line 23 | Line 77 | static char SCCSid[] = "$SunId$ LBL";
77   *      modifier brighttext id
78   *      2 fontfile textfile
79   *      0
80 < *      11
80 > *      11+
81   *              Ax Ay Az
82   *              Rx Ry Rz
83   *              Dx Dy Dz
84   *              foreground background
85 + *              [spacing]
86   *
87   *  For a single line, we use:
88   *
89   *      modifier brighttext id
90   *      N+2 fontfile . This is a line with N words...
91   *      0
92 < *      11
92 > *      11+
93   *              Ax Ay Az
94   *              Rx Ry Rz
95   *              Dx Dy Dz
96   *              foreground background
97 + *              [spacing]
98   *
99   *  Colortext is identical, except colors are given rather than
100 < *  brightnesses.  Mixtext has foreground and background modifiers:
100 > *  brightnesses.
101   *
102 + *  Mixtext has foreground and background modifiers:
103 + *
104   *      modifier mixtext id
105   *      4+ foremod backmod fontfile text..
106   *      0
107 < *      9
107 > *      9+
108   *              Ax Ay Az
109   *              Rx Ry Rz
110   *              Dx Dy Dz
111 + *              [spacing]
112   */
113  
114   #define  fndx(m)        ((m)->otype==MIX_TEXT ? 2 : 0)
115   #define  tndx(m)        ((m)->otype==MIX_TEXT ? 3 : 1)
116 + #define  sndx(m)        ((m)->otype==PAT_BTEXT ? 11 : \
117 +                                (m)->otype==PAT_CTEXT ? 15 : 9)
118  
119 < extern char  *libpath;                  /* library search path */
119 > typedef struct tline {
120 >        struct tline  *next;            /* pointer to next line */
121 >        short  *spc;                    /* character spacing */
122 >        int  width;                     /* total line width */
123 >                                        /* followed by the string */
124 > }  TLINE;
125  
126 < typedef unsigned char  GLYPH;
126 > #define  TLSTR(l)       ((char *)((l)+1))
127  
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
128   typedef struct {
69        char  **t;                      /* text array */
129          FVECT  right, down;             /* right and down unit vectors */
130          FONT  *f;                       /* our font */
131 +        TLINE  tl;                      /* line list */
132   }  TEXT;
133  
74 extern char  *fgetword();
75
134   TEXT  *gettext();
135  
136 < FONT  *getfont();
136 > TLINE  *tlalloc();
137  
80 static FONT  *fontlist = NULL;          /* our font list */
138  
139 <
83 < text(m, r)
139 > do_text(m, r)
140   register OBJREC  *m;
141   RAY  *r;
142   {
143 <        double  v[3];
143 >        FVECT  v;
144          int  foreground;
145                                  /* get transformed position */
146          if (r->rox != NULL)
# Line 99 | Line 155 | RAY  *r;
155                  char  *modname = m->oargs.sarg[foreground ? 0 : 1];
156                  if (!strcmp(modname, VOIDID))
157                          omod = OVOID;
158 <                else if ((omod = modifier(modname)) == OVOID) {
158 >                else if ((omod = lastmod(objndx(m), modname)) == OVOID) {
159                          sprintf(errmsg, "undefined modifier \"%s\"", modname);
160                          objerror(m, USER, errmsg);
161                  }
162 <                raytexture(r, omod);
162 >                if (rayshade(r, omod)) {
163 >                        if (m->omod != OVOID)
164 >                                objerror(m, USER, "inappropriate modifier");
165 >                        return(1);
166 >                }
167          } else if (m->otype == PAT_BTEXT) {
168                  if (foreground)
169                          scalecolor(r->pcol, m->oargs.farg[9]);
# Line 121 | Line 181 | RAY  *r;
181                                          m->oargs.farg[14]);
182                  multcolor(r->pcol, cval);
183          }
184 +        return(0);
185   }
186  
187  
188 + TLINE *
189 + tlalloc(s)                      /* allocate and assign text line */
190 + char  *s;
191 + {
192 +        register int  siz;
193 +        register TLINE  *tl;
194 +
195 +        siz = strlen(s) + 1;
196 +        if ((tl=(TLINE *)malloc(sizeof(TLINE)+siz)) == NULL ||
197 +                        (tl->spc=(short *)malloc(siz*sizeof(short))) == NULL)
198 +                error(SYSTEM, "out of memory in tlalloc");
199 +        tl->next = NULL;
200 +        strcpy(TLSTR(tl), s);
201 +        return(tl);
202 + }
203 +
204 +
205   TEXT *
206   gettext(tm)                     /* get text structure for material */
207   register OBJREC  *tm;
208   {
209   #define  R      (tm->oargs.farg+3)
210   #define  D      (tm->oargs.farg+6)
133        extern char  *strcpy(), *fgets();
211          FVECT  DxR;
212          double  d;
213          FILE  *fp;
214          char  linbuf[512];
215 <        register TEXT  *t;
215 >        TEXT  *t;
216          register int  i;
217 +        register TLINE  *tlp;
218          register char  *s;
219  
220          if ((t = (TEXT *)tm->os) != NULL)
221                  return(t);
222                                                  /* check arguments */
223 <        if (tm->oargs.nsargs - tndx(tm) < 1 ||
146 <                        tm->oargs.nfargs != (tm->otype == PAT_BTEXT ? 11 :
147 <                                        tm->otype == PAT_CTEXT ? 15 : 9))
223 >        if (tm->oargs.nsargs - tndx(tm) < 1 || tm->oargs.nfargs < sndx(tm))
224                  objerror(tm, USER, "bad # arguments");
225          if ((t = (TEXT *)malloc(sizeof(TEXT))) == NULL)
226 <                goto memerr;
226 >                error(SYSTEM, "out of memory in gettext");
227                                                  /* compute vectors */
228          fcross(DxR, D, R);
229          fcross(t->right, DxR, D);
230 <        d = DOT(D,D) / DOT(t->right,t->right);
230 >        d = DOT(t->right,t->right);
231 >        if (d <= FTINY*FTINY*FTINY*FTINY)
232 >                objerror(tm, USER, "illegal motion vector");
233 >        d = DOT(D,D)/d;
234          for (i = 0; i < 3; i++)
235                  t->right[i] *= d;
236          fcross(t->down, R, DxR);
237 <        d = DOT(R,R) / DOT(t->down,t->down);
237 >        d = DOT(R,R)/DOT(t->down,t->down);
238          for (i = 0; i < 3; i++)
239                  t->down[i] *= d;
240                                                  /* get text */
241 <        t->t = (char **)malloc(2*sizeof(char **));
163 <        if (t->t == NULL)
164 <                goto memerr;
241 >        tlp = &t->tl;
242          if (tm->oargs.nsargs - tndx(tm) > 1) {  /* single line */
243                  s = linbuf;
244                  for (i = tndx(tm)+1; i < tm->oargs.nsargs; i++) {
# Line 170 | Line 247 | register OBJREC  *tm;
247                          *s++ = ' ';
248                  }
249                  *--s = '\0';
250 <                t->t[0] = savqstr(linbuf);
251 <                t->t[1] = NULL;
250 >                tlp->next = tlalloc(linbuf);
251 >                tlp = tlp->next;
252          } else {                                /* text file */
253                  if ((s = getpath(tm->oargs.sarg[tndx(tm)],
254 <                                libpath, R_OK)) == NULL) {
254 >                                getlibpath(), R_OK)) == NULL) {
255                          sprintf(errmsg, "cannot find text file \"%s\"",
256                                          tm->oargs.sarg[tndx(tm)]);
257                          error(USER, errmsg);
258                  }
259                  if ((fp = fopen(s, "r")) == NULL) {
260 <                        sprintf(errmsg, "cannot open text file \"%s\"",
184 <                                        s);
260 >                        sprintf(errmsg, "cannot open text file \"%s\"", s);
261                          error(SYSTEM, errmsg);
262                  }
263 <                for (i=0; fgets(linbuf,sizeof(linbuf),fp)!=NULL; i++) {
263 >                while (fgets(linbuf, sizeof(linbuf), fp) != NULL) {
264                          s = linbuf + strlen(linbuf) - 1;
265                          if (*s == '\n')
266                                  *s = '\0';
267 <                        t->t=(char **)realloc((char *)t->t,
268 <                                        (i+2)*sizeof(char **));
193 <                        if (t->t == NULL)
194 <                                goto memerr;
195 <                        t->t[i] = savqstr(linbuf);
267 >                        tlp->next = tlalloc(linbuf);
268 >                        tlp = tlp->next;
269                  }
197                t->t[i] = NULL;
270                  fclose(fp);
271          }
272 +        tlp->next = NULL;
273                                                  /* get the font */
274          t->f = getfont(tm->oargs.sarg[fndx(tm)]);
275 +                                                /* compute character spacing */
276 +        i = sndx(tm);
277 +        d = i < tm->oargs.nfargs ? tm->oargs.farg[i] : 0.0;
278 +        i = d * 255.0;
279 +        t->tl.width = 0;
280 +        for (tlp = t->tl.next; tlp != NULL; tlp = tlp->next) {
281 +                if (i < 0)
282 +                        tlp->width = squeeztext(tlp->spc, TLSTR(tlp), t->f, -i);
283 +                else if (i > 0)
284 +                        tlp->width = proptext(tlp->spc, TLSTR(tlp), t->f, i, 3);
285 +                else
286 +                        tlp->width = uniftext(tlp->spc, TLSTR(tlp), t->f);
287 +                if (tlp->width > t->tl.width)
288 +                        t->tl.width = tlp->width;
289 +        }
290                                                  /* we're done */
291          tm->os = (char *)t;
292          return(t);
205 memerr:
206        error(SYSTEM, "out of memory in gettext");
293   #undef  R
294   #undef  D
295   }
# Line 213 | Line 299 | freetext(m)                    /* free text structures associated with
299   OBJREC  *m;
300   {
301          register TEXT  *tp;
302 <        register int  i;
302 >        register TLINE  *tlp;
303  
304          tp = (TEXT *)m->os;
305          if (tp == NULL)
306                  return;
307 <        for (i = 0; tp->t[i] != NULL; i++)
308 <                freeqstr(tp->t[i]);
309 <        free((char *)tp->t);
310 <        free((char *)tp);
307 >        while ((tlp = tp->tl.next) != NULL) {
308 >                tp->tl.next = tlp->next;
309 >                free((void *)tlp->spc);
310 >                free((void *)tlp);
311 >        }
312 >        freefont(tp->f);        /* release font reference */
313 >        free((void *)tp);
314          m->os = NULL;
315   }
316  
317  
318   intext(p, m)                    /* check to see if p is in text glyph */
319   FVECT  p;
320 < register OBJREC  *m;
320 > OBJREC  *m;
321   {
322          register TEXT  *tp;
323 <        register int  i;
324 <        double  v[3], y, x;
325 <        int  col, lno;
323 >        register TLINE  *tlp;
324 >        FVECT  v;
325 >        double  y, x;
326 >        register int  i, h;
327                                  /* first, compute position in text */
328 +        tp = gettext(m);
329          v[0] = p[0] - m->oargs.farg[0];
330          v[1] = p[1] - m->oargs.farg[1];
331          v[2] = p[2] - m->oargs.farg[2];
332 <        col = x = DOT(v, tp->right);
333 <        lno = y = DOT(v, tp->down);
332 >        x = DOT(v, tp->right);
333 >        i = sndx(m);
334 >        if (i < m->oargs.nfargs)
335 >                x *= tp->f->mwidth + 255.*fabs(m->oargs.farg[i]);
336 >        else
337 >                x *= 255.;
338 >        h = x;
339 >        i = y = DOT(v, tp->down);
340          if (x < 0.0 || y < 0.0)
341                  return(0);
342 <        x -= (double)col;
343 <        y = (lno+1) - y;
344 <                                /* get the font character */
345 <        tp = gettext(m);
346 <        for (i = 0; i < lno; i++)
347 <                if (tp->t[i] == NULL)
348 <                        return(0);
252 <        if (col >= strlen(tp->t[i]))
342 >        x -= (double)h;
343 >        y = ((i+1) - y)*255.;
344 >                                /* find the line position */
345 >        for (tlp = tp->tl.next; tlp != NULL; tlp = tlp->next)
346 >                if (--i < 0)
347 >                        break;
348 >        if (tlp == NULL || h >= tlp->width)
349                  return(0);
350 <        return(inglyph(x, y, tp->f->fg[tp->t[i][col]]));
350 >        for (i = 0; (h -= tlp->spc[i]) >= 0; i++)
351 >                if (h < 255 && inglyph(h+x, y,
352 >                                tp->f->fg[TLSTR(tlp)[i]&0xff]))
353 >                        return(1);
354 >        return(0);
355   }
356  
357  
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
358   inglyph(x, y, gl)               /* (x,y) within font glyph gl? */
359 < double  x, y;
360 < GLYPH  *gl;
359 > double  x, y;           /* real coordinates in range [0,255) */
360 > register GLYPH  *gl;
361   {
362          int  n, ncross;
363          int  xlb, ylb;
364 <        register GLYPH  *p0, *p1;
364 >        int  tv;
365 >        register GORD  *p0, *p1;
366  
367          if (gl == NULL)
368                  return(0);
369 <        x *= 256.0;                     /* get glyph coordinates */
370 <        y *= 256.0;
371 <        xlb = x + 0.5;
372 <        ylb = y + 0.5;
373 <        n = *gl++;                      /* get # of vertices */
374 <        p0 = gl + 2*(n-1);              /* connect last to first */
375 <        p1 = gl;
369 >        xlb = x;
370 >        ylb = y;
371 >        if (gl->left > xlb || gl->right <= xlb ||       /* check extent */
372 >                        gl->bottom > ylb || gl->top <= ylb)
373 >                return(0);
374 >        xlb = xlb<<1 | 1;               /* add 1/2 to test points... */
375 >        ylb = ylb<<1 | 1;               /* ...so no equal comparisons */
376 >        n = gl->nverts;                 /* get # of vertices */
377 >        p0 = gvlist(gl) + 2*(n-1);      /* connect last to first */
378 >        p1 = gvlist(gl);
379          ncross = 0;
380                                          /* positive x axis cross test */
381          while (n--) {
382 <                if ((p0[1] > ylb) ^ (p1[1] > ylb))
383 <                        if (p0[0] > xlb && p1[0] > xlb)
382 >                if ((p0[1]<<1 > ylb) ^ (p1[1]<<1 > ylb)) {
383 >                        tv = p0[0]<<1 > xlb | (p1[0]<<1 > xlb) << 1;
384 >                        if (tv == 03)
385                                  ncross++;
386 <                        else if (p0[0] > xlb || p1[0] > xlb)
386 >                        else if (tv)
387                                  ncross += (p1[1] > p0[1]) ^
388                                                  ((p0[1]-y)*(p1[0]-x) >
389                                                  (p0[0]-x)*(p1[1]-y));
390 +                }
391                  p0 = p1;
392                  p1 += 2;
393          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines