Blackjack: small improvement in the c200 buttonmap - use the volume buttons instead...
[Rockbox.git] / tools / convbdf.c
blobcbb13cb34a75abc162e9403468864787b582ebaa
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 fprintf(stderr, "Header parsed\n");
462 return 1;
465 /* read bdf font bitmaps, return 0 on error*/
466 int bdf_read_bitmaps(FILE *fp, struct font* pf)
468 int ofs = 0;
469 int ofr = 0;
470 int maxwidth = 0;
471 int i, k, encoding, width;
472 int bbw, bbh, bbx, bby;
473 int proportional = 0;
474 int encodetable = 0;
475 int l;
476 char buf[256];
478 /* reset file pointer*/
479 fseek(fp, 0L, SEEK_SET);
481 /* initially mark offsets as not used*/
482 for (i=0; i<pf->size; ++i)
483 pf->offset[i] = -1;
485 for (;;) {
486 if (!bdf_getline(fp, buf, sizeof(buf))) {
487 fprintf(stderr, "Error: EOF on file\n");
488 return 0;
490 if (isprefix(buf, "STARTCHAR")) {
491 encoding = width = bbw = bbh = bbx = bby = -1;
492 continue;
494 if (isprefix(buf, "ENCODING ")) {
495 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
496 fprintf(stderr, "Error: bad 'ENCODING'\n");
497 return 0;
499 if (encoding < start_char || encoding > limit_char)
500 encoding = -1;
501 continue;
503 if (isprefix(buf, "DWIDTH ")) {
504 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
505 fprintf(stderr, "Error: bad 'DWIDTH'\n");
506 return 0;
508 /* use font boundingbox width if DWIDTH <= 0*/
509 if (width <= 0)
510 width = pf->fbbw - pf->fbbx;
511 continue;
513 if (isprefix(buf, "BBX ")) {
514 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
515 fprintf(stderr, "Error: bad 'BBX'\n");
516 return 0;
518 continue;
520 if (strequal(buf, "BITMAP") || strequal(buf, "BITMAP ")) {
521 bitmap_t *ch_bitmap = pf->bits + ofs;
522 int ch_words;
524 if (encoding < 0)
525 continue;
527 /* set bits offset in encode map*/
528 if (pf->offset[encoding-pf->firstchar] != (unsigned int)-1) {
529 fprintf(stderr, "Error: duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
530 encoding, encoding);
531 continue;
533 pf->offset[encoding-pf->firstchar] = ofs;
534 pf->offrot[encoding-pf->firstchar] = ofr;
536 /* calc char width*/
537 if (bbx < 0) {
538 width -= bbx;
539 /*if (width > maxwidth)
540 width = maxwidth;*/
541 bbx = 0;
543 if (width > maxwidth)
544 maxwidth = width;
545 pf->width[encoding-pf->firstchar] = width;
547 /* clear bitmap*/
548 memset(ch_bitmap, 0, BITMAP_BYTES(width) * pf->height);
550 ch_words = BITMAP_WORDS(width);
551 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
552 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
554 /* read bitmaps*/
555 for (i=0; ; ++i) {
556 int hexnibbles;
558 if (!bdf_getline(fp, buf, sizeof(buf))) {
559 fprintf(stderr, "Error: EOF reading BITMAP data\n");
560 return 0;
562 if (isprefix(buf, "ENDCHAR"))
563 break;
565 hexnibbles = strlen(buf);
566 for (k=0; k<ch_words; ++k) {
567 int ndx = k * BITMAP_NIBBLES;
568 int padnibbles = hexnibbles - ndx;
569 bitmap_t value;
571 if (padnibbles <= 0)
572 break;
573 if (padnibbles >= BITMAP_NIBBLES)
574 padnibbles = 0;
576 value = bdf_hexval((unsigned char *)buf,
577 ndx, ndx+BITMAP_NIBBLES-1-padnibbles);
578 value <<= padnibbles * BITMAP_NIBBLES;
580 BM(pf->height - pf->descent - bby - bbh + i, k) |=
581 value >> bbx;
582 /* handle overflow into next image word*/
583 if (bbx) {
584 BM(pf->height - pf->descent - bby - bbh + i, k+1) =
585 value << (BITMAP_BITSPERIMAGE - bbx);
590 ofs += BITMAP_WORDS(width) * pf->height;
591 ofr += pf->width[encoding-pf->firstchar] * ((pf->height+7)/8);
593 continue;
595 if (strequal(buf, "ENDFONT"))
596 break;
599 /* set max width*/
600 pf->maxwidth = maxwidth;
602 /* change unused width values to default char values*/
603 for (i=0; i<pf->size; ++i) {
604 int defchar = pf->defaultchar - pf->firstchar;
606 if (pf->offset[i] == (unsigned int)-1)
607 pf->width[i] = pf->width[defchar];
610 /* determine whether font doesn't require encode table*/
611 #ifdef ROTATE
612 l = 0;
613 for (i=0; i<pf->size; ++i) {
614 if (pf->offrot[i] != l) {
615 encodetable = 1;
616 break;
618 l += pf->maxwidth * ((pf->height + 7) / 8);
620 #else
621 l = 0;
622 for (i=0; i<pf->size; ++i) {
623 if (pf->offset[i] != l) {
624 encodetable = 1;
625 break;
627 l += BITMAP_WORDS(pf->width[i]) * pf->height;
629 #endif
630 if (!encodetable) {
631 free(pf->offset);
632 pf->offset = NULL;
635 /* determine whether font is fixed-width*/
636 for (i=0; i<pf->size; ++i) {
637 if (pf->width[i] != maxwidth) {
638 proportional = 1;
639 break;
642 if (!proportional) {
643 free(pf->width);
644 pf->width = NULL;
647 /* reallocate bits array to actual bits used*/
648 if (ofs < pf->bits_size) {
649 pf->bits = realloc(pf->bits, ofs * sizeof(bitmap_t));
650 pf->bits_size = ofs;
652 else {
653 if (ofs > pf->bits_size) {
654 fprintf(stderr, "Warning: DWIDTH spec > max FONTBOUNDINGBOX\n");
655 if (ofs > pf->bits_size+EXTRA) {
656 fprintf(stderr, "Error: Not enough bits initially allocated\n");
657 return 0;
659 pf->bits_size = ofs;
663 #ifdef ROTATE
664 pf->bits_size = ofr; /* always update, rotated is smaller */
665 #endif
667 return 1;
670 /* read the next non-comment line, returns buf or NULL if EOF*/
671 char *bdf_getline(FILE *fp, char *buf, int len)
673 int c;
674 char *b;
676 for (;;) {
677 b = buf;
678 while ((c = getc(fp)) != EOF) {
679 if (c == '\r')
680 continue;
681 if (c == '\n')
682 break;
683 if (b - buf >= (len - 1))
684 break;
685 *b++ = c;
687 *b = '\0';
688 if (c == EOF && b == buf)
689 return NULL;
690 if (b != buf && !isprefix(buf, "COMMENT"))
691 break;
693 return buf;
696 /* return hex value of portion of buffer*/
697 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2)
699 bitmap_t val = 0;
700 int i, c;
702 for (i=ndx1; i<=ndx2; ++i) {
703 c = buf[i];
704 if (c >= '0' && c <= '9')
705 c -= '0';
706 else
707 if (c >= 'A' && c <= 'F')
708 c = c - 'A' + 10;
709 else
710 if (c >= 'a' && c <= 'f')
711 c = c - 'a' + 10;
712 else
713 c = 0;
714 val = (val << 4) | c;
716 return val;
720 * Take an bitmap_t bitmap and convert to Rockbox format.
721 * Used for converting font glyphs for the time being.
722 * Can use for standard X11 and Win32 images as well.
723 * See format description in lcd-recorder.c
725 * Doing it this way keeps fonts in standard formats,
726 * as well as keeping Rockbox hw bitmap format.
728 int rotleft(unsigned char *dst, /* output buffer */
729 size_t dstlen, /* buffer size */
730 bitmap_t *src, unsigned int width, unsigned int height)
732 unsigned int i,j;
733 unsigned int src_words; /* # words of input image*/
734 unsigned int dst_mask; /* bit mask for destination */
735 bitmap_t src_mask; /* bit mask for source */
737 /* calc words of input image*/
738 src_words = BITMAP_WORDS(width) * height;
740 if(((height + 7) / 8) * width > dstlen) {
741 fprintf(stderr, "%s:%d %d x %d overflows %d bytes buffer, needs %d\n",
742 __FILE__, __LINE__, width, height, dstlen,
743 ((height + 7) / 8) * width );
744 return 0;
747 /* clear background*/
748 memset(dst, 0, ((height + 7) / 8) * width);
750 dst_mask = 1;
752 for (i=0; i < src_words; i++) {
754 /* calc src input bit*/
755 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
757 /* for each input column...*/
758 for(j=0; j < width; j++) {
760 if (src_mask == 0) /* input word done? */
762 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
763 i++; /* next input word */
766 /* if set in input, set in rotated output */
767 if (src[i] & src_mask)
768 dst[j] |= dst_mask;
770 src_mask >>= 1; /* next input bit */
773 dst_mask <<= 1; /* next output bit (row) */
774 if (dst_mask > (1 << 7)) /* output bit > 7? */
776 dst_mask = 1;
777 dst += width; /* next output byte row */
780 return ((height + 7) / 8) * width; /* return result size in bytes */
784 /* generate C source from in-core font*/
785 int gen_c_source(struct font* pf, char *path)
787 FILE *ofp;
788 int i, ofr = 0;
789 int did_syncmsg = 0;
790 time_t t = time(0);
791 bitmap_t *ofs = pf->bits;
792 char buf[256];
793 char obuf[256];
794 char hdr1[] = {
795 "/* Generated by convbdf on %s. */\n"
796 "#include \"font.h\"\n"
797 "#ifdef HAVE_LCD_BITMAP\n"
798 "\n"
799 "/* Font information:\n"
800 " name: %s\n"
801 " facename: %s\n"
802 " w x h: %dx%d\n"
803 " size: %d\n"
804 " ascent: %d\n"
805 " descent: %d\n"
806 " first char: %d (0x%02x)\n"
807 " last char: %d (0x%02x)\n"
808 " default char: %d (0x%02x)\n"
809 " proportional: %s\n"
810 " %s\n"
811 "*/\n"
812 "\n"
813 "/* Font character bitmap data. */\n"
814 "static const unsigned char _font_bits[] = {\n"
817 ofp = fopen(path, "w");
818 if (!ofp) {
819 fprintf(stderr, "Can't create %s\n", path);
820 return 1;
823 strcpy(buf, ctime(&t));
824 buf[strlen(buf)-1] = 0;
826 fprintf(ofp, hdr1, buf,
827 pf->name,
828 pf->facename? pf->facename: "",
829 pf->maxwidth, pf->height,
830 pf->size,
831 pf->ascent, pf->descent,
832 pf->firstchar, pf->firstchar,
833 pf->firstchar+pf->size-1, pf->firstchar+pf->size-1,
834 pf->defaultchar, pf->defaultchar,
835 pf->width? "yes": "no",
836 pf->copyright? pf->copyright: "");
838 /* generate bitmaps*/
839 for (i=0; i<pf->size; ++i) {
840 int x;
841 int bitcount = 0;
842 int width = pf->width ? pf->width[i] : pf->maxwidth;
843 int height = pf->height;
844 bitmap_t *bits;
845 bitmap_t bitvalue;
847 /* Skip missing glyphs */
848 if (pf->offset && (pf->offset[i] == (unsigned int)-1))
849 continue;
851 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
853 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d",
854 i+pf->firstchar, i+pf->firstchar, width);
856 if (gen_map) {
857 fprintf(ofp, "\n +");
858 for (x=0; x<width; ++x) fprintf(ofp, "-");
859 fprintf(ofp, "+\n");
861 x = 0;
862 while (height > 0) {
863 if (x == 0) fprintf(ofp, " |");
865 if (bitcount <= 0) {
866 bitcount = BITMAP_BITSPERIMAGE;
867 bitvalue = *bits++;
870 fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " ");
872 bitvalue = BITMAP_SHIFTBIT(bitvalue);
873 --bitcount;
874 if (++x == width) {
875 fprintf(ofp, "|\n");
876 --height;
877 x = 0;
878 bitcount = 0;
881 fprintf(ofp, " +");
882 for (x=0; x<width; ++x)
883 fprintf(ofp, "-");
884 fprintf(ofp, "+ */\n");
886 else
887 fprintf(ofp, " */\n");
889 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
890 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
892 unsigned char bytemap[512];
893 int y8, ix=0;
895 int size = rotleft(bytemap, sizeof(bytemap), bits, width,
896 pf->height);
897 for (y8=0; y8<pf->height; y8+=8) /* column rows */
899 for (x=0; x<width; x++) {
900 fprintf(ofp, "0x%02x, ", bytemap[ix]);
901 ix++;
903 fprintf(ofp, "\n");
906 /* update offrot since bits are now in sorted order */
907 pf->offrot[i] = ofr;
908 ofr += size;
911 #else
912 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) {
913 fprintf(ofp, "0x%04x,\n", *bits);
914 if (!did_syncmsg && *bits++ != *ofs++) {
915 fprintf(stderr, "Warning: found encoding values in non-sorted order (not an error).\n");
916 did_syncmsg = 1;
919 #endif
921 fprintf(ofp, "};\n\n");
923 if (pf->offset) {
924 /* output offset table*/
925 fprintf(ofp, "/* Character->glyph mapping. */\n"
926 "static const unsigned short _sysfont_offset[] = {\n");
928 for (i=0; i<pf->size; ++i) {
929 if (pf->offset[i] == (unsigned int)-1) {
930 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
931 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
933 fprintf(ofp, " %ld,\t/* (0x%02x) */\n",
934 #ifdef ROTATE
935 pf->offrot[i], i+pf->firstchar);
936 #else
937 pf->offset[i], i+pf->firstchar);
938 #endif
940 fprintf(ofp, "};\n\n");
943 /* output width table for proportional fonts*/
944 if (pf->width) {
945 fprintf(ofp, "/* Character width data. */\n"
946 "static const unsigned char _sysfont_width[] = {\n");
948 for (i=0; i<pf->size; ++i)
949 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
950 pf->width[i], i+pf->firstchar);
951 fprintf(ofp, "};\n\n");
954 /* output struct font struct*/
955 if (pf->offset)
956 sprintf(obuf, "_sysfont_offset,");
957 else
958 sprintf(obuf, "0, /* no encode table */");
960 if (pf->width)
961 sprintf(buf, "_sysfont_width, /* width */");
962 else
963 sprintf(buf, "0, /* fixed width */");
965 fprintf(ofp, "/* Exported structure definition. */\n"
966 "const struct font sysfont = {\n"
967 " %d, /* maxwidth */\n"
968 " %d, /* height */\n"
969 " %d, /* ascent */\n"
970 " %d, /* firstchar */\n"
971 " %d, /* size */\n"
972 " _font_bits, /* bits */\n"
973 " %s /* offset */\n"
974 " %s\n"
975 " %d, /* defaultchar */\n"
976 " %d /* bits_size */\n"
977 "};\n"
978 "#endif /* HAVE_LCD_BITMAP */\n",
979 pf->maxwidth, pf->height,
980 pf->ascent,
981 pf->firstchar,
982 pf->size,
983 obuf,
984 buf,
985 pf->defaultchar,
986 pf->bits_size);
988 return 0;
991 /* generate C header from in-core font*/
992 int gen_h_header(struct font* pf, char *path)
994 FILE *ofp;
995 time_t t = time(0);
996 char buf[256];
997 char hdr1[] = {
998 "/* Generated by convbdf on %s. */\n"
999 "#ifdef HAVE_LCD_BITMAP\n"
1000 "\n"
1001 "/* Font information*/\n"
1002 "#define SYSFONT_NAME %s\n"
1003 "#define SYSFONT_FACENAME %s\n"
1004 "#define SYSFONT_WIDTH %d\n"
1005 "#define SYSFONT_HEIGHT %d\n"
1006 "#define SYSFONT_SIZE %d\n"
1007 "#define SYSFONT_ASCENT %d\n"
1008 "#define SYSFONT_DESCENT %d\n"
1009 "#define SYSFONT_FIRST_CHAR %d\n"
1010 "#define SYSFONT_LAST_CHAR %d\n"
1011 "#define SYSFONT_DEFAULT_CHAR %d\n"
1012 "#define SYSFONT_PROPORTIONAL %d\n"
1013 "#define SYSFONT_COPYRIGHT %s\n"
1014 "#define SYSFONT_BITS_SIZE %d\n"
1015 "\n"
1016 "#endif\n"
1019 ofp = fopen(path, "w");
1020 if (!ofp) {
1021 fprintf(stderr, "Can't create %s\n", path);
1022 return 1;
1025 strcpy(buf, ctime(&t));
1026 buf[strlen(buf)-1] = 0;
1028 fprintf(ofp, hdr1, buf,
1029 pf->name,
1030 pf->facename? pf->facename: "",
1031 pf->maxwidth,
1032 pf->height,
1033 pf->size,
1034 pf->ascent,
1035 pf->descent,
1036 pf->firstchar,
1037 pf->firstchar+pf->size-1,
1038 pf->defaultchar,
1039 pf->width? 1: 0,
1040 pf->copyright? pf->copyright: "",
1041 pf->bits_size);
1043 return 0;
1046 static int writebyte(FILE *fp, unsigned char c)
1048 return putc(c, fp) != EOF;
1051 static int writeshort(FILE *fp, unsigned short s)
1053 putc(s, fp);
1054 return putc(s>>8, fp) != EOF;
1057 static int writeint(FILE *fp, unsigned int l)
1059 putc(l, fp);
1060 putc(l>>8, fp);
1061 putc(l>>16, fp);
1062 return putc(l>>24, fp) != EOF;
1065 static int writestr(FILE *fp, char *str, int count)
1067 return fwrite(str, 1, count, fp) == count;
1070 static int writestrpad(FILE *fp, char *str, int totlen)
1072 int ret;
1074 while (str && *str && totlen > 0) {
1075 if (*str) {
1076 ret = putc(*str++, fp);
1077 --totlen;
1080 while (--totlen >= 0)
1081 ret = putc(' ', fp);
1082 return ret;
1085 /* generate .fnt format file from in-core font*/
1086 int gen_fnt_file(struct font* pf, char *path)
1088 FILE *ofp;
1089 int i;
1090 #ifdef ROTATE
1091 int ofr = 0;
1092 #endif
1094 ofp = fopen(path, "wb");
1095 if (!ofp) {
1096 fprintf(stderr, "Can't create %s\n", path);
1097 return 1;
1100 /* write magic and version #*/
1101 writestr(ofp, VERSION, 4);
1102 #ifndef ROTATE
1103 /* internal font name*/
1104 writestrpad(ofp, pf->name, 64);
1106 /* copyright*/
1107 writestrpad(ofp, pf->copyright, 256);
1108 #endif
1109 /* font info*/
1110 writeshort(ofp, pf->maxwidth);
1111 writeshort(ofp, pf->height);
1112 writeshort(ofp, pf->ascent);
1113 writeshort(ofp, 0);
1114 writeint(ofp, pf->firstchar);
1115 writeint(ofp, pf->defaultchar);
1116 writeint(ofp, pf->size);
1118 /* variable font data sizes*/
1119 writeint(ofp, pf->bits_size); /* # words of bitmap_t*/
1120 writeint(ofp, pf->offset? pf->size: 0); /* # ints of offset*/
1121 writeint(ofp, pf->width? pf->size: 0); /* # bytes of width*/
1122 /* variable font data*/
1123 #ifdef ROTATE
1124 for (i=0; i<pf->size; ++i)
1126 bitmap_t* bits;
1127 int width = pf->width ? pf->width[i] : pf->maxwidth;
1128 int size;
1129 unsigned char bytemap[512];
1131 /* Skip missing glyphs */
1132 if (pf->offset && (pf->offset[i] == (unsigned int)-1))
1133 continue;
1135 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
1137 size = rotleft(bytemap, sizeof(bytemap), bits, width, pf->height);
1138 writestr(ofp, (char *)bytemap, size);
1140 /* update offrot since bits are now in sorted order */
1141 pf->offrot[i] = ofr;
1142 ofr += size;
1145 if ( pf->bits_size < 0xFFDB )
1147 /* bitmap offset is small enough, use unsigned short for offset */
1148 if (ftell(ofp) & 1)
1149 writebyte(ofp, 0); /* pad to 16-bit boundary*/
1151 else
1153 /* bitmap offset is large then 64K, use unsigned int for offset */
1154 while (ftell(ofp) & 3)
1155 writebyte(ofp, 0); /* pad to 32-bit boundary*/
1158 if (pf->offset)
1160 for (i=0; i<pf->size; ++i)
1162 if (pf->offset[i] == (unsigned int)-1) {
1163 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
1165 if ( pf->bits_size < 0xFFDB )
1166 writeshort(ofp, pf->offrot[i]);
1167 else
1168 writeint(ofp, pf->offrot[i]);
1172 if (pf->width)
1173 for (i=0; i<pf->size; ++i)
1174 writebyte(ofp, pf->width[i]);
1175 #else
1176 for (i=0; i<pf->bits_size; ++i)
1177 writeshort(ofp, pf->bits[i]);
1178 if (ftell(ofp) & 2)
1179 writeshort(ofp, 0); /* pad to 32-bit boundary*/
1181 if (pf->offset)
1182 for (i=0; i<pf->size; ++i) {
1183 if (pf->offset[i] == (unsigned int)-1) {
1184 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
1186 writeint(ofp, pf->offset[i]);
1189 if (pf->width)
1190 for (i=0; i<pf->size; ++i)
1191 writebyte(ofp, pf->width[i]);
1192 #endif
1193 fclose(ofp);
1194 return 0;