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