summaryrefslogtreecommitdiff
path: root/Build/source/texk/ps2pkm/pk2bm.c
blob: efc334616e14e9c4bd38783a786f67900998b371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * NAME
 *      pk2bm - create a bitmap from a TeX pkfont
 * SYNOPSIS:
 *	pk2bm [-bh] [-W width] [-H height] {-c char|-o octchar} pkfont 
 * DESCRIPTION
 *	This program generates a bitmap (ASCII file) which can be used
 *	to create X11 applications. The bitmap file consists of all
 *	pixels of the specified character (via the -c or -o option) from 
 *	the given pkfont. The format is described in bitmap(X11).
 *
 *	The pkfont is a packed fontfile generated by gftopk(TeX) from a 
 *	gffont. A gffont is the output of METAFONT a program to design fonts
 *	in a device independant way.
 *
 *	With the -b flag a bitmap is generated in which all black pixels are
 *	drawn as a `*' and all white bits as a `.'. With the -h flag a 
 *	hexadecimal bitmap dump is generated. 
 *
 *      The -W and/or -H options can be used to create a bitmap of
 *	respectivally `width' and `height' pixels. The pk-bitmap will in
 *	this case be centered according to these new dimensions.
 *
 *	The output is written to the standard output.
 * SEE ALSO
 *	``METAFONT'', Donald Knuth.
 *	``The PKtype processor'', belonging to the METAFONTware.
 *	bitmap(X11), gftopk(TeX), pktype(TeX), ps2pk(1)
 * AUTHOR
 *	Piet Tutelaers
 *	rcpt@urc.tue.nl
 */
#include <stdio.h>
#include "pkin.h"

#ifdef WIN32
#include <fcntl.h>
#endif

int main(int argc, char *argv[])
{
   int done, C = 0, c, h = 0, w = 0, row, col;
   unsigned int bitmap = 0, hexmap = 0;
   halfword *word;
   quarterword lsbf();
   void dots();
   chardesc cd;
   char *myname = "pk2bm", *pkname;
   int atoo(char *);
	
   while (--argc > 0 && (*++argv)[0] == '-') {
      done=0;
      while ((!done) && (c = *++argv[0]))	/* allow -bcK as an option */
         switch (c) {
      	 case 'c':
      	    if (*++argv[0] == '\0') {
      	       argc--;  argv++;
      	    }
      	    C = *argv[0];
      	    done = 1; break;
      	 case 'o':
      	    if (*++argv[0] == '\0') {
      	       argc--;  ++argv;
      	    }
      	    C = atoo(argv[0]);
      	    done = 1; break;
      	 case 'H':
      	    if (*++argv[0] == '\0') {
      	       argc--; argv++;
      	    }
      	    h = atoi(argv[0]);
            done=1;
      	    break;
      	 case 'W':
      	    if (*++argv[0] == '\0') {
      	       argc--; argv++;
      	    }
      	    w = atoi(argv[0]);
	    done=1;
      	    break;
      	 case 'b':
      	    bitmap = 1;
      	    break;
      	 case 'h':
      	    hexmap = 1;
      	    break;
      	 default:
      	    printf("%s: %c illegal option\n", myname, c);
      	    exit(1);
      	 }
   }

   if (argc == 0) {
      printf("Usage: %s [-bh] {-c char|-o octchar} [-W width -H height] pkfile\n", myname);
      exit(1);
   }

#ifdef WIN32
   setmode(fileno(stdout), _O_BINARY);
#endif

   pkname = argv[0];

   if (readchar(pkname, C, &cd)) {
      int H, dh, W, dw, bitsleft;
      halfword nextword; quarterword nextbyte;

      H=cd.cheight; if (h==0) h=H; dh=(h-H)/2;
      W=cd.cwidth; if (w==0) w=W; dw=(w-W)/2;
	   
      if (bitmap || hexmap) {
         printf("character : %d (%c)\n", (int) cd.charcode, 
	         (char)cd.charcode);
         printf("   height : %d\n", (int) cd.cheight);
         printf("    width : %d\n", (int) cd.cwidth);
         printf("     xoff : %d\n", (int) cd.xoff);
         printf("     yoff : %d\n", (int) cd.yoff);
      }
      else {
         printf("#define %s_%c_width \t %d\n",
              pkname, (char)cd.charcode, (int) w);
         printf("#define %s_%c_height \t %d\n",
              pkname, (char)cd.charcode, (int) h);
         printf("#define %s_%c_xoff \t %d\n",
              pkname, (char)cd.charcode, (int) dw);
         printf("#define %s_%c_yoff \t %d\n",
              pkname, (char)cd.charcode, (int) dh);
         printf("static char %s_%c_bits[] = {", 
   	      pkname, (char)cd.charcode);
      }
      for (row=0; row<h-H-dh; row++) {
          printf("\n  ");
          for (col=0; col<w; col+=8)
             if (bitmap) dots(0, w-col>7? 8: w-col);
             else if (hexmap) printf("%02x", 0x00);
             else printf(" 0x%02x, ", 0x00);
      }
      word = cd.raster;
      for (row=h-H-dh; row<h-dh; row++) {
         bitsleft = dw; nextword = 0;
         printf("\n  ");
         for (col=0; col<w; col+=8) {
            if (bitsleft>=8) {
               nextbyte = nextword >> (bitsleft-8) & 0xff;
	       bitsleft-=8;
	       
               if (bitmap) dots(nextbyte, w-col>7? 8: w-col); 
               else if (hexmap) printf("%02x", nextbyte);
               else printf(" 0x%02x, ", lsbf(nextbyte));
	    }
	    else {
	       nextbyte = nextword << (8-bitsleft) & 0xff;
	       if (col-dw<W)
	          nextword=(*word++);
	       else nextword=0;
	       nextbyte = nextbyte | (nextword >> (16-(8-bitsleft))
		                           & 0xff);
	       bitsleft = 16 - (8-bitsleft);
	       
               if (bitmap) dots(nextbyte, w-col>7? 8: w-col);
               else if (hexmap) printf("%02x", nextbyte);
               else printf(" 0x%02x, ", lsbf(nextbyte));
	    }
	 }
      }
      for (row=h-dh; row<h; row++) {
          printf("\n  ");
	  for (col=0; col<w; col+=8)
             if (bitmap) dots(0, w-col>7? 8: w-col);
             else if (hexmap) printf("%02x", 0x00);
             else printf(" 0x%02x, ", 0x00);
      }
      if ((!bitmap) && (!hexmap)) printf("};\n");
      else putchar('\n');
   }
   return 0;
}

/*
 * lsbf() transforms a byte (least significant bit first).
 */
quarterword lsbf(u)
quarterword u;
{
	int i; quarterword bit, result = 0;

	for (i=0; i<8; i++) {
	   bit = u & 001;
	   result = (result<<1) | bit;
	   u = u >> 1;
	}
	return result;
}

/*
 * dots() print byte: `*' for a black and `.' for a white pixel
 */
void dots(u, n)
quarterword u; int n;
{  unsigned int bit;

   bit=1<<7;
   for ( ;n>0 ;n--) {
      if (u & bit) putchar('*');
      else putchar('.');
      bit>>=1;
   }
}

int atoo(char *oct)
{  int octal = 0;
   while (*oct != '\0') octal = 8*octal + (*oct++) - '0';
   return octal & 0xff;
}