ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/text.c
Revision: 2.23
Committed: Sun Jul 27 22:12:03 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.22: +2 -2 lines
Log Message:
Added grouping parens to reduce ambiguity warnings.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: text.c,v 2.22 2003/05/13 17:58:33 greg Exp $";
3 #endif
4 /*
5 * text.c - functions for text patterns and mixtures.
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
19 * motion vector, and the foreground and background brightness.
20 * For a file, the description is as follows:
21 *
22 * modifier brighttext id
23 * 2 fontfile textfile
24 * 0
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+
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.
46 *
47 * Mixtext has foreground and background modifiers:
48 *
49 * modifier mixtext id
50 * 4+ foremod backmod fontfile text..
51 * 0
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 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 #define TLSTR(l) ((char *)((l)+1))
72
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 TEXT *gettext();
80
81 TLINE *tlalloc();
82
83
84 do_text(m, r)
85 register OBJREC *m;
86 RAY *r;
87 {
88 FVECT v;
89 int foreground;
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 /* 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 = lastmod(objndx(m), modname)) == OVOID) {
104 sprintf(errmsg, "undefined modifier \"%s\"", modname);
105 objerror(m, USER, errmsg);
106 }
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, m->oargs.farg[9]);
115 else
116 scalecolor(r->pcol, m->oargs.farg[10]);
117 } else { /* PAT_CTEXT */
118 COLOR cval;
119 if (foreground)
120 setcolor(cval, m->oargs.farg[9],
121 m->oargs.farg[10],
122 m->oargs.farg[11]);
123 else
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 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;
153 {
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 TLINE *tlp;
163 register char *s;
164
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 *--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 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 freetext(m) /* free text structures associated with m */
244 OBJREC *m;
245 {
246 register TEXT *tp;
247 register TLINE *tlp;
248
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 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; /* real coordinates in range [0,255) */
305 register GLYPH *gl;
306 {
307 int n, ncross;
308 int xlb, ylb;
309 int tv;
310 register GORD *p0, *p1;
311
312 if (gl == NULL)
313 return(0);
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]<<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 (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 }
339 return(ncross & 01);
340 }