Enhance text rendering to arbitrary x,y coords.
[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.10  2006/04/27 05:39:23  bernie
19  *#* Enhance text rendering to arbitrary x,y coords.
20  *#*
21  *#* Revision 1.9  2006/04/11 00:08:24  bernie
22  *#* text_offset(): New function, but I'm not quite confident with the design.
23  *#*
24  *#* Revision 1.8  2006/03/22 09:50:37  bernie
25  *#* Use the same format for fonts and rasters.
26  *#*
27  *#* Revision 1.7  2006/03/20 17:51:55  bernie
28  *#* Cleanups.
29  *#*
30  *#* Revision 1.6  2006/03/13 02:05:54  bernie
31  *#* Mark slow paths as UNLIKELY.
32  *#*
33  *#* Revision 1.5  2006/03/07 22:18:04  bernie
34  *#* Correctly compute text width for prop fonts; Make styles a per-bitmap attribute.
35  *#*
36  *#* Revision 1.4  2006/02/15 09:10:15  bernie
37  *#* Implement prop fonts; Fix algo styles.
38  *#*
39  *#* Revision 1.3  2006/02/10 12:31:55  bernie
40  *#* Add multiple font support in bitmaps.
41  *#*
42  *#* Revision 1.2  2005/11/04 18:17:45  bernie
43  *#* Fix header guards and includes for new location of gfx module.
44  *#*
45  *#* Revision 1.1  2005/11/04 18:11:35  bernie
46  *#* Move graphics stuff from mware/ to gfx/.
47  *#*
48  *#* Revision 1.13  2005/11/04 16:20:02  bernie
49  *#* Fix reference to README.devlib in header.
50  *#*
51  *#* Revision 1.12  2005/04/11 19:10:28  bernie
52  *#* Include top-level headers from cfg/ subdir.
53  *#*
54  *#* Revision 1.11  2005/01/20 18:46:31  aleph
55  *#* Fix progmem includes.
56  *#*
57  *#* Revision 1.10  2005/01/08 09:20:12  bernie
58  *#* Really make it work on both architectures.
59  *#*
60  *#* Revision 1.9  2004/12/31 16:44:29  bernie
61  *#* Sanitize for non-Harvard processors.
62  *#*
63  *#* Revision 1.8  2004/11/16 21:16:28  bernie
64  *#* Update to new naming scheme in mware/gfx.c.
65  *#*
66  *#* Revision 1.7  2004/09/20 03:28:28  bernie
67  *#* Fix header.
68  *#*
69  *#* Revision 1.6  2004/09/14 20:57:15  bernie
70  *#* Use debug.h instead of kdebug.h.
71  *#*
72  *#* Revision 1.5  2004/09/06 21:51:26  bernie
73  *#* Extend interface to allow any algorithmic style.
74  *#*
75  *#* Revision 1.2  2004/06/03 11:27:09  bernie
76  *#* Add dual-license information.
77  *#*
78  *#* Revision 1.1  2004/05/23 15:43:16  bernie
79  *#* Import mware modules.
80  *#*
81  *#* Revision 1.17  2004/05/15 16:57:01  aleph
82  *#* Fixes for non-DEBUG build
83  *#*
84  *#* Revision 1.16  2004/04/03 20:42:49  aleph
85  *#* Add text_clear()
86  *#*
87  *#* Revision 1.15  2004/03/24 15:03:45  bernie
88  *#* Use explicit include paths; clean Doxygen comments
89  *#*
90  *#* Revision 1.14  2004/03/19 16:52:28  bernie
91  *#* Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
92  *#*
93  *#* Revision 1.13  2004/03/17 18:23:32  bernie
94  *#* Oops.
95  *#*
96  *#* Revision 1.12  2004/03/17 18:03:22  bernie
97  *#* Make diagnostic message shorter
98  *#*
99  *#* Revision 1.11  2004/03/13 22:52:54  aleph
100  *#* documentation fixes
101  *#*/
102
103 #include <gfx/gfx.h>
104 #include <gfx/font.h>
105 #include <gfx/text.h>
106 #include <gfx/text.h>
107
108 #include <gfx/gfx_p.h> // FIXME: BM_DRAWPIXEL
109
110 #include <cfg/debug.h>
111
112
113 /**
114  * ANSI escape sequences flag: true for ESC state on.
115  *
116  * \fixme Move to Bitmap.flags.
117  */
118 static bool ansi_mode = false;
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  * Move (imaginary) cursor to column and row specified.
132  * Next text write will start a that row and col.
133  */
134 void text_moveTo(struct Bitmap *bm, int row, int col)
135 {
136         ASSERT(col >= 0);
137         ASSERT(col < bm->width / bm->font->width);
138         ASSERT(row >= 0);
139         ASSERT(row < bm->height / bm->font->height);
140
141         text_setCoord(bm, col * bm->font->width, row * bm->font->height);
142 }
143
144
145 /*!
146  * Render char \a c on Bitmap \a bm.
147  */
148 static int text_putglyph(char c, struct Bitmap *bm)
149 {
150         const uint8_t * PROGMEM glyph;  /* font is in progmem */
151         uint8_t glyph_width, glyph_height, glyph_height_bytes;
152         unsigned char index = (unsigned char)c;
153
154         /* Check for out of range char and replace with '?' or first char in font. */
155         if (UNLIKELY(!FONT_HAS_GLYPH(bm->font, index)))
156         {
157                 kprintf("Illegal char '%c' (0x%02x)\n", index, index);
158                 if (FONT_HAS_GLYPH(bm->font, '?'))
159                         index = '?';
160                 else
161                         index = bm->font->first;
162         }
163
164         /* Make character relative to font start */
165         index -= bm->font->first;
166
167         glyph_height = bm->font->height;
168         // FIXME: for vertical fonts only
169         glyph_height_bytes = ROUND_UP2(glyph_height, 8);
170
171         if (bm->font->offset)
172         {
173                 /* Proportional font */
174                 glyph_width = bm->font->widths[index]; /* TODO: optimize away */
175                 glyph = bm->font->glyph + bm->font->offset[index];
176         }
177         else
178         {
179                 /*
180                  * Fixed-width font: compute the first column of pixels
181                  * of the selected glyph using the character code to index
182                  * the glyph array.
183                  */
184                 glyph_width = bm->font->width;
185
186                 //For horizontal fonts
187                 //glyph = bm->font->glyph + index * (((glyph_width + 7) / 8) * glyph_height);
188                 glyph = bm->font->glyph + index * glyph_height_bytes * glyph_width;
189         }
190
191         /* Slow path for styled glyphs */
192         if (UNLIKELY(bm->styles))
193         {
194                 uint8_t styles = bm->styles;
195                 uint8_t prev_dots = 0, italic_prev_dots = 0;
196                 uint8_t dots;
197                 uint8_t row, col;
198
199                 /* This style alone could be handled by the fast path too */
200                 if (bm->styles & STYLEF_CONDENSED)
201                         --glyph_width;
202
203                 if (bm->styles & STYLEF_EXPANDED)
204                         glyph_width *= 2;
205
206                 /* Check if glyph fits in the bitmap. */
207                 if ((bm->penX < 0) || (bm->penX + glyph_width > bm->width)
208                         || (bm->penY < 0) || (bm->penY + glyph_height > bm->height))
209                 {
210                         kprintf("bad coords x=%d y=%d\n", bm->penX, bm->penY);
211                         return 0;
212                 }
213
214                 for (row = 0; row < glyph_height_bytes; ++row)
215                 {
216                         /* For each dot column in the glyph... */
217                         for (col = 0; col < glyph_width; ++col)
218                         {
219                                 uint8_t src_col = col;
220                                 uint8_t i;
221
222                                 /* Expanded style: advances only once every two columns. */
223                                 if (styles & STYLEF_EXPANDED)
224                                         src_col /= 2;
225
226                                 /* Fetch a column of dots from glyph. */
227                                 dots = PGM_READ_CHAR(RAST_ADDR(glyph, src_col, row * 8, glyph_width));
228
229                                 /* Italic: get lower 4 dots from previous column */
230                                 if (styles & STYLEF_ITALIC)
231                                 {
232                                         uint8_t new_dots = dots;
233                                         dots = (dots & 0xF0) | italic_prev_dots;
234                                         italic_prev_dots = new_dots & 0x0F;
235                                 }
236
237                                 /* Bold: "or" pixels with the previous column */
238                                 if (styles & STYLEF_BOLD)
239                                 {
240                                         uint8_t new_dots = dots;
241                                         dots |= prev_dots;
242                                         prev_dots = new_dots;
243                                 }
244
245                                 /* Underlined: turn on base pixel */
246                                 if ((styles & STYLEF_UNDERLINE)
247                                         && (row == glyph_height_bytes - 1))
248                                         dots |= (1 << (glyph_height - row * 8 - 1));
249
250                                 /* Inverted: invert pixels */
251                                 if (styles & STYLEF_INVERT)
252                                         dots = ~dots;
253
254                                 /* Output dots */
255                                 for (i = 0; i < 8 && (row * 8) + i < glyph_height; ++i)
256                                         BM_DRAWPIXEL(bm, bm->penX + col, bm->penY + row * 8 + i, dots & (1<<i));
257                         }
258                 }
259         }
260         else
261         {
262                 /* No style: fast vanilla copy of glyph to bitmap */
263                 gfx_blitRaster(bm, bm->penX, bm->penY, glyph, glyph_width, glyph_height, glyph_width);
264         }
265
266         /* Update current pen position */
267         bm->penX += glyph_width;
268
269         return c;
270 }
271
272
273 /*!
274  * Render char \c c, with (currently) limited ANSI escapes
275  * emulation support and '\n' for newline.
276  */
277 int text_putchar(char c, struct Bitmap *bm)
278 {
279         /* Handle ANSI escape sequences */
280         if (UNLIKELY(ansi_mode))
281         {
282                 switch (c)
283                 {
284                 case ANSI_ESC_CLEARSCREEN:
285                         gfx_bitmapClear(bm);
286                         bm->penX = 0;
287                         bm->penY = 0;
288                         text_style(bm, 0, STYLEF_MASK);
289                         break;
290                 DB(default:
291                         kprintf("Unknown ANSI esc code: %x\n", c);)
292                 }
293                 ansi_mode = false;
294         }
295         else if (c == '\033')  /* Enter ANSI ESC mode */
296         {
297                 ansi_mode = true;
298         }
299         else if (c == '\n')  /* Go one line down on a line-feed */
300         {
301                 if (bm->penY + bm->font->height < bm->height)
302                 {
303                         bm->penY += bm->font->height;
304                         bm->penX = 0;
305                 }
306         }
307         else
308         {
309                 text_putglyph(c, bm);
310         }
311         return c;
312 }
313
314
315 /*!
316  * Clear the screen and reset cursor position
317  */
318 void text_clear(struct Bitmap *bmp)
319 {
320         text_putchar('\x1b', bmp);
321         text_putchar('c', bmp);
322 }
323
324
325 void text_clearLine(struct Bitmap *bm, int line)
326 {
327         gfx_rectClear(bm, 0, line * bm->font->height, bm->width, (line + 1) * bm->font->height);
328 }
329
330
331 /**
332  * Set/clear algorithmic font style bits.
333  *
334  * \param bm     Pointer to Bitmap to affect.
335  * \param flags  Style flags to set
336  * \param mask   Mask of flags to modify
337  * \return       Old style flags
338  *
339  * Examples:
340  * Turn on bold, leave other styles alone
341  *   \code text_style(bm, STYLEF_BOLD, STYLEF_BOLD); \endcode
342  *
343  * Turn off bold and turn on italic, leave others as they are
344  *   \code text_style(bm, STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
345  *
346  * Query current style without chaning it
347  *   \code style = text_style(bm, 0, 0); \endcode
348  *
349  * Reset all styles (plain text)
350  *   \code text_style(bm, 0, STYLE_MASK); \endcode
351  */
352 uint8_t text_style(struct Bitmap *bm, uint8_t flags, uint8_t mask)
353 {
354         uint8_t old = bm->styles;
355         bm->styles = (bm->styles & ~mask) | flags;
356         return old;
357 }