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...
15 #define ROTATE /* define this for the new, rotated format */
18 /* loadable font magic and version #*/
20 #define VERSION "RB12" /* newer version */
22 #define VERSION "RB11"
25 /* bitmap_t helper macros*/
26 #define BITMAP_WORDS(x) (((x)+15)/16) /* image size in words*/
27 #define BITMAP_BYTES(x) (BITMAP_WORDS(x)*sizeof(bitmap_t))
28 #define BITMAP_BITSPERIMAGE (sizeof(bitmap_t) * 8)
29 #define BITMAP_BITVALUE(n) ((bitmap_t) (((bitmap_t) 1) << (n)))
30 #define BITMAP_FIRSTBIT (BITMAP_BITVALUE(BITMAP_BITSPERIMAGE - 1))
31 #define BITMAP_TESTBIT(m) ((m) & BITMAP_FIRSTBIT)
32 #define BITMAP_SHIFTBIT(m) ((bitmap_t) ((m) << 1))
34 typedef unsigned short bitmap_t
; /* bitmap image unit size*/
36 /* builtin C-based proportional/fixed font structure */
37 /* based on The Microwindows Project http://microwindows.org */
39 int maxwidth
; /* max width in pixels*/
40 int height
; /* height in pixels*/
41 int ascent
; /* ascent (baseline) height*/
42 int firstchar
; /* first character in bitmap*/
43 int size
; /* font size in glyphs*/
44 bitmap_t
* bits
; /* 16-bit right-padded bitmap data*/
45 unsigned int* offset
; /* offsets into bitmap data*/
46 unsigned char* width
; /* character widths or NULL if fixed*/
47 int defaultchar
; /* default char (not glyph index)*/
48 int bits_size
; /* # words of bitmap_t bits*/
50 /* unused by runtime system, read in by convbdf*/
51 unsigned int* offrot
; /* offsets into rotated bitmap data*/
52 char * name
; /* font name*/
53 char * facename
; /* facename of font*/
54 char * copyright
; /* copyright info for loadable fonts*/
57 int fbbw
, fbbh
, fbbx
, fbby
;
61 #define isprefix(buf,str) (!strncmp(buf, str, strlen(str)))
62 #define strequal(s1,s2) (!strcmp(s1, s2))
64 #define EXTRA 300 /* # bytes extra allocation for buggy .bdf files*/
70 int limit_char
= 65535;
75 void getopts(int *pac
, char ***pav
);
76 int convbdf(char *path
);
78 void free_font(struct font
* pf
);
79 struct font
* bdf_read_font(char *path
);
80 int bdf_read_header(FILE *fp
, struct font
* pf
);
81 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
);
82 char * bdf_getline(FILE *fp
, char *buf
, int len
);
83 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
);
85 int gen_c_source(struct font
* pf
, char *path
);
86 int gen_fnt_file(struct font
* pf
, char *path
);
92 "Usage: convbdf [options] [input-files]\n"
93 " convbdf [options] [-o output-file] [single-input-file]\n"
95 " -c Convert .bdf to .c source file\n"
96 " -f Convert .bdf to .fnt font file\n"
97 " -s N Start output at character encodings >= N\n"
98 " -l N Limit output to character encodings <= N\n"
99 " -n Don't generate bitmaps as comments in .c file\n"
102 fprintf(stderr
, help
);
105 /* parse command line options*/
106 void getopts(int *pac
, char ***pav
)
114 while (ac
> 0 && av
[0][0] == '-') {
118 case ' ': /* multiple -args on av[]*/
119 while( *p
&& *p
== ' ')
121 if( *p
++ != '-') /* next option must have dash*/
123 break; /* proceed to next option*/
124 case 'c': /* generate .c output*/
127 case 'f': /* generate .fnt output*/
130 case 'n': /* don't gen bitmap comments*/
133 case 'o': /* set output file*/
137 while (*p
&& *p
!= ' ')
143 strcpy(outfile
, av
[0]);
146 case 'l': /* set encoding limit*/
148 limit_char
= atoi(p
);
149 while (*p
&& *p
!= ' ')
155 limit_char
= atoi(av
[0]);
158 case 's': /* set encoding start*/
160 start_char
= atoi(p
);
161 while (*p
&& *p
!= ' ')
167 start_char
= atoi(av
[0]);
171 fprintf(stderr
, "Unknown option ignored: %c\r\n", *(p
-1));
179 /* remove directory prefix and file suffix from full path*/
180 char *basename(char *path
)
183 static char base
[256];
185 /* remove prepended path and extension*/
187 for (p
=path
; *p
; ++p
) {
192 for (p
=base
; *p
; ++p
) {
201 int convbdf(char *path
)
206 pf
= bdf_read_font(path
);
212 strcpy(outfile
, basename(path
));
213 strcat(outfile
, ".c");
215 ret
|= gen_c_source(pf
, outfile
);
220 strcpy(outfile
, basename(path
));
221 strcat(outfile
, ".fnt");
223 ret
|= gen_fnt_file(pf
, outfile
);
230 int main(int ac
, char **av
)
234 ++av
; --ac
; /* skip av[0]*/
235 getopts(&ac
, &av
); /* read command line options*/
237 if (ac
< 1 || (!gen_c
&& !gen_fnt
)) {
242 if (ac
> 1 || (gen_c
&& gen_fnt
)) {
249 ret
|= convbdf(av
[0]);
256 /* free font structure*/
257 void free_font(struct font
* pf
)
276 /* build incore structure from .bdf file*/
277 struct font
* bdf_read_font(char *path
)
282 fp
= fopen(path
, "rb");
284 fprintf(stderr
, "Error opening file: %s\n", path
);
288 pf
= (struct font
*)calloc(1, sizeof(struct font
));
292 pf
->name
= strdup(basename(path
));
294 if (!bdf_read_header(fp
, pf
)) {
295 fprintf(stderr
, "Error reading font header\n");
299 if (!bdf_read_bitmaps(fp
, pf
)) {
300 fprintf(stderr
, "Error reading font bitmaps\n");
313 /* read bdf font header information, return 0 on error*/
314 int bdf_read_header(FILE *fp
, struct font
* pf
)
317 int nchars
, maxwidth
;
318 int firstchar
= 65535;
324 /* set certain values to errors for later error checking*/
325 pf
->defaultchar
= -1;
330 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
331 fprintf(stderr
, "Error: EOF on file\n");
334 if (isprefix(buf
, "FONT ")) { /* not required*/
335 if (sscanf(buf
, "FONT %[^\n]", facename
) != 1) {
336 fprintf(stderr
, "Error: bad 'FONT'\n");
339 pf
->facename
= strdup(facename
);
342 if (isprefix(buf
, "COPYRIGHT ")) { /* not required*/
343 if (sscanf(buf
, "COPYRIGHT \"%[^\"]", copyright
) != 1) {
344 fprintf(stderr
, "Error: bad 'COPYRIGHT'\n");
347 pf
->copyright
= strdup(copyright
);
350 if (isprefix(buf
, "DEFAULT_CHAR ")) { /* not required*/
351 if (sscanf(buf
, "DEFAULT_CHAR %d", &pf
->defaultchar
) != 1) {
352 fprintf(stderr
, "Error: bad 'DEFAULT_CHAR'\n");
356 if (isprefix(buf
, "FONT_DESCENT ")) {
357 if (sscanf(buf
, "FONT_DESCENT %d", &pf
->descent
) != 1) {
358 fprintf(stderr
, "Error: bad 'FONT_DESCENT'\n");
363 if (isprefix(buf
, "FONT_ASCENT ")) {
364 if (sscanf(buf
, "FONT_ASCENT %d", &pf
->ascent
) != 1) {
365 fprintf(stderr
, "Error: bad 'FONT_ASCENT'\n");
370 if (isprefix(buf
, "FONTBOUNDINGBOX ")) {
371 if (sscanf(buf
, "FONTBOUNDINGBOX %d %d %d %d",
372 &pf
->fbbw
, &pf
->fbbh
, &pf
->fbbx
, &pf
->fbby
) != 4) {
373 fprintf(stderr
, "Error: bad 'FONTBOUNDINGBOX'\n");
378 if (isprefix(buf
, "CHARS ")) {
379 if (sscanf(buf
, "CHARS %d", &nchars
) != 1) {
380 fprintf(stderr
, "Error: bad 'CHARS'\n");
387 * Reading ENCODING is necessary to get firstchar/lastchar
388 * which is needed to pre-calculate our offset and widths
391 if (isprefix(buf
, "ENCODING ")) {
392 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
393 fprintf(stderr
, "Error: bad 'ENCODING'\n");
397 encoding
<= limit_char
&&
398 encoding
>= start_char
) {
400 if (firstchar
> encoding
)
401 firstchar
= encoding
;
402 if (lastchar
< encoding
)
407 if (strequal(buf
, "ENDFONT"))
411 /* calc font height*/
412 if (pf
->ascent
< 0 || pf
->descent
< 0 || firstchar
< 0) {
413 fprintf(stderr
, "Error: Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
416 pf
->height
= pf
->ascent
+ pf
->descent
;
418 /* calc default char*/
419 if (pf
->defaultchar
< 0 ||
420 pf
->defaultchar
< firstchar
||
421 pf
->defaultchar
> limit_char
||
422 pf
->defaultchar
> lastchar
)
423 pf
->defaultchar
= firstchar
;
425 /* calc font size (offset/width entries)*/
426 pf
->firstchar
= firstchar
;
427 pf
->size
= lastchar
- firstchar
+ 1;
429 /* use the font boundingbox to get initial maxwidth*/
430 /*maxwidth = pf->fbbw - pf->fbbx;*/
433 /* initially use font maxwidth * height for bits allocation*/
434 pf
->bits_size
= nchars
* BITMAP_WORDS(maxwidth
) * pf
->height
;
436 /* allocate bits, offset, and width arrays*/
437 pf
->bits
= (bitmap_t
*)malloc(pf
->bits_size
* sizeof(bitmap_t
) + EXTRA
);
438 pf
->offset
= (unsigned int *)malloc(pf
->size
* sizeof(unsigned int));
439 pf
->offrot
= (unsigned int *)malloc(pf
->size
* sizeof(unsigned int));
440 pf
->width
= (unsigned char *)malloc(pf
->size
* sizeof(unsigned char));
442 if (!pf
->bits
|| !pf
->offset
|| !pf
->offrot
|| !pf
->width
) {
443 fprintf(stderr
, "Error: no memory for font load\n");
447 fprintf(stderr
, "Header parsed\n");
451 /* read bdf font bitmaps, return 0 on error*/
452 int bdf_read_bitmaps(FILE *fp
, struct font
* pf
)
457 int i
, k
, encoding
, width
;
458 int bbw
, bbh
, bbx
, bby
;
459 int proportional
= 0;
464 /* reset file pointer*/
465 fseek(fp
, 0L, SEEK_SET
);
467 /* initially mark offsets as not used*/
468 for (i
=0; i
<pf
->size
; ++i
)
472 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
473 fprintf(stderr
, "Error: EOF on file\n");
476 if (isprefix(buf
, "STARTCHAR")) {
477 encoding
= width
= bbw
= bbh
= bbx
= bby
= -1;
480 if (isprefix(buf
, "ENCODING ")) {
481 if (sscanf(buf
, "ENCODING %d", &encoding
) != 1) {
482 fprintf(stderr
, "Error: bad 'ENCODING'\n");
485 if (encoding
< start_char
|| encoding
> limit_char
)
489 if (isprefix(buf
, "DWIDTH ")) {
490 if (sscanf(buf
, "DWIDTH %d", &width
) != 1) {
491 fprintf(stderr
, "Error: bad 'DWIDTH'\n");
494 /* use font boundingbox width if DWIDTH <= 0*/
496 width
= pf
->fbbw
- pf
->fbbx
;
499 if (isprefix(buf
, "BBX ")) {
500 if (sscanf(buf
, "BBX %d %d %d %d", &bbw
, &bbh
, &bbx
, &bby
) != 4) {
501 fprintf(stderr
, "Error: bad 'BBX'\n");
506 if (strequal(buf
, "BITMAP")) {
507 bitmap_t
*ch_bitmap
= pf
->bits
+ ofs
;
513 /* set bits offset in encode map*/
514 if (pf
->offset
[encoding
-pf
->firstchar
] != (unsigned int)-1) {
515 fprintf(stderr
, "Error: duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
519 pf
->offset
[encoding
-pf
->firstchar
] = ofs
;
520 pf
->offrot
[encoding
-pf
->firstchar
] = ofr
;
525 /*if (width > maxwidth)
529 if (width
> maxwidth
)
531 pf
->width
[encoding
-pf
->firstchar
] = width
;
534 memset(ch_bitmap
, 0, BITMAP_BYTES(width
) * pf
->height
);
536 ch_words
= BITMAP_WORDS(width
);
537 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
538 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
544 if (!bdf_getline(fp
, buf
, sizeof(buf
))) {
545 fprintf(stderr
, "Error: EOF reading BITMAP data\n");
548 if (isprefix(buf
, "ENDCHAR"))
551 hexnibbles
= strlen(buf
);
552 for (k
=0; k
<ch_words
; ++k
) {
553 int ndx
= k
* BITMAP_NIBBLES
;
554 int padnibbles
= hexnibbles
- ndx
;
559 if (padnibbles
>= BITMAP_NIBBLES
)
562 value
= bdf_hexval((unsigned char *)buf
,
563 ndx
, ndx
+BITMAP_NIBBLES
-1-padnibbles
);
564 value
<<= padnibbles
* BITMAP_NIBBLES
;
566 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
) |=
568 /* handle overflow into next image word*/
570 BM(pf
->height
- pf
->descent
- bby
- bbh
+ i
, k
+1) =
571 value
<< (BITMAP_BITSPERIMAGE
- bbx
);
576 ofs
+= BITMAP_WORDS(width
) * pf
->height
;
577 ofr
+= pf
->width
[encoding
-pf
->firstchar
] * ((pf
->height
+7)/8);
581 if (strequal(buf
, "ENDFONT"))
586 pf
->maxwidth
= maxwidth
;
588 /* change unused offset/width values to default char values*/
589 for (i
=0; i
<pf
->size
; ++i
) {
590 int defchar
= pf
->defaultchar
- pf
->firstchar
;
592 if (pf
->offset
[i
] == (unsigned int)-1) {
593 pf
->offset
[i
] = pf
->offset
[defchar
];
594 pf
->offrot
[i
] = pf
->offrot
[defchar
];
595 pf
->width
[i
] = pf
->width
[defchar
];
599 /* determine whether font doesn't require encode table*/
602 for (i
=0; i
<pf
->size
; ++i
) {
603 if (pf
->offrot
[i
] != l
) {
607 l
+= pf
->maxwidth
* ((pf
->height
+ 7) / 8);
611 for (i
=0; i
<pf
->size
; ++i
) {
612 if (pf
->offset
[i
] != l
) {
616 l
+= BITMAP_WORDS(pf
->width
[i
]) * pf
->height
;
624 /* determine whether font is fixed-width*/
625 for (i
=0; i
<pf
->size
; ++i
) {
626 if (pf
->width
[i
] != maxwidth
) {
636 /* reallocate bits array to actual bits used*/
637 if (ofs
< pf
->bits_size
) {
638 pf
->bits
= realloc(pf
->bits
, ofs
* sizeof(bitmap_t
));
642 if (ofs
> pf
->bits_size
) {
643 fprintf(stderr
, "Warning: DWIDTH spec > max FONTBOUNDINGBOX\n");
644 if (ofs
> pf
->bits_size
+EXTRA
) {
645 fprintf(stderr
, "Error: Not enough bits initially allocated\n");
653 pf
->bits_size
= ofr
; /* always update, rotated is smaller */
659 /* read the next non-comment line, returns buf or NULL if EOF*/
660 char *bdf_getline(FILE *fp
, char *buf
, int len
)
667 while ((c
= getc(fp
)) != EOF
) {
672 if (b
- buf
>= (len
- 1))
677 if (c
== EOF
&& b
== buf
)
679 if (b
!= buf
&& !isprefix(buf
, "COMMENT"))
685 /* return hex value of portion of buffer*/
686 bitmap_t
bdf_hexval(unsigned char *buf
, int ndx1
, int ndx2
)
691 for (i
=ndx1
; i
<=ndx2
; ++i
) {
693 if (c
>= '0' && c
<= '9')
696 if (c
>= 'A' && c
<= 'F')
699 if (c
>= 'a' && c
<= 'f')
703 val
= (val
<< 4) | c
;
709 * Take an bitmap_t bitmap and convert to Rockbox format.
710 * Used for converting font glyphs for the time being.
711 * Can use for standard X11 and Win32 images as well.
712 * See format description in lcd-recorder.c
714 * Doing it this way keeps fonts in standard formats,
715 * as well as keeping Rockbox hw bitmap format.
717 int rotleft(unsigned char *dst
, bitmap_t
*src
, unsigned int width
,
721 unsigned int src_words
; /* # words of input image*/
722 unsigned int dst_mask
; /* bit mask for destination */
723 bitmap_t src_mask
; /* bit mask for source */
725 /* calc words of input image*/
726 src_words
= BITMAP_WORDS(width
) * height
;
728 /* clear background*/
729 memset(dst
, 0, ((height
+ 7) / 8) * width
);
733 for (i
=0; i
< src_words
; i
++) {
735 /* calc src input bit*/
736 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
738 /* for each input column...*/
739 for(j
=0; j
< width
; j
++) {
741 if (src_mask
== 0) /* input word done? */
743 src_mask
= 1 << (sizeof (bitmap_t
) * 8 - 1);
744 i
++; /* next input word */
747 /* if set in input, set in rotated output */
748 if (src
[i
] & src_mask
)
751 src_mask
>>= 1; /* next input bit */
754 dst_mask
<<= 1; /* next output bit (row) */
755 if (dst_mask
> (1 << 7)) /* output bit > 7? */
758 dst
+= width
; /* next output byte row */
761 return ((height
+ 7) / 8) * width
; /* return result size in bytes */
765 /* generate C source from in-core font*/
766 int gen_c_source(struct font
* pf
, char *path
)
770 int did_defaultchar
= 0;
773 bitmap_t
*ofs
= pf
->bits
;
777 "/* Generated by convbdf on %s. */\n"
778 "#include \"font.h\"\n"
779 "#ifdef HAVE_LCD_BITMAP\n"
781 "/* Font information:\n"
788 " first char: %d (0x%02x)\n"
789 " last char: %d (0x%02x)\n"
790 " default char: %d (0x%02x)\n"
791 " proportional: %s\n"
795 "/* Font character bitmap data. */\n"
796 "static const unsigned char _font_bits[] = {\n"
799 ofp
= fopen(path
, "w");
801 fprintf(stderr
, "Can't create %s\n", path
);
805 strcpy(buf
, ctime(&t
));
806 buf
[strlen(buf
)-1] = 0;
808 fprintf(ofp
, hdr1
, buf
,
810 pf
->facename
? pf
->facename
: "",
811 pf
->maxwidth
, pf
->height
,
813 pf
->ascent
, pf
->descent
,
814 pf
->firstchar
, pf
->firstchar
,
815 pf
->firstchar
+pf
->size
-1, pf
->firstchar
+pf
->size
-1,
816 pf
->defaultchar
, pf
->defaultchar
,
817 pf
->width
? "yes": "no",
818 pf
->copyright
? pf
->copyright
: "");
820 /* generate bitmaps*/
821 for (i
=0; i
<pf
->size
; ++i
) {
824 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
825 int height
= pf
->height
;
826 bitmap_t
*bits
= pf
->bits
+ (pf
->offset
? pf
->offset
[i
]: (height
* i
));
830 * Generate bitmap bits only if not this index isn't
831 * the default character in encode map, or the default
832 * character hasn't been generated yet.
835 (pf
->offset
[i
] == pf
->offset
[pf
->defaultchar
-pf
->firstchar
])) {
836 if (did_defaultchar
) {
837 pf
->offrot
[i
] = pf
->offrot
[pf
->defaultchar
-pf
->firstchar
];
843 fprintf(ofp
, "\n/* Character %d (0x%02x):\n width %d",
844 i
+pf
->firstchar
, i
+pf
->firstchar
, width
);
847 fprintf(ofp
, "\n +");
848 for (x
=0; x
<width
; ++x
) fprintf(ofp
, "-");
853 if (x
== 0) fprintf(ofp
, " |");
856 bitcount
= BITMAP_BITSPERIMAGE
;
860 fprintf(ofp
, BITMAP_TESTBIT(bitvalue
)? "*": " ");
862 bitvalue
= BITMAP_SHIFTBIT(bitvalue
);
872 for (x
=0; x
<width
; ++x
)
874 fprintf(ofp
, "+ */\n");
877 fprintf(ofp
, " */\n");
879 bits
= pf
->bits
+ (pf
->offset
? pf
->offset
[i
]: (pf
->height
* i
));
880 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
882 unsigned char bytemap
[256];
885 int size
= rotleft(bytemap
, bits
, width
, pf
->height
);
886 for (y8
=0; y8
<pf
->height
; y8
+=8) /* column rows */
888 for (x
=0; x
<width
; x
++) {
889 fprintf(ofp
, "0x%02x, ", bytemap
[ix
]);
895 /* update offrot since bits are now in sorted order */
901 for (x
=BITMAP_WORDS(width
)*pf
->height
; x
>0; --x
) {
902 fprintf(ofp
, "0x%04x,\n", *bits
);
903 if (!did_syncmsg
&& *bits
++ != *ofs
++) {
904 fprintf(stderr
, "Warning: found encoding values in non-sorted order (not an error).\n");
910 fprintf(ofp
, "};\n\n");
913 /* output offset table*/
914 fprintf(ofp
, "/* Character->glyph mapping. */\n"
915 "static const unsigned short _sysfont_offset[] = {\n");
917 for (i
=0; i
<pf
->size
; ++i
)
918 fprintf(ofp
, " %ld,\t/* (0x%02x) */\n",
920 pf
->offrot
[i
], i
+pf
->firstchar
);
922 pf
->offset
[i
], i
+pf
->firstchar
);
924 fprintf(ofp
, "};\n\n");
927 /* output width table for proportional fonts*/
929 fprintf(ofp
, "/* Character width data. */\n"
930 "static const unsigned char _sysfont_width[] = {\n");
932 for (i
=0; i
<pf
->size
; ++i
)
933 fprintf(ofp
, " %d,\t/* (0x%02x) */\n",
934 pf
->width
[i
], i
+pf
->firstchar
);
935 fprintf(ofp
, "};\n\n");
938 /* output struct font struct*/
940 sprintf(obuf
, "_sysfont_offset,");
942 sprintf(obuf
, "0, /* no encode table */");
945 sprintf(buf
, "_sysfont_width, /* width */");
947 sprintf(buf
, "0, /* fixed width */");
949 fprintf(ofp
, "/* Exported structure definition. */\n"
950 "const struct font sysfont = {\n"
951 " %d, /* maxwidth */\n"
952 " %d, /* height */\n"
953 " %d, /* ascent */\n"
954 " %d, /* firstchar */\n"
956 " _font_bits, /* bits */\n"
959 " %d, /* defaultchar */\n"
960 " %d /* bits_size */\n"
962 "#endif /* HAVE_LCD_BITMAP */\n",
963 pf
->maxwidth
, pf
->height
,
975 static int writebyte(FILE *fp
, unsigned char c
)
977 return putc(c
, fp
) != EOF
;
980 static int writeshort(FILE *fp
, unsigned short s
)
983 return putc(s
>>8, fp
) != EOF
;
986 static int writeint(FILE *fp
, unsigned int l
)
991 return putc(l
>>24, fp
) != EOF
;
994 static int writestr(FILE *fp
, char *str
, int count
)
996 return fwrite(str
, 1, count
, fp
) == count
;
999 static int writestrpad(FILE *fp
, char *str
, int totlen
)
1003 while (str
&& *str
&& totlen
> 0) {
1005 ret
= putc(*str
++, fp
);
1009 while (--totlen
>= 0)
1010 ret
= putc(' ', fp
);
1014 /* generate .fnt format file from in-core font*/
1015 int gen_fnt_file(struct font
* pf
, char *path
)
1019 int did_defaultchar
= 0;
1024 ofp
= fopen(path
, "wb");
1026 fprintf(stderr
, "Can't create %s\n", path
);
1030 /* write magic and version #*/
1031 writestr(ofp
, VERSION
, 4);
1033 /* internal font name*/
1034 writestrpad(ofp
, pf
->name
, 64);
1037 writestrpad(ofp
, pf
->copyright
, 256);
1040 writeshort(ofp
, pf
->maxwidth
);
1041 writeshort(ofp
, pf
->height
);
1042 writeshort(ofp
, pf
->ascent
);
1044 writeint(ofp
, pf
->firstchar
);
1045 writeint(ofp
, pf
->defaultchar
);
1046 writeint(ofp
, pf
->size
);
1048 /* variable font data sizes*/
1049 writeint(ofp
, pf
->bits_size
); /* # words of bitmap_t*/
1050 writeint(ofp
, pf
->offset
? pf
->size
: 0); /* # ints of offset*/
1051 writeint(ofp
, pf
->width
? pf
->size
: 0); /* # bytes of width*/
1052 /* variable font data*/
1054 for (i
=0; i
<pf
->size
; ++i
)
1056 bitmap_t
* bits
= pf
->bits
+ (pf
->offset
? pf
->offset
[i
]: (pf
->height
* i
));
1057 int width
= pf
->width
? pf
->width
[i
] : pf
->maxwidth
;
1059 unsigned char bytemap
[256];
1062 (pf
->offset
[i
] == pf
->offset
[pf
->defaultchar
-pf
->firstchar
])) {
1063 if (did_defaultchar
) {
1064 pf
->offrot
[i
] = pf
->offrot
[pf
->defaultchar
-pf
->firstchar
];
1067 did_defaultchar
= 1;
1070 size
= rotleft(bytemap
, bits
, width
, pf
->height
);
1071 writestr(ofp
, (char *)bytemap
, size
);
1073 /* update offrot since bits are now in sorted order */
1074 pf
->offrot
[i
] = ofr
;
1078 if ( pf
->bits_size
< 0xFFDB )
1080 /* bitmap offset is small enough, use unsigned short for offset */
1082 writebyte(ofp
, 0); /* pad to 16-bit boundary*/
1086 /* bitmap offset is large then 64K, use unsigned int for offset */
1087 while (ftell(ofp
) & 3)
1088 writebyte(ofp
, 0); /* pad to 32-bit boundary*/
1093 for (i
=0; i
<pf
->size
; ++i
)
1095 if ( pf
->bits_size
< 0xFFDB )
1096 writeshort(ofp
, pf
->offrot
[i
]);
1098 writeint(ofp
, pf
->offrot
[i
]);
1103 for (i
=0; i
<pf
->size
; ++i
)
1104 writebyte(ofp
, pf
->width
[i
]);
1106 for (i
=0; i
<pf
->bits_size
; ++i
)
1107 writeshort(ofp
, pf
->bits
[i
]);
1109 writeshort(ofp
, 0); /* pad to 32-bit boundary*/
1112 for (i
=0; i
<pf
->size
; ++i
)
1113 writeint(ofp
, pf
->offset
[i
]);
1116 for (i
=0; i
<pf
->size
; ++i
)
1117 writebyte(ofp
, pf
->width
[i
]);