| 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 |
|
|
double atof();
|
| 61 |
|
|
int an;
|
| 62 |
|
|
|
| 63 |
|
|
for (an = 1; an < argc && argv[an][0] == '-'; an++)
|
| 64 |
|
|
switch (argv[an][1]) {
|
| 65 |
|
|
case 'c': /* color */
|
| 66 |
|
|
switch (argv[an][2]) {
|
| 67 |
|
|
case 'f': /* foreground */
|
| 68 |
|
|
setcolr(fgcolr, atof(argv[an+1]),
|
| 69 |
|
|
atof(argv[an+2]),
|
| 70 |
|
|
atof(argv[an+3]));
|
| 71 |
|
|
an += 3;
|
| 72 |
|
|
break;
|
| 73 |
|
|
case 'b': /* background */
|
| 74 |
|
|
setcolr(bgcolr, atof(argv[an+1]),
|
| 75 |
|
|
atof(argv[an+2]),
|
| 76 |
|
|
atof(argv[an+3]));
|
| 77 |
|
|
an += 3;
|
| 78 |
|
|
break;
|
| 79 |
|
|
default:
|
| 80 |
|
|
goto unkopt;
|
| 81 |
|
|
}
|
| 82 |
|
|
break;
|
| 83 |
|
|
case 'f': /* font */
|
| 84 |
|
|
fontfile = argv[++an];
|
| 85 |
|
|
break;
|
| 86 |
|
|
case 'd': /* direction */
|
| 87 |
|
|
switch (argv[an][2]) {
|
| 88 |
|
|
case 'r': /* right */
|
| 89 |
|
|
case 'u': /* up */
|
| 90 |
|
|
case 'l': /* left */
|
| 91 |
|
|
case 'd': /* down */
|
| 92 |
|
|
direct = argv[an][2];
|
| 93 |
|
|
break;
|
| 94 |
|
|
default:
|
| 95 |
|
|
goto unkopt;
|
| 96 |
|
|
}
|
| 97 |
|
|
break;
|
| 98 |
|
|
case 'h': /* height of characters */
|
| 99 |
|
|
cheight = atoi(argv[++an]);
|
| 100 |
|
|
break;
|
| 101 |
|
|
case 'a': /* aspect ratio */
|
| 102 |
|
|
aspect = atof(argv[++an]);
|
| 103 |
|
|
break;
|
| 104 |
|
|
default:;
|
| 105 |
|
|
unkopt:
|
| 106 |
|
|
fprintf(stderr, "%s: unknown option: %s\n",
|
| 107 |
|
|
argv[0], argv[an]);
|
| 108 |
|
|
exit(1);
|
| 109 |
|
|
}
|
| 110 |
|
|
/* get text */
|
| 111 |
|
|
if (an == argc)
|
| 112 |
|
|
gettext(stdin);
|
| 113 |
|
|
else
|
| 114 |
|
|
arg_text(argc-an, argv+an);
|
| 115 |
|
|
|
| 116 |
|
|
/* create bit map */
|
| 117 |
|
|
makemap();
|
| 118 |
|
|
/* load font file */
|
| 119 |
|
|
loadfont();
|
| 120 |
|
|
/* convert text to bitmap */
|
| 121 |
|
|
maptext();
|
| 122 |
|
|
/* print header */
|
| 123 |
|
|
printargs(argc, argv, stdout);
|
| 124 |
greg |
1.3 |
fputformat(COLRFMT, stdout);
|
| 125 |
|
|
putchar('\n');
|
| 126 |
greg |
1.1 |
/* write out bitmap */
|
| 127 |
|
|
writemap(stdout);
|
| 128 |
|
|
|
| 129 |
|
|
exit(0);
|
| 130 |
|
|
}
|
| 131 |
|
|
|
| 132 |
|
|
|
| 133 |
|
|
makemap() /* create the bit map */
|
| 134 |
|
|
{
|
| 135 |
|
|
cwidth = cheight/aspect + 0.5;
|
| 136 |
|
|
if (direct == 'r' || direct == 'l') {
|
| 137 |
|
|
xsiz = maxline*cwidth;
|
| 138 |
|
|
ysiz = nlines*cheight;
|
| 139 |
|
|
} else { /* reverse orientation */
|
| 140 |
|
|
xsiz = nlines*cheight;
|
| 141 |
|
|
ysiz = maxline*cwidth;
|
| 142 |
|
|
}
|
| 143 |
|
|
xdim = (xsiz+7)/8;
|
| 144 |
|
|
ourbitmap = (BYTE *)calloc(ysiz, xdim);
|
| 145 |
|
|
if (ourbitmap == NULL) {
|
| 146 |
|
|
fprintf(stderr, "out of memory in makemap\n");
|
| 147 |
|
|
exit(1);
|
| 148 |
|
|
}
|
| 149 |
|
|
}
|
| 150 |
|
|
|
| 151 |
|
|
|
| 152 |
|
|
loadfont() /* load the font file */
|
| 153 |
|
|
{
|
| 154 |
|
|
FILE *fp;
|
| 155 |
|
|
char *err;
|
| 156 |
|
|
int gn, ngv, gv;
|
| 157 |
|
|
register GLYPH *g;
|
| 158 |
|
|
|
| 159 |
greg |
1.2 |
if ((fp = fropen(fontfile)) == NULL) {
|
| 160 |
|
|
fprintf(stderr, "cannot find font file \"%s\"\n",
|
| 161 |
greg |
1.1 |
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 |
|
|
|
| 205 |
|
|
gettext(fp) /* get text from a file */
|
| 206 |
|
|
FILE *fp;
|
| 207 |
|
|
{
|
| 208 |
|
|
char *fgets();
|
| 209 |
|
|
char buf[MAXLINE];
|
| 210 |
|
|
register LINE *curl;
|
| 211 |
|
|
int len;
|
| 212 |
|
|
|
| 213 |
|
|
maxline = 0;
|
| 214 |
|
|
nlines = 0;
|
| 215 |
|
|
while (fgets(buf, MAXLINE, fp) != NULL) {
|
| 216 |
|
|
curl = (LINE *)malloc(sizeof(LINE));
|
| 217 |
|
|
if (curl == NULL)
|
| 218 |
|
|
goto memerr;
|
| 219 |
|
|
len = strlen(buf);
|
| 220 |
|
|
curl->s = malloc(len--);
|
| 221 |
|
|
if (curl->s == NULL)
|
| 222 |
|
|
goto memerr;
|
| 223 |
|
|
strncpy(curl->s, buf, len);
|
| 224 |
|
|
curl->s[len] = '\0';
|
| 225 |
|
|
curl->next = ourtext;
|
| 226 |
|
|
ourtext = curl;
|
| 227 |
|
|
if (len > maxline)
|
| 228 |
|
|
maxline = len;
|
| 229 |
|
|
nlines++;
|
| 230 |
|
|
}
|
| 231 |
|
|
return;
|
| 232 |
|
|
memerr:
|
| 233 |
|
|
fprintf(stderr, "out of memory in gettext\n");
|
| 234 |
|
|
exit(1);
|
| 235 |
|
|
}
|
| 236 |
|
|
|
| 237 |
|
|
|
| 238 |
|
|
arg_text(ac, av) /* get text from arguments */
|
| 239 |
|
|
int ac;
|
| 240 |
|
|
char *av[];
|
| 241 |
|
|
{
|
| 242 |
|
|
register char *cp;
|
| 243 |
|
|
|
| 244 |
|
|
ourtext = (LINE *)malloc(sizeof(LINE));
|
| 245 |
|
|
if (ourtext == NULL)
|
| 246 |
|
|
goto memerr;
|
| 247 |
|
|
ourtext->s = malloc(MAXLINE);
|
| 248 |
|
|
if (ourtext->s == NULL)
|
| 249 |
|
|
goto memerr;
|
| 250 |
|
|
for (cp = ourtext->s; ac-- > 0; av++) {
|
| 251 |
|
|
strcpy(cp, *av);
|
| 252 |
|
|
cp += strlen(*av);
|
| 253 |
|
|
*cp++ = ' ';
|
| 254 |
|
|
}
|
| 255 |
|
|
*--cp = '\0';
|
| 256 |
|
|
ourtext->next = NULL;
|
| 257 |
|
|
maxline = strlen(ourtext->s);
|
| 258 |
|
|
nlines = 1;
|
| 259 |
|
|
return;
|
| 260 |
|
|
memerr:
|
| 261 |
|
|
fprintf(stderr, "out of memory in arg_text\n");
|
| 262 |
|
|
exit(1);
|
| 263 |
|
|
}
|
| 264 |
|
|
|
| 265 |
|
|
|
| 266 |
|
|
maptext() /* map our text */
|
| 267 |
|
|
{
|
| 268 |
|
|
register LINE *curl;
|
| 269 |
|
|
int l;
|
| 270 |
|
|
register int c;
|
| 271 |
|
|
|
| 272 |
|
|
for (l = 0, curl = ourtext; curl != NULL; l++, curl = curl->next)
|
| 273 |
|
|
for (c = strlen(curl->s)-1; c >= 0; c--)
|
| 274 |
|
|
mapglyph(ourfont[curl->s[c]], c, l);
|
| 275 |
|
|
}
|
| 276 |
|
|
|
| 277 |
|
|
|
| 278 |
|
|
mapglyph(gl, tx0, ty0) /* convert a glyph */
|
| 279 |
|
|
register GLYPH *gl;
|
| 280 |
|
|
int tx0, ty0;
|
| 281 |
|
|
{
|
| 282 |
|
|
int n;
|
| 283 |
|
|
int p0[2], p1[2];
|
| 284 |
|
|
|
| 285 |
|
|
if (gl == NULL)
|
| 286 |
|
|
return;
|
| 287 |
|
|
|
| 288 |
|
|
tx0 <<= 8; ty0 <<= 8;
|
| 289 |
|
|
n = *gl++;
|
| 290 |
|
|
mapcoord(p0, gl[2*n-2]+tx0, gl[2*n-1]+ty0);
|
| 291 |
|
|
while (n--) {
|
| 292 |
|
|
mapcoord(p1, gl[0]+tx0, gl[1]+ty0);
|
| 293 |
|
|
mapedge(p0[0], p0[1], p1[0]-p0[0], p1[1]-p0[1]);
|
| 294 |
|
|
p0[0] = p1[0]; p0[1] = p1[1];
|
| 295 |
|
|
gl += 2;
|
| 296 |
|
|
}
|
| 297 |
|
|
}
|
| 298 |
|
|
|
| 299 |
|
|
|
| 300 |
|
|
mapcoord(p, tx, ty) /* map text to picture coordinates */
|
| 301 |
|
|
int p[2], tx, ty;
|
| 302 |
|
|
{
|
| 303 |
|
|
tx = (long)tx*cwidth >> 8;
|
| 304 |
|
|
ty = (long)ty*cheight >> 8;
|
| 305 |
|
|
|
| 306 |
|
|
switch (direct) {
|
| 307 |
|
|
case 'r': /* right */
|
| 308 |
|
|
p[0] = tx;
|
| 309 |
|
|
p[1] = ty;
|
| 310 |
|
|
return;
|
| 311 |
|
|
case 'u': /* up */
|
| 312 |
|
|
p[0] = xsiz-1-ty;
|
| 313 |
|
|
p[1] = tx;
|
| 314 |
|
|
return;
|
| 315 |
|
|
case 'l': /* left */
|
| 316 |
|
|
p[0] = xsiz-1-tx;
|
| 317 |
|
|
p[1] = ysiz-1-ty;
|
| 318 |
|
|
return;
|
| 319 |
|
|
case 'd': /* down */
|
| 320 |
|
|
p[0] = ty;
|
| 321 |
|
|
p[1] = ysiz-1-tx;
|
| 322 |
|
|
return;
|
| 323 |
|
|
}
|
| 324 |
|
|
}
|
| 325 |
|
|
|
| 326 |
|
|
|
| 327 |
|
|
mapedge(x, y, run, rise) /* map an edge */
|
| 328 |
|
|
register int x, y;
|
| 329 |
|
|
int run, rise;
|
| 330 |
|
|
{
|
| 331 |
|
|
int xstep;
|
| 332 |
|
|
int rise2, run2;
|
| 333 |
|
|
int n;
|
| 334 |
|
|
|
| 335 |
|
|
if (rise == 0)
|
| 336 |
|
|
return;
|
| 337 |
|
|
/* always draw up */
|
| 338 |
|
|
if (rise < 0) {
|
| 339 |
|
|
x += run;
|
| 340 |
|
|
y += rise;
|
| 341 |
|
|
rise = -rise;
|
| 342 |
|
|
run = -run;
|
| 343 |
|
|
}
|
| 344 |
|
|
if (run < 0) {
|
| 345 |
|
|
xstep = -1;
|
| 346 |
|
|
run = -run;
|
| 347 |
|
|
} else
|
| 348 |
|
|
xstep = 1;
|
| 349 |
|
|
n = rise;
|
| 350 |
|
|
run2 = rise2 = 0;
|
| 351 |
|
|
while (n)
|
| 352 |
|
|
if (rise2 >= run2) {
|
| 353 |
|
|
tglbit(x, y);
|
| 354 |
|
|
n--;
|
| 355 |
|
|
y++;
|
| 356 |
|
|
run2 += run;
|
| 357 |
|
|
} else {
|
| 358 |
|
|
x += xstep;
|
| 359 |
|
|
rise2 += rise;
|
| 360 |
|
|
}
|
| 361 |
|
|
}
|
| 362 |
|
|
|
| 363 |
|
|
|
| 364 |
|
|
writemap(fp) /* write out bitmap */
|
| 365 |
|
|
FILE *fp;
|
| 366 |
|
|
{
|
| 367 |
|
|
COLR *scanout;
|
| 368 |
|
|
int y;
|
| 369 |
|
|
register int x;
|
| 370 |
|
|
register int inglyph;
|
| 371 |
|
|
|
| 372 |
|
|
fprintf(fp, "-Y %d +X %d\n", ysiz, xsiz);
|
| 373 |
|
|
|
| 374 |
|
|
scanout = (COLR *)malloc(xsiz*sizeof(COLR));
|
| 375 |
|
|
if (scanout == NULL) {
|
| 376 |
|
|
fprintf(stderr, "out of memory in writemap\n");
|
| 377 |
|
|
exit(1);
|
| 378 |
|
|
}
|
| 379 |
|
|
for (y = ysiz-1; y >= 0; y--) {
|
| 380 |
|
|
inglyph = 0;
|
| 381 |
|
|
for (x = 0; x < xsiz; x++) {
|
| 382 |
|
|
if (tstbit(x, y))
|
| 383 |
|
|
inglyph ^= 1;
|
| 384 |
|
|
if (inglyph)
|
| 385 |
|
|
copycolr(scanout[x], fgcolr);
|
| 386 |
|
|
else
|
| 387 |
|
|
copycolr(scanout[x], bgcolr);
|
| 388 |
|
|
}
|
| 389 |
|
|
if (fwritecolrs(scanout, xsiz, fp) < 0) {
|
| 390 |
|
|
fprintf(stderr, "write error in writemap\n");
|
| 391 |
|
|
exit(1);
|
| 392 |
|
|
}
|
| 393 |
|
|
}
|
| 394 |
|
|
free((char *)scanout);
|
| 395 |
|
|
}
|