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.6 by greg, Mon Jul 22 13:02:27 1991 UTC vs.
Revision 1.7 by greg, Mon Aug 5 09:32:12 1991 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 65 | Line 65 | typedef struct font {
65          struct font  *next;             /* next font in list */
66   }  FONT;
67  
68 + typedef struct {
69 +        char  **t;                      /* text array */
70 +        FVECT  right, down;             /* right and down unit vectors */
71 +        FONT  *f;                       /* our font */
72 + }  TEXT;
73 +
74   extern char  *fgetword();
75  
76 < extern GLYPH  *getglyph();
76 > TEXT  *gettext();
77  
78 < extern FONT  *getfont();
78 > FONT  *getfont();
79  
80   static FONT  *fontlist = NULL;          /* our font list */
81  
# Line 78 | Line 84 | text(m, r)
84   register OBJREC  *m;
85   RAY  *r;
86   {
87 <        double  v[3], y, x;
82 <        int  col, lno;
87 >        double  v[3];
88          int  foreground;
89 <        GLYPH  *g;
85 <        register double  *ap;
86 <
87 <        if (m->oargs.nsargs - tndx(m) < 1 ||
88 <                        m->oargs.nfargs != (m->otype == PAT_BTEXT ? 11 :
89 <                                        m->otype == PAT_CTEXT ? 15 : 9))
90 <                objerror(m, USER, "bad # arguments");
91 <
92 <                                /* first, discover position in text */
93 <        ap = m->oargs.farg;
89 >                                /* get transformed position */
90          if (r->rox != NULL)
91                  multp3(v, r->rop, r->rox->b.xfm);
92          else
93                  VCOPY(v, r->rop);
94 <        v[0] -= ap[0];
95 <        v[1] -= ap[1];
100 <        v[2] -= ap[2];
101 <        col = x = DOT(v, ap+3) / DOT(ap+3, ap+3);
102 <        lno = y = DOT(v, ap+6) / DOT(ap+6, ap+6);
103 <        x -= col;
104 <        y = (lno+1) - y;
105 <                                /* get the font character, check it */
106 <        if ((g = getglyph(m, lno, col)) == NULL)
107 <                foreground = 0;
108 <        else
109 <                foreground = inglyph(x, y, g);
94 >                                /* check if we are within a text glyph */
95 >        foreground = intext(v, m);
96                                  /* modify */
97          if (m->otype == MIX_TEXT) {
98                  OBJECT  omod;
# Line 117 | Line 103 | RAY  *r;
103                          sprintf(errmsg, "undefined modifier \"%s\"", modname);
104                          objerror(m, USER, errmsg);
105                  }
106 <                raymixture(r, omod, OVOID, 1.0);
106 >                raytexture(r, omod);
107          } else if (m->otype == PAT_BTEXT) {
108                  if (foreground)
109 <                        scalecolor(r->pcol, ap[9]);
109 >                        scalecolor(r->pcol, m->oargs.farg[9]);
110                  else
111 <                        scalecolor(r->pcol, ap[10]);
111 >                        scalecolor(r->pcol, m->oargs.farg[10]);
112          } else { /* PAT_CTEXT */
113                  COLOR  cval;
114                  if (foreground)
115 <                        setcolor(cval, ap[9], ap[10], ap[11]);
115 >                        setcolor(cval, m->oargs.farg[9],
116 >                                        m->oargs.farg[10],
117 >                                        m->oargs.farg[11]);
118                  else
119 <                        setcolor(cval, ap[12], ap[13], ap[14]);
119 >                        setcolor(cval, m->oargs.farg[12],
120 >                                        m->oargs.farg[13],
121 >                                        m->oargs.farg[14]);
122                  multcolor(r->pcol, cval);
123          }
124   }
125  
126  
127 < GLYPH *
128 < getglyph(tm, lno, col)          /* get a glyph from a text description */
127 > TEXT *
128 > gettext(tm)                     /* get text structure for material */
129   register OBJREC  *tm;
140 int  lno;
141 int  col;
130   {
131 + #define  R      (tm->oargs.farg+3)
132 + #define  D      (tm->oargs.farg+6)
133          extern char  *strcpy(), *fgets();
134 +        FVECT  DxR;
135 +        double  d;
136          FILE  *fp;
137          char  linbuf[512];
138 +        register TEXT  *t;
139          register int  i;
147        register char  **txt;
140          register char  *s;
141  
142 <        if (lno < 0 || col < 0)
143 <                return(NULL);
144 <        if (tm->os == NULL) {
145 <                txt = (char **)malloc(2*sizeof(char **));
146 <                if (txt == NULL)
147 <                        goto memerr;
148 <                if (tm->oargs.nsargs - tndx(tm) > 1) {  /* single line */
149 <                        s = linbuf;
150 <                        for (i = tndx(tm)+1; i < tm->oargs.nsargs; i++) {
151 <                                strcpy(s, tm->oargs.sarg[i]);
152 <                                s += strlen(s);
153 <                                *s++ = ' ';
154 <                        }
155 <                        *--s = '\0';
156 <                        txt[0] = savqstr(linbuf);
157 <                        txt[1] = NULL;
158 <                } else {                                /* text file */
159 <                        if ((s = getpath(tm->oargs.sarg[tndx(tm)],
160 <                                        libpath, R_OK)) == NULL) {
161 <                                sprintf(errmsg, "cannot find text file \"%s\"",
162 <                                                tm->oargs.sarg[tndx(tm)]);
163 <                                error(USER, errmsg);
164 <                        }
165 <                        if ((fp = fopen(s, "r")) == NULL) {
166 <                                sprintf(errmsg, "cannot open text file \"%s\"",
167 <                                                s);
168 <                                error(SYSTEM, errmsg);
169 <                        }
170 <                        for (i=0; fgets(linbuf,sizeof(linbuf),fp)!=NULL; i++) {
179 <                                s = linbuf + strlen(linbuf) - 1;
180 <                                if (*s == '\n')
181 <                                        *s = '\0';
182 <                                txt=(char **)realloc(txt,(i+2)*sizeof(char **));
183 <                                if (txt == NULL)
184 <                                        goto memerr;
185 <                                txt[i] = savqstr(linbuf);
186 <                        }
187 <                        txt[i] = NULL;
188 <                        fclose(fp);
142 >        if ((t = (TEXT *)tm->os) != NULL)
143 >                return(t);
144 >                                                /* check arguments */
145 >        if (tm->oargs.nsargs - tndx(tm) < 1 ||
146 >                        tm->oargs.nfargs != (tm->otype == PAT_BTEXT ? 11 :
147 >                                        tm->otype == PAT_CTEXT ? 15 : 9))
148 >                objerror(tm, USER, "bad # arguments");
149 >        if ((t = (TEXT *)malloc(sizeof(TEXT))) == NULL)
150 >                goto memerr;
151 >                                                /* compute vectors */
152 >        fcross(DxR, D, R);
153 >        fcross(t->right, DxR, D);
154 >        d = DOT(D,D) / DOT(t->right,t->right);
155 >        for (i = 0; i < 3; i++)
156 >                t->right[i] *= d;
157 >        fcross(t->down, R, DxR);
158 >        d = DOT(R,R) / DOT(t->down,t->down);
159 >        for (i = 0; i < 3; i++)
160 >                t->down[i] *= d;
161 >                                                /* get text */
162 >        t->t = (char **)malloc(2*sizeof(char **));
163 >        if (t->t == NULL)
164 >                goto memerr;
165 >        if (tm->oargs.nsargs - tndx(tm) > 1) {  /* single line */
166 >                s = linbuf;
167 >                for (i = tndx(tm)+1; i < tm->oargs.nsargs; i++) {
168 >                        strcpy(s, tm->oargs.sarg[i]);
169 >                        s += strlen(s);
170 >                        *s++ = ' ';
171                  }
172 <                tm->os = (char *)txt;
172 >                *--s = '\0';
173 >                t->t[0] = savqstr(linbuf);
174 >                t->t[1] = NULL;
175 >        } else {                                /* text file */
176 >                if ((s = getpath(tm->oargs.sarg[tndx(tm)],
177 >                                libpath, R_OK)) == NULL) {
178 >                        sprintf(errmsg, "cannot find text file \"%s\"",
179 >                                        tm->oargs.sarg[tndx(tm)]);
180 >                        error(USER, errmsg);
181 >                }
182 >                if ((fp = fopen(s, "r")) == NULL) {
183 >                        sprintf(errmsg, "cannot open text file \"%s\"",
184 >                                        s);
185 >                        error(SYSTEM, errmsg);
186 >                }
187 >                for (i=0; fgets(linbuf,sizeof(linbuf),fp)!=NULL; i++) {
188 >                        s = linbuf + strlen(linbuf) - 1;
189 >                        if (*s == '\n')
190 >                                *s = '\0';
191 >                        t->t=(char **)realloc((char *)t->t,
192 >                                        (i+2)*sizeof(char **));
193 >                        if (t->t == NULL)
194 >                                goto memerr;
195 >                        t->t[i] = savqstr(linbuf);
196 >                }
197 >                t->t[i] = NULL;
198 >                fclose(fp);
199          }
200 <        txt = (char **)tm->os;
201 <        for (i = 0; i < lno; i++)
202 <                if (txt[i] == NULL)
203 <                        break;
204 <        if ((s = txt[i]) == NULL || col >= strlen(s))
197 <                return(NULL);
198 <        else
199 <                return(getfont(tm->oargs.sarg[fndx(tm)])->fg[s[col]]);
200 >                                                /* get the font */
201 >        t->f = getfont(tm->oargs.sarg[fndx(tm)]);
202 >                                                /* we're done */
203 >        tm->os = (char *)t;
204 >        return(t);
205   memerr:
206 <        error(SYSTEM, "out of memory in getglyph");
206 >        error(SYSTEM, "out of memory in gettext");
207 > #undef  R
208 > #undef  D
209   }
210  
211  
212 + freetext(m)                     /* free text structures associated with m */
213 + OBJREC  *m;
214 + {
215 +        register TEXT  *tp;
216 +        register int  i;
217 +
218 +        tp = (TEXT *)m->os;
219 +        if (tp == NULL)
220 +                return;
221 +        for (i = 0; tp->t[i] != NULL; i++)
222 +                freeqstr(tp->t[i]);
223 +        free((char *)tp->t);
224 +        free((char *)tp);
225 +        m->os = NULL;
226 + }
227 +
228 +
229 + intext(p, m)                    /* check to see if p is in text glyph */
230 + FVECT  p;
231 + register OBJREC  *m;
232 + {
233 +        register TEXT  *tp;
234 +        register int  i;
235 +        double  v[3], y, x;
236 +        int  col, lno;
237 +                                /* first, compute position in text */
238 +        v[0] = p[0] - m->oargs.farg[0];
239 +        v[1] = p[1] - m->oargs.farg[1];
240 +        v[2] = p[2] - m->oargs.farg[2];
241 +        col = x = DOT(v, tp->right);
242 +        lno = y = DOT(v, tp->down);
243 +        if (x < 0.0 || y < 0.0)
244 +                return(0);
245 +        x -= (double)col;
246 +        y = (lno+1) - y;
247 +                                /* get the font character */
248 +        tp = gettext(m);
249 +        for (i = 0; i < lno; i++)
250 +                if (tp->t[i] == NULL)
251 +                        return(0);
252 +        if (col >= strlen(tp->t[i]))
253 +                return(0);
254 +        return(inglyph(x, y, tp->f->fg[tp->t[i][col]]));
255 + }
256 +
257 +
258   FONT *
259   getfont(fname)                          /* return font fname */
260   char  *fname;
# Line 286 | Line 339 | GLYPH  *gl;
339          int  xlb, ylb;
340          register GLYPH  *p0, *p1;
341  
342 <        if (x < 0.0 || y < 0.0)
342 >        if (gl == NULL)
343                  return(0);
344          x *= 256.0;                     /* get glyph coordinates */
345          y *= 256.0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines