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 2.1 by greg, Tue Nov 12 16:04:19 1991 UTC vs.
Revision 2.10 by greg, Tue Jul 7 18:24:34 1992 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 + #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 */
# 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 < extern char  *malloc(), *calloc();
53 < extern FILE  *fropen();
68 > extern char  *getenv();
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 95 | Line 109 | char  *argv[];
109                                  goto unkopt;
110                          }
111                          break;
112 +                case 'x':                       /* x resolution */
113 +                        xsiz = atoi(argv[++an])*SSS;
114 +                        break;
115 +                case 'y':
116 +                        ysiz = atoi(argv[++an])*SSS;
117 +                        break;
118                  case 'h':                       /* height of characters */
119 <                        cheight = atoi(argv[++an]);
119 >                        cheight = atoi(argv[++an])*SSS;
120 >                        xsiz = xsiz = 0;
121                          break;
122                  case 'a':                       /* aspect ratio */
123                          aspect = atof(argv[++an]);
124                          break;
125 +                case 's':                       /* spacing */
126 +                        spacing = atof(argv[++an]);
127 +                        break;
128                  default:;
129   unkopt:
130                          fprintf(stderr, "%s: unknown option: %s\n",
131                                          argv[0], argv[an]);
132                          exit(1);
133                  }
134 +                                        /* load font file */
135 +        if ((libpath = getenv(ULIBVAR)) == NULL)
136 +                libpath = DEFPATH;
137 +        ourfont = getfont(fontfile);
138                                          /* get text */
139          if (an == argc)
140                  gettext(stdin);
# Line 115 | Line 143 | unkopt:
143  
144                                          /* create bit map */
145          makemap();
118                                        /* load font file */
119        loadfont();
146                                          /* convert text to bitmap */
147          maptext();
148                                          /* print header */
# Line 132 | Line 158 | unkopt:
158  
159   makemap()                       /* create the bit map */
160   {
161 <        cwidth = cheight/aspect + 0.5;
161 >        double  pictaspect;
162 >        
163          if (direct == 'r' || direct == 'l') {
164 <                xsiz = maxline*cwidth;
165 <                ysiz = nlines*cheight;
164 >                if (xsiz == 0 || ysiz == 0) {
165 >                        cwidth = cheight/aspect + 0.5;
166 >                        xsiz = (long)maxwidth*cwidth >> 8;
167 >                        ysiz = nlines*cheight;
168 >                } else if (aspect > FTINY) {
169 >                        pictaspect = 256*nlines*aspect/maxwidth;
170 >                        if (pictaspect*xsiz < ysiz)
171 >                                ysiz = pictaspect*xsiz + 0.5;
172 >                        else
173 >                                xsiz = ysiz/pictaspect + 0.5;
174 >                        cheight = ysiz/nlines;
175 >                        cwidth = cheight/aspect + 0.5;
176 >                } else {
177 >                        pictaspect = (double)ysiz/xsiz;
178 >                        aspect = pictaspect*maxwidth/(256*nlines);
179 >                        cheight = ysiz/nlines;
180 >                        cwidth = cheight/aspect + 0.5;
181 >                }
182          } else {                        /* reverse orientation */
183 <                xsiz = nlines*cheight;
184 <                ysiz = maxline*cwidth;
183 >                if (xsiz == 0 || ysiz == 0) {
184 >                        cwidth = cheight/aspect + 0.5;
185 >                        xsiz = nlines*cheight;
186 >                        ysiz = (long)maxwidth*cwidth >> 8;
187 >                } else if (aspect > FTINY) {
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 + 0.5;
195 >                } else {
196 >                        pictaspect = (double)ysiz/xsiz;
197 >                        aspect = maxwidth/(256*nlines*pictaspect);
198 >                        cheight = xsiz/nlines;
199 >                        cwidth = cheight/aspect + 0.5;
200 >                }
201          }
202 +        if (xsiz % SSS)
203 +                xsiz += SSS - xsiz%SSS;
204 +        if (ysiz % SSS)
205 +                ysiz += SSS - ysiz%SSS;
206          xdim = (xsiz+7)/8;
207 <        ourbitmap = (BYTE *)calloc(ysiz, xdim);
208 <        if (ourbitmap == NULL) {
209 <                fprintf(stderr, "out of memory in makemap\n");
210 <                exit(1);
148 <        }
207 >        ourbitmap = (BYTE *)bmalloc(ysiz*xdim);
208 >        if (ourbitmap == NULL)
209 >                error(SYSTEM, "Out of memory in makemap");
210 >        bzero((char *)ourbitmap, ysiz*xdim);
211   }
212  
213  
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
214   gettext(fp)                     /* get text from a file */
215   FILE  *fp;
216   {
# Line 211 | Line 220 | FILE  *fp;
220          int  len;
221  
222          maxline = 0;
223 +        maxwidth = 0;
224          nlines = 0;
225          while (fgets(buf, MAXLINE, fp) != NULL) {
226                  curl = (LINE *)malloc(sizeof(LINE));
227                  if (curl == NULL)
228                          goto memerr;
229                  len = strlen(buf);
230 <                curl->s = malloc(len--);
231 <                if (curl->s == NULL)
230 >                curl->s = malloc(len);
231 >                curl->sp = (short *)malloc(sizeof(short)*len--);
232 >                if (curl->s == NULL | curl->sp == NULL)
233                          goto memerr;
234 +                if (len > maxline)
235 +                        maxline = len;
236                  strncpy(curl->s, buf, len);
237                  curl->s[len] = '\0';
238 +                if (spacing < -1./256.)
239 +                        len = squeeztext(curl->sp, curl->s, ourfont,
240 +                                        (int)(spacing*-256.0));
241 +                else if (spacing > 1./256.)
242 +                        len = proptext(curl->sp, curl->s, ourfont,
243 +                                        (int)(spacing*256.0), 3);
244 +                else
245 +                        len = uniftext(curl->sp, curl->s, ourfont);
246 +                if (len > maxwidth)
247 +                        maxwidth = len;
248                  curl->next = ourtext;
249                  ourtext = curl;
227                if (len > maxline)
228                        maxline = len;
250                  nlines++;
251          }
252          return;
253   memerr:
254 <        fprintf(stderr, "out of memory in gettext\n");
234 <        exit(1);
254 >        error(SYSTEM, "Out of memory in gettext");
255   }
256  
257  
# Line 255 | Line 275 | char  *av[];
275          *--cp = '\0';
276          ourtext->next = NULL;
277          maxline = strlen(ourtext->s);
278 +        ourtext->sp = (short *)malloc(sizeof(short)*(maxline+1));
279 +        if (ourtext->sp == NULL)
280 +                goto memerr;
281 +        if (spacing < 0.0)
282 +                maxwidth = squeeztext(ourtext->sp, ourtext->s, ourfont,
283 +                                (int)(spacing*-256.0));
284 +        else if (spacing > 0.0)
285 +                maxwidth = proptext(ourtext->sp, ourtext->s, ourfont,
286 +                                (int)(spacing*256.0), 3);
287 +        else
288 +                maxwidth = uniftext(ourtext->sp, ourtext->s, ourfont);
289          nlines = 1;
290          return;
291   memerr:
292 <        fprintf(stderr, "out of memory in arg_text\n");
262 <        exit(1);
292 >        error(SYSTEM, "Out of memory in arg_text");
293   }
294  
295  
296   maptext()                       /* map our text */
297   {
298          register LINE  *curl;
299 <        int  l;
300 <        register int  c;
299 >        int  l, len;
300 >        register int  i, c;
301  
302 <        for (l = 0, curl = ourtext; curl != NULL; l++, curl = curl->next)
303 <                for (c = strlen(curl->s)-1; c >= 0; c--)
304 <                        mapglyph(ourfont[curl->s[c]], c, l);
302 >        for (l = 0, curl = ourtext; curl != NULL; l += 256, curl = curl->next) {
303 >                len = strlen(curl->s); c = 0;
304 >                for (i = 0; i < len; i++) {
305 >                        c += curl->sp[i];
306 >                        mapglyph(ourfont->fg[curl->s[i]&0xff], c, l);
307 >                }
308 >        }
309   }
310  
311  
312   mapglyph(gl, tx0, ty0)          /* convert a glyph */
313 < register GLYPH  *gl;
313 > GLYPH  *gl;
314   int  tx0, ty0;
315   {
316          int  n;
317 +        register GORD  *gp;
318          int  p0[2], p1[2];
319  
320          if (gl == NULL)
321                  return;
322  
323 <        tx0 <<= 8; ty0 <<= 8;
324 <        n = *gl++;
325 <        mapcoord(p0, gl[2*n-2]+tx0, gl[2*n-1]+ty0);
323 >        n = gl->nverts;
324 >        gp = gvlist(gl);
325 >        mapcoord(p0, gp[2*n-2]+tx0, gp[2*n-1]+ty0);
326          while (n--) {
327 <                mapcoord(p1, gl[0]+tx0, gl[1]+ty0);
327 >                mapcoord(p1, gp[0]+tx0, gp[1]+ty0);
328                  mapedge(p0[0], p0[1], p1[0]-p0[0], p1[1]-p0[1]);
329                  p0[0] = p1[0]; p0[1] = p1[1];
330 <                gl += 2;
330 >                gp += 2;
331          }
332   }
333  
# Line 364 | Line 399 | int  run, rise;
399   writemap(fp)                    /* write out bitmap */
400   FILE  *fp;
401   {
402 +        COLR  pixval[SSS*SSS+1];        /* possible pixel values */
403 +        COLOR  ctmp0, ctmp1;
404 +        double  d;
405          COLR  *scanout;
406 <        int  y;
407 <        register int  x;
406 >        int  x, y;
407 >        register int  i, j;
408 >        int  cnt;
409          register int  inglyph;
410  
411 <        fprintf(fp, "-Y %d +X %d\n", ysiz, xsiz);
411 >        fprintf(fp, "-Y %d +X %d\n", ysiz/SSS, xsiz/SSS);
412  
413 <        scanout = (COLR *)malloc(xsiz*sizeof(COLR));
414 <        if (scanout == NULL) {
415 <                fprintf(stderr, "out of memory in writemap\n");
416 <                exit(1);
413 >        scanout = (COLR *)malloc(xsiz/SSS*sizeof(COLR));
414 >        if (scanout == NULL)
415 >                error(SYSTEM, "Out of memory in writemap");
416 >        for (i = 0; i <= SSS*SSS; i++) {        /* compute possible values */
417 >                copycolor(ctmp0, fgcolor);
418 >                d = (double)i/(SSS*SSS);
419 >                scalecolor(ctmp0, d);
420 >                copycolor(ctmp1, bgcolor);
421 >                d = 1.0 - d;
422 >                scalecolor(ctmp1, d);
423 >                addcolor(ctmp0, ctmp1);
424 >                setcolr(pixval[i], colval(ctmp0,RED),
425 >                                colval(ctmp0,GRN), colval(ctmp0,BLU));
426          }
427 <        for (y = ysiz-1; y >= 0; y--) {
427 >        for (y = ysiz/SSS-1; y >= 0; y--) {
428                  inglyph = 0;
429 <                for (x = 0; x < xsiz; x++) {
430 <                        if (tstbit(x, y))
431 <                                inglyph ^= 1;
432 <                        if (inglyph)
433 <                                copycolr(scanout[x], fgcolr);
434 <                        else
435 <                                copycolr(scanout[x], bgcolr);
429 >                for (x = 0; x < xsiz/SSS; x++) {
430 >                        cnt = 0;
431 >                        for (j = 0; j < SSS; j++)
432 >                                for (i = 0; i < SSS; i++) {
433 >                                        if (tstbit(x*SSS+i, y*SSS+j))
434 >                                                inglyph ^= 1<<j;
435 >                                        if (inglyph & 1<<j)
436 >                                                cnt++;
437 >                                }
438 >                        copycolr(scanout[x], pixval[cnt]);
439                  }
440 <                if (fwritecolrs(scanout, xsiz, fp) < 0) {
440 >                if (fwritecolrs(scanout, xsiz/SSS, fp) < 0) {
441                          fprintf(stderr, "write error in writemap\n");
442                          exit(1);
443                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines