Add some other modules.
[bertos.git] / bertos / gfx / text.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \version $Id$
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  * \author Stefano Fedrigo <aleph@develer.com>
37  *
38  * \brief Text graphic routines
39  */
40
41 #include <gfx/gfx.h>
42 #include <gfx/font.h>
43 #include <gfx/text.h>
44
45 #include <gfx/gfx_p.h> // FIXME: BM_DRAWPIXEL
46
47 #include <cfg/debug.h>
48
49
50 /**
51  * ANSI escape sequences flag: true for ESC state on.
52  *
53  * \todo Move to Bitmap.flags.
54  */
55 static bool ansi_mode = false;
56
57 /**
58  * Move (imaginary) cursor to coordinates specified.
59  */
60 void text_setCoord(struct Bitmap *bm, int x, int y)
61 {
62         bm->penX = x;
63         bm->penY = y;
64 }
65
66
67 /**
68  * Move (imaginary) cursor to column and row specified.
69  * Next text write will start a that row and col.
70  */
71 void text_moveTo(struct Bitmap *bm, int row, int col)
72 {
73         ASSERT(col >= 0);
74         ASSERT(col < bm->width / bm->font->width);
75         ASSERT(row >= 0);
76         ASSERT(row < bm->height / bm->font->height);
77
78         text_setCoord(bm, col * bm->font->width, row * bm->font->height);
79 }
80
81
82 /**
83  * Render char \a c on Bitmap \a bm.
84  */
85 static int text_putglyph(char c, struct Bitmap *bm)
86 {
87         const uint8_t * PROGMEM glyph;  /* font is in progmem */
88         uint8_t glyph_width, glyph_height, glyph_height_bytes;
89         unsigned char index = (unsigned char)c;
90
91         /* Check for out of range char and replace with '?' or first char in font. */
92         if (UNLIKELY(!FONT_HAS_GLYPH(bm->font, index)))
93         {
94                 kprintf("Illegal char '%c' (0x%02x)\n", index, index);
95                 if (FONT_HAS_GLYPH(bm->font, '?'))
96                         index = '?';
97                 else
98                         index = bm->font->first;
99         }
100
101         /* Make character relative to font start */
102         index -= bm->font->first;
103
104         glyph_height = bm->font->height;
105         // FIXME: for vertical fonts only
106         glyph_height_bytes = (glyph_height + 7) / 8;
107
108         if (bm->font->offset)
109         {
110                 /* Proportional font */
111                 glyph_width = bm->font->widths[index]; /* TODO: optimize away */
112                 glyph = bm->font->glyph + bm->font->offset[index];
113         }
114         else
115         {
116                 /*
117                  * Fixed-width font: compute the first column of pixels
118                  * of the selected glyph using the character code to index
119                  * the glyph array.
120                  */
121                 glyph_width = bm->font->width;
122
123                 //For horizontal fonts
124                 //glyph = bm->font->glyph + index * (((glyph_width + 7) / 8) * glyph_height);
125                 glyph = bm->font->glyph + index * glyph_height_bytes * glyph_width;
126         }
127
128         /* Slow path for styled glyphs */
129         if (UNLIKELY(bm->styles))
130         {
131                 uint8_t styles = bm->styles;
132                 uint8_t prev_dots = 0, italic_prev_dots = 0;
133                 uint8_t dots;
134                 uint8_t row, col, row_bit;
135
136                 /*
137                  * To avoid repeating clipping and other expensive computations,
138                  * we cluster calls to gfx_blitRaster() using a small buffer.
139                  */
140                 #define CONFIG_TEXT_RENDER_OPTIMIZE 1
141                 #if CONFIG_TEXT_RENDER_OPTIMIZE
142                         #define RENDER_BUF_WIDTH 12
143                         #define RENDER_BUF_HEIGHT 8
144                         uint8_t render_buf[RAST_SIZE(RENDER_BUF_WIDTH, RENDER_BUF_HEIGHT)];
145                         uint8_t render_xpos = 0;
146                 #endif
147
148                 /* This style alone could be handled by the fast path too */
149                 if (bm->styles & STYLEF_CONDENSED)
150                         --glyph_width;
151
152                 if (bm->styles & STYLEF_EXPANDED)
153                         glyph_width *= 2;
154
155                 for (row = 0, row_bit = 0; row < glyph_height_bytes; ++row, row_bit += 8)
156                 {
157                         /* For each dot column in the glyph... */
158                         for (col = 0; col < glyph_width; ++col)
159                         {
160                                 uint8_t src_col = col;
161
162                                 /* Expanded style: advances only once every two columns. */
163                                 if (styles & STYLEF_EXPANDED)
164                                         src_col /= 2;
165
166                                 /* Fetch a column of dots from glyph. */
167                                 dots = PGM_READ_CHAR(RAST_ADDR(glyph, src_col, row_bit, glyph_width));
168
169                                 /* Italic: get lower 4 dots from previous column */
170                                 if (styles & STYLEF_ITALIC)
171                                 {
172                                         uint8_t new_dots = dots;
173                                         dots = (dots & 0xF0) | italic_prev_dots;
174                                         italic_prev_dots = new_dots & 0x0F;
175                                 }
176
177                                 /* Bold: "or" pixels with the previous column */
178                                 if (styles & STYLEF_BOLD)
179                                 {
180                                         uint8_t new_dots = dots;
181                                         dots |= prev_dots;
182                                         prev_dots = new_dots;
183                                 }
184
185                                 /* Underlined: turn on base pixel */
186                                 if ((styles & STYLEF_UNDERLINE)
187                                         && (row == glyph_height_bytes - 1))
188                                         dots |= (1 << (glyph_height - row_bit - 1));
189
190                                 /* Inverted: invert pixels */
191                                 if (styles & STYLEF_INVERT)
192                                         dots = ~dots;
193
194                                 /* Output dots */
195                                 #if CONFIG_TEXT_RENDER_OPTIMIZE
196                                         render_buf[render_xpos++] = dots;
197                                         if (render_xpos == RENDER_BUF_WIDTH)
198                                         {
199                                                 gfx_blitRaster(bm, bm->penX + col - render_xpos + 1, bm->penY + row_bit,
200                                                         render_buf, render_xpos,
201                                                         MIN((uint8_t)RENDER_BUF_HEIGHT, (uint8_t)(glyph_height - row_bit)),
202                                                         RENDER_BUF_WIDTH);
203                                                 render_xpos = 0;
204                                         }
205                                 #else
206                                         gfx_blitRaster(bm, bm->penX + col, bm->penY + row_bit,
207                                                 &dots, 1, MIN((uint8_t)8, glyph_height - row_bit), 1);
208                                 #endif
209                         }
210
211                         #if CONFIG_TEXT_RENDER_OPTIMIZE
212                                 /* Flush out rest of render buffer */
213                                 if (render_xpos != 0)
214                                 {
215                                         gfx_blitRaster(bm, bm->penX + col - render_xpos, bm->penY + row_bit,
216                                                 render_buf, render_xpos,
217                                                 MIN((uint8_t)RENDER_BUF_HEIGHT, (uint8_t)(glyph_height - row_bit)),
218                                                 RENDER_BUF_WIDTH);
219                                         render_xpos = 0;
220                                 }
221                         #endif
222                 }
223         }
224         else
225         {
226                 /* No style: fast vanilla copy of glyph to bitmap */
227                 gfx_blitRaster(bm, bm->penX, bm->penY, glyph, glyph_width, glyph_height, glyph_width);
228         }
229
230         /* Update current pen position */
231         bm->penX += glyph_width;
232
233         return c;
234 }
235
236
237 /**
238  * Render char \c c, with (currently) limited ANSI escapes
239  * emulation support and '\n' for newline.
240  */
241 int text_putchar(char c, struct Bitmap *bm)
242 {
243         /* Handle ANSI escape sequences */
244         if (UNLIKELY(ansi_mode))
245         {
246                 switch (c)
247                 {
248                 case ANSI_ESC_CLEARSCREEN:
249                         gfx_bitmapClear(bm);
250                         bm->penX = 0;
251                         bm->penY = 0;
252                         text_style(bm, 0, STYLEF_MASK);
253                         break;
254                 DB(default:
255                         kprintf("Unknown ANSI esc code: %x\n", c);)
256                 }
257                 ansi_mode = false;
258         }
259         else if (c == '\033')  /* Enter ANSI ESC mode */
260         {
261                 ansi_mode = true;
262         }
263         else if (c == '\n')  /* Go one line down on a line-feed */
264         {
265                 if (bm->penY + bm->font->height < bm->height)
266                 {
267                         bm->penY += bm->font->height;
268                         bm->penX = 0;
269                 }
270         }
271         else
272         {
273                 text_putglyph(c, bm);
274         }
275         return c;
276 }
277
278
279 /**
280  * Clear the screen and reset cursor position
281  */
282 void text_clear(struct Bitmap *bmp)
283 {
284         text_putchar('\x1b', bmp);
285         text_putchar('c', bmp);
286 }
287
288
289 void text_clearLine(struct Bitmap *bm, int line)
290 {
291         gfx_rectClear(bm, 0, line * bm->font->height, bm->width, (line + 1) * bm->font->height);
292 }
293
294
295 /**
296  * Set/clear algorithmic font style bits.
297  *
298  * \param bm     Pointer to Bitmap to affect.
299  * \param flags  Style flags to set
300  * \param mask   Mask of flags to modify
301  * \return       Old style flags
302  *
303  * Examples:
304  * Turn on bold, leave other styles alone
305  *   \code text_style(bm, STYLEF_BOLD, STYLEF_BOLD); \endcode
306  *
307  * Turn off bold and turn on italic, leave others as they are
308  *   \code text_style(bm, STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
309  *
310  * Query current style without chaning it
311  *   \code style = text_style(bm, 0, 0); \endcode
312  *
313  * Reset all styles (plain text)
314  *   \code text_style(bm, 0, STYLE_MASK); \endcode
315  */
316 uint8_t text_style(struct Bitmap *bm, uint8_t flags, uint8_t mask)
317 {
318         uint8_t old = bm->styles;
319         bm->styles = (bm->styles & ~mask) | flags;
320         return old;
321 }