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