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