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 number */
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 int depth
; /* depth of the font, 0=1bit 1=4bit */
50 bitmap_t
* bits
; /* 16-bit right-padded bitmap data */
51 int* offset
; /* offsets into bitmap data */
52 unsigned char* width
; /* character widths or NULL if fixed */
53 int defaultchar
; /* default char (not glyph index) */
54 int bits_size
; /* # words of bitmap_t bits */
56 /* unused by runtime system, read in by convbdf */
57 int nchars
; /* number of different glyphs */
58 int nchars_declared
; /* number of glyphs as declared in the header */
59 int ascent_declared
; /* ascent as declared in the header */
60 int descent_declared
; /* descent as declared in the header */
61 int max_char_ascent
; /* max. char ascent (before adjusting) */
62 int max_char_descent
; /* max. char descent (before adjusting) */
63 unsigned int* offrot
; /* offsets into rotated bitmap data */
64 char* name
; /* font name */
65 char* facename
; /* facename of font */
66 char* copyright
; /* copyright info for loadable fonts */
69 int fbbw
, fbbh
, fbbx
, fbby
;
71 /* Max 'overflow' of a char's ascent (descent) over the font's one */
72 int max_over_ascent
, max_over_descent
;
74 /* The number of clipped ascents/descents/total */
75 int num_clipped_ascent
, num_clipped_descent
, num_clipped
;
77 /* default width in pixels (can be overwritten at char level) */
82 /* Description of how the ascent/descent is allowed to grow */
84 int value
; /* The delta value (in pixels or percents) */
85 int percent
; /* Is the value in percents (true) or pixels (false)? */
86 int force
; /* MUST the value be set (true) or is it just a max (false) */
89 #define isprefix(buf,str) (!strncmp(buf, str, strlen(str)))
90 #define strequal(s1,s2) (!strcmp(s1, s2))
92 #define MAX(a,b) ((a) > (b) ? (a) : (b))
93 #define MIN(a,b) ((a) < (b) ? (a) : (b))
96 #define ROTATION_BUF_SIZE 2048
99 /* Depending on the verbosity level some warnings are printed or not */
100 int verbosity_level
= 0;
103 /* Prints a warning of the specified verbosity level. It will only be
104 really printed if the level is >= the level set in the settings */
105 void print_warning(int level
, const char *fmt
, ...);
106 void print_error(const char *fmt
, ...);
107 void print_info(const char *fmt
, ...);
108 void print_trace(const char *fmt
, ...);
109 #define VL_CLIP_FONT 1 /* Verbosity level for clip related warnings at font level */
110 #define VL_CLIP_CHAR 2 /* Verbosity level for clip related warnings at char level */
111 #define VL_MISC 1 /* Verbosity level for other warnings */
118 int limit_char
= 65535;
122 struct stretch stretch_ascent
= { 0, 0, 1 }; /* Don't allow ascent to grow by default */
123 struct stretch stretch_descent
= { 0, 0, 1 }; /* Don't allow descent to grow by default */
127 void getopts(int *pac
, char ***pav
);
128 int convbdf(char *path
);
130 void free_font(struct font
* pf
);
131 struct font
* bdf_read_font(char *path
);
132 int bdf_read_header(FILE *fp
, struct font
* pf
);
133 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
);
136 Counts the glyphs and determines the max dimensions of glyphs
137 (fills the fields nchars, maxwidth, max_over_ascent, max_over_descent).
138 Returns 0 on failure or not-0 on success.
140 int bdf_analyze_font(FILE *fp
, struct font
* pf
);
141 void bdf_correct_bbx(int *width
, int *bbx
); /* Corrects bbx and width if bbx<0 */
143 /* Corrects the ascent and returns the new value (value to use) */
144 int adjust_ascent(int ascent
, int overflow
, struct stretch
*stretch
);
146 char * bdf_getline(FILE *fp
, char *buf
, int len
);
147 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
);
149 int gen_c_source(struct font
* pf
, char *path
);
150 int gen_h_header(struct font
* pf
, char *path
);
151 int gen_fnt_file(struct font
* pf
, char *path
);
156 /* We use string array because some C compilers issue warnings about too long strings */
158 "Usage: convbdf [options] [input-files]\n",
159 " convbdf [options] [-o output-file] [single-input-file]\n",
161 " -c Convert .bdf to .c source file\n",
162 " -h Convert .bdf to .h header file (to create sysfont.h)\n",
163 " -f Convert .bdf to .fnt font file\n",
164 " -s N Start output at character encodings >= N\n",
165 " -l N Limit output to character encodings <= N\n",
166 " -n Don't generate bitmaps as comments in .c file\n",
167 " -a N[%][!] Allow the ascent to grow N pixels/% to avoid glyph clipping\n",
168 " -d N[%][!] Allow the descent to grow N pixels/% to avoid glyph clipping\n",
169 " An ! in the -a and -d options forces the growth; N may be negative\n",
170 " -v N Verbosity level: 0=quite quiet, 1=more verbose, 2=even more, etc.\n",
171 " -t Print internal tracing messages\n",
172 NULL
/* Must be the last element in the array */
177 print_info("%s", *(p
++));
181 void parse_ascent_opt(char *val
, struct stretch
*opt
) {
188 p
= buf
+ strlen(buf
);
195 else if (*p
== '!') {
203 opt
->value
= atoi(buf
);
206 /* parse command line options */
207 void getopts(int *pac
, char ***pav
)
215 while (ac
> 0 && av
[0][0] == '-') {
219 case ' ': /* multiple -args on av[] */
220 while( *p
&& *p
== ' ')
222 if( *p
++ != '-') /* next option must have dash */
224 break; /* proceed to next option */
225 case 'c': /* generate .c output */
228 case 'h': /* generate .h output */
231 case 'f': /* generate .fnt output */
234 case 'n': /* don't gen bitmap comments */
237 case 'o': /* set output file */
241 while (*p
&& *p
!= ' ')
247 strcpy(outfile
, av
[0]);
250 case 'l': /* set encoding limit */
252 limit_char
= atoi(p
);
253 while (*p
&& *p
!= ' ')
259 limit_char
= atoi(av
[0]);
262 case 's': /* set encoding start */
264 start_char
= atoi(p
);
265 while (*p
&& *p
!= ' ')
271 start_char
= atoi(av
[0]);
274 case 'a': /* ascent growth */
276 parse_ascent_opt(p
, &stretch_ascent
);
277 while (*p
&& *p
!= ' ')
283 parse_ascent_opt(av
[0], &stretch_ascent
);
286 case 'd': /* descent growth */
288 parse_ascent_opt(p
, &stretch_descent
);
289 while (*p
&& *p
!= ' ')
295 parse_ascent_opt(av
[0], &stretch_descent
);
298 case 'v': /* verbosity */
300 verbosity_level
= atoi(p
);
301 while (*p
&& *p
!= ' ')
307 verbosity_level
= atoi(av
[0]);
310 case 't': /* tracing */
314 print_info("Unknown option ignored: %c\n", *(p
-1));
322 void print_warning(int level
, const char *fmt
, ...) {
323 if (verbosity_level
>= level
) {
326 fprintf(stderr
, " WARN: ");
327 vfprintf(stderr
, fmt
, ap
);
332 void print_trace(const char *fmt
, ...) {
336 fprintf(stderr
, "TRACE: ");
337 vfprintf(stderr
, fmt
, ap
);
342 void print_error(const char *fmt
, ...) {
345 fprintf(stderr
, "ERROR: ");
346 vfprintf(stderr
, fmt
, ap
);
350 void print_info(const char *fmt
, ...) {
353 fprintf(stderr
, " INFO: ");
354 vfprintf(stderr
, fmt
, ap
);
358 /* remove directory prefix and file suffix from full path */
359 char *basename(char *path
)
362 static char base
[256];
364 /* remove prepended path and extension */
366 for (p
=path
; *p
; ++p
) {
371 for (p
=base
; *p
; ++p
) {
380 int convbdf(char *path
)
385 pf
= bdf_read_font(path
);
391 strcpy(outfile
, basename(path
));
392 strcat(outfile
, ".c");
394 ret
|= gen_c_source(pf
, outfile
);
399 strcpy(outfile
, basename(path
));
400 strcat(outfile
, ".h");
402 ret
|= gen_h_header(pf
, outfile
);
407 strcpy(outfile
, basename(path
));
408 strcat(outfile
, ".fnt");
410 ret
|= gen_fnt_file(pf
, outfile
);
417 int main(int ac
, char **av
)
421 ++av
; --ac
; /* skip av[0] */
422 getopts(&ac
, &av
); /* read command line options */
424 if (ac
< 1 || (!gen_c
&& !gen_h
&& !gen_fnt
)) {
430 if (ac
> 1 || (gen_c
&& gen_fnt
) || (gen_c
&& gen_h
) || (gen_h
&& gen_fnt
)) {
437 ret
|= convbdf(av
[0]);
444 /* free font structure */
445 void free_font(struct font
* pf
)
464 /* build incore structure from .bdf file */
465 struct font
* bdf_read_font(char *path
)
470 fp
= fopen(path
, "rb");
472 print_error("Error opening file: %s\n", path
);
476 pf
= (struct font
*)calloc(1, sizeof(struct font
));
479 memset(pf
, 0, sizeof(struct font
));
481 pf
->name
= strdup(basename(path
));
483 if (!bdf_read_header(fp
, pf
)) {
484 print_error("Error reading font header\n");
487 print_trace("Read font header, nchars_decl=%d\n", pf
->nchars_declared
);
489 if (!bdf_analyze_font(fp
, pf
)) {
490 print_error("Error analyzing the font\n");
493 print_trace("Analyzed font, nchars=%d, maxwidth=%d, asc_over=%d, desc_over=%d\n",
494 pf
->nchars
, pf
->maxwidth
, pf
->max_over_ascent
, pf
->max_over_descent
);
496 if (pf
->nchars
!= pf
->nchars_declared
) {
497 print_warning(VL_MISC
, "The declared number of chars (%d) "
498 "does not match the real number (%d)\n",
499 pf
->nchars_declared
, pf
->nchars
);
502 /* Correct ascent/descent if necessary */
503 pf
->ascent
= adjust_ascent(pf
->ascent_declared
, pf
->max_over_ascent
, &stretch_ascent
);
504 if (pf
->ascent
!= pf
->ascent_declared
) {
505 print_info("Font ascent has been changed from %d to %d\n",
506 pf
->ascent_declared
, pf
->ascent
);
508 pf
->descent
= adjust_ascent(pf
->descent
, pf
->max_over_descent
, &stretch_descent
);
509 if (pf
->descent
!= pf
->descent_declared
) {
510 print_info("Font descent has been changed from %d to %d\n",
511 pf
->descent_declared
, pf
->descent
);
513 pf
->height
= pf
->ascent
+ pf
->descent
;
514 if (pf
->height
!= pf
->ascent_declared
+ pf
->descent_declared
) {
515 print_warning(VL_CLIP_FONT
, "Generated font's height: %d\n", pf
->height
);
518 if (pf
->ascent
> pf
->max_char_ascent
) {
519 print_trace("Font's ascent could be reduced by %d to %d without clipping\n",
520 (pf
->ascent
- pf
->max_char_ascent
), pf
->max_char_ascent
);
522 if (pf
->descent
> pf
->max_char_descent
) {
523 print_trace("Font's descent could be reduced by %d to %d without clipping\n",
524 (pf
->descent
- pf
->max_char_descent
), pf
->max_char_descent
);
529 pf
->bits_size
= pf
->size
* BITMAP_WORDS(pf
->maxwidth
) * pf
->height
;
530 pf
->bits
= (bitmap_t
*)malloc(pf
->bits_size
* sizeof(bitmap_t
));
531 pf
->offset
= (int *)malloc(pf
->size
* sizeof(int));
532 pf
->offrot
= (unsigned int *)malloc(pf
->size
* sizeof(unsigned int));
533 pf
->width
= (unsigned char *)malloc(pf
->size
* sizeof(unsigned char));
535 if (!pf
->bits
|| !pf
->offset
|| !pf
->offrot
|| !pf
->width
) {
536 print_error("no memory for font load\n");
540 pf
->num_clipped_ascent
= pf
->num_clipped_descent
= pf
->num_clipped
= 0;
541 pf
->max_over_ascent
= pf
->max_over_descent
= 0;
543 if (!bdf_read_bitmaps(fp
, pf
)) {
544 print_error("Error reading font bitmaps\n");
547 print_trace("Read bitmaps\n");
549 if (pf
->num_clipped
> 0) {
550 print_warning(VL_CLIP_FONT
, "%d character(s) out of %d were clipped "
551 "(%d at ascent, %d at descent)\n",
552 pf
->num_clipped
, pf
->nchars
,
553 pf
->num_clipped_ascent
, pf
->num_clipped_descent
);
554 print_warning(VL_CLIP_FONT
, "max overflows: %d pixel(s) at ascent, %d pixel(s) at descent\n",
555 pf
->max_over_ascent
, pf
->max_over_descent
);
567 /* read bdf font header information, return 0 on error */
568 int bdf_read_header(FILE *fp
, struct font
* pf
)
571 int firstchar
= 65535;
578 /* set certain values to errors for later error checking */
579 pf
->defaultchar
= -1;
582 pf
->default_width
= -1;
585 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
586 print_error("EOF on file\n");
589 if (isprefix(buf
, "FONT ")) { /* not required */
590 if (sscanf(buf
, "FONT %[^\n]", facename
) != 1) {
591 print_error("bad 'FONT'\n");
594 pf
->facename
= strdup(facename
);
597 if (isprefix(buf
, "COPYRIGHT ")) { /* not required */
598 if (sscanf(buf
, "COPYRIGHT \"%[^\"]", copyright
) != 1) {
599 print_error("bad 'COPYRIGHT'\n");
602 pf
->copyright
= strdup(copyright
);
605 if (isprefix(buf
, "DEFAULT_CHAR ")) { /* not required */
606 if (sscanf(buf
, "DEFAULT_CHAR %d", &pf
->defaultchar
) != 1) {
607 print_error("bad 'DEFAULT_CHAR'\n");
611 if (isprefix(buf
, "FONT_DESCENT ")) {
612 if (sscanf(buf
, "FONT_DESCENT %d", &pf
->descent_declared
) != 1) {
613 print_error("bad 'FONT_DESCENT'\n");
616 pf
->descent
= pf
->descent_declared
; /* For now */
619 if (isprefix(buf
, "FONT_ASCENT ")) {
620 if (sscanf(buf
, "FONT_ASCENT %d", &pf
->ascent_declared
) != 1) {
621 print_error("bad 'FONT_ASCENT'\n");
624 pf
->ascent
= pf
->ascent_declared
; /* For now */
627 if (isprefix(buf
, "FONTBOUNDINGBOX ")) {
628 if (sscanf(buf
, "FONTBOUNDINGBOX %d %d %d %d",
629 &pf
->fbbw
, &pf
->fbbh
, &pf
->fbbx
, &pf
->fbby
) != 4) {
630 print_error("bad 'FONTBOUNDINGBOX'\n");
635 if (isprefix(buf
, "CHARS ")) {
636 if (sscanf(buf
, "CHARS %d", &pf
->nchars_declared
) != 1) {
637 print_error("bad 'CHARS'\n");
642 if (isprefix(buf
, "STARTCHAR")) {
647 /* for BDF version 2.2 */
648 if (is_header
&& isprefix(buf
, "DWIDTH ")) {
649 if (sscanf(buf
, "DWIDTH %d", &pf
->default_width
) != 1) {
650 print_error("bad 'DWIDTH' at font level\n");
657 * Reading ENCODING is necessary to get firstchar/lastchar
658 * which is needed to pre-calculate our offset and widths
661 if (isprefix(buf
, "ENCODING ")) {
662 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
663 print_error("bad 'ENCODING'\n");
667 encoding
<= limit_char
&&
668 encoding
>= start_char
) {
670 if (firstchar
> encoding
)
671 firstchar
= encoding
;
672 if (lastchar
< encoding
)
677 if (strequal(buf
, "ENDFONT"))
681 /* calc font height */
682 if (pf
->ascent
< 0 || pf
->descent
< 0 || firstchar
< 0) {
683 print_error("Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
686 pf
->height
= pf
->ascent
+ pf
->descent
;
688 /* calc default char */
689 if (pf
->defaultchar
< 0 ||
690 pf
->defaultchar
< firstchar
||
691 pf
->defaultchar
> limit_char
||
692 pf
->defaultchar
> lastchar
)
693 pf
->defaultchar
= firstchar
;
695 /* calc font size (offset/width entries) */
696 pf
->firstchar
= firstchar
;
697 pf
->size
= lastchar
- firstchar
+ 1;
703 * TODO: rework the code to avoid logics duplication in
704 * bdf_read_bitmaps and bdf_analyze_font
708 /* read bdf font bitmaps, return 0 on error */
709 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
)
713 int i
, k
, encoding
, width
;
714 int bbw
, bbh
, bbx
, bby
;
715 int proportional
= 0;
722 /* reset file pointer */
723 fseek(fp
, 0L, SEEK_SET
);
725 /* initially mark offsets as not used */
726 for (i
=0; i
<pf
->size
; ++i
)
730 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
731 print_error("EOF on file\n");
734 if (isprefix(buf
, "STARTCHAR")) {
735 encoding
= width
= -1;
742 if (isprefix(buf
, "ENCODING ")) {
743 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
744 print_error("bad 'ENCODING'\n");
747 if (encoding
< start_char
|| encoding
> limit_char
)
751 if (isprefix(buf
, "DWIDTH ")) {
752 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
753 print_error("bad 'DWIDTH'\n");
756 /* use font boundingbox width if DWIDTH <= 0 */
758 width
= pf
->fbbw
- pf
->fbbx
;
761 if (isprefix(buf
, "BBX ")) {
762 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
763 print_error("bad 'BBX'\n");
768 if (strequal(buf
, "BITMAP") || strequal(buf
, "BITMAP ")) {
769 int overflow_asc
, overflow_desc
;
770 int bbh_orig
, bby_orig
, y
;
775 if (width
< 0 && pf
->default_width
> 0)
776 width
= pf
->default_width
;
778 /* set bits offset in encode map */
779 if (pf
->offset
[encoding
-pf
->firstchar
] != -1) {
780 print_error("duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
784 pf
->offset
[encoding
-pf
->firstchar
] = ofs
;
785 pf
->offrot
[encoding
-pf
->firstchar
] = ofr
;
787 /* calc char width */
788 bdf_correct_bbx(&width
, &bbx
);
789 pf
->width
[encoding
-pf
->firstchar
] = width
;
791 ch_bitmap
= pf
->bits
+ ofs
;
792 ch_words
= BITMAP_WORDS(width
);
793 memset(ch_bitmap
, 0, BITMAP_BYTES(width
) * pf
->height
); /* clear bitmap */
795 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
796 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
801 overflow_asc
= bby
+ bbh
- pf
->ascent
;
802 if (overflow_asc
> 0) {
803 pf
->num_clipped_ascent
++;
804 if (overflow_asc
> pf
->max_over_ascent
) {
805 pf
->max_over_ascent
= overflow_asc
;
807 bbh
= MAX(bbh
- overflow_asc
, 0); /* Clipped -> decrease the height */
808 print_warning(VL_CLIP_CHAR
, "character %d goes %d pixel(s)"
809 " beyond the font's ascent, it will be clipped\n",
810 encoding
, overflow_asc
);
812 overflow_desc
= -bby
- pf
->descent
;
813 if (overflow_desc
> 0) {
814 pf
->num_clipped_descent
++;
815 if (overflow_desc
> pf
->max_over_descent
) {
816 pf
->max_over_descent
= overflow_desc
;
818 bby
+= overflow_desc
;
819 bbh
= MAX(bbh
- overflow_desc
, 0); /* Clipped -> decrease the height */
820 print_warning(VL_CLIP_CHAR
, "character %d goes %d pixel(s)"
821 " beyond the font's descent, it will be clipped\n",
822 encoding
, overflow_desc
);
824 if (overflow_asc
> 0 || overflow_desc
> 0) {
828 y
= bby_orig
+ bbh_orig
; /* 0-based y within the char */
834 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
835 print_error("EOF reading BITMAP data for character %d\n",
839 if (isprefix(buf
, "ENDCHAR"))
843 if ((y
>= pf
->ascent
) || (y
< -pf
->descent
)) {
844 /* We're beyond the area that Rockbox can render -> clip */
845 --i
; /* This line doesn't count */
849 hexnibbles
= strlen(buf
);
850 for (k
=0; k
<ch_words
; ++k
) {
851 int ndx
= k
* BITMAP_NIBBLES
;
852 int padnibbles
= hexnibbles
- ndx
;
857 if (padnibbles
>= (int)BITMAP_NIBBLES
)
860 value
= bdf_hexval((unsigned char *)buf
,
861 ndx
, ndx
+BITMAP_NIBBLES
-1-padnibbles
);
862 value
<<= padnibbles
* BITMAP_NIBBLES
;
864 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
) |=
866 /* handle overflow into next image word */
868 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
+1) =
869 value
<< (BITMAP_BITSPERIMAGE
- bbx
);
874 ofs
+= BITMAP_WORDS(width
) * pf
->height
;
875 ofr
+= pf
->width
[encoding
-pf
->firstchar
] * ((pf
->height
+7)/8);
879 if (strequal(buf
, "ENDFONT"))
883 /* change unused width values to default char values */
884 for (i
=0; i
<pf
->size
; ++i
) {
885 int defchar
= pf
->defaultchar
- pf
->firstchar
;
887 if (pf
->offset
[i
] == -1)
888 pf
->width
[i
] = pf
->width
[defchar
];
891 /* determine whether font doesn't require encode table */
894 for (i
=0; i
<pf
->size
; ++i
) {
895 if ((int)pf
->offrot
[i
] != offset
) {
899 offset
+= pf
->maxwidth
* ((pf
->height
+ 7) / 8);
903 for (i
=0; i
<pf
->size
; ++i
) {
904 if (pf
->offset
[i
] != offset
) {
908 offset
+= BITMAP_WORDS(pf
->width
[i
]) * pf
->height
;
916 /* determine whether font is fixed-width */
917 for (i
=0; i
<pf
->size
; ++i
) {
918 if (pf
->width
[i
] != pf
->maxwidth
) {
928 /* reallocate bits array to actual bits used */
929 if (ofs
< pf
->bits_size
) {
930 pf
->bits
= realloc(pf
->bits
, ofs
* sizeof(bitmap_t
));
935 pf
->bits_size
= ofr
; /* always update, rotated is smaller */
941 /* read the next non-comment line, returns buf or NULL if EOF */
942 char *bdf_getline(FILE *fp
, char *buf
, int len
)
949 while ((c
= getc(fp
)) != EOF
) {
954 if (b
- buf
>= (len
- 1))
959 if (c
== EOF
&& b
== buf
)
961 if (b
!= buf
&& !isprefix(buf
, "COMMENT"))
967 void bdf_correct_bbx(int *width
, int *bbx
) {
969 /* Rockbox can't render overlapping glyphs */
975 int bdf_analyze_font(FILE *fp
, struct font
* pf
) {
978 int width
, bbw
, bbh
, bbx
, bby
, ascent
, overflow
;
979 int read_enc
= 0, read_width
= 0, read_bbx
= 0, read_endchar
= 1;
982 /* reset file pointer */
983 fseek(fp
, 0L, SEEK_SET
);
987 pf
->max_char_ascent
= pf
->max_char_descent
= 0;
988 pf
->max_over_ascent
= pf
->max_over_descent
= 0;
992 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
993 print_error("EOF on file\n");
996 if (isprefix(buf
, "ENDFONT")) {
998 print_error("No terminating ENDCHAR for character %d\n", encoding
);
1003 if (isprefix(buf
, "STARTCHAR")) {
1004 print_trace("Read STARTCHAR, nchars=%d, read_endchar=%d\n", pf
->nchars
, read_endchar
);
1005 if (!read_endchar
) {
1006 print_error("No terminating ENDCHAR for character %d\n", encoding
);
1009 read_enc
= read_width
= read_bbx
= read_endchar
= 0;
1012 if (isprefix(buf
, "ENDCHAR")) {
1014 print_error("ENCODING is not specified\n");
1017 ignore_char
= (encoding
< start_char
|| encoding
> limit_char
);
1019 if (!read_width
&& pf
->default_width
> 0)
1021 width
= pf
->default_width
;
1024 if (!read_width
|| !read_bbx
) {
1025 print_error("WIDTH or BBX is not specified for character %d\n",
1028 bdf_correct_bbx(&width
, &bbx
);
1029 if (width
> pf
->maxwidth
) {
1030 pf
->maxwidth
= width
;
1034 pf
->max_char_ascent
= MAX(pf
->max_char_ascent
, ascent
);
1035 overflow
= ascent
- pf
->ascent
;
1036 pf
->max_over_ascent
= MAX(pf
->max_over_ascent
, overflow
);
1039 pf
->max_char_descent
= MAX(pf
->max_char_descent
, ascent
);
1040 overflow
= ascent
- pf
->descent
;
1041 pf
->max_over_descent
= MAX(pf
->max_over_descent
, overflow
);
1047 if (isprefix(buf
, "ENCODING ")) {
1048 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
1049 print_error("bad 'ENCODING': '%s'\n", buf
);
1055 if (isprefix(buf
, "DWIDTH ")) {
1056 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
1057 print_error("bad 'DWIDTH': '%s'\n", buf
);
1060 /* use font boundingbox width if DWIDTH <= 0 */
1062 print_error("Negative char width: %d\n", width
);
1067 if (isprefix(buf
, "BBX ")) {
1068 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
1069 print_error("bad 'BBX': '%s'\n", buf
);
1079 int adjust_ascent(int ascent
, int overflow
, struct stretch
*stretch
) {
1081 int px
= stretch
->value
;
1082 if (stretch
->percent
) {
1083 px
= ascent
* px
/ 100;
1086 if (stretch
->force
) {
1087 result
= ascent
+ px
;
1090 result
= ascent
+ MIN(overflow
, px
);
1092 result
= MAX(result
, 0);
1097 /* return hex value of portion of buffer */
1098 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
)
1103 for (i
=ndx1
; i
<=ndx2
; ++i
) {
1105 if (c
>= '0' && c
<= '9')
1108 if (c
>= 'A' && c
<= 'F')
1111 if (c
>= 'a' && c
<= 'f')
1115 val
= (val
<< 4) | c
;
1124 * Take an bitmap_t bitmap and convert to Rockbox format.
1125 * Used for converting font glyphs for the time being.
1126 * Can use for standard X11 and Win32 images as well.
1127 * See format description in lcd-recorder.c
1129 * Doing it this way keeps fonts in standard formats,
1130 * as well as keeping Rockbox hw bitmap format.
1132 * Returns the size of the rotated glyph (in bytes) or a
1133 * negative value if the glyph could not be rotated.
1135 int rotleft(unsigned char *dst
, /* output buffer */
1136 size_t dstlen
, /* buffer size */
1137 bitmap_t
*src
, unsigned int width
, unsigned int height
,
1141 unsigned int src_words
; /* # words of input image */
1142 unsigned int dst_mask
; /* bit mask for destination */
1143 bitmap_t src_mask
; /* bit mask for source */
1145 /* How large the buffer should be to hold the rotated bitmap
1146 of a glyph of size (width x height) */
1147 unsigned int needed_size
= ((height
+ 7) / 8) * width
;
1149 if (needed_size
> dstlen
) {
1150 print_error("Character %d: Glyph of size %d x %d can't be rotated "
1151 "(buffer size is %lu, needs %u)\n",
1152 char_code
, width
, height
, (unsigned long)dstlen
, needed_size
);
1156 /* calc words of input image */
1157 src_words
= BITMAP_WORDS(width
) * height
;
1159 /* clear background */
1160 memset(dst
, 0, needed_size
);
1164 for (i
=0; i
< src_words
; i
++) {
1166 /* calc src input bit */
1167 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
1169 /* for each input column... */
1170 for(j
=0; j
< width
; j
++) {
1172 if (src_mask
== 0) /* input word done? */
1174 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
1175 i
++; /* next input word */
1178 /* if set in input, set in rotated output */
1179 if (src
[i
] & src_mask
)
1182 src_mask
>>= 1; /* next input bit */
1185 dst_mask
<<= 1; /* next output bit (row) */
1186 if (dst_mask
> (1 << 7)) /* output bit > 7? */
1189 dst
+= width
; /* next output byte row */
1192 return needed_size
; /* return result size in bytes */
1198 /* generate C source from in-core font */
1199 int gen_c_source(struct font
* pf
, char *path
)
1207 int did_syncmsg
= 0;
1208 bitmap_t
*ofs
= pf
->bits
;
1213 "/* Generated by convbdf on %s. */\n"
1214 "#include \"font.h\"\n"
1215 "#ifdef HAVE_LCD_BITMAP\n"
1217 "/* Font information:\n"
1225 " first char: %d (0x%02x)\n"
1226 " last char: %d (0x%02x)\n"
1227 " default char: %d (0x%02x)\n"
1228 " proportional: %s\n"
1232 "/* Font character bitmap data. */\n"
1233 "static const unsigned char _font_bits[] = {\n"
1236 ofp
= fopen(path
, "w");
1238 print_error("Can't create %s\n", path
);
1242 strcpy(buf
, ctime(&t
));
1243 buf
[strlen(buf
)-1] = 0;
1245 fprintf(ofp
, hdr1
, buf
,
1247 pf
->facename
? pf
->facename
: "",
1248 pf
->maxwidth
, pf
->height
,
1250 pf
->ascent
, pf
->descent
, pf
->depth
,
1251 pf
->firstchar
, pf
->firstchar
,
1252 pf
->firstchar
+pf
->size
-1, pf
->firstchar
+pf
->size
-1,
1253 pf
->defaultchar
, pf
->defaultchar
,
1254 pf
->width
? "yes": "no",
1255 pf
->copyright
? pf
->copyright
: "");
1257 /* generate bitmaps */
1258 for (i
=0; i
<pf
->size
; ++i
) {
1261 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1262 int height
= pf
->height
;
1263 int char_code
= pf
->firstchar
+ i
;
1265 bitmap_t bitvalue
=0;
1267 /* Skip missing glyphs */
1268 if (pf
->offset
&& (pf
->offset
[i
] == -1))
1271 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1273 fprintf(ofp
, "\n/* Character %d (0x%02x):\n width %d",
1274 char_code
, char_code
, width
);
1277 fprintf(ofp
, "\n +");
1278 for (x
=0; x
<width
; ++x
) fprintf(ofp
, "-");
1279 fprintf(ofp
, "+\n");
1282 while (height
> 0) {
1283 if (x
== 0) fprintf(ofp
, " |");
1285 if (bitcount
<= 0) {
1286 bitcount
= BITMAP_BITSPERIMAGE
;
1290 fprintf(ofp
, BITMAP_TESTBIT(bitvalue
)? "*": " ");
1292 bitvalue
= BITMAP_SHIFTBIT(bitvalue
);
1295 fprintf(ofp
, "|\n");
1302 for (x
=0; x
<width
; ++x
)
1304 fprintf(ofp
, "+ */\n");
1307 fprintf(ofp
, " */\n");
1309 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1310 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
1312 unsigned char bytemap
[ROTATION_BUF_SIZE
];
1315 int size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
,
1316 pf
->height
, char_code
);
1321 for (y8
=0; y8
<pf
->height
; y8
+=8) /* column rows */
1323 for (x
=0; x
<width
; x
++) {
1324 fprintf(ofp
, "0x%02x, ", bytemap
[ix
]);
1330 /* update offrot since bits are now in sorted order */
1331 pf
->offrot
[i
] = ofr
;
1335 for (x
=BITMAP_WORDS(width
)*pf
->height
; x
>0; --x
) {
1336 fprintf(ofp
, "0x%04x,\n", *bits
);
1337 if (!did_syncmsg
&& *bits
++ != *ofs
++) {
1338 print_warning(VL_MISC
, "found encoding values in non-sorted order (not an error).\n");
1344 fprintf(ofp
, "};\n\n");
1347 /* output offset table */
1348 fprintf(ofp
, "/* Character->glyph mapping. */\n"
1349 "static const unsigned short _sysfont_offset[] = {\n");
1351 for (i
=0; i
<pf
->size
; ++i
) {
1352 int offset
= pf
->offset
[i
];
1353 int offrot
= pf
->offrot
[i
];
1355 offset
= pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1356 offrot
= pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1358 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
1360 offrot
, i
+pf
->firstchar
);
1362 offset
, i
+pf
->firstchar
);
1365 fprintf(ofp
, "};\n\n");
1368 /* output width table for proportional fonts */
1370 fprintf(ofp
, "/* Character width data. */\n"
1371 "static const unsigned char _sysfont_width[] = {\n");
1373 for (i
=0; i
<pf
->size
; ++i
)
1374 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
1375 pf
->width
[i
], i
+pf
->firstchar
);
1376 fprintf(ofp
, "};\n\n");
1379 /* output struct font struct */
1381 sprintf(obuf
, "_sysfont_offset,");
1383 sprintf(obuf
, "0, /* no encode table */");
1386 sprintf(buf
, "_sysfont_width, /* width */");
1388 sprintf(buf
, "0, /* fixed width */");
1390 fprintf(ofp
, "/* Exported structure definition. */\n"
1391 "const struct font sysfont = {\n"
1392 " %d, /* maxwidth */\n"
1393 " %d, /* height */\n"
1394 " %d, /* ascent */\n"
1395 " %d, /* firstchar */\n"
1397 " %d, /* depth */\n"
1398 " _font_bits, /* bits */\n"
1399 " %s /* offset */\n"
1401 " %d, /* defaultchar */\n"
1402 " %d, /* bits_size */\n",
1403 pf
->maxwidth
, pf
->height
,
1413 fprintf(ofp
, " -1, /* font fd */\n"
1414 " -1, /* font fd width */\n"
1415 " -1, /* font fd offset */\n"
1416 " -1, /* font handle */\n"
1417 " 0, /* buffer start */\n"
1418 " 0, /* ^ position */\n"
1420 " 0, /* ^ size */\n"
1421 " {{0,0,0,0,0},0,0,0,0,0}, /* cache */\n"
1426 "#endif /* HAVE_LCD_BITMAP */\n"
1432 /* generate C header from in-core font */
1433 int gen_h_header(struct font
* pf
, char *path
)
1439 "/* Generated by convbdf on %s. */\n"
1440 "#ifdef HAVE_LCD_BITMAP\n"
1442 "/* Font information */\n"
1443 "#define SYSFONT_NAME %s\n"
1444 "#define SYSFONT_FACENAME %s\n"
1445 "#define SYSFONT_WIDTH %d\n"
1446 "#define SYSFONT_HEIGHT %d\n"
1447 "#define SYSFONT_SIZE %d\n"
1448 "#define SYSFONT_ASCENT %d\n"
1449 "#define SYSFONT_DEPTH %d\n";
1451 "#define SYSFONT_DESCENT %d\n"
1452 "#define SYSFONT_FIRST_CHAR %d\n"
1453 "#define SYSFONT_LAST_CHAR %d\n"
1454 "#define SYSFONT_DEFAULT_CHAR %d\n"
1455 "#define SYSFONT_PROPORTIONAL %d\n"
1456 "#define SYSFONT_COPYRIGHT %s\n"
1457 "#define SYSFONT_BITS_SIZE %d\n"
1461 ofp
= fopen(path
, "w");
1463 print_error("Can't create %s\n", path
);
1467 strcpy(buf
, ctime(&t
));
1468 buf
[strlen(buf
)-1] = 0;
1470 fprintf(ofp
, hdr1
, buf
,
1472 pf
->facename
? pf
->facename
: "",
1482 pf
->firstchar
+pf
->size
-1,
1485 pf
->copyright
? pf
->copyright
: "",
1491 static int writebyte(FILE *fp
, unsigned char c
)
1493 return putc(c
, fp
) != EOF
;
1496 static int writeshort(FILE *fp
, unsigned short s
)
1499 return putc(s
>>8, fp
) != EOF
;
1502 static int writeint(FILE *fp
, unsigned int l
)
1507 return putc(l
>>24, fp
) != EOF
;
1510 static int writestr(FILE *fp
, char *str
, int count
)
1512 return (int)fwrite(str
, 1, count
, fp
) == count
;
1516 static int writestrpad(FILE *fp
, char *str
, int totlen
)
1520 while (str
&& *str
&& totlen
> 0) {
1522 ret
= putc(*str
++, fp
);
1526 while (--totlen
>= 0)
1527 ret
= putc(' ', fp
);
1532 /* generate .fnt format file from in-core font */
1533 int gen_fnt_file(struct font
* pf
, char *path
)
1541 ofp
= fopen(path
, "wb");
1543 print_error("Can't create %s\n", path
);
1547 /* write magic and version number */
1548 writestr(ofp
, VERSION
, 4);
1550 /* internal font name */
1551 writestrpad(ofp
, pf
->name
, 64);
1554 writestrpad(ofp
, pf
->copyright
, 256);
1557 writeshort(ofp
, pf
->maxwidth
);
1558 writeshort(ofp
, pf
->height
);
1559 writeshort(ofp
, pf
->ascent
);
1560 writeshort(ofp
, 0); /* depth = 0 for bdffonts */
1561 writeint(ofp
, pf
->firstchar
);
1562 writeint(ofp
, pf
->defaultchar
);
1563 writeint(ofp
, pf
->size
);
1565 /* variable font data sizes */
1566 writeint(ofp
, pf
->bits_size
); /* # words of bitmap_t */
1567 writeint(ofp
, pf
->offset
? pf
->size
: 0); /* # ints of offset */
1568 writeint(ofp
, pf
->width
? pf
->size
: 0); /* # bytes of width */
1569 /* variable font data */
1571 for (i
=0; i
<pf
->size
; ++i
)
1574 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1576 int char_code
= pf
->firstchar
+ i
;
1577 unsigned char bytemap
[ROTATION_BUF_SIZE
];
1579 /* Skip missing glyphs */
1580 if (pf
->offset
&& (pf
->offset
[i
] == -1))
1583 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1585 size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
, pf
->height
, char_code
);
1589 writestr(ofp
, (char *)bytemap
, size
);
1591 /* update offrot since bits are now in sorted order */
1592 pf
->offrot
[i
] = ofr
;
1596 if ( pf
->bits_size
< 0xFFDB )
1598 /* bitmap offset is small enough, use unsigned short for offset */
1600 writebyte(ofp
, 0); /* pad to 16-bit boundary */
1604 /* bitmap offset is large then 64K, use unsigned int for offset */
1605 while (ftell(ofp
) & 3)
1606 writebyte(ofp
, 0); /* pad to 32-bit boundary */
1611 for (i
=0; i
<pf
->size
; ++i
)
1613 int offrot
= pf
->offrot
[i
];
1614 if (pf
->offset
[i
] == -1) {
1615 offrot
= pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1617 if ( pf
->bits_size
< 0xFFDB )
1618 writeshort(ofp
, offrot
);
1620 writeint(ofp
, offrot
);
1625 for (i
=0; i
<pf
->size
; ++i
)
1626 writebyte(ofp
, pf
->width
[i
]);
1628 for (i
=0; i
<pf
->bits_size
; ++i
)
1629 writeshort(ofp
, pf
->bits
[i
]);
1631 writeshort(ofp
, 0); /* pad to 32-bit boundary */
1634 for (i
=0; i
<pf
->size
; ++i
) {
1635 int offset
= pf
->offset
[i
];
1637 offset
= pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1639 writeint(ofp
, offset
);
1643 for (i
=0; i
<pf
->size
; ++i
)
1644 writebyte(ofp
, pf
->width
[i
]);