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