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 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 " An ! in the -a and -d options forces the growth; N may be negative\n",
169 " -v N Verbosity level: 0=quite quiet, 1=more verbose, 2=even more, etc.\n",
170 " -t Print internal tracing messages\n",
171 NULL
/* Must be the last element in the array */
176 print_info("%s", *(p
++));
180 void parse_ascent_opt(char *val
, struct stretch
*opt
) {
187 p
= buf
+ strlen(buf
);
194 else if (*p
== '!') {
202 opt
->value
= atoi(buf
);
205 /* parse command line options */
206 void getopts(int *pac
, char ***pav
)
214 while (ac
> 0 && av
[0][0] == '-') {
218 case ' ': /* multiple -args on av[] */
219 while( *p
&& *p
== ' ')
221 if( *p
++ != '-') /* next option must have dash */
223 break; /* proceed to next option */
224 case 'c': /* generate .c output */
227 case 'h': /* generate .h output */
230 case 'f': /* generate .fnt output */
233 case 'n': /* don't gen bitmap comments */
236 case 'o': /* set output file */
240 while (*p
&& *p
!= ' ')
246 strcpy(outfile
, av
[0]);
249 case 'l': /* set encoding limit */
251 limit_char
= atoi(p
);
252 while (*p
&& *p
!= ' ')
258 limit_char
= atoi(av
[0]);
261 case 's': /* set encoding start */
263 start_char
= atoi(p
);
264 while (*p
&& *p
!= ' ')
270 start_char
= atoi(av
[0]);
273 case 'a': /* ascent growth */
275 parse_ascent_opt(p
, &stretch_ascent
);
276 while (*p
&& *p
!= ' ')
282 parse_ascent_opt(av
[0], &stretch_ascent
);
285 case 'd': /* descent growth */
287 parse_ascent_opt(p
, &stretch_descent
);
288 while (*p
&& *p
!= ' ')
294 parse_ascent_opt(av
[0], &stretch_descent
);
297 case 'v': /* verbosity */
299 verbosity_level
= atoi(p
);
300 while (*p
&& *p
!= ' ')
306 verbosity_level
= atoi(av
[0]);
309 case 't': /* tracing */
313 print_info("Unknown option ignored: %c\n", *(p
-1));
321 void print_warning(int level
, const char *fmt
, ...) {
322 if (verbosity_level
>= level
) {
325 fprintf(stderr
, " WARN: ");
326 vfprintf(stderr
, fmt
, ap
);
331 void print_trace(const char *fmt
, ...) {
335 fprintf(stderr
, "TRACE: ");
336 vfprintf(stderr
, fmt
, ap
);
341 void print_error(const char *fmt
, ...) {
344 fprintf(stderr
, "ERROR: ");
345 vfprintf(stderr
, fmt
, ap
);
349 void print_info(const char *fmt
, ...) {
352 fprintf(stderr
, " INFO: ");
353 vfprintf(stderr
, fmt
, ap
);
357 /* remove directory prefix and file suffix from full path */
358 char *basename(char *path
)
361 static char base
[256];
363 /* remove prepended path and extension */
365 for (p
=path
; *p
; ++p
) {
370 for (p
=base
; *p
; ++p
) {
379 int convbdf(char *path
)
384 pf
= bdf_read_font(path
);
390 strcpy(outfile
, basename(path
));
391 strcat(outfile
, ".c");
393 ret
|= gen_c_source(pf
, outfile
);
398 strcpy(outfile
, basename(path
));
399 strcat(outfile
, ".h");
401 ret
|= gen_h_header(pf
, outfile
);
406 strcpy(outfile
, basename(path
));
407 strcat(outfile
, ".fnt");
409 ret
|= gen_fnt_file(pf
, outfile
);
416 int main(int ac
, char **av
)
420 ++av
; --ac
; /* skip av[0] */
421 getopts(&ac
, &av
); /* read command line options */
423 if (ac
< 1 || (!gen_c
&& !gen_h
&& !gen_fnt
)) {
429 if (ac
> 1 || (gen_c
&& gen_fnt
) || (gen_c
&& gen_h
) || (gen_h
&& gen_fnt
)) {
436 ret
|= convbdf(av
[0]);
443 /* free font structure */
444 void free_font(struct font
* pf
)
463 /* build incore structure from .bdf file */
464 struct font
* bdf_read_font(char *path
)
469 fp
= fopen(path
, "rb");
471 print_error("Error opening file: %s\n", path
);
475 pf
= (struct font
*)calloc(1, sizeof(struct font
));
478 memset(pf
, 0, sizeof(struct font
));
480 pf
->name
= strdup(basename(path
));
482 if (!bdf_read_header(fp
, pf
)) {
483 print_error("Error reading font header\n");
486 print_trace("Read font header, nchars_decl=%d\n", pf
->nchars_declared
);
488 if (!bdf_analyze_font(fp
, pf
)) {
489 print_error("Error analyzing the font\n");
492 print_trace("Analyzed font, nchars=%d, maxwidth=%d, asc_over=%d, desc_over=%d\n",
493 pf
->nchars
, pf
->maxwidth
, pf
->max_over_ascent
, pf
->max_over_descent
);
495 if (pf
->nchars
!= pf
->nchars_declared
) {
496 print_warning(VL_MISC
, "The declared number of chars (%d) "
497 "does not match the real number (%d)\n",
498 pf
->nchars_declared
, pf
->nchars
);
501 /* Correct ascent/descent if necessary */
502 pf
->ascent
= adjust_ascent(pf
->ascent_declared
, pf
->max_over_ascent
, &stretch_ascent
);
503 if (pf
->ascent
!= pf
->ascent_declared
) {
504 print_info("Font ascent has been changed from %d to %d\n",
505 pf
->ascent_declared
, pf
->ascent
);
507 pf
->descent
= adjust_ascent(pf
->descent
, pf
->max_over_descent
, &stretch_descent
);
508 if (pf
->descent
!= pf
->descent_declared
) {
509 print_info("Font descent has been changed from %d to %d\n",
510 pf
->descent_declared
, pf
->descent
);
512 pf
->height
= pf
->ascent
+ pf
->descent
;
513 if (pf
->height
!= pf
->ascent_declared
+ pf
->descent_declared
) {
514 print_warning(VL_CLIP_FONT
, "Generated font's height: %d\n", pf
->height
);
517 if (pf
->ascent
> pf
->max_char_ascent
) {
518 print_trace("Font's ascent could be reduced by %d to %d without clipping\n",
519 (pf
->ascent
- pf
->max_char_ascent
), pf
->max_char_ascent
);
521 if (pf
->descent
> pf
->max_char_descent
) {
522 print_trace("Font's descent could be reduced by %d to %d without clipping\n",
523 (pf
->descent
- pf
->max_char_descent
), pf
->max_char_descent
);
528 pf
->bits_size
= pf
->size
* BITMAP_WORDS(pf
->maxwidth
) * pf
->height
;
529 pf
->bits
= (bitmap_t
*)malloc(pf
->bits_size
* sizeof(bitmap_t
));
530 pf
->offset
= (int *)malloc(pf
->size
* sizeof(int));
531 pf
->offrot
= (unsigned int *)malloc(pf
->size
* sizeof(unsigned int));
532 pf
->width
= (unsigned char *)malloc(pf
->size
* sizeof(unsigned char));
534 if (!pf
->bits
|| !pf
->offset
|| !pf
->offrot
|| !pf
->width
) {
535 print_error("no memory for font load\n");
539 pf
->num_clipped_ascent
= pf
->num_clipped_descent
= pf
->num_clipped
= 0;
540 pf
->max_over_ascent
= pf
->max_over_descent
= 0;
542 if (!bdf_read_bitmaps(fp
, pf
)) {
543 print_error("Error reading font bitmaps\n");
546 print_trace("Read bitmaps\n");
548 if (pf
->num_clipped
> 0) {
549 print_warning(VL_CLIP_FONT
, "%d character(s) out of %d were clipped "
550 "(%d at ascent, %d at descent)\n",
551 pf
->num_clipped
, pf
->nchars
,
552 pf
->num_clipped_ascent
, pf
->num_clipped_descent
);
553 print_warning(VL_CLIP_FONT
, "max overflows: %d pixel(s) at ascent, %d pixel(s) at descent\n",
554 pf
->max_over_ascent
, pf
->max_over_descent
);
566 /* read bdf font header information, return 0 on error */
567 int bdf_read_header(FILE *fp
, struct font
* pf
)
570 int firstchar
= 65535;
577 /* set certain values to errors for later error checking */
578 pf
->defaultchar
= -1;
581 pf
->default_width
= -1;
584 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
585 print_error("EOF on file\n");
588 if (isprefix(buf
, "FONT ")) { /* not required */
589 if (sscanf(buf
, "FONT %[^\n]", facename
) != 1) {
590 print_error("bad 'FONT'\n");
593 pf
->facename
= strdup(facename
);
596 if (isprefix(buf
, "COPYRIGHT ")) { /* not required */
597 if (sscanf(buf
, "COPYRIGHT \"%[^\"]", copyright
) != 1) {
598 print_error("bad 'COPYRIGHT'\n");
601 pf
->copyright
= strdup(copyright
);
604 if (isprefix(buf
, "DEFAULT_CHAR ")) { /* not required */
605 if (sscanf(buf
, "DEFAULT_CHAR %d", &pf
->defaultchar
) != 1) {
606 print_error("bad 'DEFAULT_CHAR'\n");
610 if (isprefix(buf
, "FONT_DESCENT ")) {
611 if (sscanf(buf
, "FONT_DESCENT %d", &pf
->descent_declared
) != 1) {
612 print_error("bad 'FONT_DESCENT'\n");
615 pf
->descent
= pf
->descent_declared
; /* For now */
618 if (isprefix(buf
, "FONT_ASCENT ")) {
619 if (sscanf(buf
, "FONT_ASCENT %d", &pf
->ascent_declared
) != 1) {
620 print_error("bad 'FONT_ASCENT'\n");
623 pf
->ascent
= pf
->ascent_declared
; /* For now */
626 if (isprefix(buf
, "FONTBOUNDINGBOX ")) {
627 if (sscanf(buf
, "FONTBOUNDINGBOX %d %d %d %d",
628 &pf
->fbbw
, &pf
->fbbh
, &pf
->fbbx
, &pf
->fbby
) != 4) {
629 print_error("bad 'FONTBOUNDINGBOX'\n");
634 if (isprefix(buf
, "CHARS ")) {
635 if (sscanf(buf
, "CHARS %d", &pf
->nchars_declared
) != 1) {
636 print_error("bad 'CHARS'\n");
641 if (isprefix(buf
, "STARTCHAR")) {
646 /* for BDF version 2.2 */
647 if (is_header
&& isprefix(buf
, "DWIDTH ")) {
648 if (sscanf(buf
, "DWIDTH %d", &pf
->default_width
) != 1) {
649 print_error("bad 'DWIDTH' at font level\n");
656 * Reading ENCODING is necessary to get firstchar/lastchar
657 * which is needed to pre-calculate our offset and widths
660 if (isprefix(buf
, "ENCODING ")) {
661 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
662 print_error("bad 'ENCODING'\n");
666 encoding
<= limit_char
&&
667 encoding
>= start_char
) {
669 if (firstchar
> encoding
)
670 firstchar
= encoding
;
671 if (lastchar
< encoding
)
676 if (strequal(buf
, "ENDFONT"))
680 /* calc font height */
681 if (pf
->ascent
< 0 || pf
->descent
< 0 || firstchar
< 0) {
682 print_error("Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
685 pf
->height
= pf
->ascent
+ pf
->descent
;
687 /* calc default char */
688 if (pf
->defaultchar
< 0 ||
689 pf
->defaultchar
< firstchar
||
690 pf
->defaultchar
> limit_char
||
691 pf
->defaultchar
> lastchar
)
692 pf
->defaultchar
= firstchar
;
694 /* calc font size (offset/width entries) */
695 pf
->firstchar
= firstchar
;
696 pf
->size
= lastchar
- firstchar
+ 1;
702 * TODO: rework the code to avoid logics duplication in
703 * bdf_read_bitmaps and bdf_analyze_font
707 /* read bdf font bitmaps, return 0 on error */
708 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
)
712 int i
, k
, encoding
, width
;
713 int bbw
, bbh
, bbx
, bby
;
714 int proportional
= 0;
721 /* reset file pointer */
722 fseek(fp
, 0L, SEEK_SET
);
724 /* initially mark offsets as not used */
725 for (i
=0; i
<pf
->size
; ++i
)
729 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
730 print_error("EOF on file\n");
733 if (isprefix(buf
, "STARTCHAR")) {
734 encoding
= width
= -1;
741 if (isprefix(buf
, "ENCODING ")) {
742 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
743 print_error("bad 'ENCODING'\n");
746 if (encoding
< start_char
|| encoding
> limit_char
)
750 if (isprefix(buf
, "DWIDTH ")) {
751 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
752 print_error("bad 'DWIDTH'\n");
755 /* use font boundingbox width if DWIDTH <= 0 */
757 width
= pf
->fbbw
- pf
->fbbx
;
760 if (isprefix(buf
, "BBX ")) {
761 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
762 print_error("bad 'BBX'\n");
767 if (strequal(buf
, "BITMAP") || strequal(buf
, "BITMAP ")) {
768 int overflow_asc
, overflow_desc
;
769 int bbh_orig
, bby_orig
, y
;
774 if (width
< 0 && pf
->default_width
> 0)
775 width
= pf
->default_width
;
777 /* set bits offset in encode map */
778 if (pf
->offset
[encoding
-pf
->firstchar
] != -1) {
779 print_error("duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
783 pf
->offset
[encoding
-pf
->firstchar
] = ofs
;
784 pf
->offrot
[encoding
-pf
->firstchar
] = ofr
;
786 /* calc char width */
787 bdf_correct_bbx(&width
, &bbx
);
788 pf
->width
[encoding
-pf
->firstchar
] = width
;
790 ch_bitmap
= pf
->bits
+ ofs
;
791 ch_words
= BITMAP_WORDS(width
);
792 memset(ch_bitmap
, 0, BITMAP_BYTES(width
) * pf
->height
); /* clear bitmap */
794 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
795 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
800 overflow_asc
= bby
+ bbh
- pf
->ascent
;
801 if (overflow_asc
> 0) {
802 pf
->num_clipped_ascent
++;
803 if (overflow_asc
> pf
->max_over_ascent
) {
804 pf
->max_over_ascent
= overflow_asc
;
806 bbh
= MAX(bbh
- overflow_asc
, 0); /* Clipped -> decrease the height */
807 print_warning(VL_CLIP_CHAR
, "character %d goes %d pixel(s)"
808 " beyond the font's ascent, it will be clipped\n",
809 encoding
, overflow_asc
);
811 overflow_desc
= -bby
- pf
->descent
;
812 if (overflow_desc
> 0) {
813 pf
->num_clipped_descent
++;
814 if (overflow_desc
> pf
->max_over_descent
) {
815 pf
->max_over_descent
= overflow_desc
;
817 bby
+= overflow_desc
;
818 bbh
= MAX(bbh
- overflow_desc
, 0); /* Clipped -> decrease the height */
819 print_warning(VL_CLIP_CHAR
, "character %d goes %d pixel(s)"
820 " beyond the font's descent, it will be clipped\n",
821 encoding
, overflow_desc
);
823 if (overflow_asc
> 0 || overflow_desc
> 0) {
827 y
= bby_orig
+ bbh_orig
; /* 0-based y within the char */
833 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
834 print_error("EOF reading BITMAP data for character %d\n",
838 if (isprefix(buf
, "ENDCHAR"))
842 if ((y
>= pf
->ascent
) || (y
< -pf
->descent
)) {
843 /* We're beyond the area that Rockbox can render -> clip */
844 --i
; /* This line doesn't count */
848 hexnibbles
= strlen(buf
);
849 for (k
=0; k
<ch_words
; ++k
) {
850 int ndx
= k
* BITMAP_NIBBLES
;
851 int padnibbles
= hexnibbles
- ndx
;
856 if (padnibbles
>= (int)BITMAP_NIBBLES
)
859 value
= bdf_hexval((unsigned char *)buf
,
860 ndx
, ndx
+BITMAP_NIBBLES
-1-padnibbles
);
861 value
<<= padnibbles
* BITMAP_NIBBLES
;
863 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
) |=
865 /* handle overflow into next image word */
867 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
+1) =
868 value
<< (BITMAP_BITSPERIMAGE
- bbx
);
873 ofs
+= BITMAP_WORDS(width
) * pf
->height
;
874 ofr
+= pf
->width
[encoding
-pf
->firstchar
] * ((pf
->height
+7)/8);
878 if (strequal(buf
, "ENDFONT"))
882 /* change unused width values to default char values */
883 for (i
=0; i
<pf
->size
; ++i
) {
884 int defchar
= pf
->defaultchar
- pf
->firstchar
;
886 if (pf
->offset
[i
] == -1)
887 pf
->width
[i
] = pf
->width
[defchar
];
890 /* determine whether font doesn't require encode table */
893 for (i
=0; i
<pf
->size
; ++i
) {
894 if ((int)pf
->offrot
[i
] != offset
) {
898 offset
+= pf
->maxwidth
* ((pf
->height
+ 7) / 8);
902 for (i
=0; i
<pf
->size
; ++i
) {
903 if (pf
->offset
[i
] != offset
) {
907 offset
+= BITMAP_WORDS(pf
->width
[i
]) * pf
->height
;
915 /* determine whether font is fixed-width */
916 for (i
=0; i
<pf
->size
; ++i
) {
917 if (pf
->width
[i
] != pf
->maxwidth
) {
927 /* reallocate bits array to actual bits used */
928 if (ofs
< pf
->bits_size
) {
929 pf
->bits
= realloc(pf
->bits
, ofs
* sizeof(bitmap_t
));
934 pf
->bits_size
= ofr
; /* always update, rotated is smaller */
940 /* read the next non-comment line, returns buf or NULL if EOF */
941 char *bdf_getline(FILE *fp
, char *buf
, int len
)
948 while ((c
= getc(fp
)) != EOF
) {
953 if (b
- buf
>= (len
- 1))
958 if (c
== EOF
&& b
== buf
)
960 if (b
!= buf
&& !isprefix(buf
, "COMMENT"))
966 void bdf_correct_bbx(int *width
, int *bbx
) {
968 /* Rockbox can't render overlapping glyphs */
974 int bdf_analyze_font(FILE *fp
, struct font
* pf
) {
977 int width
, bbw
, bbh
, bbx
, bby
, ascent
, overflow
;
978 int read_enc
= 0, read_width
= 0, read_bbx
= 0, read_endchar
= 1;
981 /* reset file pointer */
982 fseek(fp
, 0L, SEEK_SET
);
986 pf
->max_char_ascent
= pf
->max_char_descent
= 0;
987 pf
->max_over_ascent
= pf
->max_over_descent
= 0;
991 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
992 print_error("EOF on file\n");
995 if (isprefix(buf
, "ENDFONT")) {
997 print_error("No terminating ENDCHAR for character %d\n", encoding
);
1002 if (isprefix(buf
, "STARTCHAR")) {
1003 print_trace("Read STARTCHAR, nchars=%d, read_endchar=%d\n", pf
->nchars
, read_endchar
);
1004 if (!read_endchar
) {
1005 print_error("No terminating ENDCHAR for character %d\n", encoding
);
1008 read_enc
= read_width
= read_bbx
= read_endchar
= 0;
1011 if (isprefix(buf
, "ENDCHAR")) {
1013 print_error("ENCODING is not specified\n");
1016 ignore_char
= (encoding
< start_char
|| encoding
> limit_char
);
1018 if (!read_width
&& pf
->default_width
> 0)
1020 width
= pf
->default_width
;
1023 if (!read_width
|| !read_bbx
) {
1024 print_error("WIDTH or BBX is not specified for character %d\n",
1027 bdf_correct_bbx(&width
, &bbx
);
1028 if (width
> pf
->maxwidth
) {
1029 pf
->maxwidth
= width
;
1033 pf
->max_char_ascent
= MAX(pf
->max_char_ascent
, ascent
);
1034 overflow
= ascent
- pf
->ascent
;
1035 pf
->max_over_ascent
= MAX(pf
->max_over_ascent
, overflow
);
1038 pf
->max_char_descent
= MAX(pf
->max_char_descent
, ascent
);
1039 overflow
= ascent
- pf
->descent
;
1040 pf
->max_over_descent
= MAX(pf
->max_over_descent
, overflow
);
1046 if (isprefix(buf
, "ENCODING ")) {
1047 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
1048 print_error("bad 'ENCODING': '%s'\n", buf
);
1054 if (isprefix(buf
, "DWIDTH ")) {
1055 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
1056 print_error("bad 'DWIDTH': '%s'\n", buf
);
1059 /* use font boundingbox width if DWIDTH <= 0 */
1061 print_error("Negative char width: %d\n", width
);
1066 if (isprefix(buf
, "BBX ")) {
1067 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
1068 print_error("bad 'BBX': '%s'\n", buf
);
1078 int adjust_ascent(int ascent
, int overflow
, struct stretch
*stretch
) {
1080 int px
= stretch
->value
;
1081 if (stretch
->percent
) {
1082 px
= ascent
* px
/ 100;
1085 if (stretch
->force
) {
1086 result
= ascent
+ px
;
1089 result
= ascent
+ MIN(overflow
, px
);
1091 result
= MAX(result
, 0);
1096 /* return hex value of portion of buffer */
1097 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
)
1102 for (i
=ndx1
; i
<=ndx2
; ++i
) {
1104 if (c
>= '0' && c
<= '9')
1107 if (c
>= 'A' && c
<= 'F')
1110 if (c
>= 'a' && c
<= 'f')
1114 val
= (val
<< 4) | c
;
1123 * Take an bitmap_t bitmap and convert to Rockbox format.
1124 * Used for converting font glyphs for the time being.
1125 * Can use for standard X11 and Win32 images as well.
1126 * See format description in lcd-recorder.c
1128 * Doing it this way keeps fonts in standard formats,
1129 * as well as keeping Rockbox hw bitmap format.
1131 * Returns the size of the rotated glyph (in bytes) or a
1132 * negative value if the glyph could not be rotated.
1134 int rotleft(unsigned char *dst
, /* output buffer */
1135 size_t dstlen
, /* buffer size */
1136 bitmap_t
*src
, unsigned int width
, unsigned int height
,
1140 unsigned int src_words
; /* # words of input image */
1141 unsigned int dst_mask
; /* bit mask for destination */
1142 bitmap_t src_mask
; /* bit mask for source */
1144 /* How large the buffer should be to hold the rotated bitmap
1145 of a glyph of size (width x height) */
1146 unsigned int needed_size
= ((height
+ 7) / 8) * width
;
1148 if (needed_size
> dstlen
) {
1149 print_error("Character %d: Glyph of size %d x %d can't be rotated "
1150 "(buffer size is %lu, needs %u)\n",
1151 char_code
, width
, height
, (unsigned long)dstlen
, needed_size
);
1155 /* calc words of input image */
1156 src_words
= BITMAP_WORDS(width
) * height
;
1158 /* clear background */
1159 memset(dst
, 0, needed_size
);
1163 for (i
=0; i
< src_words
; i
++) {
1165 /* calc src input bit */
1166 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
1168 /* for each input column... */
1169 for(j
=0; j
< width
; j
++) {
1171 if (src_mask
== 0) /* input word done? */
1173 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
1174 i
++; /* next input word */
1177 /* if set in input, set in rotated output */
1178 if (src
[i
] & src_mask
)
1181 src_mask
>>= 1; /* next input bit */
1184 dst_mask
<<= 1; /* next output bit (row) */
1185 if (dst_mask
> (1 << 7)) /* output bit > 7? */
1188 dst
+= width
; /* next output byte row */
1191 return needed_size
; /* return result size in bytes */
1197 /* generate C source from in-core font */
1198 int gen_c_source(struct font
* pf
, char *path
)
1206 int did_syncmsg
= 0;
1207 bitmap_t
*ofs
= pf
->bits
;
1212 "/* Generated by convbdf on %s. */\n"
1213 "#include \"font.h\"\n"
1214 "#ifdef HAVE_LCD_BITMAP\n"
1216 "/* Font information:\n"
1223 " first char: %d (0x%02x)\n"
1224 " last char: %d (0x%02x)\n"
1225 " default char: %d (0x%02x)\n"
1226 " proportional: %s\n"
1230 "/* Font character bitmap data. */\n"
1231 "static const unsigned char _font_bits[] = {\n"
1234 ofp
= fopen(path
, "w");
1236 print_error("Can't create %s\n", path
);
1240 strcpy(buf
, ctime(&t
));
1241 buf
[strlen(buf
)-1] = 0;
1243 fprintf(ofp
, hdr1
, buf
,
1245 pf
->facename
? pf
->facename
: "",
1246 pf
->maxwidth
, pf
->height
,
1248 pf
->ascent
, pf
->descent
,
1249 pf
->firstchar
, pf
->firstchar
,
1250 pf
->firstchar
+pf
->size
-1, pf
->firstchar
+pf
->size
-1,
1251 pf
->defaultchar
, pf
->defaultchar
,
1252 pf
->width
? "yes": "no",
1253 pf
->copyright
? pf
->copyright
: "");
1255 /* generate bitmaps */
1256 for (i
=0; i
<pf
->size
; ++i
) {
1259 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1260 int height
= pf
->height
;
1261 int char_code
= pf
->firstchar
+ i
;
1263 bitmap_t bitvalue
=0;
1265 /* Skip missing glyphs */
1266 if (pf
->offset
&& (pf
->offset
[i
] == -1))
1269 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1271 fprintf(ofp
, "\n/* Character %d (0x%02x):\n width %d",
1272 char_code
, char_code
, width
);
1275 fprintf(ofp
, "\n +");
1276 for (x
=0; x
<width
; ++x
) fprintf(ofp
, "-");
1277 fprintf(ofp
, "+\n");
1280 while (height
> 0) {
1281 if (x
== 0) fprintf(ofp
, " |");
1283 if (bitcount
<= 0) {
1284 bitcount
= BITMAP_BITSPERIMAGE
;
1288 fprintf(ofp
, BITMAP_TESTBIT(bitvalue
)? "*": " ");
1290 bitvalue
= BITMAP_SHIFTBIT(bitvalue
);
1293 fprintf(ofp
, "|\n");
1300 for (x
=0; x
<width
; ++x
)
1302 fprintf(ofp
, "+ */\n");
1305 fprintf(ofp
, " */\n");
1307 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1308 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
1310 unsigned char bytemap
[ROTATION_BUF_SIZE
];
1313 int size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
,
1314 pf
->height
, char_code
);
1319 for (y8
=0; y8
<pf
->height
; y8
+=8) /* column rows */
1321 for (x
=0; x
<width
; x
++) {
1322 fprintf(ofp
, "0x%02x, ", bytemap
[ix
]);
1328 /* update offrot since bits are now in sorted order */
1329 pf
->offrot
[i
] = ofr
;
1333 for (x
=BITMAP_WORDS(width
)*pf
->height
; x
>0; --x
) {
1334 fprintf(ofp
, "0x%04x,\n", *bits
);
1335 if (!did_syncmsg
&& *bits
++ != *ofs
++) {
1336 print_warning(VL_MISC
, "found encoding values in non-sorted order (not an error).\n");
1342 fprintf(ofp
, "};\n\n");
1345 /* output offset table */
1346 fprintf(ofp
, "/* Character->glyph mapping. */\n"
1347 "static const unsigned short _sysfont_offset[] = {\n");
1349 for (i
=0; i
<pf
->size
; ++i
) {
1350 int offset
= pf
->offset
[i
];
1351 int offrot
= pf
->offrot
[i
];
1353 offset
= pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1354 offrot
= pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1356 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
1358 offrot
, i
+pf
->firstchar
);
1360 offset
, i
+pf
->firstchar
);
1363 fprintf(ofp
, "};\n\n");
1366 /* output width table for proportional fonts */
1368 fprintf(ofp
, "/* Character width data. */\n"
1369 "static const unsigned char _sysfont_width[] = {\n");
1371 for (i
=0; i
<pf
->size
; ++i
)
1372 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
1373 pf
->width
[i
], i
+pf
->firstchar
);
1374 fprintf(ofp
, "};\n\n");
1377 /* output struct font struct */
1379 sprintf(obuf
, "_sysfont_offset,");
1381 sprintf(obuf
, "0, /* no encode table */");
1384 sprintf(buf
, "_sysfont_width, /* width */");
1386 sprintf(buf
, "0, /* fixed width */");
1388 fprintf(ofp
, "/* Exported structure definition. */\n"
1389 "const struct font sysfont = {\n"
1390 " %d, /* maxwidth */\n"
1391 " %d, /* height */\n"
1392 " %d, /* ascent */\n"
1393 " %d, /* firstchar */\n"
1395 " _font_bits, /* bits */\n"
1396 " %s /* offset */\n"
1398 " %d, /* defaultchar */\n"
1399 " %d, /* bits_size */\n"
1400 " -1, /* font fd */\n"
1401 " 0, /* buffer start */\n"
1402 " 0, /* ^ position */\n"
1404 " 0, /* ^ size */\n"
1405 " {{0,0,0,0,0},0,0,0}, /* cache */\n"
1410 "#endif /* HAVE_LCD_BITMAP */\n",
1411 pf
->maxwidth
, pf
->height
,
1423 /* generate C header from in-core font */
1424 int gen_h_header(struct font
* pf
, char *path
)
1430 "/* Generated by convbdf on %s. */\n"
1431 "#ifdef HAVE_LCD_BITMAP\n"
1433 "/* Font information */\n"
1434 "#define SYSFONT_NAME %s\n"
1435 "#define SYSFONT_FACENAME %s\n"
1436 "#define SYSFONT_WIDTH %d\n"
1437 "#define SYSFONT_HEIGHT %d\n"
1438 "#define SYSFONT_SIZE %d\n"
1439 "#define SYSFONT_ASCENT %d\n";
1441 "#define SYSFONT_DESCENT %d\n"
1442 "#define SYSFONT_FIRST_CHAR %d\n"
1443 "#define SYSFONT_LAST_CHAR %d\n"
1444 "#define SYSFONT_DEFAULT_CHAR %d\n"
1445 "#define SYSFONT_PROPORTIONAL %d\n"
1446 "#define SYSFONT_COPYRIGHT %s\n"
1447 "#define SYSFONT_BITS_SIZE %d\n"
1451 ofp
= fopen(path
, "w");
1453 print_error("Can't create %s\n", path
);
1457 strcpy(buf
, ctime(&t
));
1458 buf
[strlen(buf
)-1] = 0;
1460 fprintf(ofp
, hdr1
, buf
,
1462 pf
->facename
? pf
->facename
: "",
1471 pf
->firstchar
+pf
->size
-1,
1474 pf
->copyright
? pf
->copyright
: "",
1480 static int writebyte(FILE *fp
, unsigned char c
)
1482 return putc(c
, fp
) != EOF
;
1485 static int writeshort(FILE *fp
, unsigned short s
)
1488 return putc(s
>>8, fp
) != EOF
;
1491 static int writeint(FILE *fp
, unsigned int l
)
1496 return putc(l
>>24, fp
) != EOF
;
1499 static int writestr(FILE *fp
, char *str
, int count
)
1501 return (int)fwrite(str
, 1, count
, fp
) == count
;
1505 static int writestrpad(FILE *fp
, char *str
, int totlen
)
1509 while (str
&& *str
&& totlen
> 0) {
1511 ret
= putc(*str
++, fp
);
1515 while (--totlen
>= 0)
1516 ret
= putc(' ', fp
);
1521 /* generate .fnt format file from in-core font */
1522 int gen_fnt_file(struct font
* pf
, char *path
)
1530 ofp
= fopen(path
, "wb");
1532 print_error("Can't create %s\n", path
);
1536 /* write magic and version number */
1537 writestr(ofp
, VERSION
, 4);
1539 /* internal font name */
1540 writestrpad(ofp
, pf
->name
, 64);
1543 writestrpad(ofp
, pf
->copyright
, 256);
1546 writeshort(ofp
, pf
->maxwidth
);
1547 writeshort(ofp
, pf
->height
);
1548 writeshort(ofp
, pf
->ascent
);
1550 writeint(ofp
, pf
->firstchar
);
1551 writeint(ofp
, pf
->defaultchar
);
1552 writeint(ofp
, pf
->size
);
1554 /* variable font data sizes */
1555 writeint(ofp
, pf
->bits_size
); /* # words of bitmap_t */
1556 writeint(ofp
, pf
->offset
? pf
->size
: 0); /* # ints of offset */
1557 writeint(ofp
, pf
->width
? pf
->size
: 0); /* # bytes of width */
1558 /* variable font data */
1560 for (i
=0; i
<pf
->size
; ++i
)
1563 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1565 int char_code
= pf
->firstchar
+ i
;
1566 unsigned char bytemap
[ROTATION_BUF_SIZE
];
1568 /* Skip missing glyphs */
1569 if (pf
->offset
&& (pf
->offset
[i
] == -1))
1572 bits
= pf
->bits
+ (pf
->offset
? (int)pf
->offset
[i
]: (pf
->height
* i
));
1574 size
= rotleft(bytemap
, sizeof(bytemap
), bits
, width
, pf
->height
, char_code
);
1578 writestr(ofp
, (char *)bytemap
, size
);
1580 /* update offrot since bits are now in sorted order */
1581 pf
->offrot
[i
] = ofr
;
1585 if ( pf
->bits_size
< 0xFFDB )
1587 /* bitmap offset is small enough, use unsigned short for offset */
1589 writebyte(ofp
, 0); /* pad to 16-bit boundary */
1593 /* bitmap offset is large then 64K, use unsigned int for offset */
1594 while (ftell(ofp
) & 3)
1595 writebyte(ofp
, 0); /* pad to 32-bit boundary */
1600 for (i
=0; i
<pf
->size
; ++i
)
1602 int offrot
= pf
->offrot
[i
];
1603 if (pf
->offset
[i
] == -1) {
1604 offrot
= pf
->offrot
[pf
->defaultchar
- pf
->firstchar
];
1606 if ( pf
->bits_size
< 0xFFDB )
1607 writeshort(ofp
, offrot
);
1609 writeint(ofp
, offrot
);
1614 for (i
=0; i
<pf
->size
; ++i
)
1615 writebyte(ofp
, pf
->width
[i
]);
1617 for (i
=0; i
<pf
->bits_size
; ++i
)
1618 writeshort(ofp
, pf
->bits
[i
]);
1620 writeshort(ofp
, 0); /* pad to 32-bit boundary */
1623 for (i
=0; i
<pf
->size
; ++i
) {
1624 int offset
= pf
->offset
[i
];
1626 offset
= pf
->offset
[pf
->defaultchar
- pf
->firstchar
];
1628 writeint(ofp
, offset
);
1632 for (i
=0; i
<pf
->size
; ++i
)
1633 writebyte(ofp
, pf
->width
[i
]);