Don't reset everything when (un)sleeping the TEA5767. Fixes FS#6162.
[Rockbox.git] / tools / convbdf.c
blob6dcd2d8394aca948c09c122672367a1d4576e461
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_fnt = 0;
68 int gen_map = 1;
69 int start_char = 0;
70 int limit_char = 65535;
71 int oflag = 0;
72 char outfile[256];
74 void usage(void);
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);
88 void
89 usage(void)
91 char help[] = {
92 "Usage: convbdf [options] [input-files]\n"
93 " convbdf [options] [-o output-file] [single-input-file]\n"
94 "Options:\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)
108 char *p;
109 char **av;
110 int ac;
112 ac = *pac;
113 av = *pav;
114 while (ac > 0 && av[0][0] == '-') {
115 p = &av[0][1];
116 while( *p)
117 switch(*p++) {
118 case ' ': /* multiple -args on av[]*/
119 while( *p && *p == ' ')
120 p++;
121 if( *p++ != '-') /* next option must have dash*/
122 p = "";
123 break; /* proceed to next option*/
124 case 'c': /* generate .c output*/
125 gen_c = 1;
126 break;
127 case 'f': /* generate .fnt output*/
128 gen_fnt = 1;
129 break;
130 case 'n': /* don't gen bitmap comments*/
131 gen_map = 0;
132 break;
133 case 'o': /* set output file*/
134 oflag = 1;
135 if (*p) {
136 strcpy(outfile, p);
137 while (*p && *p != ' ')
138 p++;
140 else {
141 av++; ac--;
142 if (ac > 0)
143 strcpy(outfile, av[0]);
145 break;
146 case 'l': /* set encoding limit*/
147 if (*p) {
148 limit_char = atoi(p);
149 while (*p && *p != ' ')
150 p++;
152 else {
153 av++; ac--;
154 if (ac > 0)
155 limit_char = atoi(av[0]);
157 break;
158 case 's': /* set encoding start*/
159 if (*p) {
160 start_char = atoi(p);
161 while (*p && *p != ' ')
162 p++;
164 else {
165 av++; ac--;
166 if (ac > 0)
167 start_char = atoi(av[0]);
169 break;
170 default:
171 fprintf(stderr, "Unknown option ignored: %c\r\n", *(p-1));
173 ++av; --ac;
175 *pac = ac;
176 *pav = av;
179 /* remove directory prefix and file suffix from full path*/
180 char *basename(char *path)
182 char *p, *b;
183 static char base[256];
185 /* remove prepended path and extension*/
186 b = path;
187 for (p=path; *p; ++p) {
188 if (*p == '/')
189 b = p + 1;
191 strcpy(base, b);
192 for (p=base; *p; ++p) {
193 if (*p == '.') {
194 *p = 0;
195 break;
198 return base;
201 int convbdf(char *path)
203 struct font* pf;
204 int ret = 0;
206 pf = bdf_read_font(path);
207 if (!pf)
208 exit(1);
210 if (gen_c) {
211 if (!oflag) {
212 strcpy(outfile, basename(path));
213 strcat(outfile, ".c");
215 ret |= gen_c_source(pf, outfile);
218 if (gen_fnt) {
219 if (!oflag) {
220 strcpy(outfile, basename(path));
221 strcat(outfile, ".fnt");
223 ret |= gen_fnt_file(pf, outfile);
226 free_font(pf);
227 return ret;
230 int main(int ac, char **av)
232 int ret = 0;
234 ++av; --ac; /* skip av[0]*/
235 getopts(&ac, &av); /* read command line options*/
237 if (ac < 1 || (!gen_c && !gen_fnt)) {
238 usage();
239 exit(1);
241 if (oflag) {
242 if (ac > 1 || (gen_c && gen_fnt)) {
243 usage();
244 exit(1);
248 while (ac > 0) {
249 ret |= convbdf(av[0]);
250 ++av; --ac;
253 exit(ret);
256 /* free font structure*/
257 void free_font(struct font* pf)
259 if (!pf)
260 return;
261 if (pf->name)
262 free(pf->name);
263 if (pf->facename)
264 free(pf->facename);
265 if (pf->bits)
266 free(pf->bits);
267 if (pf->offset)
268 free(pf->offset);
269 if (pf->offrot)
270 free(pf->offrot);
271 if (pf->width)
272 free(pf->width);
273 free(pf);
276 /* build incore structure from .bdf file*/
277 struct font* bdf_read_font(char *path)
279 FILE *fp;
280 struct font* pf;
282 fp = fopen(path, "rb");
283 if (!fp) {
284 fprintf(stderr, "Error opening file: %s\n", path);
285 return NULL;
288 pf = (struct font*)calloc(1, sizeof(struct font));
289 if (!pf)
290 goto errout;
292 pf->name = strdup(basename(path));
294 if (!bdf_read_header(fp, pf)) {
295 fprintf(stderr, "Error reading font header\n");
296 goto errout;
299 if (!bdf_read_bitmaps(fp, pf)) {
300 fprintf(stderr, "Error reading font bitmaps\n");
301 goto errout;
304 fclose(fp);
305 return pf;
307 errout:
308 fclose(fp);
309 free_font(pf);
310 return NULL;
313 /* read bdf font header information, return 0 on error*/
314 int bdf_read_header(FILE *fp, struct font* pf)
316 int encoding;
317 int nchars, maxwidth;
318 int firstchar = 65535;
319 int lastchar = -1;
320 char buf[256];
321 char facename[256];
322 char copyright[256];
324 /* set certain values to errors for later error checking*/
325 pf->defaultchar = -1;
326 pf->ascent = -1;
327 pf->descent = -1;
329 for (;;) {
330 if (!bdf_getline(fp, buf, sizeof(buf))) {
331 fprintf(stderr, "Error: EOF on file\n");
332 return 0;
334 if (isprefix(buf, "FONT ")) { /* not required*/
335 if (sscanf(buf, "FONT %[^\n]", facename) != 1) {
336 fprintf(stderr, "Error: bad 'FONT'\n");
337 return 0;
339 pf->facename = strdup(facename);
340 continue;
342 if (isprefix(buf, "COPYRIGHT ")) { /* not required*/
343 if (sscanf(buf, "COPYRIGHT \"%[^\"]", copyright) != 1) {
344 fprintf(stderr, "Error: bad 'COPYRIGHT'\n");
345 return 0;
347 pf->copyright = strdup(copyright);
348 continue;
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");
353 return 0;
356 if (isprefix(buf, "FONT_DESCENT ")) {
357 if (sscanf(buf, "FONT_DESCENT %d", &pf->descent) != 1) {
358 fprintf(stderr, "Error: bad 'FONT_DESCENT'\n");
359 return 0;
361 continue;
363 if (isprefix(buf, "FONT_ASCENT ")) {
364 if (sscanf(buf, "FONT_ASCENT %d", &pf->ascent) != 1) {
365 fprintf(stderr, "Error: bad 'FONT_ASCENT'\n");
366 return 0;
368 continue;
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");
374 return 0;
376 continue;
378 if (isprefix(buf, "CHARS ")) {
379 if (sscanf(buf, "CHARS %d", &nchars) != 1) {
380 fprintf(stderr, "Error: bad 'CHARS'\n");
381 return 0;
383 continue;
387 * Reading ENCODING is necessary to get firstchar/lastchar
388 * which is needed to pre-calculate our offset and widths
389 * array sizes.
391 if (isprefix(buf, "ENCODING ")) {
392 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
393 fprintf(stderr, "Error: bad 'ENCODING'\n");
394 return 0;
396 if (encoding >= 0 &&
397 encoding <= limit_char &&
398 encoding >= start_char) {
400 if (firstchar > encoding)
401 firstchar = encoding;
402 if (lastchar < encoding)
403 lastchar = encoding;
405 continue;
407 if (strequal(buf, "ENDFONT"))
408 break;
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");
414 return 0;
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;*/
431 maxwidth = pf->fbbw;
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");
444 return 0;
447 fprintf(stderr, "Header parsed\n");
448 return 1;
451 /* read bdf font bitmaps, return 0 on error*/
452 int bdf_read_bitmaps(FILE *fp, struct font* pf)
454 int ofs = 0;
455 int ofr = 0;
456 int maxwidth = 0;
457 int i, k, encoding, width;
458 int bbw, bbh, bbx, bby;
459 int proportional = 0;
460 int encodetable = 0;
461 int l;
462 char buf[256];
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)
469 pf->offset[i] = -1;
471 for (;;) {
472 if (!bdf_getline(fp, buf, sizeof(buf))) {
473 fprintf(stderr, "Error: EOF on file\n");
474 return 0;
476 if (isprefix(buf, "STARTCHAR")) {
477 encoding = width = bbw = bbh = bbx = bby = -1;
478 continue;
480 if (isprefix(buf, "ENCODING ")) {
481 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
482 fprintf(stderr, "Error: bad 'ENCODING'\n");
483 return 0;
485 if (encoding < start_char || encoding > limit_char)
486 encoding = -1;
487 continue;
489 if (isprefix(buf, "DWIDTH ")) {
490 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
491 fprintf(stderr, "Error: bad 'DWIDTH'\n");
492 return 0;
494 /* use font boundingbox width if DWIDTH <= 0*/
495 if (width <= 0)
496 width = pf->fbbw - pf->fbbx;
497 continue;
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");
502 return 0;
504 continue;
506 if (strequal(buf, "BITMAP") || strequal(buf, "BITMAP ")) {
507 bitmap_t *ch_bitmap = pf->bits + ofs;
508 int ch_words;
510 if (encoding < 0)
511 continue;
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",
516 encoding, encoding);
517 continue;
519 pf->offset[encoding-pf->firstchar] = ofs;
520 pf->offrot[encoding-pf->firstchar] = ofr;
522 /* calc char width*/
523 if (bbx < 0) {
524 width -= bbx;
525 /*if (width > maxwidth)
526 width = maxwidth;*/
527 bbx = 0;
529 if (width > maxwidth)
530 maxwidth = width;
531 pf->width[encoding-pf->firstchar] = width;
533 /* clear bitmap*/
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)
540 /* read bitmaps*/
541 for (i=0; ; ++i) {
542 int hexnibbles;
544 if (!bdf_getline(fp, buf, sizeof(buf))) {
545 fprintf(stderr, "Error: EOF reading BITMAP data\n");
546 return 0;
548 if (isprefix(buf, "ENDCHAR"))
549 break;
551 hexnibbles = strlen(buf);
552 for (k=0; k<ch_words; ++k) {
553 int ndx = k * BITMAP_NIBBLES;
554 int padnibbles = hexnibbles - ndx;
555 bitmap_t value;
557 if (padnibbles <= 0)
558 break;
559 if (padnibbles >= BITMAP_NIBBLES)
560 padnibbles = 0;
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) |=
567 value >> bbx;
568 /* handle overflow into next image word*/
569 if (bbx) {
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);
579 continue;
581 if (strequal(buf, "ENDFONT"))
582 break;
585 /* set max width*/
586 pf->maxwidth = maxwidth;
588 /* change unused 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->width[i] = pf->width[defchar];
596 /* determine whether font doesn't require encode table*/
597 #ifdef ROTATE
598 l = 0;
599 for (i=0; i<pf->size; ++i) {
600 if (pf->offrot[i] != l) {
601 encodetable = 1;
602 break;
604 l += pf->maxwidth * ((pf->height + 7) / 8);
606 #else
607 l = 0;
608 for (i=0; i<pf->size; ++i) {
609 if (pf->offset[i] != l) {
610 encodetable = 1;
611 break;
613 l += BITMAP_WORDS(pf->width[i]) * pf->height;
615 #endif
616 if (!encodetable) {
617 free(pf->offset);
618 pf->offset = NULL;
621 /* determine whether font is fixed-width*/
622 for (i=0; i<pf->size; ++i) {
623 if (pf->width[i] != maxwidth) {
624 proportional = 1;
625 break;
628 if (!proportional) {
629 free(pf->width);
630 pf->width = NULL;
633 /* reallocate bits array to actual bits used*/
634 if (ofs < pf->bits_size) {
635 pf->bits = realloc(pf->bits, ofs * sizeof(bitmap_t));
636 pf->bits_size = ofs;
638 else {
639 if (ofs > pf->bits_size) {
640 fprintf(stderr, "Warning: DWIDTH spec > max FONTBOUNDINGBOX\n");
641 if (ofs > pf->bits_size+EXTRA) {
642 fprintf(stderr, "Error: Not enough bits initially allocated\n");
643 return 0;
645 pf->bits_size = ofs;
649 #ifdef ROTATE
650 pf->bits_size = ofr; /* always update, rotated is smaller */
651 #endif
653 return 1;
656 /* read the next non-comment line, returns buf or NULL if EOF*/
657 char *bdf_getline(FILE *fp, char *buf, int len)
659 int c;
660 char *b;
662 for (;;) {
663 b = buf;
664 while ((c = getc(fp)) != EOF) {
665 if (c == '\r')
666 continue;
667 if (c == '\n')
668 break;
669 if (b - buf >= (len - 1))
670 break;
671 *b++ = c;
673 *b = '\0';
674 if (c == EOF && b == buf)
675 return NULL;
676 if (b != buf && !isprefix(buf, "COMMENT"))
677 break;
679 return buf;
682 /* return hex value of portion of buffer*/
683 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2)
685 bitmap_t val = 0;
686 int i, c;
688 for (i=ndx1; i<=ndx2; ++i) {
689 c = buf[i];
690 if (c >= '0' && c <= '9')
691 c -= '0';
692 else
693 if (c >= 'A' && c <= 'F')
694 c = c - 'A' + 10;
695 else
696 if (c >= 'a' && c <= 'f')
697 c = c - 'a' + 10;
698 else
699 c = 0;
700 val = (val << 4) | c;
702 return val;
706 * Take an bitmap_t bitmap and convert to Rockbox format.
707 * Used for converting font glyphs for the time being.
708 * Can use for standard X11 and Win32 images as well.
709 * See format description in lcd-recorder.c
711 * Doing it this way keeps fonts in standard formats,
712 * as well as keeping Rockbox hw bitmap format.
714 int rotleft(unsigned char *dst, bitmap_t *src, unsigned int width,
715 unsigned int height)
717 unsigned int i,j;
718 unsigned int src_words; /* # words of input image*/
719 unsigned int dst_mask; /* bit mask for destination */
720 bitmap_t src_mask; /* bit mask for source */
722 /* calc words of input image*/
723 src_words = BITMAP_WORDS(width) * height;
725 /* clear background*/
726 memset(dst, 0, ((height + 7) / 8) * width);
728 dst_mask = 1;
730 for (i=0; i < src_words; i++) {
732 /* calc src input bit*/
733 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
735 /* for each input column...*/
736 for(j=0; j < width; j++) {
738 if (src_mask == 0) /* input word done? */
740 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
741 i++; /* next input word */
744 /* if set in input, set in rotated output */
745 if (src[i] & src_mask)
746 dst[j] |= dst_mask;
748 src_mask >>= 1; /* next input bit */
751 dst_mask <<= 1; /* next output bit (row) */
752 if (dst_mask > (1 << 7)) /* output bit > 7? */
754 dst_mask = 1;
755 dst += width; /* next output byte row */
758 return ((height + 7) / 8) * width; /* return result size in bytes */
762 /* generate C source from in-core font*/
763 int gen_c_source(struct font* pf, char *path)
765 FILE *ofp;
766 int i, ofr = 0;
767 int did_syncmsg = 0;
768 time_t t = time(0);
769 bitmap_t *ofs = pf->bits;
770 char buf[256];
771 char obuf[256];
772 char hdr1[] = {
773 "/* Generated by convbdf on %s. */\n"
774 "#include \"font.h\"\n"
775 "#ifdef HAVE_LCD_BITMAP\n"
776 "\n"
777 "/* Font information:\n"
778 " name: %s\n"
779 " facename: %s\n"
780 " w x h: %dx%d\n"
781 " size: %d\n"
782 " ascent: %d\n"
783 " descent: %d\n"
784 " first char: %d (0x%02x)\n"
785 " last char: %d (0x%02x)\n"
786 " default char: %d (0x%02x)\n"
787 " proportional: %s\n"
788 " %s\n"
789 "*/\n"
790 "\n"
791 "/* Font character bitmap data. */\n"
792 "static const unsigned char _font_bits[] = {\n"
795 ofp = fopen(path, "w");
796 if (!ofp) {
797 fprintf(stderr, "Can't create %s\n", path);
798 return 1;
801 strcpy(buf, ctime(&t));
802 buf[strlen(buf)-1] = 0;
804 fprintf(ofp, hdr1, buf,
805 pf->name,
806 pf->facename? pf->facename: "",
807 pf->maxwidth, pf->height,
808 pf->size,
809 pf->ascent, pf->descent,
810 pf->firstchar, pf->firstchar,
811 pf->firstchar+pf->size-1, pf->firstchar+pf->size-1,
812 pf->defaultchar, pf->defaultchar,
813 pf->width? "yes": "no",
814 pf->copyright? pf->copyright: "");
816 /* generate bitmaps*/
817 for (i=0; i<pf->size; ++i) {
818 int x;
819 int bitcount = 0;
820 int width = pf->width ? pf->width[i] : pf->maxwidth;
821 int height = pf->height;
822 bitmap_t *bits;
823 bitmap_t bitvalue;
825 /* Skip missing glyphs */
826 if (pf->offset && (pf->offset[i] == (unsigned int)-1))
827 continue;
829 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
831 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d",
832 i+pf->firstchar, i+pf->firstchar, width);
834 if (gen_map) {
835 fprintf(ofp, "\n +");
836 for (x=0; x<width; ++x) fprintf(ofp, "-");
837 fprintf(ofp, "+\n");
839 x = 0;
840 while (height > 0) {
841 if (x == 0) fprintf(ofp, " |");
843 if (bitcount <= 0) {
844 bitcount = BITMAP_BITSPERIMAGE;
845 bitvalue = *bits++;
848 fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " ");
850 bitvalue = BITMAP_SHIFTBIT(bitvalue);
851 --bitcount;
852 if (++x == width) {
853 fprintf(ofp, "|\n");
854 --height;
855 x = 0;
856 bitcount = 0;
859 fprintf(ofp, " +");
860 for (x=0; x<width; ++x)
861 fprintf(ofp, "-");
862 fprintf(ofp, "+ */\n");
864 else
865 fprintf(ofp, " */\n");
867 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
868 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
870 unsigned char bytemap[256];
871 int y8, ix=0;
873 int size = rotleft(bytemap, bits, width, pf->height);
874 for (y8=0; y8<pf->height; y8+=8) /* column rows */
876 for (x=0; x<width; x++) {
877 fprintf(ofp, "0x%02x, ", bytemap[ix]);
878 ix++;
880 fprintf(ofp, "\n");
883 /* update offrot since bits are now in sorted order */
884 pf->offrot[i] = ofr;
885 ofr += size;
888 #else
889 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) {
890 fprintf(ofp, "0x%04x,\n", *bits);
891 if (!did_syncmsg && *bits++ != *ofs++) {
892 fprintf(stderr, "Warning: found encoding values in non-sorted order (not an error).\n");
893 did_syncmsg = 1;
896 #endif
898 fprintf(ofp, "};\n\n");
900 if (pf->offset) {
901 /* output offset table*/
902 fprintf(ofp, "/* Character->glyph mapping. */\n"
903 "static const unsigned short _sysfont_offset[] = {\n");
905 for (i=0; i<pf->size; ++i) {
906 if (pf->offset[i] == (unsigned int)-1) {
907 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
908 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
910 fprintf(ofp, " %ld,\t/* (0x%02x) */\n",
911 #ifdef ROTATE
912 pf->offrot[i], i+pf->firstchar);
913 #else
914 pf->offset[i], i+pf->firstchar);
915 #endif
917 fprintf(ofp, "};\n\n");
920 /* output width table for proportional fonts*/
921 if (pf->width) {
922 fprintf(ofp, "/* Character width data. */\n"
923 "static const unsigned char _sysfont_width[] = {\n");
925 for (i=0; i<pf->size; ++i)
926 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
927 pf->width[i], i+pf->firstchar);
928 fprintf(ofp, "};\n\n");
931 /* output struct font struct*/
932 if (pf->offset)
933 sprintf(obuf, "_sysfont_offset,");
934 else
935 sprintf(obuf, "0, /* no encode table */");
937 if (pf->width)
938 sprintf(buf, "_sysfont_width, /* width */");
939 else
940 sprintf(buf, "0, /* fixed width */");
942 fprintf(ofp, "/* Exported structure definition. */\n"
943 "const struct font sysfont = {\n"
944 " %d, /* maxwidth */\n"
945 " %d, /* height */\n"
946 " %d, /* ascent */\n"
947 " %d, /* firstchar */\n"
948 " %d, /* size */\n"
949 " _font_bits, /* bits */\n"
950 " %s /* offset */\n"
951 " %s\n"
952 " %d, /* defaultchar */\n"
953 " %d /* bits_size */\n"
954 "};\n"
955 "#endif /* HAVE_LCD_BITMAP */\n",
956 pf->maxwidth, pf->height,
957 pf->ascent,
958 pf->firstchar,
959 pf->size,
960 obuf,
961 buf,
962 pf->defaultchar,
963 pf->bits_size);
965 return 0;
968 static int writebyte(FILE *fp, unsigned char c)
970 return putc(c, fp) != EOF;
973 static int writeshort(FILE *fp, unsigned short s)
975 putc(s, fp);
976 return putc(s>>8, fp) != EOF;
979 static int writeint(FILE *fp, unsigned int l)
981 putc(l, fp);
982 putc(l>>8, fp);
983 putc(l>>16, fp);
984 return putc(l>>24, fp) != EOF;
987 static int writestr(FILE *fp, char *str, int count)
989 return fwrite(str, 1, count, fp) == count;
992 static int writestrpad(FILE *fp, char *str, int totlen)
994 int ret;
996 while (str && *str && totlen > 0) {
997 if (*str) {
998 ret = putc(*str++, fp);
999 --totlen;
1002 while (--totlen >= 0)
1003 ret = putc(' ', fp);
1004 return ret;
1007 /* generate .fnt format file from in-core font*/
1008 int gen_fnt_file(struct font* pf, char *path)
1010 FILE *ofp;
1011 int i;
1012 #ifdef ROTATE
1013 int ofr = 0;
1014 #endif
1016 ofp = fopen(path, "wb");
1017 if (!ofp) {
1018 fprintf(stderr, "Can't create %s\n", path);
1019 return 1;
1022 /* write magic and version #*/
1023 writestr(ofp, VERSION, 4);
1024 #ifndef ROTATE
1025 /* internal font name*/
1026 writestrpad(ofp, pf->name, 64);
1028 /* copyright*/
1029 writestrpad(ofp, pf->copyright, 256);
1030 #endif
1031 /* font info*/
1032 writeshort(ofp, pf->maxwidth);
1033 writeshort(ofp, pf->height);
1034 writeshort(ofp, pf->ascent);
1035 writeshort(ofp, 0);
1036 writeint(ofp, pf->firstchar);
1037 writeint(ofp, pf->defaultchar);
1038 writeint(ofp, pf->size);
1040 /* variable font data sizes*/
1041 writeint(ofp, pf->bits_size); /* # words of bitmap_t*/
1042 writeint(ofp, pf->offset? pf->size: 0); /* # ints of offset*/
1043 writeint(ofp, pf->width? pf->size: 0); /* # bytes of width*/
1044 /* variable font data*/
1045 #ifdef ROTATE
1046 for (i=0; i<pf->size; ++i)
1048 bitmap_t* bits;
1049 int width = pf->width ? pf->width[i] : pf->maxwidth;
1050 int size;
1051 unsigned char bytemap[256];
1053 /* Skip missing glyphs */
1054 if (pf->offset && (pf->offset[i] == (unsigned int)-1))
1055 continue;
1057 bits = pf->bits + (pf->offset? pf->offset[i]: (pf->height * i));
1059 size = rotleft(bytemap, bits, width, pf->height);
1060 writestr(ofp, (char *)bytemap, size);
1062 /* update offrot since bits are now in sorted order */
1063 pf->offrot[i] = ofr;
1064 ofr += size;
1067 if ( pf->bits_size < 0xFFDB )
1069 /* bitmap offset is small enough, use unsigned short for offset */
1070 if (ftell(ofp) & 1)
1071 writebyte(ofp, 0); /* pad to 16-bit boundary*/
1073 else
1075 /* bitmap offset is large then 64K, use unsigned int for offset */
1076 while (ftell(ofp) & 3)
1077 writebyte(ofp, 0); /* pad to 32-bit boundary*/
1080 if (pf->offset)
1082 for (i=0; i<pf->size; ++i)
1084 if (pf->offset[i] == (unsigned int)-1) {
1085 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
1087 if ( pf->bits_size < 0xFFDB )
1088 writeshort(ofp, pf->offrot[i]);
1089 else
1090 writeint(ofp, pf->offrot[i]);
1094 if (pf->width)
1095 for (i=0; i<pf->size; ++i)
1096 writebyte(ofp, pf->width[i]);
1097 #else
1098 for (i=0; i<pf->bits_size; ++i)
1099 writeshort(ofp, pf->bits[i]);
1100 if (ftell(ofp) & 2)
1101 writeshort(ofp, 0); /* pad to 32-bit boundary*/
1103 if (pf->offset)
1104 for (i=0; i<pf->size; ++i) {
1105 if (pf->offset[i] == (unsigned int)-1) {
1106 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
1108 writeint(ofp, pf->offset[i]);
1111 if (pf->width)
1112 for (i=0; i<pf->size; ++i)
1113 writebyte(ofp, pf->width[i]);
1114 #endif
1115 fclose(ofp);
1116 return 0;