Document line selector color config file settings (FS#8880 by Justin Hannigan).
[kugel-rb.git] / tools / convbdf.c
blob010e8ee2c2e2a2bc17a2b4f747cd669b2b4e2f94
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 >= (int)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 ((int)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 %ld bytes buffer, needs %d\n",
741 __FILE__, __LINE__, width, height, (unsigned long)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 time_t t = time(0);
789 #ifndef ROTATE
790 int did_syncmsg = 0;
791 bitmap_t *ofs = pf->bits;
792 #endif
793 char buf[256];
794 char obuf[256];
795 char hdr1[] = {
796 "/* Generated by convbdf on %s. */\n"
797 "#include \"font.h\"\n"
798 "#ifdef HAVE_LCD_BITMAP\n"
799 "\n"
800 "/* Font information:\n"
801 " name: %s\n"
802 " facename: %s\n"
803 " w x h: %dx%d\n"
804 " size: %d\n"
805 " ascent: %d\n"
806 " descent: %d\n"
807 " first char: %d (0x%02x)\n"
808 " last char: %d (0x%02x)\n"
809 " default char: %d (0x%02x)\n"
810 " proportional: %s\n"
811 " %s\n"
812 "*/\n"
813 "\n"
814 "/* Font character bitmap data. */\n"
815 "static const unsigned char _font_bits[] = {\n"
818 ofp = fopen(path, "w");
819 if (!ofp) {
820 fprintf(stderr, "Can't create %s\n", path);
821 return 1;
824 strcpy(buf, ctime(&t));
825 buf[strlen(buf)-1] = 0;
827 fprintf(ofp, hdr1, buf,
828 pf->name,
829 pf->facename? pf->facename: "",
830 pf->maxwidth, pf->height,
831 pf->size,
832 pf->ascent, pf->descent,
833 pf->firstchar, pf->firstchar,
834 pf->firstchar+pf->size-1, pf->firstchar+pf->size-1,
835 pf->defaultchar, pf->defaultchar,
836 pf->width? "yes": "no",
837 pf->copyright? pf->copyright: "");
839 /* generate bitmaps*/
840 for (i=0; i<pf->size; ++i) {
841 int x;
842 int bitcount = 0;
843 int width = pf->width ? pf->width[i] : pf->maxwidth;
844 int height = pf->height;
845 bitmap_t *bits;
846 bitmap_t bitvalue=0;
848 /* Skip missing glyphs */
849 if (pf->offset && (pf->offset[i] == (unsigned int)-1))
850 continue;
852 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
854 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d",
855 i+pf->firstchar, i+pf->firstchar, width);
857 if (gen_map) {
858 fprintf(ofp, "\n +");
859 for (x=0; x<width; ++x) fprintf(ofp, "-");
860 fprintf(ofp, "+\n");
862 x = 0;
863 while (height > 0) {
864 if (x == 0) fprintf(ofp, " |");
866 if (bitcount <= 0) {
867 bitcount = BITMAP_BITSPERIMAGE;
868 bitvalue = *bits++;
871 fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " ");
873 bitvalue = BITMAP_SHIFTBIT(bitvalue);
874 --bitcount;
875 if (++x == width) {
876 fprintf(ofp, "|\n");
877 --height;
878 x = 0;
879 bitcount = 0;
882 fprintf(ofp, " +");
883 for (x=0; x<width; ++x)
884 fprintf(ofp, "-");
885 fprintf(ofp, "+ */\n");
887 else
888 fprintf(ofp, " */\n");
890 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
891 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
893 unsigned char bytemap[512];
894 int y8, ix=0;
896 int size = rotleft(bytemap, sizeof(bytemap), bits, width,
897 pf->height);
898 for (y8=0; y8<pf->height; y8+=8) /* column rows */
900 for (x=0; x<width; x++) {
901 fprintf(ofp, "0x%02x, ", bytemap[ix]);
902 ix++;
904 fprintf(ofp, "\n");
907 /* update offrot since bits are now in sorted order */
908 pf->offrot[i] = ofr;
909 ofr += size;
912 #else
913 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) {
914 fprintf(ofp, "0x%04x,\n", *bits);
915 if (!did_syncmsg && *bits++ != *ofs++) {
916 fprintf(stderr, "Warning: found encoding values in non-sorted order (not an error).\n");
917 did_syncmsg = 1;
920 #endif
922 fprintf(ofp, "};\n\n");
924 if (pf->offset) {
925 /* output offset table*/
926 fprintf(ofp, "/* Character->glyph mapping. */\n"
927 "static const unsigned short _sysfont_offset[] = {\n");
929 for (i=0; i<pf->size; ++i) {
930 if (pf->offset[i] == (unsigned int)-1) {
931 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
932 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
934 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
935 #ifdef ROTATE
936 pf->offrot[i], i+pf->firstchar);
937 #else
938 pf->offset[i], i+pf->firstchar);
939 #endif
941 fprintf(ofp, "};\n\n");
944 /* output width table for proportional fonts*/
945 if (pf->width) {
946 fprintf(ofp, "/* Character width data. */\n"
947 "static const unsigned char _sysfont_width[] = {\n");
949 for (i=0; i<pf->size; ++i)
950 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
951 pf->width[i], i+pf->firstchar);
952 fprintf(ofp, "};\n\n");
955 /* output struct font struct*/
956 if (pf->offset)
957 sprintf(obuf, "_sysfont_offset,");
958 else
959 sprintf(obuf, "0, /* no encode table */");
961 if (pf->width)
962 sprintf(buf, "_sysfont_width, /* width */");
963 else
964 sprintf(buf, "0, /* fixed width */");
966 fprintf(ofp, "/* Exported structure definition. */\n"
967 "const struct font sysfont = {\n"
968 " %d, /* maxwidth */\n"
969 " %d, /* height */\n"
970 " %d, /* ascent */\n"
971 " %d, /* firstchar */\n"
972 " %d, /* size */\n"
973 " _font_bits, /* bits */\n"
974 " %s /* offset */\n"
975 " %s\n"
976 " %d, /* defaultchar */\n"
977 " %d /* bits_size */\n"
978 "};\n"
979 "#endif /* HAVE_LCD_BITMAP */\n",
980 pf->maxwidth, pf->height,
981 pf->ascent,
982 pf->firstchar,
983 pf->size,
984 obuf,
985 buf,
986 pf->defaultchar,
987 pf->bits_size);
989 return 0;
992 /* generate C header from in-core font*/
993 int gen_h_header(struct font* pf, char *path)
995 FILE *ofp;
996 time_t t = time(0);
997 char buf[256];
998 char *hdr1 =
999 "/* Generated by convbdf on %s. */\n"
1000 "#ifdef HAVE_LCD_BITMAP\n"
1001 "\n"
1002 "/* Font information*/\n"
1003 "#define SYSFONT_NAME %s\n"
1004 "#define SYSFONT_FACENAME %s\n"
1005 "#define SYSFONT_WIDTH %d\n"
1006 "#define SYSFONT_HEIGHT %d\n"
1007 "#define SYSFONT_SIZE %d\n"
1008 "#define SYSFONT_ASCENT %d\n";
1009 char *hdr2 =
1010 "#define SYSFONT_DESCENT %d\n"
1011 "#define SYSFONT_FIRST_CHAR %d\n"
1012 "#define SYSFONT_LAST_CHAR %d\n"
1013 "#define SYSFONT_DEFAULT_CHAR %d\n"
1014 "#define SYSFONT_PROPORTIONAL %d\n"
1015 "#define SYSFONT_COPYRIGHT %s\n"
1016 "#define SYSFONT_BITS_SIZE %d\n"
1017 "\n"
1018 "#endif\n";
1020 ofp = fopen(path, "w");
1021 if (!ofp) {
1022 fprintf(stderr, "Can't create %s\n", path);
1023 return 1;
1026 strcpy(buf, ctime(&t));
1027 buf[strlen(buf)-1] = 0;
1029 fprintf(ofp, hdr1, buf,
1030 pf->name,
1031 pf->facename? pf->facename: "",
1032 pf->maxwidth,
1033 pf->height,
1034 pf->size,
1035 pf->ascent);
1037 fprintf(ofp, hdr2,
1038 pf->descent,
1039 pf->firstchar,
1040 pf->firstchar+pf->size-1,
1041 pf->defaultchar,
1042 pf->width? 1: 0,
1043 pf->copyright? pf->copyright: "",
1044 pf->bits_size);
1046 return 0;
1049 static int writebyte(FILE *fp, unsigned char c)
1051 return putc(c, fp) != EOF;
1054 static int writeshort(FILE *fp, unsigned short s)
1056 putc(s, fp);
1057 return putc(s>>8, fp) != EOF;
1060 static int writeint(FILE *fp, unsigned int l)
1062 putc(l, fp);
1063 putc(l>>8, fp);
1064 putc(l>>16, fp);
1065 return putc(l>>24, fp) != EOF;
1068 static int writestr(FILE *fp, char *str, int count)
1070 return (int)fwrite(str, 1, count, fp) == count;
1073 #ifndef ROTATE
1074 static int writestrpad(FILE *fp, char *str, int totlen)
1076 int ret;
1078 while (str && *str && totlen > 0) {
1079 if (*str) {
1080 ret = putc(*str++, fp);
1081 --totlen;
1084 while (--totlen >= 0)
1085 ret = putc(' ', fp);
1086 return ret;
1088 #endif
1090 /* generate .fnt format file from in-core font*/
1091 int gen_fnt_file(struct font* pf, char *path)
1093 FILE *ofp;
1094 int i;
1095 #ifdef ROTATE
1096 int ofr = 0;
1097 #endif
1099 ofp = fopen(path, "wb");
1100 if (!ofp) {
1101 fprintf(stderr, "Can't create %s\n", path);
1102 return 1;
1105 /* write magic and version #*/
1106 writestr(ofp, VERSION, 4);
1107 #ifndef ROTATE
1108 /* internal font name*/
1109 writestrpad(ofp, pf->name, 64);
1111 /* copyright*/
1112 writestrpad(ofp, pf->copyright, 256);
1113 #endif
1114 /* font info*/
1115 writeshort(ofp, pf->maxwidth);
1116 writeshort(ofp, pf->height);
1117 writeshort(ofp, pf->ascent);
1118 writeshort(ofp, 0);
1119 writeint(ofp, pf->firstchar);
1120 writeint(ofp, pf->defaultchar);
1121 writeint(ofp, pf->size);
1123 /* variable font data sizes*/
1124 writeint(ofp, pf->bits_size); /* # words of bitmap_t*/
1125 writeint(ofp, pf->offset? pf->size: 0); /* # ints of offset*/
1126 writeint(ofp, pf->width? pf->size: 0); /* # bytes of width*/
1127 /* variable font data*/
1128 #ifdef ROTATE
1129 for (i=0; i<pf->size; ++i)
1131 bitmap_t* bits;
1132 int width = pf->width ? pf->width[i] : pf->maxwidth;
1133 int size;
1134 unsigned char bytemap[512];
1136 /* Skip missing glyphs */
1137 if (pf->offset && (pf->offset[i] == (unsigned int)-1))
1138 continue;
1140 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1142 size = rotleft(bytemap, sizeof(bytemap), bits, width, pf->height);
1143 writestr(ofp, (char *)bytemap, size);
1145 /* update offrot since bits are now in sorted order */
1146 pf->offrot[i] = ofr;
1147 ofr += size;
1150 if ( pf->bits_size < 0xFFDB )
1152 /* bitmap offset is small enough, use unsigned short for offset */
1153 if (ftell(ofp) & 1)
1154 writebyte(ofp, 0); /* pad to 16-bit boundary*/
1156 else
1158 /* bitmap offset is large then 64K, use unsigned int for offset */
1159 while (ftell(ofp) & 3)
1160 writebyte(ofp, 0); /* pad to 32-bit boundary*/
1163 if (pf->offset)
1165 for (i=0; i<pf->size; ++i)
1167 if (pf->offset[i] == (unsigned int)-1) {
1168 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
1170 if ( pf->bits_size < 0xFFDB )
1171 writeshort(ofp, pf->offrot[i]);
1172 else
1173 writeint(ofp, pf->offrot[i]);
1177 if (pf->width)
1178 for (i=0; i<pf->size; ++i)
1179 writebyte(ofp, pf->width[i]);
1180 #else
1181 for (i=0; i<pf->bits_size; ++i)
1182 writeshort(ofp, pf->bits[i]);
1183 if (ftell(ofp) & 2)
1184 writeshort(ofp, 0); /* pad to 32-bit boundary*/
1186 if (pf->offset)
1187 for (i=0; i<pf->size; ++i) {
1188 if (pf->offset[i] == (unsigned int)-1) {
1189 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
1191 writeint(ofp, pf->offset[i]);
1194 if (pf->width)
1195 for (i=0; i<pf->size; ++i)
1196 writebyte(ofp, pf->width[i]);
1197 #endif
1198 fclose(ofp);
1199 return 0;