Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* This converts a PBM to a C header file containing a DGen font made from the
  2.  * PBM. The PBM width should be divisible by 96, with cells which from left to
  3.  * right are the glyphs from 0x20 (space) to 0x7f (character after ~).
  4.  * The color 1 in the PBM is opaque, and 0 is transparent. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.   FILE *pbm, *df;
  14.   char magic[0x20], tmp[0x200];
  15.   int x,y, i,j;
  16.   int width;
  17.   signed char w, h;
  18.  
  19.   /* Open the pbm and read it */
  20.   if(argc < 3)
  21.     { printf("Usage: %s pbmfile outfile\n", argv[0]); exit(2); }
  22.   if(!(pbm = fopen(argv[1], "r"))) exit(1);
  23.  
  24.         if (fgets(magic, 0x20, pbm) == NULL) {
  25.         error:
  26.                 fclose(pbm);
  27.                 return 1;
  28.         }
  29.  
  30.   /* Throw away possible comments */
  31.   do {
  32.                 if (fgets(tmp, 0x200, pbm) == NULL) {
  33.                         printf("Invalid file contents\n");
  34.                         goto error;
  35.                 }
  36.   } while(*tmp == '#');
  37.  
  38.         sscanf(tmp, "%d%hhd", &width, &h);
  39.         /* Verify magic and dimensions */
  40.         if (strcmp(magic, "P1\n")) {
  41.                 printf("Bad magic\n");
  42.                 goto error;
  43.         }
  44.         if ((width < 0) || (width % 96) || ((w = (width / 96)) < 0)) {
  45.                 printf("Bad X dimension\n");
  46.                 goto error;
  47.         }
  48.         if (h < 0) {
  49.                 printf("Bad Y dimension\n");
  50.                 goto error;
  51.         }
  52.  
  53.         unsigned char (*pbm_contents)[width][h];
  54.  
  55.         if ((pbm_contents = calloc(1, sizeof(*pbm_contents))) == NULL)
  56.                 goto invalid;
  57.         for (y = 0; (y < h); ++y)
  58.                 for (x = 0; (x < width); ++x) {
  59.                         char c;
  60.  
  61.                         do {
  62.                                 if (fread(&c, 1, 1, pbm) != 1)
  63.                                         goto invalid;
  64.                         }
  65.                         while (isspace(c));
  66.                         if (!isdigit(c))
  67.                                 goto invalid;
  68.                         (*pbm_contents)[x][y] = (c - 0x30);
  69.                 }
  70.  
  71.   fclose(pbm);
  72.  
  73.   /* Start the header to our output file */
  74.   if(!(df = fopen(argv[2], "w"))) exit(1);
  75.   fprintf(df, "/* DGen font, generated by pbm2df from \"%s\" */\n\n"
  76.               "#include <stdio.h> /* for NULL definition */\n\n",
  77.               argv[1]);
  78.   /* Now do each cell */
  79.   for(i = 0; i < 96; ++i)
  80.     {
  81.       fprintf(df, "static const short _glyph_%02X[] = {", i + 32);
  82.       j = 0;
  83.       for(y = 0; (y < h); ++y)
  84.         for(x = (i * w); x < ((i * w) + w); ++x)
  85.           {
  86.             if((*pbm_contents)[x][y]) { fprintf(df, "%d, ", j); j = 0; }
  87.             ++j;
  88.           }
  89.       fprintf(df, "-1};\n");
  90.     }
  91.   /* Compile it all into one big mess ;) */
  92.   fprintf(df, "const short *dgen_font_%hhdx%hhd[0x80] = {\n"
  93.               "  /* Fill in for the control characters */\n"
  94.               "  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n"
  95.               "  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n"
  96.               "  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n"
  97.               "  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL", w, h);
  98.   for(i = 32; i < 128; ++i)
  99.     fprintf(df, ",\n  _glyph_%02X", i);
  100.  
  101.   /* Close off the end of the file */
  102.   fprintf(df, "\n};\n");
  103.   fclose(df);
  104.  
  105.   printf("Successfully generated dgen font source \"%s\" from \"%s\"\n",
  106.          argv[2], argv[1]);
  107.  
  108.         free(pbm_contents);
  109.  
  110.   return 0;
  111. invalid:
  112.         free(pbm_contents);
  113.         fclose(pbm);
  114.         printf("Invalid file contents\n");
  115.         return 1;
  116. }
  117.