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