4 * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
5 * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
6 * This file is part of DevLib - See devlib/README for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
12 * \author Stefano Fedrigo <aleph@develer.com>
14 * \brief Text graphic routines
19 *#* Revision 1.3 2004/08/25 14:12:09 rasky
20 *#* Aggiornato il comment block dei log RCS
22 *#* Revision 1.2 2004/06/03 11:27:09 bernie
23 *#* Add dual-license information.
25 *#* Revision 1.1 2004/05/23 15:43:16 bernie
26 *#* Import mware modules.
28 *#* Revision 1.17 2004/05/15 16:57:01 aleph
29 *#* Fixes for non-DEBUG build
31 *#* Revision 1.16 2004/04/03 20:42:49 aleph
34 *#* Revision 1.15 2004/03/24 15:03:45 bernie
35 *#* Use explicit include paths; clean Doxygen comments
37 *#* Revision 1.14 2004/03/19 16:52:28 bernie
38 *#* Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
40 *#* Revision 1.13 2004/03/17 18:23:32 bernie
43 *#* Revision 1.12 2004/03/17 18:03:22 bernie
44 *#* Make diagnostic message shorter
46 *#* Revision 1.11 2004/03/13 22:52:54 aleph
47 *#* documentation fixes
53 #include <drv/kdebug.h>
56 * Flags degli stili algoritmici
57 * La routine di rendering del testo e' in grado di applicare
58 * delle semplici trasformazioni al font interno per generare
59 * automaticamente degli stili predefiniti (bold, italic,
60 * underline) a partire dal set di caratteri plain.
62 static uint8_t text_styles;
64 /*! ANSI escape sequences flag: true for ESC state on */
65 static bool ansi_mode = false;
69 * Move (imaginary) cursor to column and row specified.
70 * Next text write will start a that row and col.
72 void text_moveto(struct Bitmap *bm, int row, int col)
75 ASSERT(col < bm->width / FONT_WIDTH);
77 ASSERT(row < bm->height / FONT_HEIGHT);
79 bm->penX = col * FONT_WIDTH;
80 bm->penY = row * FONT_HEIGHT;
85 * Move (imaginary) cursor to coordinates specified.
87 void text_setcoord(struct Bitmap *bm, int x, int y)
95 * Render char <code>c</code>
97 static int text_putglyph(char c, struct Bitmap *bm)
99 const uint8_t * PROGMEM glyph; /* font is in progmem */
105 * Il carattere da stampare viene usato come indice per prelevare
106 * la prima colonna di dots del glyph all'interno del font.
108 glyph = font + (((unsigned char)c) * FONT_WIDTH);
109 glyph_width = FONT_WIDTH;
111 if (text_styles & STYLEF_CONDENSED)
114 if (text_styles & STYLEF_EXPANDED)
117 /* The y coord is rounded at multiples of 8 for simplicity */
118 bm->penY &= ~((coord_t)7);
120 /* Check if glyph to write fits in the bitmap */
121 if ((bm->penX < 0) || (bm->penX + glyph_width > bm->width) ||
122 (bm->penY < 0) || (bm->penY + FONT_HEIGHT > bm->height))
124 DB(kprintf("bad coords x=%d y=%d\n", bm->penX, bm->penY);)
128 /* Locate position where to write in the raster */
129 buf = bm->raster + bm->penY / 8 * bm->width + bm->penX;
131 bm->penX += glyph_width;
133 /* If some styles are set */
136 uint8_t prev_dots = 0, italic_prev_dots = 0, new_dots;
139 /* Per ogni colonna di dot del glyph... */
140 for (i = 0; i < glyph_width; ++i)
142 dots = pgm_read_byte(glyph);
144 /* Advance to next column in glyph.
145 * Expand: advances only once every two columns
147 if (!(text_styles & STYLEF_EXPANDED) || (i & 1))
150 /* Italic: get lower 4 dots from previous column */
151 if (text_styles & STYLEF_ITALIC)
154 dots = (dots & 0xF0) | italic_prev_dots;
155 italic_prev_dots = new_dots & 0x0F;
158 /* Bold: "or" pixels with the previous column */
159 if (text_styles & STYLEF_BOLD)
163 prev_dots = new_dots;
166 /* Underlined: turn on base pixel */
167 if (text_styles & STYLEF_UNDERLINE)
170 /* Inverted: invert pixels */
171 if (text_styles & STYLEF_INVERT)
178 else /* No style: fast vanilla copy of glyph to line buffer */
179 while (glyph_width--)
180 *buf++ = pgm_read_byte(glyph++);
187 * Render char \c c, with (currently) limited ANSI escapes
188 * emulation support and '\n' for newline.
190 int text_putchar(char c, struct Bitmap *bm)
192 /* Handle ANSI escape sequences */
197 case ANSI_ESC_CLEARSCREEN:
201 text_style(0, STYLEF_MASK);
204 kprintf("Unknown ANSI esc code: %x\n", c);)
208 else if (c == '\033') /* Enter ANSI ESC mode */
212 else if (c == '\n') /* Go one line down on a line-feed */
214 if (bm->penY + FONT_HEIGHT < bm->height)
216 bm->penY += FONT_HEIGHT;
222 text_putglyph(c, bm);
229 * Clear the screen and reset cursor position
231 void text_clear(struct Bitmap *bmp)
233 text_putchar('\x1b', bmp);
234 text_putchar('c', bmp);
239 * Set/clear algorithmic font style bits.
241 * \param flags Style flags to set
242 * \param mask Mask of flags to modify
243 * \return Old style flags
246 * Turn on bold, leave other styles alone
247 * \code prt_style(STYLEF_BOLD, STYLEF_BOLD); \endcode
249 * Turn off bold and turn on italic, leave others as they are
250 * \code prt_style(STYLEF_ITALIC, STYLEF_BOLD | STYLEF_ITALIC); \endcode
252 * Query current style without chaning it
253 * \code style = prt_style(0, 0); \endcode
255 * Reset all styles (plain text)
256 * \code prt_style(0, STYLE_MASK); \endcode
258 uint8_t text_style(uint8_t flags, uint8_t mask)
260 uint8_t old = text_styles;
261 text_styles = (text_styles & ~mask) | flags;