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.19 by schorsch, Thu Jun 5 19:29:34 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  psign.c - produce picture from text.
6   *
7   *      7/1/87
8   */
9  
10 < #include  <stdio.h>
14 <
10 > #include  "standard.h"
11   #include  "color.h"
12 + #include  "font.h"
13  
14 < #define  MAXLINE                512     /* longest allowable line */
14 > #ifndef  SSS
15 > #define  SSS                    3       /* super-sample size */
16 > #endif
17  
18 + #define  MAXLINE                512     /* longest allowable line */
19 +
20   char  *fontfile = "helvet.fnt";         /* our font file */
21  
22 < COLR  bgcolr = WHTCOLR;                 /* background color */
23 < COLR  fgcolr = BLKCOLR;                 /* foreground color */
22 > COLOR  bgcolor = WHTCOLOR;              /* background color */
23 > COLOR  fgcolor = BLKCOLOR;              /* foreground color */
24  
25   int  direct = 'r';                      /* direction (right, up, left, down) */
26  
27 < int  cheight = 32;                      /* character height */
28 < double  aspect = 1.67;                  /* height/width character aspect */
27 > int  cheight = 32*SSS;                  /* character height */
28 > double  aspect = 1.67;                  /* height/width character aspect */
29 > double  spacing = 0.0;                  /* character spacing */
30   int  cwidth;                            /* computed character width */
31  
32   unsigned char  *ourbitmap;              /* our output bitmap */
33   int  xsiz, ysiz;                        /* bitmap dimensions */
34   int  xdim;                              /* size of horizontal scan (bytes) */
35  
36 < #define  bitop(x,y,op)          (ourbitmap[(y)*xdim+((x)>>3)] op (1<<((x)&7)))
37 < #define  tstbit(x,y)            bitop(x,y,&)
38 < #define  setbit(x,y)            bitop(x,y,|=)
39 < #define  clrbit(x,y)            bitop(x,y,&=~)
40 < #define  tglbit(x,y)            bitop(x,y,^=)
36 > #define  bitop(x,y,op)          (ourbitmap[(y)*xdim+((x)>>3)] op (1<<((x)&7)))
37 > #define  tstbit(x,y)            bitop(x,y,&)
38 > #define  setbit(x,y)            bitop(x,y,|=)
39 > #define  clrbit(x,y)            bitop(x,y,&=~)
40 > #define  tglbit(x,y)            bitop(x,y,^=)
41  
42 < typedef unsigned char  GLYPH;
42 > FONT  *ourfont;                         /* our font */
43  
42 GLYPH  *ourfont[128];                   /* our font */
43
44   typedef struct line {
45          char  *s;               /* line w/o LF */
46 +        short  *sp;             /* character spacing */
47          struct line  *next;     /* next line up */
48   } LINE;
49  
50   LINE  *ourtext;                         /* our text */
51   int  nlines, maxline;                   /* text dimensions */
52 + int  maxwidth;                          /* maximum line width (dvi) */
53  
52 extern char  *malloc(), *calloc();
53 extern FILE  *fropen();
54  
55
55   main(argc, argv)
56   int  argc;
57   char  *argv[];
58   {
60        double  atof();
59          int  an;
60 <
60 >        SET_FILE_BINARY(stdout);
61          for (an = 1; an < argc && argv[an][0] == '-'; an++)
62                  switch (argv[an][1]) {
63                  case 'c':                       /* color */
64                          switch (argv[an][2]) {
65                          case 'f':                       /* foreground */
66 <                                setcolr(fgcolr, atof(argv[an+1]),
66 >                                setcolor(fgcolor, atof(argv[an+1]),
67                                                  atof(argv[an+2]),
68                                                  atof(argv[an+3]));
69                                  an += 3;
70                                  break;
71                          case 'b':                       /* background */
72 <                                setcolr(bgcolr, atof(argv[an+1]),
72 >                                setcolor(bgcolor, atof(argv[an+1]),
73                                                  atof(argv[an+2]),
74                                                  atof(argv[an+3]));
75                                  an += 3;
# Line 95 | Line 93 | char  *argv[];
93                                  goto unkopt;
94                          }
95                          break;
96 +                case 'x':                       /* x resolution */
97 +                        xsiz = atoi(argv[++an])*SSS;
98 +                        break;
99 +                case 'y':
100 +                        ysiz = atoi(argv[++an])*SSS;
101 +                        break;
102                  case 'h':                       /* height of characters */
103 <                        cheight = atoi(argv[++an]);
103 >                        cheight = atoi(argv[++an])*SSS;
104                          break;
105                  case 'a':                       /* aspect ratio */
106                          aspect = atof(argv[++an]);
107                          break;
108 +                case 's':                       /* spacing */
109 +                        spacing = atof(argv[++an]);
110 +                        break;
111                  default:;
112   unkopt:
113                          fprintf(stderr, "%s: unknown option: %s\n",
114                                          argv[0], argv[an]);
115                          exit(1);
116                  }
117 +                                        /* load font file */
118 +        ourfont = getfont(fontfile);
119                                          /* get text */
120          if (an == argc)
121                  gettext(stdin);
# Line 115 | Line 124 | unkopt:
124  
125                                          /* create bit map */
126          makemap();
118                                        /* load font file */
119        loadfont();
127                                          /* convert text to bitmap */
128          maptext();
129                                          /* print header */
130 +        newheader("RADIANCE", stdout);
131          printargs(argc, argv, stdout);
132          fputformat(COLRFMT, stdout);
133          putchar('\n');
# Line 132 | Line 140 | unkopt:
140  
141   makemap()                       /* create the bit map */
142   {
143 <        cwidth = cheight/aspect + 0.5;
143 >        double  pictaspect;
144 >        
145          if (direct == 'r' || direct == 'l') {
146 <                xsiz = maxline*cwidth;
147 <                ysiz = nlines*cheight;
146 >                if (xsiz <= 0) {
147 >                        cwidth = cheight/aspect + 0.5;
148 >                        xsiz = (long)maxwidth*cwidth >> 8;
149 >                        ysiz = nlines*cheight;
150 >                } else if (aspect > FTINY) {
151 >                        if (ysiz <= 0)
152 >                                ysiz = cheight*nlines;
153 >                        pictaspect = 256*nlines*aspect/maxwidth;
154 >                        if (pictaspect*xsiz < ysiz)
155 >                                ysiz = pictaspect*xsiz + 0.5;
156 >                        else
157 >                                xsiz = ysiz/pictaspect + 0.5;
158 >                        cheight = ysiz/nlines;
159 >                        cwidth = cheight/aspect;
160 >                } else {
161 >                        if (ysiz <= 0)
162 >                                ysiz = cheight*nlines;
163 >                        pictaspect = (double)ysiz/xsiz;
164 >                        aspect = pictaspect*maxwidth/(256*nlines);
165 >                        cheight = ysiz/nlines;
166 >                        cwidth = cheight/aspect;
167 >                }
168          } else {                        /* reverse orientation */
169 <                xsiz = nlines*cheight;
170 <                ysiz = maxline*cwidth;
169 >                if (ysiz <= 0) {
170 >                        cwidth = cheight/aspect + 0.5;
171 >                        xsiz = nlines*cheight;
172 >                        ysiz = (long)maxwidth*cwidth >> 8;
173 >                } else if (aspect > FTINY) {
174 >                        if (xsiz <= 0)
175 >                                xsiz = cheight*nlines;
176 >                        pictaspect = maxwidth/(256*nlines*aspect);
177 >                        if (pictaspect*xsiz < ysiz)
178 >                                ysiz = pictaspect*xsiz + 0.5;
179 >                        else
180 >                                xsiz = ysiz/pictaspect + 0.5;
181 >                        cheight = xsiz/nlines;
182 >                        cwidth = cheight/aspect;
183 >                } else {
184 >                        if (xsiz <= 0)
185 >                                xsiz = cheight*nlines;
186 >                        pictaspect = (double)ysiz/xsiz;
187 >                        aspect = maxwidth/(256*nlines*pictaspect);
188 >                        cheight = xsiz/nlines;
189 >                        cwidth = cheight/aspect;
190 >                }
191          }
192 +        if (xsiz % SSS)
193 +                xsiz += SSS - xsiz%SSS;
194 +        if (ysiz % SSS)
195 +                ysiz += SSS - ysiz%SSS;
196          xdim = (xsiz+7)/8;
197 <        ourbitmap = (BYTE *)calloc(ysiz, xdim);
198 <        if (ourbitmap == NULL) {
199 <                fprintf(stderr, "out of memory in makemap\n");
200 <                exit(1);
148 <        }
197 >        ourbitmap = (BYTE *)bmalloc(ysiz*xdim);
198 >        if (ourbitmap == NULL)
199 >                error(SYSTEM, "Out of memory in makemap");
200 >        bzero((char *)ourbitmap, ysiz*xdim);
201   }
202  
203  
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
204   gettext(fp)                     /* get text from a file */
205   FILE  *fp;
206   {
208        char  *fgets();
207          char  buf[MAXLINE];
208          register LINE  *curl;
209          int  len;
210  
211          maxline = 0;
212 +        maxwidth = 0;
213          nlines = 0;
214          while (fgets(buf, MAXLINE, fp) != NULL) {
215                  curl = (LINE *)malloc(sizeof(LINE));
216                  if (curl == NULL)
217                          goto memerr;
218                  len = strlen(buf);
219 <                curl->s = malloc(len--);
220 <                if (curl->s == NULL)
219 >                curl->s = (char *)malloc(len);
220 >                curl->sp = (short *)malloc(sizeof(short)*len--);
221 >                if (curl->s == NULL | curl->sp == NULL)
222                          goto memerr;
223 +                if (len > maxline)
224 +                        maxline = len;
225                  strncpy(curl->s, buf, len);
226                  curl->s[len] = '\0';
227 +                if (spacing < -1./256.)
228 +                        len = squeeztext(curl->sp, curl->s, ourfont,
229 +                                        (int)(spacing*-256.));
230 +                else if (spacing > 1./256.)
231 +                        len = proptext(curl->sp, curl->s, ourfont,
232 +                                        (int)(spacing*256.), 3);
233 +                else
234 +                        len = uniftext(curl->sp, curl->s, ourfont);
235 +                if (len > maxwidth)
236 +                        maxwidth = len;
237                  curl->next = ourtext;
238                  ourtext = curl;
227                if (len > maxline)
228                        maxline = len;
239                  nlines++;
240          }
241          return;
242   memerr:
243 <        fprintf(stderr, "out of memory in gettext\n");
234 <        exit(1);
243 >        error(SYSTEM, "Out of memory in gettext");
244   }
245  
246  
# Line 244 | Line 253 | char  *av[];
253          ourtext = (LINE *)malloc(sizeof(LINE));
254          if (ourtext == NULL)
255                  goto memerr;
256 <        ourtext->s = malloc(MAXLINE);
256 >        ourtext->s = (char *)malloc(MAXLINE);
257          if (ourtext->s == NULL)
258                  goto memerr;
259          for (cp = ourtext->s; ac-- > 0; av++) {
# Line 255 | Line 264 | char  *av[];
264          *--cp = '\0';
265          ourtext->next = NULL;
266          maxline = strlen(ourtext->s);
267 +        ourtext->sp = (short *)malloc(sizeof(short)*(maxline+1));
268 +        if (ourtext->sp == NULL)
269 +                goto memerr;
270 +        if (spacing < -1./256.)
271 +                maxwidth = squeeztext(ourtext->sp, ourtext->s, ourfont,
272 +                                (int)(spacing*-256.));
273 +        else if (spacing > 1./256.)
274 +                maxwidth = proptext(ourtext->sp, ourtext->s, ourfont,
275 +                                (int)(spacing*256.), 3);
276 +        else
277 +                maxwidth = uniftext(ourtext->sp, ourtext->s, ourfont);
278          nlines = 1;
279          return;
280   memerr:
281 <        fprintf(stderr, "out of memory in arg_text\n");
262 <        exit(1);
281 >        error(SYSTEM, "Out of memory in arg_text");
282   }
283  
284  
285   maptext()                       /* map our text */
286   {
287          register LINE  *curl;
288 <        int  l;
289 <        register int  c;
288 >        int  l, len;
289 >        register int  i, c;
290  
291 <        for (l = 0, curl = ourtext; curl != NULL; l++, curl = curl->next)
292 <                for (c = strlen(curl->s)-1; c >= 0; c--)
293 <                        mapglyph(ourfont[curl->s[c]], c, l);
291 >        for (l = 0, curl = ourtext; curl != NULL; l += 256, curl = curl->next) {
292 >                len = strlen(curl->s); c = 0;
293 >                for (i = 0; i < len; i++) {
294 >                        c += curl->sp[i];
295 >                        mapglyph(ourfont->fg[curl->s[i]&0xff], c, l);
296 >                }
297 >        }
298   }
299  
300  
301   mapglyph(gl, tx0, ty0)          /* convert a glyph */
302 < register GLYPH  *gl;
302 > GLYPH  *gl;
303   int  tx0, ty0;
304   {
305          int  n;
306 +        register GORD  *gp;
307          int  p0[2], p1[2];
308  
309          if (gl == NULL)
310                  return;
311  
312 <        tx0 <<= 8; ty0 <<= 8;
313 <        n = *gl++;
314 <        mapcoord(p0, gl[2*n-2]+tx0, gl[2*n-1]+ty0);
312 >        n = gl->nverts;
313 >        gp = gvlist(gl);
314 >        mapcoord(p0, gp[2*n-2]+tx0, gp[2*n-1]+ty0);
315          while (n--) {
316 <                mapcoord(p1, gl[0]+tx0, gl[1]+ty0);
316 >                mapcoord(p1, gp[0]+tx0, gp[1]+ty0);
317                  mapedge(p0[0], p0[1], p1[0]-p0[0], p1[1]-p0[1]);
318                  p0[0] = p1[0]; p0[1] = p1[1];
319 <                gl += 2;
319 >                gp += 2;
320          }
321   }
322  
# Line 364 | Line 388 | int  run, rise;
388   writemap(fp)                    /* write out bitmap */
389   FILE  *fp;
390   {
391 +        COLR  pixval[SSS*SSS+1];        /* possible pixel values */
392 +        COLOR  ctmp0, ctmp1;
393 +        double  d;
394          COLR  *scanout;
395 <        int  y;
396 <        register int  x;
395 >        int  x, y;
396 >        register int  i, j;
397 >        int  cnt;
398          register int  inglyph;
399  
400 <        fprintf(fp, "-Y %d +X %d\n", ysiz, xsiz);
400 >        fprintf(fp, "-Y %d +X %d\n", ysiz/SSS, xsiz/SSS);
401  
402 <        scanout = (COLR *)malloc(xsiz*sizeof(COLR));
403 <        if (scanout == NULL) {
404 <                fprintf(stderr, "out of memory in writemap\n");
405 <                exit(1);
402 >        scanout = (COLR *)malloc(xsiz/SSS*sizeof(COLR));
403 >        if (scanout == NULL)
404 >                error(SYSTEM, "Out of memory in writemap");
405 >        for (i = 0; i <= SSS*SSS; i++) {        /* compute possible values */
406 >                copycolor(ctmp0, fgcolor);
407 >                d = (double)i/(SSS*SSS);
408 >                scalecolor(ctmp0, d);
409 >                copycolor(ctmp1, bgcolor);
410 >                d = 1.0 - d;
411 >                scalecolor(ctmp1, d);
412 >                addcolor(ctmp0, ctmp1);
413 >                setcolr(pixval[i], colval(ctmp0,RED),
414 >                                colval(ctmp0,GRN), colval(ctmp0,BLU));
415          }
416 <        for (y = ysiz-1; y >= 0; y--) {
416 >        for (y = ysiz/SSS-1; y >= 0; y--) {
417                  inglyph = 0;
418 <                for (x = 0; x < xsiz; x++) {
419 <                        if (tstbit(x, y))
420 <                                inglyph ^= 1;
421 <                        if (inglyph)
422 <                                copycolr(scanout[x], fgcolr);
423 <                        else
424 <                                copycolr(scanout[x], bgcolr);
418 >                for (x = 0; x < xsiz/SSS; x++) {
419 >                        cnt = 0;
420 >                        for (j = 0; j < SSS; j++)
421 >                                for (i = 0; i < SSS; i++) {
422 >                                        if (tstbit(x*SSS+i, y*SSS+j))
423 >                                                inglyph ^= 1<<j;
424 >                                        if (inglyph & 1<<j)
425 >                                                cnt++;
426 >                                }
427 >                        copycolr(scanout[x], pixval[cnt]);
428                  }
429 <                if (fwritecolrs(scanout, xsiz, fp) < 0) {
429 >                if (fwritecolrs(scanout, xsiz/SSS, fp) < 0) {
430                          fprintf(stderr, "write error in writemap\n");
431                          exit(1);
432                  }
433          }
434 <        free((char *)scanout);
434 >        free((void *)scanout);
435   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines