gfx_blitImage(): New function; gfx_blitRaster(): Fix clipping bug.
[bertos.git] / gfx / text.c
index 0fa994cd55189e41abbd9a8ba44b9ad9a7d6392b..08d3ae6167283343b7d0b4d4b31a7fc60fce1696 100755 (executable)
 
 /*#*
  *#* $Log$
+ *#* Revision 1.8  2006/03/22 09:50:37  bernie
+ *#* Use the same format for fonts and rasters.
+ *#*
+ *#* Revision 1.7  2006/03/20 17:51:55  bernie
+ *#* Cleanups.
+ *#*
+ *#* 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/15 09:10:15  bernie
  *#* Implement prop fonts; Fix algo styles.
  *#*
 #include <cfg/debug.h>
 
 
-/*!
- * Flags degli stili algoritmici
- *
- * La routine di rendering del testo e' in grado di applicare
- * delle semplici trasformazioni al font interno per generare
- * automaticamente degli stili predefiniti (bold, italic,
- * underline) a partire dal set di caratteri plain.
- */
-static uint8_t text_styles;
-
 /*! ANSI escape sequences flag: true for ESC state on */
 static bool ansi_mode = false;
 
@@ -142,10 +144,10 @@ static int text_putglyph(char c, struct Bitmap *bm)
        unsigned char index = (unsigned char)c;
 
        /* Check for out of range char and replace with '?' or first char in font. */
