summaryrefslogtreecommitdiff
path: root/tools/convbdf.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/convbdf.c')
-rw-r--r--tools/convbdf.c175
1 files changed, 151 insertions, 24 deletions
diff --git a/tools/convbdf.c b/tools/convbdf.c
index ac88ea889b..e369a91966 100644
--- a/tools/convbdf.c
+++ b/tools/convbdf.c
@@ -12,9 +12,15 @@
12#include <string.h> 12#include <string.h>
13#include <time.h> 13#include <time.h>
14 14
15#define ROTATE /* define this for the new, rotated format */
16
15/* BEGIN font.h*/ 17/* BEGIN font.h*/
16/* loadable font magic and version #*/ 18/* loadable font magic and version #*/
19#ifdef ROTATE
20#define VERSION "RB12" /* newer version */
21#else
17#define VERSION "RB11" 22#define VERSION "RB11"
23#endif
18 24
19/* bitmap_t helper macros*/ 25/* bitmap_t helper macros*/
20#define BITMAP_WORDS(x) (((x)+15)/16) /* image size in words*/ 26#define BITMAP_WORDS(x) (((x)+15)/16) /* image size in words*/
@@ -30,7 +36,6 @@ typedef unsigned short bitmap_t; /* bitmap image unit size*/
30/* builtin C-based proportional/fixed font structure */ 36/* builtin C-based proportional/fixed font structure */
31/* based on The Microwindows Project http://microwindows.org */ 37/* based on The Microwindows Project http://microwindows.org */
32struct font { 38struct font {
33 char * name; /* font name*/
34 int maxwidth; /* max width in pixels*/ 39 int maxwidth; /* max width in pixels*/
35 int height; /* height in pixels*/ 40 int height; /* height in pixels*/
36 int ascent; /* ascent (baseline) height*/ 41 int ascent; /* ascent (baseline) height*/
@@ -43,6 +48,8 @@ struct font {
43 long bits_size; /* # words of bitmap_t bits*/ 48 long bits_size; /* # words of bitmap_t bits*/
44 49
45 /* unused by runtime system, read in by convbdf*/ 50 /* unused by runtime system, read in by convbdf*/
51 unsigned long* offrot; /* offsets into rotated bitmap data*/
52 char * name; /* font name*/
46 char * facename; /* facename of font*/ 53 char * facename; /* facename of font*/
47 char * copyright; /* copyright info for loadable fonts*/ 54 char * copyright; /* copyright info for loadable fonts*/
48 int pixel_size; 55 int pixel_size;
@@ -259,6 +266,8 @@ void free_font(struct font* pf)
259 free(pf->bits); 266 free(pf->bits);
260 if (pf->offset) 267 if (pf->offset)
261 free(pf->offset); 268 free(pf->offset);
269 if (pf->offrot)
270 free(pf->offrot);
262 if (pf->width) 271 if (pf->width)
263 free(pf->width); 272 free(pf->width);
264 free(pf); 273 free(pf);
@@ -426,9 +435,10 @@ int bdf_read_header(FILE *fp, struct font* pf)
426 /* allocate bits, offset, and width arrays*/ 435 /* allocate bits, offset, and width arrays*/
427 pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t) + EXTRA); 436 pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t) + EXTRA);
428 pf->offset = (unsigned long *)malloc(pf->size * sizeof(unsigned long)); 437 pf->offset = (unsigned long *)malloc(pf->size * sizeof(unsigned long));
438 pf->offrot = (unsigned long *)malloc(pf->size * sizeof(unsigned long));
429 pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char)); 439 pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char));
430 440
431 if (!pf->bits || !pf->offset || !pf->width) { 441 if (!pf->bits || !pf->offset || !pf->offrot || !pf->width) {
432 fprintf(stderr, "Error: no memory for font load\n"); 442 fprintf(stderr, "Error: no memory for font load\n");
433 return 0; 443 return 0;
434 } 444 }
@@ -440,6 +450,7 @@ int bdf_read_header(FILE *fp, struct font* pf)
440int bdf_read_bitmaps(FILE *fp, struct font* pf) 450int bdf_read_bitmaps(FILE *fp, struct font* pf)
441{ 451{
442 long ofs = 0; 452 long ofs = 0;
453 long ofr = 0;
443 int maxwidth = 0; 454 int maxwidth = 0;
444 int i, k, encoding, width; 455 int i, k, encoding, width;
445 int bbw, bbh, bbx, bby; 456 int bbw, bbh, bbx, bby;
@@ -504,6 +515,7 @@ int bdf_read_bitmaps(FILE *fp, struct font* pf)
504 continue; 515 continue;
505 } 516 }
506 pf->offset[encoding-pf->firstchar] = ofs; 517 pf->offset[encoding-pf->firstchar] = ofs;
518 pf->offrot[encoding-pf->firstchar] = ofr;
507 519
508 /* calc char width*/ 520 /* calc char width*/
509 if (bbx < 0) { 521 if (bbx < 0) {
@@ -560,6 +572,7 @@ int bdf_read_bitmaps(FILE *fp, struct font* pf)
560 } 572 }
561 573
562 ofs += BITMAP_WORDS(width) * pf->height; 574 ofs += BITMAP_WORDS(width) * pf->height;
575 ofr += pf->width[encoding-pf->firstchar] * ((pf->height+7)/8);
563 576
564 continue; 577 continue;
565 } 578 }
@@ -576,6 +589,7 @@ int bdf_read_bitmaps(FILE *fp, struct font* pf)
576 589
577 if (pf->offset[i] == (unsigned long)-1) { 590 if (pf->offset[i] == (unsigned long)-1) {
578 pf->offset[i] = pf->offset[defchar]; 591 pf->offset[i] = pf->offset[defchar];
592 pf->offrot[i] = pf->offrot[defchar];
579 pf->width[i] = pf->width[defchar]; 593 pf->width[i] = pf->width[defchar];
580 } 594 }
581 } 595 }
@@ -622,6 +636,10 @@ int bdf_read_bitmaps(FILE *fp, struct font* pf)
622 } 636 }
623 } 637 }
624 638
639#ifdef ROTATE
640 pf->bits_size = ofr; /* always update, rotated is smaller */
641#endif
642
625 return 1; 643 return 1;
626} 644}
627 645
@@ -674,6 +692,62 @@ bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2)
674 return val; 692 return val;
675} 693}
676 694
695/*
696 * Take an bitmap_t bitmap and convert to Rockbox format.
697 * Used for converting font glyphs for the time being.
698 * Can use for standard X11 and Win32 images as well.
699 * See format description in lcd-recorder.c
700 *
701 * Doing it this way keeps fonts in standard formats,
702 * as well as keeping Rockbox hw bitmap format.
703 */
704int rotleft(unsigned char *dst, bitmap_t *src, unsigned int width,
705 unsigned int height)
706{
707 unsigned int i,j;
708 unsigned int src_words; /* # words of input image*/
709 unsigned int dst_mask; /* bit mask for destination */
710 bitmap_t src_mask; /* bit mask for source */
711
712 /* calc words of input image*/
713 src_words = BITMAP_WORDS(width) * height;
714
715 /* clear background*/
716 memset(dst, 0, ((height + 7) / 8) * width);
717
718 dst_mask = 1;
719
720 for (i=0; i < src_words; i++) {
721
722 /* calc src input bit*/
723 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
724
725 /* for each input column...*/
726 for(j=0; j < width; j++) {
727
728 /* if set in input, set in rotated output */
729 if (src[i] & src_mask)
730 dst[j] |= dst_mask;
731
732 src_mask >>= 1; /* next input bit */
733 if (src_mask == 0) /* input word done? */
734 {
735 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
736 i++; /* next input word */
737 }
738 }
739
740 dst_mask <<= 1; /* next output bit (row) */
741 if (dst_mask > (1 << 7)) /* output bit > 7? */
742 {
743 dst_mask = 1;
744 dst += width; /* next output byte row */
745 }
746 }
747 return ((height + 7) / 8) * width; /* return result size in bytes */
748}
749
750
677/* generate C source from in-core font*/ 751/* generate C source from in-core font*/
678int gen_c_source(struct font* pf, char *path) 752int gen_c_source(struct font* pf, char *path)
679{ 753{
@@ -704,7 +778,7 @@ int gen_c_source(struct font* pf, char *path)
704 "*/\n" 778 "*/\n"
705 "\n" 779 "\n"
706 "/* Font character bitmap data. */\n" 780 "/* Font character bitmap data. */\n"
707 "static bitmap_t _font_bits[] = {\n" 781 "static const unsigned char _font_bits[] = {\n"
708 }; 782 };
709 783
710 ofp = fopen(path, "w"); 784 ofp = fopen(path, "w");
@@ -786,6 +860,27 @@ int gen_c_source(struct font* pf, char *path)
786 fprintf(ofp, " */\n"); 860 fprintf(ofp, " */\n");
787 861
788 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i)); 862 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
863#ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
864 {
865 unsigned char bytemap[256];
866 int y8, ix=0;
867
868 rotleft(bytemap, bits, width, pf->height);
869 ofs = (bitmap_t*)bytemap;
870 for (y8=0; y8<pf->height; y8+=8) /* column rows */
871 {
872 for (x=0; x<width; x++) {
873 fprintf(ofp, "0x%02x, ", bytemap[ix]);
874 if (!did_syncmsg && bytemap[ix] != *ofs++) {
875 fprintf(stderr, "Warning: found encoding values in non-sorted order (not an error).\n");
876 did_syncmsg = 1;
877 }
878 ix++;
879 }
880 fprintf(ofp, "\n");
881 }
882 }
883#else
789 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) { 884 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) {
790 fprintf(ofp, "0x%04x,\n", *bits); 885 fprintf(ofp, "0x%04x,\n", *bits);
791 if (!did_syncmsg && *bits++ != *ofs++) { 886 if (!did_syncmsg && *bits++ != *ofs++) {
@@ -793,24 +888,29 @@ int gen_c_source(struct font* pf, char *path)
793 did_syncmsg = 1; 888 did_syncmsg = 1;
794 } 889 }
795 } 890 }
891#endif
796 } 892 }
797 fprintf(ofp, "};\n\n"); 893 fprintf(ofp, "};\n\n");
798 894
799 if (pf->offset) { 895 if (pf->offset) {
800 /* output offset table*/ 896 /* output offset table*/
801 fprintf(ofp, "/* Character->glyph mapping. */\n" 897 fprintf(ofp, "/* Character->glyph mapping. */\n"
802 "static unsigned long _sysfont_offset[] = {\n"); 898 "static const unsigned short _sysfont_offset[] = {\n");
803 899
804 for (i=0; i<pf->size; ++i) 900 for (i=0; i<pf->size; ++i)
805 fprintf(ofp, " %ld,\t/* (0x%02x) */\n", 901 fprintf(ofp, " %ld,\t/* (0x%02x) */\n",
902#ifdef ROTATE
903 pf->offrot[i], i+pf->firstchar);
904#else
806 pf->offset[i], i+pf->firstchar); 905 pf->offset[i], i+pf->firstchar);
906#endif
807 fprintf(ofp, "};\n\n"); 907 fprintf(ofp, "};\n\n");
808 } 908 }
809 909
810 /* output width table for proportional fonts*/ 910 /* output width table for proportional fonts*/
811 if (pf->width) { 911 if (pf->width) {
812 fprintf(ofp, "/* Character width data. */\n" 912 fprintf(ofp, "/* Character width data. */\n"
813 "static unsigned char _sysfont_width[] = {\n"); 913 "static const unsigned char _sysfont_width[] = {\n");
814 914
815 for (i=0; i<pf->size; ++i) 915 for (i=0; i<pf->size; ++i)
816 fprintf(ofp, " %d,\t/* (0x%02x) */\n", 916 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
@@ -822,28 +922,25 @@ int gen_c_source(struct font* pf, char *path)
822 if (pf->offset) 922 if (pf->offset)
823 sprintf(obuf, "_sysfont_offset,"); 923 sprintf(obuf, "_sysfont_offset,");
824 else 924 else
825 sprintf(obuf, "0, /* no encode table*/"); 925 sprintf(obuf, "0, /* no encode table */");
826 926
827 if (pf->width) 927 if (pf->width)
828 sprintf(buf, "_sysfont_width,"); 928 sprintf(buf, "_sysfont_width, /* width */");
829 else 929 else
830 sprintf(buf, "0, /* fixed width*/"); 930 sprintf(buf, "0, /* fixed width */");
831 931
832 fprintf(ofp, "/* Exported structure definition. */\n" 932 fprintf(ofp, "/* Exported structure definition. */\n"
833 "struct font sysfont = {\n" 933 "const struct font sysfont = {\n"
834 " \"%s\",\n" 934 " %d, /* maxwidth */\n"
835 " %d,\n" 935 " %d, /* height */\n"
836 " %d,\n" 936 " %d, /* ascent */\n"
837 " %d,\n" 937 " %d, /* firstchar */\n"
838 " %d,\n" 938 " %d, /* size */\n"
839 " %d,\n" 939 " _font_bits, /* bits */\n"
840 " _font_bits,\n" 940 " %s /* offset */\n"
841 " %s\n" 941 " %s\n"
842 " %s\n" 942 " %d, /* defaultchar */\n"
843 " %d,\n"
844 " sizeof(_font_bits)/sizeof(bitmap_t),\n"
845 "};\n", 943 "};\n",
846 pf->name,
847 pf->maxwidth, pf->height, 944 pf->maxwidth, pf->height,
848 pf->ascent, 945 pf->ascent,
849 pf->firstchar, 946 pf->firstchar,
@@ -899,6 +996,7 @@ int gen_fnt_file(struct font* pf, char *path)
899{ 996{
900 FILE *ofp; 997 FILE *ofp;
901 int i; 998 int i;
999 int did_defaultchar = 0;
902 1000
903 ofp = fopen(path, "wb"); 1001 ofp = fopen(path, "wb");
904 if (!ofp) { 1002 if (!ofp) {
@@ -908,13 +1006,13 @@ int gen_fnt_file(struct font* pf, char *path)
908 1006
909 /* write magic and version #*/ 1007 /* write magic and version #*/
910 writestr(ofp, VERSION, 4); 1008 writestr(ofp, VERSION, 4);
911 1009#ifndef ROTATE
912 /* internal font name*/ 1010 /* internal font name*/
913 writestrpad(ofp, pf->name, 64); 1011 writestrpad(ofp, pf->name, 64);
914 1012
915 /* copyright*/ 1013 /* copyright*/
916 writestrpad(ofp, pf->copyright, 256); 1014 writestrpad(ofp, pf->copyright, 256);
917 1015#endif
918 /* font info*/ 1016 /* font info*/
919 writeshort(ofp, pf->maxwidth); 1017 writeshort(ofp, pf->maxwidth);
920 writeshort(ofp, pf->height); 1018 writeshort(ofp, pf->height);
@@ -928,8 +1026,37 @@ int gen_fnt_file(struct font* pf, char *path)
928 writelong(ofp, pf->bits_size); /* # words of bitmap_t*/ 1026 writelong(ofp, pf->bits_size); /* # words of bitmap_t*/
929 writelong(ofp, pf->offset? pf->size: 0); /* # longs of offset*/ 1027 writelong(ofp, pf->offset? pf->size: 0); /* # longs of offset*/
930 writelong(ofp, pf->width? pf->size: 0); /* # bytes of width*/ 1028 writelong(ofp, pf->width? pf->size: 0); /* # bytes of width*/
931
932 /* variable font data*/ 1029 /* variable font data*/
1030#ifdef ROTATE
1031 for (i=0; i<pf->size; ++i)
1032 {
1033 bitmap_t* bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
1034 int width = pf->width ? pf->width[i] : pf->maxwidth;
1035 int size;
1036 unsigned char bytemap[256];
1037
1038 if (pf->offset &&
1039 (pf->offset[i] == pf->offset[pf->defaultchar-pf->firstchar])) {
1040 if (did_defaultchar)
1041 continue;
1042 did_defaultchar = 1;
1043 }
1044
1045 size = rotleft(bytemap, bits, width, pf->height);
1046 writestr(ofp, bytemap, size);
1047 }
1048
1049 if (ftell(ofp) & 1)
1050 writebyte(ofp, 0); /* pad to 16-bit boundary*/
1051
1052 if (pf->offset)
1053 for (i=0; i<pf->size; ++i)
1054 writeshort(ofp, pf->offrot[i]);
1055
1056 if (pf->width)
1057 for (i=0; i<pf->size; ++i)
1058 writebyte(ofp, pf->width[i]);
1059#else
933 for (i=0; i<pf->bits_size; ++i) 1060 for (i=0; i<pf->bits_size; ++i)
934 writeshort(ofp, pf->bits[i]); 1061 writeshort(ofp, pf->bits[i]);
935 if (ftell(ofp) & 2) 1062 if (ftell(ofp) & 2)
@@ -942,7 +1069,7 @@ int gen_fnt_file(struct font* pf, char *path)
942 if (pf->width) 1069 if (pf->width)
943 for (i=0; i<pf->size; ++i) 1070 for (i=0; i<pf->size; ++i)
944 writebyte(ofp, pf->width[i]); 1071 writebyte(ofp, pf->width[i]);
945 1072#endif
946 fclose(ofp); 1073 fclose(ofp);
947 return 0; 1074 return 0;
948} 1075}