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.9 by greg, Wed Aug 7 08:37:27 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  
58 extern char  *libpath;                  /* library search path */
59
60 typedef unsigned char  GLYPH;
61
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
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  
# Line 78 | Line 131 | typedef struct {
131          TLINE  tl;                      /* line list */
132   }  TEXT;
133  
81 extern char  *fgetword();
82
134   TEXT  *gettext();
135  
136   TLINE  *tlalloc();
137  
87 FONT  *getfont();
138  
139 < static FONT  *fontlist = NULL;          /* our font list */
90 <
91 <
92 < 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 108 | 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 130 | Line 181 | RAY  *r;
181                                          m->oargs.farg[14]);
182                  multcolor(r->pcol, cval);
183          }
184 +        return(0);
185   }
186  
187  
# Line 137 | Line 189 | TLINE *
189   tlalloc(s)                      /* allocate and assign text line */
190   char  *s;
191   {
192 <        extern char  *strcpy();
192 >        register int  siz;
193          register TLINE  *tl;
194  
195 <        tl = (TLINE *)malloc(sizeof(TLINE)+1+strlen(s));
196 <        if (tl == NULL)
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);
# Line 155 | Line 208 | register OBJREC  *tm;
208   {
209   #define  R      (tm->oargs.farg+3)
210   #define  D      (tm->oargs.farg+6)
158        extern char  *strcpy(), *fgets();
211          FVECT  DxR;
212          double  d;
213          FILE  *fp;
# Line 168 | Line 220 | register OBJREC  *tm;
220          if ((t = (TEXT *)tm->os) != NULL)
221                  return(t);
222                                                  /* check arguments */
223 <        if (tm->oargs.nsargs - tndx(tm) < 1 ||
172 <                        tm->oargs.nfargs != (tm->otype == PAT_BTEXT ? 11 :
173 <                                        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                  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);
# Line 198 | Line 251 | register OBJREC  *tm;
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\"",
208 <                                        s);
260 >                        sprintf(errmsg, "cannot open text file \"%s\"", s);
261                          error(SYSTEM, errmsg);
262                  }
263                  while (fgets(linbuf, sizeof(linbuf), fp) != NULL) {
# Line 220 | Line 272 | register OBJREC  *tm;
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);
# Line 237 | Line 304 | OBJREC  *m;
304          tp = (TEXT *)m->os;
305          if (tp == NULL)
306                  return;
307 <        for (tlp = tp->tl.next; tlp != NULL; tlp = tlp->next);
308 <                free((char *)tlp);
309 <        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  
# Line 250 | Line 321 | OBJREC  *m;
321   {
322          register TEXT  *tp;
323          register TLINE  *tlp;
324 <        double  v[3], y, x;
325 <        int  col;
326 <        register int  lno;
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 */
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 (--lno < 0)
346 >                if (--i < 0)
347                          break;
348 <        if (tlp == NULL || col >= strlen(TLSTR(tlp)))
348 >        if (tlp == NULL || h >= tlp->width)
349                  return(0);
350 <        return(inglyph(x, y, tp->f->fg[TLSTR(tlp)[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  
277 FONT *
278 getfont(fname)                          /* return font fname */
279 char  *fname;
280 {
281        char  buf[16];
282        FILE  *fp;
283        char  *pathname, *err;
284        int  gn, ngv, gv;
285        register GLYPH  *g;
286        register FONT  *f;
287
288        for (f = fontlist; f != NULL; f = f->next)
289                if (!strcmp(f->name, fname))
290                        return(f);
291                                                /* load the font file */
292        if ((pathname = getpath(fname, libpath, R_OK)) == NULL) {
293                sprintf(errmsg, "cannot find font file \"%s\"", fname);
294                error(USER, errmsg);
295        }
296        f = (FONT *)calloc(1, sizeof(FONT));
297        if (f == NULL)
298                goto memerr;
299        f->name = savestr(fname);
300        if ((fp = fopen(pathname, "r")) == NULL) {
301                sprintf(errmsg, "cannot open font file \"%s\"",
302                                pathname);
303                error(SYSTEM, errmsg);
304        }
305        while (fgetword(buf,sizeof(buf),fp) != NULL) {  /* get each glyph */
306                if (!isint(buf))
307                        goto nonint;
308                gn = atoi(buf);
309                if (gn < 0 || gn > 255) {
310                        err = "illegal";
311                        goto fonterr;
312                }
313                if (f->fg[gn] != NULL) {
314                        err = "duplicate";
315                        goto fonterr;
316                }
317                if (fgetword(buf,sizeof(buf),fp) == NULL || !isint(buf) ||
318                                (ngv = atoi(buf)) < 0 || ngv > 255) {
319                        err = "bad # vertices for";
320                        goto fonterr;
321                }
322                g = (GLYPH *)malloc((2*ngv+1)*sizeof(GLYPH));
323                if (g == NULL)
324                        goto memerr;
325                f->fg[gn] = g;
326                *g++ = ngv;
327                ngv *= 2;
328                while (ngv--) {
329                        if (fgetword(buf,sizeof(buf),fp) == NULL ||
330                                        !isint(buf) ||
331                                        (gv = atoi(buf)) < 0 || gv > 255) {
332                                err = "bad vertex for";
333                                goto fonterr;
334                        }
335                        *g++ = gv;
336                }
337        }
338        fclose(fp);
339        f->next = fontlist;
340        return(fontlist = f);
341 nonint:
342        sprintf(errmsg, "non-integer in font file \"%s\"", pathname);
343        error(USER, errmsg);
344 fonterr:
345        sprintf(errmsg, "%s character (%d) in font file \"%s\"",
346                        err, gn, pathname);
347        error(USER, errmsg);
348 memerr:
349        error(SYSTEM, "out of memory in fontglyph");
350 }
351
352
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