Mark slow paths as UNLIKELY.
[bertos.git] / gfx / text.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \brief Text graphic routines
10  *
11  * \version $Id$
12  * \author Bernardo Innocenti <bernie@develer.com>
13  * \author Stefano Fedrigo <aleph@develer.com>
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.6  2006/03/13 02:05:54  bernie
19  *#* Mark slow paths as UNLIKELY.
20  *#*
21  *#* Revision 1.5  2006/03/07 22:18:04  bernie
22  *#* Correctly compute text width for prop fonts; Make styles a per-bitmap attribute.
23  *#*
24  *#* Revision 1.4  2006/02/15 09:10:15  bernie
25  *#* Implement prop fonts; Fix algo styles.
26  *#*
27  *#* Revision 1.3  2006/02/10 12:31:55  bernie
28  *#* Add multiple font support in bitmaps.
29  *#*
30  *#* Revision 1.2  2005/11/04 18:17:45  bernie
31  *#* Fix header guards and includes for new location of gfx module.
32  *#*
33  *#* Revision 1.1  2005/11/04 18:11:35  bernie
34  *#* Move graphics stuff from mware/ to gfx/.
35  *#*
36  *#* Revision 1.13  2005/11/04 16:20:02  bernie
37  *#* Fix reference to README.devlib in header.
38  *#*
39  *#* Revision 1.12  2005/04/11 19:10:28  bernie
40  *#* Include top-level headers from cfg/ subdir.
41  *#*
42  *#* Revision 1.11  2005/01/20 18:46:31  aleph
43  *#* Fix progmem includes.
44  *#*
45  *#* Revision 1.10  2005/01/08 09:20:12  bernie
46  *#* Really make it work on both architectures.
47  *#*
48  *#* Revision 1.9  2004/12/31 16:44:29  bernie
49  *#* Sanitize for non-Harvard processors.
50  *#*
51  *#* Revision 1.8  2004/11/16 21:16:28  bernie
52  *#* Update to new naming scheme in mware/gfx.c.
53  *#*
54  *#* Revision 1.7  2004/09/20 03:28:28  bernie
55  *#* Fix header.
56  *#*
57  *#* Revision 1.6  2004/09/14 20:57:15  bernie
58  *#* Use debug.h instead of kdebug.h.
59  *#*
60  *#* Revision 1.5  2004/09/06 21:51:26  bernie
61  *#* Extend interface to allow any algorithmic style.
62  *#*
63  *#* Revision 1.2  2004/06/03 11:27:09  bernie
64  *#* Add dual-license information.
65  *#*
66  *#* Revision 1.1  2004/05/23 15:43:16  bernie
67  *#* Import mware modules.
68  *#*
69  *#* Revision 1.17  2004/05/15 16:57:01  aleph
70  *#* Fixes for non-DEBUG build
71  *#*
72  *#* Revision 1.16  2004/04/03 20:42:49  aleph
73  *#* Add text_clear()
74  *#*
75  *#* Revision 1.15  2004/03/24 15:03:45  bernie
76  *#* Use explicit include paths; clean Doxygen comments
77  *#*
78  *#* Revision 1.14  2004/03/19 16:52:28  bernie
79  *#* Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
80  *#*
81  *#* Revision 1.13  2004/03/17 18:23:32  bernie
82  *#* Oops.
83  *#*
84  *#* Revision 1.12  2004/03/17 18:03:22  bernie
85  *#* Make diagnostic message shorter
86  *#*
87  *#* Revision 1.11  2004/03/13 22:52:54  aleph
88  *#* documentation fixes
89  *#*/
90
91 #include <gfx/gfx.h>
92 #include <gfx/font.h>
93 #include <gfx/text.h>
94 #include <gfx/text.h>
95
96 #include <gfx/gfx_p.h> // FIXME: BM_DRAWPIXEL
97
98 #include <cfg/debug.h>
99
100
101 /*! ANSI escape sequences flag: true for ESC state on */
102 static bool ansi_mode = false;
103
104
105 /*!
106  * Move (imaginary) cursor to column and row specified.
107  * Next text write will start a that row and col.
108  */
109 void text_moveto(struct Bitmap *bm, int row, int col)
110 {
111         ASSERT(col >= 0);
112         ASSERT(col < bm->width / bm->font->width);
113         ASSERT(row >= 0);
114         ASSERT(row < bm->height / bm->font->height);
115
116         bm->penX = col * bm->font->width;
117         bm->penY = row * bm->font->height;
118 }
119
120
121 /*!
122  * Move (imaginary) cursor to coordinates specified.
123  */
124 void text_setcoord(struct Bitmap *bm, int x, int y)
125 {
126         bm->penX = x;
127         bm->penY = y;
128 }
129
130
131 /*!
132  * Render char \a c on Bitmap \a bm.
133  */
134 static int text_putglyph(char c, struct Bitmap *bm)
135 {
136         const uint8_t * PROGMEM glyph;  /* font is in progmem */
137         uint8_t glyph_width, glyph_height, glyph_height_bytes;
138         unsigned char index = (unsigned char)c;
139
140         /* Check for out of range char and replace with '?' or first char in font. */
141         if (UNLIKELY(!FONT_HAS_GLYPH(bm->font, index)))
142         {
143                 kprintf("Illegal char '%c' (0x%02x)\n", index, index);
144                 if (FONT_HAS_GLYPH(bm->font, '?'))
145                         index = '?';
146                 else
147                         index = bm->font->first;
148         }
149
150         /* Make character relative to font start */
151         index -= bm->font->first;
152
153         glyph_height = bm->font->height;
154         // FIXME: for vertical fonts only
155         glyph_height_bytes = (glyph_height + 7) / 8;
156
157         if (bm->font->offset)
158         {
159                 /* Proportional font */
160                 glyph_width = bm->font->widths[index]; /* TODO: optimize away */
161                 glyph = bm->font->glyph + bm->font->offset[index];
162         }
163         else
164         {
165                 /*
166                  * Fixed-width font: compute the first column of pixels
167                  * of the selected glyph using the character code to index
168                  * the glyph array.
169                  */
170                 glyph_width = bm->font->width;
171
172                 //For horizontal fonts
173                 //glyph = bm->font->glyph + index * (((glyph_width + 7) / 8) * glyph_height);
174                 glyph = bm->font->glyph + index * glyph_height_bytes * glyph_width;
175         }
176
177         /* Slow path for styled glyphs */
178         if (UNLIKELY(bm->styles))
179         {
180                 uint8_t prev_dots = 0, italic_prev_dots = 0;
181                 uint8_t dots;
182                 uint8_t row, col;
183
184                 /* This style alone could be handled by the fast path too */
185                 if (bm->styles & STYLEF_CONDENSED)
186                         --glyph_width;
187
188                 if (bm->styles & STYLEF_EXPANDED)
189                         glyph_width *= 2;
190
191                 /* Check if glyph fits in the bitmap. */
192                 if ((bm->penX < 0) || (bm->penX + glyph_width > bm->width)
193                         || (bm->penY < 0) || (bm->penY + glyph_height > bm->height))
194                 {
195                         kprintf("bad coords x=%d y=%d\n", bm->penX, bm->penY);
196                         return 0;
197                 }
198
199                 for (row = 0; row < glyph_height_bytes; ++row)
200                 {
201                         /* For each dot column in the glyph... */
202                         for (col = 0; col < glyph_width; ++col)
203                         {
204                                 uint8_t src_col = col;
205                                 uint8_t i;
206
207                                 /* Expanded style: advances only once every two columns. */
208                                 if (bm->styles & STYLEF_EXPANDED)
209                                         src_col /= 2;
210
211                                 /* Fetch a column of dots from glyph. */
212                                 dots = PGM_READ_CHAR(glyph + src_col * glyph_height_bytes + row);
213
214                                 /* Italic: get lower 4 dots from previous column */
215                                 if (bm->styles & STYLEF_ITALIC)
216                                 {
217                                         uint8_t new_dots = dots;
218                                         dots = (dots & 0xF0) | italic_prev_dots;
219                                         italic_prev_dots = new_dots & 0x0F;
220                                 }
221
222                                 /* Bold: "or" pixels with the previous column */
223                                 if (bm->styles & STYLEF_BOLD)
224                                 {
225                                         uint8_t new_dots = dots;
226                                         dots |= prev_dots;
227                                         prev_dots = new_dots;
228                                 }
229
230                                 /* Underlined: turn on base pixel */
231                                 if (bm->styles & STYLEF_UNDERLINE)
232                                         dots |= 0x80;
233
234                                 /* Inverted: invert pixels */
235                                 if (bm->styles & STYLEF_INVERT)
236                                         dots = ~dots;
237
238                                 /* Output dots */
239                                 for (i = 0; i < 8 && (row * 8) + i < glyph_height; ++i)
240                                         BM_DRAWPIXEL(bm, bm->penX + col, bm->penY + row * 8 + i, dots & (1<<i));
241                         }
242                 }
243         }
244         else
245         {
246                 /* No style: fast vanilla copy of glyph to bitmap */
247                 gfx_blitRaster(bm, bm->penX, bm->penY, glyph, glyph_width, glyph_height, glyph_height_bytes);
248         }
249
250         /* Update current pen position */
251         bm->penX += glyph_width;
252
253         return c;
254 }
255
256
257 /*!
258  * Render char \c c, with (currently) limited ANSI escapes
259  * emulation support and '\n' for newline.
260  */
261 int text_putchar(char c, struct Bitmap *bm)
262 {
263         /* Handle ANSI escape sequences */
264         if (UNLIKELY(ansi_mode))
265         {
266                 switch (c)
267                 {
268                 case ANSI_ESC_CLEARSCREEN:
269                         gfx_bitmapClear(bm);
270                         bm->penX = 0;
271                         bm->penY = 0;
272                         text_style(bm, 0, STYLEF_MASK);
273                         break;
274                 DB(default:
275                         kprintf("Unknown ANSI esc code: %x\n", c);)
276                 }
277                 ansi_mode = false;
278         }
279         else if (c == '\033')  /* Enter ANSI ESC mode */
280         {
281                 ansi_mode = true;
282         }
283         else if (c == '\n')  /* Go one line down on a line-feed */
284         {
285                 if (bm->penY + bm->font->height < bm->height)
286                 {
287                         bm->penY += bm->font->height;
288                         bm->penX = 0;
289                 }
290         }
291         else
292         {
293                 text_putglyph(c, bm);
294         }
295         return c;
296 }
297
298
299 /*!
300  * Clear the screen and reset cursor position
301  */
302 void text_clear(struct Bitmap *bmp)
303 {
304         text_putchar('\x1b', bmp);
305         text_putchar('c', bmp);
306 }
307
308
309 void text_clearLine(struct Bitmap *bm, int line)
310 {
311         gfx_rectClear(bm, 0, line * bm->font->height, bm->width, (line + 1) * bm->font->height);
312 }
313
314
315 /**
316  * Set/clear algorithmic font style bits.
317  *
318  * \param bm     Pointer to Bitmap to affect.
319  * \param flags  Style flags to set
320  * \param mask   Mask of flags to modify
321  * \return       Old style flags
322  *
323  * Examples:
324  * Turn on bold, leave other styles alone
325  *   \code text_style(bm, STYLEF_BOLD, STYLEF_BOLD); \endcode
326  *
327  * Turn off bold and turn on italic, leave others as they are
328  *   \code text_style(bm, STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
329  *
330  * Query current style without chaning it
331  *   \code style = text_style(bm, 0, 0); \endcode
332  *
333  * Reset all styles (plain text)
334  *   \code text_style(bm, 0, STYLE_MASK); \endcode
335  */
336 uint8_t text_style(struct Bitmap *bm, uint8_t flags, uint8_t mask)
337 {
338         uint8_t old = bm->styles;
339         bm->styles = (bm->styles & ~mask) | flags;
340         return old;
341 }