X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=gfx%2Ftext_format.c;h=1b0e8a340571b976f160a7466807a7bb2ea1d500;hb=42b50d5905e886d792cd1a338c446ac43f61d6cf;hp=c56eea6b4e9f1cb7f155afdf6deb62f64333c89c;hpb=68d5a6796ff0efc9666ae359be2150d9638182f6;p=bertos.git diff --git a/gfx/text_format.c b/gfx/text_format.c index c56eea6b..1b0e8a34 100755 --- a/gfx/text_format.c +++ b/gfx/text_format.c @@ -15,6 +15,18 @@ /*#* *#* $Log$ + *#* Revision 1.6 2006/03/13 02:05:54 bernie + *#* Mark slow paths as UNLIKELY. + *#* + *#* Revision 1.5 2006/03/07 22:18:04 bernie + *#* Correctly compute text width for prop fonts; Make styles a per-bitmap attribute. + *#* + *#* Revision 1.4 2006/02/10 12:31:33 bernie + *#* Add multiple font support in bitmaps. + *#* + *#* Revision 1.3 2005/11/27 23:31:58 bernie + *#* Reorder includes. + *#* *#* Revision 1.2 2005/11/04 18:17:45 bernie *#* Fix header guards and includes for new location of gfx module. *#* @@ -59,10 +71,11 @@ *#* *#*/ -#include +#include "text.h" + +#include /* _formatted_write() */ #include #include -#include /* _formatted_write() */ #include /* vsprintf() */ #include @@ -153,7 +166,7 @@ int PGM_FUNC(text_xprintf)(struct Bitmap *bm, text_moveto(bm, row, col); if (style & STYLEF_MASK) - oldstyle = text_style(style, STYLEF_MASK); + oldstyle = text_style(bm, style, STYLEF_MASK); if (style & (TEXT_CENTER | TEXT_RIGHT)) { @@ -163,27 +176,81 @@ int PGM_FUNC(text_xprintf)(struct Bitmap *bm, pad /= 2; if (style & TEXT_FILL) - gfx_rectFillC(bm, 0, row * FONT_HEIGHT, pad, (row + 1) * FONT_HEIGHT, + gfx_rectFillC(bm, 0, row * bm->font->height, pad, (row + 1) * bm->font->height, (style & STYLEF_INVERT) ? 0xFF : 0x00); - text_setcoord(bm, pad, row * FONT_HEIGHT); + text_setcoord(bm, pad, row * bm->font->height); } len = PGM_FUNC(text_vprintf)(bm, fmt, ap); va_end(ap); if (style & TEXT_FILL) - gfx_rectFillC(bm, bm->penX, row * FONT_HEIGHT, bm->width, (row + 1) * FONT_HEIGHT, + gfx_rectFillC(bm, bm->penX, row * bm->font->height, bm->width, (row + 1) * bm->font->height, (style & STYLEF_INVERT) ? 0xFF : 0x00); /* Restore old style */ if (style & STYLEF_MASK) - text_style(oldstyle, STYLEF_MASK); + text_style(bm, oldstyle, STYLEF_MASK); return len; } +struct TextWidthData +{ + Bitmap *bitmap; + coord_t width; +}; + +/** + * Compute width in pixels of a character. + * + * Compute the on screen width of a character, taking the + * current style and font into account. + * + * The width is accumulated in the WidthData structure + * passed as second argument. + * + * This is a formatted_write() callback used by text_vwidthf() + * to compute the length of a formatted string. + */ +static int text_charWidth(int c, struct TextWidthData *twd) +{ + unsigned char index = (unsigned char)c; + Bitmap *bm = twd->bitmap; + coord_t glyph_width; + + + if (UNLIKELY(index < bm->font->first || index > bm->font->last)) + { + if ('?' >= bm->font->first && '?' <= bm->font->last) + index = '?'; + else + index = bm->font->first; + } + + /* Make character relative to font start */ + index -= bm->font->first; + + if (bm->font->offset) + /* Proportional font */ + glyph_width = bm->font->widths[index]; /* TODO: optimize away */ + else + /* Fixed width font */ + glyph_width = bm->font->width; + + if (bm->styles & STYLEF_CONDENSED) + --glyph_width; + + if (bm->styles & STYLEF_EXPANDED) + glyph_width *= 2; + + twd->width += glyph_width; + + return c; +} + /*! * Return the width in pixels of a vprintf()-formatted string. */ @@ -192,7 +259,15 @@ int PGM_FUNC(text_vwidthf)( const char * PGM_ATTR fmt, va_list ap) { - return PGM_FUNC(vsprintf)(NULL, fmt, ap) * FONT_WIDTH; + /* Fixed font with no styles affecting the width? */ + if (!bm->font->offset && !(bm->styles & (STYLEF_CONDENSED | STYLEF_EXPANDED))) + return PGM_FUNC(vsprintf)(NULL, fmt, ap) * bm->font->width; + else + { + struct TextWidthData twd = { bm, 0 }; + _formatted_write(fmt, (void (*)(char, void *))text_charWidth, &twd, ap); + return twd.width; + } }