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.
9 * \brief Text graphic routines
12 * \author Bernardo Innocenti <bernie@develer.com>
13 * \author Stefano Fedrigo <aleph@develer.com>
18 *#* Revision 1.5 2006/03/07 22:18:04 bernie
19 *#* Correctly compute text width for prop fonts; Make styles a per-bitmap attribute.
21 *#* Revision 1.4 2006/02/15 09:10:15 bernie
22 *#* Implement prop fonts; Fix algo styles.
24 *#* Revision 1.3 2006/02/10 12:31:55 bernie
25 *#* Add multiple font support in bitmaps.
27 *#* Revision 1.2 2005/11/04 18:17:45 bernie
28 *#* Fix header guards and includes for new location of gfx module.
30 *#* Revision 1.1 2005/11/04 18:11:35 bernie
31 *#* Move graphics stuff from mware/ to gfx/.
33 *#* Revision 1.13 2005/11/04 16:20:02 bernie
34 *#* Fix reference to README.devlib in header.
36 *#* Revision 1.12 2005/04/11 19:10:28 bernie
37 *#* Include top-level headers from cfg/ subdir.
39 *#* Revision 1.11 2005/01/20 18:46:31 aleph
40 *#* Fix progmem includes.
42 *#* Revision 1.10 2005/01/08 09:20:12 bernie
43 *#* Really make it work on both architectures.
45 *#* Revision 1.9 2004/12/31 16:44:29 bernie
46 *#* Sanitize for non-Harvard processors.
48 *#* Revision 1.8 2004/11/16 21:16:28 bernie
49 *#* Update to new naming scheme in mware/gfx.c.
51 *#* Revision 1.7 2004/09/20 03:28:28 bernie
54 *#* Revision 1.6 2004/09/14 20:57:15 bernie
55 *#* Use debug.h instead of kdebug.h.
57 *#* Revision 1.5 2004/09/06 21:51:26 bernie
58 *#* Extend interface to allow any algorithmic style.
60 *#* Revision 1.2 2004/06/03 11:27:09 bernie
61 *#* Add dual-license information.
63 *#* Revision 1.1 2004/05/23 15:43:16 bernie
64 *#* Import mware modules.
66 *#* Revision 1.17 2004/05/15 16:57:01 aleph
67 *#* Fixes for non-DEBUG build
69 *#* Revision 1.16 2004/04/03 20:42:49 aleph
72 *#* Revision 1.15 2004/03/24 15:03:45 bernie
73 *#* Use explicit include paths; clean Doxygen comments
75 *#* Revision 1.14 2004/03/19 16:52:28 bernie
76 *#* Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
78 *#* Revision 1.13 2004/03/17 18:23:32 bernie
81 *#* Revision 1.12 2004/03/17 18:03:22 bernie
82 *#* Make diagnostic message shorter
84 *#* Revision 1.11 2004/03/13 22:52:54 aleph
85 *#* documentation fixes
93 #include <gfx/gfx_p.h> // FIXME: BM_DRAWPIXEL
95 #include <cfg/debug.h>
98 /*! ANSI escape sequences flag: true for ESC state on */
99 static bool ansi_mode = false;
103 * Move (imaginary) cursor to column and row specified.
104 * Next text write will start a that row and col.
106 void text_moveto(struct Bitmap *bm, int row, int col)
109 ASSERT(col < bm->width / bm->font->width);
111 ASSERT(row < bm->height / bm->font->height);
113 bm->penX = col * bm->font->width;
114 bm->penY = row * bm->font->height;
119 * Move (imaginary) cursor to coordinates specified.
121 void text_setcoord(struct Bitmap *bm, int x, int y)
129 * Render char \a c on Bitmap \a bm.
131 static int text_putglyph(char c, struct Bitmap *bm)
133 const uint8_t * PROGMEM glyph; /* font is in progmem */
134 uint8_t glyph_width, glyph_height, glyph_height_bytes;
135 unsigned char index = (unsigned char)c;
137 /* Check for out of range char and replace with '?' or first char in font. */
138 if (index < bm->font->first || index > bm->font->last)
140 kprintf("Illegal char '%c' (0x%02x)\n", index, index);
141 if ('?' >= bm->font->first && '?' <= bm->font->last)
144 index = bm->font->first;
147 /* Make character relative to font start */
148 index -= bm->font->first;
150 glyph_height = bm->font->height;
151 // FIXME: for vertical fonts only
152 glyph_height_bytes = (glyph_height + 7) / 8;
154 if (bm->font->offset)
156 /* Proportional font */
157 glyph_width = bm->font->widths[index]; /* TODO: optimize away */
158 glyph = bm->font->glyph + bm->font->offset[index];
163 * Fixed-width font: compute the first column of pixels
164 * of the selected glyph using the character code to index
167 glyph_width = bm->font->width;
169 //For horizontal fonts
170 //glyph = bm->font->glyph + index * (((glyph_width + 7) / 8) * glyph_height);
171 glyph = bm->font->glyph + index * glyph_height_bytes * glyph_width;
174 /* Slow path for styled glyphs */
177 uint8_t prev_dots = 0, italic_prev_dots = 0;
181 /* This style alone could be handled by the fast path too */
182 if (bm->styles & STYLEF_CONDENSED)
185 if (bm->styles & STYLEF_EXPANDED)
188 /* Check if glyph fits in the bitmap. */
189 if ((bm->penX < 0) || (bm->penX + glyph_width > bm->width)
190 || (bm->penY < 0) || (bm->penY + glyph_height > bm->height))
192 kprintf("bad coords x=%d y=%d\n", bm->penX, bm->penY);
196 for (row = 0; row < glyph_height_bytes; ++row)
198 /* For each dot column in the glyph... */
199 for (col = 0; col < glyph_width; ++col)
201 uint8_t src_col = col;
204 /* Expanded style: advances only once every two columns. */
205 if (bm->styles & STYLEF_EXPANDED)
208 /* Fetch a column of dots from glyph. */
209 dots = PGM_READ_CHAR(glyph + src_col * glyph_height_bytes + row);
211 /* Italic: get lower 4 dots from previous column */
212 if (bm->styles & STYLEF_ITALIC)
214 uint8_t new_dots = dots;
215 dots = (dots & 0xF0) | italic_prev_dots;
216 italic_prev_dots = new_dots & 0x0F;
219 /* Bold: "or" pixels with the previous column */
220 if (bm->styles & STYLEF_BOLD)
222 uint8_t new_dots = dots;
224 prev_dots = new_dots;
227 /* Underlined: turn on base pixel */
228 if (bm->styles & STYLEF_UNDERLINE)
231 /* Inverted: invert pixels */
232 if (bm->styles & STYLEF_INVERT)
236 for (i = 0; i < 8 && (row * 8) + i < glyph_height; ++i)
237 BM_DRAWPIXEL(bm, bm->penX + col, bm->penY + row * 8 + i, dots & (1<<i));
243 /* No style: fast vanilla copy of glyph to bitmap */
244 gfx_blitRaster(bm, bm->penX, bm->penY, glyph, glyph_width, glyph_height, glyph_height_bytes);
247 /* Update current pen position */
248 bm->penX += glyph_width;
255 * Render char \c c, with (currently) limited ANSI escapes
256 * emulation support and '\n' for newline.
258 int text_putchar(char c, struct Bitmap *bm)
260 /* Handle ANSI escape sequences */
265 case ANSI_ESC_CLEARSCREEN:
269 text_style(bm, 0, STYLEF_MASK);
272 kprintf("Unknown ANSI esc code: %x\n", c);)
276 else if (c == '\033') /* Enter ANSI ESC mode */
280 else if (c == '\n') /* Go one line down on a line-feed */
282 if (bm->penY + bm->font->height < bm->height)
284 bm->penY += bm->font->height;
290 text_putglyph(c, bm);
297 * Clear the screen and reset cursor position
299 void text_clear(struct Bitmap *bmp)
301 text_putchar('\x1b', bmp);
302 text_putchar('c', bmp);
306 void text_clearLine(struct Bitmap *bm, int line)
308 gfx_rectClear(bm, 0, line * bm->font->height, bm->width, (line + 1) * bm->font->height);
313 * Set/clear algorithmic font style bits.
315 * \param flags Style flags to set
316 * \param mask Mask of flags to modify
317 * \return Old style flags
320 * Turn on bold, leave other styles alone
321 * \code text_style(bm, STYLEF_BOLD, STYLEF_BOLD); \endcode
323 * Turn off bold and turn on italic, leave others as they are
324 * \code text_style(bm, STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
326 * Query current style without chaning it
327 * \code style = text_style(bm, 0, 0); \endcode
329 * Reset all styles (plain text)
330 * \code text_style(bm, 0, STYLE_MASK); \endcode
332 uint8_t text_style(struct Bitmap *bm, uint8_t flags, uint8_t mask)
334 uint8_t old = bm->styles;
335 bm->styles = (bm->styles & ~mask) | flags;