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