ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/text.c
Revision: 1.4
Committed: Sat Dec 15 15:03:40 1990 UTC (33 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +5 -2 lines
Log Message:
changed handling of matrix transformations with new MAT4 & XF types
dynamic allocation of ray transformations with newrayxf()
added missing light source vector transformation to m_brdf.c

File Contents

# User Rev Content
1 greg 1.4 /* Copyright (c) 1990 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * text.c - functions for text patterns and mixtures.
9     *
10     * 11/12/86
11     */
12    
13     #include "ray.h"
14    
15     #include "otypes.h"
16    
17     /*
18     * A text pattern is specified as the text (a file or line),
19     * the upper left anchor point, the right motion vector, the down
20     * motion vector, and the foreground and background brightness.
21     * For a file, the description is as follows:
22     *
23     * modifier brighttext id
24     * 2 fontfile textfile
25     * 0
26     * 11
27     * Ax Ay Az
28     * Rx Ry Rz
29     * Dx Dy Dz
30     * foreground background
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     *
43     * Colortext is identical, except colors are given rather than
44     * brightnesses. Mixtext has foreground and background modifiers:
45     *
46     * modifier mixtext id
47     * 4+ foremod backmod fontfile text..
48     * 0
49     * 9
50     * Ax Ay Az
51     * Rx Ry Rz
52     * Dx Dy Dz
53     */
54    
55     #define fndx(m) ((m)->otype==MIX_TEXT ? 2 : 0)
56     #define tndx(m) ((m)->otype==MIX_TEXT ? 3 : 1)
57    
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    
68     extern GLYPH *getglyph();
69    
70     extern FONT *getfont();
71    
72     static FONT *fontlist = NULL; /* our font list */
73    
74    
75     text(m, r)
76     register OBJREC *m;
77     RAY *r;
78     {
79     double v[3], y, x;
80     int col, lno;
81     int foreground;
82     GLYPH *g;
83     register double *ap;
84    
85     if (m->oargs.nsargs - tndx(m) < 1 ||
86     m->oargs.nfargs != (m->otype == PAT_BTEXT ? 11 :
87     m->otype == PAT_CTEXT ? 15 : 9))
88     objerror(m, USER, "bad # arguments");
89    
90     /* first, discover position in text */
91     ap = m->oargs.farg;
92 greg 1.4 if (r->rox != NULL)
93     multp3(v, r->rop, r->rox->b.xfm);
94     else
95     VCOPY(v, r->rop);
96 greg 1.2 v[0] -= ap[0];
97     v[1] -= ap[1];
98     v[2] -= ap[2];
99 greg 1.1 col = x = DOT(v, ap+3) / DOT(ap+3, ap+3);
100     lno = y = DOT(v, ap+6) / DOT(ap+6, ap+6);
101     x -= col;
102     y = (lno+1) - y;
103     /* get the font character, check it */
104     if ((g = getglyph(m, lno, col)) == NULL)
105     foreground = 0;
106     else
107     foreground = inglyph(x, y, g);
108     /* modify */
109     if (m->otype == MIX_TEXT) {
110     OBJECT omod;
111     char *modname = m->oargs.sarg[foreground ? 0 : 1];
112     if (!strcmp(modname, VOIDID))
113     omod = OVOID;
114     else if ((omod = modifier(modname)) == OVOID) {
115     sprintf(errmsg, "undefined modifier \"%s\"", modname);
116     objerror(m, USER, errmsg);
117     }
118     raymixture(r, omod, OVOID, 1.0);
119     } else if (m->otype == PAT_BTEXT) {
120     if (foreground)
121     scalecolor(r->pcol, ap[9]);
122     else
123     scalecolor(r->pcol, ap[10]);
124     } else { /* PAT_CTEXT */
125     COLOR cval;
126     if (foreground)
127     setcolor(cval, ap[9], ap[10], ap[11]);
128     else
129     setcolor(cval, ap[12], ap[13], ap[14]);
130     multcolor(r->pcol, cval);
131     }
132     }
133    
134    
135     GLYPH *
136     getglyph(tm, lno, col) /* get a glyph from a text description */
137     register OBJREC *tm;
138     int lno;
139     int col;
140     {
141     extern char *strcpy(), *fgets();
142     FILE *fp;
143     char linbuf[512];
144     register int i;
145     register char **txt;
146     register char *s;
147    
148     if (lno < 0 || col < 0)
149     return(NULL);
150     if (tm->os == NULL) {
151     txt = (char **)malloc(2*sizeof(char **));
152     if (txt == NULL)
153     goto memerr;
154     if (tm->oargs.nsargs - tndx(tm) > 1) { /* single line */
155     s = linbuf;
156     for (i = tndx(tm)+1; i < tm->oargs.nsargs; i++) {
157     strcpy(s, tm->oargs.sarg[i]);
158     s += strlen(s);
159     *s++ = ' ';
160     }
161     *--s = '\0';
162     txt[0] = savqstr(linbuf);
163     txt[1] = NULL;
164     } else { /* text file */
165     if ((s = getpath(tm->oargs.sarg[tndx(tm)],
166 greg 1.3 libpath, R_OK)) == NULL) {
167 greg 1.1 sprintf(errmsg, "cannot find text file \"%s\"",
168     tm->oargs.sarg[tndx(tm)]);
169     error(USER, errmsg);
170     }
171     if ((fp = fopen(s, "r")) == NULL) {
172     sprintf(errmsg, "cannot open text file \"%s\"",
173     s);
174     error(SYSTEM, errmsg);
175     }
176     for (i=0; fgets(linbuf,sizeof(linbuf),fp)!=NULL; i++) {
177     s = linbuf + strlen(linbuf) - 1;
178     if (*s == '\n')
179     *s = '\0';
180     txt=(char **)realloc(txt,(i+2)*sizeof(char **));
181     if (txt == NULL)
182     goto memerr;
183     txt[i] = savqstr(linbuf);
184     }
185     txt[i] = NULL;
186     fclose(fp);
187     }
188     tm->os = (char *)txt;
189     }
190     txt = (char **)tm->os;
191     for (i = 0; i < lno; i++)
192     if (txt[i] == NULL)
193     break;
194     if ((s = txt[i]) == NULL || col >= strlen(s))
195     return(NULL);
196     else
197     return(getfont(tm->oargs.sarg[fndx(tm)])->fg[s[col]]);
198     memerr:
199     error(SYSTEM, "out of memory in getglyph");
200     }
201    
202    
203     FONT *
204     getfont(fname) /* return font fname */
205     char *fname;
206     {
207     FILE *fp;
208     char *pathname, *err;
209     int gn, ngv, gv;
210     register GLYPH *g;
211     register FONT *f;
212    
213     for (f = fontlist; f != NULL; f = f->next)
214     if (!strcmp(f->name, fname))
215     return(f);
216     /* load the font file */
217 greg 1.3 if ((pathname = getpath(fname, libpath, R_OK)) == NULL) {
218 greg 1.1 sprintf(errmsg, "cannot find font file \"%s\"", fname);
219     error(USER, errmsg);
220     }
221     f = (FONT *)calloc(1, sizeof(FONT));
222     if (f == NULL)
223     goto memerr;
224     f->name = savestr(fname);
225     if ((fp = fopen(pathname, "r")) == NULL) {
226     sprintf(errmsg, "cannot open font file \"%s\"",
227     pathname);
228     error(SYSTEM, errmsg);
229     }
230     while (fscanf(fp, "%d", &gn) == 1) { /* get each glyph */
231     if (gn < 0 || gn > 255) {
232     err = "illegal";
233     goto fonterr;
234     }
235     if (f->fg[gn] != NULL) {
236     err = "duplicate";
237     goto fonterr;
238     }
239     if (fscanf(fp, "%d", &ngv) != 1 ||
240     ngv < 0 || ngv > 255) {
241     err = "bad # vertices for";
242     goto fonterr;
243     }
244     g = (GLYPH *)malloc((2*ngv+1)*sizeof(GLYPH));
245     if (g == NULL)
246     goto memerr;
247     f->fg[gn] = g;
248     *g++ = ngv;
249     ngv *= 2;
250     while (ngv--) {
251     if (fscanf(fp, "%d", &gv) != 1 ||
252     gv < 0 || gv > 255) {
253     err = "bad vertex for";
254     goto fonterr;
255     }
256     *g++ = gv;
257     }
258     }
259     fclose(fp);
260     f->next = fontlist;
261     return(fontlist = f);
262     fonterr:
263     sprintf(errmsg, "%s character (%d) in font file \"%s\"",
264     err, gn, pathname);
265     error(USER, errmsg);
266     memerr:
267     error(SYSTEM, "out of memory in fontglyph");
268     }
269    
270    
271     inglyph(x, y, gl) /* (x,y) within font glyph gl? */
272     double x, y;
273     GLYPH *gl;
274     {
275     int n, ncross;
276     int xlb, ylb;
277     register GLYPH *p0, *p1;
278    
279     if (x < 0.0 || y < 0.0)
280     return(0);
281     xlb = x *= 255.0; /* get glyph coordinates */
282     ylb = y *= 255.0;
283     n = *gl++; /* get # of vertices */
284     p0 = gl + 2*(n-1); /* connect last to first */
285     p1 = gl;
286     ncross = 0;
287     /* positive x axis cross test */
288     while (n--) {
289     if ((p0[1] > ylb) ^ (p1[1] > ylb))
290     if (p0[0] > xlb && p1[0] > xlb)
291     ncross++;
292     else if (p0[0] > xlb || p1[0] > xlb)
293     ncross += (p1[1] > p0[1]) ^
294     ((p0[1]-y)*(p1[0]-x) >
295     (p0[0]-x)*(p1[1]-y));
296     p0 = p1;
297     p1 += 2;
298     }
299     return(ncross & 01);
300     }