ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/psign.c
(Generate patch)

Comparing ray/src/px/psign.c (file contents):
Revision 1.3 by greg, Thu May 30 08:22:52 1991 UTC vs.
Revision 2.5 by greg, Tue Jun 16 16:27:37 1992 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
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   int  cwidth;                            /* computed character width */
42  
# Line 37 | Line 50 | int  xdim;                             /* size of horizontal scan (bytes) */
50   #define  clrbit(x,y)            bitop(x,y,&=~)
51   #define  tglbit(x,y)            bitop(x,y,^=)
52  
53 < typedef unsigned char  GLYPH;
53 > FONT  *ourfont;                         /* our font */
54  
55 < GLYPH  *ourfont[128];                   /* our font */
55 > char  *libpath;                         /* library search path */
56  
57   typedef struct line {
58          char  *s;               /* line w/o LF */
59 +        short  *sp;             /* character spacing */
60          struct line  *next;     /* next line up */
61   } LINE;
62  
63   LINE  *ourtext;                         /* our text */
64   int  nlines, maxline;                   /* text dimensions */
65 + int  maxwidth;                          /* maximum line width (dvi) */
66  
67 + extern char  *getenv();
68   extern char  *malloc(), *calloc();
53 extern FILE  *fropen();
69  
70  
71   main(argc, argv)
72   int  argc;
73   char  *argv[];
74   {
60        double  atof();
75          int  an;
76  
77          for (an = 1; an < argc && argv[an][0] == '-'; an++)
# Line 65 | Line 79 | char  *argv[];
79                  case 'c':                       /* color */
80                          switch (argv[an][2]) {
81                          case 'f':                       /* foreground */
82 <                                setcolr(fgcolr, atof(argv[an+1]),
82 >                                setcolor(fgcolor, atof(argv[an+1]),
83                                                  atof(argv[an+2]),
84                                                  atof(argv[an+3]));
85                                  an += 3;
86                                  break;
87                          case 'b':                       /* background */
88 <                                setcolr(bgcolr, atof(argv[an+1]),
88 >                                setcolor(bgcolor, atof(argv[an+1]),
89                                                  atof(argv[an+2]),
90                                                  atof(argv[an+3]));
91                                  an += 3;
# Line 96 | Line 110 | char  *argv[];
110                          }
111                          break;
112                  case 'h':                       /* height of characters */
113 <                        cheight = atoi(argv[++an]);
113 >                        cheight = atoi(argv[++an])*SSS;
114                          break;
115                  case 'a':                       /* aspect ratio */
116                          aspect = atof(argv[++an]);
# Line 107 | Line 121 | unkopt:
121                                          argv[0], argv[an]);
122                          exit(1);
123                  }
124 +                                        /* load font file */
125 +        if ((libpath = getenv(ULIBVAR)) == NULL)
126 +                libpath = DEFPATH;
127 +        ourfont = getfont(fontfile);
128                                          /* get text */
129          if (an == argc)
130                  gettext(stdin);
# Line 115 | Line 133 | unkopt:
133  
134                                          /* create bit map */
135          makemap();
118                                        /* load font file */
119        loadfont();
136                                          /* convert text to bitmap */
137          maptext();
138                                          /* print header */
# Line 134 | Line 150 | makemap()                      /* create the bit map */
150   {
151          cwidth = cheight/aspect + 0.5;
152          if (direct == 'r' || direct == 'l') {
153 <                xsiz = maxline*cwidth;
153 >                xsiz = (long)maxwidth*cwidth >> 8;
154                  ysiz = nlines*cheight;
155          } else {                        /* reverse orientation */
156                  xsiz = nlines*cheight;
157 <                ysiz = maxline*cwidth;
157 >                ysiz = (long)maxwidth*cwidth >> 8;
158          }
159 +        if (xsiz % SSS)
160 +                xsiz += SSS - xsiz%SSS;
161 +        if (ysiz % SSS)
162 +                ysiz += SSS - ysiz%SSS;
163          xdim = (xsiz+7)/8;
164          ourbitmap = (BYTE *)calloc(ysiz, xdim);
165 <        if (ourbitmap == NULL) {
166 <                fprintf(stderr, "out of memory in makemap\n");
147 <                exit(1);
148 <        }
165 >        if (ourbitmap == NULL)
166 >                error(SYSTEM, "Out of memory in makemap");
167   }
168  
169  
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
170   gettext(fp)                     /* get text from a file */
171   FILE  *fp;
172   {
# Line 211 | Line 176 | FILE  *fp;
176          int  len;
177  
178          maxline = 0;
179 +        maxwidth = 0;
180          nlines = 0;
181          while (fgets(buf, MAXLINE, fp) != NULL) {
182                  curl = (LINE *)malloc(sizeof(LINE));
183                  if (curl == NULL)
184                          goto memerr;
185                  len = strlen(buf);
186 <                curl->s = malloc(len--);
187 <                if (curl->s == NULL)
186 >                curl->s = malloc(len);
187 >                curl->sp = (short *)malloc(sizeof(short)*len--);
188 >                if (curl->s == NULL | curl->sp == NULL)
189                          goto memerr;
190 +                if (len > maxline)
191 +                        maxline = len;
192                  strncpy(curl->s, buf, len);
193                  curl->s[len] = '\0';
194 + len = uniftext(curl->sp, curl->s, ourfont);
195 +                if (len > maxwidth)
196 +                        maxwidth = len;
197                  curl->next = ourtext;
198                  ourtext = curl;
227                if (len > maxline)
228                        maxline = len;
199                  nlines++;
200          }
201          return;
202   memerr:
203 <        fprintf(stderr, "out of memory in gettext\n");
234 <        exit(1);
203 >        error(SYSTEM, "Out of memory in gettext");
204   }
205  
206  
# Line 255 | Line 224 | char  *av[];
224          *--cp = '\0';
225          ourtext->next = NULL;
226          maxline = strlen(ourtext->s);
227 +        ourtext->sp = (short *)malloc(sizeof(short)*(maxline+1));
228 +        if (ourtext->sp == NULL)
229 +                goto memerr;
230 + maxwidth = squeeztext(ourtext->sp, ourtext->s, ourfont, 50);
231          nlines = 1;
232          return;
233   memerr:
234 <        fprintf(stderr, "out of memory in arg_text\n");
262 <        exit(1);
234 >        error(SYSTEM, "Out of memory in arg_text");
235   }
236  
237  
238   maptext()                       /* map our text */
239   {
240          register LINE  *curl;
241 <        int  l;
242 <        register int  c;
241 >        int  l, len;
242 >        register int  i, c;
243  
244 <        for (l = 0, curl = ourtext; curl != NULL; l++, curl = curl->next)
245 <                for (c = strlen(curl->s)-1; c >= 0; c--)
246 <                        mapglyph(ourfont[curl->s[c]], c, l);
244 >        for (l = 0, curl = ourtext; curl != NULL; l += 256, curl = curl->next) {
245 >                len = strlen(curl->s); c = 0;
246 >                for (i = 0; i < len; i++) {
247 >                        c += curl->sp[i];
248 >                        mapglyph(ourfont->fg[curl->s[i]&0xff], c, l);
249 >                }
250 >        }
251   }
252  
253  
254   mapglyph(gl, tx0, ty0)          /* convert a glyph */
255 < register GLYPH  *gl;
255 > GLYPH  *gl;
256   int  tx0, ty0;
257   {
258          int  n;
259 +        register GORD  *gp;
260          int  p0[2], p1[2];
261  
262          if (gl == NULL)
263                  return;
264  
265 <        tx0 <<= 8; ty0 <<= 8;
266 <        n = *gl++;
267 <        mapcoord(p0, gl[2*n-2]+tx0, gl[2*n-1]+ty0);
265 >        n = gl->nverts;
266 >        gp = gvlist(gl);
267 >        mapcoord(p0, gp[2*n-2]+tx0, gp[2*n-1]+ty0);
268          while (n--) {
269 <                mapcoord(p1, gl[0]+tx0, gl[1]+ty0);
269 >                mapcoord(p1, gp[0]+tx0, gp[1]+ty0);
270                  mapedge(p0[0], p0[1], p1[0]-p0[0], p1[1]-p0[1]);
271                  p0[0] = p1[0]; p0[1] = p1[1];
272 <                gl += 2;
272 >                gp += 2;
273          }
274   }
275  
# Line 364 | Line 341 | int  run, rise;
341   writemap(fp)                    /* write out bitmap */
342   FILE  *fp;
343   {
344 +        COLR  pixval[SSS*SSS+1];        /* possible pixel values */
345 +        COLOR  ctmp0, ctmp1;
346 +        double  d;
347          COLR  *scanout;
348 <        int  y;
349 <        register int  x;
348 >        int  x, y;
349 >        register int  i, j;
350 >        int  cnt;
351          register int  inglyph;
352  
353 <        fprintf(fp, "-Y %d +X %d\n", ysiz, xsiz);
353 >        fprintf(fp, "-Y %d +X %d\n", ysiz/SSS, xsiz/SSS);
354  
355 <        scanout = (COLR *)malloc(xsiz*sizeof(COLR));
356 <        if (scanout == NULL) {
357 <                fprintf(stderr, "out of memory in writemap\n");
358 <                exit(1);
355 >        scanout = (COLR *)malloc(xsiz/SSS*sizeof(COLR));
356 >        if (scanout == NULL)
357 >                error(SYSTEM, "Out of memory in writemap");
358 >        for (i = 0; i <= SSS*SSS; i++) {        /* compute possible values */
359 >                copycolor(ctmp0, fgcolor);
360 >                d = (double)i/(SSS*SSS);
361 >                scalecolor(ctmp0, d);
362 >                copycolor(ctmp1, bgcolor);
363 >                d = 1.0 - d;
364 >                scalecolor(ctmp1, d);
365 >                addcolor(ctmp0, ctmp1);
366 >                setcolr(pixval[i], colval(ctmp0,RED),
367 >                                colval(ctmp0,GRN), colval(ctmp0,BLU));
368          }
369 <        for (y = ysiz-1; y >= 0; y--) {
369 >        for (y = ysiz/SSS-1; y >= 0; y--) {
370                  inglyph = 0;
371 <                for (x = 0; x < xsiz; x++) {
372 <                        if (tstbit(x, y))
373 <                                inglyph ^= 1;
374 <                        if (inglyph)
375 <                                copycolr(scanout[x], fgcolr);
376 <                        else
377 <                                copycolr(scanout[x], bgcolr);
371 >                for (x = 0; x < xsiz/SSS; x++) {
372 >                        cnt = 0;
373 >                        for (j = 0; j < SSS; j++)
374 >                                for (i = 0; i < SSS; i++) {
375 >                                        if (tstbit(x*SSS+i, y*SSS+j))
376 >                                                inglyph ^= 1<<j;
377 >                                        if (inglyph & 1<<j)
378 >                                                cnt++;
379 >                                }
380 >                        copycolr(scanout[x], pixval[cnt]);
381                  }
382 <                if (fwritecolrs(scanout, xsiz, fp) < 0) {
382 >                if (fwritecolrs(scanout, xsiz/SSS, fp) < 0) {
383                          fprintf(stderr, "write error in writemap\n");
384                          exit(1);
385                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines