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