Index: tools/convbdf.c =================================================================== --- tools/convbdf.c (Revision 20068) +++ tools/convbdf.c (Arbeitskopie) @@ -40,7 +40,8 @@ int height; /* height in pixels*/ int ascent; /* ascent (baseline) height*/ int firstchar; /* first character in bitmap*/ - int size; /* font size in glyphs*/ + int nchars; /* number of characters */ + int size; /* font size in glyphs ('holes' included) */ bitmap_t* bits; /* 16-bit right-padded bitmap data*/ unsigned int* offset; /* offsets into bitmap data*/ unsigned char* width; /* character widths or NULL if fixed*/ @@ -82,6 +83,7 @@ int bdf_read_bitmaps(FILE *fp, struct font* pf); char * bdf_getline(FILE *fp, char *buf, int len); bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2); +void bitmap_buf_alloc(struct font* pf); int gen_c_source(struct font* pf, char *path); int gen_h_header(struct font* pf, char *path); @@ -302,6 +304,7 @@ pf = (struct font*)calloc(1, sizeof(struct font)); if (!pf) goto errout; + memset(pf, 0, sizeof(struct font)); pf->name = strdup(basename(path)); @@ -328,7 +331,6 @@ int bdf_read_header(FILE *fp, struct font* pf) { int encoding; - int nchars, maxwidth; int firstchar = 65535; int lastchar = -1; char buf[256]; @@ -390,7 +392,7 @@ continue; } if (isprefix(buf, "CHARS ")) { - if (sscanf(buf, "CHARS %d", &nchars) != 1) { + if (sscanf(buf, "CHARS %d", &pf->nchars) != 1) { fprintf(stderr, "Error: bad 'CHARS'\n"); return 0; } @@ -439,16 +441,17 @@ /* calc font size (offset/width entries)*/ pf->firstchar = firstchar; pf->size = lastchar - firstchar + 1; + if (pf->size < pf->nchars) { + fprintf(stderr, "Error: NCHARS and max code mismatch\n"); + return 0; + } /* use the font boundingbox to get initial maxwidth*/ /*maxwidth = pf->fbbw - pf->fbbx;*/ - maxwidth = pf->fbbw; + pf->maxwidth = pf->fbbw; + bitmap_buf_alloc(pf); /* Allocate bitmaps */ - /* initially use font maxwidth * height for bits allocation*/ - pf->bits_size = nchars * BITMAP_WORDS(maxwidth) * pf->height; - /* allocate bits, offset, and width arrays*/ - pf->bits = (bitmap_t *)malloc(pf->bits_size * sizeof(bitmap_t) + EXTRA); pf->offset = (unsigned int *)malloc(pf->size * sizeof(unsigned int)); pf->offrot = (unsigned int *)malloc(pf->size * sizeof(unsigned int)); pf->width = (unsigned char *)malloc(pf->size * sizeof(unsigned char)); @@ -466,13 +469,14 @@ { int ofs = 0; int ofr = 0; - int maxwidth = 0; int i, k, encoding, width; int bbw, bbh, bbx, bby; int proportional = 0; int encodetable = 0; int l; char buf[256]; + bitmap_t *ch_bitmap; + int ch_words; /* reset file pointer*/ fseek(fp, 0L, SEEK_SET); @@ -517,8 +521,6 @@ continue; } if (strequal(buf, "BITMAP") || strequal(buf, "BITMAP ")) { - bitmap_t *ch_bitmap = pf->bits + ofs; - int ch_words; if (encoding < 0) continue; @@ -539,14 +541,18 @@ width = maxwidth;*/ bbx = 0; } - if (width > maxwidth) - maxwidth = width; + if (width > pf->maxwidth) { + pf->maxwidth = width; + bitmap_buf_alloc(pf); /* Re-allocate bitmaps since the maxwidth has grown */ + fprintf(stderr, "Warning: width of the char %d is greater than the font width\n", + encoding); + } pf->width[encoding-pf->firstchar] = width; - /* clear bitmap*/ - memset(ch_bitmap, 0, BITMAP_BYTES(width) * pf->height); + ch_bitmap = pf->bits + ofs; + memset(ch_bitmap, 0, BITMAP_BYTES(width) * pf->height); /* clear bitmap*/ + ch_words = BITMAP_WORDS(width); - ch_words = BITMAP_WORDS(width); #define BM(row,col) (*(ch_bitmap + ((row)*ch_words) + (col))) #define BITMAP_NIBBLES (BITMAP_BITSPERIMAGE/4) @@ -595,9 +601,6 @@ break; } - /* set max width*/ - pf->maxwidth = maxwidth; - /* change unused width values to default char values*/ for (i=0; isize; ++i) { int defchar = pf->defaultchar - pf->firstchar; @@ -633,7 +636,7 @@ /* determine whether font is fixed-width*/ for (i=0; isize; ++i) { - if (pf->width[i] != maxwidth) { + if (pf->width[i] != pf->maxwidth) { proportional = 1; break; } @@ -692,6 +695,19 @@ return buf; } +/* + Calculates the necessary size of the bit buffer to hold all the + bitmaps for the glyphs in the font. Shoud be called every time + the max width of the font grows. Font height, the max width and + the number of chars in the font must have been already set. +*/ +void bitmap_buf_alloc(struct font* pf) +{ + pf->bits_size = pf->nchars * BITMAP_WORDS(pf->maxwidth) * pf->height; + pf->bits = (bitmap_t *)realloc(pf->bits, pf->bits_size * sizeof(bitmap_t)); +} + + /* return hex value of portion of buffer*/ bitmap_t bdf_hexval(unsigned char *buf, int ndx1, int ndx2) {