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 (ac
> 1 || (gen_c
&& gen_fnt
) || (gen_c
&& gen_h
) || (gen_h
&& gen_fnt
)) {
434 ret
|= convbdf(av
[0]);
441 /* free font structure */
442 void free_font(struct font
* pf
)
461 /* build incore structure from .bdf file */
462 struct font
* bdf_read_font(char *path
)
467 fp
= fopen(path
, "rb");
469 print_error("Error opening file: %s\n", path
);
473 pf
= (struct font
*)calloc(1, sizeof(struct font
));
476 memset(pf
, 0, sizeof(struct font
));
478 pf
->name
= strdup(basename(path
));
480 if (!bdf_read_header(fp
, pf
)) {
481 print_error("Error reading font header\n");
484 print_trace("Read font header, nchars_decl=%d\n", pf
->nchars_declared
);
486 if (!bdf_analyze_font(fp
, pf
)) {
487 print_error("Error analyzing the font\n");
490 print_trace("Analyzed font, nchars=%d, maxwidth=%d, asc_over=%d, desc_over=%d\n",
491 pf
->nchars
, pf
->maxwidth
, pf
->max_over_ascent
, pf
->max_over_descent
);
493 if (pf
->nchars
!= pf
->nchars_declared
) {
494 print_warning(VL_MISC
, "The declared number of chars (%d) "
495 "does not match the real number (%d)\n",
496 pf
->nchars_declared
, pf
->nchars
);
499 /* Correct ascent/descent if necessary */
500 pf
->ascent
= adjust_ascent(pf
->ascent_declared
, pf
->max_over_ascent
, &stretch_ascent
);
501 if (pf
->ascent
!= pf
->ascent_declared
) {
502 print_info("Font ascent has been changed from %d to %d\n",
503 pf
->ascent_declared
, pf
->ascent
);
505 pf
->descent
= adjust_ascent(pf
->descent
, pf
->max_over_descent
, &stretch_descent
);
506 if (pf
->descent
!= pf
->descent_declared
) {
507 print_info("Font descent has been changed from %d to %d\n",
508 pf
->descent_declared
, pf
->descent
);
510 pf
->height
= pf
->ascent
+ pf
->descent
;
511 if (pf
->height
!= pf
->ascent_declared
+ pf
->descent_declared
) {
512 print_warning(VL_CLIP_FONT
, "Generated font's height: %d\n", pf
->height
);
515 if (pf
->ascent
> pf
->max_char_ascent
) {
516 print_trace("Font's ascent could be reduced by %d to %d without clipping\n",
517 (pf
->ascent
- pf
->max_char_ascent
), pf
->max_char_ascent
);
519 if (pf
->descent
> pf
->max_char_descent
) {
520 print_trace("Font's descent could be reduced by %d to %d without clipping\n",
521 (pf
->descent
- pf
->max_char_descent
), pf
->max_char_descent
);
526 pf
->bits_size
= pf
->size
* BITMAP_WORDS(pf
->maxwidth
) * pf
->height
;
527 pf
->bits
= (bitmap_t
*)malloc(pf
->bits_size
* sizeof(bitmap_t
));
528 pf
->offset
= (int *)malloc(pf
->size
* sizeof(int));
529 pf
->offrot
= (unsigned int *)malloc(pf
->size
* sizeof(unsigned int));
530 pf
->width
= (unsigned char *)malloc(pf
->size
* sizeof(unsigned char));
532 if (!pf
->bits
|| !pf
->offset
|| !pf
->offrot
|| !pf
->width
) {
533 print_error("no memory for font load\n");
537 pf
->num_clipped_ascent
= pf
->num_clipped_descent
= pf
->num_clipped
= 0;
538 pf
->max_over_ascent
= pf
->max_over_descent
= 0;
540 if (!bdf_read_bitmaps(fp
, pf
)) {
541 print_error("Error reading font bitmaps\n");
544 print_trace("Read bitmaps\n");
546 if (pf
->num_clipped
> 0) {
547 print_warning(VL_CLIP_FONT
, "%d character(s) out of %d were clipped "
548 "(%d at ascent, %d at descent)\n",
549 pf
->num_clipped
, pf
->nchars
,
550 pf
->num_clipped_ascent
, pf
->num_clipped_descent
);
551 print_warning(VL_CLIP_FONT
, "max overflows: %d pixel(s) at ascent, %d pixel(s) at descent\n",
552 pf
->max_over_ascent
, pf
->max_over_descent
);
564 /* read bdf font header information, return 0 on error */
565 int bdf_read_header(FILE *fp
, struct font
* pf
)
568 int firstchar
= 65535;
575 /* set certain values to errors for later error checking */
576 pf
->defaultchar
= -1;
579 pf
->default_width
= -1;
582 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
583 print_error("EOF on file\n");
586 if (isprefix(buf
, "FONT ")) { /* not required */
587 if (sscanf(buf
, "FONT %[^\n]", facename
) != 1) {
588 print_error("bad 'FONT'\n");
591 pf
->facename
= strdup(facename
);
594 if (isprefix(buf
, "COPYRIGHT ")) { /* not required */
595 if (sscanf(buf
, "COPYRIGHT \"%[^\"]", copyright
) != 1) {
596 print_error("bad 'COPYRIGHT'\n");
599 pf
->copyright
= strdup(copyright
);
602 if (isprefix(buf
, "DEFAULT_CHAR ")) { /* not required */
603 if (sscanf(buf
, "DEFAULT_CHAR %d", &pf
->defaultchar
) != 1) {
604 print_error("bad 'DEFAULT_CHAR'\n");
608 if (isprefix(buf
, "FONT_DESCENT ")) {
609 if (sscanf(buf
, "FONT_DESCENT %d", &pf
->descent_declared
) != 1) {
610 print_error("bad 'FONT_DESCENT'\n");
613 pf
->descent
= pf
->descent_declared
; /* For now */
616 if (isprefix(buf
, "FONT_ASCENT ")) {
617 if (sscanf(buf
, "FONT_ASCENT %d", &pf
->ascent_declared
) != 1) {
618 print_error("bad 'FONT_ASCENT'\n");
621 pf
->ascent
= pf
->ascent_declared
; /* For now */
624 if (isprefix(buf
, "FONTBOUNDINGBOX ")) {
625 if (sscanf(buf
, "FONTBOUNDINGBOX %d %d %d %d",
626 &pf
->fbbw
, &pf
->fbbh
, &pf
->fbbx
, &pf
->fbby
) != 4) {
627 print_error("bad 'FONTBOUNDINGBOX'\n");
632 if (isprefix(buf
, "CHARS ")) {
633 if (sscanf(buf
, "CHARS %d", &pf
->nchars_declared
) != 1) {
634 print_error("bad 'CHARS'\n");
639 if (isprefix(buf
, "STARTCHAR")) {
644 /* for BDF version 2.2 */
645 if (is_header
&& isprefix(buf
, "DWIDTH ")) {
646 if (sscanf(buf
, "DWIDTH %d", &pf
->default_width
) != 1) {
647 print_error("bad 'DWIDTH' at font level\n");
654 * Reading ENCODING is necessary to get firstchar/lastchar
655 * which is needed to pre-calculate our offset and widths
658 if (isprefix(buf
, "ENCODING ")) {
659 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
660 print_error("bad 'ENCODING'\n");
664 encoding
<= limit_char
&&
665 encoding
>= start_char
) {
667 if (firstchar
> encoding
)
668 firstchar
= encoding
;
669 if (lastchar
< encoding
)
674 if (strequal(buf
, "ENDFONT"))
678 /* calc font height*/
679 if (pf
->ascent
< 0 || pf
->descent
< 0 || firstchar
< 0) {
680 print_error("Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
683 pf
->height
= pf
->ascent
+ pf
->descent
;
685 /* calc default char */
686 if (pf
->defaultchar
< 0 ||
687 pf
->defaultchar
< firstchar
||
688 pf
->defaultchar
> limit_char
||
689 pf
->defaultchar
> lastchar
)
690 pf
->defaultchar
= firstchar
;
692 /* calc font size (offset/width entries) */
693 pf
->firstchar
= firstchar
;
694 pf
->size
= lastchar
- firstchar
+ 1;
700 * TODO: rework the code to avoid logics duplication in
701 * bdf_read_bitmaps and bdf_analyze_font
705 /* read bdf font bitmaps, return 0 on error */
706 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
)
710 int i
, k
, encoding
, width
;
711 int bbw
, bbh
, bbx
, bby
;
712 int proportional
= 0;
719 /* reset file pointer */
720 fseek(fp
, 0L, SEEK_SET
);
722 /* initially mark offsets as not used */
723 for (i
=0; i
<pf
->size
; ++i
)
727 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
728 print_error("EOF on file\n");
731 if (isprefix(buf
, "STARTCHAR")) {
732 encoding
= width
= -1;
739 if (isprefix(buf
, "ENCODING ")) {
740 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
741 print_error("bad 'ENCODING'\n");
744 if (encoding
< start_char
|| encoding
> limit_char
)
748 if (isprefix(buf
, "DWIDTH ")) {
749 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
750 print_error("bad 'DWIDTH'\n");
753 /* use font boundingbox width if DWIDTH <= 0 */
755 width
= pf
->fbbw
- pf
->fbbx
;
758 if (isprefix(buf
, "BBX ")) {
759 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
760 print_error("bad 'BBX'\n");
765 if (strequal(buf
, "BITMAP") || strequal(buf
, "BITMAP ")) {
766 int overflow_asc
, overflow_desc
;
767 int bbh_orig
, bby_orig
, y
;
772 if (width
< 0 && pf
->default_width
> 0)
773 width
= pf
->default_width
;
775 /* set bits offset in encode map*/
776 if (pf
->offset
[encoding
-pf
->firstchar
] != -1) {
777 print_error("duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
781 pf
->offset
[encoding
-pf
->firstchar
] = ofs
;
782 pf
->offrot
[encoding
-pf
->firstchar
] = ofr
;
784 /* calc char width */
785 bdf_correct_bbx(&width
, &bbx
);
786 pf
->width
[encoding
-pf
->firstchar
] = width
;
788 ch_bitmap
= pf
->bits
+ ofs
;
789 ch_words
= BITMAP_WORDS(width
);
790 memset(ch_bitmap
, 0, BITMAP_BYTES(width
) * pf
->height
); /* clear bitmap */
792 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
793 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
798 overflow_asc
= bby
+ bbh
- pf
->ascent
;
799 if (overflow_asc
> 0) {
800 pf
->num_clipped_ascent
++;
801 if (overflow_asc
> pf
->max_over_ascent
) {
802 pf
->max_over_ascent
= overflow_asc
;
804 bbh
= MAX(bbh
- overflow_asc
, 0); /* Clipped -> decrease the height */
805 print_warning(VL_CLIP_CHAR
, "character %d goes %d pixel(s)"
806 " beyond the font's ascent, it will be clipped\n",
807 encoding
, overflow_asc
);
809 overflow_desc
= -bby
- pf
->descent
;
810 if (overflow_desc
> 0) {
811 pf
->num_clipped_descent
++;
812 if (overflow_desc
> pf
->max_over_descent
) {
813 pf
->max_over_descent
= overflow_desc
;
815 bby
+= overflow_desc
;
816 bbh
= MAX(bbh
- overflow_desc
, 0); /* Clipped -> decrease the height */
817 print_warning(VL_CLIP_CHAR
, "character %d goes %d pixel(s)"
818 " beyond the font's descent, it will be clipped\n",
819 encoding
, overflow_desc
);
821 if (overflow_asc
> 0 || overflow_desc
> 0) {
825 y
= bby_orig
+ bbh_orig
; /* 0-based y within the char */
831 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
832 print_error("EOF reading BITMAP data for character %d\n",
836 if (isprefix(buf
, "ENDCHAR"))
840 if ((y
>= pf
->ascent
) || (y
< -pf
->descent
)) {
841 /* We're beyond the area that Rockbox can render -> clip */
842 --i
; /* This line doesn't count */
846 hexnibbles
= strlen(buf
);
847 for (k
=0; k
<ch_words
; ++k
) {
848 int ndx
= k
* BITMAP_NIBBLES
;
849 int padnibbles
= hexnibbles
- ndx
;
854 if (padnibbles
>= (int)BITMAP_NIBBLES
)
857 value
= bdf_hexval((unsigned char *)buf
,
858 ndx
, ndx
+BITMAP_NIBBLES
-1-padnibbles
);
859 value
<<= padnibbles
* BITMAP_NIBBLES
;
861 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
) |=
863 /* handle overflow into next image word */
865 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
+1) =
866 value
<< (BITMAP_BITSPERIMAGE
- bbx
);
871 ofs
+= BITMAP_WORDS(width
) * pf
->height
;
872 ofr
+= pf
->width
[encoding
-pf
->firstchar
] * ((pf
->height
+7)/8);
876 if (strequal(buf
, "ENDFONT"))
880 /* change unused width values to default char values */
881 for (i
=0; i
<pf
->size
; ++i
) {
882 int defchar
= pf
->defaultchar
- pf
->firstchar
;
884 if (pf
->offset
[i
] == -1)
885 pf
->width
[i
] = pf
->width
[defchar
];
888 /* determine whether font doesn't require encode table */
891 for (i
=0; i
<pf
->size
; ++i
) {
892 if ((int)pf
->offrot
[i
] != l
) {
896 l
+= pf
->maxwidth
* ((pf
->height
+ 7) / 8);
900 for (i
=0; i
<pf
->size
; ++i
) {
901 if (pf
->offset
[i
] != l
) {
905 l
+= BITMAP_WORDS(pf
->width
[i
]) * pf
->height
;
913 /* determine whether font is fixed-width */
914 for (i
=0; i
<pf
->size
; ++i
) {
915 if (pf
->width
[i
] != pf
->maxwidth
) {
925 /* reallocate bits array to actual bits used */
926 if (ofs
< pf
->bits_size
) {
927 pf
->bits
= realloc(pf
->bits
, ofs
* sizeof(bitmap_t
));
932 pf
->bits_size
= ofr
; /* always update, rotated is smaller */
938 /* read the next non-comment line, returns buf or NULL if EOF */
939 char *bdf_getline(FILE *fp
, char *buf
, int len
)
946 while ((c
= getc(fp
)) != EOF
) {
951 if (b
- buf
>= (len
- 1))
956 if (c
== EOF
&& b
== buf
)
958 if (b
!= buf
&& !isprefix(buf
, "COMMENT"))
964 void bdf_correct_bbx(int *width
, int *bbx
) {
966 /* Rockbox can't render overlapping glyphs */
972 int bdf_analyze_font(FILE *fp
, struct font
* pf
) {
975 int width
, bbw
, bbh
, bbx
, bby
, ascent
, overflow
;
976 int read_enc
= 0, read_width
= 0, read_bbx
= 0, read_endchar
= 1;
979 /* reset file pointer */
980 fseek(fp
, 0L, SEEK_SET
);
984 pf
->max_char_ascent
= pf
->max_char_descent
= 0;
985 pf
->max_over_ascent
= pf
->max_over_descent
= 0;
989 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
990 print_error("EOF on file\n");
993 if (isprefix(buf
, "ENDFONT")) {
995 print_error("No terminating ENDCHAR for character %d\n", encoding
);
1000 if (isprefix(buf
, "STARTCHAR")) {
1001 print_trace("Read STARTCHAR, nchars=%d, read_endchar=%d\n", pf
->nchars
, read_endchar
);
1002 if (!read_endchar
) {
1003 print_error("No terminating ENDCHAR for character %d\n", encoding
);
1006 read_enc
= read_width
= read_bbx
= read_endchar
= 0;
1009 if (isprefix(buf
, "ENDCHAR")) {
1011 print_error("ENCODING is not specified\n");
1014 ignore_char
= (encoding
< start_char
|| encoding
> limit_char
);
1016 if (!read_width
&& pf
->default_width
> 0)
1018 width
= pf
->default_width
;
1021 if (!read_width
|| !read_bbx
) {
1022 print_error("WIDTH or BBX is not specified for character %d\n",
1025 bdf_correct_bbx(&width
, &bbx
);
1026 if (width
> pf
->maxwidth
) {
1027 pf
->maxwidth
= width
;
1031 pf
->max_char_ascent
= MAX(pf
->max_char_ascent
, ascent
);
1032 overflow
= ascent
- pf
->ascent
;
1033 pf
->max_over_ascent
= MAX(pf
->max_over_ascent
, overflow
);
1036 pf
->max_char_descent
= MAX(pf
->max_char_descent
, ascent
);
1037 overflow
= ascent
- pf
->descent
;
1038 pf
->max_over_descent
= MAX(pf
->max_over_descent
, overflow
);
1044 if (isprefix(buf
, "ENCODING ")) {
1045 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
1046 print_error("bad 'ENCODING': '%s'\n", buf
);
1052 if (isprefix(buf
, "DWIDTH ")) {
1053 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
1054 print_error("bad 'DWIDTH': '%s'\n", buf
);
1057 /* use font boundingbox width if DWIDTH <= 0 */
1059 print_error("Negative char width: %d\n", width
);
1064 if (isprefix(buf
, "BBX ")) {
1065 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
1066 print_error("bad 'BBX': '%s'\n", buf
);
1076 int adjust_ascent(int ascent
, int overflow
, struct stretch
*stretch
) {
1078 int px
= stretch
->value
;
1079 if (stretch
->percent
) {
1080 px
= ascent
* px
/ 100;
1083 if (stretch
->force
) {
1084 result
= ascent
+ px
;
1087 result
= ascent
+ MIN(overflow
, px
);
1089 result
= MAX(result
, 0);
1094 /* return hex value of portion of buffer*/
1095 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
)
1100 for (i
=ndx1
; i
<=ndx2
; ++i
) {
1102 if (c
>= '0' && c
<= '9')
1105 if (c
>= 'A' && c
<= 'F')
1108 if (c
>= 'a' && c
<= 'f')
1112 val
= (val
<< 4) | c
;
1121 * Take an bitmap_t bitmap and convert to Rockbox format.
1122 * Used for converting font glyphs for the time being.
1123 * Can use for standard X11 and Win32 images as well.
1124 * See format description in lcd-recorder.c
1126 * Doing it this way keeps fonts in standard formats,
1127 * as well as keeping Rockbox hw bitmap format.
1129 * Returns the size of the rotated glyph (in bytes) or a
1130 * negative value if the glyph could not be rotated.
1132 int rotleft(unsigned char *dst
, /* output buffer */
1133 size_t dstlen
, /* buffer size */
1134 bitmap_t
*src
, unsigned int width
, unsigned int height
,
1138 unsigned int src_words
; /* # words of input image */
1139 unsigned int dst_mask
; /* bit mask for destination */
1140 bitmap_t src_mask
; /* bit mask for source */
1142 /* How large the buffer should be to hold the rotated bitmap
1143 of a glyph of size (width x height) */
1144 unsigned int needed_size
= ((height
+ 7) / 8) * width
;
1146 if (needed_size
> dstlen
) {
1147 print_error("Character %d: Glyph of size %d x %d can't be rotated "
1148 "(buffer size is %lu, needs %u)\n",
1149 char_code
, width
, height
, (unsigned long)dstlen
, needed_size
);
1153 /* calc words of input image*/
1154 src_words
= BITMAP_WORDS(width
) * height
;
1156 /* clear background*/
1157 memset(dst
, 0, needed_size
);
1161 for (i
=0; i
< src_words
; i
++) {
1163 /* calc src input bit*/
1164 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
1166 /* for each input column...*/
1167 for(j
=0; j
< width
; j
++) {
1169 if (src_mask
== 0) /* input word done? */
1171 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
1172 i
++; /* next input word */
1175 /* if set in input, set in rotated output */
1176 if (src
[i
] & src_mask
)
1179 src_mask
>>= 1; /* next input bit */
1182 dst_mask
<<= 1; /* next output bit (row) */
1183 if (dst_mask
> (1 << 7)) /* output bit > 7? */
1186 dst
+= width
; /* next output byte row */
1189 return needed_size
; /* return result size in bytes */
1195 /* generate C source from in-core font*/
1196 int gen_c_source(struct font
* pf
, char *path
)
1204 int did_syncmsg
= 0;
1205 bitmap_t
*ofs
= pf
->bits
;
1210 "/* Generated by convbdf on %s. */\n"
1211 "#include \"font.h\"\n"
1212 "#ifdef HAVE_LCD_BITMAP\n"
1214 "/* Font information:\n"
1221 " first char: %d (0x%02x)\n"
1222 " last char: %d (0x%02x)\n"
1223 " default char: %d (0x%02x)\n"
1224 " proportional: %s\n"
1228 "/* Font character bitmap data. */\n"
1229 "static const unsigned char _font_bits[] = {\n"
1232 ofp
= fopen(path
, "w");
1234 print_error("Can't create %s\n", path
);
1238 strcpy(buf
, ctime(&t
));
1239 buf
[strlen(buf
)-1] = 0;
1241 fprintf(ofp
, hdr1
, buf
,
1243 pf
->facename
? pf
->facename
: "",
1244 pf
->maxwidth
, pf
->height
,
1246 pf
->ascent
, pf
->descent
,
1247 pf
->firstchar
, pf
->firstchar
,
1248 pf
->firstchar
+pf
->size
-1, pf
->firstchar
+pf
->size
-1,
1249 pf
->defaultchar
, pf
->defaultchar
,
1250 pf
->width
? "yes": "no",
1251 pf
->copyright
? pf
->copyright
: "");
1253 /* generate bitmaps*/
1254 for (i
=0; i
<pf
->size
; ++i
) {
1257 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1258 int height
= pf
->height
;
1259 int char_code
= pf
->firstchar
+ i
;
1261 bitmap_t bitvalue
=0;
1263 /* Skip missing glyphs */
1264 if (pf
->offset
&& (pf
->offset
[i
] == -1))
1267 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1269 fprintf(ofp
, "\n/* Character %d (0x%02x):\n width %d",
1270 char_code
, char_code
, width
);
1273 fprintf(ofp
, "\n +");
1274 for (x
=0; x
<width
; ++x
) fprintf(ofp
, "-");
1275 fprintf(ofp
, "+\n");
1278 while (height
> 0) {
1279 if (x
== 0) fprintf(ofp
, " |");
1281 if (bitcount
<= 0) {
1282 bitcount
= BITMAP_BITSPERIMAGE
;
1286 fprintf(ofp
, BITMAP_TESTBIT(bitvalue
)? "*": " ");
1288 bitvalue
= BITMAP_SHIFTBIT(bitvalue
);
1291 fprintf(ofp
, "|\n");
1298 for (x
=0; x
<width
; ++x
)
1300 fprintf(ofp
, "+ */\n");
1303 fprintf(ofp
, " */\n");
1305 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1306 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
1308 unsigned char bytemap
[ROTATION_BUF_SIZE
];
1311 int size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
,
1312 pf
->height
, char_code
);
1317 for (y8
=0; y8
<pf
->height
; y8
+=8) /* column rows */
1319 for (x
=0; x
<width
; x
++) {
1320 fprintf(ofp
, "0x%02x, ", bytemap
[ix
]);
1326 /* update offrot since bits are now in sorted order */
1327 pf
->offrot
[i
] = ofr
;
1332 for (x
=BITMAP_WORDS(width
)*pf
->height
; x
>0; --x
) {
1333 fprintf(ofp
, "0x%04x,\n", *bits
);
1334 if (!did_syncmsg
&& *bits
++ != *ofs
++) {
1335 print_warning(VL_MISC
, "found encoding values in non-sorted order (not an error).\n");
1341 fprintf(ofp
, "};\n\n");
1344 /* output offset table*/
1345 fprintf(ofp
, "/* Character->glyph mapping. */\n"
1346 "static const unsigned short _sysfont_offset[] = {\n");
1348 for (i
=0; i
<pf
->size
; ++i
) {
1349 if (pf
->offset
[i
] == -1) {
1350 pf
->offset
[i
] = pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1351 pf
->offrot
[i
] = pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1353 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
1355 pf
->offrot
[i
], i
+pf
->firstchar
);
1357 pf
->offset
[i
], i
+pf
->firstchar
);
1360 fprintf(ofp
, "};\n\n");
1363 /* output width table for proportional fonts*/
1365 fprintf(ofp
, "/* Character width data. */\n"
1366 "static const unsigned char _sysfont_width[] = {\n");
1368 for (i
=0; i
<pf
->size
; ++i
)
1369 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
1370 pf
->width
[i
], i
+pf
->firstchar
);
1371 fprintf(ofp
, "};\n\n");
1374 /* output struct font struct*/
1376 sprintf(obuf
, "_sysfont_offset,");
1378 sprintf(obuf
, "0, /* no encode table */");
1381 sprintf(buf
, "_sysfont_width, /* width */");
1383 sprintf(buf
, "0, /* fixed width */");
1385 fprintf(ofp
, "/* Exported structure definition. */\n"
1386 "const struct font sysfont = {\n"
1387 " %d, /* maxwidth */\n"
1388 " %d, /* height */\n"
1389 " %d, /* ascent */\n"
1390 " %d, /* firstchar */\n"
1392 " _font_bits, /* bits */\n"
1393 " %s /* offset */\n"
1395 " %d, /* defaultchar */\n"
1396 " %d /* bits_size */\n"
1398 "#endif /* HAVE_LCD_BITMAP */\n",
1399 pf
->maxwidth
, pf
->height
,
1411 /* generate C header from in-core font*/
1412 int gen_h_header(struct font
* pf
, char *path
)
1418 "/* Generated by convbdf on %s. */\n"
1419 "#ifdef HAVE_LCD_BITMAP\n"
1421 "/* Font information*/\n"
1422 "#define SYSFONT_NAME %s\n"
1423 "#define SYSFONT_FACENAME %s\n"
1424 "#define SYSFONT_WIDTH %d\n"
1425 "#define SYSFONT_HEIGHT %d\n"
1426 "#define SYSFONT_SIZE %d\n"
1427 "#define SYSFONT_ASCENT %d\n";
1429 "#define SYSFONT_DESCENT %d\n"
1430 "#define SYSFONT_FIRST_CHAR %d\n"
1431 "#define SYSFONT_LAST_CHAR %d\n"
1432 "#define SYSFONT_DEFAULT_CHAR %d\n"
1433 "#define SYSFONT_PROPORTIONAL %d\n"
1434 "#define SYSFONT_COPYRIGHT %s\n"
1435 "#define SYSFONT_BITS_SIZE %d\n"
1439 ofp
= fopen(path
, "w");
1441 print_error("Can't create %s\n", path
);
1445 strcpy(buf
, ctime(&t
));
1446 buf
[strlen(buf
)-1] = 0;
1448 fprintf(ofp
, hdr1
, buf
,
1450 pf
->facename
? pf
->facename
: "",
1459 pf
->firstchar
+pf
->size
-1,
1462 pf
->copyright
? pf
->copyright
: "",
1468 static int writebyte(FILE *fp
, unsigned char c
)
1470 return putc(c
, fp
) != EOF
;
1473 static int writeshort(FILE *fp
, unsigned short s
)
1476 return putc(s
>>8, fp
) != EOF
;
1479 static int writeint(FILE *fp
, unsigned int l
)
1484 return putc(l
>>24, fp
) != EOF
;
1487 static int writestr(FILE *fp
, char *str
, int count
)
1489 return (int)fwrite(str
, 1, count
, fp
) == count
;
1493 static int writestrpad(FILE *fp
, char *str
, int totlen
)
1497 while (str
&& *str
&& totlen
> 0) {
1499 ret
= putc(*str
++, fp
);
1503 while (--totlen
>= 0)
1504 ret
= putc(' ', fp
);
1509 /* generate .fnt format file from in-core font*/
1510 int gen_fnt_file(struct font
* pf
, char *path
)
1518 ofp
= fopen(path
, "wb");
1520 print_error("Can't create %s\n", path
);
1524 /* write magic and version #*/
1525 writestr(ofp
, VERSION
, 4);
1527 /* internal font name*/
1528 writestrpad(ofp
, pf
->name
, 64);
1531 writestrpad(ofp
, pf
->copyright
, 256);
1534 writeshort(ofp
, pf
->maxwidth
);
1535 writeshort(ofp
, pf
->height
);
1536 writeshort(ofp
, pf
->ascent
);
1538 writeint(ofp
, pf
->firstchar
);
1539 writeint(ofp
, pf
->defaultchar
);
1540 writeint(ofp
, pf
->size
);
1542 /* variable font data sizes*/
1543 writeint(ofp
, pf
->bits_size
); /* # words of bitmap_t*/
1544 writeint(ofp
, pf
->offset
? pf
->size
: 0); /* # ints of offset*/
1545 writeint(ofp
, pf
->width
? pf
->size
: 0); /* # bytes of width*/
1546 /* variable font data*/
1548 for (i
=0; i
<pf
->size
; ++i
)
1551 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1553 int char_code
= pf
->firstchar
+ i
;
1554 unsigned char bytemap
[ROTATION_BUF_SIZE
];
1556 /* Skip missing glyphs */
1557 if (pf
->offset
&& (pf
->offset
[i
] == -1))
1560 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1562 size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
, pf
->height
, char_code
);
1566 writestr(ofp
, (char *)bytemap
, size
);
1568 /* update offrot since bits are now in sorted order */
1569 pf
->offrot
[i
] = ofr
;
1573 if ( pf
->bits_size
< 0xFFDB )
1575 /* bitmap offset is small enough, use unsigned short for offset */
1577 writebyte(ofp
, 0); /* pad to 16-bit boundary*/
1581 /* bitmap offset is large then 64K, use unsigned int for offset */
1582 while (ftell(ofp
) & 3)
1583 writebyte(ofp
, 0); /* pad to 32-bit boundary*/
1588 for (i
=0; i
<pf
->size
; ++i
)
1590 if (pf
->offset
[i
] == -1) {
1591 pf
->offrot
[i
] = pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1593 if ( pf
->bits_size
< 0xFFDB )
1594 writeshort(ofp
, pf
->offrot
[i
]);
1596 writeint(ofp
, pf
->offrot
[i
]);
1601 for (i
=0; i
<pf
->size
; ++i
)
1602 writebyte(ofp
, pf
->width
[i
]);
1604 for (i
=0; i
<pf
->bits_size
; ++i
)
1605 writeshort(ofp
, pf
->bits
[i
]);
1607 writeshort(ofp
, 0); /* pad to 32-bit boundary*/
1610 for (i
=0; i
<pf
->size
; ++i
) {
1611 if (pf
->offset
[i
] == -1) {
1612 pf
->offset
[i
] = pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1614 writeint(ofp
, pf
->offset
[i
]);
1618 for (i
=0; i
<pf
->size
; ++i
)
1619 writebyte(ofp
, pf
->width
[i
]);