-       if (index < bm->font->first || index > bm->font->last)
+       if (UNLIKELY(!FONT_HAS_GLYPH(bm->font, index)))
        {
                kprintf("Illegal char '%c' (0x%02x)\n", index, index);
-               if ('?' >= bm->font->first && '?' <= bm->font->last)
+               if (FONT_HAS_GLYPH(bm->font, '?'))
                        index = '?';
                else
                        index = bm->font->first;
@@ -156,7 +158,7 @@ static int text_putglyph(char c, struct Bitmap *bm)
 
        glyph_height = bm->font->height;
        // FIXME: for vertical fonts only
-       glyph_height_bytes = (glyph_height + 7) / 8;
+       glyph_height_bytes = ROUND_UP2(glyph_height, 8);
 
        if (bm->font->offset)
        {
@@ -178,19 +180,21 @@ static int text_putglyph(char c, struct Bitmap *bm)
                glyph = bm->font->glyph + index * glyph_height_bytes * glyph_width;
        }
 
-       if (text_styles & STYLEF_CONDENSED)
-               --glyph_width;
-
-       if (text_styles & STYLEF_EXPANDED)
-               glyph_width *= 2;
-
        /* Slow path for styled glyphs */
-       if (text_styles)
+       if (UNLIKELY(bm->styles))
        {
+               uint8_t styles = bm->styles;
                uint8_t prev_dots = 0, italic_prev_dots = 0;
                uint8_t dots;
                uint8_t row, col;
 
+               /* This style alone could be handled by the fast path too */
+               if (bm->styles & STYLEF_CONDENSED)
+                       --glyph_width;
+
+               if (bm->styles & STYLEF_EXPANDED)
+                       glyph_width *= 2;
+
                /* Check if glyph fits in the bitmap. */
                if ((bm->penX < 0) || (bm->penX + glyph_width > bm->width)
                        || (bm->penY < 0) || (bm->penY + glyph_height > bm->height))
@@ -208,14 +212,14 @@ static int text_putglyph(char c, struct Bitmap *bm)
                                uint8_t i;
 
                                /* Expanded style: advances only once every two columns. */
-                               if (text_styles & STYLEF_EXPANDED)
+                               if (styles & STYLEF_EXPANDED)
                                        src_col /= 2;
 
                                /* Fetch a column of dots from glyph. */
-                               dots = PGM_READ_CHAR(glyph + src_col * glyph_height_bytes + row);
+                               dots = PGM_READ_CHAR(RAST_ADDR(glyph, src_col, row * 8, glyph_width));
 
                                /* Italic: get lower 4 dots from previous column */
-                               if (text_styles & STYLEF_ITALIC)
+                               if (styles & STYLEF_ITALIC)
                                {
                                        uint8_t new_dots = dots;
                                        dots = (dots & 0xF0) | italic_prev_dots;
@@ -223,7 +227,7 @@ static int text_putglyph(char c, struct Bitmap *bm)
                                }
 
                                /* Bold: "or" pixels with the previous column */
-                               if (text_styles & STYLEF_BOLD)
+                               if (styles & STYLEF_BOLD)
                                {
                                        uint8_t new_dots = dots;
                                        dots |= prev_dots;
@@ -231,11 +235,12 @@ static int text_putglyph(char c, struct Bitmap *bm)
                                }
 
                                /* Underlined: turn on base pixel */
-                               if (text_styles & STYLEF_UNDERLINE)
-                                       dots |= 0x80;
+                               if ((styles & STYLEF_UNDERLINE)
+                                       && (row == glyph_height_bytes - 1))
+                                       dots |= (1 << (glyph_height - row * 8 - 1));
 
                                /* Inverted: invert pixels */
-                               if (text_styles & STYLEF_INVERT)
+                               if (styles & STYLEF_INVERT)
                                        dots = ~dots;
 
                                /* Output dots */
@@ -247,7 +252,7 @@ static int text_putglyph(char c, struct Bitmap *bm)
        else
        {
                /* No style: fast vanilla copy of glyph to bitmap */
-               gfx_blitRaster(bm, bm->penX, bm->penY, glyph, glyph_width, glyph_height, glyph_height_bytes);
+               gfx_blitRaster(bm, bm->penX, bm->penY, glyph, glyph_width, glyph_height, glyph_width);
        }
 
        /* Update current pen position */
@@ -264,7 +269,7 @@ static int text_putglyph(char c, struct Bitmap *bm)
 int text_putchar(char c, struct Bitmap *bm)
 {
        /* Handle ANSI escape sequences */
-       if (ansi_mode)
+       if (UNLIKELY(ansi_mode))
        {
                switch (c)
                {
@@ -272,7 +277,7 @@ int text_putchar(char c, struct Bitmap *bm)
                        gfx_bitmapClear(bm);
                        bm->penX = 0;
                        bm->penY = 0;
-                       text_style(0, STYLEF_MASK);
+                       text_style(bm, 0, STYLEF_MASK);
                        break;
                DB(default:
                        kprintf("Unknown ANSI esc code: %x\n", c);)
@@ -315,29 +320,30 @@ void text_clearLine(struct Bitmap *bm, int line)
 }
 
 
-/*!
+/**
  * Set/clear algorithmic font style bits.
  *
+ * \param bm     Pointer to Bitmap to affect.
  * \param flags  Style flags to set
  * \param mask   Mask of flags to modify
  * \return       Old style flags
  *
  * Examples:
  * Turn on bold, leave other styles alone
- *   \code prt_style(STYLEF_BOLD, STYLEF_BOLD); \endcode
+ *   \code text_style(bm, STYLEF_BOLD, STYLEF_BOLD); \endcode
  *
  * Turn off bold and turn on italic, leave others as they are
- *   \code prt_style(STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
+ *   \code text_style(bm, STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
  *
  * Query current style without chaning it
- *   \code style = prt_style(0, 0); \endcode
+ *   \code style = text_style(bm, 0, 0); \endcode
  *
  * Reset all styles (plain text)
- *   \code prt_style(0, STYLE_MASK); \endcode
+ *   \code text_style(bm, 0, STYLE_MASK); \endcode
  */
-uint8_t text_style(uint8_t flags, uint8_t mask)
+uint8_t text_style(struct Bitmap *bm, uint8_t flags, uint8_t mask)
 {
-       uint8_t old = text_styles;
-       text_styles = (text_styles & ~mask) | flags;
+       uint8_t old = bm->styles;
+       bm->styles = (bm->styles & ~mask) | flags;
        return old;
 }