2 * Convert BDF files to C source and/or Rockbox .fnt file format
4 * Copyright (c) 2002 by Greg Haerr <greg@censoft.com>
6 * What fun it is converting font data...
15 #define ROTATE /* define this for the new, rotated format */
18 /* loadable font magic and version #*/
20 #define VERSION "RB12" /* newer version */
22 #define VERSION "RB11"
25 /* bitmap_t helper macros*/
26 #define BITMAP_WORDS(x) (((x)+15)/16) /* image size in words*/
27 #define BITMAP_BYTES(x) (BITMAP_WORDS(x)*sizeof(bitmap_t))
28 #define BITMAP_BITSPERIMAGE (sizeof(bitmap_t) * 8)
29 #define BITMAP_BITVALUE(n) ((bitmap_t) (((bitmap_t) 1) << (n)))
30 #define BITMAP_FIRSTBIT (BITMAP_BITVALUE(BITMAP_BITSPERIMAGE - 1))
31 #define BITMAP_TESTBIT(m) ((m) & BITMAP_FIRSTBIT)
32 #define BITMAP_SHIFTBIT(m) ((bitmap_t) ((m) << 1))
34 typedef unsigned short bitmap_t
; /* bitmap image unit size*/
36 /* builtin C-based proportional/fixed font structure */
37 /* based on The Microwindows Project http://microwindows.org */
39 int maxwidth
; /* max width in pixels*/
40 int height
; /* height in pixels*/
41 int ascent
; /* ascent (baseline) height*/
42 int firstchar
; /* first character in bitmap*/
43 int size
; /* font size in glyphs*/
44 bitmap_t
* bits
; /* 16-bit right-padded bitmap data*/
45 unsigned int* offset
; /* offsets into bitmap data*/
46 unsigned char* width
; /* character widths or NULL if fixed*/
47 int defaultchar
; /* default char (not glyph index)*/
48 int bits_size
; /* # words of bitmap_t bits*/
50 /* unused by runtime system, read in by convbdf*/
51 unsigned int* offrot
; /* offsets into rotated bitmap data*/
52 char * name
; /* font name*/
53 char * facename
; /* facename of font*/
54 char * copyright
; /* copyright info for loadable fonts*/
57 int fbbw
, fbbh
, fbbx
, fbby
;
61 #define isprefix(buf,str) (!strncmp(buf, str, strlen(str)))
62 #define strequal(s1,s2) (!strcmp(s1, s2))
64 #define EXTRA 300 /* # bytes extra allocation for buggy .bdf files*/
71 int limit_char
= 65535;
76 void getopts(int *pac
, char ***pav
);
77 int convbdf(char *path
);
79 void free_font(struct font
* pf
);
80 struct font
* bdf_read_font(char *path
);
81 int bdf_read_header(FILE *fp
, struct font
* pf
);
82 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
);
83 char * bdf_getline(FILE *fp
, char *buf
, int len
);
84 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
);
86 int gen_c_source(struct font
* pf
, char *path
);
87 int gen_h_header(struct font
* pf
, char *path
);
88 int gen_fnt_file(struct font
* pf
, char *path
);
94 "Usage: convbdf [options] [input-files]\n"
95 " convbdf [options] [-o output-file] [single-input-file]\n"
97 " -c Convert .bdf to .c source file\n"
98 " -h Convert .bdf to .h header file (to create sysfont.h)\n"
99 " -f Convert .bdf to .fnt font file\n"
100 " -s N Start output at character encodings >= N\n"
101 " -l N Limit output to character encodings <= N\n"
102 " -n Don't generate bitmaps as comments in .c file\n"
105 fprintf(stderr
, help
);
108 /* parse command line options*/
109 void getopts(int *pac
, char ***pav
)
117 while (ac
> 0 && av
[0][0] == '-') {
121 case ' ': /* multiple -args on av[]*/
122 while( *p
&& *p
== ' ')
124 if( *p
++ != '-') /* next option must have dash*/
126 break; /* proceed to next option*/
127 case 'c': /* generate .c output*/
130 case 'h': /* generate .h output*/
133 case 'f': /* generate .fnt output*/
136 case 'n': /* don't gen bitmap comments*/
139 case 'o': /* set output file*/
143 while (*p
&& *p
!= ' ')
149 strcpy(outfile
, av
[0]);
152 case 'l': /* set encoding limit*/
154 limit_char
= atoi(p
);
155 while (*p
&& *p
!= ' ')
161 limit_char
= atoi(av
[0]);
164 case 's': /* set encoding start*/
166 start_char
= atoi(p
);
167 while (*p
&& *p
!= ' ')
173 start_char
= atoi(av
[0]);
177 fprintf(stderr
, "Unknown option ignored: %c\r\n", *(p
-1));
185 /* remove directory prefix and file suffix from full path*/
186 char *basename(char *path
)
189 static char base
[256];
191 /* remove prepended path and extension*/
193 for (p
=path
; *p
; ++p
) {
198 for (p
=base
; *p
; ++p
) {
207 int convbdf(char *path
)
212 pf
= bdf_read_font(path
);
218 strcpy(outfile
, basename(path
));
219 strcat(outfile
, ".c");
221 ret
|= gen_c_source(pf
, outfile
);
226 strcpy(outfile
, basename(path
));
227 strcat(outfile
, ".h");
229 ret
|= gen_h_header(pf
, outfile
);
234 strcpy(outfile
, basename(path
));
235 strcat(outfile
, ".fnt");
237 ret
|= gen_fnt_file(pf
, outfile
);
244 int main(int ac
, char **av
)
248 ++av
; --ac
; /* skip av[0]*/
249 getopts(&ac
, &av
); /* read command line options*/
251 if (ac
< 1 || (!gen_c
&& !gen_h
&& !gen_fnt
)) {
256 if (ac
> 1 || (gen_c
&& gen_fnt
) || (gen_c
&& gen_h
) || (gen_h
&& gen_fnt
)) {
263 ret
|= convbdf(av
[0]);
270 /* free font structure*/
271 void free_font(struct font
* pf
)
290 /* build incore structure from .bdf file*/
291 struct font
* bdf_read_font(char *path
)
296 fp
= fopen(path
, "rb");
298 fprintf(stderr
, "Error opening file: %s\n", path
);
302 pf
= (struct font
*)calloc(1, sizeof(struct font
));
306 pf
->name
= strdup(basename(path
));
308 if (!bdf_read_header(fp
, pf
)) {
309 fprintf(stderr
, "Error reading font header\n");
313 if (!bdf_read_bitmaps(fp
, pf
)) {
314 fprintf(stderr
, "Error reading font bitmaps\n");
327 /* read bdf font header information, return 0 on error*/
328 int bdf_read_header(FILE *fp
, struct font
* pf
)
331 int nchars
, maxwidth
;
332 int firstchar
= 65535;
338 /* set certain values to errors for later error checking*/
339 pf
->defaultchar
= -1;
344 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
345 fprintf(stderr
, "Error: EOF on file\n");
348 if (isprefix(buf
, "FONT ")) { /* not required*/
349 if (sscanf(buf
, "FONT %[^\n]", facename
) != 1) {
350 fprintf(stderr
, "Error: bad 'FONT'\n");
353 pf
->facename
= strdup(facename
);
356 if (isprefix(buf
, "COPYRIGHT ")) { /* not required*/
357 if (sscanf(buf
, "COPYRIGHT \"%[^\"]", copyright
) != 1) {
358 fprintf(stderr
, "Error: bad 'COPYRIGHT'\n");
361 pf
->copyright
= strdup(copyright
);
364 if (isprefix(buf
, "DEFAULT_CHAR ")) { /* not required*/
365 if (sscanf(buf
, "DEFAULT_CHAR %d", &pf
->defaultchar
) != 1) {
366 fprintf(stderr
, "Error: bad 'DEFAULT_CHAR'\n");
370 if (isprefix(buf
, "FONT_DESCENT ")) {
371 if (sscanf(buf
, "FONT_DESCENT %d", &pf
->descent
) != 1) {
372 fprintf(stderr
, "Error: bad 'FONT_DESCENT'\n");
377 if (isprefix(buf
, "FONT_ASCENT ")) {
378 if (sscanf(buf
, "FONT_ASCENT %d", &pf
->ascent
) != 1) {
379 fprintf(stderr
, "Error: bad 'FONT_ASCENT'\n");
384 if (isprefix(buf
, "FONTBOUNDINGBOX ")) {
385 if (sscanf(buf
, "FONTBOUNDINGBOX %d %d %d %d",
386 &pf
->fbbw
, &pf
->fbbh
, &pf
->fbbx
, &pf
->fbby
) != 4) {
387 fprintf(stderr
, "Error: bad 'FONTBOUNDINGBOX'\n");
392 if (isprefix(buf
, "CHARS ")) {
393 if (sscanf(buf
, "CHARS %d", &nchars
) != 1) {
394 fprintf(stderr
, "Error: bad 'CHARS'\n");
401 * Reading ENCODING is necessary to get firstchar/lastchar
402 * which is needed to pre-calculate our offset and widths
405 if (isprefix(buf
, "ENCODING ")) {
406 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
407 fprintf(stderr
, "Error: bad 'ENCODING'\n");
411 encoding
<= limit_char
&&
412 encoding
>= start_char
) {
414 if (firstchar
> encoding
)
415 firstchar
= encoding
;
416 if (lastchar
< encoding
)
421 if (strequal(buf
, "ENDFONT"))
425 /* calc font height*/
426 if (pf
->ascent
< 0 || pf
->descent
< 0 || firstchar
< 0) {
427 fprintf(stderr
, "Error: Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
430 pf
->height
= pf
->ascent
+ pf
->descent
;
432 /* calc default char*/
433 if (pf
->defaultchar
< 0 ||
434 pf
->defaultchar
< firstchar
||
435 pf
->defaultchar
> limit_char
||
436 pf
->defaultchar
> lastchar
)
437 pf
->defaultchar
= firstchar
;
439 /* calc font size (offset/width entries)*/
440 pf
->firstchar
= firstchar
;
441 pf
->size
= lastchar
- firstchar
+ 1;
443 /* use the font boundingbox to get initial maxwidth*/
444 /*maxwidth = pf->fbbw - pf->fbbx;*/
447 /* initially use font maxwidth * height for bits allocation*/
448 pf
->bits_size
= nchars
* BITMAP_WORDS(maxwidth
) * pf
->height
;
450 /* allocate bits, offset, and width arrays*/
451 pf
->bits
= (bitmap_t
*)malloc(pf
->bits_size
* sizeof(bitmap_t
) + EXTRA
);
452 pf
->offset
= (unsigned int *)malloc(pf
->size
* sizeof(unsigned int));
453 pf
->offrot
= (unsigned int *)malloc(pf
->size
* sizeof(unsigned int));
454 pf
->width
= (unsigned char *)malloc(pf
->size
* sizeof(unsigned char));
456 if (!pf
->bits
|| !pf
->offset
|| !pf
->offrot
|| !pf
->width
) {
457 fprintf(stderr
, "Error: no memory for font load\n");
461 fprintf(stderr
, "Header parsed\n");
465 /* read bdf font bitmaps, return 0 on error*/
466 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
)
471 int i
, k
, encoding
, width
;
472 int bbw
, bbh
, bbx
, bby
;
473 int proportional
= 0;
478 /* reset file pointer*/
479 fseek(fp
, 0L, SEEK_SET
);
481 /* initially mark offsets as not used*/
482 for (i
=0; i
<pf
->size
; ++i
)
486 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
487 fprintf(stderr
, "Error: EOF on file\n");
490 if (isprefix(buf
, "STARTCHAR")) {
491 encoding
= width
= bbw
= bbh
= bbx
= bby
= -1;
494 if (isprefix(buf
, "ENCODING ")) {
495 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
496 fprintf(stderr
, "Error: bad 'ENCODING'\n");
499 if (encoding
< start_char
|| encoding
> limit_char
)
503 if (isprefix(buf
, "DWIDTH ")) {
504 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
505 fprintf(stderr
, "Error: bad 'DWIDTH'\n");
508 /* use font boundingbox width if DWIDTH <= 0*/
510 width
= pf
->fbbw
- pf
->fbbx
;
513 if (isprefix(buf
, "BBX ")) {
514 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
515 fprintf(stderr
, "Error: bad 'BBX'\n");
520 if (strequal(buf
, "BITMAP") || strequal(buf
, "BITMAP ")) {
521 bitmap_t
*ch_bitmap
= pf
->bits
+ ofs
;
527 /* set bits offset in encode map*/
528 if (pf
->offset
[encoding
-pf
->firstchar
] != (unsigned int)-1) {
529 fprintf(stderr
, "Error: duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
533 pf
->offset
[encoding
-pf
->firstchar
] = ofs
;
534 pf
->offrot
[encoding
-pf
->firstchar
] = ofr
;
539 /*if (width > maxwidth)
543 if (width
> maxwidth
)
545 pf
->width
[encoding
-pf
->firstchar
] = width
;
548 memset(ch_bitmap
, 0, BITMAP_BYTES(width
) * pf
->height
);
550 ch_words
= BITMAP_WORDS(width
);
551 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
552 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
558 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
559 fprintf(stderr
, "Error: EOF reading BITMAP data\n");
562 if (isprefix(buf
, "ENDCHAR"))
565 hexnibbles
= strlen(buf
);
566 for (k
=0; k
<ch_words
; ++k
) {
567 int ndx
= k
* BITMAP_NIBBLES
;
568 int padnibbles
= hexnibbles
- ndx
;
573 if (padnibbles
>= BITMAP_NIBBLES
)
576 value
= bdf_hexval((unsigned char *)buf
,
577 ndx
, ndx
+BITMAP_NIBBLES
-1-padnibbles
);
578 value
<<= padnibbles
* BITMAP_NIBBLES
;
580 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
) |=
582 /* handle overflow into next image word*/
584 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
+1) =
585 value
<< (BITMAP_BITSPERIMAGE
- bbx
);
590 ofs
+= BITMAP_WORDS(width
) * pf
->height
;
591 ofr
+= pf
->width
[encoding
-pf
->firstchar
] * ((pf
->height
+7)/8);
595 if (strequal(buf
, "ENDFONT"))
600 pf
->maxwidth
= maxwidth
;
602 /* change unused width values to default char values*/
603 for (i
=0; i
<pf
->size
; ++i
) {
604 int defchar
= pf
->defaultchar
- pf
->firstchar
;
606 if (pf
->offset
[i
] == (unsigned int)-1)
607 pf
->width
[i
] = pf
->width
[defchar
];
610 /* determine whether font doesn't require encode table*/
613 for (i
=0; i
<pf
->size
; ++i
) {
614 if (pf
->offrot
[i
] != l
) {
618 l
+= pf
->maxwidth
* ((pf
->height
+ 7) / 8);
622 for (i
=0; i
<pf
->size
; ++i
) {
623 if (pf
->offset
[i
] != l
) {
627 l
+= BITMAP_WORDS(pf
->width
[i
]) * pf
->height
;
635 /* determine whether font is fixed-width*/
636 for (i
=0; i
<pf
->size
; ++i
) {
637 if (pf
->width
[i
] != maxwidth
) {
647 /* reallocate bits array to actual bits used*/
648 if (ofs
< pf
->bits_size
) {
649 pf
->bits
= realloc(pf
->bits
, ofs
* sizeof(bitmap_t
));
653 if (ofs
> pf
->bits_size
) {
654 fprintf(stderr
, "Warning: DWIDTH spec > max FONTBOUNDINGBOX\n");
655 if (ofs
> pf
->bits_size
+EXTRA
) {
656 fprintf(stderr
, "Error: Not enough bits initially allocated\n");
664 pf
->bits_size
= ofr
; /* always update, rotated is smaller */
670 /* read the next non-comment line, returns buf or NULL if EOF*/
671 char *bdf_getline(FILE *fp
, char *buf
, int len
)
678 while ((c
= getc(fp
)) != EOF
) {
683 if (b
- buf
>= (len
- 1))
688 if (c
== EOF
&& b
== buf
)
690 if (b
!= buf
&& !isprefix(buf
, "COMMENT"))
696 /* return hex value of portion of buffer*/
697 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
)
702 for (i
=ndx1
; i
<=ndx2
; ++i
) {
704 if (c
>= '0' && c
<= '9')
707 if (c
>= 'A' && c
<= 'F')
710 if (c
>= 'a' && c
<= 'f')
714 val
= (val
<< 4) | c
;
720 * Take an bitmap_t bitmap and convert to Rockbox format.
721 * Used for converting font glyphs for the time being.
722 * Can use for standard X11 and Win32 images as well.
723 * See format description in lcd-recorder.c
725 * Doing it this way keeps fonts in standard formats,
726 * as well as keeping Rockbox hw bitmap format.
728 int rotleft(unsigned char *dst
, /* output buffer */
729 size_t dstlen
, /* buffer size */
730 bitmap_t
*src
, unsigned int width
, unsigned int height
)
733 unsigned int src_words
; /* # words of input image*/
734 unsigned int dst_mask
; /* bit mask for destination */
735 bitmap_t src_mask
; /* bit mask for source */
737 /* calc words of input image*/
738 src_words
= BITMAP_WORDS(width
) * height
;
740 if(((height
+ 7) / 8) * width
> dstlen
) {
741 fprintf(stderr
, "%s:%d %d x %d overflows %d bytes buffer, needs %d\n",
742 __FILE__
, __LINE__
, width
, height
, dstlen
,
743 ((height
+ 7) / 8) * width
);
747 /* clear background*/
748 memset(dst
, 0, ((height
+ 7) / 8) * width
);
752 for (i
=0; i
< src_words
; i
++) {
754 /* calc src input bit*/
755 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
757 /* for each input column...*/
758 for(j
=0; j
< width
; j
++) {
760 if (src_mask
== 0) /* input word done? */
762 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
763 i
++; /* next input word */
766 /* if set in input, set in rotated output */
767 if (src
[i
] & src_mask
)
770 src_mask
>>= 1; /* next input bit */
773 dst_mask
<<= 1; /* next output bit (row) */
774 if (dst_mask
> (1 << 7)) /* output bit > 7? */
777 dst
+= width
; /* next output byte row */
780 return ((height
+ 7) / 8) * width
; /* return result size in bytes */
784 /* generate C source from in-core font*/
785 int gen_c_source(struct font
* pf
, char *path
)
791 bitmap_t
*ofs
= pf
->bits
;
795 "/* Generated by convbdf on %s. */\n"
796 "#include \"font.h\"\n"
797 "#ifdef HAVE_LCD_BITMAP\n"
799 "/* Font information:\n"
806 " first char: %d (0x%02x)\n"
807 " last char: %d (0x%02x)\n"
808 " default char: %d (0x%02x)\n"
809 " proportional: %s\n"
813 "/* Font character bitmap data. */\n"
814 "static const unsigned char _font_bits[] = {\n"
817 ofp
= fopen(path
, "w");
819 fprintf(stderr
, "Can't create %s\n", path
);
823 strcpy(buf
, ctime(&t
));
824 buf
[strlen(buf
)-1] = 0;
826 fprintf(ofp
, hdr1
, buf
,
828 pf
->facename
? pf
->facename
: "",
829 pf
->maxwidth
, pf
->height
,
831 pf
->ascent
, pf
->descent
,
832 pf
->firstchar
, pf
->firstchar
,
833 pf
->firstchar
+pf
->size
-1, pf
->firstchar
+pf
->size
-1,
834 pf
->defaultchar
, pf
->defaultchar
,
835 pf
->width
? "yes": "no",
836 pf
->copyright
? pf
->copyright
: "");
838 /* generate bitmaps*/
839 for (i
=0; i
<pf
->size
; ++i
) {
842 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
843 int height
= pf
->height
;
847 /* Skip missing glyphs */
848 if (pf
->offset
&& (pf
->offset
[i
] == (unsigned int)-1))
851 bits
= pf
->bits
+ (pf
->offset
? pf
->offset
[i
]: (pf
->height
* i
));
853 fprintf(ofp
, "\n/* Character %d (0x%02x):\n width %d",
854 i
+pf
->firstchar
, i
+pf
->firstchar
, width
);
857 fprintf(ofp
, "\n +");
858 for (x
=0; x
<width
; ++x
) fprintf(ofp
, "-");
863 if (x
== 0) fprintf(ofp
, " |");
866 bitcount
= BITMAP_BITSPERIMAGE
;
870 fprintf(ofp
, BITMAP_TESTBIT(bitvalue
)? "*": " ");
872 bitvalue
= BITMAP_SHIFTBIT(bitvalue
);
882 for (x
=0; x
<width
; ++x
)
884 fprintf(ofp
, "+ */\n");
887 fprintf(ofp
, " */\n");
889 bits
= pf
->bits
+ (pf
->offset
? pf
->offset
[i
]: (pf
->height
* i
));
890 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
892 unsigned char bytemap
[512];
895 int size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
,
897 for (y8
=0; y8
<pf
->height
; y8
+=8) /* column rows */
899 for (x
=0; x
<width
; x
++) {
900 fprintf(ofp
, "0x%02x, ", bytemap
[ix
]);
906 /* update offrot since bits are now in sorted order */
912 for (x
=BITMAP_WORDS(width
)*pf
->height
; x
>0; --x
) {
913 fprintf(ofp
, "0x%04x,\n", *bits
);
914 if (!did_syncmsg
&& *bits
++ != *ofs
++) {
915 fprintf(stderr
, "Warning: found encoding values in non-sorted order (not an error).\n");
921 fprintf(ofp
, "};\n\n");
924 /* output offset table*/
925 fprintf(ofp
, "/* Character->glyph mapping. */\n"
926 "static const unsigned short _sysfont_offset[] = {\n");
928 for (i
=0; i
<pf
->size
; ++i
) {
929 if (pf
->offset
[i
] == (unsigned int)-1) {
930 pf
->offset
[i
] = pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
931 pf
->offrot
[i
] = pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
933 fprintf(ofp
, " %ld,\t/* (0x%02x) */\n",
935 pf
->offrot
[i
], i
+pf
->firstchar
);
937 pf
->offset
[i
], i
+pf
->firstchar
);
940 fprintf(ofp
, "};\n\n");
943 /* output width table for proportional fonts*/
945 fprintf(ofp
, "/* Character width data. */\n"
946 "static const unsigned char _sysfont_width[] = {\n");
948 for (i
=0; i
<pf
->size
; ++i
)
949 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
950 pf
->width
[i
], i
+pf
->firstchar
);
951 fprintf(ofp
, "};\n\n");
954 /* output struct font struct*/
956 sprintf(obuf
, "_sysfont_offset,");
958 sprintf(obuf
, "0, /* no encode table */");
961 sprintf(buf
, "_sysfont_width, /* width */");
963 sprintf(buf
, "0, /* fixed width */");
965 fprintf(ofp
, "/* Exported structure definition. */\n"
966 "const struct font sysfont = {\n"
967 " %d, /* maxwidth */\n"
968 " %d, /* height */\n"
969 " %d, /* ascent */\n"
970 " %d, /* firstchar */\n"
972 " _font_bits, /* bits */\n"
975 " %d, /* defaultchar */\n"
976 " %d /* bits_size */\n"
978 "#endif /* HAVE_LCD_BITMAP */\n",
979 pf
->maxwidth
, pf
->height
,
991 /* generate C header from in-core font*/
992 int gen_h_header(struct font
* pf
, char *path
)
998 "/* Generated by convbdf on %s. */\n"
999 "#ifdef HAVE_LCD_BITMAP\n"
1001 "/* Font information*/\n"
1002 "#define SYSFONT_NAME %s\n"
1003 "#define SYSFONT_FACENAME %s\n"
1004 "#define SYSFONT_WIDTH %d\n"
1005 "#define SYSFONT_HEIGHT %d\n"
1006 "#define SYSFONT_SIZE %d\n"
1007 "#define SYSFONT_ASCENT %d\n"
1008 "#define SYSFONT_DESCENT %d\n"
1009 "#define SYSFONT_FIRST_CHAR %d\n"
1010 "#define SYSFONT_LAST_CHAR %d\n"
1011 "#define SYSFONT_DEFAULT_CHAR %d\n"
1012 "#define SYSFONT_PROPORTIONAL %d\n"
1013 "#define SYSFONT_COPYRIGHT %s\n"
1014 "#define SYSFONT_BITS_SIZE %d\n"
1019 ofp
= fopen(path
, "w");
1021 fprintf(stderr
, "Can't create %s\n", path
);
1025 strcpy(buf
, ctime(&t
));
1026 buf
[strlen(buf
)-1] = 0;
1028 fprintf(ofp
, hdr1
, buf
,
1030 pf
->facename
? pf
->facename
: "",
1037 pf
->firstchar
+pf
->size
-1,
1040 pf
->copyright
? pf
->copyright
: "",
1046 static int writebyte(FILE *fp
, unsigned char c
)
1048 return putc(c
, fp
) != EOF
;
1051 static int writeshort(FILE *fp
, unsigned short s
)
1054 return putc(s
>>8, fp
) != EOF
;
1057 static int writeint(FILE *fp
, unsigned int l
)
1062 return putc(l
>>24, fp
) != EOF
;
1065 static int writestr(FILE *fp
, char *str
, int count
)
1067 return fwrite(str
, 1, count
, fp
) == count
;
1070 static int writestrpad(FILE *fp
, char *str
, int totlen
)
1074 while (str
&& *str
&& totlen
> 0) {
1076 ret
= putc(*str
++, fp
);
1080 while (--totlen
>= 0)
1081 ret
= putc(' ', fp
);
1085 /* generate .fnt format file from in-core font*/
1086 int gen_fnt_file(struct font
* pf
, char *path
)
1094 ofp
= fopen(path
, "wb");
1096 fprintf(stderr
, "Can't create %s\n", path
);
1100 /* write magic and version #*/
1101 writestr(ofp
, VERSION
, 4);
1103 /* internal font name*/
1104 writestrpad(ofp
, pf
->name
, 64);
1107 writestrpad(ofp
, pf
->copyright
, 256);
1110 writeshort(ofp
, pf
->maxwidth
);
1111 writeshort(ofp
, pf
->height
);
1112 writeshort(ofp
, pf
->ascent
);
1114 writeint(ofp
, pf
->firstchar
);
1115 writeint(ofp
, pf
->defaultchar
);
1116 writeint(ofp
, pf
->size
);
1118 /* variable font data sizes*/
1119 writeint(ofp
, pf
->bits_size
); /* # words of bitmap_t*/
1120 writeint(ofp
, pf
->offset
? pf
->size
: 0); /* # ints of offset*/
1121 writeint(ofp
, pf
->width
? pf
->size
: 0); /* # bytes of width*/
1122 /* variable font data*/
1124 for (i
=0; i
<pf
->size
; ++i
)
1127 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1129 unsigned char bytemap
[512];
1131 /* Skip missing glyphs */
1132 if (pf
->offset
&& (pf
->offset
[i
] == (unsigned int)-1))
1135 bits
= pf
->bits
+ (pf
->offset
? pf
->offset
[i
]: (pf
->height
* i
));
1137 size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
, pf
->height
);
1138 writestr(ofp
, (char *)bytemap
, size
);
1140 /* update offrot since bits are now in sorted order */
1141 pf
->offrot
[i
] = ofr
;
1145 if ( pf
->bits_size
< 0xFFDB )
1147 /* bitmap offset is small enough, use unsigned short for offset */
1149 writebyte(ofp
, 0); /* pad to 16-bit boundary*/
1153 /* bitmap offset is large then 64K, use unsigned int for offset */
1154 while (ftell(ofp
) & 3)
1155 writebyte(ofp
, 0); /* pad to 32-bit boundary*/
1160 for (i
=0; i
<pf
->size
; ++i
)
1162 if (pf
->offset
[i
] == (unsigned int)-1) {
1163 pf
->offrot
[i
] = pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1165 if ( pf
->bits_size
< 0xFFDB )
1166 writeshort(ofp
, pf
->offrot
[i
]);
1168 writeint(ofp
, pf
->offrot
[i
]);
1173 for (i
=0; i
<pf
->size
; ++i
)
1174 writebyte(ofp
, pf
->width
[i
]);
1176 for (i
=0; i
<pf
->bits_size
; ++i
)
1177 writeshort(ofp
, pf
->bits
[i
]);
1179 writeshort(ofp
, 0); /* pad to 32-bit boundary*/
1182 for (i
=0; i
<pf
->size
; ++i
) {
1183 if (pf
->offset
[i
] == (unsigned int)-1) {
1184 pf
->offset
[i
] = pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1186 writeint(ofp
, pf
->offset
[i
]);
1190 for (i
=0; i
<pf
->size
; ++i
)
1191 writebyte(ofp
, pf
->width
[i
]);