Make error message more informative; increase the max size of rotatable glyphs
[kugel-rb.git] / tools / convbdf.c
blob9b181a4f82f42ede1d5af4357902e92bf4d9b25c
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 /* END font.h*/
78 /* Description of how the ascent/descent is allowed to grow */
79 struct stretch {
80 int value; /* The delta value (in pixels or percents) */
81 int percent; /* Is the value in percents (true) or pixels (false)? */
82 int force; /* MUST the value be set (true) or is it just a max (false) */
85 #define isprefix(buf,str) (!strncmp(buf, str, strlen(str)))
86 #define strequal(s1,s2) (!strcmp(s1, s2))
88 #define MAX(a,b) ((a) > (b) ? (a) : (b))
89 #define MIN(a,b) ((a) < (b) ? (a) : (b))
91 #ifdef ROTATE
92 #define ROTATION_BUF_SIZE 2048
93 #endif
95 /* Depending on the verbosity level some warnings are printed or not */
96 int verbosity_level = 0;
97 int trace = 0;
99 /* Prints a warning of the specified verbosity level. It will only be
100 really printed if the level is >= the level set in the settings */
101 void print_warning(int level, const char *fmt, ...);
102 void print_error(const char *fmt, ...);
103 void print_info(const char *fmt, ...);
104 void print_trace(const char *fmt, ...);
105 #define VL_CLIP_FONT 1 /* Verbosity level for clip related warnings at font level */
106 #define VL_CLIP_CHAR 2 /* Verbosity level for clip related warnings at char level */
107 #define VL_MISC 1 /* Verbosity level for other warnings */
109 int gen_c = 0;
110 int gen_h = 0;
111 int gen_fnt = 0;
112 int gen_map = 1;
113 int start_char = 0;
114 int limit_char = 65535;
115 int oflag = 0;
116 char outfile[256];
118 struct stretch stretch_ascent = { 0, 0, 1 }; /* Don't allow ascent to grow by default */
119 struct stretch stretch_descent = { 0, 0, 1 }; /* Don't allow descent to grow by default */
122 void usage(void);
123 void getopts(int *pac, char ***pav);
124 int convbdf(char *path);
126 void free_font(struct font* pf);
127 struct font* bdf_read_font(char *path);
128 int bdf_read_header(FILE *fp, struct font* pf);
129 int bdf_read_bitmaps(FILE *fp, struct font* pf);
132 Counts the glyphs and determines the max dimensions of glyphs
133 (fills the fields nchars, maxwidth, max_over_ascent, max_over_descent).
134 Returns 0 on failure or not-0 on success.
136 int bdf_analyze_font(FILE *fp, struct font* pf);
137 void bdf_correct_bbx(int *width, int *bbx); /* Corrects bbx and width if bbx<0 */
139 /* Corrects the ascent and returns the new value (value to use) */
140 int adjust_ascent(int ascent, int overflow, struct stretch *stretch);
142 char * bdf_getline(FILE *fp, char *buf, int len);
143 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2);
145 int gen_c_source(struct font* pf, char *path);
146 int gen_h_header(struct font* pf, char *path);
147 int gen_fnt_file(struct font* pf, char *path);
149 void
150 usage(void)
152 /* We use string array because some C compilers issue warnings about too long strings */
153 char *help[] = {
154 "Usage: convbdf [options] [input-files]\n",
155 " convbdf [options] [-o output-file] [single-input-file]\n",
156 "Options:\n",
157 " -c Convert .bdf to .c source file\n",
158 " -h Convert .bdf to .h header file (to create sysfont.h)\n",
159 " -f Convert .bdf to .fnt font file\n",
160 " -s N Start output at character encodings >= N\n",
161 " -l N Limit output to character encodings <= N\n",
162 " -n Don't generate bitmaps as comments in .c file\n",
163 " -a N[%][!] Allow the ascent to grow N pixels/% to avoid glyph clipping\n",
164 " -d N[%][!] Allow the descent to grow N pixels/% to avoid glyph clipping\n",
165 " -v N Verbosity level: 0=quite quiet, 1=more verbose, 2=even more, etc.\n",
166 " -t Print internal tracing messages\n",
167 NULL /* Must be the last element in the array */
170 char **p = help;
171 while (*p != NULL)
172 print_info("%s", *(p++));
176 void parse_ascent_opt(char *val, struct stretch *opt) {
177 char buf[256];
178 char *p;
179 strcpy(buf, val);
181 opt->force = 0;
182 opt->percent = 0;
183 p = buf + strlen(buf);
184 while (p > buf) {
185 p--;
186 if (*p == '%') {
187 opt->percent = 1;
188 *p = '\0';
190 else if (*p == '!') {
191 opt->force = 1;
192 *p = '\0';
194 else {
195 break;
198 opt->value = atoi(buf);
201 /* parse command line options*/
202 void getopts(int *pac, char ***pav)
204 char *p;
205 char **av;
206 int ac;
208 ac = *pac;
209 av = *pav;
210 while (ac > 0 && av[0][0] == '-') {
211 p = &av[0][1];
212 while( *p)
213 switch(*p++) {
214 case ' ': /* multiple -args on av[] */
215 while( *p && *p == ' ')
216 p++;
217 if( *p++ != '-') /* next option must have dash */
218 p = "";
219 break; /* proceed to next option */
220 case 'c': /* generate .c output */
221 gen_c = 1;
222 break;
223 case 'h': /* generate .h output */
224 gen_h = 1;
225 break;
226 case 'f': /* generate .fnt output */
227 gen_fnt = 1;
228 break;
229 case 'n': /* don't gen bitmap comments */
230 gen_map = 0;
231 break;
232 case 'o': /* set output file */
233 oflag = 1;
234 if (*p) {
235 strcpy(outfile, p);
236 while (*p && *p != ' ')
237 p++;
239 else {
240 av++; ac--;
241 if (ac > 0)
242 strcpy(outfile, av[0]);
244 break;
245 case 'l': /* set encoding limit */
246 if (*p) {
247 limit_char = atoi(p);
248 while (*p && *p != ' ')
249 p++;
251 else {
252 av++; ac--;
253 if (ac > 0)
254 limit_char = atoi(av[0]);
256 break;
257 case 's': /* set encoding start */
258 if (*p) {
259 start_char = atoi(p);
260 while (*p && *p != ' ')
261 p++;
263 else {
264 av++; ac--;
265 if (ac > 0)
266 start_char = atoi(av[0]);
268 break;
269 case 'a': /* ascent growth */
270 if (*p) {
271 parse_ascent_opt(p, &stretch_ascent);
272 while (*p && *p != ' ')
273 p++;
275 else {
276 av++; ac--;
277 if (ac > 0)
278 parse_ascent_opt(av[0], &stretch_ascent);
280 break;
281 case 'd': /* descent growth */
282 if (*p) {
283 parse_ascent_opt(p, &stretch_descent);
284 while (*p && *p != ' ')
285 p++;
287 else {
288 av++; ac--;
289 if (ac > 0)
290 parse_ascent_opt(av[0], &stretch_descent);
292 break;
293 case 'v': /* verbosity */
294 if (*p) {
295 verbosity_level = atoi(p);
296 while (*p && *p != ' ')
297 p++;
299 else {
300 av++; ac--;
301 if (ac > 0)
302 verbosity_level = atoi(av[0]);
304 break;
305 case 't': /* tracing */
306 trace = 1;
307 break;
308 default:
309 print_info("Unknown option ignored: %c\n", *(p-1));
311 ++av; --ac;
313 *pac = ac;
314 *pav = av;
317 void print_warning(int level, const char *fmt, ...) {
318 if (verbosity_level >= level) {
319 va_list ap;
320 va_start(ap, fmt);
321 fprintf(stderr, " WARN: ");
322 vfprintf(stderr, fmt, ap);
323 va_end(ap);
327 void print_trace(const char *fmt, ...) {
328 if (trace) {
329 va_list ap;
330 va_start(ap, fmt);
331 fprintf(stderr, "TRACE: ");
332 vfprintf(stderr, fmt, ap);
333 va_end(ap);
337 void print_error(const char *fmt, ...) {
338 va_list ap;
339 va_start(ap, fmt);
340 fprintf(stderr, "ERROR: ");
341 vfprintf(stderr, fmt, ap);
342 va_end(ap);
345 void print_info(const char *fmt, ...) {
346 va_list ap;
347 va_start(ap, fmt);
348 fprintf(stderr, " INFO: ");
349 vfprintf(stderr, fmt, ap);
350 va_end(ap);
353 /* remove directory prefix and file suffix from full path */
354 char *basename(char *path)
356 char *p, *b;
357 static char base[256];
359 /* remove prepended path and extension */
360 b = path;
361 for (p=path; *p; ++p) {
362 if (*p == '/')
363 b = p + 1;
365 strcpy(base, b);
366 for (p=base; *p; ++p) {
367 if (*p == '.') {
368 *p = 0;
369 break;
372 return base;
375 int convbdf(char *path)
377 struct font* pf;
378 int ret = 0;
380 pf = bdf_read_font(path);
381 if (!pf)
382 exit(1);
384 if (gen_c) {
385 if (!oflag) {
386 strcpy(outfile, basename(path));
387 strcat(outfile, ".c");
389 ret |= gen_c_source(pf, outfile);
392 if (gen_h) {
393 if (!oflag) {
394 strcpy(outfile, basename(path));
395 strcat(outfile, ".h");
397 ret |= gen_h_header(pf, outfile);
400 if (gen_fnt) {
401 if (!oflag) {
402 strcpy(outfile, basename(path));
403 strcat(outfile, ".fnt");
405 ret |= gen_fnt_file(pf, outfile);
408 free_font(pf);
409 return ret;
412 int main(int ac, char **av)
414 int ret = 0;
416 ++av; --ac; /* skip av[0] */
417 getopts(&ac, &av); /* read command line options */
419 if (ac < 1 || (!gen_c && !gen_h && !gen_fnt)) {
420 usage();
421 exit(1);
423 if (oflag) {
424 if (ac > 1 || (gen_c && gen_fnt) || (gen_c && gen_h) || (gen_h && gen_fnt)) {
425 usage();
426 exit(1);
430 while (ac > 0) {
431 ret |= convbdf(av[0]);
432 ++av; --ac;
435 exit(ret);
438 /* free font structure */
439 void free_font(struct font* pf)
441 if (!pf)
442 return;
443 if (pf->name)
444 free(pf->name);
445 if (pf->facename)
446 free(pf->facename);
447 if (pf->bits)
448 free(pf->bits);
449 if (pf->offset)
450 free(pf->offset);
451 if (pf->offrot)
452 free(pf->offrot);
453 if (pf->width)
454 free(pf->width);
455 free(pf);
458 /* build incore structure from .bdf file */
459 struct font* bdf_read_font(char *path)
461 FILE *fp;
462 struct font* pf;
464 fp = fopen(path, "rb");
465 if (!fp) {
466 print_error("Error opening file: %s\n", path);
467 return NULL;
470 pf = (struct font*)calloc(1, sizeof(struct font));
471 if (!pf)
472 goto errout;
473 memset(pf, 0, sizeof(struct font));
475 pf->name = strdup(basename(path));
477 if (!bdf_read_header(fp, pf)) {
478 print_error("Error reading font header\n");
479 goto errout;
481 print_trace("Read font header, nchars_decl=%d\n", pf->nchars_declared);
483 if (!bdf_analyze_font(fp, pf)) {
484 print_error("Error analyzing the font\n");
485 goto errout;
487 print_trace("Analyzed font, nchars=%d, maxwidth=%d, asc_over=%d, desc_over=%d\n",
488 pf->nchars, pf->maxwidth, pf->max_over_ascent, pf->max_over_descent);
490 if (pf->nchars != pf->nchars_declared) {
491 print_warning(VL_MISC, "The declared number of chars (%d) "
492 "does not match the real number (%d)\n",
493 pf->nchars_declared, pf->nchars);
496 /* Correct ascent/descent if necessary */
497 pf->ascent = adjust_ascent(pf->ascent_declared, pf->max_over_ascent, &stretch_ascent);
498 if (pf->ascent != pf->ascent_declared) {
499 print_info("Font ascent has been changed from %d to %d\n",
500 pf->ascent_declared, pf->ascent);
502 pf->descent = adjust_ascent(pf->descent, pf->max_over_descent, &stretch_descent);
503 if (pf->descent != pf->descent_declared) {
504 print_info("Font descent has been changed from %d to %d\n",
505 pf->descent_declared, pf->descent);
507 pf->height = pf->ascent + pf->descent;
508 if (pf->height != pf->ascent_declared + pf->descent_declared) {
509 print_warning(VL_CLIP_FONT, "Generated font's height: %d\n", pf->height);
512 if (pf->ascent > pf->max_char_ascent) {
513 print_trace("Font's ascent could be reduced by %d to %d without clipping\n",
514 (pf->ascent - pf->max_char_ascent), pf->max_char_ascent);
516 if (pf->descent > pf->max_char_descent) {
517 print_trace("Font's descent could be reduced by %d to %d without clipping\n",
518 (pf->descent - pf->max_char_descent), pf->max_char_descent);
522 /* Alocate memory */
523 pf->bits_size = pf->size * BITMAP_WORDS(pf->maxwidth) * pf->height;
524 pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t));
525 pf->offset = (int *)malloc(pf->size * sizeof(int));
526 pf->offrot = (unsigned int *)malloc(pf->size * sizeof(unsigned int));
527 pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char));
529 if (!pf->bits || !pf->offset || !pf->offrot || !pf->width) {
530 print_error("no memory for font load\n");
531 goto errout;
534 pf->num_clipped_ascent = pf->num_clipped_descent = pf->num_clipped = 0;
535 pf->max_over_ascent = pf->max_over_descent = 0;
537 if (!bdf_read_bitmaps(fp, pf)) {
538 print_error("Error reading font bitmaps\n");
539 goto errout;
541 print_trace("Read bitmaps\n");
543 if (pf->num_clipped > 0) {
544 print_warning(VL_CLIP_FONT, "%d character(s) out of %d were clipped "
545 "(%d at ascent, %d at descent)\n",
546 pf->num_clipped, pf->nchars,
547 pf->num_clipped_ascent, pf->num_clipped_descent);
548 print_warning(VL_CLIP_FONT, "max overflows: %d pixel(s) at ascent, %d pixel(s) at descent\n",
549 pf->max_over_ascent, pf->max_over_descent);
552 fclose(fp);
553 return pf;
555 errout:
556 fclose(fp);
557 free_font(pf);
558 return NULL;
561 /* read bdf font header information, return 0 on error */
562 int bdf_read_header(FILE *fp, struct font* pf)
564 int encoding;
565 int firstchar = 65535;
566 int lastchar = -1;
567 char buf[256];
568 char facename[256];
569 char copyright[256];
571 /* set certain values to errors for later error checking */
572 pf->defaultchar = -1;
573 pf->ascent = -1;
574 pf->descent = -1;
576 for (;;) {
577 if (!bdf_getline(fp, buf, sizeof(buf))) {
578 print_error("EOF on file\n");
579 return 0;
581 if (isprefix(buf, "FONT ")) { /* not required */
582 if (sscanf(buf, "FONT %[^\n]", facename) != 1) {
583 print_error("bad 'FONT'\n");
584 return 0;
586 pf->facename = strdup(facename);
587 continue;
589 if (isprefix(buf, "COPYRIGHT ")) { /* not required */
590 if (sscanf(buf, "COPYRIGHT \"%[^\"]", copyright) != 1) {
591 print_error("bad 'COPYRIGHT'\n");
592 return 0;
594 pf->copyright = strdup(copyright);
595 continue;
597 if (isprefix(buf, "DEFAULT_CHAR ")) { /* not required */
598 if (sscanf(buf, "DEFAULT_CHAR %d", &pf->defaultchar) != 1) {
599 print_error("bad 'DEFAULT_CHAR'\n");
600 return 0;
603 if (isprefix(buf, "FONT_DESCENT ")) {
604 if (sscanf(buf, "FONT_DESCENT %d", &pf->descent_declared) != 1) {
605 print_error("bad 'FONT_DESCENT'\n");
606 return 0;
608 pf->descent = pf->descent_declared; /* For now */
609 continue;
611 if (isprefix(buf, "FONT_ASCENT ")) {
612 if (sscanf(buf, "FONT_ASCENT %d", &pf->ascent_declared) != 1) {
613 print_error("bad 'FONT_ASCENT'\n");
614 return 0;
616 pf->ascent = pf->ascent_declared; /* For now */
617 continue;
619 if (isprefix(buf, "FONTBOUNDINGBOX ")) {
620 if (sscanf(buf, "FONTBOUNDINGBOX %d %d %d %d",
621 &pf->fbbw, &pf->fbbh, &pf->fbbx, &pf->fbby) != 4) {
622 print_error("bad 'FONTBOUNDINGBOX'\n");
623 return 0;
625 continue;
627 if (isprefix(buf, "CHARS ")) {
628 if (sscanf(buf, "CHARS %d", &pf->nchars_declared) != 1) {
629 print_error("bad 'CHARS'\n");
630 return 0;
632 continue;
636 * Reading ENCODING is necessary to get firstchar/lastchar
637 * which is needed to pre-calculate our offset and widths
638 * array sizes.
640 if (isprefix(buf, "ENCODING ")) {
641 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
642 print_error("bad 'ENCODING'\n");
643 return 0;
645 if (encoding >= 0 &&
646 encoding <= limit_char &&
647 encoding >= start_char) {
649 if (firstchar > encoding)
650 firstchar = encoding;
651 if (lastchar < encoding)
652 lastchar = encoding;
654 continue;
656 if (strequal(buf, "ENDFONT"))
657 break;
660 /* calc font height*/
661 if (pf->ascent < 0 || pf->descent < 0 || firstchar < 0) {
662 print_error("Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
663 return 0;
665 pf->height = pf->ascent + pf->descent;
667 /* calc default char */
668 if (pf->defaultchar < 0 ||
669 pf->defaultchar < firstchar ||
670 pf->defaultchar > limit_char ||
671 pf->defaultchar > lastchar)
672 pf->defaultchar = firstchar;
674 /* calc font size (offset/width entries) */
675 pf->firstchar = firstchar;
676 pf->size = lastchar - firstchar + 1;
678 return 1;
682 * TODO: rework the code to avoid logics duplication in
683 * bdf_read_bitmaps and bdf_analyze_font
687 /* read bdf font bitmaps, return 0 on error */
688 int bdf_read_bitmaps(FILE *fp, struct font* pf)
690 int ofs = 0;
691 int ofr = 0;
692 int i, k, encoding, width;
693 int bbw, bbh, bbx, bby;
694 int proportional = 0;
695 int encodetable = 0;
696 int l;
697 char buf[256];
698 bitmap_t *ch_bitmap;
699 int ch_words;
701 /* reset file pointer */
702 fseek(fp, 0L, SEEK_SET);
704 /* initially mark offsets as not used */
705 for (i=0; i<pf->size; ++i)
706 pf->offset[i] = -1;
708 for (;;) {
709 if (!bdf_getline(fp, buf, sizeof(buf))) {
710 print_error("EOF on file\n");
711 return 0;
713 if (isprefix(buf, "STARTCHAR")) {
714 encoding = width = -1;
715 bbw = pf->fbbw;
716 bbh = pf->fbbh;
717 bbx = pf->fbbx;
718 bby = pf->fbby;
719 continue;
721 if (isprefix(buf, "ENCODING ")) {
722 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
723 print_error("bad 'ENCODING'\n");
724 return 0;
726 if (encoding < start_char || encoding > limit_char)
727 encoding = -1;
728 continue;
730 if (isprefix(buf, "DWIDTH ")) {
731 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
732 print_error("bad 'DWIDTH'\n");
733 return 0;
735 /* use font boundingbox width if DWIDTH <= 0 */
736 if (width <= 0)
737 width = pf->fbbw - pf->fbbx;
738 continue;
740 if (isprefix(buf, "BBX ")) {
741 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
742 print_error("bad 'BBX'\n");
743 return 0;
745 continue;
747 if (strequal(buf, "BITMAP") || strequal(buf, "BITMAP ")) {
748 int overflow_asc, overflow_desc;
749 int bbh_orig, bby_orig, y;
751 if (encoding < 0)
752 continue;
754 /* set bits offset in encode map*/
755 if (pf->offset[encoding-pf->firstchar] != -1) {
756 print_error("duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
757 encoding, encoding);
758 continue;
760 pf->offset[encoding-pf->firstchar] = ofs;
761 pf->offrot[encoding-pf->firstchar] = ofr;
763 /* calc char width */
764 bdf_correct_bbx(&width, &bbx);
765 pf->width[encoding-pf->firstchar] = width;
767 ch_bitmap = pf->bits + ofs;
768 ch_words = BITMAP_WORDS(width);
769 memset(ch_bitmap, 0, BITMAP_BYTES(width) * pf->height); /* clear bitmap */
771 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
772 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
774 bbh_orig = bbh;
775 bby_orig = bby;
777 overflow_asc = bby + bbh - pf->ascent;
778 if (overflow_asc > 0) {
779 pf->num_clipped_ascent++;
780 if (overflow_asc > pf->max_over_ascent) {
781 pf->max_over_ascent = overflow_asc;
783 bbh = MAX(bbh - overflow_asc, 0); /* Clipped -> decrease the height */
784 print_warning(VL_CLIP_CHAR, "character %d goes %d pixel(s)"
785 " beyond the font's ascent, it will be clipped\n",
786 encoding, overflow_asc);
788 overflow_desc = -bby - pf->descent;
789 if (overflow_desc > 0) {
790 pf->num_clipped_descent++;
791 if (overflow_desc > pf->max_over_descent) {
792 pf->max_over_descent = overflow_desc;
794 bby += overflow_desc;
795 bbh = MAX(bbh - overflow_desc, 0); /* Clipped -> decrease the height */
796 print_warning(VL_CLIP_CHAR, "character %d goes %d pixel(s)"
797 " beyond the font's descent, it will be clipped\n",
798 encoding, overflow_desc);
800 if (overflow_asc > 0 || overflow_desc > 0) {
801 pf->num_clipped++;
804 y = bby_orig + bbh_orig; /* 0-based y within the char */
806 /* read bitmaps */
807 for (i=0; ; ++i) {
808 int hexnibbles;
810 if (!bdf_getline(fp, buf, sizeof(buf))) {
811 print_error("EOF reading BITMAP data for character %d\n",
812 encoding);
813 return 0;
815 if (isprefix(buf, "ENDCHAR"))
816 break;
818 y--;
819 if ((y >= pf->ascent) || (y < -pf->descent)) {
820 /* We're beyond the area that Rockbox can render -> clip */
821 --i; /* This line doesn't count */
822 continue;
825 hexnibbles = strlen(buf);
826 for (k=0; k<ch_words; ++k) {
827 int ndx = k * BITMAP_NIBBLES;
828 int padnibbles = hexnibbles - ndx;
829 bitmap_t value;
831 if (padnibbles <= 0)
832 break;
833 if (padnibbles >= (int)BITMAP_NIBBLES)
834 padnibbles = 0;
836 value = bdf_hexval((unsigned char *)buf,
837 ndx, ndx+BITMAP_NIBBLES-1-padnibbles);
838 value <<= padnibbles * BITMAP_NIBBLES;
840 BM(pf->height - pf->descent - bby - bbh + i, k) |=
841 value >> bbx;
842 /* handle overflow into next image word */
843 if (bbx) {
844 BM(pf->height - pf->descent - bby - bbh + i, k+1) =
845 value << (BITMAP_BITSPERIMAGE - bbx);
850 ofs += BITMAP_WORDS(width) * pf->height;
851 ofr += pf->width[encoding-pf->firstchar] * ((pf->height+7)/8);
853 continue;
855 if (strequal(buf, "ENDFONT"))
856 break;
859 /* change unused width values to default char values */
860 for (i=0; i<pf->size; ++i) {
861 int defchar = pf->defaultchar - pf->firstchar;
863 if (pf->offset[i] == -1)
864 pf->width[i] = pf->width[defchar];
867 /* determine whether font doesn't require encode table */
868 #ifdef ROTATE
869 l = 0;
870 for (i=0; i<pf->size; ++i) {
871 if ((int)pf->offrot[i] != l) {
872 encodetable = 1;
873 break;
875 l += pf->maxwidth * ((pf->height + 7) / 8);
877 #else
878 l = 0;
879 for (i=0; i<pf->size; ++i) {
880 if (pf->offset[i] != l) {
881 encodetable = 1;
882 break;
884 l += BITMAP_WORDS(pf->width[i]) * pf->height;
886 #endif
887 if (!encodetable) {
888 free(pf->offset);
889 pf->offset = NULL;
892 /* determine whether font is fixed-width */
893 for (i=0; i<pf->size; ++i) {
894 if (pf->width[i] != pf->maxwidth) {
895 proportional = 1;
896 break;
899 if (!proportional) {
900 free(pf->width);
901 pf->width = NULL;
904 /* reallocate bits array to actual bits used */
905 if (ofs < pf->bits_size) {
906 pf->bits = realloc(pf->bits, ofs * sizeof(bitmap_t));
907 pf->bits_size = ofs;
910 #ifdef ROTATE
911 pf->bits_size = ofr; /* always update, rotated is smaller */
912 #endif
914 return 1;
917 /* read the next non-comment line, returns buf or NULL if EOF */
918 char *bdf_getline(FILE *fp, char *buf, int len)
920 int c;
921 char *b;
923 for (;;) {
924 b = buf;
925 while ((c = getc(fp)) != EOF) {
926 if (c == '\r')
927 continue;
928 if (c == '\n')
929 break;
930 if (b - buf >= (len - 1))
931 break;
932 *b++ = c;
934 *b = '\0';
935 if (c == EOF && b == buf)
936 return NULL;
937 if (b != buf && !isprefix(buf, "COMMENT"))
938 break;
940 return buf;
943 void bdf_correct_bbx(int *width, int *bbx) {
944 if (*bbx < 0) {
945 /* Rockbox can't render overlapping glyphs */
946 *width -= *bbx;
947 *bbx = 0;
951 int bdf_analyze_font(FILE *fp, struct font* pf) {
952 char buf[256];
953 int encoding;
954 int width, bbw, bbh, bbx, bby, ascent, overflow;
955 int read_enc = 0, read_width = 0, read_bbx = 0, read_endchar = 1;
956 int ignore_char = 0;
958 /* reset file pointer */
959 fseek(fp, 0L, SEEK_SET);
961 pf->maxwidth = 0;
962 pf->nchars = 0;
963 pf->max_char_ascent = pf->max_char_descent = 0;
964 pf->max_over_ascent = pf->max_over_descent = 0;
966 for (;;) {
968 if (!bdf_getline(fp, buf, sizeof(buf))) {
969 print_error("EOF on file\n");
970 return 0;
972 if (isprefix(buf, "ENDFONT")) {
973 if (!read_endchar) {
974 print_error("No terminating ENDCHAR for character %d\n", encoding);
975 return 0;
977 break;
979 if (isprefix(buf, "STARTCHAR")) {
980 print_trace("Read STARTCHAR, nchars=%d, read_endchar=%d\n", pf->nchars, read_endchar);
981 if (!read_endchar) {
982 print_error("No terminating ENDCHAR for character %d\n", encoding);
983 return 0;
985 read_enc = read_width = read_bbx = read_endchar = 0;
986 continue;
988 if (isprefix(buf, "ENDCHAR")) {
989 if (!read_enc) {
990 print_error("ENCODING is not specified\n");
991 return 0;
993 ignore_char = (encoding < start_char || encoding > limit_char);
994 if (!ignore_char) {
995 if (!read_width || !read_bbx) {
996 print_error("WIDTH or BBX is not specified for character %d\n",
997 encoding);
999 bdf_correct_bbx(&width, &bbx);
1000 if (width > pf->maxwidth) {
1001 pf->maxwidth = width;
1004 ascent = bby + bbh;
1005 pf->max_char_ascent = MAX(pf->max_char_ascent, ascent);
1006 overflow = ascent - pf->ascent;
1007 pf->max_over_ascent = MAX(pf->max_over_ascent, overflow);
1009 ascent = -bby;
1010 pf->max_char_descent = MAX(pf->max_char_descent, ascent);
1011 overflow = ascent - pf->descent;
1012 pf->max_over_descent = MAX(pf->max_over_descent, overflow);
1014 pf->nchars++;
1015 read_endchar = 1;
1016 continue;
1018 if (isprefix(buf, "ENCODING ")) {
1019 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
1020 print_error("bad 'ENCODING': '%s'\n", buf);
1021 return 0;
1023 read_enc = 1;
1024 continue;
1026 if (isprefix(buf, "DWIDTH ")) {
1027 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
1028 print_error("bad 'DWIDTH': '%s'\n", buf);
1029 return 0;
1031 /* use font boundingbox width if DWIDTH <= 0 */
1032 if (width < 0) {
1033 print_error("Negative char width: %d\n", width);
1034 return 0;
1036 read_width = 1;
1038 if (isprefix(buf, "BBX ")) {
1039 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
1040 print_error("bad 'BBX': '%s'\n", buf);
1041 return 0;
1043 read_bbx = 1;
1044 continue;
1047 return 1;
1050 int adjust_ascent(int ascent, int overflow, struct stretch *stretch) {
1051 int result;
1052 int px = stretch->value;
1053 if (stretch->percent) {
1054 px = ascent * px / 100;
1057 if (stretch->force) {
1058 result = ascent + px;
1060 else {
1061 result = ascent + MIN(overflow, px);
1063 result = MAX(result, 0);
1064 return result;
1068 /* return hex value of portion of buffer*/
1069 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2)
1071 bitmap_t val = 0;
1072 int i, c;
1074 for (i=ndx1; i<=ndx2; ++i) {
1075 c = buf[i];
1076 if (c >= '0' && c <= '9')
1077 c -= '0';
1078 else
1079 if (c >= 'A' && c <= 'F')
1080 c = c - 'A' + 10;
1081 else
1082 if (c >= 'a' && c <= 'f')
1083 c = c - 'a' + 10;
1084 else
1085 c = 0;
1086 val = (val << 4) | c;
1088 return val;
1092 #ifdef ROTATE
1095 * Take an bitmap_t bitmap and convert to Rockbox format.
1096 * Used for converting font glyphs for the time being.
1097 * Can use for standard X11 and Win32 images as well.
1098 * See format description in lcd-recorder.c
1100 * Doing it this way keeps fonts in standard formats,
1101 * as well as keeping Rockbox hw bitmap format.
1103 * Returns the size of the rotated glyph (in bytes) or a
1104 * negative value if the glyph could not be rotated.
1106 int rotleft(unsigned char *dst, /* output buffer */
1107 size_t dstlen, /* buffer size */
1108 bitmap_t *src, unsigned int width, unsigned int height,
1109 int char_code)
1111 unsigned int i,j;
1112 unsigned int src_words; /* # words of input image */
1113 unsigned int dst_mask; /* bit mask for destination */
1114 bitmap_t src_mask; /* bit mask for source */
1116 /* How large the buffer should be to hold the rotated bitmap
1117 of a glyph of size (width x height) */
1118 unsigned int needed_size = ((height + 7) / 8) * width;
1120 if (needed_size > dstlen) {
1121 print_error("Character %d: Glyph of size %d x %d can't be rotated "
1122 "(buffer size is %lu, needs %u)\n",
1123 char_code, width, height, (unsigned long)dstlen, needed_size);
1124 return -1;
1127 /* calc words of input image*/
1128 src_words = BITMAP_WORDS(width) * height;
1130 /* clear background*/
1131 memset(dst, 0, needed_size);
1133 dst_mask = 1;
1135 for (i=0; i < src_words; i++) {
1137 /* calc src input bit*/
1138 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
1140 /* for each input column...*/
1141 for(j=0; j < width; j++) {
1143 if (src_mask == 0) /* input word done? */
1145 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
1146 i++; /* next input word */
1149 /* if set in input, set in rotated output */
1150 if (src[i] & src_mask)
1151 dst[j] |= dst_mask;
1153 src_mask >>= 1; /* next input bit */
1156 dst_mask <<= 1; /* next output bit (row) */
1157 if (dst_mask > (1 << 7)) /* output bit > 7? */
1159 dst_mask = 1;
1160 dst += width; /* next output byte row */
1163 return needed_size; /* return result size in bytes */
1166 #endif /* ROTATE */
1169 /* generate C source from in-core font*/
1170 int gen_c_source(struct font* pf, char *path)
1172 FILE *ofp;
1173 int i;
1174 time_t t = time(0);
1175 #ifdef ROTATE
1176 int ofr = 0;
1177 #else
1178 int did_syncmsg = 0;
1179 bitmap_t *ofs = pf->bits;
1180 #endif
1181 char buf[256];
1182 char obuf[256];
1183 char hdr1[] = {
1184 "/* Generated by convbdf on %s. */\n"
1185 "#include \"font.h\"\n"
1186 "#ifdef HAVE_LCD_BITMAP\n"
1187 "\n"
1188 "/* Font information:\n"
1189 " name: %s\n"
1190 " facename: %s\n"
1191 " w x h: %dx%d\n"
1192 " size: %d\n"
1193 " ascent: %d\n"
1194 " descent: %d\n"
1195 " first char: %d (0x%02x)\n"
1196 " last char: %d (0x%02x)\n"
1197 " default char: %d (0x%02x)\n"
1198 " proportional: %s\n"
1199 " %s\n"
1200 "*/\n"
1201 "\n"
1202 "/* Font character bitmap data. */\n"
1203 "static const unsigned char _font_bits[] = {\n"
1206 ofp = fopen(path, "w");
1207 if (!ofp) {
1208 print_error("Can't create %s\n", path);
1209 return 1;
1212 strcpy(buf, ctime(&t));
1213 buf[strlen(buf)-1] = 0;
1215 fprintf(ofp, hdr1, buf,
1216 pf->name,
1217 pf->facename? pf->facename: "",
1218 pf->maxwidth, pf->height,
1219 pf->size,
1220 pf->ascent, pf->descent,
1221 pf->firstchar, pf->firstchar,
1222 pf->firstchar+pf->size-1, pf->firstchar+pf->size-1,
1223 pf->defaultchar, pf->defaultchar,
1224 pf->width? "yes": "no",
1225 pf->copyright? pf->copyright: "");
1227 /* generate bitmaps*/
1228 for (i=0; i<pf->size; ++i) {
1229 int x;
1230 int bitcount = 0;
1231 int width = pf->width ? pf->width[i] : pf->maxwidth;
1232 int height = pf->height;
1233 int char_code = pf->firstchar + i;
1234 bitmap_t *bits;
1235 bitmap_t bitvalue=0;
1237 /* Skip missing glyphs */
1238 if (pf->offset && (pf->offset[i] == -1))
1239 continue;
1241 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1243 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d",
1244 char_code, char_code, width);
1246 if (gen_map) {
1247 fprintf(ofp, "\n +");
1248 for (x=0; x<width; ++x) fprintf(ofp, "-");
1249 fprintf(ofp, "+\n");
1251 x = 0;
1252 while (height > 0) {
1253 if (x == 0) fprintf(ofp, " |");
1255 if (bitcount <= 0) {
1256 bitcount = BITMAP_BITSPERIMAGE;
1257 bitvalue = *bits++;
1260 fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " ");
1262 bitvalue = BITMAP_SHIFTBIT(bitvalue);
1263 --bitcount;
1264 if (++x == width) {
1265 fprintf(ofp, "|\n");
1266 --height;
1267 x = 0;
1268 bitcount = 0;
1271 fprintf(ofp, " +");
1272 for (x=0; x<width; ++x)
1273 fprintf(ofp, "-");
1274 fprintf(ofp, "+ */\n");
1276 else
1277 fprintf(ofp, " */\n");
1279 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1280 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
1282 unsigned char bytemap[ROTATION_BUF_SIZE];
1283 int y8, ix=0;
1285 int size = rotleft(bytemap, sizeof(bytemap), bits, width,
1286 pf->height, char_code);
1287 if (size < 0) {
1288 return -1;
1291 for (y8=0; y8<pf->height; y8+=8) /* column rows */
1293 for (x=0; x<width; x++) {
1294 fprintf(ofp, "0x%02x, ", bytemap[ix]);
1295 ix++;
1297 fprintf(ofp, "\n");
1300 /* update offrot since bits are now in sorted order */
1301 pf->offrot[i] = ofr;
1302 ofr += size;
1305 #else
1306 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) {
1307 fprintf(ofp, "0x%04x,\n", *bits);
1308 if (!did_syncmsg && *bits++ != *ofs++) {
1309 print_warning(VL_MISC, "found encoding values in non-sorted order (not an error).\n");
1310 did_syncmsg = 1;
1313 #endif
1315 fprintf(ofp, "};\n\n");
1317 if (pf->offset) {
1318 /* output offset table*/
1319 fprintf(ofp, "/* Character->glyph mapping. */\n"
1320 "static const unsigned short _sysfont_offset[] = {\n");
1322 for (i=0; i<pf->size; ++i) {
1323 if (pf->offset[i] == -1) {
1324 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
1325 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
1327 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
1328 #ifdef ROTATE
1329 pf->offrot[i], i+pf->firstchar);
1330 #else
1331 pf->offset[i], i+pf->firstchar);
1332 #endif
1334 fprintf(ofp, "};\n\n");
1337 /* output width table for proportional fonts*/
1338 if (pf->width) {
1339 fprintf(ofp, "/* Character width data. */\n"
1340 "static const unsigned char _sysfont_width[] = {\n");
1342 for (i=0; i<pf->size; ++i)
1343 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
1344 pf->width[i], i+pf->firstchar);
1345 fprintf(ofp, "};\n\n");
1348 /* output struct font struct*/
1349 if (pf->offset)
1350 sprintf(obuf, "_sysfont_offset,");
1351 else
1352 sprintf(obuf, "0, /* no encode table */");
1354 if (pf->width)
1355 sprintf(buf, "_sysfont_width, /* width */");
1356 else
1357 sprintf(buf, "0, /* fixed width */");
1359 fprintf(ofp, "/* Exported structure definition. */\n"
1360 "const struct font sysfont = {\n"
1361 " %d, /* maxwidth */\n"
1362 " %d, /* height */\n"
1363 " %d, /* ascent */\n"
1364 " %d, /* firstchar */\n"
1365 " %d, /* size */\n"
1366 " _font_bits, /* bits */\n"
1367 " %s /* offset */\n"
1368 " %s\n"
1369 " %d, /* defaultchar */\n"
1370 " %d /* bits_size */\n"
1371 "};\n"
1372 "#endif /* HAVE_LCD_BITMAP */\n",
1373 pf->maxwidth, pf->height,
1374 pf->ascent,
1375 pf->firstchar,
1376 pf->size,
1377 obuf,
1378 buf,
1379 pf->defaultchar,
1380 pf->bits_size);
1382 return 0;
1385 /* generate C header from in-core font*/
1386 int gen_h_header(struct font* pf, char *path)
1388 FILE *ofp;
1389 time_t t = time(0);
1390 char buf[256];
1391 char *hdr1 =
1392 "/* Generated by convbdf on %s. */\n"
1393 "#ifdef HAVE_LCD_BITMAP\n"
1394 "\n"
1395 "/* Font information*/\n"
1396 "#define SYSFONT_NAME %s\n"
1397 "#define SYSFONT_FACENAME %s\n"
1398 "#define SYSFONT_WIDTH %d\n"
1399 "#define SYSFONT_HEIGHT %d\n"
1400 "#define SYSFONT_SIZE %d\n"
1401 "#define SYSFONT_ASCENT %d\n";
1402 char *hdr2 =
1403 "#define SYSFONT_DESCENT %d\n"
1404 "#define SYSFONT_FIRST_CHAR %d\n"
1405 "#define SYSFONT_LAST_CHAR %d\n"
1406 "#define SYSFONT_DEFAULT_CHAR %d\n"
1407 "#define SYSFONT_PROPORTIONAL %d\n"
1408 "#define SYSFONT_COPYRIGHT %s\n"
1409 "#define SYSFONT_BITS_SIZE %d\n"
1410 "\n"
1411 "#endif\n";
1413 ofp = fopen(path, "w");
1414 if (!ofp) {
1415 print_error("Can't create %s\n", path);
1416 return 1;
1419 strcpy(buf, ctime(&t));
1420 buf[strlen(buf)-1] = 0;
1422 fprintf(ofp, hdr1, buf,
1423 pf->name,
1424 pf->facename? pf->facename: "",
1425 pf->maxwidth,
1426 pf->height,
1427 pf->size,
1428 pf->ascent);
1430 fprintf(ofp, hdr2,
1431 pf->descent,
1432 pf->firstchar,
1433 pf->firstchar+pf->size-1,
1434 pf->defaultchar,
1435 pf->width? 1: 0,
1436 pf->copyright? pf->copyright: "",
1437 pf->bits_size);
1439 return 0;
1442 static int writebyte(FILE *fp, unsigned char c)
1444 return putc(c, fp) != EOF;
1447 static int writeshort(FILE *fp, unsigned short s)
1449 putc(s, fp);
1450 return putc(s>>8, fp) != EOF;
1453 static int writeint(FILE *fp, unsigned int l)
1455 putc(l, fp);
1456 putc(l>>8, fp);
1457 putc(l>>16, fp);
1458 return putc(l>>24, fp) != EOF;
1461 static int writestr(FILE *fp, char *str, int count)
1463 return (int)fwrite(str, 1, count, fp) == count;
1466 #ifndef ROTATE
1467 static int writestrpad(FILE *fp, char *str, int totlen)
1469 int ret = EOF;
1471 while (str && *str && totlen > 0) {
1472 if (*str) {
1473 ret = putc(*str++, fp);
1474 --totlen;
1477 while (--totlen >= 0)
1478 ret = putc(' ', fp);
1479 return ret;
1481 #endif
1483 /* generate .fnt format file from in-core font*/
1484 int gen_fnt_file(struct font* pf, char *path)
1486 FILE *ofp;
1487 int i;
1488 #ifdef ROTATE
1489 int ofr = 0;
1490 #endif
1492 ofp = fopen(path, "wb");
1493 if (!ofp) {
1494 print_error("Can't create %s\n", path);
1495 return 1;
1498 /* write magic and version #*/
1499 writestr(ofp, VERSION, 4);
1500 #ifndef ROTATE
1501 /* internal font name*/
1502 writestrpad(ofp, pf->name, 64);
1504 /* copyright*/
1505 writestrpad(ofp, pf->copyright, 256);
1506 #endif
1507 /* font info*/
1508 writeshort(ofp, pf->maxwidth);
1509 writeshort(ofp, pf->height);
1510 writeshort(ofp, pf->ascent);
1511 writeshort(ofp, 0);
1512 writeint(ofp, pf->firstchar);
1513 writeint(ofp, pf->defaultchar);
1514 writeint(ofp, pf->size);
1516 /* variable font data sizes*/
1517 writeint(ofp, pf->bits_size); /* # words of bitmap_t*/
1518 writeint(ofp, pf->offset? pf->size: 0); /* # ints of offset*/
1519 writeint(ofp, pf->width? pf->size: 0); /* # bytes of width*/
1520 /* variable font data*/
1521 #ifdef ROTATE
1522 for (i=0; i<pf->size; ++i)
1524 bitmap_t* bits;
1525 int width = pf->width ? pf->width[i] : pf->maxwidth;
1526 int size;
1527 int char_code = pf->firstchar + i;
1528 unsigned char bytemap[ROTATION_BUF_SIZE];
1530 /* Skip missing glyphs */
1531 if (pf->offset && (pf->offset[i] == -1))
1532 continue;
1534 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1536 size = rotleft(bytemap, sizeof(bytemap), bits, width, pf->height, char_code);
1537 if (size < 0) {
1538 return -1;
1540 writestr(ofp, (char *)bytemap, size);
1542 /* update offrot since bits are now in sorted order */
1543 pf->offrot[i] = ofr;
1544 ofr += size;
1547 if ( pf->bits_size < 0xFFDB )
1549 /* bitmap offset is small enough, use unsigned short for offset */
1550 if (ftell(ofp) & 1)
1551 writebyte(ofp, 0); /* pad to 16-bit boundary*/
1553 else
1555 /* bitmap offset is large then 64K, use unsigned int for offset */
1556 while (ftell(ofp) & 3)
1557 writebyte(ofp, 0); /* pad to 32-bit boundary*/
1560 if (pf->offset)
1562 for (i=0; i<pf->size; ++i)
1564 if (pf->offset[i] == -1) {
1565 pf->offrot[i] = pf->offrot[pf->defaultchar - pf->firstchar];
1567 if ( pf->bits_size < 0xFFDB )
1568 writeshort(ofp, pf->offrot[i]);
1569 else
1570 writeint(ofp, pf->offrot[i]);
1574 if (pf->width)
1575 for (i=0; i<pf->size; ++i)
1576 writebyte(ofp, pf->width[i]);
1577 #else
1578 for (i=0; i<pf->bits_size; ++i)
1579 writeshort(ofp, pf->bits[i]);
1580 if (ftell(ofp) & 2)
1581 writeshort(ofp, 0); /* pad to 32-bit boundary*/
1583 if (pf->offset)
1584 for (i=0; i<pf->size; ++i) {
1585 if (pf->offset[i] == -1) {
1586 pf->offset[i] = pf->offset[pf->defaultchar - pf->firstchar];
1588 writeint(ofp, pf->offset[i]);
1591 if (pf->width)
1592 for (i=0; i<pf->size; ++i)
1593 writebyte(ofp, pf->width[i]);
1594 #endif
1595 fclose(ofp);
1596 return 0;