More nice apostrophes
[kugel-rb.git] / tools / convbdf.c
blob9e42cb8d01d1ec4f345863eb2fbbec78b556ce34
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);
426 if (oflag) {
427 if (ac > 1 || (gen_c && gen_fnt) || (gen_c && gen_h) || (gen_h && gen_fnt)) {
428 usage();
429 exit(1);
433 while (ac > 0) {
434 ret |= convbdf(av[0]);
435 ++av; --ac;
438 exit(ret);
441 /* free font structure */
442 void free_font(struct font* pf)
444 if (!pf)
445 return;
446 if (pf->name)
447 free(pf->name);
448 if (pf->facename)
449 free(pf->facename);
450 if (pf->bits)
451 free(pf->bits);
452 if (pf->offset)
453 free(pf->offset);
454 if (pf->offrot)
455 free(pf->offrot);
456 if (pf->width)
457 free(pf->width);
458 free(pf);
461 /* build incore structure from .bdf file */
462 struct font* bdf_read_font(char *path)
464 FILE *fp;
465 struct font* pf;
467 fp = fopen(path, "rb");
468 if (!fp) {
469 print_error("Error opening file: %s\n", path);
470 return NULL;
473 pf = (struct font*)calloc(1, sizeof(struct font));
474 if (!pf)
475 goto errout;
476 memset(pf, 0, sizeof(struct font));
478 pf->name = strdup(basename(path));
480 if (!bdf_read_header(fp, pf)) {
481 print_error("Error reading font header\n");
482 goto errout;
484 print_trace("Read font header, nchars_decl=%d\n", pf->nchars_declared);
486 if (!bdf_analyze_font(fp, pf)) {
487 print_error("Error analyzing the font\n");
488 goto errout;
490 print_trace("Analyzed font, nchars=%d, maxwidth=%d, asc_over=%d, desc_over=%d\n",
491 pf->nchars, pf->maxwidth, pf->max_over_ascent, pf->max_over_descent);
493 if (pf->nchars != pf->nchars_declared) {
494 print_warning(VL_MISC, "The declared number of chars (%d) "
495 "does not match the real number (%d)\n",
496 pf->nchars_declared, pf->nchars);
499 /* Correct ascent/descent if necessary */
500 pf->ascent = adjust_ascent(pf->ascent_declared, pf->max_over_ascent, &stretch_ascent);
501 if (pf->ascent != pf->ascent_declared) {
502 print_info("Font ascent has been changed from %d to %d\n",
503 pf->ascent_declared, pf->ascent);
505 pf->descent = adjust_ascent(pf->descent, pf->max_over_descent, &stretch_descent);
506 if (pf->descent != pf->descent_declared) {
507 print_info("Font descent has been changed from %d to %d\n",
508 pf->descent_declared, pf->descent);
510 pf->height = pf->ascent + pf->descent;
511 if (pf->height != pf->ascent_declared + pf->descent_declared) {
512 print_warning(VL_CLIP_FONT, "Generated font's height: %d\n", pf->height);
515 if (pf->ascent > pf->max_char_ascent) {
516 print_trace("Font's ascent could be reduced by %d to %d without clipping\n",
517 (pf->ascent - pf->max_char_ascent), pf->max_char_ascent);
519 if (pf->descent > pf->max_char_descent) {
520 print_trace("Font's descent could be reduced by %d to %d without clipping\n",
521 (pf->descent - pf->max_char_descent), pf->max_char_descent);
525 /* Alocate memory */
526 pf->bits_size = pf->size * BITMAP_WORDS(pf->maxwidth) * pf->height;
527 pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t));
528 pf->offset = (int *)malloc(pf->size * sizeof(int));
529 pf->offrot = (unsigned int *)malloc(pf->size * sizeof(unsigned int));
530 pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char));
532 if (!pf->bits || !pf->offset || !pf->offrot || !pf->width) {
533 print_error("no memory for font load\n");
534 goto errout;
537 pf->num_clipped_ascent = pf->num_clipped_descent = pf->num_clipped = 0;
538 pf->max_over_ascent = pf->max_over_descent = 0;
540 if (!bdf_read_bitmaps(fp, pf)) {
541 print_error("Error reading font bitmaps\n");
542 goto errout;
544 print_trace("Read bitmaps\n");
546 if (pf->num_clipped > 0) {
547 print_warning(VL_CLIP_FONT, "%d character(s) out of %d were clipped "
548 "(%d at ascent, %d at descent)\n",
549 pf->num_clipped, pf->nchars,
550 pf->num_clipped_ascent, pf->num_clipped_descent);
551 print_warning(VL_CLIP_FONT, "max overflows: %d pixel(s) at ascent, %d pixel(s) at descent\n",
552 pf->max_over_ascent, pf->max_over_descent);
555 fclose(fp);
556 return pf;
558 errout:
559 fclose(fp);
560 free_font(pf);
561 return NULL;
564 /* read bdf font header information, return 0 on error */
565 int bdf_read_header(FILE *fp, struct font* pf)
567 int encoding;
568 int firstchar = 65535;
569 int lastchar = -1;
570 char buf[256];
571 char facename[256];
572 char copyright[256];
573 int is_header = 1;
575 /* set certain values to errors for later error checking */
576 pf->defaultchar = -1;
577 pf->ascent = -1;
578 pf->descent = -1;
579 pf->default_width = -1;
581 for (;;) {
582 if (!bdf_getline(fp, buf, sizeof(buf))) {
583 print_error("EOF on file\n");
584 return 0;
586 if (isprefix(buf, "FONT ")) { /* not required */
587 if (sscanf(buf, "FONT %[^\n]", facename) != 1) {
588 print_error("bad 'FONT'\n");
589 return 0;
591 pf->facename = strdup(facename);
592 continue;
594 if (isprefix(buf, "COPYRIGHT ")) { /* not required */
595 if (sscanf(buf, "COPYRIGHT \"%[^\"]", copyright) != 1) {
596 print_error("bad 'COPYRIGHT'\n");
597 return 0;
599 pf->copyright = strdup(copyright);
600 continue;
602 if (isprefix(buf, "DEFAULT_CHAR ")) { /* not required */
603 if (sscanf(buf, "DEFAULT_CHAR %d", &pf->defaultchar) != 1) {
604 print_error("bad 'DEFAULT_CHAR'\n");
605 return 0;
608 if (isprefix(buf, "FONT_DESCENT ")) {
609 if (sscanf(buf, "FONT_DESCENT %d", &pf->descent_declared) != 1) {
610 print_error("bad 'FONT_DESCENT'\n");
611 return 0;
613 pf->descent = pf->descent_declared; /* For now */
614 continue;
616 if (isprefix(buf, "FONT_ASCENT ")) {
617 if (sscanf(buf, "FONT_ASCENT %d", &pf->ascent_declared) != 1) {
618 print_error("bad 'FONT_ASCENT'\n");
619 return 0;
621 pf->ascent = pf->ascent_declared; /* For now */
622 continue;
624 if (isprefix(buf, "FONTBOUNDINGBOX ")) {
625 if (sscanf(buf, "FONTBOUNDINGBOX %d %d %d %d",
626 &pf->fbbw, &pf->fbbh, &pf->fbbx, &pf->fbby) != 4) {
627 print_error("bad 'FONTBOUNDINGBOX'\n");
628 return 0;
630 continue;
632 if (isprefix(buf, "CHARS ")) {
633 if (sscanf(buf, "CHARS %d", &pf->nchars_declared) != 1) {
634 print_error("bad 'CHARS'\n");
635 return 0;
637 continue;
639 if (isprefix(buf, "STARTCHAR")) {
640 is_header = 0;
641 continue;
644 /* for BDF version 2.2 */
645 if (is_header && isprefix(buf, "DWIDTH ")) {
646 if (sscanf(buf, "DWIDTH %d", &pf->default_width) != 1) {
647 print_error("bad 'DWIDTH' at font level\n");
648 return 0;
650 continue;
654 * Reading ENCODING is necessary to get firstchar/lastchar
655 * which is needed to pre-calculate our offset and widths
656 * array sizes.
658 if (isprefix(buf, "ENCODING ")) {
659 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
660 print_error("bad 'ENCODING'\n");
661 return 0;
663 if (encoding >= 0 &&
664 encoding <= limit_char &&
665 encoding >= start_char) {
667 if (firstchar > encoding)
668 firstchar = encoding;
669 if (lastchar < encoding)
670 lastchar = encoding;
672 continue;
674 if (strequal(buf, "ENDFONT"))
675 break;
678 /* calc font height*/
679 if (pf->ascent < 0 || pf->descent < 0 || firstchar < 0) {
680 print_error("Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
681 return 0;
683 pf->height = pf->ascent + pf->descent;
685 /* calc default char */
686 if (pf->defaultchar < 0 ||
687 pf->defaultchar < firstchar ||
688 pf->defaultchar > limit_char ||
689 pf->defaultchar > lastchar)
690 pf->defaultchar = firstchar;
692 /* calc font size (offset/width entries) */
693 pf->firstchar = firstchar;
694 pf->size = lastchar - firstchar + 1;
696 return 1;
700 * TODO: rework the code to avoid logics duplication in
701 * bdf_read_bitmaps and bdf_analyze_font
705 /* read bdf font bitmaps, return 0 on error */
706 int bdf_read_bitmaps(FILE *fp, struct font* pf)
708 int ofs = 0;
709 int ofr = 0;
710 int i, k, encoding, width;
711 int bbw, bbh, bbx, bby;
712 int proportional = 0;
713 int encodetable = 0;
714 int l;
715 char buf[256];
716 bitmap_t *ch_bitmap;
717 int ch_words;
719 /* reset file pointer */
720 fseek(fp, 0L, SEEK_SET);
722 /* initially mark offsets as not used */
723 for (i=0; i<pf->size; ++i)
724 pf->offset[i] = -1;
726 for (;;) {
727 if (!bdf_getline(fp, buf, sizeof(buf))) {
728 print_error("EOF on file\n");
729 return 0;
731 if (isprefix(buf, "STARTCHAR")) {
732 encoding = width = -1;
733 bbw = pf->fbbw;
734 bbh = pf->fbbh;
735 bbx = pf->fbbx;
736 bby = pf->fbby;
737 continue;
739 if (isprefix(buf, "ENCODING ")) {
740 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
741 print_error("bad 'ENCODING'\n");
742 return 0;
744 if (encoding < start_char || encoding > limit_char)
745 encoding = -1;
746 continue;
748 if (isprefix(buf, "DWIDTH ")) {
749 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
750 print_error("bad 'DWIDTH'\n");
751 return 0;
753 /* use font boundingbox width if DWIDTH <= 0 */
754 if (width <= 0)
755 width = pf->fbbw - pf->fbbx;
756 continue;
758 if (isprefix(buf, "BBX ")) {
759 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
760 print_error("bad 'BBX'\n");
761 return 0;
763 continue;
765 if (strequal(buf, "BITMAP") || strequal(buf, "BITMAP ")) {
766 int overflow_asc, overflow_desc;
767 int bbh_orig, bby_orig, y;
769 if (encoding < 0)
770 continue;
772 if (width < 0 && pf->default_width > 0)
773 width = pf->default_width;
775 /* set bits offset in encode map*/
776 if (pf->offset[encoding-pf->firstchar] != -1) {
777 print_error("duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
778 encoding, encoding);
779 continue;
781 pf->offset[encoding-pf->firstchar] = ofs;
782 pf->offrot[encoding-pf->firstchar] = ofr;
784 /* calc char width */
785 bdf_correct_bbx(&width, &bbx);
786 pf->width[encoding-pf->firstchar] = width;
788 ch_bitmap = pf->bits + ofs;
789 ch_words = BITMAP_WORDS(width);
790 memset(ch_bitmap, 0, BITMAP_BYTES(width) * pf->height); /* clear bitmap */
792 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
793 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
795 bbh_orig = bbh;
796 bby_orig = bby;
798 overflow_asc = bby + bbh - pf->ascent;
799 if (overflow_asc > 0) {
800 pf->num_clipped_ascent++;
801 if (overflow_asc > pf->max_over_ascent) {
802 pf->max_over_ascent = overflow_asc;
804 bbh = MAX(bbh - overflow_asc, 0); /* Clipped -> decrease the height */
805 print_warning(VL_CLIP_CHAR, "character %d goes %d pixel(s)"
806 " beyond the font's ascent, it will be clipped\n",
807 encoding, overflow_asc);
809 overflow_desc = -bby - pf->descent;
810 if (overflow_desc > 0) {
811 pf->num_clipped_descent++;
812 if (overflow_desc > pf->max_over_descent) {
813 pf->max_over_descent = overflow_desc;
815 bby += overflow_desc;
816 bbh = MAX(bbh - overflow_desc, 0); /* Clipped -> decrease the height */
817 print_warning(VL_CLIP_CHAR, "character %d goes %d pixel(s)"
818 " beyond the font's descent, it will be clipped\n",
819 encoding, overflow_desc);
821 if (overflow_asc > 0 || overflow_desc > 0) {
822 pf->num_clipped++;
825 y = bby_orig + bbh_orig; /* 0-based y within the char */
827 /* read bitmaps */
828 for (i=0; ; ++i) {
829 int hexnibbles;
831 if (!bdf_getline(fp, buf, sizeof(buf))) {
832 print_error("EOF reading BITMAP data for character %d\n",
833 encoding);
834 return 0;
836 if (isprefix(buf, "ENDCHAR"))
837 break;
839 y--;
840 if ((y >= pf->ascent) || (y < -pf->descent)) {
841 /* We're beyond the area that Rockbox can render -> clip */
842 --i; /* This line doesn't count */
843 continue;
846 hexnibbles = strlen(buf);
847 for (k=0; k<ch_words; ++k) {
848 int ndx = k * BITMAP_NIBBLES;
849 int padnibbles = hexnibbles - ndx;
850 bitmap_t value;
852 if (padnibbles <= 0)
853 break;
854 if (padnibbles >= (int)BITMAP_NIBBLES)
855 padnibbles = 0;
857 value = bdf_hexval((unsigned char *)buf,
858 ndx, ndx+BITMAP_NIBBLES-1-padnibbles);
859 value <<= padnibbles * BITMAP_NIBBLES;
861 BM(pf->height - pf->descent - bby - bbh + i, k) |=
862 value >> bbx;
863 /* handle overflow into next image word */
864 if (bbx) {
865 BM(pf->height - pf->descent - bby - bbh + i, k+1) =
866 value << (BITMAP_BITSPERIMAGE - bbx);
871 ofs += BITMAP_WORDS(width) * pf->height;
872 ofr += pf->width[encoding-pf->firstchar] * ((pf->height+7)/8);
874 continue;
876 if (strequal(buf, "ENDFONT"))
877 break;
880 /* change unused width values to default char values */
881 for (i=0; i<pf->size; ++i) {
882 int defchar = pf->defaultchar - pf->firstchar;
884 if (pf->offset[i] == -1)
885 pf->width[i] = pf->width[defchar];
888 /* determine whether font doesn't require encode table */
889 #ifdef ROTATE
890 l = 0;
891 for (i=0; i<pf->size; ++i) {
892 if ((int)pf->offrot[i] != l) {
893 encodetable = 1;
894 break;
896 l += pf->maxwidth * ((pf->height + 7) / 8);
898 #else
899 l = 0;
900 for (i=0; i<pf->size; ++i) {
901 if (pf->offset[i] != l) {
902 encodetable = 1;
903 break;
905 l += BITMAP_WORDS(pf->width[i]) * pf->height;
907 #endif
908 if (!encodetable) {
909 free(pf->offset);
910 pf->offset = NULL;
913 /* determine whether font is fixed-width */
914 for (i=0; i<pf->size; ++i) {
915 if (pf->width[i] != pf->maxwidth) {
916 proportional = 1;
917 break;
920 if (!proportional) {
921 free(pf->width);
922 pf->width = NULL;
925 /* reallocate bits array to actual bits used */
926 if (ofs < pf->bits_size) {
927 pf->bits = realloc(pf->bits, ofs * sizeof(bitmap_t));
928 pf->bits_size = ofs;
931 #ifdef ROTATE
932 pf->bits_size = ofr; /* always update, rotated is smaller */
933 #endif
935 return 1;
938 /* read the next non-comment line, returns buf or NULL if EOF */
939 char *bdf_getline(FILE *fp, char *buf, int len)
941 int c;
942 char *b;
944 for (;;) {
945 b = buf;
946 while ((c = getc(fp)) != EOF) {
947 if (c == '\r')
948 continue;
949 if (c == '\n')
950 break;
951 if (b - buf >= (len - 1))
952 break;
953 *b++ = c;
955 *b = '\0';
956 if (c == EOF && b == buf)
957 return NULL;
958 if (b != buf && !isprefix(buf, "COMMENT"))
959 break;
961 return buf;
964 void bdf_correct_bbx(int *width, int *bbx) {
965 if (*bbx < 0) {
966 /* Rockbox can't render overlapping glyphs */
967 *width -= *bbx;
968 *bbx = 0;
972 int bdf_analyze_font(FILE *fp, struct font* pf) {
973 char buf[256];
974 int encoding;
975 int width, bbw, bbh, bbx, bby, ascent, overflow;
976 int read_enc = 0, read_width = 0, read_bbx = 0, read_endchar = 1;
977 int ignore_char = 0;
979 /* reset file pointer */
980 fseek(fp, 0L, SEEK_SET);
982 pf->maxwidth = 0;
983 pf->nchars = 0;
984 pf->max_char_ascent = pf->max_char_descent = 0;
985 pf->max_over_ascent = pf->max_over_descent = 0;
987 for (;;) {
989 if (!bdf_getline(fp, buf, sizeof(buf))) {
990 print_error("EOF on file\n");
991 return 0;
993 if (isprefix(buf, "ENDFONT")) {
994 if (!read_endchar) {
995 print_error("No terminating ENDCHAR for character %d\n", encoding);
996 return 0;
998 break;
1000 if (isprefix(buf, "STARTCHAR")) {
1001 print_trace("Read STARTCHAR, nchars=%d, read_endchar=%d\n", pf->nchars, read_endchar);
1002 if (!read_endchar) {
1003 print_error("No terminating ENDCHAR for character %d\n", encoding);
1004 return 0;
1006 read_enc = read_width = read_bbx = read_endchar = 0;
1007 continue;
1009 if (isprefix(buf, "ENDCHAR")) {
1010 if (!read_enc) {
1011 print_error("ENCODING is not specified\n");
1012 return 0;
1014 ignore_char = (encoding < start_char || encoding > limit_char);
1015 if (!ignore_char) {
1016 if (!read_width && pf->default_width > 0)
1018 width = pf->default_width;
1019 read_width = 1;
1021 if (!read_width || !read_bbx) {
1022 print_error("WIDTH or BBX is not specified for character %d\n",
1023 encoding);
1025 bdf_correct_bbx(&width, &bbx);
1026 if (width > pf->maxwidth) {
1027 pf->maxwidth = width;
1030 ascent = bby + bbh;
1031 pf->max_char_ascent = MAX(pf->max_char_ascent, ascent);
1032 overflow = ascent - pf->ascent;
1033 pf->max_over_ascent = MAX(pf->max_over_ascent, overflow);
1035 ascent = -bby;
1036 pf->max_char_descent = MAX(pf->max_char_descent, ascent);
1037 overflow = ascent - pf->descent;
1038 pf->max_over_descent = MAX(pf->max_over_descent, overflow);
1040 pf->nchars++;
1041 read_endchar = 1;
1042 continue;
1044 if (isprefix(buf, "ENCODING ")) {
1045 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
1046 print_error("bad 'ENCODING': '%s'\n", buf);
1047 return 0;
1049 read_enc = 1;
1050 continue;
1052 if (isprefix(buf, "DWIDTH ")) {
1053 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
1054 print_error("bad 'DWIDTH': '%s'\n", buf);
1055 return 0;
1057 /* use font boundingbox width if DWIDTH <= 0 */
1058 if (width < 0) {
1059 print_error("Negative char width: %d\n", width);
1060 return 0;
1062 read_width = 1;
1064 if (isprefix(buf, "BBX ")) {
1065 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
1066 print_error("bad 'BBX': '%s'\n", buf);
1067 return 0;
1069 read_bbx = 1;
1070 continue;
1073 return 1;
1076 int adjust_ascent(int ascent, int overflow, struct stretch *stretch) {
1077 int result;
1078 int px = stretch->value;
1079 if (stretch->percent) {
1080 px = ascent * px / 100;
1083 if (stretch->force) {
1084 result = ascent + px;
1086 else {
1087 result = ascent + MIN(overflow, px);
1089 result = MAX(result, 0);
1090 return result;
1094 /* return hex value of portion of buffer*/
1095 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2)
1097 bitmap_t val = 0;
1098 int i, c;
1100 for (i=ndx1; i<=ndx2; ++i) {
1101 c = buf[i];
1102 if (c >= '0' && c <= '9')
1103 c -= '0';
1104 else
1105 if (c >= 'A' && c <= 'F')
1106 c = c - 'A' + 10;
1107 else
1108 if (c >= 'a' && c <= 'f')
1109 c = c - 'a' + 10;
1110 else
1111 c = 0;
1112 val = (val << 4) | c;
1114 return val;
1118 #ifdef ROTATE
1121 * Take an bitmap_t bitmap and convert to Rockbox format.
1122 * Used for converting font glyphs for the time being.
1123 * Can use for standard X11 and Win32 images as well.
1124 * See format description in lcd-recorder.c
1126 * Doing it this way keeps fonts in standard formats,
1127 * as well as keeping Rockbox hw bitmap format.
1129 * Returns the size of the rotated glyph (in bytes) or a
1130 * negative value if the glyph could not be rotated.
1132 int rotleft(unsigned char *dst, /* output buffer */
1133 size_t dstlen, /* buffer size */
1134 bitmap_t *src, unsigned int width, unsigned int height,
1135 int char_code)
1137 unsigned int i,j;
1138 unsigned int src_words; /* # words of input image */
1139 unsigned int dst_mask; /* bit mask for destination */
1140 bitmap_t src_mask; /* bit mask for source */
1142 /* How large the buffer should be to hold the rotated bitmap
1143 of a glyph of size (width x height) */
1144 unsigned int needed_size = ((height + 7) / 8) * width;
1146 if (needed_size > dstlen) {
1147 print_error("Character %d: Glyph of size %d x %d can't be rotated "
1148 "(buffer size is %lu, needs %u)\n",
1149 char_code, width, height, (unsigned long)dstlen, needed_size);
1150 return -1;
1153 /* calc words of input image*/
1154 src_words = BITMAP_WORDS(width) * height;
1156 /* clear background*/
1157 memset(dst, 0, needed_size);
1159 dst_mask = 1;
1161 for (i=0; i < src_words; i++) {
1163 /* calc src input bit*/
1164 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
1166 /* for each input column...*/
1167 for(j=0; j < width; j++) {
1169 if (src_mask == 0) /* input word done? */
1171 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
1172 i++; /* next input word */
1175 /* if set in input, set in rotated output */
1176 if (src[i] & src_mask)
1177 dst[j] |= dst_mask;
1179 src_mask >>= 1; /* next input bit */
1182 dst_mask <<= 1; /* next output bit (row) */
1183 if (dst_mask > (1 << 7)) /* output bit > 7? */
1185 dst_mask = 1;
1186 dst += width; /* next output byte row */
1189 return needed_size; /* return result size in bytes */
1192 #endif /* ROTATE */
1195 /* generate C source from in-core font*/
1196 int gen_c_source(struct font* pf, char *path)
1198 FILE *ofp;
1199 int i;
1200 time_t t = time(0);
1201 #ifdef ROTATE
1202 int ofr = 0;
1203 #else
1204 int did_syncmsg = 0;
1205 bitmap_t *ofs = pf->bits;
1206 #endif
1207 char buf[256];
1208 char obuf[256];
1209 char hdr1[] = {
1210 "/* Generated by convbdf on %s. */\n"
1211 "#include \"font.h\"\n"
1212 "#ifdef HAVE_LCD_BITMAP\n"
1213 "\n"
1214 "/* Font information:\n"
1215 " name: %s\n"
1216 " facename: %s\n"
1217 " w x h: %dx%d\n"
1218 " size: %d\n"
1219 " ascent: %d\n"
1220 " descent: %d\n"
1221 " first char: %d (0x%02x)\n"
1222 " last char: %d (0x%02x)\n"
1223 " default char: %d (0x%02x)\n"
1224 " proportional: %s\n"
1225 " %s\n"
1226 "*/\n"
1227 "\n"
1228 "/* Font character bitmap data. */\n"
1229 "static const unsigned char _font_bits[] = {\n"
1232 ofp = fopen(path, "w");
1233 if (!ofp) {
1234 print_error("Can't create %s\n", path);
1235 return 1;
1238 strcpy(buf, ctime(&t));
1239 buf[strlen(buf)-1] = 0;
1241 fprintf(ofp, hdr1, buf,
1242 pf->name,
1243 pf->facename? pf->facename: "",
1244 pf->maxwidth, pf->height,
1245 pf->size,
1246 pf->ascent, pf->descent,
1247 pf->firstchar, pf->firstchar,
1248 pf->firstchar+pf->size-1, pf->firstchar+pf->size-1,
1249 pf->defaultchar, pf->defaultchar,
1250 pf->width? "yes": "no",
1251 pf->copyright? pf->copyright: "");
1253 /* generate bitmaps*/
1254 for (i=0; i<pf->size; ++i) {
1255 int x;
1256 int bitcount = 0;
1257 int width = pf->width ? pf->width[i] : pf->maxwidth;
1258 int height = pf->height;
1259 int char_code = pf->firstchar + i;
1260 bitmap_t *bits;
1261 bitmap_t bitvalue=0;
1263 /* Skip missing glyphs */
1264 if (pf->offset && (pf->offset[i] == -1))
1265 continue;
1267 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1269 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d",
1270 char_code, char_code, width);
1272 if (gen_map) {
1273 fprintf(ofp, "\n +");
1274 for (x=0; x<width; ++x) fprintf(ofp, "-");
1275 fprintf(ofp, "+\n");
1277 x = 0;
1278 while (height > 0) {
1279 if (x == 0) fprintf(ofp, " |");
1281 if (bitcount <= 0) {
1282 bitcount = BITMAP_BITSPERIMAGE;
1283 bitvalue = *bits++;
1286 fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " ");
1288 bitvalue = BITMAP_SHIFTBIT(bitvalue);
1289 --bitcount;
1290 if (++x == width) {
1291 fprintf(ofp, "|\n");
1292 --height;
1293 x = 0;
1294 bitcount = 0;
1297 fprintf(ofp, " +");
1298 for (x=0; x<width; ++x)
1299 fprintf(ofp, "-");
1300 fprintf(ofp, "+ */\n");
1302 else
1303 fprintf(ofp, " */\n");
1305 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1306 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
1308 unsigned char bytemap[ROTATION_BUF_SIZE];
1309 int y8, ix=0;
1311 int size = rotleft(bytemap, sizeof(bytemap), bits, width,
1312 pf->height, char_code);
1313 if (size < 0) {
1314 return -1;
1317 for (y8=0; y8<pf->height; y8+=8) /* column rows */
1319 for (x=0; x<width; x++) {
1320 fprintf(ofp, "0x%02x, ", bytemap[ix]);
1321 ix++;
1323 fprintf(ofp, "\n");
1326 /* update offrot since bits are now in sorted order */
1327 pf->offrot[i] = ofr;
1328 ofr += size;
1331 #else
1332 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) {
1333 fprintf(ofp, "0x%04x,\n", *bits);
1334 if (!did_syncmsg && *bits++ != *ofs++) {
1335 print_warning(VL_MISC, "found encoding values in non-sorted order (not an error).\n");
1336 did_syncmsg = 1;
1339 #endif
1341 fprintf(ofp, "};\n\n");
1343 if (pf->offset) {
1344 /* output offset table*/
1345 fprintf(ofp, "/* Character->glyph mapping. */\n"
1346 "static const unsigned short _sysfont_offset[] = {\n");
1348 for (i=0; i<pf->size; ++i) {
1349 if (pf->offset[i] == -1) {
1350 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
1351 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
1353 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
1354 #ifdef ROTATE
1355 pf->offrot[i], i+pf->firstchar);
1356 #else
1357 pf->offset[i], i+pf->firstchar);
1358 #endif
1360 fprintf(ofp, "};\n\n");
1363 /* output width table for proportional fonts*/
1364 if (pf->width) {
1365 fprintf(ofp, "/* Character width data. */\n"
1366 "static const unsigned char _sysfont_width[] = {\n");
1368 for (i=0; i<pf->size; ++i)
1369 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
1370 pf->width[i], i+pf->firstchar);
1371 fprintf(ofp, "};\n\n");
1374 /* output struct font struct*/
1375 if (pf->offset)
1376 sprintf(obuf, "_sysfont_offset,");
1377 else
1378 sprintf(obuf, "0, /* no encode table */");
1380 if (pf->width)
1381 sprintf(buf, "_sysfont_width, /* width */");
1382 else
1383 sprintf(buf, "0, /* fixed width */");
1385 fprintf(ofp, "/* Exported structure definition. */\n"
1386 "const struct font sysfont = {\n"
1387 " %d, /* maxwidth */\n"
1388 " %d, /* height */\n"
1389 " %d, /* ascent */\n"
1390 " %d, /* firstchar */\n"
1391 " %d, /* size */\n"
1392 " _font_bits, /* bits */\n"
1393 " %s /* offset */\n"
1394 " %s\n"
1395 " %d, /* defaultchar */\n"
1396 " %d, /* bits_size */\n"
1397 " -1, /* font fd */\n"
1398 " 0, /* buffer start */\n"
1399 " 0, /* ^ position */\n"
1400 " 0, /* ^ end */\n"
1401 " 0, /* ^ size */\n"
1402 " {{0,0,0,0,0},0,0,0}, /* cache */\n"
1403 " 0, /* */\n"
1404 " 0, /* */\n"
1405 " 0, /* */\n"
1406 "};\n"
1407 "#endif /* HAVE_LCD_BITMAP */\n",
1408 pf->maxwidth, pf->height,
1409 pf->ascent,
1410 pf->firstchar,
1411 pf->size,
1412 obuf,
1413 buf,
1414 pf->defaultchar,
1415 pf->bits_size);
1417 return 0;
1420 /* generate C header from in-core font*/
1421 int gen_h_header(struct font* pf, char *path)
1423 FILE *ofp;
1424 time_t t = time(0);
1425 char buf[256];
1426 char *hdr1 =
1427 "/* Generated by convbdf on %s. */\n"
1428 "#ifdef HAVE_LCD_BITMAP\n"
1429 "\n"
1430 "/* Font information*/\n"
1431 "#define SYSFONT_NAME %s\n"
1432 "#define SYSFONT_FACENAME %s\n"
1433 "#define SYSFONT_WIDTH %d\n"
1434 "#define SYSFONT_HEIGHT %d\n"
1435 "#define SYSFONT_SIZE %d\n"
1436 "#define SYSFONT_ASCENT %d\n";
1437 char *hdr2 =
1438 "#define SYSFONT_DESCENT %d\n"
1439 "#define SYSFONT_FIRST_CHAR %d\n"
1440 "#define SYSFONT_LAST_CHAR %d\n"
1441 "#define SYSFONT_DEFAULT_CHAR %d\n"
1442 "#define SYSFONT_PROPORTIONAL %d\n"
1443 "#define SYSFONT_COPYRIGHT %s\n"
1444 "#define SYSFONT_BITS_SIZE %d\n"
1445 "\n"
1446 "#endif\n";
1448 ofp = fopen(path, "w");
1449 if (!ofp) {
1450 print_error("Can't create %s\n", path);
1451 return 1;
1454 strcpy(buf, ctime(&t));
1455 buf[strlen(buf)-1] = 0;
1457 fprintf(ofp, hdr1, buf,
1458 pf->name,
1459 pf->facename? pf->facename: "",
1460 pf->maxwidth,
1461 pf->height,
1462 pf->size,
1463 pf->ascent);
1465 fprintf(ofp, hdr2,
1466 pf->descent,
1467 pf->firstchar,
1468 pf->firstchar+pf->size-1,
1469 pf->defaultchar,
1470 pf->width? 1: 0,
1471 pf->copyright? pf->copyright: "",
1472 pf->bits_size);
1474 return 0;
1477 static int writebyte(FILE *fp, unsigned char c)
1479 return putc(c, fp) != EOF;
1482 static int writeshort(FILE *fp, unsigned short s)
1484 putc(s, fp);
1485 return putc(s>>8, fp) != EOF;
1488 static int writeint(FILE *fp, unsigned int l)
1490 putc(l, fp);
1491 putc(l>>8, fp);
1492 putc(l>>16, fp);
1493 return putc(l>>24, fp) != EOF;
1496 static int writestr(FILE *fp, char *str, int count)
1498 return (int)fwrite(str, 1, count, fp) == count;
1501 #ifndef ROTATE
1502 static int writestrpad(FILE *fp, char *str, int totlen)
1504 int ret = EOF;
1506 while (str && *str && totlen > 0) {
1507 if (*str) {
1508 ret = putc(*str++, fp);
1509 --totlen;
1512 while (--totlen >= 0)
1513 ret = putc(' ', fp);
1514 return ret;
1516 #endif
1518 /* generate .fnt format file from in-core font*/
1519 int gen_fnt_file(struct font* pf, char *path)
1521 FILE *ofp;
1522 int i;
1523 #ifdef ROTATE
1524 int ofr = 0;
1525 #endif
1527 ofp = fopen(path, "wb");
1528 if (!ofp) {
1529 print_error("Can't create %s\n", path);
1530 return 1;
1533 /* write magic and version #*/
1534 writestr(ofp, VERSION, 4);
1535 #ifndef ROTATE
1536 /* internal font name*/
1537 writestrpad(ofp, pf->name, 64);
1539 /* copyright*/
1540 writestrpad(ofp, pf->copyright, 256);
1541 #endif
1542 /* font info*/
1543 writeshort(ofp, pf->maxwidth);
1544 writeshort(ofp, pf->height);
1545 writeshort(ofp, pf->ascent);
1546 writeshort(ofp, 0);
1547 writeint(ofp, pf->firstchar);
1548 writeint(ofp, pf->defaultchar);
1549 writeint(ofp, pf->size);
1551 /* variable font data sizes*/
1552 writeint(ofp, pf->bits_size); /* # words of bitmap_t*/
1553 writeint(ofp, pf->offset? pf->size: 0); /* # ints of offset*/
1554 writeint(ofp, pf->width? pf->size: 0); /* # bytes of width*/
1555 /* variable font data*/
1556 #ifdef ROTATE
1557 for (i=0; i<pf->size; ++i)
1559 bitmap_t* bits;
1560 int width = pf->width ? pf->width[i] : pf->maxwidth;
1561 int size;
1562 int char_code = pf->firstchar + i;
1563 unsigned char bytemap[ROTATION_BUF_SIZE];
1565 /* Skip missing glyphs */
1566 if (pf->offset && (pf->offset[i] == -1))
1567 continue;
1569 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1571 size = rotleft(bytemap, sizeof(bytemap), bits, width, pf->height, char_code);
1572 if (size < 0) {
1573 return -1;
1575 writestr(ofp, (char *)bytemap, size);
1577 /* update offrot since bits are now in sorted order */
1578 pf->offrot[i] = ofr;
1579 ofr += size;
1582 if ( pf->bits_size < 0xFFDB )
1584 /* bitmap offset is small enough, use unsigned short for offset */
1585 if (ftell(ofp) & 1)
1586 writebyte(ofp, 0); /* pad to 16-bit boundary*/
1588 else
1590 /* bitmap offset is large then 64K, use unsigned int for offset */
1591 while (ftell(ofp) & 3)
1592 writebyte(ofp, 0); /* pad to 32-bit boundary*/
1595 if (pf->offset)
1597 for (i=0; i<pf->size; ++i)
1599 if (pf->offset[i] == -1) {
1600 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
1602 if ( pf->bits_size < 0xFFDB )
1603 writeshort(ofp, pf->offrot[i]);
1604 else
1605 writeint(ofp, pf->offrot[i]);
1609 if (pf->width)
1610 for (i=0; i<pf->size; ++i)
1611 writebyte(ofp, pf->width[i]);
1612 #else
1613 for (i=0; i<pf->bits_size; ++i)
1614 writeshort(ofp, pf->bits[i]);
1615 if (ftell(ofp) & 2)
1616 writeshort(ofp, 0); /* pad to 32-bit boundary*/
1618 if (pf->offset)
1619 for (i=0; i<pf->size; ++i) {
1620 if (pf->offset[i] == -1) {
1621 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
1623 writeint(ofp, pf->offset[i]);
1626 if (pf->width)
1627 for (i=0; i<pf->size; ++i)
1628 writebyte(ofp, pf->width[i]);
1629 #endif
1630 fclose(ofp);
1631 return 0;