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