4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See devlib/README for information.
9 * \brief Text graphic routines
12 * \author Bernardo Innocenti <bernie@develer.com>
13 * \author Stefano Fedrigo <aleph@develer.com>
18 *#* Revision 1.12 2005/04/11 19:10:28 bernie
19 *#* Include top-level headers from cfg/ subdir.
21 *#* Revision 1.11 2005/01/20 18:46:31 aleph
22 *#* Fix progmem includes.
24 *#* Revision 1.10 2005/01/08 09:20:12 bernie
25 *#* Really make it work on both architectures.
27 *#* Revision 1.9 2004/12/31 16:44:29 bernie
28 *#* Sanitize for non-Harvard processors.
30 *#* Revision 1.8 2004/11/16 21:16:28 bernie
31 *#* Update to new naming scheme in mware/gfx.c.
33 *#* Revision 1.7 2004/09/20 03:28:28 bernie
36 *#* Revision 1.6 2004/09/14 20:57:15 bernie
37 *#* Use debug.h instead of kdebug.h.
39 *#* Revision 1.5 2004/09/06 21:51:26 bernie
40 *#* Extend interface to allow any algorithmic style.
42 *#* Revision 1.2 2004/06/03 11:27:09 bernie
43 *#* Add dual-license information.
45 *#* Revision 1.1 2004/05/23 15:43:16 bernie
46 *#* Import mware modules.
48 *#* Revision 1.17 2004/05/15 16:57:01 aleph
49 *#* Fixes for non-DEBUG build
51 *#* Revision 1.16 2004/04/03 20:42:49 aleph
54 *#* Revision 1.15 2004/03/24 15:03:45 bernie
55 *#* Use explicit include paths; clean Doxygen comments
57 *#* Revision 1.14 2004/03/19 16:52:28 bernie
58 *#* Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
60 *#* Revision 1.13 2004/03/17 18:23:32 bernie
63 *#* Revision 1.12 2004/03/17 18:03:22 bernie
64 *#* Make diagnostic message shorter
66 *#* Revision 1.11 2004/03/13 22:52:54 aleph
67 *#* documentation fixes
74 #include <cfg/debug.h>
78 * Flags degli stili algoritmici
80 * La routine di rendering del testo e' in grado di applicare
81 * delle semplici trasformazioni al font interno per generare
82 * automaticamente degli stili predefiniti (bold, italic,
83 * underline) a partire dal set di caratteri plain.
85 static uint8_t text_styles;
87 /*! ANSI escape sequences flag: true for ESC state on */
88 static bool ansi_mode = false;
92 * Move (imaginary) cursor to column and row specified.
93 * Next text write will start a that row and col.
95 void text_moveto(struct Bitmap *bm, int row, int col)
98 ASSERT(col < bm->width / FONT_WIDTH);
100 ASSERT(row < bm->height / FONT_HEIGHT);
102 bm->penX = col * FONT_WIDTH;
103 bm->penY = row * FONT_HEIGHT;
108 * Move (imaginary) cursor to coordinates specified.
110 void text_setcoord(struct Bitmap *bm, int x, int y)
118 * Render char \a c on Bitmap \a bm
120 static int text_putglyph(char c, struct Bitmap *bm)
122 const uint8_t * PROGMEM glyph; /* font is in progmem */
128 * Il carattere da stampare viene usato come indice per prelevare
129 * la prima colonna di dots del glyph all'interno del font.
131 glyph = font + (((unsigned char)c) * FONT_WIDTH);
132 glyph_width = FONT_WIDTH;
134 if (text_styles & STYLEF_CONDENSED)
137 if (text_styles & STYLEF_EXPANDED)
140 /* The y coord is rounded at multiples of 8 for simplicity */
141 bm->penY &= ~((coord_t)7);
143 /* Check if glyph to write fits in the bitmap */
144 if ((bm->penX < 0) || (bm->penX + glyph_width > bm->width) ||
145 (bm->penY < 0) || (bm->penY + FONT_HEIGHT > bm->height))
147 DB(kprintf("bad coords x=%d y=%d\n", bm->penX, bm->penY);)
151 /* Locate position where to write in the raster */
152 buf = bm->raster + bm->penY / 8 * bm->width + bm->penX;
154 bm->penX += glyph_width;
156 /* If some styles are set */
159 uint8_t prev_dots = 0, italic_prev_dots = 0, new_dots;
162 /* Per ogni colonna di dot del glyph... */
163 for (i = 0; i < glyph_width; ++i)
165 dots = PGM_READ_CHAR(glyph);
167 /* Advance to next column in glyph.
168 * Expand: advances only once every two columns
170 if (!(text_styles & STYLEF_EXPANDED) || (i & 1))
173 /* Italic: get lower 4 dots from previous column */
174 if (text_styles & STYLEF_ITALIC)
177 dots = (dots & 0xF0) | italic_prev_dots;
178 italic_prev_dots = new_dots & 0x0F;
181 /* Bold: "or" pixels with the previous column */
182 if (text_styles & STYLEF_BOLD)
186 prev_dots = new_dots;
189 /* Underlined: turn on base pixel */
190 if (text_styles & STYLEF_UNDERLINE)
193 /* Inverted: invert pixels */
194 if (text_styles & STYLEF_INVERT)
201 else /* No style: fast vanilla copy of glyph to line buffer */
202 while (glyph_width--)
203 *buf++ = PGM_READ_CHAR(glyph++);
210 * Render char \c c, with (currently) limited ANSI escapes
211 * emulation support and '\n' for newline.
213 int text_putchar(char c, struct Bitmap *bm)
215 /* Handle ANSI escape sequences */
220 case ANSI_ESC_CLEARSCREEN:
224 text_style(0, STYLEF_MASK);
227 kprintf("Unknown ANSI esc code: %x\n", c);)
231 else if (c == '\033') /* Enter ANSI ESC mode */
235 else if (c == '\n') /* Go one line down on a line-feed */
237 if (bm->penY + FONT_HEIGHT < bm->height)
239 bm->penY += FONT_HEIGHT;
245 text_putglyph(c, bm);
252 * Clear the screen and reset cursor position
254 void text_clear(struct Bitmap *bmp)
256 text_putchar('\x1b', bmp);
257 text_putchar('c', bmp);
261 void text_clearLine(struct Bitmap *bmp, int line)
263 gfx_rectClear(bmp, 0, line * FONT_HEIGHT, bmp->width, (line + 1) * FONT_HEIGHT);
268 * Set/clear algorithmic font style bits.
270 * \param flags Style flags to set
271 * \param mask Mask of flags to modify
272 * \return Old style flags
275 * Turn on bold, leave other styles alone
276 * \code prt_style(STYLEF_BOLD, STYLEF_BOLD); \endcode
278 * Turn off bold and turn on italic, leave others as they are
279 * \code prt_style(STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
281 * Query current style without chaning it
282 * \code style = prt_style(0, 0); \endcode
284 * Reset all styles (plain text)
285 * \code prt_style(0, STYLE_MASK); \endcode
287 uint8_t text_style(uint8_t flags, uint8_t mask)
289 uint8_t old = text_styles;
290 text_styles = (text_styles & ~mask) | flags;