Back to index

tetex-bin  3.0
pk2bm.c
Go to the documentation of this file.
00001 /*
00002  * NAME
00003  *      pk2bm - create a bitmap from a TeX pkfont
00004  * SYNOPSIS:
00005  *     pk2bm [-bh] [-W width] [-H height] {-c char|-o octchar} pkfont 
00006  * DESCRIPTION
00007  *     This program generates a bitmap (ASCII file) which can be used
00008  *     to create X11 applications. The bitmap file consists of all
00009  *     pixels of the specified character (via the -c or -o option) from 
00010  *     the given pkfont. The format is described in bitmap(X11).
00011  *
00012  *     The pkfont is a packed fontfile generated by gftopk(TeX) from a 
00013  *     gffont. A gffont is the output of METAFONT a program to design fonts
00014  *     in a device independant way.
00015  *
00016  *     With the -b flag a bitmap is generated in which all black pixels are
00017  *     drawn as a `*' and all white bits as a `.'. With the -h flag a 
00018  *     hexadecimal bitmap dump is generated. 
00019  *
00020  *      The -W and/or -H options can be used to create a bitmap of
00021  *     respectivally `width' and `height' pixels. The pk-bitmap will in
00022  *     this case be centered according to these new dimensions.
00023  *
00024  *     The output is written to the standard output.
00025  * SEE ALSO
00026  *     ``METAFONT'', Donald Knuth.
00027  *     ``The PKtype processor'', belonging to the METAFONTware.
00028  *     bitmap(X11), gftopk(TeX), pktype(TeX), ps2pk(1)
00029  * AUTHOR
00030  *     Piet Tutelaers
00031  *     rcpt@urc.tue.nl
00032  */
00033 #include <stdio.h>
00034 #include "pkin.h"
00035 
00036 int main(int argc, char *argv[])
00037 {
00038    int done, C = 0, c, h = 0, w = 0, row, col;
00039    unsigned int bitmap = 0, hexmap = 0;
00040    halfword *word;
00041    quarterword lsbf();
00042    void dots();
00043    chardesc cd;
00044    char *myname = "pk2bm", *pkname;
00045    int atoo(char *);
00046        
00047    while (--argc > 0 && (*++argv)[0] == '-') {
00048       done=0;
00049       while ((!done) && (c = *++argv[0])) /* allow -bcK as an option */
00050          switch (c) {
00051         case 'c':
00052            if (*++argv[0] == '\0') {
00053               argc--;  argv++;
00054            }
00055            C = *argv[0];
00056            done = 1; break;
00057         case 'o':
00058            if (*++argv[0] == '\0') {
00059               argc--;  ++argv;
00060            }
00061            C = atoo(argv[0]);
00062            done = 1; break;
00063         case 'H':
00064            if (*++argv[0] == '\0') {
00065               argc--; argv++;
00066            }
00067            h = atoi(argv[0]);
00068             done=1;
00069            break;
00070         case 'W':
00071            if (*++argv[0] == '\0') {
00072               argc--; argv++;
00073            }
00074            w = atoi(argv[0]);
00075            done=1;
00076            break;
00077         case 'b':
00078            bitmap = 1;
00079            break;
00080         case 'h':
00081            hexmap = 1;
00082            break;
00083         default:
00084            printf("%s: %c illegal option\n", myname, c);
00085            exit(1);
00086         }
00087    }
00088 
00089    if (argc == 0) {
00090       printf("Usage: %s [-bh] {-c char|-o octchar} [-W width -H height] pkfile\n", myname);
00091       exit(1);
00092    }
00093 
00094    pkname = argv[0];
00095 
00096    if (readchar(pkname, C, &cd)) {
00097       int H, dh, W, dw, bitsleft;
00098       halfword nextword; quarterword nextbyte;
00099 
00100       H=cd.cheight; if (h==0) h=H; dh=(h-H)/2;
00101       W=cd.cwidth; if (w==0) w=W; dw=(w-W)/2;
00102           
00103       if (bitmap || hexmap) {
00104          printf("character : %d (%c)\n", (int) cd.charcode, 
00105                 (char)cd.charcode);
00106          printf("   height : %d\n", (int) cd.cheight);
00107          printf("    width : %d\n", (int) cd.cwidth);
00108          printf("     xoff : %d\n", (int) cd.xoff);
00109          printf("     yoff : %d\n", (int) cd.yoff);
00110       }
00111       else {
00112          printf("#define %s_%c_width \t %d\n",
00113               pkname, (char)cd.charcode, (int) w);
00114          printf("#define %s_%c_height \t %d\n",
00115               pkname, (char)cd.charcode, (int) h);
00116          printf("#define %s_%c_xoff \t %d\n",
00117               pkname, (char)cd.charcode, (int) dw);
00118          printf("#define %s_%c_yoff \t %d\n",
00119               pkname, (char)cd.charcode, (int) dh);
00120          printf("static char %s_%c_bits[] = {", 
00121              pkname, (char)cd.charcode);
00122       }
00123       for (row=0; row<h-H-dh; row++) {
00124           printf("\n  ");
00125           for (col=0; col<w; col+=8)
00126              if (bitmap) dots(0, w-col>7? 8: w-col);
00127              else if (hexmap) printf("%02x", 0x00);
00128              else printf(" 0x%02x, ", 0x00);
00129       }
00130       word = cd.raster;
00131       for (row=h-H-dh; row<h-dh; row++) {
00132          bitsleft = dw; nextword = 0;
00133          printf("\n  ");
00134          for (col=0; col<w; col+=8) {
00135             if (bitsleft>=8) {
00136                nextbyte = nextword >> (bitsleft-8) & 0xff;
00137               bitsleft-=8;
00138               
00139                if (bitmap) dots(nextbyte, w-col>7? 8: w-col); 
00140                else if (hexmap) printf("%02x", nextbyte);
00141                else printf(" 0x%02x, ", lsbf(nextbyte));
00142            }
00143            else {
00144               nextbyte = nextword << (8-bitsleft) & 0xff;
00145               if (col-dw<W)
00146                  nextword=(*word++);
00147               else nextword=0;
00148               nextbyte = nextbyte | (nextword >> (16-(8-bitsleft))
00149                                          & 0xff);
00150               bitsleft = 16 - (8-bitsleft);
00151               
00152                if (bitmap) dots(nextbyte, w-col>7? 8: w-col);
00153                else if (hexmap) printf("%02x", nextbyte);
00154                else printf(" 0x%02x, ", lsbf(nextbyte));
00155            }
00156         }
00157       }
00158       for (row=h-dh; row<h; row++) {
00159           printf("\n  ");
00160          for (col=0; col<w; col+=8)
00161              if (bitmap) dots(0, w-col>7? 8: w-col);
00162              else if (hexmap) printf("%02x", 0x00);
00163              else printf(" 0x%02x, ", 0x00);
00164       }
00165       if ((!bitmap) && (!hexmap)) printf("};\n");
00166       else putchar('\n');
00167    }
00168    return 0;
00169 }
00170 
00171 /*
00172  * lsbf() transforms a byte (least significant bit first).
00173  */
00174 quarterword lsbf(u)
00175 quarterword u;
00176 {
00177        int i; quarterword bit, result = 0;
00178 
00179        for (i=0; i<8; i++) {
00180           bit = u & 001;
00181           result = (result<<1) | bit;
00182           u = u >> 1;
00183        }
00184        return result;
00185 }
00186 
00187 /*
00188  * dots() print byte: `*' for a black and `.' for a white pixel
00189  */
00190 void dots(u, n)
00191 quarterword u; int n;
00192 {  unsigned int bit;
00193 
00194    bit=1<<7;
00195    for ( ;n>0 ;n--) {
00196       if (u & bit) putchar('*');
00197       else putchar('.');
00198       bit>>=1;
00199    }
00200 }
00201 
00202 int atoo(char *oct)
00203 {  int octal = 0;
00204    while (*oct != '\0') octal = 8*octal + (*oct++) - '0';
00205    return octal & 0xff;
00206 }