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