ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/text.c
Revision: 2.21
Committed: Tue Feb 25 02:47:23 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.20: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.20 static const char RCSid[] = "$Id$";
3 greg 1.1 #endif
4     /*
5     * text.c - functions for text patterns and mixtures.
6     */
7    
8 greg 2.21 #include "copyright.h"
9 greg 2.20
10 greg 1.1 #include "ray.h"
11    
12     #include "otypes.h"
13    
14 greg 2.2 #include "font.h"
15    
16 greg 1.1 /*
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 greg 2.4 * 11+
26 greg 1.1 * Ax Ay Az
27     * Rx Ry Rz
28     * Dx Dy Dz
29     * foreground background
30 greg 2.4 * [spacing]
31 greg 1.1 *
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 greg 2.4 * 11+
38 greg 1.1 * Ax Ay Az
39     * Rx Ry Rz
40     * Dx Dy Dz
41     * foreground background
42 greg 2.4 * [spacing]
43 greg 1.1 *
44     * Colortext is identical, except colors are given rather than
45 greg 2.15 * brightnesses.
46 greg 1.1 *
47 greg 2.15 * Mixtext has foreground and background modifiers:
48     *
49 greg 1.1 * modifier mixtext id
50     * 4+ foremod backmod fontfile text..
51     * 0
52 greg 2.4 * 9+
53 greg 1.1 * Ax Ay Az
54     * Rx Ry Rz
55     * Dx Dy Dz
56 greg 2.4 * [spacing]
57 greg 1.1 */
58    
59     #define fndx(m) ((m)->otype==MIX_TEXT ? 2 : 0)
60     #define tndx(m) ((m)->otype==MIX_TEXT ? 3 : 1)
61 greg 2.4 #define sndx(m) ((m)->otype==PAT_BTEXT ? 11 : \
62     (m)->otype==PAT_CTEXT ? 15 : 9)
63 greg 1.1
64 greg 1.8 typedef struct tline {
65     struct tline *next; /* pointer to next line */
66 greg 2.4 short *spc; /* character spacing */
67     int width; /* total line width */
68 greg 1.8 /* followed by the string */
69     } TLINE;
70    
71 greg 1.9 #define TLSTR(l) ((char *)((l)+1))
72 greg 1.8
73 greg 1.7 typedef struct {
74     FVECT right, down; /* right and down unit vectors */
75     FONT *f; /* our font */
76 greg 1.8 TLINE tl; /* line list */
77 greg 1.7 } TEXT;
78    
79     TEXT *gettext();
80 greg 1.1
81 greg 1.8 TLINE *tlalloc();
82    
83 greg 1.1
84 greg 2.16 do_text(m, r)
85 greg 1.1 register OBJREC *m;
86     RAY *r;
87     {
88 greg 1.10 FVECT v;
89 greg 1.1 int foreground;
90 greg 1.7 /* get transformed position */
91 greg 1.4 if (r->rox != NULL)
92     multp3(v, r->rop, r->rox->b.xfm);
93     else
94     VCOPY(v, r->rop);
95 greg 1.7 /* check if we are within a text glyph */
96     foreground = intext(v, m);
97 greg 1.1 /* 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 gwlarson 2.18 else if ((omod = lastmod(objndx(m), modname)) == OVOID) {
104 greg 1.1 sprintf(errmsg, "undefined modifier \"%s\"", modname);
105     objerror(m, USER, errmsg);
106     }
107 greg 2.12 if (rayshade(r, omod)) {
108     if (m->omod != OVOID)
109     objerror(m, USER, "inappropriate modifier");
110     return(1);
111     }
112 greg 2.15 } else if (m->otype == PAT_BTEXT) {
113 greg 1.1 if (foreground)
114 greg 1.7 scalecolor(r->pcol, m->oargs.farg[9]);
115 greg 1.1 else
116 greg 1.7 scalecolor(r->pcol, m->oargs.farg[10]);
117 greg 1.1 } else { /* PAT_CTEXT */
118     COLOR cval;
119     if (foreground)
120 greg 1.7 setcolor(cval, m->oargs.farg[9],
121     m->oargs.farg[10],
122     m->oargs.farg[11]);
123 greg 1.1 else
124 greg 1.7 setcolor(cval, m->oargs.farg[12],
125     m->oargs.farg[13],
126     m->oargs.farg[14]);
127 greg 1.1 multcolor(r->pcol, cval);
128     }
129 greg 2.12 return(0);
130 greg 1.1 }
131    
132    
133 greg 1.8 TLINE *
134     tlalloc(s) /* allocate and assign text line */
135     char *s;
136     {
137 greg 2.4 register int siz;
138 greg 1.8 register TLINE *tl;
139    
140 greg 2.4 siz = strlen(s) + 1;
141     if ((tl=(TLINE *)malloc(sizeof(TLINE)+siz)) == NULL ||
142     (tl->spc=(short *)malloc(siz*sizeof(short))) == NULL)
143 greg 1.8 error(SYSTEM, "out of memory in tlalloc");
144     tl->next = NULL;
145     strcpy(TLSTR(tl), s);
146     return(tl);
147     }
148    
149    
150 greg 1.7 TEXT *
151     gettext(tm) /* get text structure for material */
152 greg 1.1 register OBJREC *tm;
153     {
154 greg 1.7 #define R (tm->oargs.farg+3)
155     #define D (tm->oargs.farg+6)
156     FVECT DxR;
157     double d;
158 greg 1.1 FILE *fp;
159     char linbuf[512];
160 greg 1.8 TEXT *t;
161 greg 1.1 register int i;
162 greg 1.8 register TLINE *tlp;
163 greg 1.1 register char *s;
164    
165 greg 1.7 if ((t = (TEXT *)tm->os) != NULL)
166     return(t);
167     /* check arguments */
168 greg 2.4 if (tm->oargs.nsargs - tndx(tm) < 1 || tm->oargs.nfargs < sndx(tm))
169 greg 1.7 objerror(tm, USER, "bad # arguments");
170     if ((t = (TEXT *)malloc(sizeof(TEXT))) == NULL)
171 greg 2.10 error(SYSTEM, "out of memory in gettext");
172 greg 1.7 /* compute vectors */
173     fcross(DxR, D, R);
174     fcross(t->right, DxR, D);
175 gregl 2.17 d = DOT(t->right,t->right);
176 gwlarson 2.19 if (d <= FTINY*FTINY*FTINY*FTINY)
177 gregl 2.17 objerror(tm, USER, "illegal motion vector");
178     d = DOT(D,D)/d;
179 greg 1.7 for (i = 0; i < 3; i++)
180     t->right[i] *= d;
181     fcross(t->down, R, DxR);
182 greg 1.9 d = DOT(R,R)/DOT(t->down,t->down);
183 greg 1.7 for (i = 0; i < 3; i++)
184     t->down[i] *= d;
185     /* get text */
186 greg 1.8 tlp = &t->tl;
187 greg 1.7 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 greg 1.1 }
194 greg 1.7 *--s = '\0';
195 greg 1.8 tlp->next = tlalloc(linbuf);
196     tlp = tlp->next;
197 greg 1.7 } else { /* text file */
198     if ((s = getpath(tm->oargs.sarg[tndx(tm)],
199 greg 2.13 getlibpath(), R_OK)) == NULL) {
200 greg 1.7 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 greg 2.4 sprintf(errmsg, "cannot open text file \"%s\"", s);
206 greg 1.7 error(SYSTEM, errmsg);
207     }
208 greg 1.8 while (fgets(linbuf, sizeof(linbuf), fp) != NULL) {
209 greg 1.7 s = linbuf + strlen(linbuf) - 1;
210     if (*s == '\n')
211     *s = '\0';
212 greg 1.8 tlp->next = tlalloc(linbuf);
213     tlp = tlp->next;
214 greg 1.7 }
215     fclose(fp);
216 greg 1.1 }
217 greg 1.8 tlp->next = NULL;
218 greg 1.7 /* get the font */
219     t->f = getfont(tm->oargs.sarg[fndx(tm)]);
220 greg 2.4 /* compute character spacing */
221     i = sndx(tm);
222     d = i < tm->oargs.nfargs ? tm->oargs.farg[i] : 0.0;
223 greg 2.14 i = d * 255.0;
224 greg 2.6 t->tl.width = 0;
225 greg 2.4 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 greg 2.6 if (tlp->width > t->tl.width)
233     t->tl.width = tlp->width;
234 greg 2.4 }
235 greg 1.7 /* we're done */
236     tm->os = (char *)t;
237     return(t);
238     #undef R
239     #undef D
240 greg 1.1 }
241    
242    
243 greg 1.7 freetext(m) /* free text structures associated with m */
244     OBJREC *m;
245     {
246 greg 2.9 register TEXT *tp;
247     register TLINE *tlp;
248 greg 1.7
249     tp = (TEXT *)m->os;
250     if (tp == NULL)
251     return;
252 greg 2.9 while ((tlp = tp->tl.next) != NULL) {
253     tp->tl.next = tlp->next;
254 greg 2.20 free((void *)tlp->spc);
255     free((void *)tlp);
256 greg 2.4 }
257 greg 2.20 freefont(tp->f); /* release font reference */
258     free((void *)tp);
259 greg 1.7 m->os = NULL;
260     }
261    
262    
263     intext(p, m) /* check to see if p is in text glyph */
264     FVECT p;
265 greg 1.8 OBJREC *m;
266 greg 1.7 {
267     register TEXT *tp;
268 greg 1.8 register TLINE *tlp;
269 greg 1.10 FVECT v;
270     double y, x;
271 greg 2.4 register int i, h;
272 greg 1.7 /* first, compute position in text */
273 greg 1.9 tp = gettext(m);
274 greg 1.7 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 greg 2.6 x = DOT(v, tp->right);
278     i = sndx(m);
279     if (i < m->oargs.nfargs)
280 greg 2.14 x *= tp->f->mwidth + 255.*fabs(m->oargs.farg[i]);
281 greg 2.6 else
282 greg 2.14 x *= 255.;
283 greg 2.6 h = x;
284 greg 2.4 i = y = DOT(v, tp->down);
285 greg 1.7 if (x < 0.0 || y < 0.0)
286     return(0);
287 greg 2.4 x -= (double)h;
288 greg 2.14 y = ((i+1) - y)*255.;
289 greg 2.4 /* find the line position */
290 greg 1.8 for (tlp = tp->tl.next; tlp != NULL; tlp = tlp->next)
291 greg 2.4 if (--i < 0)
292 greg 1.8 break;
293 greg 2.4 if (tlp == NULL || h >= tlp->width)
294 greg 1.7 return(0);
295 greg 2.4 for (i = 0; (h -= tlp->spc[i]) >= 0; i++)
296 greg 2.14 if (h < 255 && inglyph(h+x, y,
297 greg 2.4 tp->f->fg[TLSTR(tlp)[i]&0xff]))
298     return(1);
299     return(0);
300 greg 1.7 }
301    
302    
303 greg 1.1 inglyph(x, y, gl) /* (x,y) within font glyph gl? */
304 greg 2.14 double x, y; /* real coordinates in range [0,255) */
305 greg 2.3 register GLYPH *gl;
306 greg 1.1 {
307     int n, ncross;
308 greg 2.5 int xlb, ylb;
309     int tv;
310 greg 2.2 register GORD *p0, *p1;
311 greg 1.1
312 greg 1.7 if (gl == NULL)
313 greg 1.1 return(0);
314 greg 2.5 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 greg 2.2 n = gl->nverts; /* get # of vertices */
322     p0 = gvlist(gl) + 2*(n-1); /* connect last to first */
323     p1 = gvlist(gl);
324 greg 1.1 ncross = 0;
325     /* positive x axis cross test */
326     while (n--) {
327 greg 2.5 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 greg 1.1 ncross++;
331 greg 2.5 else if (tv)
332 greg 1.1 ncross += (p1[1] > p0[1]) ^
333     ((p0[1]-y)*(p1[0]-x) >
334     (p0[0]-x)*(p1[1]-y));
335 greg 2.5 }
336 greg 1.1 p0 = p1;
337     p1 += 2;
338     }
339     return(ncross & 01);
340     }