Fix bug with NULL buffers (caught with unit test).
[bertos.git] / mware / text.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See devlib/README 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.11  2005/01/20 18:46:31  aleph
19  *#* Fix progmem includes.
20  *#*
21  *#* Revision 1.10  2005/01/08 09:20:12  bernie
22  *#* Really make it work on both architectures.
23  *#*
24  *#* Revision 1.9  2004/12/31 16:44:29  bernie
25  *#* Sanitize for non-Harvard processors.
26  *#*
27  *#* Revision 1.8  2004/11/16 21:16:28  bernie
28  *#* Update to new naming scheme in mware/gfx.c.
29  *#*
30  *#* Revision 1.7  2004/09/20 03:28:28  bernie
31  *#* Fix header.
32  *#*
33  *#* Revision 1.6  2004/09/14 20:57:15  bernie
34  *#* Use debug.h instead of kdebug.h.
35  *#*
36  *#* Revision 1.5  2004/09/06 21:51:26  bernie
37  *#* Extend interface to allow any algorithmic style.
38  *#*
39  *#* Revision 1.2  2004/06/03 11:27:09  bernie
40  *#* Add dual-license information.
41  *#*
42  *#* Revision 1.1  2004/05/23 15:43:16  bernie
43  *#* Import mware modules.
44  *#*
45  *#* Revision 1.17  2004/05/15 16:57:01  aleph
46  *#* Fixes for non-DEBUG build
47  *#*
48  *#* Revision 1.16  2004/04/03 20:42:49  aleph
49  *#* Add text_clear()
50  *#*
51  *#* Revision 1.15  2004/03/24 15:03:45  bernie
52  *#* Use explicit include paths; clean Doxygen comments
53  *#*
54  *#* Revision 1.14  2004/03/19 16:52:28  bernie
55  *#* Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
56  *#*
57  *#* Revision 1.13  2004/03/17 18:23:32  bernie
58  *#* Oops.
59  *#*
60  *#* Revision 1.12  2004/03/17 18:03:22  bernie
61  *#* Make diagnostic message shorter
62  *#*
63  *#* Revision 1.11  2004/03/13 22:52:54  aleph
64  *#* documentation fixes
65  *#*/
66
67 #include "gfx.h"
68 #include "font.h"
69 #include "text.h"
70
71 #include <debug.h>
72
73
74 /*!
75  * Flags degli stili algoritmici
76  *
77  * La routine di rendering del testo e' in grado di applicare
78  * delle semplici trasformazioni al font interno per generare
79  * automaticamente degli stili predefiniti (bold, italic,
80  * underline) a partire dal set di caratteri plain.
81  */
82 static uint8_t text_styles;
83
84 /*! ANSI escape sequences flag: true for ESC state on */
85 static bool ansi_mode = false;
86
87
88 /*!
89  * Move (imaginary) cursor to column and row specified.
90  * Next text write will start a that row and col.
91  */
92 void text_moveto(struct Bitmap *bm, int row, int col)
93 {
94         ASSERT(col >= 0);
95         ASSERT(col < bm->width / FONT_WIDTH);
96         ASSERT(row >= 0);
97         ASSERT(row < bm->height / FONT_HEIGHT);
98
99         bm->penX = col * FONT_WIDTH;
100         bm->penY = row * FONT_HEIGHT;
101 }
102
103
104 /*!
105  * Move (imaginary) cursor to coordinates specified.
106  */
107 void text_setcoord(struct Bitmap *bm, int x, int y)
108 {
109         bm->penX = x;
110         bm->penY = y;
111 }
112
113
114 /*!
115  * Render char \a c on Bitmap \a bm
116  */
117 static int text_putglyph(char c, struct Bitmap *bm)
118 {
119         const uint8_t * PROGMEM glyph;  /* font is in progmem */
120         uint8_t glyph_width;
121         uint8_t i;
122         uint8_t *buf;
123
124         /*
125          * Il carattere da stampare viene usato come indice per prelevare
126          * la prima colonna di dots del glyph all'interno del font.
127          */
128         glyph = font + (((unsigned char)c) * FONT_WIDTH);
129         glyph_width = FONT_WIDTH;
130
131         if (text_styles & STYLEF_CONDENSED)
132                 --glyph_width;
133
134         if (text_styles & STYLEF_EXPANDED)
135                 glyph_width *= 2;
136
137         /* The y coord is rounded at multiples of 8 for simplicity */
138         bm->penY &= ~((coord_t)7);
139
140         /* Check if glyph to write fits in the bitmap */
141         if ((bm->penX < 0) || (bm->penX + glyph_width > bm->width) ||
142                 (bm->penY < 0) || (bm->penY + FONT_HEIGHT > bm->height))
143         {
144                 DB(kprintf("bad coords x=%d y=%d\n", bm->penX, bm->penY);)
145                 return 0;
146         }
147
148         /* Locate position where to write in the raster */
149         buf = bm->raster + bm->penY / 8 * bm->width + bm->penX;
150
151         bm->penX += glyph_width;
152
153         /* If some styles are set */
154         if (text_styles)
155         {
156                 uint8_t prev_dots = 0, italic_prev_dots = 0, new_dots;
157                 uint8_t dots;
158
159                 /* Per ogni colonna di dot del glyph... */
160                 for (i = 0; i < glyph_width; ++i)
161                 {
162                         dots = PGM_READ_CHAR(glyph);
163
164                         /* Advance to next column in glyph.
165                          * Expand: advances only once every two columns
166                          */
167                         if (!(text_styles & STYLEF_EXPANDED) || (i & 1))
168                                 glyph++;
169
170                         /* Italic: get lower 4 dots from previous column */
171                         if (text_styles & STYLEF_ITALIC)
172                         {
173                                 new_dots = dots;
174                                 dots = (dots & 0xF0) | italic_prev_dots;
175                                 italic_prev_dots = new_dots & 0x0F;
176                         }
177
178                         /* Bold: "or" pixels with the previous column */
179                         if (text_styles & STYLEF_BOLD)
180                         {
181                                 new_dots = dots;
182                                 dots |= prev_dots;
183                                 prev_dots = new_dots;
184                         }
185
186                         /* Underlined: turn on base pixel */
187                         if (text_styles & STYLEF_UNDERLINE)
188                                 dots |= 0x80;
189
190                         /* Inverted: invert pixels */
191                         if (text_styles & STYLEF_INVERT)
192                                 dots = ~dots;
193
194                         /* Output dots */
195                         *buf++ = dots;
196                 }
197         }
198         else /* No style: fast vanilla copy of glyph to line buffer */
199                 while (glyph_width--)
200                         *buf++ = PGM_READ_CHAR(glyph++);
201
202         return c;
203 }
204
205
206 /*!
207  * Render char \c c, with (currently) limited ANSI escapes
208  * emulation support and '\n' for newline.
209  */
210 int text_putchar(char c, struct Bitmap *bm)
211 {
212         /* Handle ANSI escape sequences */
213         if (ansi_mode)
214         {
215                 switch (c)
216                 {
217                 case ANSI_ESC_CLEARSCREEN:
218                         gfx_bitmapClear(bm);
219                         bm->penX = 0;
220                         bm->penY = 0;
221                         text_style(0, STYLEF_MASK);
222                         break;
223                 DB(default:
224                         kprintf("Unknown ANSI esc code: %x\n", c);)
225                 }
226                 ansi_mode = false;
227         }
228         else if (c == '\033')  /* Enter ANSI ESC mode */
229         {
230                 ansi_mode = true;
231         }
232         else if (c == '\n')  /* Go one line down on a line-feed */
233         {
234                 if (bm->penY + FONT_HEIGHT < bm->height)
235                 {
236                         bm->penY += FONT_HEIGHT;
237                         bm->penX = 0;
238                 }
239         }
240         else
241         {
242                 text_putglyph(c, bm);
243         }
244         return c;
245 }
246
247
248 /*!
249  * Clear the screen and reset cursor position
250  */
251 void text_clear(struct Bitmap *bmp)
252 {
253         text_putchar('\x1b', bmp);
254         text_putchar('c', bmp);
255 }
256
257
258 void text_clearLine(struct Bitmap *bmp, int line)
259 {
260         gfx_rectClear(bmp, 0, line * FONT_HEIGHT, bmp->width, (line + 1) * FONT_HEIGHT);
261 }
262
263
264 /*!
265  * Set/clear algorithmic font style bits.
266  *
267  * \param flags  Style flags to set
268  * \param mask   Mask of flags to modify
269  * \return       Old style flags
270  *
271  * Examples:
272  * Turn on bold, leave other styles alone
273  *   \code prt_style(STYLEF_BOLD, STYLEF_BOLD); \endcode
274  *
275  * Turn off bold and turn on italic, leave others as they are
276  *   \code prt_style(STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
277  *
278  * Query current style without chaning it
279  *   \code style = prt_style(0, 0); \endcode
280  *
281  * Reset all styles (plain text)
282  *   \code prt_style(0, STYLE_MASK); \endcode
283  */
284 uint8_t text_style(uint8_t flags, uint8_t mask)
285 {
286         uint8_t old = text_styles;
287         text_styles = (text_styles & ~mask) | flags;
288         return old;
289 }