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.8 2006/03/22 09:50:37 bernie
19 *#* Use the same format for fonts and rasters.
21 *#* Revision 1.7 2006/03/20 17:51:55 bernie
24 *#* Revision 1.6 2006/03/13 02:05:54 bernie
25 *#* Mark slow paths as UNLIKELY.
27 *#* Revision 1.5 2006/03/07 22:18:04 bernie
28 *#* Correctly compute text width for prop fonts; Make styles a per-bitmap attribute.
30 *#* Revision 1.4 2006/02/15 09:10:15 bernie
31 *#* Implement prop fonts; Fix algo styles.
33 *#* Revision 1.3 2006/02/10 12:31:55 bernie
34 *#* Add multiple font support in bitmaps.
36 *#* Revision 1.2 2005/11/04 18:17:45 bernie
37 *#* Fix header guards and includes for new location of gfx module.
39 *#* Revision 1.1 2005/11/04 18:11:35 bernie
40 *#* Move graphics stuff from mware/ to gfx/.
42 *#* Revision 1.13 2005/11/04 16:20:02 bernie
43 *#* Fix reference to README.devlib in header.
45 *#* Revision 1.12 2005/04/11 19:10:28 bernie
46 *#* Include top-level headers from cfg/ subdir.
48 *#* Revision 1.11 2005/01/20 18:46:31 aleph
49 *#* Fix progmem includes.
51 *#* Revision 1.10 2005/01/08 09:20:12 bernie
52 *#* Really make it work on both architectures.
54 *#* Revision 1.9 2004/12/31 16:44:29 bernie
55 *#* Sanitize for non-Harvard processors.
57 *#* Revision 1.8 2004/11/16 21:16:28 bernie
58 *#* Update to new naming scheme in mware/gfx.c.
60 *#* Revision 1.7 2004/09/20 03:28:28 bernie
63 *#* Revision 1.6 2004/09/14 20:57:15 bernie
64 *#* Use debug.h instead of kdebug.h.
66 *#* Revision 1.5 2004/09/06 21:51:26 bernie
67 *#* Extend interface to allow any algorithmic style.
69 *#* Revision 1.2 2004/06/03 11:27:09 bernie
70 *#* Add dual-license information.
72 *#* Revision 1.1 2004/05/23 15:43:16 bernie
73 *#* Import mware modules.
75 *#* Revision 1.17 2004/05/15 16:57:01 aleph
76 *#* Fixes for non-DEBUG build
78 *#* Revision 1.16 2004/04/03 20:42:49 aleph
81 *#* Revision 1.15 2004/03/24 15:03:45 bernie
82 *#* Use explicit include paths; clean Doxygen comments
84 *#* Revision 1.14 2004/03/19 16:52:28 bernie
85 *#* Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
87 *#* Revision 1.13 2004/03/17 18:23:32 bernie
90 *#* Revision 1.12 2004/03/17 18:03:22 bernie
91 *#* Make diagnostic message shorter
93 *#* Revision 1.11 2004/03/13 22:52:54 aleph
94 *#* documentation fixes
100 #include <gfx/text.h>
102 #include <gfx/gfx_p.h> // FIXME: BM_DRAWPIXEL
104 #include <cfg/debug.h>
107 /*! ANSI escape sequences flag: true for ESC state on */
108 static bool ansi_mode = false;
112 * Move (imaginary) cursor to column and row specified.
113 * Next text write will start a that row and col.
115 void text_moveto(struct Bitmap *bm, int row, int col)
118 ASSERT(col < bm->width / bm->font->width);
120 ASSERT(row < bm->height / bm->font->height);
122 bm->penX = col * bm->font->width;
123 bm->penY = row * bm->font->height;
128 * Move (imaginary) cursor to coordinates specified.
130 void text_setcoord(struct Bitmap *bm, int x, int y)
138 * Render char \a c on Bitmap \a bm.
140 static int text_putglyph(char c, struct Bitmap *bm)
142 const uint8_t * PROGMEM glyph; /* font is in progmem */
143 uint8_t glyph_width, glyph_height, glyph_height_bytes;
144 unsigned char index = (unsigned char)c;
146 /* Check for out of range char and replace with '?' or first char in font. */
147 if (UNLIKELY(!FONT_HAS_GLYPH(bm->font, index)))
149 kprintf("Illegal char '%c' (0x%02x)\n", index, index);
150 if (FONT_HAS_GLYPH(bm->font, '?'))
153 index = bm->font->first;
156 /* Make character relative to font start */
157 index -= bm->font->first;
159 glyph_height = bm->font->height;
160 // FIXME: for vertical fonts only
161 glyph_height_bytes = ROUND_UP2(glyph_height, 8);
163 if (bm->font->offset)
165 /* Proportional font */
166 glyph_width = bm->font->widths[index]; /* TODO: optimize away */
167 glyph = bm->font->glyph + bm->font->offset[index];
172 * Fixed-width font: compute the first column of pixels
173 * of the selected glyph using the character code to index
176 glyph_width = bm->font->width;
178 //For horizontal fonts
179 //glyph = bm->font->glyph + index * (((glyph_width + 7) / 8) * glyph_height);
180 glyph = bm->font->glyph + index * glyph_height_bytes * glyph_width;
183 /* Slow path for styled glyphs */
184 if (UNLIKELY(bm->styles))
186 uint8_t styles = bm->styles;
187 uint8_t prev_dots = 0, italic_prev_dots = 0;
191 /* This style alone could be handled by the fast path too */
192 if (bm->styles & STYLEF_CONDENSED)
195 if (bm->styles & STYLEF_EXPANDED)
198 /* Check if glyph fits in the bitmap. */
199 if ((bm->penX < 0) || (bm->penX + glyph_width > bm->width)
200 || (bm->penY < 0) || (bm->penY + glyph_height > bm->height))
202 kprintf("bad coords x=%d y=%d\n", bm->penX, bm->penY);
206 for (row = 0; row < glyph_height_bytes; ++row)
208 /* For each dot column in the glyph... */
209 for (col = 0; col < glyph_width; ++col)
211 uint8_t src_col = col;
214 /* Expanded style: advances only once every two columns. */
215 if (styles & STYLEF_EXPANDED)
218 /* Fetch a column of dots from glyph. */
219 dots = PGM_READ_CHAR(RAST_ADDR(glyph, src_col, row * 8, glyph_width));
221 /* Italic: get lower 4 dots from previous column */
222 if (styles & STYLEF_ITALIC)
224 uint8_t new_dots = dots;
225 dots = (dots & 0xF0) | italic_prev_dots;
226 italic_prev_dots = new_dots & 0x0F;
229 /* Bold: "or" pixels with the previous column */
230 if (styles & STYLEF_BOLD)
232 uint8_t new_dots = dots;
234 prev_dots = new_dots;
237 /* Underlined: turn on base pixel */
238 if ((styles & STYLEF_UNDERLINE)
239 && (row == glyph_height_bytes - 1))
240 dots |= (1 << (glyph_height - row * 8 - 1));
242 /* Inverted: invert pixels */
243 if (styles & STYLEF_INVERT)
247 for (i = 0; i < 8 && (row * 8) + i < glyph_height; ++i)
248 BM_DRAWPIXEL(bm, bm->penX + col, bm->penY + row * 8 + i, dots & (1<<i));
254 /* No style: fast vanilla copy of glyph to bitmap */
255 gfx_blitRaster(bm, bm->penX, bm->penY, glyph, glyph_width, glyph_height, glyph_width);
258 /* Update current pen position */
259 bm->penX += glyph_width;
266 * Render char \c c, with (currently) limited ANSI escapes
267 * emulation support and '\n' for newline.
269 int text_putchar(char c, struct Bitmap *bm)
271 /* Handle ANSI escape sequences */
272 if (UNLIKELY(ansi_mode))
276 case ANSI_ESC_CLEARSCREEN:
280 text_style(bm, 0, STYLEF_MASK);
283 kprintf("Unknown ANSI esc code: %x\n", c);)
287 else if (c == '\033') /* Enter ANSI ESC mode */
291 else if (c == '\n') /* Go one line down on a line-feed */
293 if (bm->penY + bm->font->height < bm->height)
295 bm->penY += bm->font->height;
301 text_putglyph(c, bm);
308 * Clear the screen and reset cursor position
310 void text_clear(struct Bitmap *bmp)
312 text_putchar('\x1b', bmp);
313 text_putchar('c', bmp);
317 void text_clearLine(struct Bitmap *bm, int line)
319 gfx_rectClear(bm, 0, line * bm->font->height, bm->width, (line + 1) * bm->font->height);
324 * Set/clear algorithmic font style bits.
326 * \param bm Pointer to Bitmap to affect.
327 * \param flags Style flags to set
328 * \param mask Mask of flags to modify
329 * \return Old style flags
332 * Turn on bold, leave other styles alone
333 * \code text_style(bm, STYLEF_BOLD, STYLEF_BOLD); \endcode
335 * Turn off bold and turn on italic, leave others as they are
336 * \code text_style(bm, STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
338 * Query current style without chaning it
339 * \code style = text_style(bm, 0, 0); \endcode
341 * Reset all styles (plain text)
342 * \code text_style(bm, 0, STYLE_MASK); \endcode
344 uint8_t text_style(struct Bitmap *bm, uint8_t flags, uint8_t mask)
346 uint8_t old = bm->styles;
347 bm->styles = (bm->styles & ~mask) | flags;