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...
16 #define ROTATE /* define this for the new, rotated format */
19 /* loadable font magic and version #*/
21 #define VERSION "RB12" /* newer version */
23 #define VERSION "RB11"
27 * bitmap_t helper macros
29 typedef unsigned short bitmap_t
; /* bitmap image unit size*/
31 /* Number of words to hold a pixel line of width x pixels */
32 #define BITMAP_BITSPERIMAGE (sizeof(bitmap_t) * 8)
33 #define BITMAP_WORDS(x) (((x)+BITMAP_BITSPERIMAGE-1)/BITMAP_BITSPERIMAGE)
34 #define BITMAP_BYTES(x) (BITMAP_WORDS(x)*sizeof(bitmap_t))
35 #define BITMAP_BITVALUE(n) ((bitmap_t) (((bitmap_t) 1) << (n)))
36 #define BITMAP_FIRSTBIT (BITMAP_BITVALUE(BITMAP_BITSPERIMAGE - 1))
37 #define BITMAP_TESTBIT(m) ((m) & BITMAP_FIRSTBIT)
38 #define BITMAP_SHIFTBIT(m) ((bitmap_t) ((m) << 1))
41 /* builtin C-based proportional/fixed font structure */
42 /* based on The Microwindows Project http://microwindows.org */
44 int maxwidth
; /* max width in pixels */
45 int height
; /* height in pixels */
46 int ascent
; /* ascent (baseline) height */
47 int firstchar
; /* first character in bitmap */
48 int size
; /* font size in glyphs ('holes' included) */
49 bitmap_t
* bits
; /* 16-bit right-padded bitmap data */
50 int* offset
; /* offsets into bitmap data */
51 unsigned char* width
; /* character widths or NULL if fixed */
52 int defaultchar
; /* default char (not glyph index) */
53 int bits_size
; /* # words of bitmap_t bits */
55 /* unused by runtime system, read in by convbdf */
56 int nchars
; /* number of different glyphs */
57 int nchars_declared
; /* number of glyphs as declared in the header */
58 int ascent_declared
; /* ascent as declared in the header */
59 int descent_declared
; /* descent as declared in the header */
60 int max_char_ascent
; /* max. char ascent (before adjusting) */
61 int max_char_descent
; /* max. char descent (before adjusting) */
62 unsigned int* offrot
; /* offsets into rotated bitmap data */
63 char* name
; /* font name */
64 char* facename
; /* facename of font */
65 char* copyright
; /* copyright info for loadable fonts */
68 int fbbw
, fbbh
, fbbx
, fbby
;
70 /* Max 'overflow' of a char's ascent (descent) over the font's one */
71 int max_over_ascent
, max_over_descent
;
73 /* The number of clipped ascents/descents/total */
74 int num_clipped_ascent
, num_clipped_descent
, num_clipped
;
76 /* default width in pixels (can be overwritten at char level) */
81 /* Description of how the ascent/descent is allowed to grow */
83 int value
; /* The delta value (in pixels or percents) */
84 int percent
; /* Is the value in percents (true) or pixels (false)? */
85 int force
; /* MUST the value be set (true) or is it just a max (false) */
88 #define isprefix(buf,str) (!strncmp(buf, str, strlen(str)))
89 #define strequal(s1,s2) (!strcmp(s1, s2))
91 #define MAX(a,b) ((a) > (b) ? (a) : (b))
92 #define MIN(a,b) ((a) < (b) ? (a) : (b))
95 #define ROTATION_BUF_SIZE 2048
98 /* Depending on the verbosity level some warnings are printed or not */
99 int verbosity_level
= 0;
102 /* Prints a warning of the specified verbosity level. It will only be
103 really printed if the level is >= the level set in the settings */
104 void print_warning(int level
, const char *fmt
, ...);
105 void print_error(const char *fmt
, ...);
106 void print_info(const char *fmt
, ...);
107 void print_trace(const char *fmt
, ...);
108 #define VL_CLIP_FONT 1 /* Verbosity level for clip related warnings at font level */
109 #define VL_CLIP_CHAR 2 /* Verbosity level for clip related warnings at char level */
110 #define VL_MISC 1 /* Verbosity level for other warnings */
117 int limit_char
= 65535;
121 struct stretch stretch_ascent
= { 0, 0, 1 }; /* Don't allow ascent to grow by default */
122 struct stretch stretch_descent
= { 0, 0, 1 }; /* Don't allow descent to grow by default */
126 void getopts(int *pac
, char ***pav
);
127 int convbdf(char *path
);
129 void free_font(struct font
* pf
);
130 struct font
* bdf_read_font(char *path
);
131 int bdf_read_header(FILE *fp
, struct font
* pf
);
132 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
);
135 Counts the glyphs and determines the max dimensions of glyphs
136 (fills the fields nchars, maxwidth, max_over_ascent, max_over_descent).
137 Returns 0 on failure or not-0 on success.
139 int bdf_analyze_font(FILE *fp
, struct font
* pf
);
140 void bdf_correct_bbx(int *width
, int *bbx
); /* Corrects bbx and width if bbx<0 */
142 /* Corrects the ascent and returns the new value (value to use) */
143 int adjust_ascent(int ascent
, int overflow
, struct stretch
*stretch
);
145 char * bdf_getline(FILE *fp
, char *buf
, int len
);
146 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
);
148 int gen_c_source(struct font
* pf
, char *path
);
149 int gen_h_header(struct font
* pf
, char *path
);
150 int gen_fnt_file(struct font
* pf
, char *path
);
155 /* We use string array because some C compilers issue warnings about too long strings */
157 "Usage: convbdf [options] [input-files]\n",
158 " convbdf [options] [-o output-file] [single-input-file]\n",
160 " -c Convert .bdf to .c source file\n",
161 " -h Convert .bdf to .h header file (to create sysfont.h)\n",
162 " -f Convert .bdf to .fnt font file\n",
163 " -s N Start output at character encodings >= N\n",
164 " -l N Limit output to character encodings <= N\n",
165 " -n Don't generate bitmaps as comments in .c file\n",
166 " -a N[%][!] Allow the ascent to grow N pixels/% to avoid glyph clipping\n",
167 " -d N[%][!] Allow the descent to grow N pixels/% to avoid glyph clipping\n",
168 " -v N Verbosity level: 0=quite quiet, 1=more verbose, 2=even more, etc.\n",
169 " -t Print internal tracing messages\n",
170 NULL
/* Must be the last element in the array */
175 print_info("%s", *(p
++));
179 void parse_ascent_opt(char *val
, struct stretch
*opt
) {
186 p
= buf
+ strlen(buf
);
193 else if (*p
== '!') {
201 opt
->value
= atoi(buf
);
204 /* parse command line options*/
205 void getopts(int *pac
, char ***pav
)
213 while (ac
> 0 && av
[0][0] == '-') {
217 case ' ': /* multiple -args on av[] */
218 while( *p
&& *p
== ' ')
220 if( *p
++ != '-') /* next option must have dash */
222 break; /* proceed to next option */
223 case 'c': /* generate .c output */
226 case 'h': /* generate .h output */
229 case 'f': /* generate .fnt output */
232 case 'n': /* don't gen bitmap comments */
235 case 'o': /* set output file */
239 while (*p
&& *p
!= ' ')
245 strcpy(outfile
, av
[0]);
248 case 'l': /* set encoding limit */
250 limit_char
= atoi(p
);
251 while (*p
&& *p
!= ' ')
257 limit_char
= atoi(av
[0]);
260 case 's': /* set encoding start */
262 start_char
= atoi(p
);
263 while (*p
&& *p
!= ' ')
269 start_char
= atoi(av
[0]);
272 case 'a': /* ascent growth */
274 parse_ascent_opt(p
, &stretch_ascent
);
275 while (*p
&& *p
!= ' ')
281 parse_ascent_opt(av
[0], &stretch_ascent
);
284 case 'd': /* descent growth */
286 parse_ascent_opt(p
, &stretch_descent
);
287 while (*p
&& *p
!= ' ')
293 parse_ascent_opt(av
[0], &stretch_descent
);
296 case 'v': /* verbosity */
298 verbosity_level
= atoi(p
);
299 while (*p
&& *p
!= ' ')
305 verbosity_level
= atoi(av
[0]);
308 case 't': /* tracing */
312 print_info("Unknown option ignored: %c\n", *(p
-1));
320 void print_warning(int level
, const char *fmt
, ...) {
321 if (verbosity_level
>= level
) {
324 fprintf(stderr
, " WARN: ");
325 vfprintf(stderr
, fmt
, ap
);
330 void print_trace(const char *fmt
, ...) {
334 fprintf(stderr
, "TRACE: ");
335 vfprintf(stderr
, fmt
, ap
);
340 void print_error(const char *fmt
, ...) {
343 fprintf(stderr
, "ERROR: ");
344 vfprintf(stderr
, fmt
, ap
);
348 void print_info(const char *fmt
, ...) {
351 fprintf(stderr
, " INFO: ");
352 vfprintf(stderr
, fmt
, ap
);
356 /* remove directory prefix and file suffix from full path */
357 char *basename(char *path
)
360 static char base
[256];
362 /* remove prepended path and extension */
364 for (p
=path
; *p
; ++p
) {
369 for (p
=base
; *p
; ++p
) {
378 int convbdf(char *path
)
383 pf
= bdf_read_font(path
);
389 strcpy(outfile
, basename(path
));
390 strcat(outfile
, ".c");
392 ret
|= gen_c_source(pf
, outfile
);
397 strcpy(outfile
, basename(path
));
398 strcat(outfile
, ".h");
400 ret
|= gen_h_header(pf
, outfile
);
405 strcpy(outfile
, basename(path
));
406 strcat(outfile
, ".fnt");
408 ret
|= gen_fnt_file(pf
, outfile
);
415 int main(int ac
, char **av
)
419 ++av
; --ac
; /* skip av[0] */
420 getopts(&ac
, &av
); /* read command line options */
422 if (ac
< 1 || (!gen_c
&& !gen_h
&& !gen_fnt
)) {
427 if (gen_c
&& gen_fnt
) {
428 print_info(".c and .fnt files can not be produced in the same run!\n");
433 if (ac
> 1 || (gen_c
&& gen_fnt
) || (gen_c
&& gen_h
) || (gen_h
&& gen_fnt
)) {
440 ret
|= convbdf(av
[0]);
447 /* free font structure */
448 void free_font(struct font
* pf
)
467 /* build incore structure from .bdf file */
468 struct font
* bdf_read_font(char *path
)
473 fp
= fopen(path
, "rb");
475 print_error("Error opening file: %s\n", path
);
479 pf
= (struct font
*)calloc(1, sizeof(struct font
));
482 memset(pf
, 0, sizeof(struct font
));
484 pf
->name
= strdup(basename(path
));
486 if (!bdf_read_header(fp
, pf
)) {
487 print_error("Error reading font header\n");
490 print_trace("Read font header, nchars_decl=%d\n", pf
->nchars_declared
);
492 if (!bdf_analyze_font(fp
, pf
)) {
493 print_error("Error analyzing the font\n");
496 print_trace("Analyzed font, nchars=%d, maxwidth=%d, asc_over=%d, desc_over=%d\n",
497 pf
->nchars
, pf
->maxwidth
, pf
->max_over_ascent
, pf
->max_over_descent
);
499 if (pf
->nchars
!= pf
->nchars_declared
) {
500 print_warning(VL_MISC
, "The declared number of chars (%d) "
501 "does not match the real number (%d)\n",
502 pf
->nchars_declared
, pf
->nchars
);
505 /* Correct ascent/descent if necessary */
506 pf
->ascent
= adjust_ascent(pf
->ascent_declared
, pf
->max_over_ascent
, &stretch_ascent
);
507 if (pf
->ascent
!= pf
->ascent_declared
) {
508 print_info("Font ascent has been changed from %d to %d\n",
509 pf
->ascent_declared
, pf
->ascent
);
511 pf
->descent
= adjust_ascent(pf
->descent
, pf
->max_over_descent
, &stretch_descent
);
512 if (pf
->descent
!= pf
->descent_declared
) {
513 print_info("Font descent has been changed from %d to %d\n",
514 pf
->descent_declared
, pf
->descent
);
516 pf
->height
= pf
->ascent
+ pf
->descent
;
517 if (pf
->height
!= pf
->ascent_declared
+ pf
->descent_declared
) {
518 print_warning(VL_CLIP_FONT
, "Generated font's height: %d\n", pf
->height
);
521 if (pf
->ascent
> pf
->max_char_ascent
) {
522 print_trace("Font's ascent could be reduced by %d to %d without clipping\n",
523 (pf
->ascent
- pf
->max_char_ascent
), pf
->max_char_ascent
);
525 if (pf
->descent
> pf
->max_char_descent
) {
526 print_trace("Font's descent could be reduced by %d to %d without clipping\n",
527 (pf
->descent
- pf
->max_char_descent
), pf
->max_char_descent
);
532 pf
->bits_size
= pf
->size
* BITMAP_WORDS(pf
->maxwidth
) * pf
->height
;
533 pf
->bits
= (bitmap_t
*)malloc(pf
->bits_size
* sizeof(bitmap_t
));
534 pf
->offset
= (int *)malloc(pf
->size
* sizeof(int));
535 pf
->offrot
= (unsigned int *)malloc(pf
->size
* sizeof(unsigned int));
536 pf
->width
= (unsigned char *)malloc(pf
->size
* sizeof(unsigned char));
538 if (!pf
->bits
|| !pf
->offset
|| !pf
->offrot
|| !pf
->width
) {
539 print_error("no memory for font load\n");
543 pf
->num_clipped_ascent
= pf
->num_clipped_descent
= pf
->num_clipped
= 0;
544 pf
->max_over_ascent
= pf
->max_over_descent
= 0;
546 if (!bdf_read_bitmaps(fp
, pf
)) {
547 print_error("Error reading font bitmaps\n");
550 print_trace("Read bitmaps\n");
552 if (pf
->num_clipped
> 0) {
553 print_warning(VL_CLIP_FONT
, "%d character(s) out of %d were clipped "
554 "(%d at ascent, %d at descent)\n",
555 pf
->num_clipped
, pf
->nchars
,
556 pf
->num_clipped_ascent
, pf
->num_clipped_descent
);
557 print_warning(VL_CLIP_FONT
, "max overflows: %d pixel(s) at ascent, %d pixel(s) at descent\n",
558 pf
->max_over_ascent
, pf
->max_over_descent
);
570 /* read bdf font header information, return 0 on error */
571 int bdf_read_header(FILE *fp
, struct font
* pf
)
574 int firstchar
= 65535;
581 /* set certain values to errors for later error checking */
582 pf
->defaultchar
= -1;
585 pf
->default_width
= -1;
588 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
589 print_error("EOF on file\n");
592 if (isprefix(buf
, "FONT ")) { /* not required */
593 if (sscanf(buf
, "FONT %[^\n]", facename
) != 1) {
594 print_error("bad 'FONT'\n");
597 pf
->facename
= strdup(facename
);
600 if (isprefix(buf
, "COPYRIGHT ")) { /* not required */
601 if (sscanf(buf
, "COPYRIGHT \"%[^\"]", copyright
) != 1) {
602 print_error("bad 'COPYRIGHT'\n");
605 pf
->copyright
= strdup(copyright
);
608 if (isprefix(buf
, "DEFAULT_CHAR ")) { /* not required */
609 if (sscanf(buf
, "DEFAULT_CHAR %d", &pf
->defaultchar
) != 1) {
610 print_error("bad 'DEFAULT_CHAR'\n");
614 if (isprefix(buf
, "FONT_DESCENT ")) {
615 if (sscanf(buf
, "FONT_DESCENT %d", &pf
->descent_declared
) != 1) {
616 print_error("bad 'FONT_DESCENT'\n");
619 pf
->descent
= pf
->descent_declared
; /* For now */
622 if (isprefix(buf
, "FONT_ASCENT ")) {
623 if (sscanf(buf
, "FONT_ASCENT %d", &pf
->ascent_declared
) != 1) {
624 print_error("bad 'FONT_ASCENT'\n");
627 pf
->ascent
= pf
->ascent_declared
; /* For now */
630 if (isprefix(buf
, "FONTBOUNDINGBOX ")) {
631 if (sscanf(buf
, "FONTBOUNDINGBOX %d %d %d %d",
632 &pf
->fbbw
, &pf
->fbbh
, &pf
->fbbx
, &pf
->fbby
) != 4) {
633 print_error("bad 'FONTBOUNDINGBOX'\n");
638 if (isprefix(buf
, "CHARS ")) {
639 if (sscanf(buf
, "CHARS %d", &pf
->nchars_declared
) != 1) {
640 print_error("bad 'CHARS'\n");
645 if (isprefix(buf
, "STARTCHAR")) {
650 /* for BDF version 2.2 */
651 if (is_header
&& isprefix(buf
, "DWIDTH ")) {
652 if (sscanf(buf
, "DWIDTH %d", &pf
->default_width
) != 1) {
653 print_error("bad 'DWIDTH' at font level\n");
660 * Reading ENCODING is necessary to get firstchar/lastchar
661 * which is needed to pre-calculate our offset and widths
664 if (isprefix(buf
, "ENCODING ")) {
665 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
666 print_error("bad 'ENCODING'\n");
670 encoding
<= limit_char
&&
671 encoding
>= start_char
) {
673 if (firstchar
> encoding
)
674 firstchar
= encoding
;
675 if (lastchar
< encoding
)
680 if (strequal(buf
, "ENDFONT"))
684 /* calc font height*/
685 if (pf
->ascent
< 0 || pf
->descent
< 0 || firstchar
< 0) {
686 print_error("Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
689 pf
->height
= pf
->ascent
+ pf
->descent
;
691 /* calc default char */
692 if (pf
->defaultchar
< 0 ||
693 pf
->defaultchar
< firstchar
||
694 pf
->defaultchar
> limit_char
||
695 pf
->defaultchar
> lastchar
)
696 pf
->defaultchar
= firstchar
;
698 /* calc font size (offset/width entries) */
699 pf
->firstchar
= firstchar
;
700 pf
->size
= lastchar
- firstchar
+ 1;
706 * TODO: rework the code to avoid logics duplication in
707 * bdf_read_bitmaps and bdf_analyze_font
711 /* read bdf font bitmaps, return 0 on error */
712 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
)
716 int i
, k
, encoding
, width
;
717 int bbw
, bbh
, bbx
, bby
;
718 int proportional
= 0;
725 /* reset file pointer */
726 fseek(fp
, 0L, SEEK_SET
);
728 /* initially mark offsets as not used */
729 for (i
=0; i
<pf
->size
; ++i
)
733 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
734 print_error("EOF on file\n");
737 if (isprefix(buf
, "STARTCHAR")) {
738 encoding
= width
= -1;
745 if (isprefix(buf
, "ENCODING ")) {
746 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
747 print_error("bad 'ENCODING'\n");
750 if (encoding
< start_char
|| encoding
> limit_char
)
754 if (isprefix(buf
, "DWIDTH ")) {
755 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
756 print_error("bad 'DWIDTH'\n");
759 /* use font boundingbox width if DWIDTH <= 0 */
761 width
= pf
->fbbw
- pf
->fbbx
;
764 if (isprefix(buf
, "BBX ")) {
765 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
766 print_error("bad 'BBX'\n");
771 if (strequal(buf
, "BITMAP") || strequal(buf
, "BITMAP ")) {
772 int overflow_asc
, overflow_desc
;
773 int bbh_orig
, bby_orig
, y
;
778 if (width
< 0 && pf
->default_width
> 0)
779 width
= pf
->default_width
;
781 /* set bits offset in encode map*/
782 if (pf
->offset
[encoding
-pf
->firstchar
] != -1) {
783 print_error("duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
787 pf
->offset
[encoding
-pf
->firstchar
] = ofs
;
788 pf
->offrot
[encoding
-pf
->firstchar
] = ofr
;
790 /* calc char width */
791 bdf_correct_bbx(&width
, &bbx
);
792 pf
->width
[encoding
-pf
->firstchar
] = width
;
794 ch_bitmap
= pf
->bits
+ ofs
;
795 ch_words
= BITMAP_WORDS(width
);
796 memset(ch_bitmap
, 0, BITMAP_BYTES(width
) * pf
->height
); /* clear bitmap */
798 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
799 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
804 overflow_asc
= bby
+ bbh
- pf
->ascent
;
805 if (overflow_asc
> 0) {
806 pf
->num_clipped_ascent
++;
807 if (overflow_asc
> pf
->max_over_ascent
) {
808 pf
->max_over_ascent
= overflow_asc
;
810 bbh
= MAX(bbh
- overflow_asc
, 0); /* Clipped -> decrease the height */
811 print_warning(VL_CLIP_CHAR
, "character %d goes %d pixel(s)"
812 " beyond the font's ascent, it will be clipped\n",
813 encoding
, overflow_asc
);
815 overflow_desc
= -bby
- pf
->descent
;
816 if (overflow_desc
> 0) {
817 pf
->num_clipped_descent
++;
818 if (overflow_desc
> pf
->max_over_descent
) {
819 pf
->max_over_descent
= overflow_desc
;
821 bby
+= overflow_desc
;
822 bbh
= MAX(bbh
- overflow_desc
, 0); /* Clipped -> decrease the height */
823 print_warning(VL_CLIP_CHAR
, "character %d goes %d pixel(s)"
824 " beyond the font's descent, it will be clipped\n",
825 encoding
, overflow_desc
);
827 if (overflow_asc
> 0 || overflow_desc
> 0) {
831 y
= bby_orig
+ bbh_orig
; /* 0-based y within the char */
837 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
838 print_error("EOF reading BITMAP data for character %d\n",
842 if (isprefix(buf
, "ENDCHAR"))
846 if ((y
>= pf
->ascent
) || (y
< -pf
->descent
)) {
847 /* We're beyond the area that Rockbox can render -> clip */
848 --i
; /* This line doesn't count */
852 hexnibbles
= strlen(buf
);
853 for (k
=0; k
<ch_words
; ++k
) {
854 int ndx
= k
* BITMAP_NIBBLES
;
855 int padnibbles
= hexnibbles
- ndx
;
860 if (padnibbles
>= (int)BITMAP_NIBBLES
)
863 value
= bdf_hexval((unsigned char *)buf
,
864 ndx
, ndx
+BITMAP_NIBBLES
-1-padnibbles
);
865 value
<<= padnibbles
* BITMAP_NIBBLES
;
867 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
) |=
869 /* handle overflow into next image word */
871 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
+1) =
872 value
<< (BITMAP_BITSPERIMAGE
- bbx
);
877 ofs
+= BITMAP_WORDS(width
) * pf
->height
;
878 ofr
+= pf
->width
[encoding
-pf
->firstchar
] * ((pf
->height
+7)/8);
882 if (strequal(buf
, "ENDFONT"))
886 /* change unused width values to default char values */
887 for (i
=0; i
<pf
->size
; ++i
) {
888 int defchar
= pf
->defaultchar
- pf
->firstchar
;
890 if (pf
->offset
[i
] == -1)
891 pf
->width
[i
] = pf
->width
[defchar
];
894 /* determine whether font doesn't require encode table */
897 for (i
=0; i
<pf
->size
; ++i
) {
898 if ((int)pf
->offrot
[i
] != l
) {
902 l
+= pf
->maxwidth
* ((pf
->height
+ 7) / 8);
906 for (i
=0; i
<pf
->size
; ++i
) {
907 if (pf
->offset
[i
] != l
) {
911 l
+= BITMAP_WORDS(pf
->width
[i
]) * pf
->height
;
919 /* determine whether font is fixed-width */
920 for (i
=0; i
<pf
->size
; ++i
) {
921 if (pf
->width
[i
] != pf
->maxwidth
) {
931 /* reallocate bits array to actual bits used */
932 if (ofs
< pf
->bits_size
) {
933 pf
->bits
= realloc(pf
->bits
, ofs
* sizeof(bitmap_t
));
938 pf
->bits_size
= ofr
; /* always update, rotated is smaller */
944 /* read the next non-comment line, returns buf or NULL if EOF */
945 char *bdf_getline(FILE *fp
, char *buf
, int len
)
952 while ((c
= getc(fp
)) != EOF
) {
957 if (b
- buf
>= (len
- 1))
962 if (c
== EOF
&& b
== buf
)
964 if (b
!= buf
&& !isprefix(buf
, "COMMENT"))
970 void bdf_correct_bbx(int *width
, int *bbx
) {
972 /* Rockbox can't render overlapping glyphs */
978 int bdf_analyze_font(FILE *fp
, struct font
* pf
) {
981 int width
, bbw
, bbh
, bbx
, bby
, ascent
, overflow
;
982 int read_enc
= 0, read_width
= 0, read_bbx
= 0, read_endchar
= 1;
985 /* reset file pointer */
986 fseek(fp
, 0L, SEEK_SET
);
990 pf
->max_char_ascent
= pf
->max_char_descent
= 0;
991 pf
->max_over_ascent
= pf
->max_over_descent
= 0;
995 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
996 print_error("EOF on file\n");
999 if (isprefix(buf
, "ENDFONT")) {
1000 if (!read_endchar
) {
1001 print_error("No terminating ENDCHAR for character %d\n", encoding
);
1006 if (isprefix(buf
, "STARTCHAR")) {
1007 print_trace("Read STARTCHAR, nchars=%d, read_endchar=%d\n", pf
->nchars
, read_endchar
);
1008 if (!read_endchar
) {
1009 print_error("No terminating ENDCHAR for character %d\n", encoding
);
1012 read_enc
= read_width
= read_bbx
= read_endchar
= 0;
1015 if (isprefix(buf
, "ENDCHAR")) {
1017 print_error("ENCODING is not specified\n");
1020 ignore_char
= (encoding
< start_char
|| encoding
> limit_char
);
1022 if (!read_width
&& pf
->default_width
> 0)
1024 width
= pf
->default_width
;
1027 if (!read_width
|| !read_bbx
) {
1028 print_error("WIDTH or BBX is not specified for character %d\n",
1031 bdf_correct_bbx(&width
, &bbx
);
1032 if (width
> pf
->maxwidth
) {
1033 pf
->maxwidth
= width
;
1037 pf
->max_char_ascent
= MAX(pf
->max_char_ascent
, ascent
);
1038 overflow
= ascent
- pf
->ascent
;
1039 pf
->max_over_ascent
= MAX(pf
->max_over_ascent
, overflow
);
1042 pf
->max_char_descent
= MAX(pf
->max_char_descent
, ascent
);
1043 overflow
= ascent
- pf
->descent
;
1044 pf
->max_over_descent
= MAX(pf
->max_over_descent
, overflow
);
1050 if (isprefix(buf
, "ENCODING ")) {
1051 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
1052 print_error("bad 'ENCODING': '%s'\n", buf
);
1058 if (isprefix(buf
, "DWIDTH ")) {
1059 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
1060 print_error("bad 'DWIDTH': '%s'\n", buf
);
1063 /* use font boundingbox width if DWIDTH <= 0 */
1065 print_error("Negative char width: %d\n", width
);
1070 if (isprefix(buf
, "BBX ")) {
1071 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
1072 print_error("bad 'BBX': '%s'\n", buf
);
1082 int adjust_ascent(int ascent
, int overflow
, struct stretch
*stretch
) {
1084 int px
= stretch
->value
;
1085 if (stretch
->percent
) {
1086 px
= ascent
* px
/ 100;
1089 if (stretch
->force
) {
1090 result
= ascent
+ px
;
1093 result
= ascent
+ MIN(overflow
, px
);
1095 result
= MAX(result
, 0);
1100 /* return hex value of portion of buffer*/
1101 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
)
1106 for (i
=ndx1
; i
<=ndx2
; ++i
) {
1108 if (c
>= '0' && c
<= '9')
1111 if (c
>= 'A' && c
<= 'F')
1114 if (c
>= 'a' && c
<= 'f')
1118 val
= (val
<< 4) | c
;
1127 * Take an bitmap_t bitmap and convert to Rockbox format.
1128 * Used for converting font glyphs for the time being.
1129 * Can use for standard X11 and Win32 images as well.
1130 * See format description in lcd-recorder.c
1132 * Doing it this way keeps fonts in standard formats,
1133 * as well as keeping Rockbox hw bitmap format.
1135 * Returns the size of the rotated glyph (in bytes) or a
1136 * negative value if the glyph could not be rotated.
1138 int rotleft(unsigned char *dst
, /* output buffer */
1139 size_t dstlen
, /* buffer size */
1140 bitmap_t
*src
, unsigned int width
, unsigned int height
,
1144 unsigned int src_words
; /* # words of input image */
1145 unsigned int dst_mask
; /* bit mask for destination */
1146 bitmap_t src_mask
; /* bit mask for source */
1148 /* How large the buffer should be to hold the rotated bitmap
1149 of a glyph of size (width x height) */
1150 unsigned int needed_size
= ((height
+ 7) / 8) * width
;
1152 if (needed_size
> dstlen
) {
1153 print_error("Character %d: Glyph of size %d x %d can't be rotated "
1154 "(buffer size is %lu, needs %u)\n",
1155 char_code
, width
, height
, (unsigned long)dstlen
, needed_size
);
1159 /* calc words of input image*/
1160 src_words
= BITMAP_WORDS(width
) * height
;
1162 /* clear background*/
1163 memset(dst
, 0, needed_size
);
1167 for (i
=0; i
< src_words
; i
++) {
1169 /* calc src input bit*/
1170 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
1172 /* for each input column...*/
1173 for(j
=0; j
< width
; j
++) {
1175 if (src_mask
== 0) /* input word done? */
1177 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
1178 i
++; /* next input word */
1181 /* if set in input, set in rotated output */
1182 if (src
[i
] & src_mask
)
1185 src_mask
>>= 1; /* next input bit */
1188 dst_mask
<<= 1; /* next output bit (row) */
1189 if (dst_mask
> (1 << 7)) /* output bit > 7? */
1192 dst
+= width
; /* next output byte row */
1195 return needed_size
; /* return result size in bytes */
1201 /* generate C source from in-core font*/
1202 int gen_c_source(struct font
* pf
, char *path
)
1210 int did_syncmsg
= 0;
1211 bitmap_t
*ofs
= pf
->bits
;
1216 "/* Generated by convbdf on %s. */\n"
1217 "#include \"font.h\"\n"
1218 "#ifdef HAVE_LCD_BITMAP\n"
1220 "/* Font information:\n"
1227 " first char: %d (0x%02x)\n"
1228 " last char: %d (0x%02x)\n"
1229 " default char: %d (0x%02x)\n"
1230 " proportional: %s\n"
1234 "/* Font character bitmap data. */\n"
1235 "static const unsigned char _font_bits[] = {\n"
1238 ofp
= fopen(path
, "w");
1240 print_error("Can't create %s\n", path
);
1244 strcpy(buf
, ctime(&t
));
1245 buf
[strlen(buf
)-1] = 0;
1247 fprintf(ofp
, hdr1
, buf
,
1249 pf
->facename
? pf
->facename
: "",
1250 pf
->maxwidth
, pf
->height
,
1252 pf
->ascent
, pf
->descent
,
1253 pf
->firstchar
, pf
->firstchar
,
1254 pf
->firstchar
+pf
->size
-1, pf
->firstchar
+pf
->size
-1,
1255 pf
->defaultchar
, pf
->defaultchar
,
1256 pf
->width
? "yes": "no",
1257 pf
->copyright
? pf
->copyright
: "");
1259 /* generate bitmaps*/
1260 for (i
=0; i
<pf
->size
; ++i
) {
1263 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1264 int height
= pf
->height
;
1265 int char_code
= pf
->firstchar
+ i
;
1267 bitmap_t bitvalue
=0;
1269 /* Skip missing glyphs */
1270 if (pf
->offset
&& (pf
->offset
[i
] == -1))
1273 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1275 fprintf(ofp
, "\n/* Character %d (0x%02x):\n width %d",
1276 char_code
, char_code
, width
);
1279 fprintf(ofp
, "\n +");
1280 for (x
=0; x
<width
; ++x
) fprintf(ofp
, "-");
1281 fprintf(ofp
, "+\n");
1284 while (height
> 0) {
1285 if (x
== 0) fprintf(ofp
, " |");
1287 if (bitcount
<= 0) {
1288 bitcount
= BITMAP_BITSPERIMAGE
;
1292 fprintf(ofp
, BITMAP_TESTBIT(bitvalue
)? "*": " ");
1294 bitvalue
= BITMAP_SHIFTBIT(bitvalue
);
1297 fprintf(ofp
, "|\n");
1304 for (x
=0; x
<width
; ++x
)
1306 fprintf(ofp
, "+ */\n");
1309 fprintf(ofp
, " */\n");
1311 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1312 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
1314 unsigned char bytemap
[ROTATION_BUF_SIZE
];
1317 int size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
,
1318 pf
->height
, char_code
);
1323 for (y8
=0; y8
<pf
->height
; y8
+=8) /* column rows */
1325 for (x
=0; x
<width
; x
++) {
1326 fprintf(ofp
, "0x%02x, ", bytemap
[ix
]);
1332 /* update offrot since bits are now in sorted order */
1333 pf
->offrot
[i
] = ofr
;
1338 for (x
=BITMAP_WORDS(width
)*pf
->height
; x
>0; --x
) {
1339 fprintf(ofp
, "0x%04x,\n", *bits
);
1340 if (!did_syncmsg
&& *bits
++ != *ofs
++) {
1341 print_warning(VL_MISC
, "found encoding values in non-sorted order (not an error).\n");
1347 fprintf(ofp
, "};\n\n");
1350 /* output offset table*/
1351 fprintf(ofp
, "/* Character->glyph mapping. */\n"
1352 "static const unsigned short _sysfont_offset[] = {\n");
1354 for (i
=0; i
<pf
->size
; ++i
) {
1355 if (pf
->offset
[i
] == -1) {
1356 pf
->offset
[i
] = pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1357 pf
->offrot
[i
] = pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1359 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
1361 pf
->offrot
[i
], i
+pf
->firstchar
);
1363 pf
->offset
[i
], i
+pf
->firstchar
);
1366 fprintf(ofp
, "};\n\n");
1369 /* output width table for proportional fonts*/
1371 fprintf(ofp
, "/* Character width data. */\n"
1372 "static const unsigned char _sysfont_width[] = {\n");
1374 for (i
=0; i
<pf
->size
; ++i
)
1375 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
1376 pf
->width
[i
], i
+pf
->firstchar
);
1377 fprintf(ofp
, "};\n\n");
1380 /* output struct font struct*/
1382 sprintf(obuf
, "_sysfont_offset,");
1384 sprintf(obuf
, "0, /* no encode table */");
1387 sprintf(buf
, "_sysfont_width, /* width */");
1389 sprintf(buf
, "0, /* fixed width */");
1391 fprintf(ofp
, "/* Exported structure definition. */\n"
1392 "const struct font sysfont = {\n"
1393 " %d, /* maxwidth */\n"
1394 " %d, /* height */\n"
1395 " %d, /* ascent */\n"
1396 " %d, /* firstchar */\n"
1398 " _font_bits, /* bits */\n"
1399 " %s /* offset */\n"
1401 " %d, /* defaultchar */\n"
1402 " %d, /* bits_size */\n"
1403 " -1, /* font fd */\n"
1404 " 0, /* buffer start */\n"
1405 " 0, /* ^ position */\n"
1407 " 0, /* ^ size */\n"
1408 " {{0,0,0,0,0},0,0,0}, /* cache */\n"
1413 "#endif /* HAVE_LCD_BITMAP */\n",
1414 pf
->maxwidth
, pf
->height
,
1426 /* generate C header from in-core font*/
1427 int gen_h_header(struct font
* pf
, char *path
)
1433 "/* Generated by convbdf on %s. */\n"
1434 "#ifdef HAVE_LCD_BITMAP\n"
1436 "/* Font information*/\n"
1437 "#define SYSFONT_NAME %s\n"
1438 "#define SYSFONT_FACENAME %s\n"
1439 "#define SYSFONT_WIDTH %d\n"
1440 "#define SYSFONT_HEIGHT %d\n"
1441 "#define SYSFONT_SIZE %d\n"
1442 "#define SYSFONT_ASCENT %d\n";
1444 "#define SYSFONT_DESCENT %d\n"
1445 "#define SYSFONT_FIRST_CHAR %d\n"
1446 "#define SYSFONT_LAST_CHAR %d\n"
1447 "#define SYSFONT_DEFAULT_CHAR %d\n"
1448 "#define SYSFONT_PROPORTIONAL %d\n"
1449 "#define SYSFONT_COPYRIGHT %s\n"
1450 "#define SYSFONT_BITS_SIZE %d\n"
1454 ofp
= fopen(path
, "w");
1456 print_error("Can't create %s\n", path
);
1460 strcpy(buf
, ctime(&t
));
1461 buf
[strlen(buf
)-1] = 0;
1463 fprintf(ofp
, hdr1
, buf
,
1465 pf
->facename
? pf
->facename
: "",
1474 pf
->firstchar
+pf
->size
-1,
1477 pf
->copyright
? pf
->copyright
: "",
1483 static int writebyte(FILE *fp
, unsigned char c
)
1485 return putc(c
, fp
) != EOF
;
1488 static int writeshort(FILE *fp
, unsigned short s
)
1491 return putc(s
>>8, fp
) != EOF
;
1494 static int writeint(FILE *fp
, unsigned int l
)
1499 return putc(l
>>24, fp
) != EOF
;
1502 static int writestr(FILE *fp
, char *str
, int count
)
1504 return (int)fwrite(str
, 1, count
, fp
) == count
;
1508 static int writestrpad(FILE *fp
, char *str
, int totlen
)
1512 while (str
&& *str
&& totlen
> 0) {
1514 ret
= putc(*str
++, fp
);
1518 while (--totlen
>= 0)
1519 ret
= putc(' ', fp
);
1524 /* generate .fnt format file from in-core font*/
1525 int gen_fnt_file(struct font
* pf
, char *path
)
1533 ofp
= fopen(path
, "wb");
1535 print_error("Can't create %s\n", path
);
1539 /* write magic and version #*/
1540 writestr(ofp
, VERSION
, 4);
1542 /* internal font name*/
1543 writestrpad(ofp
, pf
->name
, 64);
1546 writestrpad(ofp
, pf
->copyright
, 256);
1549 writeshort(ofp
, pf
->maxwidth
);
1550 writeshort(ofp
, pf
->height
);
1551 writeshort(ofp
, pf
->ascent
);
1553 writeint(ofp
, pf
->firstchar
);
1554 writeint(ofp
, pf
->defaultchar
);
1555 writeint(ofp
, pf
->size
);
1557 /* variable font data sizes*/
1558 writeint(ofp
, pf
->bits_size
); /* # words of bitmap_t*/
1559 writeint(ofp
, pf
->offset
? pf
->size
: 0); /* # ints of offset*/
1560 writeint(ofp
, pf
->width
? pf
->size
: 0); /* # bytes of width*/
1561 /* variable font data*/
1563 for (i
=0; i
<pf
->size
; ++i
)
1566 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1568 int char_code
= pf
->firstchar
+ i
;
1569 unsigned char bytemap
[ROTATION_BUF_SIZE
];
1571 /* Skip missing glyphs */
1572 if (pf
->offset
&& (pf
->offset
[i
] == -1))
1575 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1577 size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
, pf
->height
, char_code
);
1581 writestr(ofp
, (char *)bytemap
, size
);
1583 /* update offrot since bits are now in sorted order */
1584 pf
->offrot
[i
] = ofr
;
1588 if ( pf
->bits_size
< 0xFFDB )
1590 /* bitmap offset is small enough, use unsigned short for offset */
1592 writebyte(ofp
, 0); /* pad to 16-bit boundary*/
1596 /* bitmap offset is large then 64K, use unsigned int for offset */
1597 while (ftell(ofp
) & 3)
1598 writebyte(ofp
, 0); /* pad to 32-bit boundary*/
1603 for (i
=0; i
<pf
->size
; ++i
)
1605 if (pf
->offset
[i
] == -1) {
1606 pf
->offrot
[i
] = pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1608 if ( pf
->bits_size
< 0xFFDB )
1609 writeshort(ofp
, pf
->offrot
[i
]);
1611 writeint(ofp
, pf
->offrot
[i
]);
1616 for (i
=0; i
<pf
->size
; ++i
)
1617 writebyte(ofp
, pf
->width
[i
]);
1619 for (i
=0; i
<pf
->bits_size
; ++i
)
1620 writeshort(ofp
, pf
->bits
[i
]);
1622 writeshort(ofp
, 0); /* pad to 32-bit boundary*/
1625 for (i
=0; i
<pf
->size
; ++i
) {
1626 if (pf
->offset
[i
] == -1) {
1627 pf
->offset
[i
] = pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1629 writeint(ofp
, pf
->offset
[i
]);
1633 for (i
=0; i
<pf
->size
; ++i
)
1634 writebyte(ofp
, pf
->width
[i
]);