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
;
78 /* Description of how the ascent/descent is allowed to grow */
80 int value
; /* The delta value (in pixels or percents) */
81 int percent
; /* Is the value in percents (true) or pixels (false)? */
82 int force
; /* MUST the value be set (true) or is it just a max (false) */
85 #define isprefix(buf,str) (!strncmp(buf, str, strlen(str)))
86 #define strequal(s1,s2) (!strcmp(s1, s2))
88 #define MAX(a,b) ((a) > (b) ? (a) : (b))
89 #define MIN(a,b) ((a) < (b) ? (a) : (b))
92 #define ROTATION_BUF_SIZE 2048
95 /* Depending on the verbosity level some warnings are printed or not */
96 int verbosity_level
= 0;
99 /* Prints a warning of the specified verbosity level. It will only be
100 really printed if the level is >= the level set in the settings */
101 void print_warning(int level
, const char *fmt
, ...);
102 void print_error(const char *fmt
, ...);
103 void print_info(const char *fmt
, ...);
104 void print_trace(const char *fmt
, ...);
105 #define VL_CLIP_FONT 1 /* Verbosity level for clip related warnings at font level */
106 #define VL_CLIP_CHAR 2 /* Verbosity level for clip related warnings at char level */
107 #define VL_MISC 1 /* Verbosity level for other warnings */
114 int limit_char
= 65535;
118 struct stretch stretch_ascent
= { 0, 0, 1 }; /* Don't allow ascent to grow by default */
119 struct stretch stretch_descent
= { 0, 0, 1 }; /* Don't allow descent to grow by default */
123 void getopts(int *pac
, char ***pav
);
124 int convbdf(char *path
);
126 void free_font(struct font
* pf
);
127 struct font
* bdf_read_font(char *path
);
128 int bdf_read_header(FILE *fp
, struct font
* pf
);
129 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
);
132 Counts the glyphs and determines the max dimensions of glyphs
133 (fills the fields nchars, maxwidth, max_over_ascent, max_over_descent).
134 Returns 0 on failure or not-0 on success.
136 int bdf_analyze_font(FILE *fp
, struct font
* pf
);
137 void bdf_correct_bbx(int *width
, int *bbx
); /* Corrects bbx and width if bbx<0 */
139 /* Corrects the ascent and returns the new value (value to use) */
140 int adjust_ascent(int ascent
, int overflow
, struct stretch
*stretch
);
142 char * bdf_getline(FILE *fp
, char *buf
, int len
);
143 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
);
145 int gen_c_source(struct font
* pf
, char *path
);
146 int gen_h_header(struct font
* pf
, char *path
);
147 int gen_fnt_file(struct font
* pf
, char *path
);
152 /* We use string array because some C compilers issue warnings about too long strings */
154 "Usage: convbdf [options] [input-files]\n",
155 " convbdf [options] [-o output-file] [single-input-file]\n",
157 " -c Convert .bdf to .c source file\n",
158 " -h Convert .bdf to .h header file (to create sysfont.h)\n",
159 " -f Convert .bdf to .fnt font file\n",
160 " -s N Start output at character encodings >= N\n",
161 " -l N Limit output to character encodings <= N\n",
162 " -n Don't generate bitmaps as comments in .c file\n",
163 " -a N[%][!] Allow the ascent to grow N pixels/% to avoid glyph clipping\n",
164 " -d N[%][!] Allow the descent to grow N pixels/% to avoid glyph clipping\n",
165 " -v N Verbosity level: 0=quite quiet, 1=more verbose, 2=even more, etc.\n",
166 " -t Print internal tracing messages\n",
167 NULL
/* Must be the last element in the array */
172 print_info("%s", *(p
++));
176 void parse_ascent_opt(char *val
, struct stretch
*opt
) {
183 p
= buf
+ strlen(buf
);
190 else if (*p
== '!') {
198 opt
->value
= atoi(buf
);
201 /* parse command line options*/
202 void getopts(int *pac
, char ***pav
)
210 while (ac
> 0 && av
[0][0] == '-') {
214 case ' ': /* multiple -args on av[] */
215 while( *p
&& *p
== ' ')
217 if( *p
++ != '-') /* next option must have dash */
219 break; /* proceed to next option */
220 case 'c': /* generate .c output */
223 case 'h': /* generate .h output */
226 case 'f': /* generate .fnt output */
229 case 'n': /* don't gen bitmap comments */
232 case 'o': /* set output file */
236 while (*p
&& *p
!= ' ')
242 strcpy(outfile
, av
[0]);
245 case 'l': /* set encoding limit */
247 limit_char
= atoi(p
);
248 while (*p
&& *p
!= ' ')
254 limit_char
= atoi(av
[0]);
257 case 's': /* set encoding start */
259 start_char
= atoi(p
);
260 while (*p
&& *p
!= ' ')
266 start_char
= atoi(av
[0]);
269 case 'a': /* ascent growth */
271 parse_ascent_opt(p
, &stretch_ascent
);
272 while (*p
&& *p
!= ' ')
278 parse_ascent_opt(av
[0], &stretch_ascent
);
281 case 'd': /* descent growth */
283 parse_ascent_opt(p
, &stretch_descent
);
284 while (*p
&& *p
!= ' ')
290 parse_ascent_opt(av
[0], &stretch_descent
);
293 case 'v': /* verbosity */
295 verbosity_level
= atoi(p
);
296 while (*p
&& *p
!= ' ')
302 verbosity_level
= atoi(av
[0]);
305 case 't': /* tracing */
309 print_info("Unknown option ignored: %c\n", *(p
-1));
317 void print_warning(int level
, const char *fmt
, ...) {
318 if (verbosity_level
>= level
) {
321 fprintf(stderr
, " WARN: ");
322 vfprintf(stderr
, fmt
, ap
);
327 void print_trace(const char *fmt
, ...) {
331 fprintf(stderr
, "TRACE: ");
332 vfprintf(stderr
, fmt
, ap
);
337 void print_error(const char *fmt
, ...) {
340 fprintf(stderr
, "ERROR: ");
341 vfprintf(stderr
, fmt
, ap
);
345 void print_info(const char *fmt
, ...) {
348 fprintf(stderr
, " INFO: ");
349 vfprintf(stderr
, fmt
, ap
);
353 /* remove directory prefix and file suffix from full path */
354 char *basename(char *path
)
357 static char base
[256];
359 /* remove prepended path and extension */
361 for (p
=path
; *p
; ++p
) {
366 for (p
=base
; *p
; ++p
) {
375 int convbdf(char *path
)
380 pf
= bdf_read_font(path
);
386 strcpy(outfile
, basename(path
));
387 strcat(outfile
, ".c");
389 ret
|= gen_c_source(pf
, outfile
);
394 strcpy(outfile
, basename(path
));
395 strcat(outfile
, ".h");
397 ret
|= gen_h_header(pf
, outfile
);
402 strcpy(outfile
, basename(path
));
403 strcat(outfile
, ".fnt");
405 ret
|= gen_fnt_file(pf
, outfile
);
412 int main(int ac
, char **av
)
416 ++av
; --ac
; /* skip av[0] */
417 getopts(&ac
, &av
); /* read command line options */
419 if (ac
< 1 || (!gen_c
&& !gen_h
&& !gen_fnt
)) {
424 if (ac
> 1 || (gen_c
&& gen_fnt
) || (gen_c
&& gen_h
) || (gen_h
&& gen_fnt
)) {
431 ret
|= convbdf(av
[0]);
438 /* free font structure */
439 void free_font(struct font
* pf
)
458 /* build incore structure from .bdf file */
459 struct font
* bdf_read_font(char *path
)
464 fp
= fopen(path
, "rb");
466 print_error("Error opening file: %s\n", path
);
470 pf
= (struct font
*)calloc(1, sizeof(struct font
));
473 memset(pf
, 0, sizeof(struct font
));
475 pf
->name
= strdup(basename(path
));
477 if (!bdf_read_header(fp
, pf
)) {
478 print_error("Error reading font header\n");
481 print_trace("Read font header, nchars_decl=%d\n", pf
->nchars_declared
);
483 if (!bdf_analyze_font(fp
, pf
)) {
484 print_error("Error analyzing the font\n");
487 print_trace("Analyzed font, nchars=%d, maxwidth=%d, asc_over=%d, desc_over=%d\n",
488 pf
->nchars
, pf
->maxwidth
, pf
->max_over_ascent
, pf
->max_over_descent
);
490 if (pf
->nchars
!= pf
->nchars_declared
) {
491 print_warning(VL_MISC
, "The declared number of chars (%d) "
492 "does not match the real number (%d)\n",
493 pf
->nchars_declared
, pf
->nchars
);
496 /* Correct ascent/descent if necessary */
497 pf
->ascent
= adjust_ascent(pf
->ascent_declared
, pf
->max_over_ascent
, &stretch_ascent
);
498 if (pf
->ascent
!= pf
->ascent_declared
) {
499 print_info("Font ascent has been changed from %d to %d\n",
500 pf
->ascent_declared
, pf
->ascent
);
502 pf
->descent
= adjust_ascent(pf
->descent
, pf
->max_over_descent
, &stretch_descent
);
503 if (pf
->descent
!= pf
->descent_declared
) {
504 print_info("Font descent has been changed from %d to %d\n",
505 pf
->descent_declared
, pf
->descent
);
507 pf
->height
= pf
->ascent
+ pf
->descent
;
508 if (pf
->height
!= pf
->ascent_declared
+ pf
->descent_declared
) {
509 print_warning(VL_CLIP_FONT
, "Generated font's height: %d\n", pf
->height
);
512 if (pf
->ascent
> pf
->max_char_ascent
) {
513 print_trace("Font's ascent could be reduced by %d to %d without clipping\n",
514 (pf
->ascent
- pf
->max_char_ascent
), pf
->max_char_ascent
);
516 if (pf
->descent
> pf
->max_char_descent
) {
517 print_trace("Font's descent could be reduced by %d to %d without clipping\n",
518 (pf
->descent
- pf
->max_char_descent
), pf
->max_char_descent
);
523 pf
->bits_size
= pf
->size
* BITMAP_WORDS(pf
->maxwidth
) * pf
->height
;
524 pf
->bits
= (bitmap_t
*)malloc(pf
->bits_size
* sizeof(bitmap_t
));
525 pf
->offset
= (int *)malloc(pf
->size
* sizeof(int));
526 pf
->offrot
= (unsigned int *)malloc(pf
->size
* sizeof(unsigned int));
527 pf
->width
= (unsigned char *)malloc(pf
->size
* sizeof(unsigned char));
529 if (!pf
->bits
|| !pf
->offset
|| !pf
->offrot
|| !pf
->width
) {
530 print_error("no memory for font load\n");
534 pf
->num_clipped_ascent
= pf
->num_clipped_descent
= pf
->num_clipped
= 0;
535 pf
->max_over_ascent
= pf
->max_over_descent
= 0;
537 if (!bdf_read_bitmaps(fp
, pf
)) {
538 print_error("Error reading font bitmaps\n");
541 print_trace("Read bitmaps\n");
543 if (pf
->num_clipped
> 0) {
544 print_warning(VL_CLIP_FONT
, "%d character(s) out of %d were clipped "
545 "(%d at ascent, %d at descent)\n",
546 pf
->num_clipped
, pf
->nchars
,
547 pf
->num_clipped_ascent
, pf
->num_clipped_descent
);
548 print_warning(VL_CLIP_FONT
, "max overflows: %d pixel(s) at ascent, %d pixel(s) at descent\n",
549 pf
->max_over_ascent
, pf
->max_over_descent
);
561 /* read bdf font header information, return 0 on error */
562 int bdf_read_header(FILE *fp
, struct font
* pf
)
565 int firstchar
= 65535;
571 /* set certain values to errors for later error checking */
572 pf
->defaultchar
= -1;
577 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
578 print_error("EOF on file\n");
581 if (isprefix(buf
, "FONT ")) { /* not required */
582 if (sscanf(buf
, "FONT %[^\n]", facename
) != 1) {
583 print_error("bad 'FONT'\n");
586 pf
->facename
= strdup(facename
);
589 if (isprefix(buf
, "COPYRIGHT ")) { /* not required */
590 if (sscanf(buf
, "COPYRIGHT \"%[^\"]", copyright
) != 1) {
591 print_error("bad 'COPYRIGHT'\n");
594 pf
->copyright
= strdup(copyright
);
597 if (isprefix(buf
, "DEFAULT_CHAR ")) { /* not required */
598 if (sscanf(buf
, "DEFAULT_CHAR %d", &pf
->defaultchar
) != 1) {
599 print_error("bad 'DEFAULT_CHAR'\n");
603 if (isprefix(buf
, "FONT_DESCENT ")) {
604 if (sscanf(buf
, "FONT_DESCENT %d", &pf
->descent_declared
) != 1) {
605 print_error("bad 'FONT_DESCENT'\n");
608 pf
->descent
= pf
->descent_declared
; /* For now */
611 if (isprefix(buf
, "FONT_ASCENT ")) {
612 if (sscanf(buf
, "FONT_ASCENT %d", &pf
->ascent_declared
) != 1) {
613 print_error("bad 'FONT_ASCENT'\n");
616 pf
->ascent
= pf
->ascent_declared
; /* For now */
619 if (isprefix(buf
, "FONTBOUNDINGBOX ")) {
620 if (sscanf(buf
, "FONTBOUNDINGBOX %d %d %d %d",
621 &pf
->fbbw
, &pf
->fbbh
, &pf
->fbbx
, &pf
->fbby
) != 4) {
622 print_error("bad 'FONTBOUNDINGBOX'\n");
627 if (isprefix(buf
, "CHARS ")) {
628 if (sscanf(buf
, "CHARS %d", &pf
->nchars_declared
) != 1) {
629 print_error("bad 'CHARS'\n");
636 * Reading ENCODING is necessary to get firstchar/lastchar
637 * which is needed to pre-calculate our offset and widths
640 if (isprefix(buf
, "ENCODING ")) {
641 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
642 print_error("bad 'ENCODING'\n");
646 encoding
<= limit_char
&&
647 encoding
>= start_char
) {
649 if (firstchar
> encoding
)
650 firstchar
= encoding
;
651 if (lastchar
< encoding
)
656 if (strequal(buf
, "ENDFONT"))
660 /* calc font height*/
661 if (pf
->ascent
< 0 || pf
->descent
< 0 || firstchar
< 0) {
662 print_error("Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
665 pf
->height
= pf
->ascent
+ pf
->descent
;
667 /* calc default char */
668 if (pf
->defaultchar
< 0 ||
669 pf
->defaultchar
< firstchar
||
670 pf
->defaultchar
> limit_char
||
671 pf
->defaultchar
> lastchar
)
672 pf
->defaultchar
= firstchar
;
674 /* calc font size (offset/width entries) */
675 pf
->firstchar
= firstchar
;
676 pf
->size
= lastchar
- firstchar
+ 1;
682 * TODO: rework the code to avoid logics duplication in
683 * bdf_read_bitmaps and bdf_analyze_font
687 /* read bdf font bitmaps, return 0 on error */
688 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
)
692 int i
, k
, encoding
, width
;
693 int bbw
, bbh
, bbx
, bby
;
694 int proportional
= 0;
701 /* reset file pointer */
702 fseek(fp
, 0L, SEEK_SET
);
704 /* initially mark offsets as not used */
705 for (i
=0; i
<pf
->size
; ++i
)
709 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
710 print_error("EOF on file\n");
713 if (isprefix(buf
, "STARTCHAR")) {
714 encoding
= width
= -1;
721 if (isprefix(buf
, "ENCODING ")) {
722 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
723 print_error("bad 'ENCODING'\n");
726 if (encoding
< start_char
|| encoding
> limit_char
)
730 if (isprefix(buf
, "DWIDTH ")) {
731 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
732 print_error("bad 'DWIDTH'\n");
735 /* use font boundingbox width if DWIDTH <= 0 */
737 width
= pf
->fbbw
- pf
->fbbx
;
740 if (isprefix(buf
, "BBX ")) {
741 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
742 print_error("bad 'BBX'\n");
747 if (strequal(buf
, "BITMAP") || strequal(buf
, "BITMAP ")) {
748 int overflow_asc
, overflow_desc
;
749 int bbh_orig
, bby_orig
, y
;
754 /* set bits offset in encode map*/
755 if (pf
->offset
[encoding
-pf
->firstchar
] != -1) {
756 print_error("duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
760 pf
->offset
[encoding
-pf
->firstchar
] = ofs
;
761 pf
->offrot
[encoding
-pf
->firstchar
] = ofr
;
763 /* calc char width */
764 bdf_correct_bbx(&width
, &bbx
);
765 pf
->width
[encoding
-pf
->firstchar
] = width
;
767 ch_bitmap
= pf
->bits
+ ofs
;
768 ch_words
= BITMAP_WORDS(width
);
769 memset(ch_bitmap
, 0, BITMAP_BYTES(width
) * pf
->height
); /* clear bitmap */
771 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
772 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
777 overflow_asc
= bby
+ bbh
- pf
->ascent
;
778 if (overflow_asc
> 0) {
779 pf
->num_clipped_ascent
++;
780 if (overflow_asc
> pf
->max_over_ascent
) {
781 pf
->max_over_ascent
= overflow_asc
;
783 bbh
= MAX(bbh
- overflow_asc
, 0); /* Clipped -> decrease the height */
784 print_warning(VL_CLIP_CHAR
, "character %d goes %d pixel(s)"
785 " beyond the font's ascent, it will be clipped\n",
786 encoding
, overflow_asc
);
788 overflow_desc
= -bby
- pf
->descent
;
789 if (overflow_desc
> 0) {
790 pf
->num_clipped_descent
++;
791 if (overflow_desc
> pf
->max_over_descent
) {
792 pf
->max_over_descent
= overflow_desc
;
794 bby
+= overflow_desc
;
795 bbh
= MAX(bbh
- overflow_desc
, 0); /* Clipped -> decrease the height */
796 print_warning(VL_CLIP_CHAR
, "character %d goes %d pixel(s)"
797 " beyond the font's descent, it will be clipped\n",
798 encoding
, overflow_desc
);
800 if (overflow_asc
> 0 || overflow_desc
> 0) {
804 y
= bby_orig
+ bbh_orig
; /* 0-based y within the char */
810 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
811 print_error("EOF reading BITMAP data for character %d\n",
815 if (isprefix(buf
, "ENDCHAR"))
819 if ((y
>= pf
->ascent
) || (y
< -pf
->descent
)) {
820 /* We're beyond the area that Rockbox can render -> clip */
821 --i
; /* This line doesn't count */
825 hexnibbles
= strlen(buf
);
826 for (k
=0; k
<ch_words
; ++k
) {
827 int ndx
= k
* BITMAP_NIBBLES
;
828 int padnibbles
= hexnibbles
- ndx
;
833 if (padnibbles
>= (int)BITMAP_NIBBLES
)
836 value
= bdf_hexval((unsigned char *)buf
,
837 ndx
, ndx
+BITMAP_NIBBLES
-1-padnibbles
);
838 value
<<= padnibbles
* BITMAP_NIBBLES
;
840 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
) |=
842 /* handle overflow into next image word */
844 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
+1) =
845 value
<< (BITMAP_BITSPERIMAGE
- bbx
);
850 ofs
+= BITMAP_WORDS(width
) * pf
->height
;
851 ofr
+= pf
->width
[encoding
-pf
->firstchar
] * ((pf
->height
+7)/8);
855 if (strequal(buf
, "ENDFONT"))
859 /* change unused width values to default char values */
860 for (i
=0; i
<pf
->size
; ++i
) {
861 int defchar
= pf
->defaultchar
- pf
->firstchar
;
863 if (pf
->offset
[i
] == -1)
864 pf
->width
[i
] = pf
->width
[defchar
];
867 /* determine whether font doesn't require encode table */
870 for (i
=0; i
<pf
->size
; ++i
) {
871 if ((int)pf
->offrot
[i
] != l
) {
875 l
+= pf
->maxwidth
* ((pf
->height
+ 7) / 8);
879 for (i
=0; i
<pf
->size
; ++i
) {
880 if (pf
->offset
[i
] != l
) {
884 l
+= BITMAP_WORDS(pf
->width
[i
]) * pf
->height
;
892 /* determine whether font is fixed-width */
893 for (i
=0; i
<pf
->size
; ++i
) {
894 if (pf
->width
[i
] != pf
->maxwidth
) {
904 /* reallocate bits array to actual bits used */
905 if (ofs
< pf
->bits_size
) {
906 pf
->bits
= realloc(pf
->bits
, ofs
* sizeof(bitmap_t
));
911 pf
->bits_size
= ofr
; /* always update, rotated is smaller */
917 /* read the next non-comment line, returns buf or NULL if EOF */
918 char *bdf_getline(FILE *fp
, char *buf
, int len
)
925 while ((c
= getc(fp
)) != EOF
) {
930 if (b
- buf
>= (len
- 1))
935 if (c
== EOF
&& b
== buf
)
937 if (b
!= buf
&& !isprefix(buf
, "COMMENT"))
943 void bdf_correct_bbx(int *width
, int *bbx
) {
945 /* Rockbox can't render overlapping glyphs */
951 int bdf_analyze_font(FILE *fp
, struct font
* pf
) {
954 int width
, bbw
, bbh
, bbx
, bby
, ascent
, overflow
;
955 int read_enc
= 0, read_width
= 0, read_bbx
= 0, read_endchar
= 1;
958 /* reset file pointer */
959 fseek(fp
, 0L, SEEK_SET
);
963 pf
->max_char_ascent
= pf
->max_char_descent
= 0;
964 pf
->max_over_ascent
= pf
->max_over_descent
= 0;
968 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
969 print_error("EOF on file\n");
972 if (isprefix(buf
, "ENDFONT")) {
974 print_error("No terminating ENDCHAR for character %d\n", encoding
);
979 if (isprefix(buf
, "STARTCHAR")) {
980 print_trace("Read STARTCHAR, nchars=%d, read_endchar=%d\n", pf
->nchars
, read_endchar
);
982 print_error("No terminating ENDCHAR for character %d\n", encoding
);
985 read_enc
= read_width
= read_bbx
= read_endchar
= 0;
988 if (isprefix(buf
, "ENDCHAR")) {
990 print_error("ENCODING is not specified\n");
993 ignore_char
= (encoding
< start_char
|| encoding
> limit_char
);
995 if (!read_width
|| !read_bbx
) {
996 print_error("WIDTH or BBX is not specified for character %d\n",
999 bdf_correct_bbx(&width
, &bbx
);
1000 if (width
> pf
->maxwidth
) {
1001 pf
->maxwidth
= width
;
1005 pf
->max_char_ascent
= MAX(pf
->max_char_ascent
, ascent
);
1006 overflow
= ascent
- pf
->ascent
;
1007 pf
->max_over_ascent
= MAX(pf
->max_over_ascent
, overflow
);
1010 pf
->max_char_descent
= MAX(pf
->max_char_descent
, ascent
);
1011 overflow
= ascent
- pf
->descent
;
1012 pf
->max_over_descent
= MAX(pf
->max_over_descent
, overflow
);
1018 if (isprefix(buf
, "ENCODING ")) {
1019 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
1020 print_error("bad 'ENCODING': '%s'\n", buf
);
1026 if (isprefix(buf
, "DWIDTH ")) {
1027 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
1028 print_error("bad 'DWIDTH': '%s'\n", buf
);
1031 /* use font boundingbox width if DWIDTH <= 0 */
1033 print_error("Negative char width: %d\n", width
);
1038 if (isprefix(buf
, "BBX ")) {
1039 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
1040 print_error("bad 'BBX': '%s'\n", buf
);
1050 int adjust_ascent(int ascent
, int overflow
, struct stretch
*stretch
) {
1052 int px
= stretch
->value
;
1053 if (stretch
->percent
) {
1054 px
= ascent
* px
/ 100;
1057 if (stretch
->force
) {
1058 result
= ascent
+ px
;
1061 result
= ascent
+ MIN(overflow
, px
);
1063 result
= MAX(result
, 0);
1068 /* return hex value of portion of buffer*/
1069 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
)
1074 for (i
=ndx1
; i
<=ndx2
; ++i
) {
1076 if (c
>= '0' && c
<= '9')
1079 if (c
>= 'A' && c
<= 'F')
1082 if (c
>= 'a' && c
<= 'f')
1086 val
= (val
<< 4) | c
;
1095 * Take an bitmap_t bitmap and convert to Rockbox format.
1096 * Used for converting font glyphs for the time being.
1097 * Can use for standard X11 and Win32 images as well.
1098 * See format description in lcd-recorder.c
1100 * Doing it this way keeps fonts in standard formats,
1101 * as well as keeping Rockbox hw bitmap format.
1103 * Returns the size of the rotated glyph (in bytes) or a
1104 * negative value if the glyph could not be rotated.
1106 int rotleft(unsigned char *dst
, /* output buffer */
1107 size_t dstlen
, /* buffer size */
1108 bitmap_t
*src
, unsigned int width
, unsigned int height
,
1112 unsigned int src_words
; /* # words of input image */
1113 unsigned int dst_mask
; /* bit mask for destination */
1114 bitmap_t src_mask
; /* bit mask for source */
1116 /* How large the buffer should be to hold the rotated bitmap
1117 of a glyph of size (width x height) */
1118 unsigned int needed_size
= ((height
+ 7) / 8) * width
;
1120 if (needed_size
> dstlen
) {
1121 print_error("Character %d: Glyph of size %d x %d can't be rotated "
1122 "(buffer size is %lu, needs %u)\n",
1123 char_code
, width
, height
, (unsigned long)dstlen
, needed_size
);
1127 /* calc words of input image*/
1128 src_words
= BITMAP_WORDS(width
) * height
;
1130 /* clear background*/
1131 memset(dst
, 0, needed_size
);
1135 for (i
=0; i
< src_words
; i
++) {
1137 /* calc src input bit*/
1138 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
1140 /* for each input column...*/
1141 for(j
=0; j
< width
; j
++) {
1143 if (src_mask
== 0) /* input word done? */
1145 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
1146 i
++; /* next input word */
1149 /* if set in input, set in rotated output */
1150 if (src
[i
] & src_mask
)
1153 src_mask
>>= 1; /* next input bit */
1156 dst_mask
<<= 1; /* next output bit (row) */
1157 if (dst_mask
> (1 << 7)) /* output bit > 7? */
1160 dst
+= width
; /* next output byte row */
1163 return needed_size
; /* return result size in bytes */
1169 /* generate C source from in-core font*/
1170 int gen_c_source(struct font
* pf
, char *path
)
1178 int did_syncmsg
= 0;
1179 bitmap_t
*ofs
= pf
->bits
;
1184 "/* Generated by convbdf on %s. */\n"
1185 "#include \"font.h\"\n"
1186 "#ifdef HAVE_LCD_BITMAP\n"
1188 "/* Font information:\n"
1195 " first char: %d (0x%02x)\n"
1196 " last char: %d (0x%02x)\n"
1197 " default char: %d (0x%02x)\n"
1198 " proportional: %s\n"
1202 "/* Font character bitmap data. */\n"
1203 "static const unsigned char _font_bits[] = {\n"
1206 ofp
= fopen(path
, "w");
1208 print_error("Can't create %s\n", path
);
1212 strcpy(buf
, ctime(&t
));
1213 buf
[strlen(buf
)-1] = 0;
1215 fprintf(ofp
, hdr1
, buf
,
1217 pf
->facename
? pf
->facename
: "",
1218 pf
->maxwidth
, pf
->height
,
1220 pf
->ascent
, pf
->descent
,
1221 pf
->firstchar
, pf
->firstchar
,
1222 pf
->firstchar
+pf
->size
-1, pf
->firstchar
+pf
->size
-1,
1223 pf
->defaultchar
, pf
->defaultchar
,
1224 pf
->width
? "yes": "no",
1225 pf
->copyright
? pf
->copyright
: "");
1227 /* generate bitmaps*/
1228 for (i
=0; i
<pf
->size
; ++i
) {
1231 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1232 int height
= pf
->height
;
1233 int char_code
= pf
->firstchar
+ i
;
1235 bitmap_t bitvalue
=0;
1237 /* Skip missing glyphs */
1238 if (pf
->offset
&& (pf
->offset
[i
] == -1))
1241 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1243 fprintf(ofp
, "\n/* Character %d (0x%02x):\n width %d",
1244 char_code
, char_code
, width
);
1247 fprintf(ofp
, "\n +");
1248 for (x
=0; x
<width
; ++x
) fprintf(ofp
, "-");
1249 fprintf(ofp
, "+\n");
1252 while (height
> 0) {
1253 if (x
== 0) fprintf(ofp
, " |");
1255 if (bitcount
<= 0) {
1256 bitcount
= BITMAP_BITSPERIMAGE
;
1260 fprintf(ofp
, BITMAP_TESTBIT(bitvalue
)? "*": " ");
1262 bitvalue
= BITMAP_SHIFTBIT(bitvalue
);
1265 fprintf(ofp
, "|\n");
1272 for (x
=0; x
<width
; ++x
)
1274 fprintf(ofp
, "+ */\n");
1277 fprintf(ofp
, " */\n");
1279 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1280 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
1282 unsigned char bytemap
[ROTATION_BUF_SIZE
];
1285 int size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
,
1286 pf
->height
, char_code
);
1291 for (y8
=0; y8
<pf
->height
; y8
+=8) /* column rows */
1293 for (x
=0; x
<width
; x
++) {
1294 fprintf(ofp
, "0x%02x, ", bytemap
[ix
]);
1300 /* update offrot since bits are now in sorted order */
1301 pf
->offrot
[i
] = ofr
;
1306 for (x
=BITMAP_WORDS(width
)*pf
->height
; x
>0; --x
) {
1307 fprintf(ofp
, "0x%04x,\n", *bits
);
1308 if (!did_syncmsg
&& *bits
++ != *ofs
++) {
1309 print_warning(VL_MISC
, "found encoding values in non-sorted order (not an error).\n");
1315 fprintf(ofp
, "};\n\n");
1318 /* output offset table*/
1319 fprintf(ofp
, "/* Character->glyph mapping. */\n"
1320 "static const unsigned short _sysfont_offset[] = {\n");
1322 for (i
=0; i
<pf
->size
; ++i
) {
1323 if (pf
->offset
[i
] == -1) {
1324 pf
->offset
[i
] = pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1325 pf
->offrot
[i
] = pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1327 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
1329 pf
->offrot
[i
], i
+pf
->firstchar
);
1331 pf
->offset
[i
], i
+pf
->firstchar
);
1334 fprintf(ofp
, "};\n\n");
1337 /* output width table for proportional fonts*/
1339 fprintf(ofp
, "/* Character width data. */\n"
1340 "static const unsigned char _sysfont_width[] = {\n");
1342 for (i
=0; i
<pf
->size
; ++i
)
1343 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
1344 pf
->width
[i
], i
+pf
->firstchar
);
1345 fprintf(ofp
, "};\n\n");
1348 /* output struct font struct*/
1350 sprintf(obuf
, "_sysfont_offset,");
1352 sprintf(obuf
, "0, /* no encode table */");
1355 sprintf(buf
, "_sysfont_width, /* width */");
1357 sprintf(buf
, "0, /* fixed width */");
1359 fprintf(ofp
, "/* Exported structure definition. */\n"
1360 "const struct font sysfont = {\n"
1361 " %d, /* maxwidth */\n"
1362 " %d, /* height */\n"
1363 " %d, /* ascent */\n"
1364 " %d, /* firstchar */\n"
1366 " _font_bits, /* bits */\n"
1367 " %s /* offset */\n"
1369 " %d, /* defaultchar */\n"
1370 " %d /* bits_size */\n"
1372 "#endif /* HAVE_LCD_BITMAP */\n",
1373 pf
->maxwidth
, pf
->height
,
1385 /* generate C header from in-core font*/
1386 int gen_h_header(struct font
* pf
, char *path
)
1392 "/* Generated by convbdf on %s. */\n"
1393 "#ifdef HAVE_LCD_BITMAP\n"
1395 "/* Font information*/\n"
1396 "#define SYSFONT_NAME %s\n"
1397 "#define SYSFONT_FACENAME %s\n"
1398 "#define SYSFONT_WIDTH %d\n"
1399 "#define SYSFONT_HEIGHT %d\n"
1400 "#define SYSFONT_SIZE %d\n"
1401 "#define SYSFONT_ASCENT %d\n";
1403 "#define SYSFONT_DESCENT %d\n"
1404 "#define SYSFONT_FIRST_CHAR %d\n"
1405 "#define SYSFONT_LAST_CHAR %d\n"
1406 "#define SYSFONT_DEFAULT_CHAR %d\n"
1407 "#define SYSFONT_PROPORTIONAL %d\n"
1408 "#define SYSFONT_COPYRIGHT %s\n"
1409 "#define SYSFONT_BITS_SIZE %d\n"
1413 ofp
= fopen(path
, "w");
1415 print_error("Can't create %s\n", path
);
1419 strcpy(buf
, ctime(&t
));
1420 buf
[strlen(buf
)-1] = 0;
1422 fprintf(ofp
, hdr1
, buf
,
1424 pf
->facename
? pf
->facename
: "",
1433 pf
->firstchar
+pf
->size
-1,
1436 pf
->copyright
? pf
->copyright
: "",
1442 static int writebyte(FILE *fp
, unsigned char c
)
1444 return putc(c
, fp
) != EOF
;
1447 static int writeshort(FILE *fp
, unsigned short s
)
1450 return putc(s
>>8, fp
) != EOF
;
1453 static int writeint(FILE *fp
, unsigned int l
)
1458 return putc(l
>>24, fp
) != EOF
;
1461 static int writestr(FILE *fp
, char *str
, int count
)
1463 return (int)fwrite(str
, 1, count
, fp
) == count
;
1467 static int writestrpad(FILE *fp
, char *str
, int totlen
)
1471 while (str
&& *str
&& totlen
> 0) {
1473 ret
= putc(*str
++, fp
);
1477 while (--totlen
>= 0)
1478 ret
= putc(' ', fp
);
1483 /* generate .fnt format file from in-core font*/
1484 int gen_fnt_file(struct font
* pf
, char *path
)
1492 ofp
= fopen(path
, "wb");
1494 print_error("Can't create %s\n", path
);
1498 /* write magic and version #*/
1499 writestr(ofp
, VERSION
, 4);
1501 /* internal font name*/
1502 writestrpad(ofp
, pf
->name
, 64);
1505 writestrpad(ofp
, pf
->copyright
, 256);
1508 writeshort(ofp
, pf
->maxwidth
);
1509 writeshort(ofp
, pf
->height
);
1510 writeshort(ofp
, pf
->ascent
);
1512 writeint(ofp
, pf
->firstchar
);
1513 writeint(ofp
, pf
->defaultchar
);
1514 writeint(ofp
, pf
->size
);
1516 /* variable font data sizes*/
1517 writeint(ofp
, pf
->bits_size
); /* # words of bitmap_t*/
1518 writeint(ofp
, pf
->offset
? pf
->size
: 0); /* # ints of offset*/
1519 writeint(ofp
, pf
->width
? pf
->size
: 0); /* # bytes of width*/
1520 /* variable font data*/
1522 for (i
=0; i
<pf
->size
; ++i
)
1525 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1527 int char_code
= pf
->firstchar
+ i
;
1528 unsigned char bytemap
[ROTATION_BUF_SIZE
];
1530 /* Skip missing glyphs */
1531 if (pf
->offset
&& (pf
->offset
[i
] == -1))
1534 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1536 size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
, pf
->height
, char_code
);
1540 writestr(ofp
, (char *)bytemap
, size
);
1542 /* update offrot since bits are now in sorted order */
1543 pf
->offrot
[i
] = ofr
;
1547 if ( pf
->bits_size
< 0xFFDB )
1549 /* bitmap offset is small enough, use unsigned short for offset */
1551 writebyte(ofp
, 0); /* pad to 16-bit boundary*/
1555 /* bitmap offset is large then 64K, use unsigned int for offset */
1556 while (ftell(ofp
) & 3)
1557 writebyte(ofp
, 0); /* pad to 32-bit boundary*/
1562 for (i
=0; i
<pf
->size
; ++i
)
1564 if (pf
->offset
[i
] == -1) {
1565 pf
->offrot
[i
] = pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1567 if ( pf
->bits_size
< 0xFFDB )
1568 writeshort(ofp
, pf
->offrot
[i
]);
1570 writeint(ofp
, pf
->offrot
[i
]);
1575 for (i
=0; i
<pf
->size
; ++i
)
1576 writebyte(ofp
, pf
->width
[i
]);
1578 for (i
=0; i
<pf
->bits_size
; ++i
)
1579 writeshort(ofp
, pf
->bits
[i
]);
1581 writeshort(ofp
, 0); /* pad to 32-bit boundary*/
1584 for (i
=0; i
<pf
->size
; ++i
) {
1585 if (pf
->offset
[i
] == -1) {
1586 pf
->offset
[i
] = pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1588 writeint(ofp
, pf
->offset
[i
]);
1592 for (i
=0; i
<pf
->size
; ++i
)
1593 writebyte(ofp
, pf
->width
[i
]);