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