Prepare new maemo release
[maemo-rb.git] / tools / convbdf.c
blob4cc7ec9983173287dd5158f8d666db424f650734
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 int depth; /* depth of the font, 0=1bit 1=4bit */
50 bitmap_t* bits; /* 16-bit right-padded bitmap data */
51 int* offset; /* offsets into bitmap data */
52 unsigned char* width; /* character widths or NULL if fixed */
53 int defaultchar; /* default char (not glyph index) */
54 int bits_size; /* # words of bitmap_t bits */
56 /* unused by runtime system, read in by convbdf */
57 int nchars; /* number of different glyphs */
58 int nchars_declared; /* number of glyphs as declared in the header */
59 int ascent_declared; /* ascent as declared in the header */
60 int descent_declared; /* descent as declared in the header */
61 int max_char_ascent; /* max. char ascent (before adjusting) */
62 int max_char_descent; /* max. char descent (before adjusting) */
63 unsigned int* offrot; /* offsets into rotated bitmap data */
64 char* name; /* font name */
65 char* facename; /* facename of font */
66 char* copyright; /* copyright info for loadable fonts */
67 int pixel_size;
68 int descent;
69 int fbbw, fbbh, fbbx, fbby;
71 /* Max 'overflow' of a char's ascent (descent) over the font's one */
72 int max_over_ascent, max_over_descent;
74 /* The number of clipped ascents/descents/total */
75 int num_clipped_ascent, num_clipped_descent, num_clipped;
77 /* default width in pixels (can be overwritten at char level) */
78 int default_width;
80 /* END font.h */
82 /* Description of how the ascent/descent is allowed to grow */
83 struct stretch {
84 int value; /* The delta value (in pixels or percents) */
85 int percent; /* Is the value in percents (true) or pixels (false)? */
86 int force; /* MUST the value be set (true) or is it just a max (false) */
89 #define isprefix(buf,str) (!strncmp(buf, str, strlen(str)))
90 #define strequal(s1,s2) (!strcmp(s1, s2))
92 #define MAX(a,b) ((a) > (b) ? (a) : (b))
93 #define MIN(a,b) ((a) < (b) ? (a) : (b))
95 #ifdef ROTATE
96 #define ROTATION_BUF_SIZE 2048
97 #endif
99 /* Depending on the verbosity level some warnings are printed or not */
100 int verbosity_level = 0;
101 int trace = 0;
103 /* Prints a warning of the specified verbosity level. It will only be
104 really printed if the level is >= the level set in the settings */
105 void print_warning(int level, const char *fmt, ...);
106 void print_error(const char *fmt, ...);
107 void print_info(const char *fmt, ...);
108 void print_trace(const char *fmt, ...);
109 #define VL_CLIP_FONT 1 /* Verbosity level for clip related warnings at font level */
110 #define VL_CLIP_CHAR 2 /* Verbosity level for clip related warnings at char level */
111 #define VL_MISC 1 /* Verbosity level for other warnings */
113 int gen_c = 0;
114 int gen_h = 0;
115 int gen_fnt = 0;
116 int gen_map = 1;
117 int start_char = 0;
118 int limit_char = 65535;
119 int oflag = 0;
120 char outfile[256];
122 struct stretch stretch_ascent = { 0, 0, 1 }; /* Don't allow ascent to grow by default */
123 struct stretch stretch_descent = { 0, 0, 1 }; /* Don't allow descent to grow by default */
126 void usage(void);
127 void getopts(int *pac, char ***pav);
128 int convbdf(char *path);
130 void free_font(struct font* pf);
131 struct font* bdf_read_font(char *path);
132 int bdf_read_header(FILE *fp, struct font* pf);
133 int bdf_read_bitmaps(FILE *fp, struct font* pf);
136 Counts the glyphs and determines the max dimensions of glyphs
137 (fills the fields nchars, maxwidth, max_over_ascent, max_over_descent).
138 Returns 0 on failure or not-0 on success.
140 int bdf_analyze_font(FILE *fp, struct font* pf);
141 void bdf_correct_bbx(int *width, int *bbx); /* Corrects bbx and width if bbx<0 */
143 /* Corrects the ascent and returns the new value (value to use) */
144 int adjust_ascent(int ascent, int overflow, struct stretch *stretch);
146 char * bdf_getline(FILE *fp, char *buf, int len);
147 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2);
149 int gen_c_source(struct font* pf, char *path);
150 int gen_h_header(struct font* pf, char *path);
151 int gen_fnt_file(struct font* pf, char *path);
153 void
154 usage(void)
156 /* We use string array because some C compilers issue warnings about too long strings */
157 char *help[] = {
158 "Usage: convbdf [options] [input-files]\n",
159 " convbdf [options] [-o output-file] [single-input-file]\n",
160 "Options:\n",
161 " -c Convert .bdf to .c source file\n",
162 " -h Convert .bdf to .h header file (to create sysfont.h)\n",
163 " -f Convert .bdf to .fnt font file\n",
164 " -s N Start output at character encodings >= N\n",
165 " -l N Limit output to character encodings <= N\n",
166 " -n Don't generate bitmaps as comments in .c file\n",
167 " -a N[%][!] Allow the ascent to grow N pixels/% to avoid glyph clipping\n",
168 " -d N[%][!] Allow the descent to grow N pixels/% to avoid glyph clipping\n",
169 " An ! in the -a and -d options forces the growth; N may be negative\n",
170 " -v N Verbosity level: 0=quite quiet, 1=more verbose, 2=even more, etc.\n",
171 " -t Print internal tracing messages\n",
172 NULL /* Must be the last element in the array */
175 char **p = help;
176 while (*p != NULL)
177 print_info("%s", *(p++));
181 void parse_ascent_opt(char *val, struct stretch *opt) {
182 char buf[256];
183 char *p;
184 strcpy(buf, val);
186 opt->force = 0;
187 opt->percent = 0;
188 p = buf + strlen(buf);
189 while (p > buf) {
190 p--;
191 if (*p == '%') {
192 opt->percent = 1;
193 *p = '\0';
195 else if (*p == '!') {
196 opt->force = 1;
197 *p = '\0';
199 else {
200 break;
203 opt->value = atoi(buf);
206 /* parse command line options */
207 void getopts(int *pac, char ***pav)
209 char *p;
210 char **av;
211 int ac;
213 ac = *pac;
214 av = *pav;
215 while (ac > 0 && av[0][0] == '-') {
216 p = &av[0][1];
217 while( *p)
218 switch(*p++) {
219 case ' ': /* multiple -args on av[] */
220 while( *p && *p == ' ')
221 p++;
222 if( *p++ != '-') /* next option must have dash */
223 p = "";
224 break; /* proceed to next option */
225 case 'c': /* generate .c output */
226 gen_c = 1;
227 break;
228 case 'h': /* generate .h output */
229 gen_h = 1;
230 break;
231 case 'f': /* generate .fnt output */
232 gen_fnt = 1;
233 break;
234 case 'n': /* don't gen bitmap comments */
235 gen_map = 0;
236 break;
237 case 'o': /* set output file */
238 oflag = 1;
239 if (*p) {
240 strcpy(outfile, p);
241 while (*p && *p != ' ')
242 p++;
244 else {
245 av++; ac--;
246 if (ac > 0)
247 strcpy(outfile, av[0]);
249 break;
250 case 'l': /* set encoding limit */
251 if (*p) {
252 limit_char = atoi(p);
253 while (*p && *p != ' ')
254 p++;
256 else {
257 av++; ac--;
258 if (ac > 0)
259 limit_char = atoi(av[0]);
261 break;
262 case 's': /* set encoding start */
263 if (*p) {
264 start_char = atoi(p);
265 while (*p && *p != ' ')
266 p++;
268 else {
269 av++; ac--;
270 if (ac > 0)
271 start_char = atoi(av[0]);
273 break;
274 case 'a': /* ascent growth */
275 if (*p) {
276 parse_ascent_opt(p, &stretch_ascent);
277 while (*p && *p != ' ')
278 p++;
280 else {
281 av++; ac--;
282 if (ac > 0)
283 parse_ascent_opt(av[0], &stretch_ascent);
285 break;
286 case 'd': /* descent growth */
287 if (*p) {
288 parse_ascent_opt(p, &stretch_descent);
289 while (*p && *p != ' ')
290 p++;
292 else {
293 av++; ac--;
294 if (ac > 0)
295 parse_ascent_opt(av[0], &stretch_descent);
297 break;
298 case 'v': /* verbosity */
299 if (*p) {
300 verbosity_level = atoi(p);
301 while (*p && *p != ' ')
302 p++;
304 else {
305 av++; ac--;
306 if (ac > 0)
307 verbosity_level = atoi(av[0]);
309 break;
310 case 't': /* tracing */
311 trace = 1;
312 break;
313 default:
314 print_info("Unknown option ignored: %c\n", *(p-1));
316 ++av; --ac;
318 *pac = ac;
319 *pav = av;
322 void print_warning(int level, const char *fmt, ...) {
323 if (verbosity_level >= level) {
324 va_list ap;
325 va_start(ap, fmt);
326 fprintf(stderr, " WARN: ");
327 vfprintf(stderr, fmt, ap);
328 va_end(ap);
332 void print_trace(const char *fmt, ...) {
333 if (trace) {
334 va_list ap;
335 va_start(ap, fmt);
336 fprintf(stderr, "TRACE: ");
337 vfprintf(stderr, fmt, ap);
338 va_end(ap);
342 void print_error(const char *fmt, ...) {
343 va_list ap;
344 va_start(ap, fmt);
345 fprintf(stderr, "ERROR: ");
346 vfprintf(stderr, fmt, ap);
347 va_end(ap);
350 void print_info(const char *fmt, ...) {
351 va_list ap;
352 va_start(ap, fmt);
353 fprintf(stderr, " INFO: ");
354 vfprintf(stderr, fmt, ap);
355 va_end(ap);
358 /* remove directory prefix and file suffix from full path */
359 char *basename(char *path)
361 char *p, *b;
362 static char base[256];
364 /* remove prepended path and extension */
365 b = path;
366 for (p=path; *p; ++p) {
367 if (*p == '/')
368 b = p + 1;
370 strcpy(base, b);
371 for (p=base; *p; ++p) {
372 if (*p == '.') {
373 *p = 0;
374 break;
377 return base;
380 int convbdf(char *path)
382 struct font* pf;
383 int ret = 0;
385 pf = bdf_read_font(path);
386 if (!pf)
387 exit(1);
389 if (gen_c) {
390 if (!oflag) {
391 strcpy(outfile, basename(path));
392 strcat(outfile, ".c");
394 ret |= gen_c_source(pf, outfile);
397 if (gen_h) {
398 if (!oflag) {
399 strcpy(outfile, basename(path));
400 strcat(outfile, ".h");
402 ret |= gen_h_header(pf, outfile);
405 if (gen_fnt) {
406 if (!oflag) {
407 strcpy(outfile, basename(path));
408 strcat(outfile, ".fnt");
410 ret |= gen_fnt_file(pf, outfile);
413 free_font(pf);
414 return ret;
417 int main(int ac, char **av)
419 int ret = 0;
421 ++av; --ac; /* skip av[0] */
422 getopts(&ac, &av); /* read command line options */
424 if (ac < 1 || (!gen_c && !gen_h && !gen_fnt)) {
425 usage();
426 exit(1);
429 if (oflag) {
430 if (ac > 1 || (gen_c && gen_fnt) || (gen_c && gen_h) || (gen_h && gen_fnt)) {
431 usage();
432 exit(1);
436 while (ac > 0) {
437 ret |= convbdf(av[0]);
438 ++av; --ac;
441 exit(ret);
444 /* free font structure */
445 void free_font(struct font* pf)
447 if (!pf)
448 return;
449 if (pf->name)
450 free(pf->name);
451 if (pf->facename)
452 free(pf->facename);
453 if (pf->bits)
454 free(pf->bits);
455 if (pf->offset)
456 free(pf->offset);
457 if (pf->offrot)
458 free(pf->offrot);
459 if (pf->width)
460 free(pf->width);
461 free(pf);
464 /* build incore structure from .bdf file */
465 struct font* bdf_read_font(char *path)
467 FILE *fp;
468 struct font* pf;
470 fp = fopen(path, "rb");
471 if (!fp) {
472 print_error("Error opening file: %s\n", path);
473 return NULL;
476 pf = (struct font*)calloc(1, sizeof(struct font));
477 if (!pf)
478 goto errout;
479 memset(pf, 0, sizeof(struct font));
481 pf->name = strdup(basename(path));
483 if (!bdf_read_header(fp, pf)) {
484 print_error("Error reading font header\n");
485 goto errout;
487 print_trace("Read font header, nchars_decl=%d\n", pf->nchars_declared);
489 if (!bdf_analyze_font(fp, pf)) {
490 print_error("Error analyzing the font\n");
491 goto errout;
493 print_trace("Analyzed font, nchars=%d, maxwidth=%d, asc_over=%d, desc_over=%d\n",
494 pf->nchars, pf->maxwidth, pf->max_over_ascent, pf->max_over_descent);
496 if (pf->nchars != pf->nchars_declared) {
497 print_warning(VL_MISC, "The declared number of chars (%d) "
498 "does not match the real number (%d)\n",
499 pf->nchars_declared, pf->nchars);
502 /* Correct ascent/descent if necessary */
503 pf->ascent = adjust_ascent(pf->ascent_declared, pf->max_over_ascent, &stretch_ascent);
504 if (pf->ascent != pf->ascent_declared) {
505 print_info("Font ascent has been changed from %d to %d\n",
506 pf->ascent_declared, pf->ascent);
508 pf->descent = adjust_ascent(pf->descent, pf->max_over_descent, &stretch_descent);
509 if (pf->descent != pf->descent_declared) {
510 print_info("Font descent has been changed from %d to %d\n",
511 pf->descent_declared, pf->descent);
513 pf->height = pf->ascent + pf->descent;
514 if (pf->height != pf->ascent_declared + pf->descent_declared) {
515 print_warning(VL_CLIP_FONT, "Generated font's height: %d\n", pf->height);
518 if (pf->ascent > pf->max_char_ascent) {
519 print_trace("Font's ascent could be reduced by %d to %d without clipping\n",
520 (pf->ascent - pf->max_char_ascent), pf->max_char_ascent);
522 if (pf->descent > pf->max_char_descent) {
523 print_trace("Font's descent could be reduced by %d to %d without clipping\n",
524 (pf->descent - pf->max_char_descent), pf->max_char_descent);
528 /* Alocate memory */
529 pf->bits_size = pf->size * BITMAP_WORDS(pf->maxwidth) * pf->height;
530 pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t));
531 pf->offset = (int *)malloc(pf->size * sizeof(int));
532 pf->offrot = (unsigned int *)malloc(pf->size * sizeof(unsigned int));
533 pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char));
535 if (!pf->bits || !pf->offset || !pf->offrot || !pf->width) {
536 print_error("no memory for font load\n");
537 goto errout;
540 pf->num_clipped_ascent = pf->num_clipped_descent = pf->num_clipped = 0;
541 pf->max_over_ascent = pf->max_over_descent = 0;
543 if (!bdf_read_bitmaps(fp, pf)) {
544 print_error("Error reading font bitmaps\n");
545 goto errout;
547 print_trace("Read bitmaps\n");
549 if (pf->num_clipped > 0) {
550 print_warning(VL_CLIP_FONT, "%d character(s) out of %d were clipped "
551 "(%d at ascent, %d at descent)\n",
552 pf->num_clipped, pf->nchars,
553 pf->num_clipped_ascent, pf->num_clipped_descent);
554 print_warning(VL_CLIP_FONT, "max overflows: %d pixel(s) at ascent, %d pixel(s) at descent\n",
555 pf->max_over_ascent, pf->max_over_descent);
558 fclose(fp);
559 return pf;
561 errout:
562 fclose(fp);
563 free_font(pf);
564 return NULL;
567 /* read bdf font header information, return 0 on error */
568 int bdf_read_header(FILE *fp, struct font* pf)
570 int encoding;
571 int firstchar = 65535;
572 int lastchar = -1;
573 char buf[256];
574 char facename[256];
575 char copyright[256];
576 int is_header = 1;
578 /* set certain values to errors for later error checking */
579 pf->defaultchar = -1;
580 pf->ascent = -1;
581 pf->descent = -1;
582 pf->default_width = -1;
584 for (;;) {
585 if (!bdf_getline(fp, buf, sizeof(buf))) {
586 print_error("EOF on file\n");
587 return 0;
589 if (isprefix(buf, "FONT ")) { /* not required */
590 if (sscanf(buf, "FONT %[^\n]", facename) != 1) {
591 print_error("bad 'FONT'\n");
592 return 0;
594 pf->facename = strdup(facename);
595 continue;
597 if (isprefix(buf, "COPYRIGHT ")) { /* not required */
598 if (sscanf(buf, "COPYRIGHT \"%[^\"]", copyright) != 1) {
599 print_error("bad 'COPYRIGHT'\n");
600 return 0;
602 pf->copyright = strdup(copyright);
603 continue;
605 if (isprefix(buf, "DEFAULT_CHAR ")) { /* not required */
606 if (sscanf(buf, "DEFAULT_CHAR %d", &pf->defaultchar) != 1) {
607 print_error("bad 'DEFAULT_CHAR'\n");
608 return 0;
611 if (isprefix(buf, "FONT_DESCENT ")) {
612 if (sscanf(buf, "FONT_DESCENT %d", &pf->descent_declared) != 1) {
613 print_error("bad 'FONT_DESCENT'\n");
614 return 0;
616 pf->descent = pf->descent_declared; /* For now */
617 continue;
619 if (isprefix(buf, "FONT_ASCENT ")) {
620 if (sscanf(buf, "FONT_ASCENT %d", &pf->ascent_declared) != 1) {
621 print_error("bad 'FONT_ASCENT'\n");
622 return 0;
624 pf->ascent = pf->ascent_declared; /* For now */
625 continue;
627 if (isprefix(buf, "FONTBOUNDINGBOX ")) {
628 if (sscanf(buf, "FONTBOUNDINGBOX %d %d %d %d",
629 &pf->fbbw, &pf->fbbh, &pf->fbbx, &pf->fbby) != 4) {
630 print_error("bad 'FONTBOUNDINGBOX'\n");
631 return 0;
633 continue;
635 if (isprefix(buf, "CHARS ")) {
636 if (sscanf(buf, "CHARS %d", &pf->nchars_declared) != 1) {
637 print_error("bad 'CHARS'\n");
638 return 0;
640 continue;
642 if (isprefix(buf, "STARTCHAR")) {
643 is_header = 0;
644 continue;
647 /* for BDF version 2.2 */
648 if (is_header && isprefix(buf, "DWIDTH ")) {
649 if (sscanf(buf, "DWIDTH %d", &pf->default_width) != 1) {
650 print_error("bad 'DWIDTH' at font level\n");
651 return 0;
653 continue;
657 * Reading ENCODING is necessary to get firstchar/lastchar
658 * which is needed to pre-calculate our offset and widths
659 * array sizes.
661 if (isprefix(buf, "ENCODING ")) {
662 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
663 print_error("bad 'ENCODING'\n");
664 return 0;
666 if (encoding >= 0 &&
667 encoding <= limit_char &&
668 encoding >= start_char) {
670 if (firstchar > encoding)
671 firstchar = encoding;
672 if (lastchar < encoding)
673 lastchar = encoding;
675 continue;
677 if (strequal(buf, "ENDFONT"))
678 break;
681 /* calc font height */
682 if (pf->ascent < 0 || pf->descent < 0 || firstchar < 0) {
683 print_error("Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n");
684 return 0;
686 pf->height = pf->ascent + pf->descent;
688 /* calc default char */
689 if (pf->defaultchar < 0 ||
690 pf->defaultchar < firstchar ||
691 pf->defaultchar > limit_char ||
692 pf->defaultchar > lastchar)
693 pf->defaultchar = firstchar;
695 /* calc font size (offset/width entries) */
696 pf->firstchar = firstchar;
697 pf->size = lastchar - firstchar + 1;
699 return 1;
703 * TODO: rework the code to avoid logics duplication in
704 * bdf_read_bitmaps and bdf_analyze_font
708 /* read bdf font bitmaps, return 0 on error */
709 int bdf_read_bitmaps(FILE *fp, struct font* pf)
711 int ofs = 0;
712 int ofr = 0;
713 int i, k, encoding, width;
714 int bbw, bbh, bbx, bby;
715 int proportional = 0;
716 int encodetable = 0;
717 int offset;
718 char buf[256];
719 bitmap_t *ch_bitmap;
720 int ch_words;
722 /* reset file pointer */
723 fseek(fp, 0L, SEEK_SET);
725 /* initially mark offsets as not used */
726 for (i=0; i<pf->size; ++i)
727 pf->offset[i] = -1;
729 for (;;) {
730 if (!bdf_getline(fp, buf, sizeof(buf))) {
731 print_error("EOF on file\n");
732 return 0;
734 if (isprefix(buf, "STARTCHAR")) {
735 encoding = width = -1;
736 bbw = pf->fbbw;
737 bbh = pf->fbbh;
738 bbx = pf->fbbx;
739 bby = pf->fbby;
740 continue;
742 if (isprefix(buf, "ENCODING ")) {
743 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
744 print_error("bad 'ENCODING'\n");
745 return 0;
747 if (encoding < start_char || encoding > limit_char)
748 encoding = -1;
749 continue;
751 if (isprefix(buf, "DWIDTH ")) {
752 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
753 print_error("bad 'DWIDTH'\n");
754 return 0;
756 /* use font boundingbox width if DWIDTH <= 0 */
757 if (width <= 0)
758 width = pf->fbbw - pf->fbbx;
759 continue;
761 if (isprefix(buf, "BBX ")) {
762 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
763 print_error("bad 'BBX'\n");
764 return 0;
766 continue;
768 if (strequal(buf, "BITMAP") || strequal(buf, "BITMAP ")) {
769 int overflow_asc, overflow_desc;
770 int bbh_orig, bby_orig, y;
772 if (encoding < 0)
773 continue;
775 if (width < 0 && pf->default_width > 0)
776 width = pf->default_width;
778 /* set bits offset in encode map */
779 if (pf->offset[encoding-pf->firstchar] != -1) {
780 print_error("duplicate encoding for character %d (0x%02x), ignoring duplicate\n",
781 encoding, encoding);
782 continue;
784 pf->offset[encoding-pf->firstchar] = ofs;
785 pf->offrot[encoding-pf->firstchar] = ofr;
787 /* calc char width */
788 bdf_correct_bbx(&width, &bbx);
789 pf->width[encoding-pf->firstchar] = width;
791 ch_bitmap = pf->bits + ofs;
792 ch_words = BITMAP_WORDS(width);
793 memset(ch_bitmap, 0, BITMAP_BYTES(width) * pf->height); /* clear bitmap */
795 #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col)))
796 #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4)
798 bbh_orig = bbh;
799 bby_orig = bby;
801 overflow_asc = bby + bbh - pf->ascent;
802 if (overflow_asc > 0) {
803 pf->num_clipped_ascent++;
804 if (overflow_asc > pf->max_over_ascent) {
805 pf->max_over_ascent = overflow_asc;
807 bbh = MAX(bbh - overflow_asc, 0); /* Clipped -> decrease the height */
808 print_warning(VL_CLIP_CHAR, "character %d goes %d pixel(s)"
809 " beyond the font's ascent, it will be clipped\n",
810 encoding, overflow_asc);
812 overflow_desc = -bby - pf->descent;
813 if (overflow_desc > 0) {
814 pf->num_clipped_descent++;
815 if (overflow_desc > pf->max_over_descent) {
816 pf->max_over_descent = overflow_desc;
818 bby += overflow_desc;
819 bbh = MAX(bbh - overflow_desc, 0); /* Clipped -> decrease the height */
820 print_warning(VL_CLIP_CHAR, "character %d goes %d pixel(s)"
821 " beyond the font's descent, it will be clipped\n",
822 encoding, overflow_desc);
824 if (overflow_asc > 0 || overflow_desc > 0) {
825 pf->num_clipped++;
828 y = bby_orig + bbh_orig; /* 0-based y within the char */
830 /* read bitmaps */
831 for (i=0; ; ++i) {
832 int hexnibbles;
834 if (!bdf_getline(fp, buf, sizeof(buf))) {
835 print_error("EOF reading BITMAP data for character %d\n",
836 encoding);
837 return 0;
839 if (isprefix(buf, "ENDCHAR"))
840 break;
842 y--;
843 if ((y >= pf->ascent) || (y < -pf->descent)) {
844 /* We're beyond the area that Rockbox can render -> clip */
845 --i; /* This line doesn't count */
846 continue;
849 hexnibbles = strlen(buf);
850 for (k=0; k<ch_words; ++k) {
851 int ndx = k * BITMAP_NIBBLES;
852 int padnibbles = hexnibbles - ndx;
853 bitmap_t value;
855 if (padnibbles <= 0)
856 break;
857 if (padnibbles >= (int)BITMAP_NIBBLES)
858 padnibbles = 0;
860 value = bdf_hexval((unsigned char *)buf,
861 ndx, ndx+BITMAP_NIBBLES-1-padnibbles);
862 value <<= padnibbles * BITMAP_NIBBLES;
864 BM(pf->height - pf->descent - bby - bbh + i, k) |=
865 value >> bbx;
866 /* handle overflow into next image word */
867 if (bbx) {
868 BM(pf->height - pf->descent - bby - bbh + i, k+1) =
869 value << (BITMAP_BITSPERIMAGE - bbx);
874 ofs += BITMAP_WORDS(width) * pf->height;
875 ofr += pf->width[encoding-pf->firstchar] * ((pf->height+7)/8);
877 continue;
879 if (strequal(buf, "ENDFONT"))
880 break;
883 /* change unused width values to default char values */
884 for (i=0; i<pf->size; ++i) {
885 int defchar = pf->defaultchar - pf->firstchar;
887 if (pf->offset[i] == -1)
888 pf->width[i] = pf->width[defchar];
891 /* determine whether font doesn't require encode table */
892 #ifdef ROTATE
893 offset = 0;
894 for (i=0; i<pf->size; ++i) {
895 if ((int)pf->offrot[i] != offset) {
896 encodetable = 1;
897 break;
899 offset += pf->maxwidth * ((pf->height + 7) / 8);
901 #else
902 offset = 0;
903 for (i=0; i<pf->size; ++i) {
904 if (pf->offset[i] != offset) {
905 encodetable = 1;
906 break;
908 offset += BITMAP_WORDS(pf->width[i]) * pf->height;
910 #endif
911 if (!encodetable) {
912 free(pf->offset);
913 pf->offset = NULL;
916 /* determine whether font is fixed-width */
917 for (i=0; i<pf->size; ++i) {
918 if (pf->width[i] != pf->maxwidth) {
919 proportional = 1;
920 break;
923 if (!proportional) {
924 free(pf->width);
925 pf->width = NULL;
928 /* reallocate bits array to actual bits used */
929 if (ofs < pf->bits_size) {
930 pf->bits = realloc(pf->bits, ofs * sizeof(bitmap_t));
931 pf->bits_size = ofs;
934 #ifdef ROTATE
935 pf->bits_size = ofr; /* always update, rotated is smaller */
936 #endif
938 return 1;
941 /* read the next non-comment line, returns buf or NULL if EOF */
942 char *bdf_getline(FILE *fp, char *buf, int len)
944 int c;
945 char *b;
947 for (;;) {
948 b = buf;
949 while ((c = getc(fp)) != EOF) {
950 if (c == '\r')
951 continue;
952 if (c == '\n')
953 break;
954 if (b - buf >= (len - 1))
955 break;
956 *b++ = c;
958 *b = '\0';
959 if (c == EOF && b == buf)
960 return NULL;
961 if (b != buf && !isprefix(buf, "COMMENT"))
962 break;
964 return buf;
967 void bdf_correct_bbx(int *width, int *bbx) {
968 if (*bbx < 0) {
969 /* Rockbox can't render overlapping glyphs */
970 *width -= *bbx;
971 *bbx = 0;
975 int bdf_analyze_font(FILE *fp, struct font* pf) {
976 char buf[256];
977 int encoding;
978 int width, bbw, bbh, bbx, bby, ascent, overflow;
979 int read_enc = 0, read_width = 0, read_bbx = 0, read_endchar = 1;
980 int ignore_char = 0;
982 /* reset file pointer */
983 fseek(fp, 0L, SEEK_SET);
985 pf->maxwidth = 0;
986 pf->nchars = 0;
987 pf->max_char_ascent = pf->max_char_descent = 0;
988 pf->max_over_ascent = pf->max_over_descent = 0;
990 for (;;) {
992 if (!bdf_getline(fp, buf, sizeof(buf))) {
993 print_error("EOF on file\n");
994 return 0;
996 if (isprefix(buf, "ENDFONT")) {
997 if (!read_endchar) {
998 print_error("No terminating ENDCHAR for character %d\n", encoding);
999 return 0;
1001 break;
1003 if (isprefix(buf, "STARTCHAR")) {
1004 print_trace("Read STARTCHAR, nchars=%d, read_endchar=%d\n", pf->nchars, read_endchar);
1005 if (!read_endchar) {
1006 print_error("No terminating ENDCHAR for character %d\n", encoding);
1007 return 0;
1009 read_enc = read_width = read_bbx = read_endchar = 0;
1010 continue;
1012 if (isprefix(buf, "ENDCHAR")) {
1013 if (!read_enc) {
1014 print_error("ENCODING is not specified\n");
1015 return 0;
1017 ignore_char = (encoding < start_char || encoding > limit_char);
1018 if (!ignore_char) {
1019 if (!read_width && pf->default_width > 0)
1021 width = pf->default_width;
1022 read_width = 1;
1024 if (!read_width || !read_bbx) {
1025 print_error("WIDTH or BBX is not specified for character %d\n",
1026 encoding);
1028 bdf_correct_bbx(&width, &bbx);
1029 if (width > pf->maxwidth) {
1030 pf->maxwidth = width;
1033 ascent = bby + bbh;
1034 pf->max_char_ascent = MAX(pf->max_char_ascent, ascent);
1035 overflow = ascent - pf->ascent;
1036 pf->max_over_ascent = MAX(pf->max_over_ascent, overflow);
1038 ascent = -bby;
1039 pf->max_char_descent = MAX(pf->max_char_descent, ascent);
1040 overflow = ascent - pf->descent;
1041 pf->max_over_descent = MAX(pf->max_over_descent, overflow);
1043 pf->nchars++;
1044 read_endchar = 1;
1045 continue;
1047 if (isprefix(buf, "ENCODING ")) {
1048 if (sscanf(buf, "ENCODING %d", &encoding) != 1) {
1049 print_error("bad 'ENCODING': '%s'\n", buf);
1050 return 0;
1052 read_enc = 1;
1053 continue;
1055 if (isprefix(buf, "DWIDTH ")) {
1056 if (sscanf(buf, "DWIDTH %d", &width) != 1) {
1057 print_error("bad 'DWIDTH': '%s'\n", buf);
1058 return 0;
1060 /* use font boundingbox width if DWIDTH <= 0 */
1061 if (width < 0) {
1062 print_error("Negative char width: %d\n", width);
1063 return 0;
1065 read_width = 1;
1067 if (isprefix(buf, "BBX ")) {
1068 if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) {
1069 print_error("bad 'BBX': '%s'\n", buf);
1070 return 0;
1072 read_bbx = 1;
1073 continue;
1076 return 1;
1079 int adjust_ascent(int ascent, int overflow, struct stretch *stretch) {
1080 int result;
1081 int px = stretch->value;
1082 if (stretch->percent) {
1083 px = ascent * px / 100;
1086 if (stretch->force) {
1087 result = ascent + px;
1089 else {
1090 result = ascent + MIN(overflow, px);
1092 result = MAX(result, 0);
1093 return result;
1097 /* return hex value of portion of buffer */
1098 bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2)
1100 bitmap_t val = 0;
1101 int i, c;
1103 for (i=ndx1; i<=ndx2; ++i) {
1104 c = buf[i];
1105 if (c >= '0' && c <= '9')
1106 c -= '0';
1107 else
1108 if (c >= 'A' && c <= 'F')
1109 c = c - 'A' + 10;
1110 else
1111 if (c >= 'a' && c <= 'f')
1112 c = c - 'a' + 10;
1113 else
1114 c = 0;
1115 val = (val << 4) | c;
1117 return val;
1121 #ifdef ROTATE
1124 * Take an bitmap_t bitmap and convert to Rockbox format.
1125 * Used for converting font glyphs for the time being.
1126 * Can use for standard X11 and Win32 images as well.
1127 * See format description in lcd-recorder.c
1129 * Doing it this way keeps fonts in standard formats,
1130 * as well as keeping Rockbox hw bitmap format.
1132 * Returns the size of the rotated glyph (in bytes) or a
1133 * negative value if the glyph could not be rotated.
1135 int rotleft(unsigned char *dst, /* output buffer */
1136 size_t dstlen, /* buffer size */
1137 bitmap_t *src, unsigned int width, unsigned int height,
1138 int char_code)
1140 unsigned int i,j;
1141 unsigned int src_words; /* # words of input image */
1142 unsigned int dst_mask; /* bit mask for destination */
1143 bitmap_t src_mask; /* bit mask for source */
1145 /* How large the buffer should be to hold the rotated bitmap
1146 of a glyph of size (width x height) */
1147 unsigned int needed_size = ((height + 7) / 8) * width;
1149 if (needed_size > dstlen) {
1150 print_error("Character %d: Glyph of size %d x %d can't be rotated "
1151 "(buffer size is %lu, needs %u)\n",
1152 char_code, width, height, (unsigned long)dstlen, needed_size);
1153 return -1;
1156 /* calc words of input image */
1157 src_words = BITMAP_WORDS(width) * height;
1159 /* clear background */
1160 memset(dst, 0, needed_size);
1162 dst_mask = 1;
1164 for (i=0; i < src_words; i++) {
1166 /* calc src input bit */
1167 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
1169 /* for each input column... */
1170 for(j=0; j < width; j++) {
1172 if (src_mask == 0) /* input word done? */
1174 src_mask = 1 << (sizeof (bitmap_t) * 8 - 1);
1175 i++; /* next input word */
1178 /* if set in input, set in rotated output */
1179 if (src[i] & src_mask)
1180 dst[j] |= dst_mask;
1182 src_mask >>= 1; /* next input bit */
1185 dst_mask <<= 1; /* next output bit (row) */
1186 if (dst_mask > (1 << 7)) /* output bit > 7? */
1188 dst_mask = 1;
1189 dst += width; /* next output byte row */
1192 return needed_size; /* return result size in bytes */
1195 #endif /* ROTATE */
1198 /* generate C source from in-core font */
1199 int gen_c_source(struct font* pf, char *path)
1201 FILE *ofp;
1202 int i;
1203 time_t t = time(0);
1204 #ifdef ROTATE
1205 int ofr = 0;
1206 #else
1207 int did_syncmsg = 0;
1208 bitmap_t *ofs = pf->bits;
1209 #endif
1210 char buf[256];
1211 char obuf[256];
1212 char hdr1[] = {
1213 "/* Generated by convbdf on %s. */\n"
1214 "#include \"font.h\"\n"
1215 "#ifdef HAVE_LCD_BITMAP\n"
1216 "\n"
1217 "/* Font information:\n"
1218 " name: %s\n"
1219 " facename: %s\n"
1220 " w x h: %dx%d\n"
1221 " size: %d\n"
1222 " ascent: %d\n"
1223 " descent: %d\n"
1224 " depth: %d\n"
1225 " first char: %d (0x%02x)\n"
1226 " last char: %d (0x%02x)\n"
1227 " default char: %d (0x%02x)\n"
1228 " proportional: %s\n"
1229 " %s\n"
1230 "*/\n"
1231 "\n"
1232 "/* Font character bitmap data. */\n"
1233 "static const unsigned char _font_bits[] = {\n"
1236 ofp = fopen(path, "w");
1237 if (!ofp) {
1238 print_error("Can't create %s\n", path);
1239 return 1;
1242 strcpy(buf, ctime(&t));
1243 buf[strlen(buf)-1] = 0;
1245 fprintf(ofp, hdr1, buf,
1246 pf->name,
1247 pf->facename? pf->facename: "",
1248 pf->maxwidth, pf->height,
1249 pf->size,
1250 pf->ascent, pf->descent, pf->depth,
1251 pf->firstchar, pf->firstchar,
1252 pf->firstchar+pf->size-1, pf->firstchar+pf->size-1,
1253 pf->defaultchar, pf->defaultchar,
1254 pf->width? "yes": "no",
1255 pf->copyright? pf->copyright: "");
1257 /* generate bitmaps */
1258 for (i=0; i<pf->size; ++i) {
1259 int x;
1260 int bitcount = 0;
1261 int width = pf->width ? pf->width[i] : pf->maxwidth;
1262 int height = pf->height;
1263 int char_code = pf->firstchar + i;
1264 bitmap_t *bits;
1265 bitmap_t bitvalue=0;
1267 /* Skip missing glyphs */
1268 if (pf->offset && (pf->offset[i] == -1))
1269 continue;
1271 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1273 fprintf(ofp, "\n/* Character %d (0x%02x):\n width %d",
1274 char_code, char_code, width);
1276 if (gen_map) {
1277 fprintf(ofp, "\n +");
1278 for (x=0; x<width; ++x) fprintf(ofp, "-");
1279 fprintf(ofp, "+\n");
1281 x = 0;
1282 while (height > 0) {
1283 if (x == 0) fprintf(ofp, " |");
1285 if (bitcount <= 0) {
1286 bitcount = BITMAP_BITSPERIMAGE;
1287 bitvalue = *bits++;
1290 fprintf(ofp, BITMAP_TESTBIT(bitvalue)? "*": " ");
1292 bitvalue = BITMAP_SHIFTBIT(bitvalue);
1293 --bitcount;
1294 if (++x == width) {
1295 fprintf(ofp, "|\n");
1296 --height;
1297 x = 0;
1298 bitcount = 0;
1301 fprintf(ofp, " +");
1302 for (x=0; x<width; ++x)
1303 fprintf(ofp, "-");
1304 fprintf(ofp, "+ */\n");
1306 else
1307 fprintf(ofp, " */\n");
1309 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1310 #ifdef ROTATE /* pre-rotated into Rockbox bitmap format */
1312 unsigned char bytemap[ROTATION_BUF_SIZE];
1313 int y8, ix=0;
1315 int size = rotleft(bytemap, sizeof(bytemap), bits, width,
1316 pf->height, char_code);
1317 if (size < 0) {
1318 return -1;
1321 for (y8=0; y8<pf->height; y8+=8) /* column rows */
1323 for (x=0; x<width; x++) {
1324 fprintf(ofp, "0x%02x, ", bytemap[ix]);
1325 ix++;
1327 fprintf(ofp, "\n");
1330 /* update offrot since bits are now in sorted order */
1331 pf->offrot[i] = ofr;
1332 ofr += size;
1334 #else
1335 for (x=BITMAP_WORDS(width)*pf->height; x>0; --x) {
1336 fprintf(ofp, "0x%04x,\n", *bits);
1337 if (!did_syncmsg && *bits++ != *ofs++) {
1338 print_warning(VL_MISC, "found encoding values in non-sorted order (not an error).\n");
1339 did_syncmsg = 1;
1342 #endif
1344 fprintf(ofp, "};\n\n");
1346 if (pf->offset) {
1347 /* output offset table */
1348 fprintf(ofp, "/* Character->glyph mapping. */\n"
1349 "static const unsigned short _sysfont_offset[] = {\n");
1351 for (i=0; i<pf->size; ++i) {
1352 int offset = pf->offset[i];
1353 int offrot = pf->offrot[i];
1354 if (offset == -1) {
1355 offset = pf->offset[pf->defaultchar - pf->firstchar];
1356 offrot = pf->offrot[pf->defaultchar - pf->firstchar];
1358 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
1359 #ifdef ROTATE
1360 offrot, i+pf->firstchar);
1361 #else
1362 offset, i+pf->firstchar);
1363 #endif
1365 fprintf(ofp, "};\n\n");
1368 /* output width table for proportional fonts */
1369 if (pf->width) {
1370 fprintf(ofp, "/* Character width data. */\n"
1371 "static const unsigned char _sysfont_width[] = {\n");
1373 for (i=0; i<pf->size; ++i)
1374 fprintf(ofp, " %d,\t/* (0x%02x) */\n",
1375 pf->width[i], i+pf->firstchar);
1376 fprintf(ofp, "};\n\n");
1379 /* output struct font struct */
1380 if (pf->offset)
1381 sprintf(obuf, "_sysfont_offset,");
1382 else
1383 sprintf(obuf, "0, /* no encode table */");
1385 if (pf->width)
1386 sprintf(buf, "_sysfont_width, /* width */");
1387 else
1388 sprintf(buf, "0, /* fixed width */");
1390 fprintf(ofp, "/* Exported structure definition. */\n"
1391 "const struct font sysfont = {\n"
1392 " %d, /* maxwidth */\n"
1393 " %d, /* height */\n"
1394 " %d, /* ascent */\n"
1395 " %d, /* firstchar */\n"
1396 " %d, /* size */\n"
1397 " %d, /* depth */\n"
1398 " _font_bits, /* bits */\n"
1399 " %s /* offset */\n"
1400 " %s\n"
1401 " %d, /* defaultchar */\n"
1402 " %d, /* bits_size */\n",
1403 pf->maxwidth, pf->height,
1404 pf->ascent,
1405 pf->firstchar,
1406 pf->size, 0,
1407 obuf,
1408 buf,
1409 pf->defaultchar,
1410 pf->bits_size
1413 fprintf(ofp, " -1, /* font fd */\n"
1414 " -1, /* font fd width */\n"
1415 " -1, /* font fd offset */\n"
1416 " -1, /* font handle */\n"
1417 " 0, /* buffer start */\n"
1418 " 0, /* ^ position */\n"
1419 " 0, /* ^ end */\n"
1420 " 0, /* ^ size */\n"
1421 " {{0,0,0,0,0},0,0,0,0,0}, /* cache */\n"
1422 " 0, /* */\n"
1423 " 0, /* */\n"
1424 " 0, /* */\n"
1425 "};\n"
1426 "#endif /* HAVE_LCD_BITMAP */\n"
1429 return 0;
1432 /* generate C header from in-core font */
1433 int gen_h_header(struct font* pf, char *path)
1435 FILE *ofp;
1436 time_t t = time(0);
1437 char buf[256];
1438 char *hdr1 =
1439 "/* Generated by convbdf on %s. */\n"
1440 "#ifdef HAVE_LCD_BITMAP\n"
1441 "\n"
1442 "/* Font information */\n"
1443 "#define SYSFONT_NAME %s\n"
1444 "#define SYSFONT_FACENAME %s\n"
1445 "#define SYSFONT_WIDTH %d\n"
1446 "#define SYSFONT_HEIGHT %d\n"
1447 "#define SYSFONT_SIZE %d\n"
1448 "#define SYSFONT_ASCENT %d\n"
1449 "#define SYSFONT_DEPTH %d\n";
1450 char *hdr2 =
1451 "#define SYSFONT_DESCENT %d\n"
1452 "#define SYSFONT_FIRST_CHAR %d\n"
1453 "#define SYSFONT_LAST_CHAR %d\n"
1454 "#define SYSFONT_DEFAULT_CHAR %d\n"
1455 "#define SYSFONT_PROPORTIONAL %d\n"
1456 "#define SYSFONT_COPYRIGHT %s\n"
1457 "#define SYSFONT_BITS_SIZE %d\n"
1458 "\n"
1459 "#endif\n";
1461 ofp = fopen(path, "w");
1462 if (!ofp) {
1463 print_error("Can't create %s\n", path);
1464 return 1;
1467 strcpy(buf, ctime(&t));
1468 buf[strlen(buf)-1] = 0;
1470 fprintf(ofp, hdr1, buf,
1471 pf->name,
1472 pf->facename? pf->facename: "",
1473 pf->maxwidth,
1474 pf->height,
1475 pf->size,
1476 pf->ascent,
1477 pf->depth);
1479 fprintf(ofp, hdr2,
1480 pf->descent,
1481 pf->firstchar,
1482 pf->firstchar+pf->size-1,
1483 pf->defaultchar,
1484 pf->width? 1: 0,
1485 pf->copyright? pf->copyright: "",
1486 pf->bits_size);
1488 return 0;
1491 static int writebyte(FILE *fp, unsigned char c)
1493 return putc(c, fp) != EOF;
1496 static int writeshort(FILE *fp, unsigned short s)
1498 putc(s, fp);
1499 return putc(s>>8, fp) != EOF;
1502 static int writeint(FILE *fp, unsigned int l)
1504 putc(l, fp);
1505 putc(l>>8, fp);
1506 putc(l>>16, fp);
1507 return putc(l>>24, fp) != EOF;
1510 static int writestr(FILE *fp, char *str, int count)
1512 return (int)fwrite(str, 1, count, fp) == count;
1515 #ifndef ROTATE
1516 static int writestrpad(FILE *fp, char *str, int totlen)
1518 int ret = EOF;
1520 while (str && *str && totlen > 0) {
1521 if (*str) {
1522 ret = putc(*str++, fp);
1523 --totlen;
1526 while (--totlen >= 0)
1527 ret = putc(' ', fp);
1528 return ret;
1530 #endif
1532 /* generate .fnt format file from in-core font */
1533 int gen_fnt_file(struct font* pf, char *path)
1535 FILE *ofp;
1536 int i;
1537 #ifdef ROTATE
1538 int ofr = 0;
1539 #endif
1541 ofp = fopen(path, "wb");
1542 if (!ofp) {
1543 print_error("Can't create %s\n", path);
1544 return 1;
1547 /* write magic and version number */
1548 writestr(ofp, VERSION, 4);
1549 #ifndef ROTATE
1550 /* internal font name */
1551 writestrpad(ofp, pf->name, 64);
1553 /* copyright */
1554 writestrpad(ofp, pf->copyright, 256);
1555 #endif
1556 /* font info */
1557 writeshort(ofp, pf->maxwidth);
1558 writeshort(ofp, pf->height);
1559 writeshort(ofp, pf->ascent);
1560 writeshort(ofp, 0); /* depth = 0 for bdffonts */
1561 writeint(ofp, pf->firstchar);
1562 writeint(ofp, pf->defaultchar);
1563 writeint(ofp, pf->size);
1565 /* variable font data sizes */
1566 writeint(ofp, pf->bits_size); /* # words of bitmap_t */
1567 writeint(ofp, pf->offset? pf->size: 0); /* # ints of offset */
1568 writeint(ofp, pf->width? pf->size: 0); /* # bytes of width */
1569 /* variable font data */
1570 #ifdef ROTATE
1571 for (i=0; i<pf->size; ++i)
1573 bitmap_t* bits;
1574 int width = pf->width ? pf->width[i] : pf->maxwidth;
1575 int size;
1576 int char_code = pf->firstchar + i;
1577 unsigned char bytemap[ROTATION_BUF_SIZE];
1579 /* Skip missing glyphs */
1580 if (pf->offset && (pf->offset[i] == -1))
1581 continue;
1583 bits = pf->bits + (pf->offset? (int)pf->offset[i]: (pf->height * i));
1585 size = rotleft(bytemap, sizeof(bytemap), bits, width, pf->height, char_code);
1586 if (size < 0) {
1587 return -1;
1589 writestr(ofp, (char *)bytemap, size);
1591 /* update offrot since bits are now in sorted order */
1592 pf->offrot[i] = ofr;
1593 ofr += size;
1596 if ( pf->bits_size < 0xFFDB )
1598 /* bitmap offset is small enough, use unsigned short for offset */
1599 if (ftell(ofp) & 1)
1600 writebyte(ofp, 0); /* pad to 16-bit boundary */
1602 else
1604 /* bitmap offset is large then 64K, use unsigned int for offset */
1605 while (ftell(ofp) & 3)
1606 writebyte(ofp, 0); /* pad to 32-bit boundary */
1609 if (pf->offset)
1611 for (i=0; i<pf->size; ++i)
1613 int offrot = pf->offrot[i];
1614 if (pf->offset[i] == -1) {
1615 offrot = pf->offrot[pf->defaultchar - pf->firstchar];
1617 if ( pf->bits_size < 0xFFDB )
1618 writeshort(ofp, offrot);
1619 else
1620 writeint(ofp, offrot);
1624 if (pf->width)
1625 for (i=0; i<pf->size; ++i)
1626 writebyte(ofp, pf->width[i]);
1627 #else
1628 for (i=0; i<pf->bits_size; ++i)
1629 writeshort(ofp, pf->bits[i]);
1630 if (ftell(ofp) & 2)
1631 writeshort(ofp, 0); /* pad to 32-bit boundary */
1633 if (pf->offset)
1634 for (i=0; i<pf->size; ++i) {
1635 int offset = pf->offset[i];
1636 if (offset == -1) {
1637 offset = pf->offset[pf->defaultchar - pf->firstchar];
1639 writeint(ofp, offset);
1642 if (pf->width)
1643 for (i=0; i<pf->size; ++i)
1644 writebyte(ofp, pf->width[i]);
1645 #endif
1646 fclose(ofp);
1647 return 0;