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.4 2006/02/15 09:10:15 bernie
19 *#* Implement prop fonts; Fix algo styles.
21 *#* Revision 1.3 2006/02/10 12:31:55 bernie
22 *#* Add multiple font support in bitmaps.
24 *#* Revision 1.2 2005/11/04 18:17:45 bernie
25 *#* Fix header guards and includes for new location of gfx module.
27 *#* Revision 1.1 2005/11/04 18:11:35 bernie
28 *#* Move graphics stuff from mware/ to gfx/.
30 *#* Revision 1.13 2005/11/04 16:20:02 bernie
31 *#* Fix reference to README.devlib in header.
33 *#* Revision 1.12 2005/04/11 19:10:28 bernie
34 *#* Include top-level headers from cfg/ subdir.
36 *#* Revision 1.11 2005/01/20 18:46:31 aleph
37 *#* Fix progmem includes.
39 *#* Revision 1.10 2005/01/08 09:20:12 bernie
40 *#* Really make it work on both architectures.
42 *#* Revision 1.9 2004/12/31 16:44:29 bernie
43 *#* Sanitize for non-Harvard processors.
45 *#* Revision 1.8 2004/11/16 21:16:28 bernie
46 *#* Update to new naming scheme in mware/gfx.c.
48 *#* Revision 1.7 2004/09/20 03:28:28 bernie
51 *#* Revision 1.6 2004/09/14 20:57:15 bernie
52 *#* Use debug.h instead of kdebug.h.
54 *#* Revision 1.5 2004/09/06 21:51:26 bernie
55 *#* Extend interface to allow any algorithmic style.
57 *#* Revision 1.2 2004/06/03 11:27:09 bernie
58 *#* Add dual-license information.
60 *#* Revision 1.1 2004/05/23 15:43:16 bernie
61 *#* Import mware modules.
63 *#* Revision 1.17 2004/05/15 16:57:01 aleph
64 *#* Fixes for non-DEBUG build
66 *#* Revision 1.16 2004/04/03 20:42:49 aleph
69 *#* Revision 1.15 2004/03/24 15:03:45 bernie
70 *#* Use explicit include paths; clean Doxygen comments
72 *#* Revision 1.14 2004/03/19 16:52:28 bernie
73 *#* Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
75 *#* Revision 1.13 2004/03/17 18:23:32 bernie
78 *#* Revision 1.12 2004/03/17 18:03:22 bernie
79 *#* Make diagnostic message shorter
81 *#* Revision 1.11 2004/03/13 22:52:54 aleph
82 *#* documentation fixes
90 #include <gfx/gfx_p.h> // FIXME: BM_DRAWPIXEL
92 #include <cfg/debug.h>
96 * Flags degli stili algoritmici
98 * La routine di rendering del testo e' in grado di applicare
99 * delle semplici trasformazioni al font interno per generare
100 * automaticamente degli stili predefiniti (bold, italic,
101 * underline) a partire dal set di caratteri plain.
103 static uint8_t text_styles;
105 /*! ANSI escape sequences flag: true for ESC state on */
106 static bool ansi_mode = false;
110 * Move (imaginary) cursor to column and row specified.
111 * Next text write will start a that row and col.
113 void text_moveto(struct Bitmap *bm, int row, int col)
116 ASSERT(col < bm->width / bm->font->width);
118 ASSERT(row < bm->height / bm->font->height);
120 bm->penX = col * bm->font->width;
121 bm->penY = row * bm->font->height;
126 * Move (imaginary) cursor to coordinates specified.
128 void text_setcoord(struct Bitmap *bm, int x, int y)
136 * Render char \a c on Bitmap \a bm.
138 static int text_putglyph(char c, struct Bitmap *bm)
140 const uint8_t * PROGMEM glyph; /* font is in progmem */
141 uint8_t glyph_width, glyph_height, glyph_height_bytes;
142 unsigned char index = (unsigned char)c;
144 /* Check for out of range char and replace with '?' or first char in font. */
145 if (index < bm->font->first || index > bm->font->last)
147 kprintf("Illegal char '%c' (0x%02x)\n", index, index);
148 if ('?' >= bm->font->first && '?' <= bm->font->last)
151 index = bm->font->first;
154 /* Make character relative to font start */
155 index -= bm->font->first;
157 glyph_height = bm->font->height;
158 // FIXME: for vertical fonts only
159 glyph_height_bytes = (glyph_height + 7) / 8;
161 if (bm->font->offset)
163 /* Proportional font */
164 glyph_width = bm->font->widths[index]; /* TODO: optimize away */
165 glyph = bm->font->glyph + bm->font->offset[index];
170 * Fixed-width font: compute the first column of pixels
171 * of the selected glyph using the character code to index
174 glyph_width = bm->font->width;
176 //For horizontal fonts
177 //glyph = bm->font->glyph + index * (((glyph_width + 7) / 8) * glyph_height);
178 glyph = bm->font->glyph + index * glyph_height_bytes * glyph_width;
181 if (text_styles & STYLEF_CONDENSED)
184 if (text_styles & STYLEF_EXPANDED)
187 /* Slow path for styled glyphs */
190 uint8_t prev_dots = 0, italic_prev_dots = 0;
194 /* Check if glyph fits in the bitmap. */
195 if ((bm->penX < 0) || (bm->penX + glyph_width > bm->width)
196 || (bm->penY < 0) || (bm->penY + glyph_height > bm->height))
198 kprintf("bad coords x=%d y=%d\n", bm->penX, bm->penY);
202 for (row = 0; row < glyph_height_bytes; ++row)
204 /* For each dot column in the glyph... */
205 for (col = 0; col < glyph_width; ++col)
207 uint8_t src_col = col;
210 /* Expanded style: advances only once every two columns. */
211 if (text_styles & STYLEF_EXPANDED)
214 /* Fetch a column of dots from glyph. */
215 dots = PGM_READ_CHAR(glyph + src_col * glyph_height_bytes + row);
217 /* Italic: get lower 4 dots from previous column */
218 if (text_styles & STYLEF_ITALIC)
220 uint8_t new_dots = dots;
221 dots = (dots & 0xF0) | italic_prev_dots;
222 italic_prev_dots = new_dots & 0x0F;
225 /* Bold: "or" pixels with the previous column */
226 if (text_styles & STYLEF_BOLD)
228 uint8_t new_dots = dots;
230 prev_dots = new_dots;
233 /* Underlined: turn on base pixel */
234 if (text_styles & STYLEF_UNDERLINE)
237 /* Inverted: invert pixels */
238 if (text_styles & STYLEF_INVERT)
242 for (i = 0; i < 8 && (row * 8) + i < glyph_height; ++i)
243 BM_DRAWPIXEL(bm, bm->penX + col, bm->penY + row * 8 + i, dots & (1<<i));
249 /* No style: fast vanilla copy of glyph to bitmap */
250 gfx_blitRaster(bm, bm->penX, bm->penY, glyph, glyph_width, glyph_height, glyph_height_bytes);
253 /* Update current pen position */
254 bm->penX += glyph_width;
261 * Render char \c c, with (currently) limited ANSI escapes
262 * emulation support and '\n' for newline.
264 int text_putchar(char c, struct Bitmap *bm)
266 /* Handle ANSI escape sequences */
271 case ANSI_ESC_CLEARSCREEN:
275 text_style(0, STYLEF_MASK);
278 kprintf("Unknown ANSI esc code: %x\n", c);)
282 else if (c == '\033') /* Enter ANSI ESC mode */
286 else if (c == '\n') /* Go one line down on a line-feed */
288 if (bm->penY + bm->font->height < bm->height)
290 bm->penY += bm->font->height;
296 text_putglyph(c, bm);
303 * Clear the screen and reset cursor position
305 void text_clear(struct Bitmap *bmp)
307 text_putchar('\x1b', bmp);
308 text_putchar('c', bmp);
312 void text_clearLine(struct Bitmap *bm, int line)
314 gfx_rectClear(bm, 0, line * bm->font->height, bm->width, (line + 1) * bm->font->height);
319 * Set/clear algorithmic font style bits.
321 * \param flags Style flags to set
322 * \param mask Mask of flags to modify
323 * \return Old style flags
326 * Turn on bold, leave other styles alone
327 * \code prt_style(STYLEF_BOLD, STYLEF_BOLD); \endcode
329 * Turn off bold and turn on italic, leave others as they are
330 * \code prt_style(STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
332 * Query current style without chaning it
333 * \code style = prt_style(0, 0); \endcode
335 * Reset all styles (plain text)
336 * \code prt_style(0, STYLE_MASK); \endcode
338 uint8_t text_style(uint8_t flags, uint8_t mask)
340 uint8_t old = text_styles;
341 text_styles = (text_styles & ~mask) | flags;