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.1 by greg, Thu Feb 2 10:49:28 1989 UTC vs.
Revision 2.8 by greg, Mon Jun 22 13:59:53 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1987 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# 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 < char  *fontfile = "/usr/local/lib/ray/helvet.fnt";      /* our font file */
25 > #ifndef  DEFPATH
26 > #define  DEFPATH                ":/usr/local/lib/ray"
27 > #endif
28 > #ifndef  ULIBVAR
29 > #define  ULIBVAR                "RAYPATH"
30 > #endif
31  
32 < COLR  bgcolr = WHTCOLR;                 /* background color */
22 < COLR  fgcolr = BLKCOLR;                 /* foreground color */
32 > char  *fontfile = "helvet.fnt";         /* our font file */
33  
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 */
# Line 37 | Line 51 | int  xdim;                             /* size of horizontal scan (bytes) */
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 < char  *malloc(), *calloc();
68 > extern char  *getenv();
69  
70  
71   main(argc, argv)
72   int  argc;
73   char  *argv[];
74   {
59        double  atof();
75          int  an;
76  
77          for (an = 1; an < argc && argv[an][0] == '-'; an++)
# Line 64 | 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 95 | 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]);
117                          break;
118 +                case 's':                       /* spacing */
119 +                        spacing = atof(argv[++an]);
120 +                        break;
121                  default:;
122   unkopt:
123                          fprintf(stderr, "%s: unknown option: %s\n",
124                                          argv[0], argv[an]);
125                          exit(1);
126                  }
127 +                                        /* load font file */
128 +        if ((libpath = getenv(ULIBVAR)) == NULL)
129 +                libpath = DEFPATH;
130 +        ourfont = getfont(fontfile);
131                                          /* get text */
132          if (an == argc)
133                  gettext(stdin);
# Line 114 | Line 136 | unkopt:
136  
137                                          /* create bit map */
138          makemap();
117                                        /* load font file */
118        loadfont();
139                                          /* convert text to bitmap */
140          maptext();
141                                          /* print header */
142          printargs(argc, argv, stdout);
143 <        printf("\n\n");
143 >        fputformat(COLRFMT, stdout);
144 >        putchar('\n');
145                                          /* write out bitmap */
146          writemap(stdout);
147  
# Line 132 | Line 153 | makemap()                      /* create the bit map */
153   {
154          cwidth = cheight/aspect + 0.5;
155          if (direct == 'r' || direct == 'l') {
156 <                xsiz = maxline*cwidth;
156 >                xsiz = (long)maxwidth*cwidth >> 8;
157                  ysiz = nlines*cheight;
158          } else {                        /* reverse orientation */
159                  xsiz = nlines*cheight;
160 <                ysiz = maxline*cwidth;
160 >                ysiz = (long)maxwidth*cwidth >> 8;
161          }
162 +        if (xsiz % SSS)
163 +                xsiz += SSS - xsiz%SSS;
164 +        if (ysiz % SSS)
165 +                ysiz += SSS - ysiz%SSS;
166          xdim = (xsiz+7)/8;
167 <        ourbitmap = (BYTE *)calloc(ysiz, xdim);
168 <        if (ourbitmap == NULL) {
169 <                fprintf(stderr, "out of memory in makemap\n");
170 <                exit(1);
146 <        }
167 >        ourbitmap = (BYTE *)bmalloc(ysiz*xdim);
168 >        if (ourbitmap == NULL)
169 >                error(SYSTEM, "Out of memory in makemap");
170 >        bzero((char *)ourbitmap, ysiz*xdim);
171   }
172  
173  
150 loadfont()                      /* load the font file */
151 {
152        FILE  *fp;
153        char  *err;
154        int  gn, ngv, gv;
155        register GLYPH  *g;
156
157        if ((fp = fopen(fontfile, "r")) == NULL) {
158                fprintf(stderr, "cannot open font file \"%s\"\n",
159                                fontfile);
160                exit(1);
161        }
162        while (fscanf(fp, "%d", &gn) == 1) {    /* get each glyph */
163                if (gn < 0 || gn > 127) {
164                        err = "illegal";
165                        goto fonterr;
166                }
167                if (ourfont[gn] != NULL) {
168                        err = "duplicate";
169                        goto fonterr;
170                }
171                if (fscanf(fp, "%d", &ngv) != 1 ||
172                                ngv < 0 || ngv > 255) {
173                        err = "bad # vertices for";
174                        goto fonterr;
175                }
176                g = (GLYPH *)malloc((2*ngv+1)*sizeof(GLYPH));
177                if (g == NULL)
178                        goto memerr;
179                ourfont[gn] = g;
180                *g++ = ngv;
181                ngv *= 2;
182                while (ngv--) {
183                        if (fscanf(fp, "%d", &gv) != 1 ||
184                                        gv < 0 || gv > 255) {
185                                err = "bad vertex for";
186                                goto fonterr;
187                        }
188                        *g++ = gv;
189                }
190        }
191        fclose(fp);
192        return;
193 fonterr:
194        fprintf(stderr, "%s character (%d) in font file \"%s\"\n",
195                        err, gn, fontfile);
196        exit(1);
197 memerr:
198        fprintf(stderr, "out of memory in loadfont\n");
199        exit(1);
200 }
201
202
174   gettext(fp)                     /* get text from a file */
175   FILE  *fp;
176   {
# Line 209 | Line 180 | FILE  *fp;
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;
225                if (len > maxline)
226                        maxline = len;
210                  nlines++;
211          }
212          return;
213   memerr:
214 <        fprintf(stderr, "out of memory in gettext\n");
232 <        exit(1);
214 >        error(SYSTEM, "Out of memory in gettext");
215   }
216  
217  
# Line 253 | Line 235 | char  *av[];
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");
260 <        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  
# Line 362 | Line 359 | int  run, rise;
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                  }
404          }
405          free((char *)scanout);
393 }
394
395
396 printargs(ac, av, fp)           /* print arguments to a file */
397 int  ac;
398 char  **av;
399 FILE  *fp;
400 {
401        while (ac-- > 0) {
402                fputs(*av++, fp);
403                putc(' ', fp);
404        }
406   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines