Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* fontdump.c -- an "xxd -i" workalike for dumping binary fonts as source code */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. static int
  7. hexdump(FILE *fo, FILE *fi)
  8. {
  9.         int c, n;
  10.  
  11.         n = 0;
  12.         c = fgetc(fi);
  13.         while (c != -1)
  14.         {
  15.                 n += fprintf(fo, "%d,", c);
  16.                 if (n > 72) {
  17.                         fprintf(fo, "\n");
  18.                         n = 0;
  19.                 }
  20.                 c = fgetc(fi);
  21.         }
  22.  
  23.         return n;
  24. }
  25.  
  26. int
  27. main(int argc, char **argv)
  28. {
  29.         FILE *fo;
  30.         FILE *fi;
  31.         char fontname[256];
  32.         char origname[256];
  33.         char *basename;
  34.         char *p;
  35.         int i, len;
  36.  
  37.         if (argc < 3)
  38.         {
  39.                 fprintf(stderr, "usage: fontdump output.c input.dat\n");
  40.                 return 1;
  41.         }
  42.  
  43.         fo = fopen(argv[1], "wb");
  44.         if (!fo)
  45.         {
  46.                 fprintf(stderr, "fontdump: could not open output file '%s'\n", argv[1]);
  47.                 return 1;
  48.         }
  49.  
  50.         fprintf(fo, "#ifndef __STRICT_ANSI__\n");
  51.         fprintf(fo, "#if defined(__linux__) || defined(__FreeBSD__)\n");
  52.         fprintf(fo, "#define HAVE_INCBIN\n");
  53.         fprintf(fo, "#endif\n");
  54.         fprintf(fo, "#endif\n");
  55.  
  56.         for (i = 2; i < argc; i++)
  57.         {
  58.                 fi = fopen(argv[i], "rb");
  59.                 if (!fi)
  60.                 {
  61.                         fclose(fo);
  62.                         fprintf(stderr, "fontdump: could not open input file '%s'\n", argv[i]);
  63.                         return 1;
  64.                 }
  65.  
  66.                 basename = strrchr(argv[i], '/');
  67.                 if (!basename)
  68.                         basename = strrchr(argv[i], '\\');
  69.                 if (basename)
  70.                         basename++;
  71.                 else
  72.                         basename = argv[i];
  73.  
  74.                 strcpy(origname, basename);
  75.                 p = strrchr(origname, '.');
  76.                 if (p) *p = 0;
  77.                 strcpy(fontname, origname);
  78.  
  79.                 p = fontname;
  80.                 while (*p)
  81.                 {
  82.                         if (*p == '/' || *p == '.' || *p == '\\' || *p == '-')
  83.                                 *p = '_';
  84.                         p ++;
  85.                 }
  86.  
  87.                 fseek(fi, 0, SEEK_END);
  88.                 len = ftell(fi);
  89.                 fseek(fi, 0, SEEK_SET);
  90.  
  91.                 printf("\t{\"%s\",pdf_font_%s,%d},\n", origname, fontname, len);
  92.  
  93.                 fprintf(fo, "\n#ifdef HAVE_INCBIN\n");
  94.                 fprintf(fo, "extern const unsigned char pdf_font_%s[%d];\n", fontname, len);
  95.                 fprintf(fo, "asm(\".globl pdf_font_%s\");\n", fontname);
  96.                 fprintf(fo, "asm(\".balign 8\");\n");
  97.                 fprintf(fo, "asm(\"pdf_font_%s:\");\n", fontname);
  98.                 fprintf(fo, "asm(\".incbin \\\"%s\\\"\");\n", argv[i]);
  99.                 fprintf(fo, "#else\n");
  100.                 fprintf(fo, "static const unsigned char pdf_font_%s[%d] = {\n", fontname, len);
  101.                 hexdump(fo, fi);
  102.                 fprintf(fo, "};\n");
  103.                 fprintf(fo, "#endif\n");
  104.  
  105.                 fclose(fi);
  106.         }
  107.  
  108.         if (fclose(fo))
  109.         {
  110.                 fprintf(stderr, "fontdump: could not close output file '%s'\n", argv[1]);
  111.                 return 1;
  112.         }
  113.  
  114.         return 0;
  115. }
  116.