i.MX31: Issue some NOP's immediately after MCR WFI to prevent premature execution...
[kugel-rb.git] / tools / convbdf.c
blobd6f3da8fa4605f6e65ed7987258310f2f1cde7e4
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 <stdarg.h>
14 #include <time.h>
16 #define ROTATE /* define this for the new, rotated format */
18 /* BEGIN font.h*/
19 /* loadable font magic and version #*/
20 #ifdef ROTATE
21 #define VERSION "RB12" /* newer version */
22 #else
23 #define VERSION "RB11"
24 #endif
27 * bitmap_t helper macros
29 typedef unsigned short bitmap_t; /* bitmap image unit size*/
31 /* Number of words to hold a pixel line of width x pixels */
32 #define BITMAP_BITSPERIMAGE (sizeof(bitmap_t) * 8)
33 #define BITMAP_WORDS(x) (((x)+BITMAP_BITSPERIMAGE-1)/BITMAP_BITSPERIMAGE)
34 #define BITMAP_BYTES(x) (BITMAP_WORDS(x)*sizeof(bitmap_t))
35 #define BITMAP_BITVALUE(n) ((bitmap_t) (((bitmap_t) 1) << (n)))
36 #define BITMAP_FIRSTBIT (BITMAP_BITVALUE(BITMAP_BITSPERIMAGE - 1))
37 #define BITMAP_TESTBIT(m) ((m) & BITMAP_FIRSTBIT)
38 #define BITMAP_SHIFTBIT(m) ((bitmap_t) ((m) << 1))
41 /* builtin C-based proportional/fixed font structure */
42 /* based on The Microwindows Project http://microwindows.org */
43 struct font {
44 int maxwidth; /* max width in pixels */
45 int height; /* height in pixels */
46 int ascent; /* ascent (baseline) height */
47 int firstchar; /* first character in bitmap */
48 int size; /* font size in glyphs ('holes' included) */
49 bitmap_t* bits; /* 16-bit right-padded bitmap data */
50 int* offset; /* offsets into bitmap data */
51 unsigned char* width; /* character widths or NULL if fixed */
52 int defaultchar; /* default char (not glyph index) */
53 int bits_size; /* # words of bitmap_t bits */
55 /* unused by runtime system, read in by convbdf */
56 int nchars; /* number of different glyphs */
57 int nchars_declared; /* number of glyphs as declared in the header */
58 int ascent_declared; /* ascent as declared in the header */
59 int descent_declared; /* descent as declared in the header */
60 int max_char_ascent; /* max. char ascent (before adjusting) */
61 int max_char_descent; /* max. char descent (before adjusting) */
62 unsigned int* offrot; /* offsets into rotated bitmap data */
63 char* name; /* font name */
64 char* facename; /* facename of font */
65 char* copyright; /* copyright info for loadable fonts */
66 int pixel_size;
67 int descent;
68 int fbbw, fbbh, fbbx, fbby;
70 /* Max 'overflow' of a char's ascent (descent) over the font's one */
71 int max_over_ascent, max_over_descent;
73 /* The number of clipped ascents/descents/total */
74 int num_clipped_ascent, num_clipped_descent, num_clipped;
76 /* default width in pixels (can be overwritten at char level) */
77 int default_width;
79 /* END font.h*/
81 /* Description of how the ascent/descent is allowed to grow */
82 struct stretch {
83 int value; /* The delta value (in pixels or percents) */
84 int percent; /* Is the value in percents (true) or pixels (false)? */
85 int force; /* MUST the value be set (true) or is it just a max (false) */
88 #define isprefix(buf,str) (!strncmp(buf, str, strlen(str)))
89 #define strequal(s1,s2) (!strcmp(s1, s2))
91 #define MAX(a,b) ((a) > (b) ? (a) : (b))
92 #define MIN(a,b) ((a) < (b) ? (a) : (b))
94 #ifdef ROTATE
95 #define ROTATION_BUF_SIZE 2048
96 #endif
98 /* Depending on the verbosity level some warnings are printed or not */
99 int verbosity_level = 0;
100 int trace = 0;
102 /* Prints a warning of the specified verbosity level. It will only be
103 really printed if the level is >= the level set in the settings */
104 void print_warning(int level, const char *fmt, ...);
105 void print_error(const char *fmt, ...);
106 void print_info(const char *fmt, ...);
107 void print_trace(const char *fmt, ...);
108 #define VL_CLIP_FONT 1 /* Verbosity level for clip related warnings at font level */
109 #define VL_CLIP_CHAR 2 /* Verbosity level for clip related warnings at char level */
110 #define VL_MISC 1 /* Verbosity level for other warnings */
112 int gen_c = 0;
113 int gen_h = 0;
114 int gen_fnt = 0;
115 int gen_map = 1;
116 int start_char = 0;
117 int limit_char = 65535;
118 int oflag = 0;
119 char outfile[256];
121 struct stretch stretch_ascent = { 0, 0, 1 }; /* Don't allow ascent to grow by default */
122 struct stretch stretch_descent = { 0, 0, 1 }; /* Don't allow descent to grow by default */
125 void usage(void);
126 void getopts(int *pac, char ***pav);
127 int convbdf(char *path);
129 void free_font(struct font* pf);
130 struct font* bdf_read_font(char *path);
131 int bdf_read_header(FILE *fp, struct font* pf);
132 int bdf_read_bitmaps(FILE *fp, struct font* pf);
135 Counts the glyphs and determines the max dimensions of glyphs
136 (fills the fields nchars, maxwidth, max_over_ascent, max_over_descent).
137 Returns 0 on failure or not-0 on success.
139 int bdf_analyze_font(FILE *fp, struct font* pf);
140 void bdf_correct_bbx(int *width, int *bbx); /* Corrects bbx and width if bbx<0 */
142 /* Corrects the ascent and returns the new value (value to use) */
143 int adjust_ascent(int ascent, int overflow, struct stretch *stretch);
145 char * bdf_getline(FILE *fp, char *buf, int len);
146 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2);
148 int gen_c_source(struct font* pf, char *path);
149 int gen_h_header(struct font* pf, char *path);
150 int gen_fnt_file(struct font* pf, char *path);
152 void
153 usage(void)
155 /* We use string array because some C compilers issue warnings about too long strings */
156 char *help[] = {
157 "Usage: convbdf [options] [input-files]\n",
158 " convbdf [options] [-o output-file] [single-input-file]\n",
159 "Options:\n",
160 " -c Convert .bdf to .c source file\n",
161 " -h Convert .bdf to .h header file (to create sysfont.h)\n",
162 " -f Convert .bdf to .fnt font file\n",
163 " -s N Start output at character encodings >= N\n",
164 " -l N Limit output to character encodings <= N\n",
165 " -n Don't generate bitmaps as comments in .c file\n",
166 " -a N[%][!] Allow the ascent to grow N pixels/% to avoid glyph clipping\n",
167 " -d N[%][!] Allow the descent to grow N pixels/% to avoid glyph clipping\n",
168 " -v N Verbosity level: 0=quite quiet, 1=more verbose, 2=even more, etc.\n",
169 " -t Print internal tracing messages\n",
170 NULL /* Must be the last element in the array */
173 char **p = help;
174 while (*p != NULL)
175 print_info("%s", *(p++));
179 void parse_ascent_opt(char *val, struct stretch *opt) {
180 char buf[256];
181 char *p;
182 strcpy(buf, val);
184 opt->force = 0;
185 opt->percent = 0;
186 p = buf + strlen(buf);
187 while (p > buf) {
188 p--;
189 if (*p == '%') {
190 opt->percent = 1;
191 *p = '\0';
193 else if (*p == '!') {
194 opt->force = 1;
195 *p = '\0';
197 else {
198 break;
201 opt->value = atoi(buf);
204 /* parse command line options*/
205 void getopts(int *pac, char ***pav)
207 char *p;
208 char **av;
209 int ac;
211 ac = *pac;
212 av = *pav;
213 while (ac > 0 && av[0][0] == '-') {
214 p = &av[0][1];
215 while( *p)
216 switch(*p++) {
217 case ' ': /* multiple -args on av[] */
218 while( *p && *p == ' ')
219 p++;
220 if( *p++ != '-') /* next option must have dash */
221 p = "";
222 break; /* proceed to next option */
223 case 'c': /* generate .c output */
224 gen_c = 1;
225 break;
226 case 'h': /* generate .h output */
227 gen_h = 1;
228 break;
229 case 'f': /* generate .fnt output */
230 gen_fnt = 1;
231 break;
232 case 'n': /* don't gen bitmap comments */
233 gen_map = 0;
234 break;
235 case 'o': /* set output file */
236 oflag = 1;
237 if (*p) {
238 strcpy(outfile, p);
239 while (*p && *p != ' ')
240 p++;
242 else {
243 av++; ac--;
244 if (ac > 0)
245 strcpy(outfile, av[0]);
247 break;
248 case 'l': /* set encoding limit */
249 if (*p) {
250 limit_char = atoi(p);
251 while (*p && *p != ' ')
252 p++;
254 else {
255 av++; ac--;
256 if (ac > 0)
257 limit_char = atoi(av[0]);
259 break;
260 case 's': /* set encoding start */
261 if (*p) {
262 start_char = atoi(p);
263 while (*p && *p != ' ')
264 p++;
266 else {
267 av++; ac--;
268 if (ac > 0)
269 start_char = atoi(av[0]);
271 break;
272 case 'a': /* ascent growth */
273 if (*p) {
274 parse_ascent_opt(p, &stretch_ascent);
275 while (*p && *p != ' ')
276 p++;
278 else {
279 av++; ac--;
280 if (ac > 0)
281 parse_ascent_opt(av[0], &stretch_ascent);
283 break;
284 case 'd': /* descent growth */
285 if (*p) {
286 parse_ascent_opt(p, &stretch_descent);
287 while (*p && *p != ' ')
288 p++;
290 else {
291 av++; ac--;
292 if (ac > 0)
293 parse_ascent_opt(av[0], &stretch_descent);
295 break;
296 case 'v': /* verbosity */
297 if (*p) {
298 verbosity_level = atoi(p);
299 while (*p && *p != ' ')
300 p++;
302 else {
303 av++; ac--;
304 if (ac > 0)
305 verbosity_level = atoi(av[0]);
307 break;
308 case 't': /* tracing */
309 trace = 1;
310 break;
311 default:
312 print_info("Unknown option ignored: %c\n", *(p-1));
314 ++av; --ac;
316 *pac = ac;
317 *pav = av;
320 void print_warning(int level, const char *fmt, ...) {
321 if (verbosity_level >= level) {
322 va_list ap;
323 va_start(ap, fmt);
324 fprintf(stderr, " WARN: ");
325 vfprintf(stderr, fmt, ap);
326 va_end(ap);
330 void print_trace(const char *fmt, ...) {
331 if (trace) {
332 va_list ap;
333 va_start(ap, fmt);
334 fprintf(stderr, "TRACE: ");
335 vfprintf(stderr, fmt, ap);
336 va_end(ap);
340 void print_error(const char *fmt, ...) {
341 va_list ap;
342 va_start(ap, fmt);
343 fprintf(stderr, "ERROR: ");
344 vfprintf(stderr, fmt, ap);
345 va_end(ap);
348 void print_info(const char *fmt, ...) {
349 va_list ap;
350 va_start(ap, fmt);
351 fprintf(stderr, " INFO: ");
352 vfprintf(stderr, fmt, ap);
353 va_end(ap);
356 /* remove directory prefix and file suffix from full path */
357 char *basename(char *path)
359 char *p, *b;
360 static char base[256];
362 /* remove prepended path and extension */
363 b = path;
364 for (p=path; *p; ++p) {
365 if (*p == '/')
366 b = p + 1;
368 strcpy(base, b);
369 for (p=base; *p; ++p) {
370 if (*p == '.') {
371 *p = 0;
372 break;
375 return base;
378 int convbdf(char *path)
380 struct font* pf;
381 int ret = 0;
383 pf = bdf_read_font(path);
384 if (!pf)
385 exit(1);
387 if (gen_c) {
388 if (!oflag) {
389 strcpy(outfile, basename(path));
390 strcat(outfile, ".c");
392 ret |= gen_c_source(pf, outfile);
395 if (gen_h) {
396 if (!oflag) {
397 strcpy(outfile, basename(path));
398 strcat(outfile, ".h");
400 ret |= gen_h_header(pf, outfile);
403 if (gen_fnt) {
404 if (!oflag) {
405 strcpy(outfile, basename(path));
406 strcat(outfile, ".fnt");
408 ret |= gen_fnt_file(pf, outfile);
411 free_font(pf);
412 return ret;
415 int main(int ac, char **av)
417 int ret = 0;
419 ++av; --ac; /* skip av[0] */
420 getopts(&ac, &av); /* read command line options */
422 if (ac < 1 || (!gen_c && !gen_h && !gen_fnt)) {
423 usage();
424 exit(1);
427 if (gen_c && gen_fnt) {
428 print_info(".c and .fnt files can not be produced in the same run!\n");
429 exit(1);
432 if (oflag) {
433 if (ac > 1 || (gen_c && gen_fnt) || (gen_c && gen_h) || (gen_h && gen_fnt)) {
434 usage();
435 exit(1);
439 while (ac > 0) {
440 ret |= convbdf(av[0]);
441 ++av; --ac;
444 exit(ret);
447 /* free font structure */
448 void free_font(struct font* pf)
450 if (!pf)
451 return;
452 if (pf->name)
453 free(pf->name);
454 if (pf->facename)
455 free(pf->facename);
456 if (pf->bits)
457 free(pf->bits);
458 if (pf->offset)
459 free(pf->offset);
460 if (pf->offrot)
461 free(pf->offrot);
462 if (pf->width)
463 free(pf->width);
464 free(pf);
467 /* build incore structure from .bdf file */
468 struct font* bdf_read_font(char *path)
470 FILE *fp;
471 struct font* pf;
473 fp = fopen(path, "rb");
474 if (!fp) {
475 print_error("Error opening file: %s\n", path);
476 return NULL;
479 pf = (struct font*)calloc(1, sizeof(struct font));
480 if (!pf)
481 goto errout;
482 memset(pf, 0, sizeof(struct font));
484 pf->name = strdup(basename(path));
486 if (!bdf_read_header(fp, pf)) {
487 print_error("Error reading font header\n");
488 goto errout;
490 print_trace("Read font header, nchars_decl=%d\n", pf->nchars_declared);
492 if (!bdf_analyze_font(fp, pf)) {
493 print_error("Error analyzing the font\n");
494 goto errout;
496 print_trace("Analyzed font, nchars=%d, maxwidth=%d, asc_over=%d, desc_over=%d\n",
497 pf->nchars, pf->maxwidth, pf->max_over_ascent, pf->max_over_descent);
499 if (pf->nchars != pf->nchars_declared) {
500 print_warning(VL_MISC, "The declared number of chars (%d) "
501 "does not match the real number (%d)\n",
502 pf->nchars_declared, pf->nchars);
505 /* Correct ascent/descent if necessary */
506 pf->ascent = adjust_ascent(pf->ascent_declared, pf->max_over_ascent, &stretch_ascent);
507 if (pf->ascent != pf->ascent_declared) {
508 print_info("Font ascent has been changed from %d to %d\n",
509 pf->ascent_declared, pf->ascent);
511 pf->descent = adjust_ascent(pf->descent, pf->max_over_descent, &stretch_descent);
512 if (pf->descent != pf->descent_declared) {
513 print_info("Font descent has been changed from %d to %d\n",
514 pf->descent_declared, pf->descent);
516 pf->height = pf->ascent + pf->descent;
517 if (pf->height != pf->ascent_declared + pf->descent_declared) {
518 print_warning(VL_CLIP_FONT, "Generated font's height: %d\n", pf->height);
521 if (pf->ascent > pf->max_char_ascent) {
522 print_trace("Font's ascent could be reduced by %d to %d without clipping\n",
523 (pf->ascent - pf->max_char_ascent), pf->max_char_ascent);
525 if (pf->descent > pf->max_char_descent) {
526 print_trace("Font's descent could be reduced by %d to %d without clipping\n",
527 (pf->descent - pf->max_char_descent), pf->max_char_descent);
531 /* Alocate memory */
532 pf->bits_size = pf->size * BITMAP_WORDS(pf->maxwidth) * pf->height;
533 pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t));
534 pf->offset = (int *)malloc(pf->size * sizeof(int));
535 pf->offrot = (unsigned int *)malloc(pf->size * sizeof(unsigned int));
536 pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char));
538 if (!pf->bits || !pf->offset || !pf->offrot || !pf->width) {
539 print_error("no memory for font load\n");
540 goto errout;
543 pf->num_clipped_ascent = pf->num_clipped_descent = pf->num_clipped = 0;
544 pf->max_over_ascent = pf->max_over_descent = 0;
546 if (!bdf_read_bitmaps(fp, pf)) {
547 print_error("Error reading font bitmaps\n");
548 goto errout;
550 print_trace("Read bitmaps\n");
552 if (pf->num_clipped > 0) {
553 print_warning(VL_CLIP_FONT, "%d character(s) out of %d were clipped "
554 "(%d at ascent, %d at descent)\n",
555 pf->num_clipped, pf->nchars,
556 pf->num_clipped_ascent, pf->num_clipped_descent);
557 print_warning(VL_CLIP_FONT, "max overflows: %d pixel(s) at ascent, %d pixel(s) at descent\n",
558 pf->max_over_ascent, pf->max_over_descent);
561 fclose(fp);
562 return pf;
564 errout:
565 fclose(fp);
566 free_font(pf);
567 return NULL;
570 /* read bdf font header information, return 0 on error */
571 int bdf_read_header(FILE *fp, struct font* pf)
573 int encoding;
574 int firstchar = 65535;
575 int lastchar = -1;
576 char buf[256];
577 char facename[256];
578 char copyright[256];
579 int is_header = 1;
581 /* set certain values to errors for later error checking */
582 pf->defaultchar = -1;
583 pf->ascent = -1;
584 pf->descent = -1;
585 pf->default_width = -1;
587 for (;;) {
588 if (!bdf_getline(fp, buf, sizeof(buf))) {
589 print_error("EOF on file\n");
590 return 0;
592 if (isprefix(buf, "FONT ")) { /* not required */
593 if (sscanf(buf, "FONT %[^\n]", facename) != 1) {
594 print_error("bad 'FONT'\n");
595 return 0;
597 pf->facename = strdup(facename);
598 continue;
600 if (isprefix(buf, "COPYRIGHT ")) { /* not required */
601 if (sscanf(buf, "COPYRIGHT \"%[^\"]", copyright) != 1) {
602 print_error("bad 'COPYRIGHT'\n");
603 return 0;
605 pf->copyright = strdup(copyright);
606 continue;
608 if (isprefix(buf, "DEFAULT_CHAR ")) { /* not required */
609 if (sscanf(buf, "DEFAULT_CHAR %d", &pf->defaultchar) != 1) {
610 print_error("bad 'DEFAULT_CHAR'\n");
611 return 0;
614 if (isprefix(buf, "FONT_DESCENT ")) {
615 if (sscanf(buf, "FONT_DESCENT %d", &pf->descent_declared) != 1) {
616 print_error("bad 'FONT_DESCENT'\n");
617 return 0;
619 pf->descent = pf->descent_declared; /* For now */
620 continue;
622 if (isprefix(buf, "FONT_ASCENT ")) {
623 if (sscanf(buf, "FONT_ASCENT %d", &pf->ascent_declared) != 1) {
624 print_error("bad 'FONT_ASCENT'\n");
625 return 0;
627 pf->ascent = pf->ascent_declared; /* For now */
628 continue;
630 if (isprefix(buf, "FONTBOUNDINGBOX ")) {
631 if (sscanf(buf, "FONTBOUNDINGBOX %d %d %d %d",
632 &pf->fbbw, &pf->fbbh, &pf->fbbx, &pf->fbby) != 4) {
633 print_error("bad 'FONTBOUNDINGBOX'\n");
634 return 0;
636 continue;
638 if (isprefix(buf, "CHARS ")) {
639 if (sscanf(buf, "CHARS %d", &pf->nchars_declared) != 1) {
640 print_error("bad 'CHARS'\n");
641 return 0;
643 continue;
645 if (isprefix(buf, "STARTCHAR")) {
646 is_header = 0;
647 continue;
650 /* for BDF version 2.2 */
651 if (is_header && isprefix(buf, "DWIDTH ")) {
652 if (sscanf(buf, "DWIDTH %d", &pf->default_width) != 1) {
653 print_error("bad 'DWIDTH' at font level\n");
654 return 0;
656 continue;
660 * Reading ENCODING is necessary to get firstchar/lastchar
661 * which is needed to pre-calculate our offset and widths
662 * array sizes.
664 if (isprefix(buf, "ENCODING ")) {
665 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
666 print_error("bad 'ENCODING'\n");
667 return 0;
669 if (encoding >= 0 &&
670 encoding <= limit_char &&
671 encoding >= start_char) {
673 if (firstchar > encoding)
674 firstchar = encoding;
675 if (lastchar < encoding)
676 lastchar = encoding;
678 continue;
680 if (strequal(buf, "ENDFONT"))
681 break;
684 /* calc font height*/
685 if (pf->ascent < 0 || pf->descent < 0 || firstchar < 0) {
686 print_error("Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
687 return 0;
689 pf->height = pf->ascent + pf->descent;
691 /* calc default char */
692 if (pf->defaultchar < 0 ||
693 pf->defaultchar < firstchar ||
694 pf->defaultchar > limit_char ||
695 pf->defaultchar > lastchar)
696 pf->defaultchar = firstchar;
698 /* calc font size (offset/width entries) */
699 pf->firstchar = firstchar;
700 pf->size = lastchar - firstchar + 1;
702 return 1;
706 * TODO: rework the code to avoid logics duplication in
707 * bdf_read_bitmaps and bdf_analyze_font
711 /* read bdf font bitmaps, return 0 on error */
712 int bdf_read_bitmaps(FILE *fp, struct font* pf)
714 int ofs = 0;
715 int ofr = 0;
716 int i, k, encoding, width;
717 int bbw, bbh, bbx, bby;
718 int proportional = 0;
719 int encodetable = 0;
720 int l;
721 char buf[256];
722 bitmap_t *ch_bitmap;
723 int ch_words;
725 /* reset file pointer */
726 fseek(fp, 0L, SEEK_SET);
728 /* initially mark offsets as not used */
729 for (i=0; i<pf->size; ++i)
730 pf->offset[i] = -1;
732 for (;;) {
733 if (!bdf_getline(fp, buf, sizeof(buf))) {
734 print_error("EOF on file\n");
735 return 0;
737 if (isprefix(buf, "STARTCHAR")) {
738 encoding = width = -1;
739 bbw = pf->fbbw;
740 bbh = pf->fbbh;
741 bbx = pf->fbbx;
742 bby = pf->fbby;
743 continue;
745 if (isprefix(buf, "ENCODING ")) {
746 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
747 print_error("bad 'ENCODING'\n");
748 return 0;
750 if (encoding < start_char || encoding > limit_char)
751 encoding = -1;
752 continue;
754 if (isprefix(buf, "DWIDTH ")) {
755 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
756 print_error("bad 'DWIDTH'\n");
757 return 0;
759 /* use font boundingbox width if DWIDTH <= 0 */
760 if (width <= 0)
761 width = pf->fbbw - pf->fbbx;
762 continue;
764 if (isprefix(buf, "BBX ")) {
765 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
766 print_error("bad 'BBX'\n");
767 return 0;
769 continue;
771 if (strequal(buf, "BITMAP") || strequal(buf, "BITMAP ")) {
772 int overflow_asc, overflow_desc;
773 int bbh_orig, bby_orig, y;
775 if (encoding < 0)
776 continue;
778 if (width < 0 && pf->default_width > 0)
779 width = pf->default_width;
781 /* set bits offset in encode map*/
782 if (pf->offset[encoding-pf->firstchar] != -1) {
783 print_error("duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
784 encoding, encoding);
785 continue;
787 pf->offset[encoding-pf->firstchar] = ofs;
788 pf->offrot[encoding-pf->firstchar] = ofr;
790 /* calc char width */
791 bdf_correct_bbx(&width, &bbx);
792 pf->width[encoding-pf->firstchar] = width;
794 ch_bitmap = pf->bits + ofs;
795 ch_words = BITMAP_WORDS(width);
796 memset(ch_bitmap, 0, BITMAP_BYTES(width) * pf->height); /* clear bitmap */
798 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
799 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
801 bbh_orig = bbh;
802 bby_orig = bby;
804 overflow_asc = bby + bbh - pf->ascent;
805 if (overflow_asc > 0) {
806 pf->num_clipped_ascent++;
807 if (overflow_asc > pf->max_over_ascent) {
808 pf->max_over_ascent = overflow_asc;
810 bbh = MAX(bbh - overflow_asc, 0); /* Clipped -> decrease the height */
811 print_warning(VL_CLIP_CHAR, "character %d goes %d pixel(s)"
812 " beyond the font's ascent, it will be clipped\n",
813 encoding, overflow_asc);
815 overflow_desc = -bby - pf->descent;
816 if (overflow_desc > 0) {
817 pf->num_clipped_descent++;
818 if (overflow_desc > pf->max_over_descent) {
819 pf->max_over_descent = overflow_desc;
821 bby += overflow_desc;
822 bbh = MAX(bbh - overflow_desc, 0); /* Clipped -> decrease the height */
823 print_warning(VL_CLIP_CHAR, "character %d goes %d pixel(s)"
824 " beyond the font's descent, it will be clipped\n",
825 encoding, overflow_desc);
827 if (overflow_asc > 0 || overflow_desc > 0) {
828 pf->num_clipped++;
831 y = bby_orig + bbh_orig; /* 0-based y within the char */
833 /* read bitmaps */
834 for (i=0; ; ++i) {
835 int hexnibbles;
837 if (!bdf_getline(fp, buf, sizeof(buf))) {
838 print_error("EOF reading BITMAP data for character %d\n",
839 encoding);
840 return 0;
842 if (isprefix(buf, "ENDCHAR"))
843 break;
845 y--;
846 if ((y >= pf->ascent) || (y < -pf->descent)) {
847 /* We're beyond the area that Rockbox can render -> clip */
848 --i; /* This line doesn't count */
849 continue;
852 hexnibbles = strlen(buf);
853 for (k=0; k<ch_words; ++k) {
854 int ndx = k * BITMAP_NIBBLES;
855 int padnibbles = hexnibbles - ndx;
856 bitmap_t value;
858 if (padnibbles <= 0)
859 break;
860 if (padnibbles >= (int)BITMAP_NIBBLES)
861 padnibbles = 0;
863 value = bdf_hexval((unsigned char *)buf,
864 ndx, ndx+BITMAP_NIBBLES-1-padnibbles);
865 value <<= padnibbles * BITMAP_NIBBLES;
867 BM(pf->height - pf->descent - bby - bbh + i, k) |=
868 value >> bbx;
869 /* handle overflow into next image word */
870 if (bbx) {
871 BM(pf->height - pf->descent - bby - bbh + i, k+1) =
872 value << (BITMAP_BITSPERIMAGE - bbx);
877 ofs += BITMAP_WORDS(width) * pf->height;
878 ofr += pf->width[encoding-pf->firstchar] * ((pf->height+7)/8);
880 continue;
882 if (strequal(buf, "ENDFONT"))
883 break;
886 /* change unused width values to default char values */
887 for (i=0; i<pf->size; ++i) {
888 int defchar = pf->defaultchar - pf->firstchar;
890 if (pf->offset[i] == -1)
891 pf->width[i] = pf->width[defchar];
894 /* determine whether font doesn't require encode table */
895 #ifdef ROTATE
896 l = 0;
897 for (i=0; i<pf->size; ++i) {
898 if ((int)pf->offrot[i] != l) {
899 encodetable = 1;
900 break;
902 l += pf->maxwidth * ((pf->height + 7) / 8);
904 #else
905 l = 0;
906 for (i=0; i<pf->size; ++i) {
907 if (pf->offset[i] != l) {
908 encodetable = 1;
909 break;
911 l += BITMAP_WORDS(pf->width[i]) * pf->height;
913 #endif
914 if (!encodetable) {
915 free(pf->offset);
916 pf->offset = NULL;
919 /* determine whether font is fixed-width */
920 for (i=0; i<pf->size; ++i) {
921 if (pf->width[i] != pf->maxwidth) {
922 proportional = 1;
923 break;
926 if (!proportional) {
927 free(pf->width);
928 pf->width = NULL;
931 /* reallocate bits array to actual bits used */
932 if (ofs < pf->bits_size) {
933 pf->bits = realloc(pf->bits, ofs * sizeof(bitmap_t));
934 pf->bits_size = ofs;
937 #ifdef ROTATE
938 pf->bits_size = ofr; /* always update, rotated is smaller */
939 #endif
941 return 1;
944 /* read the next non-comment line, returns buf or NULL if EOF */
945 char *bdf_getline(FILE *fp, char *buf, int len)
947 int c;
948 char *b;
950 for (;;) {
951 b = buf;
952 while ((c = getc(fp)) != EOF) {
953 if (c == '\r')
954 continue;
955 if (c == '\n')
956 break;
957 if (b - buf >= (len - 1))
958 break;
959 *b++ = c;
961 *b = '\0';
962 if (c == EOF && b == buf)
963 return NULL;
964 if (b != buf && !isprefix(buf, "COMMENT"))
965 break;
967 return buf;
970 void bdf_correct_bbx(int *width, int *bbx) {
971 if (*bbx < 0) {
972 /* Rockbox can't render overlapping glyphs */
973 *width -= *bbx;
974 *bbx = 0;
978 int bdf_analyze_font(FILE *fp, struct font* pf) {
979 char buf[256];
980 int encoding;
981 int width, bbw, bbh, bbx, bby, ascent, overflow;
982 int read_enc = 0, read_width = 0, read_bbx = 0, read_endchar = 1;
983 int ignore_char = 0;
985 /* reset file pointer */
986 fseek(fp, 0L, SEEK_SET);
988 pf->maxwidth = 0;
989 pf->nchars = 0;
990 pf->max_char_ascent = pf->max_char_descent = 0;
991 pf->max_over_ascent = pf->max_over_descent = 0;
993 for (;;) {
995 if (!bdf_getline(fp, buf, sizeof(buf))) {
996 print_error("EOF on file\n");
997 return 0;
999 if (isprefix(buf, "ENDFONT")) {
1000 if (!read_endchar) {
1001 print_error("No terminating ENDCHAR for character %d\n", encoding);
1002 return 0;
1004 break;
1006 if (isprefix(buf, "STARTCHAR")) {
1007 print_trace("Read STARTCHAR, nchars=%d, read_endchar=%d\n", pf->nchars, read_endchar);
1008 if (!read_endchar) {
1009 print_error("No terminating ENDCHAR for character %d\n", encoding);
1010 return 0;
1012 read_enc = read_width = read_bbx = read_endchar = 0;
1013 continue;
1015 if (isprefix(buf, "ENDCHAR")) {
1016 if (!read_enc) {
1017 print_error("ENCODING is not specified\n");
1018 return 0;
1020 ignore_char = (encoding < start_char || encoding > limit_char);
1021 if (!ignore_char) {
1022 if (!read_width && pf->default_width > 0)
1024 width = pf->default_width;
1025 read_width = 1;
1027 if (!read_width || !read_bbx) {
1028 print_error("WIDTH or BBX is not specified for character %d\n",
1029 encoding);
1031 bdf_correct_bbx(&width, &bbx);
1032 if (width > pf->maxwidth) {
1033 pf->maxwidth = width;
1036 ascent = bby + bbh;
1037 pf->max_char_ascent = MAX(pf->max_char_ascent, ascent);
1038 overflow = ascent - pf->ascent;
1039 pf->max_over_ascent = MAX(pf->max_over_ascent, overflow);
1041 ascent = -bby;
1042 pf->max_char_descent = MAX(pf->max_char_descent, ascent);
1043 overflow = ascent - pf->descent;
1044 pf->max_over_descent = MAX(pf->max_over_descent, overflow);
1046 pf->nchars++;
1047 read_endchar = 1;
1048 continue;
1050 if (isprefix(buf, "ENCODING ")) {
1051 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
1052 print_error("bad 'ENCODING': '%s'\n", buf);
1053 return 0;
1055 read_enc = 1;
1056 continue;
1058 if (isprefix(buf, "DWIDTH ")) {
1059 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
1060 print_error("bad 'DWIDTH': '%s'\n", buf);
1061 return 0;
1063 /* use font boundingbox width if DWIDTH <= 0 */
1064 if (width < 0) {
1065 print_error("Negative char width: %d\n", width);
1066 return 0;
1068 read_width = 1;
1070 if (isprefix(buf, "BBX ")) {
1071 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
1072 print_error("bad 'BBX': '%s'\n", buf);
1073 return 0;
1075 read_bbx = 1;
1076 continue;
1079 return 1;
1082 int adjust_ascent(int ascent, int overflow, struct stretch *stretch) {
1083 int result;
1084 int px = stretch->value;
1085 if (stretch->percent) {
1086 px = ascent * px / 100;
1089 if (stretch->force) {
1090 result = ascent + px;
1092 else {
1093 result = ascent + MIN(overflow, px);
1095 result = MAX(result, 0);
1096 return result;
1100 /* return hex value of portion of buffer*/
1101 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2)
1103 bitmap_t val = 0;
1104 int i, c;
1106 for (i=ndx1; i<=ndx2; ++i) {
1107 c = buf[i];
1108 if (c >= '0' && c <= '9')
1109 c -= '0';
1110 else
1111 if (c >= 'A' && c <= 'F')
1112 c = c - 'A' + 10;
1113 else
1114 if (c >= 'a' && c <= 'f')
1115 c = c - 'a' + 10;
1116 else
1117 c = 0;
1118 val = (val << 4) | c;
1120 return val;
1124 #ifdef ROTATE
1127 * Take an bitmap_t bitmap and convert to Rockbox format.
1128 * Used for converting font glyphs for the time being.
1129 * Can use for standard X11 and Win32 images as well.
1130 * See format description in lcd-recorder.c
1132 * Doing it this way keeps fonts in standard formats,
1133 * as well as keeping Rockbox hw bitmap format.
1135 * Returns the size of the rotated glyph (in bytes) or a
1136 * negative value if the glyph could not be rotated.
1138 int rotleft(unsigned char *dst, /* output buffer */
1139 size_t dstlen, /* buffer size */
1140 bitmap_t *src, unsigned int width, unsigned int height,
1141 int char_code)
1143 unsigned int i,j;
1144 unsigned int src_words; /* # words of input image */
1145 unsigned int dst_mask; /* bit mask for destination */
1146 bitmap_t src_mask; /* bit mask for source */
1148 /* How large the buffer should be to hold the rotated bitmap
1149 of a glyph of size (width x height) */
1150 unsigned int needed_size = ((height + 7) / 8) * width;
1152 if (needed_size > dstlen) {
1153 print_error("Character %d: Glyph of size %d x %d can't be rotated "
1154 "(buffer size is %lu, needs %u)\n",
1155 char_code, width, height, (unsigned long)dstlen, needed_size);
1156 return -1;
1159 /* calc words of input image*/
1160 src_words = BITMAP_WORDS(width) * height;
1162 /* clear background*/
1163 memset(dst, 0, needed_size);
1165 dst_mask = 1;
1167 for (i=0; i < src_words; i++) {
1169 /* calc src input bit*/
1170 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
1172 /* for each input column...*/
1173 for(j=0; j < width; j++) {
1175 if (src_mask == 0) /* input word done? */
1177 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
1178 i++; /* next input word */
1181 /* if set in input, set in rotated output */
1182 if (src[i] & src_mask)
1183 dst[j] |= dst_mask;
1185 src_mask >>= 1; /* next input bit */
1188 dst_mask <<= 1; /* next output bit (row) */
1189 if (dst_mask > (1 << 7)) /* output bit > 7? */
1191 dst_mask = 1;
1192 dst += width; /* next output byte row */
1195 return needed_size; /* return result size in bytes */
1198 #endif /* ROTATE */
1201 /* generate C source from in-core font*/
1202 int gen_c_source(struct font* pf, char *path)
1204 FILE *ofp;
1205 int i;
1206 time_t t = time(0);
1207 #ifdef ROTATE
1208 int ofr = 0;
1209 #else
1210 int did_syncmsg = 0;
1211 bitmap_t *ofs = pf->bits;
1212 #endif
1213 char buf[256];
1214 char obuf[256];
1215 char hdr1[] = {
1216 "/* Generated by convbdf on %s. */\n"
1217 "#include \"font.h\"\n"
1218 "#ifdef HAVE_LCD_BITMAP\n"
1219 "\n"
1220 "/* Font information:\n"
1221 " name: %s\n"
1222 " facename: %s\n"
1223 " w x h: %dx%d\n"
1224 " size: %d\n"
1225 " ascent: %d\n"
1226 " descent: %d\n"
1227 " first char: %d (0x%02x)\n"
1228 " last char: %d (0x%02x)\n"
1229 " default char: %d (0x%02x)\n"
1230 " proportional: %s\n"
1231 " %s\n"
1232 "*/\n"
1233 "\n"
1234 "/* Font character bitmap data. */\n"
1235 "static const unsigned char _font_bits[] = {\n"
1238 ofp = fopen(path, "w");
1239 if (!ofp) {
1240 print_error("Can't create %s\n", path);
1241 return 1;
1244 strcpy(buf, ctime(&t));
1245 buf[strlen(buf)-1] = 0;
1247 fprintf(ofp, hdr1, buf,
1248 pf->name,
1249 pf->facename? pf->facename: "",
1250 pf->maxwidth, pf->height,
1251 pf->size,
1252 pf->ascent, pf->descent,
1253 pf->firstchar, pf->firstchar,
1254 pf->firstchar+pf->size-1, pf->firstchar+pf->size-1,
1255 pf->defaultchar, pf->defaultchar,
1256 pf->width? "yes": "no",
1257 pf->copyright? pf->copyright: "");
1259 /* generate bitmaps*/
1260 for (i=0; i<pf->size; ++i) {
1261 int x;
1262 int bitcount = 0;
1263 int width = pf->width ? pf->width[i] : pf->maxwidth;
1264 int height = pf->height;
1265 int char_code = pf->firstchar + i;
1266 bitmap_t *bits;
1267 bitmap_t bitvalue=0;
1269 /* Skip missing glyphs */
1270 if (pf->offset && (pf->offset[i] == -1))
1271 continue;
1273 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1275 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d",
1276 char_code, char_code, width);
1278 if (gen_map) {
1279 fprintf(ofp, "\n +");
1280 for (x=0; x<width; ++x) fprintf(ofp, "-");
1281 fprintf(ofp, "+\n");
1283 x = 0;
1284 while (height > 0) {
1285 if (x == 0) fprintf(ofp, " |");
1287 if (bitcount <= 0) {
1288 bitcount = BITMAP_BITSPERIMAGE;
1289 bitvalue = *bits++;
1292 fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " ");
1294 bitvalue = BITMAP_SHIFTBIT(bitvalue);
1295 --bitcount;
1296 if (++x == width) {
1297 fprintf(ofp, "|\n");
1298 --height;
1299 x = 0;
1300 bitcount = 0;
1303 fprintf(ofp, " +");
1304 for (x=0; x<width; ++x)
1305 fprintf(ofp, "-");
1306 fprintf(ofp, "+ */\n");
1308 else
1309 fprintf(ofp, " */\n");
1311 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1312 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
1314 unsigned char bytemap[ROTATION_BUF_SIZE];
1315 int y8, ix=0;
1317 int size = rotleft(bytemap, sizeof(bytemap), bits, width,
1318 pf->height, char_code);
1319 if (size < 0) {
1320 return -1;
1323 for (y8=0; y8<pf->height; y8+=8) /* column rows */
1325 for (x=0; x<width; x++) {
1326 fprintf(ofp, "0x%02x, ", bytemap[ix]);
1327 ix++;
1329 fprintf(ofp, "\n");
1332 /* update offrot since bits are now in sorted order */
1333 pf->offrot[i] = ofr;
1334 ofr += size;
1337 #else
1338 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) {
1339 fprintf(ofp, "0x%04x,\n", *bits);
1340 if (!did_syncmsg && *bits++ != *ofs++) {
1341 print_warning(VL_MISC, "found encoding values in non-sorted order (not an error).\n");
1342 did_syncmsg = 1;
1345 #endif
1347 fprintf(ofp, "};\n\n");
1349 if (pf->offset) {
1350 /* output offset table*/
1351 fprintf(ofp, "/* Character->glyph mapping. */\n"
1352 "static const unsigned short _sysfont_offset[] = {\n");
1354 for (i=0; i<pf->size; ++i) {
1355 if (pf->offset[i] == -1) {
1356 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
1357 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
1359 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
1360 #ifdef ROTATE
1361 pf->offrot[i], i+pf->firstchar);
1362 #else
1363 pf->offset[i], i+pf->firstchar);
1364 #endif
1366 fprintf(ofp, "};\n\n");
1369 /* output width table for proportional fonts*/
1370 if (pf->width) {
1371 fprintf(ofp, "/* Character width data. */\n"
1372 "static const unsigned char _sysfont_width[] = {\n");
1374 for (i=0; i<pf->size; ++i)
1375 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
1376 pf->width[i], i+pf->firstchar);
1377 fprintf(ofp, "};\n\n");
1380 /* output struct font struct*/
1381 if (pf->offset)
1382 sprintf(obuf, "_sysfont_offset,");
1383 else
1384 sprintf(obuf, "0, /* no encode table */");
1386 if (pf->width)
1387 sprintf(buf, "_sysfont_width, /* width */");
1388 else
1389 sprintf(buf, "0, /* fixed width */");
1391 fprintf(ofp, "/* Exported structure definition. */\n"
1392 "const struct font sysfont = {\n"
1393 " %d, /* maxwidth */\n"
1394 " %d, /* height */\n"
1395 " %d, /* ascent */\n"
1396 " %d, /* firstchar */\n"
1397 " %d, /* size */\n"
1398 " _font_bits, /* bits */\n"
1399 " %s /* offset */\n"
1400 " %s\n"
1401 " %d, /* defaultchar */\n"
1402 " %d, /* bits_size */\n"
1403 " -1, /* font fd */\n"
1404 " 0, /* buffer start */\n"
1405 " 0, /* ^ position */\n"
1406 " 0, /* ^ end */\n"
1407 " 0, /* ^ size */\n"
1408 " {{0,0,0,0,0},0,0,0}, /* cache */\n"
1409 " 0, /* */\n"
1410 " 0, /* */\n"
1411 " 0, /* */\n"
1412 "};\n"
1413 "#endif /* HAVE_LCD_BITMAP */\n",
1414 pf->maxwidth, pf->height,
1415 pf->ascent,
1416 pf->firstchar,
1417 pf->size,
1418 obuf,
1419 buf,
1420 pf->defaultchar,
1421 pf->bits_size);
1423 return 0;
1426 /* generate C header from in-core font*/
1427 int gen_h_header(struct font* pf, char *path)
1429 FILE *ofp;
1430 time_t t = time(0);
1431 char buf[256];
1432 char *hdr1 =
1433 "/* Generated by convbdf on %s. */\n"
1434 "#ifdef HAVE_LCD_BITMAP\n"
1435 "\n"
1436 "/* Font information*/\n"
1437 "#define SYSFONT_NAME %s\n"
1438 "#define SYSFONT_FACENAME %s\n"
1439 "#define SYSFONT_WIDTH %d\n"
1440 "#define SYSFONT_HEIGHT %d\n"
1441 "#define SYSFONT_SIZE %d\n"
1442 "#define SYSFONT_ASCENT %d\n";
1443 char *hdr2 =
1444 "#define SYSFONT_DESCENT %d\n"
1445 "#define SYSFONT_FIRST_CHAR %d\n"
1446 "#define SYSFONT_LAST_CHAR %d\n"
1447 "#define SYSFONT_DEFAULT_CHAR %d\n"
1448 "#define SYSFONT_PROPORTIONAL %d\n"
1449 "#define SYSFONT_COPYRIGHT %s\n"
1450 "#define SYSFONT_BITS_SIZE %d\n"
1451 "\n"
1452 "#endif\n";
1454 ofp = fopen(path, "w");
1455 if (!ofp) {
1456 print_error("Can't create %s\n", path);
1457 return 1;
1460 strcpy(buf, ctime(&t));
1461 buf[strlen(buf)-1] = 0;
1463 fprintf(ofp, hdr1, buf,
1464 pf->name,
1465 pf->facename? pf->facename: "",
1466 pf->maxwidth,
1467 pf->height,
1468 pf->size,
1469 pf->ascent);
1471 fprintf(ofp, hdr2,
1472 pf->descent,
1473 pf->firstchar,
1474 pf->firstchar+pf->size-1,
1475 pf->defaultchar,
1476 pf->width? 1: 0,
1477 pf->copyright? pf->copyright: "",
1478 pf->bits_size);
1480 return 0;
1483 static int writebyte(FILE *fp, unsigned char c)
1485 return putc(c, fp) != EOF;
1488 static int writeshort(FILE *fp, unsigned short s)
1490 putc(s, fp);
1491 return putc(s>>8, fp) != EOF;
1494 static int writeint(FILE *fp, unsigned int l)
1496 putc(l, fp);
1497 putc(l>>8, fp);
1498 putc(l>>16, fp);
1499 return putc(l>>24, fp) != EOF;
1502 static int writestr(FILE *fp, char *str, int count)
1504 return (int)fwrite(str, 1, count, fp) == count;
1507 #ifndef ROTATE
1508 static int writestrpad(FILE *fp, char *str, int totlen)
1510 int ret = EOF;
1512 while (str && *str && totlen > 0) {
1513 if (*str) {
1514 ret = putc(*str++, fp);
1515 --totlen;
1518 while (--totlen >= 0)
1519 ret = putc(' ', fp);
1520 return ret;
1522 #endif
1524 /* generate .fnt format file from in-core font*/
1525 int gen_fnt_file(struct font* pf, char *path)
1527 FILE *ofp;
1528 int i;
1529 #ifdef ROTATE
1530 int ofr = 0;
1531 #endif
1533 ofp = fopen(path, "wb");
1534 if (!ofp) {
1535 print_error("Can't create %s\n", path);
1536 return 1;
1539 /* write magic and version #*/
1540 writestr(ofp, VERSION, 4);
1541 #ifndef ROTATE
1542 /* internal font name*/
1543 writestrpad(ofp, pf->name, 64);
1545 /* copyright*/
1546 writestrpad(ofp, pf->copyright, 256);
1547 #endif
1548 /* font info*/
1549 writeshort(ofp, pf->maxwidth);
1550 writeshort(ofp, pf->height);
1551 writeshort(ofp, pf->ascent);
1552 writeshort(ofp, 0);
1553 writeint(ofp, pf->firstchar);
1554 writeint(ofp, pf->defaultchar);
1555 writeint(ofp, pf->size);
1557 /* variable font data sizes*/
1558 writeint(ofp, pf->bits_size); /* # words of bitmap_t*/
1559 writeint(ofp, pf->offset? pf->size: 0); /* # ints of offset*/
1560 writeint(ofp, pf->width? pf->size: 0); /* # bytes of width*/
1561 /* variable font data*/
1562 #ifdef ROTATE
1563 for (i=0; i<pf->size; ++i)
1565 bitmap_t* bits;
1566 int width = pf->width ? pf->width[i] : pf->maxwidth;
1567 int size;
1568 int char_code = pf->firstchar + i;
1569 unsigned char bytemap[ROTATION_BUF_SIZE];
1571 /* Skip missing glyphs */
1572 if (pf->offset && (pf->offset[i] == -1))
1573 continue;
1575 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1577 size = rotleft(bytemap, sizeof(bytemap), bits, width, pf->height, char_code);
1578 if (size < 0) {
1579 return -1;
1581 writestr(ofp, (char *)bytemap, size);
1583 /* update offrot since bits are now in sorted order */
1584 pf->offrot[i] = ofr;
1585 ofr += size;
1588 if ( pf->bits_size < 0xFFDB )
1590 /* bitmap offset is small enough, use unsigned short for offset */
1591 if (ftell(ofp) & 1)
1592 writebyte(ofp, 0); /* pad to 16-bit boundary*/
1594 else
1596 /* bitmap offset is large then 64K, use unsigned int for offset */
1597 while (ftell(ofp) & 3)
1598 writebyte(ofp, 0); /* pad to 32-bit boundary*/
1601 if (pf->offset)
1603 for (i=0; i<pf->size; ++i)
1605 if (pf->offset[i] == -1) {
1606 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
1608 if ( pf->bits_size < 0xFFDB )
1609 writeshort(ofp, pf->offrot[i]);
1610 else
1611 writeint(ofp, pf->offrot[i]);
1615 if (pf->width)
1616 for (i=0; i<pf->size; ++i)
1617 writebyte(ofp, pf->width[i]);
1618 #else
1619 for (i=0; i<pf->bits_size; ++i)
1620 writeshort(ofp, pf->bits[i]);
1621 if (ftell(ofp) & 2)
1622 writeshort(ofp, 0); /* pad to 32-bit boundary*/
1624 if (pf->offset)
1625 for (i=0; i<pf->size; ++i) {
1626 if (pf->offset[i] == -1) {
1627 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
1629 writeint(ofp, pf->offset[i]);
1632 if (pf->width)
1633 for (i=0; i<pf->size; ++i)
1634 writebyte(ofp, pf->width[i]);
1635 #endif
1636 fclose(ofp);
1637 return 0;