Missed some more debug statements in libasf.
[kugel-rb.git] / tools / convbdf.c
blob5a563220fd76125bf08ad4330bd9f7d2f883e5ef
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 number */
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 " An ! in the -a and -d options forces the growth; N may be negative\n",
169 " -v N Verbosity level: 0=quite quiet, 1=more verbose, 2=even more, etc.\n",
170 " -t Print internal tracing messages\n",
171 NULL /* Must be the last element in the array */
174 char **p = help;
175 while (*p != NULL)
176 print_info("%s", *(p++));
180 void parse_ascent_opt(char *val, struct stretch *opt) {
181 char buf[256];
182 char *p;
183 strcpy(buf, val);
185 opt->force = 0;
186 opt->percent = 0;
187 p = buf + strlen(buf);
188 while (p > buf) {
189 p--;
190 if (*p == '%') {
191 opt->percent = 1;
192 *p = '\0';
194 else if (*p == '!') {
195 opt->force = 1;
196 *p = '\0';
198 else {
199 break;
202 opt->value = atoi(buf);
205 /* parse command line options */
206 void getopts(int *pac, char ***pav)
208 char *p;
209 char **av;
210 int ac;
212 ac = *pac;
213 av = *pav;
214 while (ac > 0 && av[0][0] == '-') {
215 p = &av[0][1];
216 while( *p)
217 switch(*p++) {
218 case ' ': /* multiple -args on av[] */
219 while( *p && *p == ' ')
220 p++;
221 if( *p++ != '-') /* next option must have dash */
222 p = "";
223 break; /* proceed to next option */
224 case 'c': /* generate .c output */
225 gen_c = 1;
226 break;
227 case 'h': /* generate .h output */
228 gen_h = 1;
229 break;
230 case 'f': /* generate .fnt output */
231 gen_fnt = 1;
232 break;
233 case 'n': /* don't gen bitmap comments */
234 gen_map = 0;
235 break;
236 case 'o': /* set output file */
237 oflag = 1;
238 if (*p) {
239 strcpy(outfile, p);
240 while (*p && *p != ' ')
241 p++;
243 else {
244 av++; ac--;
245 if (ac > 0)
246 strcpy(outfile, av[0]);
248 break;
249 case 'l': /* set encoding limit */
250 if (*p) {
251 limit_char = atoi(p);
252 while (*p && *p != ' ')
253 p++;
255 else {
256 av++; ac--;
257 if (ac > 0)
258 limit_char = atoi(av[0]);
260 break;
261 case 's': /* set encoding start */
262 if (*p) {
263 start_char = atoi(p);
264 while (*p && *p != ' ')
265 p++;
267 else {
268 av++; ac--;
269 if (ac > 0)
270 start_char = atoi(av[0]);
272 break;
273 case 'a': /* ascent growth */
274 if (*p) {
275 parse_ascent_opt(p, &stretch_ascent);
276 while (*p && *p != ' ')
277 p++;
279 else {
280 av++; ac--;
281 if (ac > 0)
282 parse_ascent_opt(av[0], &stretch_ascent);
284 break;
285 case 'd': /* descent growth */
286 if (*p) {
287 parse_ascent_opt(p, &stretch_descent);
288 while (*p && *p != ' ')
289 p++;
291 else {
292 av++; ac--;
293 if (ac > 0)
294 parse_ascent_opt(av[0], &stretch_descent);
296 break;
297 case 'v': /* verbosity */
298 if (*p) {
299 verbosity_level = atoi(p);
300 while (*p && *p != ' ')
301 p++;
303 else {
304 av++; ac--;
305 if (ac > 0)
306 verbosity_level = atoi(av[0]);
308 break;
309 case 't': /* tracing */
310 trace = 1;
311 break;
312 default:
313 print_info("Unknown option ignored: %c\n", *(p-1));
315 ++av; --ac;
317 *pac = ac;
318 *pav = av;
321 void print_warning(int level, const char *fmt, ...) {
322 if (verbosity_level >= level) {
323 va_list ap;
324 va_start(ap, fmt);
325 fprintf(stderr, " WARN: ");
326 vfprintf(stderr, fmt, ap);
327 va_end(ap);
331 void print_trace(const char *fmt, ...) {
332 if (trace) {
333 va_list ap;
334 va_start(ap, fmt);
335 fprintf(stderr, "TRACE: ");
336 vfprintf(stderr, fmt, ap);
337 va_end(ap);
341 void print_error(const char *fmt, ...) {
342 va_list ap;
343 va_start(ap, fmt);
344 fprintf(stderr, "ERROR: ");
345 vfprintf(stderr, fmt, ap);
346 va_end(ap);
349 void print_info(const char *fmt, ...) {
350 va_list ap;
351 va_start(ap, fmt);
352 fprintf(stderr, " INFO: ");
353 vfprintf(stderr, fmt, ap);
354 va_end(ap);
357 /* remove directory prefix and file suffix from full path */
358 char *basename(char *path)
360 char *p, *b;
361 static char base[256];
363 /* remove prepended path and extension */
364 b = path;
365 for (p=path; *p; ++p) {
366 if (*p == '/')
367 b = p + 1;
369 strcpy(base, b);
370 for (p=base; *p; ++p) {
371 if (*p == '.') {
372 *p = 0;
373 break;
376 return base;
379 int convbdf(char *path)
381 struct font* pf;
382 int ret = 0;
384 pf = bdf_read_font(path);
385 if (!pf)
386 exit(1);
388 if (gen_c) {
389 if (!oflag) {
390 strcpy(outfile, basename(path));
391 strcat(outfile, ".c");
393 ret |= gen_c_source(pf, outfile);
396 if (gen_h) {
397 if (!oflag) {
398 strcpy(outfile, basename(path));
399 strcat(outfile, ".h");
401 ret |= gen_h_header(pf, outfile);
404 if (gen_fnt) {
405 if (!oflag) {
406 strcpy(outfile, basename(path));
407 strcat(outfile, ".fnt");
409 ret |= gen_fnt_file(pf, outfile);
412 free_font(pf);
413 return ret;
416 int main(int ac, char **av)
418 int ret = 0;
420 ++av; --ac; /* skip av[0] */
421 getopts(&ac, &av); /* read command line options */
423 if (ac < 1 || (!gen_c && !gen_h && !gen_fnt)) {
424 usage();
425 exit(1);
428 if (oflag) {
429 if (ac > 1 || (gen_c && gen_fnt) || (gen_c && gen_h) || (gen_h && gen_fnt)) {
430 usage();
431 exit(1);
435 while (ac > 0) {
436 ret |= convbdf(av[0]);
437 ++av; --ac;
440 exit(ret);
443 /* free font structure */
444 void free_font(struct font* pf)
446 if (!pf)
447 return;
448 if (pf->name)
449 free(pf->name);
450 if (pf->facename)
451 free(pf->facename);
452 if (pf->bits)
453 free(pf->bits);
454 if (pf->offset)
455 free(pf->offset);
456 if (pf->offrot)
457 free(pf->offrot);
458 if (pf->width)
459 free(pf->width);
460 free(pf);
463 /* build incore structure from .bdf file */
464 struct font* bdf_read_font(char *path)
466 FILE *fp;
467 struct font* pf;
469 fp = fopen(path, "rb");
470 if (!fp) {
471 print_error("Error opening file: %s\n", path);
472 return NULL;
475 pf = (struct font*)calloc(1, sizeof(struct font));
476 if (!pf)
477 goto errout;
478 memset(pf, 0, sizeof(struct font));
480 pf->name = strdup(basename(path));
482 if (!bdf_read_header(fp, pf)) {
483 print_error("Error reading font header\n");
484 goto errout;
486 print_trace("Read font header, nchars_decl=%d\n", pf->nchars_declared);
488 if (!bdf_analyze_font(fp, pf)) {
489 print_error("Error analyzing the font\n");
490 goto errout;
492 print_trace("Analyzed font, nchars=%d, maxwidth=%d, asc_over=%d, desc_over=%d\n",
493 pf->nchars, pf->maxwidth, pf->max_over_ascent, pf->max_over_descent);
495 if (pf->nchars != pf->nchars_declared) {
496 print_warning(VL_MISC, "The declared number of chars (%d) "
497 "does not match the real number (%d)\n",
498 pf->nchars_declared, pf->nchars);
501 /* Correct ascent/descent if necessary */
502 pf->ascent = adjust_ascent(pf->ascent_declared, pf->max_over_ascent, &stretch_ascent);
503 if (pf->ascent != pf->ascent_declared) {
504 print_info("Font ascent has been changed from %d to %d\n",
505 pf->ascent_declared, pf->ascent);
507 pf->descent = adjust_ascent(pf->descent, pf->max_over_descent, &stretch_descent);
508 if (pf->descent != pf->descent_declared) {
509 print_info("Font descent has been changed from %d to %d\n",
510 pf->descent_declared, pf->descent);
512 pf->height = pf->ascent + pf->descent;
513 if (pf->height != pf->ascent_declared + pf->descent_declared) {
514 print_warning(VL_CLIP_FONT, "Generated font's height: %d\n", pf->height);
517 if (pf->ascent > pf->max_char_ascent) {
518 print_trace("Font's ascent could be reduced by %d to %d without clipping\n",
519 (pf->ascent - pf->max_char_ascent), pf->max_char_ascent);
521 if (pf->descent > pf->max_char_descent) {
522 print_trace("Font's descent could be reduced by %d to %d without clipping\n",
523 (pf->descent - pf->max_char_descent), pf->max_char_descent);
527 /* Alocate memory */
528 pf->bits_size = pf->size * BITMAP_WORDS(pf->maxwidth) * pf->height;
529 pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t));
530 pf->offset = (int *)malloc(pf->size * sizeof(int));
531 pf->offrot = (unsigned int *)malloc(pf->size * sizeof(unsigned int));
532 pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char));
534 if (!pf->bits || !pf->offset || !pf->offrot || !pf->width) {
535 print_error("no memory for font load\n");
536 goto errout;
539 pf->num_clipped_ascent = pf->num_clipped_descent = pf->num_clipped = 0;
540 pf->max_over_ascent = pf->max_over_descent = 0;
542 if (!bdf_read_bitmaps(fp, pf)) {
543 print_error("Error reading font bitmaps\n");
544 goto errout;
546 print_trace("Read bitmaps\n");
548 if (pf->num_clipped > 0) {
549 print_warning(VL_CLIP_FONT, "%d character(s) out of %d were clipped "
550 "(%d at ascent, %d at descent)\n",
551 pf->num_clipped, pf->nchars,
552 pf->num_clipped_ascent, pf->num_clipped_descent);
553 print_warning(VL_CLIP_FONT, "max overflows: %d pixel(s) at ascent, %d pixel(s) at descent\n",
554 pf->max_over_ascent, pf->max_over_descent);
557 fclose(fp);
558 return pf;
560 errout:
561 fclose(fp);
562 free_font(pf);
563 return NULL;
566 /* read bdf font header information, return 0 on error */
567 int bdf_read_header(FILE *fp, struct font* pf)
569 int encoding;
570 int firstchar = 65535;
571 int lastchar = -1;
572 char buf[256];
573 char facename[256];
574 char copyright[256];
575 int is_header = 1;
577 /* set certain values to errors for later error checking */
578 pf->defaultchar = -1;
579 pf->ascent = -1;
580 pf->descent = -1;
581 pf->default_width = -1;
583 for (;;) {
584 if (!bdf_getline(fp, buf, sizeof(buf))) {
585 print_error("EOF on file\n");
586 return 0;
588 if (isprefix(buf, "FONT ")) { /* not required */
589 if (sscanf(buf, "FONT %[^\n]", facename) != 1) {
590 print_error("bad 'FONT'\n");
591 return 0;
593 pf->facename = strdup(facename);
594 continue;
596 if (isprefix(buf, "COPYRIGHT ")) { /* not required */
597 if (sscanf(buf, "COPYRIGHT \"%[^\"]", copyright) != 1) {
598 print_error("bad 'COPYRIGHT'\n");
599 return 0;
601 pf->copyright = strdup(copyright);
602 continue;
604 if (isprefix(buf, "DEFAULT_CHAR ")) { /* not required */
605 if (sscanf(buf, "DEFAULT_CHAR %d", &pf->defaultchar) != 1) {
606 print_error("bad 'DEFAULT_CHAR'\n");
607 return 0;
610 if (isprefix(buf, "FONT_DESCENT ")) {
611 if (sscanf(buf, "FONT_DESCENT %d", &pf->descent_declared) != 1) {
612 print_error("bad 'FONT_DESCENT'\n");
613 return 0;
615 pf->descent = pf->descent_declared; /* For now */
616 continue;
618 if (isprefix(buf, "FONT_ASCENT ")) {
619 if (sscanf(buf, "FONT_ASCENT %d", &pf->ascent_declared) != 1) {
620 print_error("bad 'FONT_ASCENT'\n");
621 return 0;
623 pf->ascent = pf->ascent_declared; /* For now */
624 continue;
626 if (isprefix(buf, "FONTBOUNDINGBOX ")) {
627 if (sscanf(buf, "FONTBOUNDINGBOX %d %d %d %d",
628 &pf->fbbw, &pf->fbbh, &pf->fbbx, &pf->fbby) != 4) {
629 print_error("bad 'FONTBOUNDINGBOX'\n");
630 return 0;
632 continue;
634 if (isprefix(buf, "CHARS ")) {
635 if (sscanf(buf, "CHARS %d", &pf->nchars_declared) != 1) {
636 print_error("bad 'CHARS'\n");
637 return 0;
639 continue;
641 if (isprefix(buf, "STARTCHAR")) {
642 is_header = 0;
643 continue;
646 /* for BDF version 2.2 */
647 if (is_header && isprefix(buf, "DWIDTH ")) {
648 if (sscanf(buf, "DWIDTH %d", &pf->default_width) != 1) {
649 print_error("bad 'DWIDTH' at font level\n");
650 return 0;
652 continue;
656 * Reading ENCODING is necessary to get firstchar/lastchar
657 * which is needed to pre-calculate our offset and widths
658 * array sizes.
660 if (isprefix(buf, "ENCODING ")) {
661 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
662 print_error("bad 'ENCODING'\n");
663 return 0;
665 if (encoding >= 0 &&
666 encoding <= limit_char &&
667 encoding >= start_char) {
669 if (firstchar > encoding)
670 firstchar = encoding;
671 if (lastchar < encoding)
672 lastchar = encoding;
674 continue;
676 if (strequal(buf, "ENDFONT"))
677 break;
680 /* calc font height */
681 if (pf->ascent < 0 || pf->descent < 0 || firstchar < 0) {
682 print_error("Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
683 return 0;
685 pf->height = pf->ascent + pf->descent;
687 /* calc default char */
688 if (pf->defaultchar < 0 ||
689 pf->defaultchar < firstchar ||
690 pf->defaultchar > limit_char ||
691 pf->defaultchar > lastchar)
692 pf->defaultchar = firstchar;
694 /* calc font size (offset/width entries) */
695 pf->firstchar = firstchar;
696 pf->size = lastchar - firstchar + 1;
698 return 1;
702 * TODO: rework the code to avoid logics duplication in
703 * bdf_read_bitmaps and bdf_analyze_font
707 /* read bdf font bitmaps, return 0 on error */
708 int bdf_read_bitmaps(FILE *fp, struct font* pf)
710 int ofs = 0;
711 int ofr = 0;
712 int i, k, encoding, width;
713 int bbw, bbh, bbx, bby;
714 int proportional = 0;
715 int encodetable = 0;
716 int offset;
717 char buf[256];
718 bitmap_t *ch_bitmap;
719 int ch_words;
721 /* reset file pointer */
722 fseek(fp, 0L, SEEK_SET);
724 /* initially mark offsets as not used */
725 for (i=0; i<pf->size; ++i)
726 pf->offset[i] = -1;
728 for (;;) {
729 if (!bdf_getline(fp, buf, sizeof(buf))) {
730 print_error("EOF on file\n");
731 return 0;
733 if (isprefix(buf, "STARTCHAR")) {
734 encoding = width = -1;
735 bbw = pf->fbbw;
736 bbh = pf->fbbh;
737 bbx = pf->fbbx;
738 bby = pf->fbby;
739 continue;
741 if (isprefix(buf, "ENCODING ")) {
742 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
743 print_error("bad 'ENCODING'\n");
744 return 0;
746 if (encoding < start_char || encoding > limit_char)
747 encoding = -1;
748 continue;
750 if (isprefix(buf, "DWIDTH ")) {
751 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
752 print_error("bad 'DWIDTH'\n");
753 return 0;
755 /* use font boundingbox width if DWIDTH <= 0 */
756 if (width <= 0)
757 width = pf->fbbw - pf->fbbx;
758 continue;
760 if (isprefix(buf, "BBX ")) {
761 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
762 print_error("bad 'BBX'\n");
763 return 0;
765 continue;
767 if (strequal(buf, "BITMAP") || strequal(buf, "BITMAP ")) {
768 int overflow_asc, overflow_desc;
769 int bbh_orig, bby_orig, y;
771 if (encoding < 0)
772 continue;
774 if (width < 0 && pf->default_width > 0)
775 width = pf->default_width;
777 /* set bits offset in encode map */
778 if (pf->offset[encoding-pf->firstchar] != -1) {
779 print_error("duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
780 encoding, encoding);
781 continue;
783 pf->offset[encoding-pf->firstchar] = ofs;
784 pf->offrot[encoding-pf->firstchar] = ofr;
786 /* calc char width */
787 bdf_correct_bbx(&width, &bbx);
788 pf->width[encoding-pf->firstchar] = width;
790 ch_bitmap = pf->bits + ofs;
791 ch_words = BITMAP_WORDS(width);
792 memset(ch_bitmap, 0, BITMAP_BYTES(width) * pf->height); /* clear bitmap */
794 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
795 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
797 bbh_orig = bbh;
798 bby_orig = bby;
800 overflow_asc = bby + bbh - pf->ascent;
801 if (overflow_asc > 0) {
802 pf->num_clipped_ascent++;
803 if (overflow_asc > pf->max_over_ascent) {
804 pf->max_over_ascent = overflow_asc;
806 bbh = MAX(bbh - overflow_asc, 0); /* Clipped -> decrease the height */
807 print_warning(VL_CLIP_CHAR, "character %d goes %d pixel(s)"
808 " beyond the font's ascent, it will be clipped\n",
809 encoding, overflow_asc);
811 overflow_desc = -bby - pf->descent;
812 if (overflow_desc > 0) {
813 pf->num_clipped_descent++;
814 if (overflow_desc > pf->max_over_descent) {
815 pf->max_over_descent = overflow_desc;
817 bby += overflow_desc;
818 bbh = MAX(bbh - overflow_desc, 0); /* Clipped -> decrease the height */
819 print_warning(VL_CLIP_CHAR, "character %d goes %d pixel(s)"
820 " beyond the font's descent, it will be clipped\n",
821 encoding, overflow_desc);
823 if (overflow_asc > 0 || overflow_desc > 0) {
824 pf->num_clipped++;
827 y = bby_orig + bbh_orig; /* 0-based y within the char */
829 /* read bitmaps */
830 for (i=0; ; ++i) {
831 int hexnibbles;
833 if (!bdf_getline(fp, buf, sizeof(buf))) {
834 print_error("EOF reading BITMAP data for character %d\n",
835 encoding);
836 return 0;
838 if (isprefix(buf, "ENDCHAR"))
839 break;
841 y--;
842 if ((y >= pf->ascent) || (y < -pf->descent)) {
843 /* We're beyond the area that Rockbox can render -> clip */
844 --i; /* This line doesn't count */
845 continue;
848 hexnibbles = strlen(buf);
849 for (k=0; k<ch_words; ++k) {
850 int ndx = k * BITMAP_NIBBLES;
851 int padnibbles = hexnibbles - ndx;
852 bitmap_t value;
854 if (padnibbles <= 0)
855 break;
856 if (padnibbles >= (int)BITMAP_NIBBLES)
857 padnibbles = 0;
859 value = bdf_hexval((unsigned char *)buf,
860 ndx, ndx+BITMAP_NIBBLES-1-padnibbles);
861 value <<= padnibbles * BITMAP_NIBBLES;
863 BM(pf->height - pf->descent - bby - bbh + i, k) |=
864 value >> bbx;
865 /* handle overflow into next image word */
866 if (bbx) {
867 BM(pf->height - pf->descent - bby - bbh + i, k+1) =
868 value << (BITMAP_BITSPERIMAGE - bbx);
873 ofs += BITMAP_WORDS(width) * pf->height;
874 ofr += pf->width[encoding-pf->firstchar] * ((pf->height+7)/8);
876 continue;
878 if (strequal(buf, "ENDFONT"))
879 break;
882 /* change unused width values to default char values */
883 for (i=0; i<pf->size; ++i) {
884 int defchar = pf->defaultchar - pf->firstchar;
886 if (pf->offset[i] == -1)
887 pf->width[i] = pf->width[defchar];
890 /* determine whether font doesn't require encode table */
891 #ifdef ROTATE
892 offset = 0;
893 for (i=0; i<pf->size; ++i) {
894 if ((int)pf->offrot[i] != offset) {
895 encodetable = 1;
896 break;
898 offset += pf->maxwidth * ((pf->height + 7) / 8);
900 #else
901 offset = 0;
902 for (i=0; i<pf->size; ++i) {
903 if (pf->offset[i] != offset) {
904 encodetable = 1;
905 break;
907 offset += BITMAP_WORDS(pf->width[i]) * pf->height;
909 #endif
910 if (!encodetable) {
911 free(pf->offset);
912 pf->offset = NULL;
915 /* determine whether font is fixed-width */
916 for (i=0; i<pf->size; ++i) {
917 if (pf->width[i] != pf->maxwidth) {
918 proportional = 1;
919 break;
922 if (!proportional) {
923 free(pf->width);
924 pf->width = NULL;
927 /* reallocate bits array to actual bits used */
928 if (ofs < pf->bits_size) {
929 pf->bits = realloc(pf->bits, ofs * sizeof(bitmap_t));
930 pf->bits_size = ofs;
933 #ifdef ROTATE
934 pf->bits_size = ofr; /* always update, rotated is smaller */
935 #endif
937 return 1;
940 /* read the next non-comment line, returns buf or NULL if EOF */
941 char *bdf_getline(FILE *fp, char *buf, int len)
943 int c;
944 char *b;
946 for (;;) {
947 b = buf;
948 while ((c = getc(fp)) != EOF) {
949 if (c == '\r')
950 continue;
951 if (c == '\n')
952 break;
953 if (b - buf >= (len - 1))
954 break;
955 *b++ = c;
957 *b = '\0';
958 if (c == EOF && b == buf)
959 return NULL;
960 if (b != buf && !isprefix(buf, "COMMENT"))
961 break;
963 return buf;
966 void bdf_correct_bbx(int *width, int *bbx) {
967 if (*bbx < 0) {
968 /* Rockbox can't render overlapping glyphs */
969 *width -= *bbx;
970 *bbx = 0;
974 int bdf_analyze_font(FILE *fp, struct font* pf) {
975 char buf[256];
976 int encoding;
977 int width, bbw, bbh, bbx, bby, ascent, overflow;
978 int read_enc = 0, read_width = 0, read_bbx = 0, read_endchar = 1;
979 int ignore_char = 0;
981 /* reset file pointer */
982 fseek(fp, 0L, SEEK_SET);
984 pf->maxwidth = 0;
985 pf->nchars = 0;
986 pf->max_char_ascent = pf->max_char_descent = 0;
987 pf->max_over_ascent = pf->max_over_descent = 0;
989 for (;;) {
991 if (!bdf_getline(fp, buf, sizeof(buf))) {
992 print_error("EOF on file\n");
993 return 0;
995 if (isprefix(buf, "ENDFONT")) {
996 if (!read_endchar) {
997 print_error("No terminating ENDCHAR for character %d\n", encoding);
998 return 0;
1000 break;
1002 if (isprefix(buf, "STARTCHAR")) {
1003 print_trace("Read STARTCHAR, nchars=%d, read_endchar=%d\n", pf->nchars, read_endchar);
1004 if (!read_endchar) {
1005 print_error("No terminating ENDCHAR for character %d\n", encoding);
1006 return 0;
1008 read_enc = read_width = read_bbx = read_endchar = 0;
1009 continue;
1011 if (isprefix(buf, "ENDCHAR")) {
1012 if (!read_enc) {
1013 print_error("ENCODING is not specified\n");
1014 return 0;
1016 ignore_char = (encoding < start_char || encoding > limit_char);
1017 if (!ignore_char) {
1018 if (!read_width && pf->default_width > 0)
1020 width = pf->default_width;
1021 read_width = 1;
1023 if (!read_width || !read_bbx) {
1024 print_error("WIDTH or BBX is not specified for character %d\n",
1025 encoding);
1027 bdf_correct_bbx(&width, &bbx);
1028 if (width > pf->maxwidth) {
1029 pf->maxwidth = width;
1032 ascent = bby + bbh;
1033 pf->max_char_ascent = MAX(pf->max_char_ascent, ascent);
1034 overflow = ascent - pf->ascent;
1035 pf->max_over_ascent = MAX(pf->max_over_ascent, overflow);
1037 ascent = -bby;
1038 pf->max_char_descent = MAX(pf->max_char_descent, ascent);
1039 overflow = ascent - pf->descent;
1040 pf->max_over_descent = MAX(pf->max_over_descent, overflow);
1042 pf->nchars++;
1043 read_endchar = 1;
1044 continue;
1046 if (isprefix(buf, "ENCODING ")) {
1047 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
1048 print_error("bad 'ENCODING': '%s'\n", buf);
1049 return 0;
1051 read_enc = 1;
1052 continue;
1054 if (isprefix(buf, "DWIDTH ")) {
1055 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
1056 print_error("bad 'DWIDTH': '%s'\n", buf);
1057 return 0;
1059 /* use font boundingbox width if DWIDTH <= 0 */
1060 if (width < 0) {
1061 print_error("Negative char width: %d\n", width);
1062 return 0;
1064 read_width = 1;
1066 if (isprefix(buf, "BBX ")) {
1067 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
1068 print_error("bad 'BBX': '%s'\n", buf);
1069 return 0;
1071 read_bbx = 1;
1072 continue;
1075 return 1;
1078 int adjust_ascent(int ascent, int overflow, struct stretch *stretch) {
1079 int result;
1080 int px = stretch->value;
1081 if (stretch->percent) {
1082 px = ascent * px / 100;
1085 if (stretch->force) {
1086 result = ascent + px;
1088 else {
1089 result = ascent + MIN(overflow, px);
1091 result = MAX(result, 0);
1092 return result;
1096 /* return hex value of portion of buffer */
1097 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2)
1099 bitmap_t val = 0;
1100 int i, c;
1102 for (i=ndx1; i<=ndx2; ++i) {
1103 c = buf[i];
1104 if (c >= '0' && c <= '9')
1105 c -= '0';
1106 else
1107 if (c >= 'A' && c <= 'F')
1108 c = c - 'A' + 10;
1109 else
1110 if (c >= 'a' && c <= 'f')
1111 c = c - 'a' + 10;
1112 else
1113 c = 0;
1114 val = (val << 4) | c;
1116 return val;
1120 #ifdef ROTATE
1123 * Take an bitmap_t bitmap and convert to Rockbox format.
1124 * Used for converting font glyphs for the time being.
1125 * Can use for standard X11 and Win32 images as well.
1126 * See format description in lcd-recorder.c
1128 * Doing it this way keeps fonts in standard formats,
1129 * as well as keeping Rockbox hw bitmap format.
1131 * Returns the size of the rotated glyph (in bytes) or a
1132 * negative value if the glyph could not be rotated.
1134 int rotleft(unsigned char *dst, /* output buffer */
1135 size_t dstlen, /* buffer size */
1136 bitmap_t *src, unsigned int width, unsigned int height,
1137 int char_code)
1139 unsigned int i,j;
1140 unsigned int src_words; /* # words of input image */
1141 unsigned int dst_mask; /* bit mask for destination */
1142 bitmap_t src_mask; /* bit mask for source */
1144 /* How large the buffer should be to hold the rotated bitmap
1145 of a glyph of size (width x height) */
1146 unsigned int needed_size = ((height + 7) / 8) * width;
1148 if (needed_size > dstlen) {
1149 print_error("Character %d: Glyph of size %d x %d can't be rotated "
1150 "(buffer size is %lu, needs %u)\n",
1151 char_code, width, height, (unsigned long)dstlen, needed_size);
1152 return -1;
1155 /* calc words of input image */
1156 src_words = BITMAP_WORDS(width) * height;
1158 /* clear background */
1159 memset(dst, 0, needed_size);
1161 dst_mask = 1;
1163 for (i=0; i < src_words; i++) {
1165 /* calc src input bit */
1166 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
1168 /* for each input column... */
1169 for(j=0; j < width; j++) {
1171 if (src_mask == 0) /* input word done? */
1173 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
1174 i++; /* next input word */
1177 /* if set in input, set in rotated output */
1178 if (src[i] & src_mask)
1179 dst[j] |= dst_mask;
1181 src_mask >>= 1; /* next input bit */
1184 dst_mask <<= 1; /* next output bit (row) */
1185 if (dst_mask > (1 << 7)) /* output bit > 7? */
1187 dst_mask = 1;
1188 dst += width; /* next output byte row */
1191 return needed_size; /* return result size in bytes */
1194 #endif /* ROTATE */
1197 /* generate C source from in-core font */
1198 int gen_c_source(struct font* pf, char *path)
1200 FILE *ofp;
1201 int i;
1202 time_t t = time(0);
1203 #ifdef ROTATE
1204 int ofr = 0;
1205 #else
1206 int did_syncmsg = 0;
1207 bitmap_t *ofs = pf->bits;
1208 #endif
1209 char buf[256];
1210 char obuf[256];
1211 char hdr1[] = {
1212 "/* Generated by convbdf on %s. */\n"
1213 "#include \"font.h\"\n"
1214 "#ifdef HAVE_LCD_BITMAP\n"
1215 "\n"
1216 "/* Font information:\n"
1217 " name: %s\n"
1218 " facename: %s\n"
1219 " w x h: %dx%d\n"
1220 " size: %d\n"
1221 " ascent: %d\n"
1222 " descent: %d\n"
1223 " first char: %d (0x%02x)\n"
1224 " last char: %d (0x%02x)\n"
1225 " default char: %d (0x%02x)\n"
1226 " proportional: %s\n"
1227 " %s\n"
1228 "*/\n"
1229 "\n"
1230 "/* Font character bitmap data. */\n"
1231 "static const unsigned char _font_bits[] = {\n"
1234 ofp = fopen(path, "w");
1235 if (!ofp) {
1236 print_error("Can't create %s\n", path);
1237 return 1;
1240 strcpy(buf, ctime(&t));
1241 buf[strlen(buf)-1] = 0;
1243 fprintf(ofp, hdr1, buf,
1244 pf->name,
1245 pf->facename? pf->facename: "",
1246 pf->maxwidth, pf->height,
1247 pf->size,
1248 pf->ascent, pf->descent,
1249 pf->firstchar, pf->firstchar,
1250 pf->firstchar+pf->size-1, pf->firstchar+pf->size-1,
1251 pf->defaultchar, pf->defaultchar,
1252 pf->width? "yes": "no",
1253 pf->copyright? pf->copyright: "");
1255 /* generate bitmaps */
1256 for (i=0; i<pf->size; ++i) {
1257 int x;
1258 int bitcount = 0;
1259 int width = pf->width ? pf->width[i] : pf->maxwidth;
1260 int height = pf->height;
1261 int char_code = pf->firstchar + i;
1262 bitmap_t *bits;
1263 bitmap_t bitvalue=0;
1265 /* Skip missing glyphs */
1266 if (pf->offset && (pf->offset[i] == -1))
1267 continue;
1269 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1271 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d",
1272 char_code, char_code, width);
1274 if (gen_map) {
1275 fprintf(ofp, "\n +");
1276 for (x=0; x<width; ++x) fprintf(ofp, "-");
1277 fprintf(ofp, "+\n");
1279 x = 0;
1280 while (height > 0) {
1281 if (x == 0) fprintf(ofp, " |");
1283 if (bitcount <= 0) {
1284 bitcount = BITMAP_BITSPERIMAGE;
1285 bitvalue = *bits++;
1288 fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " ");
1290 bitvalue = BITMAP_SHIFTBIT(bitvalue);
1291 --bitcount;
1292 if (++x == width) {
1293 fprintf(ofp, "|\n");
1294 --height;
1295 x = 0;
1296 bitcount = 0;
1299 fprintf(ofp, " +");
1300 for (x=0; x<width; ++x)
1301 fprintf(ofp, "-");
1302 fprintf(ofp, "+ */\n");
1304 else
1305 fprintf(ofp, " */\n");
1307 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1308 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
1310 unsigned char bytemap[ROTATION_BUF_SIZE];
1311 int y8, ix=0;
1313 int size = rotleft(bytemap, sizeof(bytemap), bits, width,
1314 pf->height, char_code);
1315 if (size < 0) {
1316 return -1;
1319 for (y8=0; y8<pf->height; y8+=8) /* column rows */
1321 for (x=0; x<width; x++) {
1322 fprintf(ofp, "0x%02x, ", bytemap[ix]);
1323 ix++;
1325 fprintf(ofp, "\n");
1328 /* update offrot since bits are now in sorted order */
1329 pf->offrot[i] = ofr;
1330 ofr += size;
1332 #else
1333 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) {
1334 fprintf(ofp, "0x%04x,\n", *bits);
1335 if (!did_syncmsg && *bits++ != *ofs++) {
1336 print_warning(VL_MISC, "found encoding values in non-sorted order (not an error).\n");
1337 did_syncmsg = 1;
1340 #endif
1342 fprintf(ofp, "};\n\n");
1344 if (pf->offset) {
1345 /* output offset table */
1346 fprintf(ofp, "/* Character->glyph mapping. */\n"
1347 "static const unsigned short _sysfont_offset[] = {\n");
1349 for (i=0; i<pf->size; ++i) {
1350 int offset = pf->offset[i];
1351 int offrot = pf->offrot[i];
1352 if (offset == -1) {
1353 offset = pf->offset[pf->defaultchar - pf->firstchar];
1354 offrot = pf->offrot[pf->defaultchar - pf->firstchar];
1356 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
1357 #ifdef ROTATE
1358 offrot, i+pf->firstchar);
1359 #else
1360 offset, i+pf->firstchar);
1361 #endif
1363 fprintf(ofp, "};\n\n");
1366 /* output width table for proportional fonts */
1367 if (pf->width) {
1368 fprintf(ofp, "/* Character width data. */\n"
1369 "static const unsigned char _sysfont_width[] = {\n");
1371 for (i=0; i<pf->size; ++i)
1372 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
1373 pf->width[i], i+pf->firstchar);
1374 fprintf(ofp, "};\n\n");
1377 /* output struct font struct */
1378 if (pf->offset)
1379 sprintf(obuf, "_sysfont_offset,");
1380 else
1381 sprintf(obuf, "0, /* no encode table */");
1383 if (pf->width)
1384 sprintf(buf, "_sysfont_width, /* width */");
1385 else
1386 sprintf(buf, "0, /* fixed width */");
1388 fprintf(ofp, "/* Exported structure definition. */\n"
1389 "const struct font sysfont = {\n"
1390 " %d, /* maxwidth */\n"
1391 " %d, /* height */\n"
1392 " %d, /* ascent */\n"
1393 " %d, /* firstchar */\n"
1394 " %d, /* size */\n"
1395 " _font_bits, /* bits */\n"
1396 " %s /* offset */\n"
1397 " %s\n"
1398 " %d, /* defaultchar */\n"
1399 " %d, /* bits_size */\n"
1400 " -1, /* font fd */\n"
1401 " 0, /* buffer start */\n"
1402 " 0, /* ^ position */\n"
1403 " 0, /* ^ end */\n"
1404 " 0, /* ^ size */\n"
1405 " {{0,0,0,0,0},0,0,0}, /* cache */\n"
1406 " 0, /* */\n"
1407 " 0, /* */\n"
1408 " 0, /* */\n"
1409 "};\n"
1410 "#endif /* HAVE_LCD_BITMAP */\n",
1411 pf->maxwidth, pf->height,
1412 pf->ascent,
1413 pf->firstchar,
1414 pf->size,
1415 obuf,
1416 buf,
1417 pf->defaultchar,
1418 pf->bits_size);
1420 return 0;
1423 /* generate C header from in-core font */
1424 int gen_h_header(struct font* pf, char *path)
1426 FILE *ofp;
1427 time_t t = time(0);
1428 char buf[256];
1429 char *hdr1 =
1430 "/* Generated by convbdf on %s. */\n"
1431 "#ifdef HAVE_LCD_BITMAP\n"
1432 "\n"
1433 "/* Font information */\n"
1434 "#define SYSFONT_NAME %s\n"
1435 "#define SYSFONT_FACENAME %s\n"
1436 "#define SYSFONT_WIDTH %d\n"
1437 "#define SYSFONT_HEIGHT %d\n"
1438 "#define SYSFONT_SIZE %d\n"
1439 "#define SYSFONT_ASCENT %d\n";
1440 char *hdr2 =
1441 "#define SYSFONT_DESCENT %d\n"
1442 "#define SYSFONT_FIRST_CHAR %d\n"
1443 "#define SYSFONT_LAST_CHAR %d\n"
1444 "#define SYSFONT_DEFAULT_CHAR %d\n"
1445 "#define SYSFONT_PROPORTIONAL %d\n"
1446 "#define SYSFONT_COPYRIGHT %s\n"
1447 "#define SYSFONT_BITS_SIZE %d\n"
1448 "\n"
1449 "#endif\n";
1451 ofp = fopen(path, "w");
1452 if (!ofp) {
1453 print_error("Can't create %s\n", path);
1454 return 1;
1457 strcpy(buf, ctime(&t));
1458 buf[strlen(buf)-1] = 0;
1460 fprintf(ofp, hdr1, buf,
1461 pf->name,
1462 pf->facename? pf->facename: "",
1463 pf->maxwidth,
1464 pf->height,
1465 pf->size,
1466 pf->ascent);
1468 fprintf(ofp, hdr2,
1469 pf->descent,
1470 pf->firstchar,
1471 pf->firstchar+pf->size-1,
1472 pf->defaultchar,
1473 pf->width? 1: 0,
1474 pf->copyright? pf->copyright: "",
1475 pf->bits_size);
1477 return 0;
1480 static int writebyte(FILE *fp, unsigned char c)
1482 return putc(c, fp) != EOF;
1485 static int writeshort(FILE *fp, unsigned short s)
1487 putc(s, fp);
1488 return putc(s>>8, fp) != EOF;
1491 static int writeint(FILE *fp, unsigned int l)
1493 putc(l, fp);
1494 putc(l>>8, fp);
1495 putc(l>>16, fp);
1496 return putc(l>>24, fp) != EOF;
1499 static int writestr(FILE *fp, char *str, int count)
1501 return (int)fwrite(str, 1, count, fp) == count;
1504 #ifndef ROTATE
1505 static int writestrpad(FILE *fp, char *str, int totlen)
1507 int ret = EOF;
1509 while (str && *str && totlen > 0) {
1510 if (*str) {
1511 ret = putc(*str++, fp);
1512 --totlen;
1515 while (--totlen >= 0)
1516 ret = putc(' ', fp);
1517 return ret;
1519 #endif
1521 /* generate .fnt format file from in-core font */
1522 int gen_fnt_file(struct font* pf, char *path)
1524 FILE *ofp;
1525 int i;
1526 #ifdef ROTATE
1527 int ofr = 0;
1528 #endif
1530 ofp = fopen(path, "wb");
1531 if (!ofp) {
1532 print_error("Can't create %s\n", path);
1533 return 1;
1536 /* write magic and version number */
1537 writestr(ofp, VERSION, 4);
1538 #ifndef ROTATE
1539 /* internal font name */
1540 writestrpad(ofp, pf->name, 64);
1542 /* copyright */
1543 writestrpad(ofp, pf->copyright, 256);
1544 #endif
1545 /* font info */
1546 writeshort(ofp, pf->maxwidth);
1547 writeshort(ofp, pf->height);
1548 writeshort(ofp, pf->ascent);
1549 writeshort(ofp, 0);
1550 writeint(ofp, pf->firstchar);
1551 writeint(ofp, pf->defaultchar);
1552 writeint(ofp, pf->size);
1554 /* variable font data sizes */
1555 writeint(ofp, pf->bits_size); /* # words of bitmap_t */
1556 writeint(ofp, pf->offset? pf->size: 0); /* # ints of offset */
1557 writeint(ofp, pf->width? pf->size: 0); /* # bytes of width */
1558 /* variable font data */
1559 #ifdef ROTATE
1560 for (i=0; i<pf->size; ++i)
1562 bitmap_t* bits;
1563 int width = pf->width ? pf->width[i] : pf->maxwidth;
1564 int size;
1565 int char_code = pf->firstchar + i;
1566 unsigned char bytemap[ROTATION_BUF_SIZE];
1568 /* Skip missing glyphs */
1569 if (pf->offset && (pf->offset[i] == -1))
1570 continue;
1572 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1574 size = rotleft(bytemap, sizeof(bytemap), bits, width, pf->height, char_code);
1575 if (size < 0) {
1576 return -1;
1578 writestr(ofp, (char *)bytemap, size);
1580 /* update offrot since bits are now in sorted order */
1581 pf->offrot[i] = ofr;
1582 ofr += size;
1585 if ( pf->bits_size < 0xFFDB )
1587 /* bitmap offset is small enough, use unsigned short for offset */
1588 if (ftell(ofp) & 1)
1589 writebyte(ofp, 0); /* pad to 16-bit boundary */
1591 else
1593 /* bitmap offset is large then 64K, use unsigned int for offset */
1594 while (ftell(ofp) & 3)
1595 writebyte(ofp, 0); /* pad to 32-bit boundary */
1598 if (pf->offset)
1600 for (i=0; i<pf->size; ++i)
1602 int offrot = pf->offrot[i];
1603 if (pf->offset[i] == -1) {
1604 offrot = pf->offrot[pf->defaultchar - pf->firstchar];
1606 if ( pf->bits_size < 0xFFDB )
1607 writeshort(ofp, offrot);
1608 else
1609 writeint(ofp, offrot);
1613 if (pf->width)
1614 for (i=0; i<pf->size; ++i)
1615 writebyte(ofp, pf->width[i]);
1616 #else
1617 for (i=0; i<pf->bits_size; ++i)
1618 writeshort(ofp, pf->bits[i]);
1619 if (ftell(ofp) & 2)
1620 writeshort(ofp, 0); /* pad to 32-bit boundary */
1622 if (pf->offset)
1623 for (i=0; i<pf->size; ++i) {
1624 int offset = pf->offset[i];
1625 if (offset == -1) {
1626 offset = pf->offset[pf->defaultchar - pf->firstchar];
1628 writeint(ofp, offset);
1631 if (pf->width)
1632 for (i=0; i<pf->size; ++i)
1633 writebyte(ofp, pf->width[i]);
1634 #endif
1635 fclose(ofp);
1636 return 0;