Debian Codegraph @

texk/ps2pk/pk2bm.c

raw
1 /*
2 * NAME
3 * pk2bm - create a bitmap from a TeX pkfont
4 * SYNOPSIS:
5 * pk2bm [-bh] [-W width] [-H height] {-c char|-o octchar} pkfont
6 * DESCRIPTION
7 * This program generates a bitmap (ASCII file) which can be used
8 * to create X11 applications. The bitmap file consists of all
9 * pixels of the specified character (via the -c or -o option) from
10 * the given pkfont. The format is described in bitmap(X11).
11 *
12 * The pkfont is a packed fontfile generated by gftopk(TeX) from a
13 * gffont. A gffont is the output of METAFONT a program to design fonts
14 * in a device independant way.
15 *
16 * With the -b flag a bitmap is generated in which all black pixels are
17 * drawn as a `*' and all white bits as a `.'. With the -h flag a
18 * hexadecimal bitmap dump is generated.
19 *
20 * The -W and/or -H options can be used to create a bitmap of
21 * respectivally `width' and `height' pixels. The pk-bitmap will in
22 * this case be centered according to these new dimensions.
23 *
24 * The output is written to the standard output.
25 * SEE ALSO
26 * ``METAFONT'', Donald Knuth.
27 * ``The PKtype processor'', belonging to the METAFONTware.
28 * bitmap(X11), gftopk(TeX), pktype(TeX), ps2pk(1)
29 * AUTHOR
30 * Piet Tutelaers
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "c-auto.h"
35 #endif
36
37 #include <stdio.h>
38 #include "pkin.h"
39 #include "basics.h"
40
41 #ifdef WIN32
42 #include <fcntl.h>
43 #include <io.h>
44 #endif
45
46 static quarterword lsbf(quarterword u);
47 static void dots(quarterword u, int n);
48 static int atoo(char *oct);
49
50 int main(int argc, char *argv[])
51 {
52 int done, C = 0, c, h = 0, w = 0, row, col;
53 unsigned int bitmap = 0, hexmap = 0;
54 halfword *word;
55 chardesc cd;
56 const char *myname = "pk2bm";
57 char *pkname;
58
59 while (--argc > 0 && (*++argv)[0] == '-') {
60 done=0;
61 while ((!done) && (c = *++argv[0])) /* allow -bcK as an option */
62 switch (c) {
63 case 'c':
64 if (*++argv[0] == '\0') {
65 argc--; argv++;
66 }
67 if (argv[0] == NULL) goto invalid;
68 C = *argv[0];
69 done = 1; break;
70 case 'o':
71 if (*++argv[0] == '\0') {
72 argc--; ++argv;
73 }
74 if (argv[0] == NULL) goto invalid;
75 C = atoo(argv[0]);
76 done = 1; break;
77 case 'H':
78 if (*++argv[0] == '\0') {
79 argc--; argv++;
80 }
81 if (argv[0] == NULL) goto invalid;
82 h = atoi(argv[0]);
83 done=1;
84 break;
85 case 'W':
86 if (*++argv[0] == '\0') {
87 argc--; argv++;
88 }
89 if (argv[0] == NULL) goto invalid;
90 w = atoi(argv[0]);
91 done=1;
92 break;
93 case 'b':
94 bitmap = 1;
95 break;
96 case 'h':
97 hexmap = 1;
98 break;
99 invalid:
100 default:
101 printf("%s: %c invalid option\n", myname, c);
102 exit(1);
103 }
104 }
105
106 if (argc == 0) {
107 msg ("pk2bm (ps2pk) version " PACKAGE_VERSION " (" TL_VERSION ")\n");
108 msg ("Usage: %s [-bh] {-c char|-o octchar} [-W width -H height] pkfile\n", myname);
109 fatal("\nEmail bug reports to %s.\n", PACKAGE_BUGREPORT);
110 }
111
112 #ifdef WIN32
113 _setmode(fileno(stdout), _O_BINARY);
114 #endif
115
116 pkname = argv[0];
117
118 if (readchar(pkname, (integer)C, &cd)) {
119 int H, dh, W, dw, bitsleft;
120 halfword nextword; quarterword nextbyte;
121
122 H=cd.cheight; if (h==0) h=H; dh=(h-H)/2;
123 W=cd.cwidth; if (w==0) w=W; dw=(w-W)/2;
124
125 if (bitmap || hexmap) {
126 printf("character : %d (%c)\n", (int) cd.charcode,
127 (char)cd.charcode);
128 printf(" height : %d\n", (int) cd.cheight);
129 printf(" width : %d\n", (int) cd.cwidth);
130 printf(" xoff : %d\n", (int) cd.xoff);
131 printf(" yoff : %d\n", (int) cd.yoff);
132 }
133 else {
134 printf("#define %s_%c_width \t %d\n",
135 pkname, (char)cd.charcode, (int) w);
136 printf("#define %s_%c_height \t %d\n",
137 pkname, (char)cd.charcode, (int) h);
138 printf("#define %s_%c_xoff \t %d\n",
139 pkname, (char)cd.charcode, (int) dw);
140 printf("#define %s_%c_yoff \t %d\n",
141 pkname, (char)cd.charcode, (int) dh);
142 printf("static char %s_%c_bits[] = {",
143 pkname, (char)cd.charcode);
144 }
145 for (row=0; row<h-H-dh; row++) {
146 printf("\n ");
147 for (col=0; col<w; col+=8)
148 if (bitmap) dots(0, w-col>7? 8: w-col);
149 else if (hexmap) printf("%02x", 0x00);
150 else printf(" 0x%02x, ", 0x00);
151 }
152 word = cd.raster;
153 for (row=h-H-dh; row<h-dh; row++) {
154 bitsleft = dw; nextword = 0;
155 printf("\n ");
156 for (col=0; col<w; col+=8) {
157 if (bitsleft>=8) {
158 nextbyte = nextword >> (bitsleft-8) & 0xff;
159 bitsleft-=8;
160
161 if (bitmap) dots(nextbyte, w-col>7? 8: w-col);
162 else if (hexmap) printf("%02x", nextbyte);
163 else printf(" 0x%02x, ", lsbf(nextbyte));
164 }
165 else {
166 nextbyte = nextword << (8-bitsleft) & 0xff;
167 if (col-dw<W)
168 nextword=(*word++);
169 else nextword=0;
170 nextbyte = nextbyte | (nextword >> (16-(8-bitsleft))
171 & 0xff);
172 bitsleft = 16 - (8-bitsleft);
173
174 if (bitmap) dots(nextbyte, w-col>7? 8: w-col);
175 else if (hexmap) printf("%02x", nextbyte);
176 else printf(" 0x%02x, ", lsbf(nextbyte));
177 }
178 }
179 }
180 for (row=h-dh; row<h; row++) {
181 printf("\n ");
182 for (col=0; col<w; col+=8)
183 if (bitmap) dots(0, w-col>7? 8: w-col);
184 else if (hexmap) printf("%02x", 0x00);
185 else printf(" 0x%02x, ", 0x00);
186 }
187 if ((!bitmap) && (!hexmap)) printf("};\n");
188 else putchar('\n');
189 }
190 return 0;
191 }
192
193 /*
194 * lsbf() transforms a byte (least significant bit first).
195 */
196 static quarterword
197 lsbf(quarterword u)
198 {
199 int i; quarterword bit, result = 0;
200
201 for (i=0; i<8; i++) {
202 bit = u & 001;
203 result = (result<<1) | bit;
204 u = u >> 1;
205 }
206 return result;
207 }
208
209 /*
210 * dots() print byte: `*' for a black and `.' for a white pixel
211 */
212 static void
213 dots(quarterword u, int n)
214 {
215 unsigned int bit;
216
217 bit=1<<7;
218 for ( ;n>0 ;n--) {
219 if (u & bit) putchar('*');
220 else putchar('.');
221 bit>>=1;
222 }
223 }
224
225 static int
226 atoo(char *oct)
227 {
228 int octal = 0;
229 while (*oct != '\0') octal = 8*octal + (*oct++) - '0';
230 return octal;
231 }