10 |
|
* 7/1/87 |
11 |
|
*/ |
12 |
|
|
13 |
< |
#include <stdio.h> |
13 |
> |
#include "standard.h" |
14 |
|
|
15 |
|
#include "color.h" |
16 |
|
|
17 |
+ |
#include "font.h" |
18 |
+ |
|
19 |
+ |
#ifndef SSS |
20 |
+ |
#define SSS 3 /* super-sample size */ |
21 |
+ |
#endif |
22 |
+ |
|
23 |
|
#define MAXLINE 512 /* longest allowable line */ |
24 |
|
|
25 |
+ |
#ifndef DEFPATH |
26 |
+ |
#define DEFPATH ":/usr/local/lib/ray" |
27 |
+ |
#endif |
28 |
+ |
#ifndef ULIBVAR |
29 |
+ |
#define ULIBVAR "RAYPATH" |
30 |
+ |
#endif |
31 |
+ |
|
32 |
|
char *fontfile = "helvet.fnt"; /* our font file */ |
33 |
|
|
34 |
< |
COLR bgcolr = WHTCOLR; /* background color */ |
35 |
< |
COLR fgcolr = BLKCOLR; /* foreground color */ |
34 |
> |
COLOR bgcolor = WHTCOLOR; /* background color */ |
35 |
> |
COLOR fgcolor = BLKCOLOR; /* foreground color */ |
36 |
|
|
37 |
|
int direct = 'r'; /* direction (right, up, left, down) */ |
38 |
|
|
39 |
< |
int cheight = 32; /* character height */ |
39 |
> |
int cheight = 32*SSS; /* character height */ |
40 |
|
double aspect = 1.67; /* height/width character aspect */ |
41 |
+ |
double spacing = 0.0; /* character spacing */ |
42 |
|
int cwidth; /* computed character width */ |
43 |
|
|
44 |
|
unsigned char *ourbitmap; /* our output bitmap */ |
51 |
|
#define clrbit(x,y) bitop(x,y,&=~) |
52 |
|
#define tglbit(x,y) bitop(x,y,^=) |
53 |
|
|
54 |
< |
typedef unsigned char GLYPH; |
54 |
> |
FONT *ourfont; /* our font */ |
55 |
|
|
56 |
< |
GLYPH *ourfont[128]; /* our font */ |
56 |
> |
char *libpath; /* library search path */ |
57 |
|
|
58 |
|
typedef struct line { |
59 |
|
char *s; /* line w/o LF */ |
60 |
+ |
short *sp; /* character spacing */ |
61 |
|
struct line *next; /* next line up */ |
62 |
|
} LINE; |
63 |
|
|
64 |
|
LINE *ourtext; /* our text */ |
65 |
|
int nlines, maxline; /* text dimensions */ |
66 |
+ |
int maxwidth; /* maximum line width (dvi) */ |
67 |
|
|
68 |
+ |
extern char *getenv(); |
69 |
|
extern char *malloc(), *calloc(); |
53 |
– |
extern FILE *fropen(); |
70 |
|
|
71 |
|
|
72 |
|
main(argc, argv) |
73 |
|
int argc; |
74 |
|
char *argv[]; |
75 |
|
{ |
60 |
– |
double atof(); |
76 |
|
int an; |
77 |
|
|
78 |
|
for (an = 1; an < argc && argv[an][0] == '-'; an++) |
80 |
|
case 'c': /* color */ |
81 |
|
switch (argv[an][2]) { |
82 |
|
case 'f': /* foreground */ |
83 |
< |
setcolr(fgcolr, atof(argv[an+1]), |
83 |
> |
setcolor(fgcolor, atof(argv[an+1]), |
84 |
|
atof(argv[an+2]), |
85 |
|
atof(argv[an+3])); |
86 |
|
an += 3; |
87 |
|
break; |
88 |
|
case 'b': /* background */ |
89 |
< |
setcolr(bgcolr, atof(argv[an+1]), |
89 |
> |
setcolor(bgcolor, atof(argv[an+1]), |
90 |
|
atof(argv[an+2]), |
91 |
|
atof(argv[an+3])); |
92 |
|
an += 3; |
111 |
|
} |
112 |
|
break; |
113 |
|
case 'h': /* height of characters */ |
114 |
< |
cheight = atoi(argv[++an]); |
114 |
> |
cheight = atoi(argv[++an])*SSS; |
115 |
|
break; |
116 |
|
case 'a': /* aspect ratio */ |
117 |
|
aspect = atof(argv[++an]); |
118 |
|
break; |
119 |
+ |
case 's': /* spacing */ |
120 |
+ |
spacing = atof(argv[++an]); |
121 |
+ |
break; |
122 |
|
default:; |
123 |
|
unkopt: |
124 |
|
fprintf(stderr, "%s: unknown option: %s\n", |
125 |
|
argv[0], argv[an]); |
126 |
|
exit(1); |
127 |
|
} |
128 |
+ |
/* load font file */ |
129 |
+ |
if ((libpath = getenv(ULIBVAR)) == NULL) |
130 |
+ |
libpath = DEFPATH; |
131 |
+ |
ourfont = getfont(fontfile); |
132 |
|
/* get text */ |
133 |
|
if (an == argc) |
134 |
|
gettext(stdin); |
137 |
|
|
138 |
|
/* create bit map */ |
139 |
|
makemap(); |
118 |
– |
/* load font file */ |
119 |
– |
loadfont(); |
140 |
|
/* convert text to bitmap */ |
141 |
|
maptext(); |
142 |
|
/* print header */ |
154 |
|
{ |
155 |
|
cwidth = cheight/aspect + 0.5; |
156 |
|
if (direct == 'r' || direct == 'l') { |
157 |
< |
xsiz = maxline*cwidth; |
157 |
> |
xsiz = (long)maxwidth*cwidth >> 8; |
158 |
|
ysiz = nlines*cheight; |
159 |
|
} else { /* reverse orientation */ |
160 |
|
xsiz = nlines*cheight; |
161 |
< |
ysiz = maxline*cwidth; |
161 |
> |
ysiz = (long)maxwidth*cwidth >> 8; |
162 |
|
} |
163 |
+ |
if (xsiz % SSS) |
164 |
+ |
xsiz += SSS - xsiz%SSS; |
165 |
+ |
if (ysiz % SSS) |
166 |
+ |
ysiz += SSS - ysiz%SSS; |
167 |
|
xdim = (xsiz+7)/8; |
168 |
|
ourbitmap = (BYTE *)calloc(ysiz, xdim); |
169 |
< |
if (ourbitmap == NULL) { |
170 |
< |
fprintf(stderr, "out of memory in makemap\n"); |
147 |
< |
exit(1); |
148 |
< |
} |
169 |
> |
if (ourbitmap == NULL) |
170 |
> |
error(SYSTEM, "Out of memory in makemap"); |
171 |
|
} |
172 |
|
|
173 |
|
|
152 |
– |
loadfont() /* load the font file */ |
153 |
– |
{ |
154 |
– |
FILE *fp; |
155 |
– |
char *err; |
156 |
– |
int gn, ngv, gv; |
157 |
– |
register GLYPH *g; |
158 |
– |
|
159 |
– |
if ((fp = fropen(fontfile)) == NULL) { |
160 |
– |
fprintf(stderr, "cannot find font file \"%s\"\n", |
161 |
– |
fontfile); |
162 |
– |
exit(1); |
163 |
– |
} |
164 |
– |
while (fscanf(fp, "%d", &gn) == 1) { /* get each glyph */ |
165 |
– |
if (gn < 0 || gn > 127) { |
166 |
– |
err = "illegal"; |
167 |
– |
goto fonterr; |
168 |
– |
} |
169 |
– |
if (ourfont[gn] != NULL) { |
170 |
– |
err = "duplicate"; |
171 |
– |
goto fonterr; |
172 |
– |
} |
173 |
– |
if (fscanf(fp, "%d", &ngv) != 1 || |
174 |
– |
ngv < 0 || ngv > 255) { |
175 |
– |
err = "bad # vertices for"; |
176 |
– |
goto fonterr; |
177 |
– |
} |
178 |
– |
g = (GLYPH *)malloc((2*ngv+1)*sizeof(GLYPH)); |
179 |
– |
if (g == NULL) |
180 |
– |
goto memerr; |
181 |
– |
ourfont[gn] = g; |
182 |
– |
*g++ = ngv; |
183 |
– |
ngv *= 2; |
184 |
– |
while (ngv--) { |
185 |
– |
if (fscanf(fp, "%d", &gv) != 1 || |
186 |
– |
gv < 0 || gv > 255) { |
187 |
– |
err = "bad vertex for"; |
188 |
– |
goto fonterr; |
189 |
– |
} |
190 |
– |
*g++ = gv; |
191 |
– |
} |
192 |
– |
} |
193 |
– |
fclose(fp); |
194 |
– |
return; |
195 |
– |
fonterr: |
196 |
– |
fprintf(stderr, "%s character (%d) in font file \"%s\"\n", |
197 |
– |
err, gn, fontfile); |
198 |
– |
exit(1); |
199 |
– |
memerr: |
200 |
– |
fprintf(stderr, "out of memory in loadfont\n"); |
201 |
– |
exit(1); |
202 |
– |
} |
203 |
– |
|
204 |
– |
|
174 |
|
gettext(fp) /* get text from a file */ |
175 |
|
FILE *fp; |
176 |
|
{ |
180 |
|
int len; |
181 |
|
|
182 |
|
maxline = 0; |
183 |
+ |
maxwidth = 0; |
184 |
|
nlines = 0; |
185 |
|
while (fgets(buf, MAXLINE, fp) != NULL) { |
186 |
|
curl = (LINE *)malloc(sizeof(LINE)); |
187 |
|
if (curl == NULL) |
188 |
|
goto memerr; |
189 |
|
len = strlen(buf); |
190 |
< |
curl->s = malloc(len--); |
191 |
< |
if (curl->s == NULL) |
190 |
> |
curl->s = malloc(len); |
191 |
> |
curl->sp = (short *)malloc(sizeof(short)*len--); |
192 |
> |
if (curl->s == NULL | curl->sp == NULL) |
193 |
|
goto memerr; |
194 |
+ |
if (len > maxline) |
195 |
+ |
maxline = len; |
196 |
|
strncpy(curl->s, buf, len); |
197 |
|
curl->s[len] = '\0'; |
198 |
+ |
if (spacing < -1./256.) |
199 |
+ |
len = squeeztext(curl->sp, curl->s, ourfont, |
200 |
+ |
(int)(spacing*-256.0)); |
201 |
+ |
else if (spacing > 1./256.) |
202 |
+ |
len = proptext(curl->sp, curl->s, ourfont, |
203 |
+ |
(int)(spacing*256.0), 3); |
204 |
+ |
else |
205 |
+ |
len = uniftext(curl->sp, curl->s, ourfont); |
206 |
+ |
if (len > maxwidth) |
207 |
+ |
maxwidth = len; |
208 |
|
curl->next = ourtext; |
209 |
|
ourtext = curl; |
227 |
– |
if (len > maxline) |
228 |
– |
maxline = len; |
210 |
|
nlines++; |
211 |
|
} |
212 |
|
return; |
213 |
|
memerr: |
214 |
< |
fprintf(stderr, "out of memory in gettext\n"); |
234 |
< |
exit(1); |
214 |
> |
error(SYSTEM, "Out of memory in gettext"); |
215 |
|
} |
216 |
|
|
217 |
|
|
235 |
|
*--cp = '\0'; |
236 |
|
ourtext->next = NULL; |
237 |
|
maxline = strlen(ourtext->s); |
238 |
+ |
ourtext->sp = (short *)malloc(sizeof(short)*(maxline+1)); |
239 |
+ |
if (ourtext->sp == NULL) |
240 |
+ |
goto memerr; |
241 |
+ |
if (spacing < 0.0) |
242 |
+ |
maxwidth = squeeztext(ourtext->sp, ourtext->s, ourfont, |
243 |
+ |
(int)(spacing*-256.0)); |
244 |
+ |
else if (spacing > 0.0) |
245 |
+ |
maxwidth = proptext(ourtext->sp, ourtext->s, ourfont, |
246 |
+ |
(int)(spacing*256.0), 3); |
247 |
+ |
else |
248 |
+ |
maxwidth = uniftext(ourtext->sp, ourtext->s, ourfont); |
249 |
|
nlines = 1; |
250 |
|
return; |
251 |
|
memerr: |
252 |
< |
fprintf(stderr, "out of memory in arg_text\n"); |
262 |
< |
exit(1); |
252 |
> |
error(SYSTEM, "Out of memory in arg_text"); |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
|
maptext() /* map our text */ |
257 |
|
{ |
258 |
|
register LINE *curl; |
259 |
< |
int l; |
260 |
< |
register int c; |
259 |
> |
int l, len; |
260 |
> |
register int i, c; |
261 |
|
|
262 |
< |
for (l = 0, curl = ourtext; curl != NULL; l++, curl = curl->next) |
263 |
< |
for (c = strlen(curl->s)-1; c >= 0; c--) |
264 |
< |
mapglyph(ourfont[curl->s[c]], c, l); |
262 |
> |
for (l = 0, curl = ourtext; curl != NULL; l += 256, curl = curl->next) { |
263 |
> |
len = strlen(curl->s); c = 0; |
264 |
> |
for (i = 0; i < len; i++) { |
265 |
> |
c += curl->sp[i]; |
266 |
> |
mapglyph(ourfont->fg[curl->s[i]&0xff], c, l); |
267 |
> |
} |
268 |
> |
} |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
mapglyph(gl, tx0, ty0) /* convert a glyph */ |
273 |
< |
register GLYPH *gl; |
273 |
> |
GLYPH *gl; |
274 |
|
int tx0, ty0; |
275 |
|
{ |
276 |
|
int n; |
277 |
+ |
register GORD *gp; |
278 |
|
int p0[2], p1[2]; |
279 |
|
|
280 |
|
if (gl == NULL) |
281 |
|
return; |
282 |
|
|
283 |
< |
tx0 <<= 8; ty0 <<= 8; |
284 |
< |
n = *gl++; |
285 |
< |
mapcoord(p0, gl[2*n-2]+tx0, gl[2*n-1]+ty0); |
283 |
> |
n = gl->nverts; |
284 |
> |
gp = gvlist(gl); |
285 |
> |
mapcoord(p0, gp[2*n-2]+tx0, gp[2*n-1]+ty0); |
286 |
|
while (n--) { |
287 |
< |
mapcoord(p1, gl[0]+tx0, gl[1]+ty0); |
287 |
> |
mapcoord(p1, gp[0]+tx0, gp[1]+ty0); |
288 |
|
mapedge(p0[0], p0[1], p1[0]-p0[0], p1[1]-p0[1]); |
289 |
|
p0[0] = p1[0]; p0[1] = p1[1]; |
290 |
< |
gl += 2; |
290 |
> |
gp += 2; |
291 |
|
} |
292 |
|
} |
293 |
|
|
359 |
|
writemap(fp) /* write out bitmap */ |
360 |
|
FILE *fp; |
361 |
|
{ |
362 |
+ |
COLR pixval[SSS*SSS+1]; /* possible pixel values */ |
363 |
+ |
COLOR ctmp0, ctmp1; |
364 |
+ |
double d; |
365 |
|
COLR *scanout; |
366 |
< |
int y; |
367 |
< |
register int x; |
366 |
> |
int x, y; |
367 |
> |
register int i, j; |
368 |
> |
int cnt; |
369 |
|
register int inglyph; |
370 |
|
|
371 |
< |
fprintf(fp, "-Y %d +X %d\n", ysiz, xsiz); |
371 |
> |
fprintf(fp, "-Y %d +X %d\n", ysiz/SSS, xsiz/SSS); |
372 |
|
|
373 |
< |
scanout = (COLR *)malloc(xsiz*sizeof(COLR)); |
374 |
< |
if (scanout == NULL) { |
375 |
< |
fprintf(stderr, "out of memory in writemap\n"); |
376 |
< |
exit(1); |
373 |
> |
scanout = (COLR *)malloc(xsiz/SSS*sizeof(COLR)); |
374 |
> |
if (scanout == NULL) |
375 |
> |
error(SYSTEM, "Out of memory in writemap"); |
376 |
> |
for (i = 0; i <= SSS*SSS; i++) { /* compute possible values */ |
377 |
> |
copycolor(ctmp0, fgcolor); |
378 |
> |
d = (double)i/(SSS*SSS); |
379 |
> |
scalecolor(ctmp0, d); |
380 |
> |
copycolor(ctmp1, bgcolor); |
381 |
> |
d = 1.0 - d; |
382 |
> |
scalecolor(ctmp1, d); |
383 |
> |
addcolor(ctmp0, ctmp1); |
384 |
> |
setcolr(pixval[i], colval(ctmp0,RED), |
385 |
> |
colval(ctmp0,GRN), colval(ctmp0,BLU)); |
386 |
|
} |
387 |
< |
for (y = ysiz-1; y >= 0; y--) { |
387 |
> |
for (y = ysiz/SSS-1; y >= 0; y--) { |
388 |
|
inglyph = 0; |
389 |
< |
for (x = 0; x < xsiz; x++) { |
390 |
< |
if (tstbit(x, y)) |
391 |
< |
inglyph ^= 1; |
392 |
< |
if (inglyph) |
393 |
< |
copycolr(scanout[x], fgcolr); |
394 |
< |
else |
395 |
< |
copycolr(scanout[x], bgcolr); |
389 |
> |
for (x = 0; x < xsiz/SSS; x++) { |
390 |
> |
cnt = 0; |
391 |
> |
for (j = 0; j < SSS; j++) |
392 |
> |
for (i = 0; i < SSS; i++) { |
393 |
> |
if (tstbit(x*SSS+i, y*SSS+j)) |
394 |
> |
inglyph ^= 1<<j; |
395 |
> |
if (inglyph & 1<<j) |
396 |
> |
cnt++; |
397 |
> |
} |
398 |
> |
copycolr(scanout[x], pixval[cnt]); |
399 |
|
} |
400 |
< |
if (fwritecolrs(scanout, xsiz, fp) < 0) { |
400 |
> |
if (fwritecolrs(scanout, xsiz/SSS, fp) < 0) { |
401 |
|
fprintf(stderr, "write error in writemap\n"); |
402 |
|
exit(1); |
403 |
|
} |