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.15 by greg, Fri Jun 4 17:02:48 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1992 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 < #define  MAXLINE                512     /* longest allowable line */
17 > #include  "font.h"
18  
19 + #include  "paths.h"
20 +
21 + #ifndef  SSS
22 + #define  SSS                    3       /* super-sample size */
23 + #endif
24 +
25 + #define  MAXLINE                512     /* longest allowable line */
26 +
27   char  *fontfile = "helvet.fnt";         /* our font file */
28  
29 < COLR  bgcolr = WHTCOLR;                 /* background color */
30 < COLR  fgcolr = BLKCOLR;                 /* foreground color */
29 > COLOR  bgcolor = WHTCOLOR;              /* background color */
30 > COLOR  fgcolor = BLKCOLOR;              /* foreground color */
31  
32   int  direct = 'r';                      /* direction (right, up, left, down) */
33  
34 < int  cheight = 32;                      /* character height */
35 < double  aspect = 1.67;                  /* height/width character aspect */
34 > int  cheight = 32*SSS;                  /* character height */
35 > double  aspect = 1.67;                  /* height/width character aspect */
36 > double  spacing = 0.0;                  /* character spacing */
37   int  cwidth;                            /* computed character width */
38  
39   unsigned char  *ourbitmap;              /* our output bitmap */
40   int  xsiz, ysiz;                        /* bitmap dimensions */
41   int  xdim;                              /* size of horizontal scan (bytes) */
42  
43 < #define  bitop(x,y,op)          (ourbitmap[(y)*xdim+((x)>>3)] op (1<<((x)&7)))
44 < #define  tstbit(x,y)            bitop(x,y,&)
45 < #define  setbit(x,y)            bitop(x,y,|=)
46 < #define  clrbit(x,y)            bitop(x,y,&=~)
47 < #define  tglbit(x,y)            bitop(x,y,^=)
43 > #define  bitop(x,y,op)          (ourbitmap[(y)*xdim+((x)>>3)] op (1<<((x)&7)))
44 > #define  tstbit(x,y)            bitop(x,y,&)
45 > #define  setbit(x,y)            bitop(x,y,|=)
46 > #define  clrbit(x,y)            bitop(x,y,&=~)
47 > #define  tglbit(x,y)            bitop(x,y,^=)
48  
49 < typedef unsigned char  GLYPH;
49 > FONT  *ourfont;                         /* our font */
50  
51 < GLYPH  *ourfont[128];                   /* our font */
51 > char  *libpath;                         /* library search path */
52  
53   typedef struct line {
54          char  *s;               /* line w/o LF */
55 +        short  *sp;             /* character spacing */
56          struct line  *next;     /* next line up */
57   } LINE;
58  
59   LINE  *ourtext;                         /* our text */
60   int  nlines, maxline;                   /* text dimensions */
61 + int  maxwidth;                          /* maximum line width (dvi) */
62  
52 extern char  *malloc(), *calloc();
53 extern FILE  *fropen();
63  
55
64   main(argc, argv)
65   int  argc;
66   char  *argv[];
67   {
60        double  atof();
68          int  an;
69 <
69 > #ifdef MSDOS
70 >        setmode(fileno(stdout), O_BINARY);
71 > #endif
72          for (an = 1; an < argc && argv[an][0] == '-'; an++)
73                  switch (argv[an][1]) {
74                  case 'c':                       /* color */
75                          switch (argv[an][2]) {
76                          case 'f':                       /* foreground */
77 <                                setcolr(fgcolr, atof(argv[an+1]),
77 >                                setcolor(fgcolor, atof(argv[an+1]),
78                                                  atof(argv[an+2]),
79                                                  atof(argv[an+3]));
80                                  an += 3;
81                                  break;
82                          case 'b':                       /* background */
83 <                                setcolr(bgcolr, atof(argv[an+1]),
83 >                                setcolor(bgcolor, atof(argv[an+1]),
84                                                  atof(argv[an+2]),
85                                                  atof(argv[an+3]));
86                                  an += 3;
# Line 95 | Line 104 | char  *argv[];
104                                  goto unkopt;
105                          }
106                          break;
107 +                case 'x':                       /* x resolution */
108 +                        xsiz = atoi(argv[++an])*SSS;
109 +                        break;
110 +                case 'y':
111 +                        ysiz = atoi(argv[++an])*SSS;
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);
# Line 115 | Line 137 | unkopt:
137  
138                                          /* create bit map */
139          makemap();
118                                        /* load font file */
119        loadfont();
140                                          /* convert text to bitmap */
141          maptext();
142                                          /* print header */
# Line 132 | Line 152 | unkopt:
152  
153   makemap()                       /* create the bit map */
154   {
155 <        cwidth = cheight/aspect + 0.5;
155 >        double  pictaspect;
156 >        
157          if (direct == 'r' || direct == 'l') {
158 <                xsiz = maxline*cwidth;
159 <                ysiz = nlines*cheight;
158 >                if (xsiz <= 0) {
159 >                        cwidth = cheight/aspect + 0.5;
160 >                        xsiz = (long)maxwidth*cwidth >> 8;
161 >                        ysiz = nlines*cheight;
162 >                } else if (aspect > FTINY) {
163 >                        if (ysiz <= 0)
164 >                                ysiz = cheight*nlines;
165 >                        pictaspect = 256*nlines*aspect/maxwidth;
166 >                        if (pictaspect*xsiz < ysiz)
167 >                                ysiz = pictaspect*xsiz + 0.5;
168 >                        else
169 >                                xsiz = ysiz/pictaspect + 0.5;
170 >                        cheight = ysiz/nlines;
171 >                        cwidth = cheight/aspect;
172 >                } else {
173 >                        if (ysiz <= 0)
174 >                                ysiz = cheight*nlines;
175 >                        pictaspect = (double)ysiz/xsiz;
176 >                        aspect = pictaspect*maxwidth/(256*nlines);
177 >                        cheight = ysiz/nlines;
178 >                        cwidth = cheight/aspect;
179 >                }
180          } else {                        /* reverse orientation */
181 <                xsiz = nlines*cheight;
182 <                ysiz = maxline*cwidth;
181 >                if (ysiz <= 0) {
182 >                        cwidth = cheight/aspect + 0.5;
183 >                        xsiz = nlines*cheight;
184 >                        ysiz = (long)maxwidth*cwidth >> 8;
185 >                } else if (aspect > FTINY) {
186 >                        if (xsiz <= 0)
187 >                                xsiz = cheight*nlines;
188 >                        pictaspect = maxwidth/(256*nlines*aspect);
189 >                        if (pictaspect*xsiz < ysiz)
190 >                                ysiz = pictaspect*xsiz + 0.5;
191 >                        else
192 >                                xsiz = ysiz/pictaspect + 0.5;
193 >                        cheight = xsiz/nlines;
194 >                        cwidth = cheight/aspect;
195 >                } else {
196 >                        if (xsiz <= 0)
197 >                                xsiz = cheight*nlines;
198 >                        pictaspect = (double)ysiz/xsiz;
199 >                        aspect = maxwidth/(256*nlines*pictaspect);
200 >                        cheight = xsiz/nlines;
201 >                        cwidth = cheight/aspect;
202 >                }
203          }
204 +        if (xsiz % SSS)
205 +                xsiz += SSS - xsiz%SSS;
206 +        if (ysiz % SSS)
207 +                ysiz += SSS - ysiz%SSS;
208          xdim = (xsiz+7)/8;
209 <        ourbitmap = (BYTE *)calloc(ysiz, xdim);
210 <        if (ourbitmap == NULL) {
211 <                fprintf(stderr, "out of memory in makemap\n");
212 <                exit(1);
148 <        }
209 >        ourbitmap = (BYTE *)bmalloc(ysiz*xdim);
210 >        if (ourbitmap == NULL)
211 >                error(SYSTEM, "Out of memory in makemap");
212 >        bzero((char *)ourbitmap, ysiz*xdim);
213   }
214  
215  
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
216   gettext(fp)                     /* get text from a file */
217   FILE  *fp;
218   {
208        char  *fgets();
219          char  buf[MAXLINE];
220          register LINE  *curl;
221          int  len;
222  
223          maxline = 0;
224 +        maxwidth = 0;
225          nlines = 0;
226          while (fgets(buf, MAXLINE, fp) != NULL) {
227                  curl = (LINE *)malloc(sizeof(LINE));
228                  if (curl == NULL)
229                          goto memerr;
230                  len = strlen(buf);
231 <                curl->s = malloc(len--);
232 <                if (curl->s == NULL)
231 >                curl->s = malloc(len);
232 >                curl->sp = (short *)malloc(sizeof(short)*len--);
233 >                if (curl->s == NULL | curl->sp == NULL)
234                          goto memerr;
235 +                if (len > maxline)
236 +                        maxline = len;
237                  strncpy(curl->s, buf, len);
238                  curl->s[len] = '\0';
239 +                if (spacing < -1./256.)
240 +                        len = squeeztext(curl->sp, curl->s, ourfont,
241 +                                        (int)(spacing*-256.));
242 +                else if (spacing > 1./256.)
243 +                        len = proptext(curl->sp, curl->s, ourfont,
244 +                                        (int)(spacing*256.), 3);
245 +                else
246 +                        len = uniftext(curl->sp, curl->s, ourfont);
247 +                if (len > maxwidth)
248 +                        maxwidth = len;
249                  curl->next = ourtext;
250                  ourtext = curl;
227                if (len > maxline)
228                        maxline = len;
251                  nlines++;
252          }
253          return;
254   memerr:
255 <        fprintf(stderr, "out of memory in gettext\n");
234 <        exit(1);
255 >        error(SYSTEM, "Out of memory in gettext");
256   }
257  
258  
# Line 255 | Line 276 | char  *av[];
276          *--cp = '\0';
277          ourtext->next = NULL;
278          maxline = strlen(ourtext->s);
279 +        ourtext->sp = (short *)malloc(sizeof(short)*(maxline+1));
280 +        if (ourtext->sp == NULL)
281 +                goto memerr;
282 +        if (spacing < -1./256.)
283 +                maxwidth = squeeztext(ourtext->sp, ourtext->s, ourfont,
284 +                                (int)(spacing*-256.));
285 +        else if (spacing > 1./256.)
286 +                maxwidth = proptext(ourtext->sp, ourtext->s, ourfont,
287 +                                (int)(spacing*256.), 3);
288 +        else
289 +                maxwidth = uniftext(ourtext->sp, ourtext->s, ourfont);
290          nlines = 1;
291          return;
292   memerr:
293 <        fprintf(stderr, "out of memory in arg_text\n");
262 <        exit(1);
293 >        error(SYSTEM, "Out of memory in arg_text");
294   }
295  
296  
297   maptext()                       /* map our text */
298   {
299          register LINE  *curl;
300 <        int  l;
301 <        register int  c;
300 >        int  l, len;
301 >        register int  i, c;
302  
303 <        for (l = 0, curl = ourtext; curl != NULL; l++, curl = curl->next)
304 <                for (c = strlen(curl->s)-1; c >= 0; c--)
305 <                        mapglyph(ourfont[curl->s[c]], c, l);
303 >        for (l = 0, curl = ourtext; curl != NULL; l += 256, curl = curl->next) {
304 >                len = strlen(curl->s); c = 0;
305 >                for (i = 0; i < len; i++) {
306 >                        c += curl->sp[i];
307 >                        mapglyph(ourfont->fg[curl->s[i]&0xff], c, l);
308 >                }
309 >        }
310   }
311  
312  
313   mapglyph(gl, tx0, ty0)          /* convert a glyph */
314 < register GLYPH  *gl;
314 > GLYPH  *gl;
315   int  tx0, ty0;
316   {
317          int  n;
318 +        register GORD  *gp;
319          int  p0[2], p1[2];
320  
321          if (gl == NULL)
322                  return;
323  
324 <        tx0 <<= 8; ty0 <<= 8;
325 <        n = *gl++;
326 <        mapcoord(p0, gl[2*n-2]+tx0, gl[2*n-1]+ty0);
324 >        n = gl->nverts;
325 >        gp = gvlist(gl);
326 >        mapcoord(p0, gp[2*n-2]+tx0, gp[2*n-1]+ty0);
327          while (n--) {
328 <                mapcoord(p1, gl[0]+tx0, gl[1]+ty0);
328 >                mapcoord(p1, gp[0]+tx0, gp[1]+ty0);
329                  mapedge(p0[0], p0[1], p1[0]-p0[0], p1[1]-p0[1]);
330                  p0[0] = p1[0]; p0[1] = p1[1];
331 <                gl += 2;
331 >                gp += 2;
332          }
333   }
334  
# Line 364 | Line 400 | int  run, rise;
400   writemap(fp)                    /* write out bitmap */
401   FILE  *fp;
402   {
403 +        COLR  pixval[SSS*SSS+1];        /* possible pixel values */
404 +        COLOR  ctmp0, ctmp1;
405 +        double  d;
406          COLR  *scanout;
407 <        int  y;
408 <        register int  x;
407 >        int  x, y;
408 >        register int  i, j;
409 >        int  cnt;
410          register int  inglyph;
411  
412 <        fprintf(fp, "-Y %d +X %d\n", ysiz, xsiz);
412 >        fprintf(fp, "-Y %d +X %d\n", ysiz/SSS, xsiz/SSS);
413  
414 <        scanout = (COLR *)malloc(xsiz*sizeof(COLR));
415 <        if (scanout == NULL) {
416 <                fprintf(stderr, "out of memory in writemap\n");
417 <                exit(1);
414 >        scanout = (COLR *)malloc(xsiz/SSS*sizeof(COLR));
415 >        if (scanout == NULL)
416 >                error(SYSTEM, "Out of memory in writemap");
417 >        for (i = 0; i <= SSS*SSS; i++) {        /* compute possible values */
418 >                copycolor(ctmp0, fgcolor);
419 >                d = (double)i/(SSS*SSS);
420 >                scalecolor(ctmp0, d);
421 >                copycolor(ctmp1, bgcolor);
422 >                d = 1.0 - d;
423 >                scalecolor(ctmp1, d);
424 >                addcolor(ctmp0, ctmp1);
425 >                setcolr(pixval[i], colval(ctmp0,RED),
426 >                                colval(ctmp0,GRN), colval(ctmp0,BLU));
427          }
428 <        for (y = ysiz-1; y >= 0; y--) {
428 >        for (y = ysiz/SSS-1; y >= 0; y--) {
429                  inglyph = 0;
430 <                for (x = 0; x < xsiz; x++) {
431 <                        if (tstbit(x, y))
432 <                                inglyph ^= 1;
433 <                        if (inglyph)
434 <                                copycolr(scanout[x], fgcolr);
435 <                        else
436 <                                copycolr(scanout[x], bgcolr);
430 >                for (x = 0; x < xsiz/SSS; x++) {
431 >                        cnt = 0;
432 >                        for (j = 0; j < SSS; j++)
433 >                                for (i = 0; i < SSS; i++) {
434 >                                        if (tstbit(x*SSS+i, y*SSS+j))
435 >                                                inglyph ^= 1<<j;
436 >                                        if (inglyph & 1<<j)
437 >                                                cnt++;
438 >                                }
439 >                        copycolr(scanout[x], pixval[cnt]);
440                  }
441 <                if (fwritecolrs(scanout, xsiz, fp) < 0) {
441 >                if (fwritecolrs(scanout, xsiz/SSS, fp) < 0) {
442                          fprintf(stderr, "write error in writemap\n");
443                          exit(1);
444                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines