Make more generic and adapt to new gfx functions.
[bertos.git] / mware / text_format.c
1 /*!
2  * \file
3  * <!--
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.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  * \author Stefano Fedrigo <aleph@develer.com>
13  *
14  * \brief printf-family routines for text output
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.5  2004/08/25 14:12:09  rasky
20  *#* Aggiornato il comment block dei log RCS
21  *#*
22  *#* Revision 1.4  2004/08/05 18:46:44  bernie
23  *#* Documentation improvements.
24  *#*
25  *#* Revision 1.3  2004/08/03 15:57:18  aleph
26  *#* Add include to fix warning for vsprintf()
27  *#*
28  *#* Revision 1.2  2004/06/03 11:27:09  bernie
29  *#* Add dual-license information.
30  *#*
31  *#* Revision 1.1  2004/05/23 15:43:16  bernie
32  *#* Import mware modules.
33  *#*
34  *#* Revision 1.2  2004/03/26 18:50:50  bernie
35  *#* Move _PROGMEM stuff to compiler.h
36  *#*
37  *#* Revision 1.1  2004/03/19 16:52:28  bernie
38  *#* Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
39  *#*
40  *#*/
41
42 #include "text.h"
43 #include "formatwr.h" /* _formatted_write() */
44 #include "font.h"
45 #include "gfx.h"
46 #include <stdio.h> /* vsprintf() */
47 #include <stdarg.h>
48 #include <string.h> /* strlen() */
49
50 /*!
51  * Render string \a str in Bitmap \a bm at current cursor position
52  *
53  * \note Text formatting functions are also available with an _P suffix
54  *       accepting the source string from program memory.  This feature
55  *       is only available (and useful) on Harvard microprocessors such
56  *       as the AVR.
57  *
58  * \see text_putchar()
59  */
60 int PGM_FUNC(text_puts)(const char * PGM_ATTR str, struct Bitmap *bm)
61 {
62         char c;
63
64         while ((c = PGM_READ_CHAR(str++)))
65                 text_putchar(c, bm);
66
67         return 0;
68 }
69
70
71 /*!
72  * vprintf()-like formatter to render text in a Bitmap.
73  *
74  * Perform vprintf()-like formatting on the \a fmt format string using the
75  * variable-argument list \a ap.
76  * Render the resulting string in Bitmap \a bm starting at the current
77  * cursor position.
78  *
79  * \see text_puts() text_putchar() text_printf()
80  */
81 int PGM_FUNC(text_vprintf)(struct Bitmap *bm, const char * PGM_ATTR fmt, va_list ap)
82 {
83         return PGM_FUNC(_formatted_write)(fmt, (void (*)(char, void *))text_putchar, bm, ap);
84 }
85
86 /*!
87  * printf()-like formatter to render text in a Bitmap.
88  *
89  * Perform printf()-like formatting on the \a fmt format string.
90  * Render the resulting string in Bitmap \a bm starting at the
91  * current cursor position.
92  *
93  * \see text_puts() text_putchar() text_vprintf()
94  */
95 int PGM_FUNC(text_printf)(struct Bitmap *bm, const char * PGM_ATTR fmt, ...)
96 {
97         int len;
98
99         va_list ap;
100         va_start(ap, fmt);
101         len = PGM_FUNC(text_vprintf)(bm, fmt, ap);
102         va_end(ap);
103
104         return len;
105 }
106
107
108 /*!
109  * Render the result of printf()-like formatting in a specified position
110  * of a Bitmap.
111  *
112  * \param bm Bitmap where to render the text
113  * \param row  Starting row in character units (zero based)
114  * \param col  Starting column in character units (zero based)
115  * \param mode Formatting mode to use.  Can be TEXT_NORMAL,
116  *        TEXT_FILL, TEXT_INVERT or TEXT_RIGHT or a combination
117  *        of these flags ORed together.
118  * \param fmt  String possibly containing printf() formatting commands.
119  *
120  * \see text_puts() text_putchar() text_printf() text_vprintf()
121  * \see text_moveto() text_style()
122  */
123 int PGM_FUNC(text_xprintf)(struct Bitmap *bm,
124                 uint8_t row, uint8_t col, uint8_t mode, const char * PGM_ATTR fmt, ...)
125 {
126         int len;
127         uint8_t oldstyle = 0;
128         va_list ap;
129
130         va_start(ap, fmt);
131
132         text_moveto(bm, row, col);
133
134         if (mode & TEXT_INVERT)
135                 oldstyle = text_style(STYLEF_INVERT, STYLEF_INVERT);
136
137         if (mode & (TEXT_CENTER | TEXT_RIGHT))
138         {
139                 uint8_t pad;
140
141                 pad = bm->width/FONT_WIDTH - PGM_FUNC(vsprintf)(NULL, fmt, ap);
142
143                 if (mode & TEXT_CENTER)
144                         pad /= 2;
145
146                 while (pad--)
147                         text_putchar(' ', bm);
148         }
149
150         len = PGM_FUNC(text_vprintf)(bm, fmt, ap);
151         va_end(ap);
152
153         if (mode & (TEXT_FILL | TEXT_CENTER))
154                 while (bm->penX + FONT_WIDTH < bm->width)
155                         text_putchar(' ', bm);
156
157         if (mode & TEXT_INVERT)
158                 text_style(oldstyle, STYLEF_MASK);
159
160         return len;
161 }
162