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