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