ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/psign.c
Revision: 2.3
Committed: Sat Jun 6 07:40:51 1992 UTC (31 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +25 -63 lines
Log Message:
initial move of getfont to separate routine

File Contents

# User Rev Content
1 greg 1.3 /* Copyright (c) 1991 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * psign.c - produce picture from text.
9     *
10     * 7/1/87
11     */
12    
13 greg 2.3 #include "standard.h"
14 greg 1.1
15     #include "color.h"
16    
17 greg 2.3 #include "font.h"
18    
19 greg 1.1 #define MAXLINE 512 /* longest allowable line */
20    
21 greg 2.3 #ifndef DEFPATH
22     #define DEFPATH ":/usr/local/lib/ray"
23     #endif
24     #ifndef ULIBVAR
25     #define ULIBVAR "RAYPATH"
26     #endif
27    
28 greg 1.2 char *fontfile = "helvet.fnt"; /* our font file */
29 greg 1.1
30     COLR bgcolr = WHTCOLR; /* background color */
31     COLR fgcolr = BLKCOLR; /* foreground color */
32    
33     int direct = 'r'; /* direction (right, up, left, down) */
34    
35     int cheight = 32; /* character height */
36     double aspect = 1.67; /* height/width character aspect */
37     int cwidth; /* computed character width */
38    
39     unsigned char *ourbitmap; /* our output bitmap */
40     int xsiz, ysiz; /* bitmap dimensions */
41     int xdim; /* size of horizontal scan (bytes) */
42    
43     #define bitop(x,y,op) (ourbitmap[(y)*xdim+((x)>>3)] op (1<<((x)&7)))
44     #define tstbit(x,y) bitop(x,y,&)
45     #define setbit(x,y) bitop(x,y,|=)
46     #define clrbit(x,y) bitop(x,y,&=~)
47     #define tglbit(x,y) bitop(x,y,^=)
48    
49     typedef unsigned char GLYPH;
50    
51 greg 2.3 FONT *ourfont; /* our font */
52 greg 1.1
53 greg 2.3 char *libpath; /* library search path */
54    
55 greg 1.1 typedef struct line {
56     char *s; /* line w/o LF */
57     struct line *next; /* next line up */
58     } LINE;
59    
60     LINE *ourtext; /* our text */
61     int nlines, maxline; /* text dimensions */
62    
63 greg 2.3 extern char *getenv();
64 greg 1.2 extern char *malloc(), *calloc();
65 greg 1.1
66    
67     main(argc, argv)
68     int argc;
69     char *argv[];
70     {
71     int an;
72    
73     for (an = 1; an < argc && argv[an][0] == '-'; an++)
74     switch (argv[an][1]) {
75     case 'c': /* color */
76     switch (argv[an][2]) {
77     case 'f': /* foreground */
78     setcolr(fgcolr, atof(argv[an+1]),
79     atof(argv[an+2]),
80     atof(argv[an+3]));
81     an += 3;
82     break;
83     case 'b': /* background */
84     setcolr(bgcolr, atof(argv[an+1]),
85     atof(argv[an+2]),
86     atof(argv[an+3]));
87     an += 3;
88     break;
89     default:
90     goto unkopt;
91     }
92     break;
93     case 'f': /* font */
94     fontfile = argv[++an];
95     break;
96     case 'd': /* direction */
97     switch (argv[an][2]) {
98     case 'r': /* right */
99     case 'u': /* up */
100     case 'l': /* left */
101     case 'd': /* down */
102     direct = argv[an][2];
103     break;
104     default:
105     goto unkopt;
106     }
107     break;
108     case 'h': /* height of characters */
109     cheight = atoi(argv[++an]);
110     break;
111     case 'a': /* aspect ratio */
112     aspect = atof(argv[++an]);
113     break;
114     default:;
115     unkopt:
116     fprintf(stderr, "%s: unknown option: %s\n",
117     argv[0], argv[an]);
118     exit(1);
119     }
120     /* get text */
121     if (an == argc)
122     gettext(stdin);
123     else
124     arg_text(argc-an, argv+an);
125    
126     /* create bit map */
127     makemap();
128     /* load font file */
129 greg 2.3 if ((libpath = getenv(ULIBVAR)) == NULL)
130     libpath = DEFPATH;
131     ourfont = getfont(fontfile);
132 greg 1.1 /* convert text to bitmap */
133     maptext();
134     /* print header */
135     printargs(argc, argv, stdout);
136 greg 1.3 fputformat(COLRFMT, stdout);
137     putchar('\n');
138 greg 1.1 /* write out bitmap */
139     writemap(stdout);
140    
141     exit(0);
142     }
143    
144    
145     makemap() /* create the bit map */
146     {
147     cwidth = cheight/aspect + 0.5;
148     if (direct == 'r' || direct == 'l') {
149     xsiz = maxline*cwidth;
150     ysiz = nlines*cheight;
151     } else { /* reverse orientation */
152     xsiz = nlines*cheight;
153     ysiz = maxline*cwidth;
154     }
155     xdim = (xsiz+7)/8;
156     ourbitmap = (BYTE *)calloc(ysiz, xdim);
157     if (ourbitmap == NULL) {
158     fprintf(stderr, "out of memory in makemap\n");
159     exit(1);
160     }
161     }
162    
163    
164     gettext(fp) /* get text from a file */
165     FILE *fp;
166     {
167     char *fgets();
168     char buf[MAXLINE];
169     register LINE *curl;
170     int len;
171    
172     maxline = 0;
173     nlines = 0;
174     while (fgets(buf, MAXLINE, fp) != NULL) {
175     curl = (LINE *)malloc(sizeof(LINE));
176     if (curl == NULL)
177     goto memerr;
178     len = strlen(buf);
179     curl->s = malloc(len--);
180     if (curl->s == NULL)
181     goto memerr;
182     strncpy(curl->s, buf, len);
183     curl->s[len] = '\0';
184     curl->next = ourtext;
185     ourtext = curl;
186     if (len > maxline)
187     maxline = len;
188     nlines++;
189     }
190     return;
191     memerr:
192     fprintf(stderr, "out of memory in gettext\n");
193     exit(1);
194     }
195    
196    
197     arg_text(ac, av) /* get text from arguments */
198     int ac;
199     char *av[];
200     {
201     register char *cp;
202    
203     ourtext = (LINE *)malloc(sizeof(LINE));
204     if (ourtext == NULL)
205     goto memerr;
206     ourtext->s = malloc(MAXLINE);
207     if (ourtext->s == NULL)
208     goto memerr;
209     for (cp = ourtext->s; ac-- > 0; av++) {
210     strcpy(cp, *av);
211     cp += strlen(*av);
212     *cp++ = ' ';
213     }
214     *--cp = '\0';
215     ourtext->next = NULL;
216     maxline = strlen(ourtext->s);
217     nlines = 1;
218     return;
219     memerr:
220     fprintf(stderr, "out of memory in arg_text\n");
221     exit(1);
222     }
223    
224    
225     maptext() /* map our text */
226     {
227     register LINE *curl;
228     int l;
229     register int c;
230    
231     for (l = 0, curl = ourtext; curl != NULL; l++, curl = curl->next)
232     for (c = strlen(curl->s)-1; c >= 0; c--)
233 greg 2.3 mapglyph(ourfont->fg[curl->s[c]&0xff], c, l);
234 greg 1.1 }
235    
236    
237     mapglyph(gl, tx0, ty0) /* convert a glyph */
238 greg 2.3 GLYPH *gl;
239 greg 1.1 int tx0, ty0;
240     {
241     int n;
242 greg 2.3 register GORD *gp;
243 greg 1.1 int p0[2], p1[2];
244    
245     if (gl == NULL)
246     return;
247    
248     tx0 <<= 8; ty0 <<= 8;
249 greg 2.3 n = gl->nverts;
250     gp = gvlist(gl);
251     mapcoord(p0, gp[2*n-2]+tx0, gp[2*n-1]+ty0);
252 greg 1.1 while (n--) {
253 greg 2.3 mapcoord(p1, gp[0]+tx0, gp[1]+ty0);
254 greg 1.1 mapedge(p0[0], p0[1], p1[0]-p0[0], p1[1]-p0[1]);
255     p0[0] = p1[0]; p0[1] = p1[1];
256 greg 2.3 gp += 2;
257 greg 1.1 }
258     }
259    
260    
261     mapcoord(p, tx, ty) /* map text to picture coordinates */
262     int p[2], tx, ty;
263     {
264     tx = (long)tx*cwidth >> 8;
265     ty = (long)ty*cheight >> 8;
266    
267     switch (direct) {
268     case 'r': /* right */
269     p[0] = tx;
270     p[1] = ty;
271     return;
272     case 'u': /* up */
273     p[0] = xsiz-1-ty;
274     p[1] = tx;
275     return;
276     case 'l': /* left */
277     p[0] = xsiz-1-tx;
278     p[1] = ysiz-1-ty;
279     return;
280     case 'd': /* down */
281     p[0] = ty;
282     p[1] = ysiz-1-tx;
283     return;
284     }
285     }
286    
287    
288     mapedge(x, y, run, rise) /* map an edge */
289     register int x, y;
290     int run, rise;
291     {
292     int xstep;
293     int rise2, run2;
294     int n;
295    
296     if (rise == 0)
297     return;
298     /* always draw up */
299     if (rise < 0) {
300     x += run;
301     y += rise;
302     rise = -rise;
303     run = -run;
304     }
305     if (run < 0) {
306     xstep = -1;
307     run = -run;
308     } else
309     xstep = 1;
310     n = rise;
311     run2 = rise2 = 0;
312     while (n)
313     if (rise2 >= run2) {
314     tglbit(x, y);
315     n--;
316     y++;
317     run2 += run;
318     } else {
319     x += xstep;
320     rise2 += rise;
321     }
322     }
323    
324    
325     writemap(fp) /* write out bitmap */
326     FILE *fp;
327     {
328     COLR *scanout;
329     int y;
330     register int x;
331     register int inglyph;
332    
333     fprintf(fp, "-Y %d +X %d\n", ysiz, xsiz);
334    
335     scanout = (COLR *)malloc(xsiz*sizeof(COLR));
336     if (scanout == NULL) {
337     fprintf(stderr, "out of memory in writemap\n");
338     exit(1);
339     }
340     for (y = ysiz-1; y >= 0; y--) {
341     inglyph = 0;
342     for (x = 0; x < xsiz; x++) {
343     if (tstbit(x, y))
344     inglyph ^= 1;
345     if (inglyph)
346     copycolr(scanout[x], fgcolr);
347     else
348     copycolr(scanout[x], bgcolr);
349     }
350     if (fwritecolrs(scanout, xsiz, fp) < 0) {
351     fprintf(stderr, "write error in writemap\n");
352     exit(1);
353     }
354     }
355     free((char *)scanout);
356     }