return real data on MODE_SENSE. This might fix the mac osx problems
[Rockbox.git] / tools / convbdf.c
blobcd76563003d423be8028286c5390babeef1bb1e1
1 /*
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...
8 * 09/17/02 Version 1.0
9 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <time.h>
15 #define ROTATE /* define this for the new, rotated format */
17 /* BEGIN font.h*/
18 /* loadable font magic and version #*/
19 #ifdef ROTATE
20 #define VERSION "RB12" /* newer version */
21 #else
22 #define VERSION "RB11"
23 #endif
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 */
38 struct font {
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*/
55 int pixel_size;
56 int descent;
57 int fbbw, fbbh, fbbx, fbby;
59 /* END font.h*/
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*/
66 int gen_c = 0;
67 int gen_h = 0;
68 int gen_fnt = 0;
69 int gen_map = 1;
70 int start_char = 0;
71 int limit_char = 65535;
72 int oflag = 0;
73 char outfile[256];
75 void usage(void);
76 void getopts(int *pac, char ***pav);
77 int convbdf(char *path);
79 void free_font(struct font* pf);
80 struct font* bdf_read_font(char *path);
81 int bdf_read_header(FILE *fp, struct font* pf);
82 int bdf_read_bitmaps(FILE *fp, struct font* pf);
83 char * bdf_getline(FILE *fp, char *buf, int len);
84 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2);
86 int gen_c_source(struct font* pf, char *path);
87 int gen_h_header(struct font* pf, char *path);
88 int gen_fnt_file(struct font* pf, char *path);
90 void
91 usage(void)
93 char help[] = {
94 "Usage: convbdf [options] [input-files]\n"
95 " convbdf [options] [-o output-file] [single-input-file]\n"
96 "Options:\n"
97 " -c Convert .bdf to .c source file\n"
98 " -h Convert .bdf to .h header file (to create sysfont.h)\n"
99 " -f Convert .bdf to .fnt font file\n"
100 " -s N Start output at character encodings >= N\n"
101 " -l N Limit output to character encodings <= N\n"
102 " -n Don't generate bitmaps as comments in .c file\n"
105 fprintf(stderr, help);
108 /* parse command line options*/
109 void getopts(int *pac, char ***pav)
111 char *p;
112 char **av;
113 int ac;
115 ac = *pac;
116 av = *pav;
117 while (ac > 0 && av[0][0] == '-') {
118 p = &av[0][1];
119 while( *p)
120 switch(*p++) {
121 case ' ': /* multiple -args on av[]*/
122 while( *p && *p == ' ')
123 p++;
124 if( *p++ != '-') /* next option must have dash*/
125 p = "";
126 break; /* proceed to next option*/
127 case 'c': /* generate .c output*/
128 gen_c = 1;
129 break;
130 case 'h': /* generate .h output*/
131 gen_h = 1;
132 break;
133 case 'f': /* generate .fnt output*/
134 gen_fnt = 1;
135 break;
136 case 'n': /* don't gen bitmap comments*/
137 gen_map = 0;
138 break;
139 case 'o': /* set output file*/
140 oflag = 1;
141 if (*p) {
142 strcpy(outfile, p);
143 while (*p && *p != ' ')
144 p++;
146 else {
147 av++; ac--;
148 if (ac > 0)
149 strcpy(outfile, av[0]);
151 break;
152 case 'l': /* set encoding limit*/
153 if (*p) {
154 limit_char = atoi(p);
155 while (*p && *p != ' ')
156 p++;
158 else {
159 av++; ac--;
160 if (ac > 0)
161 limit_char = atoi(av[0]);
163 break;
164 case 's': /* set encoding start*/
165 if (*p) {
166 start_char = atoi(p);
167 while (*p && *p != ' ')
168 p++;
170 else {
171 av++; ac--;
172 if (ac > 0)
173 start_char = atoi(av[0]);
175 break;
176 default:
177 fprintf(stderr, "Unknown option ignored: %c\r\n", *(p-1));
179 ++av; --ac;
181 *pac = ac;
182 *pav = av;
185 /* remove directory prefix and file suffix from full path*/
186 char *basename(char *path)
188 char *p, *b;
189 static char base[256];
191 /* remove prepended path and extension*/
192 b = path;
193 for (p=path; *p; ++p) {
194 if (*p == '/')
195 b = p + 1;
197 strcpy(base, b);
198 for (p=base; *p; ++p) {
199 if (*p == '.') {
200 *p = 0;
201 break;
204 return base;
207 int convbdf(char *path)
209 struct font* pf;
210 int ret = 0;
212 pf = bdf_read_font(path);
213 if (!pf)
214 exit(1);
216 if (gen_c) {
217 if (!oflag) {
218 strcpy(outfile, basename(path));
219 strcat(outfile, ".c");
221 ret |= gen_c_source(pf, outfile);
224 if (gen_h) {
225 if (!oflag) {
226 strcpy(outfile, basename(path));
227 strcat(outfile, ".h");
229 ret |= gen_h_header(pf, outfile);
232 if (gen_fnt) {
233 if (!oflag) {
234 strcpy(outfile, basename(path));
235 strcat(outfile, ".fnt");
237 ret |= gen_fnt_file(pf, outfile);
240 free_font(pf);
241 return ret;
244 int main(int ac, char **av)
246 int ret = 0;
248 ++av; --ac; /* skip av[0]*/
249 getopts(&ac, &av); /* read command line options*/
251 if (ac < 1 || (!gen_c && !gen_h && !gen_fnt)) {
252 usage();
253 exit(1);
255 if (oflag) {
256 if (ac > 1 || (gen_c && gen_fnt) || (gen_c && gen_h) || (gen_h && gen_fnt)) {
257 usage();
258 exit(1);
262 while (ac > 0) {
263 ret |= convbdf(av[0]);
264 ++av; --ac;
267 exit(ret);
270 /* free font structure*/
271 void free_font(struct font* pf)
273 if (!pf)
274 return;
275 if (pf->name)
276 free(pf->name);
277 if (pf->facename)
278 free(pf->facename);
279 if (pf->bits)
280 free(pf->bits);
281 if (pf->offset)
282 free(pf->offset);
283 if (pf->offrot)
284 free(pf->offrot);
285 if (pf->width)
286 free(pf->width);
287 free(pf);
290 /* build incore structure from .bdf file*/
291 struct font* bdf_read_font(char *path)
293 FILE *fp;
294 struct font* pf;
296 fp = fopen(path, "rb");
297 if (!fp) {
298 fprintf(stderr, "Error opening file: %s\n", path);
299 return NULL;
302 pf = (struct font*)calloc(1, sizeof(struct font));
303 if (!pf)
304 goto errout;
306 pf->name = strdup(basename(path));
308 if (!bdf_read_header(fp, pf)) {
309 fprintf(stderr, "Error reading font header\n");
310 goto errout;
313 if (!bdf_read_bitmaps(fp, pf)) {
314 fprintf(stderr, "Error reading font bitmaps\n");
315 goto errout;
318 fclose(fp);
319 return pf;
321 errout:
322 fclose(fp);
323 free_font(pf);
324 return NULL;
327 /* read bdf font header information, return 0 on error*/
328 int bdf_read_header(FILE *fp, struct font* pf)
330 int encoding;
331 int nchars, maxwidth;
332 int firstchar = 65535;
333 int lastchar = -1;
334 char buf[256];
335 char facename[256];
336 char copyright[256];
338 /* set certain values to errors for later error checking*/
339 pf->defaultchar = -1;
340 pf->ascent = -1;
341 pf->descent = -1;
343 for (;;) {
344 if (!bdf_getline(fp, buf, sizeof(buf))) {
345 fprintf(stderr, "Error: EOF on file\n");
346 return 0;
348 if (isprefix(buf, "FONT ")) { /* not required*/
349 if (sscanf(buf, "FONT %[^\n]", facename) != 1) {
350 fprintf(stderr, "Error: bad 'FONT'\n");
351 return 0;
353 pf->facename = strdup(facename);
354 continue;
356 if (isprefix(buf, "COPYRIGHT ")) { /* not required*/
357 if (sscanf(buf, "COPYRIGHT \"%[^\"]", copyright) != 1) {
358 fprintf(stderr, "Error: bad 'COPYRIGHT'\n");
359 return 0;
361 pf->copyright = strdup(copyright);
362 continue;
364 if (isprefix(buf, "DEFAULT_CHAR ")) { /* not required*/
365 if (sscanf(buf, "DEFAULT_CHAR %d", &pf->defaultchar) != 1) {
366 fprintf(stderr, "Error: bad 'DEFAULT_CHAR'\n");
367 return 0;
370 if (isprefix(buf, "FONT_DESCENT ")) {
371 if (sscanf(buf, "FONT_DESCENT %d", &pf->descent) != 1) {
372 fprintf(stderr, "Error: bad 'FONT_DESCENT'\n");
373 return 0;
375 continue;
377 if (isprefix(buf, "FONT_ASCENT ")) {
378 if (sscanf(buf, "FONT_ASCENT %d", &pf->ascent) != 1) {
379 fprintf(stderr, "Error: bad 'FONT_ASCENT'\n");
380 return 0;
382 continue;
384 if (isprefix(buf, "FONTBOUNDINGBOX ")) {
385 if (sscanf(buf, "FONTBOUNDINGBOX %d %d %d %d",
386 &pf->fbbw, &pf->fbbh, &pf->fbbx, &pf->fbby) != 4) {
387 fprintf(stderr, "Error: bad 'FONTBOUNDINGBOX'\n");
388 return 0;
390 continue;
392 if (isprefix(buf, "CHARS ")) {
393 if (sscanf(buf, "CHARS %d", &nchars) != 1) {
394 fprintf(stderr, "Error: bad 'CHARS'\n");
395 return 0;
397 continue;
401 * Reading ENCODING is necessary to get firstchar/lastchar
402 * which is needed to pre-calculate our offset and widths
403 * array sizes.
405 if (isprefix(buf, "ENCODING ")) {
406 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
407 fprintf(stderr, "Error: bad 'ENCODING'\n");
408 return 0;
410 if (encoding >= 0 &&
411 encoding <= limit_char &&
412 encoding >= start_char) {
414 if (firstchar > encoding)
415 firstchar = encoding;
416 if (lastchar < encoding)
417 lastchar = encoding;
419 continue;
421 if (strequal(buf, "ENDFONT"))
422 break;
425 /* calc font height*/
426 if (pf->ascent < 0 || pf->descent < 0 || firstchar < 0) {
427 fprintf(stderr, "Error: Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
428 return 0;
430 pf->height = pf->ascent + pf->descent;
432 /* calc default char*/
433 if (pf->defaultchar < 0 ||
434 pf->defaultchar < firstchar ||
435 pf->defaultchar > limit_char ||
436 pf->defaultchar > lastchar)
437 pf->defaultchar = firstchar;
439 /* calc font size (offset/width entries)*/
440 pf->firstchar = firstchar;
441 pf->size = lastchar - firstchar + 1;
443 /* use the font boundingbox to get initial maxwidth*/
444 /*maxwidth = pf->fbbw - pf->fbbx;*/
445 maxwidth = pf->fbbw;
447 /* initially use font maxwidth * height for bits allocation*/
448 pf->bits_size = nchars * BITMAP_WORDS(maxwidth) * pf->height;
450 /* allocate bits, offset, and width arrays*/
451 pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t) + EXTRA);
452 pf->offset = (unsigned int *)malloc(pf->size * sizeof(unsigned int));
453 pf->offrot = (unsigned int *)malloc(pf->size * sizeof(unsigned int));
454 pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char));
456 if (!pf->bits || !pf->offset || !pf->offrot || !pf->width) {
457 fprintf(stderr, "Error: no memory for font load\n");
458 return 0;
461 return 1;
464 /* read bdf font bitmaps, return 0 on error*/
465 int bdf_read_bitmaps(FILE *fp, struct font* pf)
467 int ofs = 0;
468 int ofr = 0;
469 int maxwidth = 0;
470 int i, k, encoding, width;
471 int bbw, bbh, bbx, bby;
472 int proportional = 0;
473 int encodetable = 0;
474 int l;
475 char buf[256];
477 /* reset file pointer*/
478 fseek(fp, 0L, SEEK_SET);
480 /* initially mark offsets as not used*/
481 for (i=0; i<pf->size; ++i)
482 pf->offset[i] = -1;
484 for (;;) {
485 if (!bdf_getline(fp, buf, sizeof(buf))) {
486 fprintf(stderr, "Error: EOF on file\n");
487 return 0;
489 if (isprefix(buf, "STARTCHAR")) {
490 encoding = width = bbw = bbh = bbx = bby = -1;
491 continue;
493 if (isprefix(buf, "ENCODING ")) {
494 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
495 fprintf(stderr, "Error: bad 'ENCODING'\n");
496 return 0;
498 if (encoding < start_char || encoding > limit_char)
499 encoding = -1;
500 continue;
502 if (isprefix(buf, "DWIDTH ")) {
503 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
504 fprintf(stderr, "Error: bad 'DWIDTH'\n");
505 return 0;
507 /* use font boundingbox width if DWIDTH <= 0*/
508 if (width <= 0)
509 width = pf->fbbw - pf->fbbx;
510 continue;
512 if (isprefix(buf, "BBX ")) {
513 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
514 fprintf(stderr, "Error: bad 'BBX'\n");
515 return 0;
517 continue;
519 if (strequal(buf, "BITMAP") || strequal(buf, "BITMAP ")) {
520 bitmap_t *ch_bitmap = pf->bits + ofs;
521 int ch_words;
523 if (encoding < 0)
524 continue;
526 /* set bits offset in encode map*/
527 if (pf->offset[encoding-pf->firstchar] != (unsigned int)-1) {
528 fprintf(stderr, "Error: duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
529 encoding, encoding);
530 continue;
532 pf->offset[encoding-pf->firstchar] = ofs;
533 pf->offrot[encoding-pf->firstchar] = ofr;
535 /* calc char width*/
536 if (bbx < 0) {
537 width -= bbx;
538 /*if (width > maxwidth)
539 width = maxwidth;*/
540 bbx = 0;
542 if (width > maxwidth)
543 maxwidth = width;
544 pf->width[encoding-pf->firstchar] = width;
546 /* clear bitmap*/
547 memset(ch_bitmap, 0, BITMAP_BYTES(width) * pf->height);
549 ch_words = BITMAP_WORDS(width);
550 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
551 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
553 /* read bitmaps*/
554 for (i=0; ; ++i) {
555 int hexnibbles;
557 if (!bdf_getline(fp, buf, sizeof(buf))) {
558 fprintf(stderr, "Error: EOF reading BITMAP data\n");
559 return 0;
561 if (isprefix(buf, "ENDCHAR"))
562 break;
564 hexnibbles = strlen(buf);
565 for (k=0; k<ch_words; ++k) {
566 int ndx = k * BITMAP_NIBBLES;
567 int padnibbles = hexnibbles - ndx;
568 bitmap_t value;
570 if (padnibbles <= 0)
571 break;
572 if (padnibbles >= BITMAP_NIBBLES)
573 padnibbles = 0;
575 value = bdf_hexval((unsigned char *)buf,
576 ndx, ndx+BITMAP_NIBBLES-1-padnibbles);
577 value <<= padnibbles * BITMAP_NIBBLES;
579 BM(pf->height - pf->descent - bby - bbh + i, k) |=
580 value >> bbx;
581 /* handle overflow into next image word*/
582 if (bbx) {
583 BM(pf->height - pf->descent - bby - bbh + i, k+1) =
584 value << (BITMAP_BITSPERIMAGE - bbx);
589 ofs += BITMAP_WORDS(width) * pf->height;
590 ofr += pf->width[encoding-pf->firstchar] * ((pf->height+7)/8);
592 continue;
594 if (strequal(buf, "ENDFONT"))
595 break;
598 /* set max width*/
599 pf->maxwidth = maxwidth;
601 /* change unused width values to default char values*/
602 for (i=0; i<pf->size; ++i) {
603 int defchar = pf->defaultchar - pf->firstchar;
605 if (pf->offset[i] == (unsigned int)-1)
606 pf->width[i] = pf->width[defchar];
609 /* determine whether font doesn't require encode table*/
610 #ifdef ROTATE
611 l = 0;
612 for (i=0; i<pf->size; ++i) {
613 if (pf->offrot[i] != l) {
614 encodetable = 1;
615 break;
617 l += pf->maxwidth * ((pf->height + 7) / 8);
619 #else
620 l = 0;
621 for (i=0; i<pf->size; ++i) {
622 if (pf->offset[i] != l) {
623 encodetable = 1;
624 break;
626 l += BITMAP_WORDS(pf->width[i]) * pf->height;
628 #endif
629 if (!encodetable) {
630 free(pf->offset);
631 pf->offset = NULL;
634 /* determine whether font is fixed-width*/
635 for (i=0; i<pf->size; ++i) {
636 if (pf->width[i] != maxwidth) {
637 proportional = 1;
638 break;
641 if (!proportional) {
642 free(pf->width);
643 pf->width = NULL;
646 /* reallocate bits array to actual bits used*/
647 if (ofs < pf->bits_size) {
648 pf->bits = realloc(pf->bits, ofs * sizeof(bitmap_t));
649 pf->bits_size = ofs;
651 else {
652 if (ofs > pf->bits_size) {
653 fprintf(stderr, "Warning: DWIDTH spec > max FONTBOUNDINGBOX\n");
654 if (ofs > pf->bits_size+EXTRA) {
655 fprintf(stderr, "Error: Not enough bits initially allocated\n");
656 return 0;
658 pf->bits_size = ofs;
662 #ifdef ROTATE
663 pf->bits_size = ofr; /* always update, rotated is smaller */
664 #endif
666 return 1;
669 /* read the next non-comment line, returns buf or NULL if EOF*/
670 char *bdf_getline(FILE *fp, char *buf, int len)
672 int c;
673 char *b;
675 for (;;) {
676 b = buf;
677 while ((c = getc(fp)) != EOF) {
678 if (c == '\r')
679 continue;
680 if (c == '\n')
681 break;
682 if (b - buf >= (len - 1))
683 break;
684 *b++ = c;
686 *b = '\0';
687 if (c == EOF && b == buf)
688 return NULL;
689 if (b != buf && !isprefix(buf, "COMMENT"))
690 break;
692 return buf;
695 /* return hex value of portion of buffer*/
696 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2)
698 bitmap_t val = 0;
699 int i, c;
701 for (i=ndx1; i<=ndx2; ++i) {
702 c = buf[i];
703 if (c >= '0' && c <= '9')
704 c -= '0';
705 else
706 if (c >= 'A' && c <= 'F')
707 c = c - 'A' + 10;
708 else
709 if (c >= 'a' && c <= 'f')
710 c = c - 'a' + 10;
711 else
712 c = 0;
713 val = (val << 4) | c;
715 return val;
719 * Take an bitmap_t bitmap and convert to Rockbox format.
720 * Used for converting font glyphs for the time being.
721 * Can use for standard X11 and Win32 images as well.
722 * See format description in lcd-recorder.c
724 * Doing it this way keeps fonts in standard formats,
725 * as well as keeping Rockbox hw bitmap format.
727 int rotleft(unsigned char *dst, /* output buffer */
728 size_t dstlen, /* buffer size */
729 bitmap_t *src, unsigned int width, unsigned int height)
731 unsigned int i,j;
732 unsigned int src_words; /* # words of input image*/
733 unsigned int dst_mask; /* bit mask for destination */
734 bitmap_t src_mask; /* bit mask for source */
736 /* calc words of input image*/
737 src_words = BITMAP_WORDS(width) * height;
739 if(((height + 7) / 8) * width > dstlen) {
740 fprintf(stderr, "%s:%d %d x %d overflows %d bytes buffer, needs %d\n",
741 __FILE__, __LINE__, width, height, dstlen,
742 ((height + 7) / 8) * width );
743 return 0;
746 /* clear background*/
747 memset(dst, 0, ((height + 7) / 8) * width);
749 dst_mask = 1;
751 for (i=0; i < src_words; i++) {
753 /* calc src input bit*/
754 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
756 /* for each input column...*/
757 for(j=0; j < width; j++) {
759 if (src_mask == 0) /* input word done? */
761 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
762 i++; /* next input word */
765 /* if set in input, set in rotated output */
766 if (src[i] & src_mask)
767 dst[j] |= dst_mask;
769 src_mask >>= 1; /* next input bit */
772 dst_mask <<= 1; /* next output bit (row) */
773 if (dst_mask > (1 << 7)) /* output bit > 7? */
775 dst_mask = 1;
776 dst += width; /* next output byte row */
779 return ((height + 7) / 8) * width; /* return result size in bytes */
783 /* generate C source from in-core font*/
784 int gen_c_source(struct font* pf, char *path)
786 FILE *ofp;
787 int i, ofr = 0;
788 int did_syncmsg = 0;
789 time_t t = time(0);
790 bitmap_t *ofs = pf->bits;
791 char buf[256];
792 char obuf[256];
793 char hdr1[] = {
794 "/* Generated by convbdf on %s. */\n"
795 "#include \"font.h\"\n"
796 "#ifdef HAVE_LCD_BITMAP\n"
797 "\n"
798 "/* Font information:\n"
799 " name: %s\n"
800 " facename: %s\n"
801 " w x h: %dx%d\n"
802 " size: %d\n"
803 " ascent: %d\n"
804 " descent: %d\n"
805 " first char: %d (0x%02x)\n"
806 " last char: %d (0x%02x)\n"
807 " default char: %d (0x%02x)\n"
808 " proportional: %s\n"
809 " %s\n"
810 "*/\n"
811 "\n"
812 "/* Font character bitmap data. */\n"
813 "static const unsigned char _font_bits[] = {\n"
816 ofp = fopen(path, "w");
817 if (!ofp) {
818 fprintf(stderr, "Can't create %s\n", path);
819 return 1;
822 strcpy(buf, ctime(&t));
823 buf[strlen(buf)-1] = 0;
825 fprintf(ofp, hdr1, buf,
826 pf->name,
827 pf->facename? pf->facename: "",
828 pf->maxwidth, pf->height,
829 pf->size,
830 pf->ascent, pf->descent,
831 pf->firstchar, pf->firstchar,
832 pf->firstchar+pf->size-1, pf->firstchar+pf->size-1,
833 pf->defaultchar, pf->defaultchar,
834 pf->width? "yes": "no",
835 pf->copyright? pf->copyright: "");
837 /* generate bitmaps*/
838 for (i=0; i<pf->size; ++i) {
839 int x;
840 int bitcount = 0;
841 int width = pf->width ? pf->width[i] : pf->maxwidth;
842 int height = pf->height;
843 bitmap_t *bits;
844 bitmap_t bitvalue;
846 /* Skip missing glyphs */
847 if (pf->offset && (pf->offset[i] == (unsigned int)-1))
848 continue;
850 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
852 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d",
853 i+pf->firstchar, i+pf->firstchar, width);
855 if (gen_map) {
856 fprintf(ofp, "\n +");
857 for (x=0; x<width; ++x) fprintf(ofp, "-");
858 fprintf(ofp, "+\n");
860 x = 0;
861 while (height > 0) {
862 if (x == 0) fprintf(ofp, " |");
864 if (bitcount <= 0) {
865 bitcount = BITMAP_BITSPERIMAGE;
866 bitvalue = *bits++;
869 fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " ");
871 bitvalue = BITMAP_SHIFTBIT(bitvalue);
872 --bitcount;
873 if (++x == width) {
874 fprintf(ofp, "|\n");
875 --height;
876 x = 0;
877 bitcount = 0;
880 fprintf(ofp, " +");
881 for (x=0; x<width; ++x)
882 fprintf(ofp, "-");
883 fprintf(ofp, "+ */\n");
885 else
886 fprintf(ofp, " */\n");
888 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
889 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
891 unsigned char bytemap[512];
892 int y8, ix=0;
894 int size = rotleft(bytemap, sizeof(bytemap), bits, width,
895 pf->height);
896 for (y8=0; y8<pf->height; y8+=8) /* column rows */
898 for (x=0; x<width; x++) {
899 fprintf(ofp, "0x%02x, ", bytemap[ix]);
900 ix++;
902 fprintf(ofp, "\n");
905 /* update offrot since bits are now in sorted order */
906 pf->offrot[i] = ofr;
907 ofr += size;
910 #else
911 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) {
912 fprintf(ofp, "0x%04x,\n", *bits);
913 if (!did_syncmsg && *bits++ != *ofs++) {
914 fprintf(stderr, "Warning: found encoding values in non-sorted order (not an error).\n");
915 did_syncmsg = 1;
918 #endif
920 fprintf(ofp, "};\n\n");
922 if (pf->offset) {
923 /* output offset table*/
924 fprintf(ofp, "/* Character->glyph mapping. */\n"
925 "static const unsigned short _sysfont_offset[] = {\n");
927 for (i=0; i<pf->size; ++i) {
928 if (pf->offset[i] == (unsigned int)-1) {
929 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
930 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
932 fprintf(ofp, " %ld,\t/* (0x%02x) */\n",
933 #ifdef ROTATE
934 pf->offrot[i], i+pf->firstchar);
935 #else
936 pf->offset[i], i+pf->firstchar);
937 #endif
939 fprintf(ofp, "};\n\n");
942 /* output width table for proportional fonts*/
943 if (pf->width) {
944 fprintf(ofp, "/* Character width data. */\n"
945 "static const unsigned char _sysfont_width[] = {\n");
947 for (i=0; i<pf->size; ++i)
948 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
949 pf->width[i], i+pf->firstchar);
950 fprintf(ofp, "};\n\n");
953 /* output struct font struct*/
954 if (pf->offset)
955 sprintf(obuf, "_sysfont_offset,");
956 else
957 sprintf(obuf, "0, /* no encode table */");
959 if (pf->width)
960 sprintf(buf, "_sysfont_width, /* width */");
961 else
962 sprintf(buf, "0, /* fixed width */");
964 fprintf(ofp, "/* Exported structure definition. */\n"
965 "const struct font sysfont = {\n"
966 " %d, /* maxwidth */\n"
967 " %d, /* height */\n"
968 " %d, /* ascent */\n"
969 " %d, /* firstchar */\n"
970 " %d, /* size */\n"
971 " _font_bits, /* bits */\n"
972 " %s /* offset */\n"
973 " %s\n"
974 " %d, /* defaultchar */\n"
975 " %d /* bits_size */\n"
976 "};\n"
977 "#endif /* HAVE_LCD_BITMAP */\n",
978 pf->maxwidth, pf->height,
979 pf->ascent,
980 pf->firstchar,
981 pf->size,
982 obuf,
983 buf,
984 pf->defaultchar,
985 pf->bits_size);
987 return 0;
990 /* generate C header from in-core font*/
991 int gen_h_header(struct font* pf, char *path)
993 FILE *ofp;
994 time_t t = time(0);
995 char buf[256];
996 char hdr1[] = {
997 "/* Generated by convbdf on %s. */\n"
998 "#ifdef HAVE_LCD_BITMAP\n"
999 "\n"
1000 "/* Font information*/\n"
1001 "#define SYSFONT_NAME %s\n"
1002 "#define SYSFONT_FACENAME %s\n"
1003 "#define SYSFONT_WIDTH %d\n"
1004 "#define SYSFONT_HEIGHT %d\n"
1005 "#define SYSFONT_SIZE %d\n"
1006 "#define SYSFONT_ASCENT %d\n"
1007 "#define SYSFONT_DESCENT %d\n"
1008 "#define SYSFONT_FIRST_CHAR %d\n"
1009 "#define SYSFONT_LAST_CHAR %d\n"
1010 "#define SYSFONT_DEFAULT_CHAR %d\n"
1011 "#define SYSFONT_PROPORTIONAL %d\n"
1012 "#define SYSFONT_COPYRIGHT %s\n"
1013 "#define SYSFONT_BITS_SIZE %d\n"
1014 "\n"
1015 "#endif\n"
1018 ofp = fopen(path, "w");
1019 if (!ofp) {
1020 fprintf(stderr, "Can't create %s\n", path);
1021 return 1;
1024 strcpy(buf, ctime(&t));
1025 buf[strlen(buf)-1] = 0;
1027 fprintf(ofp, hdr1, buf,
1028 pf->name,
1029 pf->facename? pf->facename: "",
1030 pf->maxwidth,
1031 pf->height,
1032 pf->size,
1033 pf->ascent,
1034 pf->descent,
1035 pf->firstchar,
1036 pf->firstchar+pf->size-1,
1037 pf->defaultchar,
1038 pf->width? 1: 0,
1039 pf->copyright? pf->copyright: "",
1040 pf->bits_size);
1042 return 0;
1045 static int writebyte(FILE *fp, unsigned char c)
1047 return putc(c, fp) != EOF;
1050 static int writeshort(FILE *fp, unsigned short s)
1052 putc(s, fp);
1053 return putc(s>>8, fp) != EOF;
1056 static int writeint(FILE *fp, unsigned int l)
1058 putc(l, fp);
1059 putc(l>>8, fp);
1060 putc(l>>16, fp);
1061 return putc(l>>24, fp) != EOF;
1064 static int writestr(FILE *fp, char *str, int count)
1066 return fwrite(str, 1, count, fp) == count;
1069 static int writestrpad(FILE *fp, char *str, int totlen)
1071 int ret;
1073 while (str && *str && totlen > 0) {
1074 if (*str) {
1075 ret = putc(*str++, fp);
1076 --totlen;
1079 while (--totlen >= 0)
1080 ret = putc(' ', fp);
1081 return ret;
1084 /* generate .fnt format file from in-core font*/
1085 int gen_fnt_file(struct font* pf, char *path)
1087 FILE *ofp;
1088 int i;
1089 #ifdef ROTATE
1090 int ofr = 0;
1091 #endif
1093 ofp = fopen(path, "wb");
1094 if (!ofp) {
1095 fprintf(stderr, "Can't create %s\n", path);
1096 return 1;
1099 /* write magic and version #*/
1100 writestr(ofp, VERSION, 4);
1101 #ifndef ROTATE
1102 /* internal font name*/
1103 writestrpad(ofp, pf->name, 64);
1105 /* copyright*/
1106 writestrpad(ofp, pf->copyright, 256);
1107 #endif
1108 /* font info*/
1109 writeshort(ofp, pf->maxwidth);
1110 writeshort(ofp, pf->height);
1111 writeshort(ofp, pf->ascent);
1112 writeshort(ofp, 0);
1113 writeint(ofp, pf->firstchar);
1114 writeint(ofp, pf->defaultchar);
1115 writeint(ofp, pf->size);
1117 /* variable font data sizes*/
1118 writeint(ofp, pf->bits_size); /* # words of bitmap_t*/
1119 writeint(ofp, pf->offset? pf->size: 0); /* # ints of offset*/
1120 writeint(ofp, pf->width? pf->size: 0); /* # bytes of width*/
1121 /* variable font data*/
1122 #ifdef ROTATE
1123 for (i=0; i<pf->size; ++i)
1125 bitmap_t* bits;
1126 int width = pf->width ? pf->width[i] : pf->maxwidth;
1127 int size;
1128 unsigned char bytemap[512];
1130 /* Skip missing glyphs */
1131 if (pf->offset && (pf->offset[i] == (unsigned int)-1))
1132 continue;
1134 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
1136 size = rotleft(bytemap, sizeof(bytemap), bits, width, pf->height);
1137 writestr(ofp, (char *)bytemap, size);
1139 /* update offrot since bits are now in sorted order */
1140 pf->offrot[i] = ofr;
1141 ofr += size;
1144 if ( pf->bits_size < 0xFFDB )
1146 /* bitmap offset is small enough, use unsigned short for offset */
1147 if (ftell(ofp) & 1)
1148 writebyte(ofp, 0); /* pad to 16-bit boundary*/
1150 else
1152 /* bitmap offset is large then 64K, use unsigned int for offset */
1153 while (ftell(ofp) & 3)
1154 writebyte(ofp, 0); /* pad to 32-bit boundary*/
1157 if (pf->offset)
1159 for (i=0; i<pf->size; ++i)
1161 if (pf->offset[i] == (unsigned int)-1) {
1162 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
1164 if ( pf->bits_size < 0xFFDB )
1165 writeshort(ofp, pf->offrot[i]);
1166 else
1167 writeint(ofp, pf->offrot[i]);
1171 if (pf->width)
1172 for (i=0; i<pf->size; ++i)
1173 writebyte(ofp, pf->width[i]);
1174 #else
1175 for (i=0; i<pf->bits_size; ++i)
1176 writeshort(ofp, pf->bits[i]);
1177 if (ftell(ofp) & 2)
1178 writeshort(ofp, 0); /* pad to 32-bit boundary*/
1180 if (pf->offset)
1181 for (i=0; i<pf->size; ++i) {
1182 if (pf->offset[i] == (unsigned int)-1) {
1183 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
1185 writeint(ofp, pf->offset[i]);
1188 if (pf->width)
1189 for (i=0; i<pf->size; ++i)
1190 writebyte(ofp, pf->width[i]);
1191 #endif
1192 fclose(ofp);
1193 return 0;