diff --git a/firmware/export/font.h b/firmware/export/font.h index d17fa18..d2d5616 100644 --- a/firmware/export/font.h +++ b/firmware/export/font.h @@ -79,7 +79,8 @@ enum { * USHORT maxwidth 2 font max width in pixels * USHORT height 2 font height in pixels * USHORT ascent 2 font ascent (baseline) in pixels - * USHORT pad 2 unused, pad to 32-bit boundary + * UCHAR ascent delta 1 max font ascent in pixels - ascent + * UCHAR descent delta 1 max font descent in pixels - descent * ULONG firstchar 4 first character code in font * ULONG defaultchar 4 default character code in font * ULONG size 4 # characters in font @@ -99,8 +100,11 @@ enum { /* based on The Microwindows Project http://microwindows.org */ struct font { int maxwidth; /* max width in pixels*/ - unsigned int height; /* height in pixels*/ + unsigned int height; /* height in pixels (= maxascent + maxdescent)*/ int ascent; /* ascent (baseline) height*/ + int descent; /* descent height*/ + int maxascent; /* max ascent height*/ + int maxdescent; /* max descent height*/ int firstchar; /* first character in bitmap*/ int size; /* font size in glyphs*/ const unsigned char *bits; /* 8-bit column bitmap data*/ diff --git a/firmware/font.c b/firmware/font.c old mode 100644 new mode 100755 index e02f276..a07bca5 --- a/firmware/font.c +++ b/firmware/font.c @@ -123,6 +123,7 @@ void font_reset(void) static struct font* font_load_header(struct font *pf) { char version[4+1]; + unsigned short delta; /* Check we have enough data */ if (!HAVEBYTES(28)) @@ -139,7 +140,10 @@ static struct font* font_load_header(struct font *pf) pf->maxwidth = readshort(); pf->height = readshort(); pf->ascent = readshort(); - fileptr += 2; /* Skip padding */ + delta = readshort(); + pf->maxascent = pf->ascent + (delta & 0xff); + pf->maxdescent = pf->height - pf->maxascent; + pf->descent = pf->maxdescent - (delta >> 8); pf->firstchar = readlong(); pf->defaultchar = readlong(); pf->size = readlong(); diff --git a/tools/convbdf.c b/tools/convbdf.c old mode 100644 new mode 100755 index aae5b43..86f308e --- a/tools/convbdf.c +++ b/tools/convbdf.c @@ -39,6 +39,9 @@ struct font { int maxwidth; /* max width in pixels*/ int height; /* height in pixels*/ int ascent; /* ascent (baseline) height*/ + int descent; /* descent height*/ + int maxascent; /* max ascent (baseline) height*/ + int maxdescent; /* max descent height*/ int firstchar; /* first character in bitmap*/ int size; /* font size in glyphs*/ bitmap_t* bits; /* 16-bit right-padded bitmap data*/ @@ -53,7 +56,6 @@ struct font { char * facename; /* facename of font*/ char * copyright; /* copyright info for loadable fonts*/ int pixel_size; - int descent; int fbbw, fbbh, fbbx, fbby; }; /* END font.h*/ @@ -334,11 +336,17 @@ int bdf_read_header(FILE *fp, struct font* pf) char buf[256]; char facename[256]; char copyright[256]; + int bbw; + int bbh; + int bbx; + int bby; /* set certain values to errors for later error checking*/ pf->defaultchar = -1; pf->ascent = -1; pf->descent = -1; + pf->maxascent = -1; + pf->maxdescent = -1; for (;;) { if (!bdf_getline(fp, buf, sizeof(buf))) { @@ -372,6 +380,7 @@ int bdf_read_header(FILE *fp, struct font* pf) fprintf(stderr, "Error: bad 'FONT_DESCENT'\n"); return 0; } + pf->maxdescent = pf->descent; continue; } if (isprefix(buf, "FONT_ASCENT ")) { @@ -379,6 +388,7 @@ int bdf_read_header(FILE *fp, struct font* pf) fprintf(stderr, "Error: bad 'FONT_ASCENT'\n"); return 0; } + pf->maxascent = pf->ascent; continue; } if (isprefix(buf, "FONTBOUNDINGBOX ")) { @@ -418,6 +428,24 @@ int bdf_read_header(FILE *fp, struct font* pf) } continue; } + + /* + * BBX is not BDF header. However, in order to obtain pf->maxascent and + * pf->descent, BBX is checked here. + */ + if (isprefix(buf, "BBX ")) { + if (sscanf(buf, "BBX %d %d %d %d", &bbw, &bbh, &bbx, &bby) != 4) { + fprintf(stderr, "Error: bad 'BBX'\n"); + return 0; + } + + if (bbh + bby > pf->maxascent) + pf->maxascent = bbh + bby; + if (-bby > pf->maxdescent) + pf->maxdescent = -bby; + continue; + } + if (strequal(buf, "ENDFONT")) break; } @@ -427,7 +455,42 @@ int bdf_read_header(FILE *fp, struct font* pf) fprintf(stderr, "Error: Invalid BDF file, requires FONT_ASCENT/FONT_DESCENT/ENCODING\n"); return 0; } - pf->height = pf->ascent + pf->descent; + + if (pf->maxascent > pf->ascent) + { + if (pf->maxascent - pf->ascent > pf->ascent/2) + { + fprintf(stderr, "Error: %s the font's ascent is too larger than FONT_ASCENT.\n", + pf->name); + return 0; + } + fprintf(stderr, "Warning: %s %d pixel(s) beyond the font's ascent\n", + pf->name, pf->maxascent - pf->ascent); + } + if (pf->maxdescent > pf->descent) + { + if (pf->maxdescent > 3 * pf->descent) + { + fprintf(stderr, "Error: %s the font's descent is too larger than FONT_DESCENT.\n", + pf->name); + return 0; + } + fprintf(stderr, "Warning: %s %d pixel(s) beyond the font's descent\n", + pf->name, pf->maxdescent - pf->descent); + } + + if (pf->maxascent > pf->fbbh + pf->fbby) + { + fprintf(stderr, "Warning: %s %d pixel(s) beyond the FONTBOUNDINGBOX's ascent\n", + pf->name, pf->maxascent - pf->fbbh - pf->fbby); + } + if (pf->maxdescent > -pf->fbby) + { + fprintf(stderr, "Warning: %s %d pixel(s) beyond the FONTBOUNDINGBOX's descent\n", + pf->name, pf->maxdescent + pf->fbby); + } + + pf->height = pf->maxascent + pf->maxdescent; /* calc default char*/ if (pf->defaultchar < 0 || @@ -473,6 +536,7 @@ int bdf_read_bitmaps(FILE *fp, struct font* pf) int encodetable = 0; int l; char buf[256]; + int r; /* reset file pointer*/ fseek(fp, 0L, SEEK_SET); @@ -561,6 +625,10 @@ int bdf_read_bitmaps(FILE *fp, struct font* pf) if (isprefix(buf, "ENDCHAR")) break; + r = pf->maxascent - bby - bbh + i; + if (r < 0 || r >= pf->height) + continue; + hexnibbles = strlen(buf); for (k=0; kheight - pf->descent - bby - bbh + i, k) |= - value >> bbx; + BM(r, k) |= value >> bbx; /* handle overflow into next image word*/ if (bbx) { - BM(pf->height - pf->descent - bby - bbh + i, k+1) = - value << (BITMAP_BITSPERIMAGE - bbx); + BM(r, k+1) = value << (BITMAP_BITSPERIMAGE - bbx); } } } @@ -804,6 +870,8 @@ int gen_c_source(struct font* pf, char *path) " size: %d\n" " ascent: %d\n" " descent: %d\n" + " maxascent: %d\n" + " maxdescent: %d\n" " first char: %d (0x%02x)\n" " last char: %d (0x%02x)\n" " default char: %d (0x%02x)\n" @@ -830,6 +898,7 @@ int gen_c_source(struct font* pf, char *path) pf->maxwidth, pf->height, pf->size, pf->ascent, pf->descent, + pf->maxascent, pf->maxdescent, pf->firstchar, pf->firstchar, pf->firstchar+pf->size-1, pf->firstchar+pf->size-1, pf->defaultchar, pf->defaultchar, @@ -968,6 +1037,9 @@ int gen_c_source(struct font* pf, char *path) " %d, /* maxwidth */\n" " %d, /* height */\n" " %d, /* ascent */\n" + " %d, /* descent */\n" + " %d, /* maxascent */\n" + " %d, /* maxdescent */\n" " %d, /* firstchar */\n" " %d, /* size */\n" " _font_bits, /* bits */\n" @@ -979,6 +1051,9 @@ int gen_c_source(struct font* pf, char *path) "#endif /* HAVE_LCD_BITMAP */\n", pf->maxwidth, pf->height, pf->ascent, + pf->descent, + pf->maxascent, + pf->maxdescent, pf->firstchar, pf->size, obuf, @@ -1115,7 +1190,8 @@ int gen_fnt_file(struct font* pf, char *path) writeshort(ofp, pf->maxwidth); writeshort(ofp, pf->height); writeshort(ofp, pf->ascent); - writeshort(ofp, 0); + writebyte(ofp, pf->maxascent - pf->ascent); + writebyte(ofp, pf->maxdescent - pf->descent); writeint(ofp, pf->firstchar); writeint(ofp, pf->defaultchar); writeint(ofp, pf->size);