Move cfg/config.h to appconfig.h.
[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 README.devlib for information.
7  * -->
8  *
9  * \brief printf-family routines for text output
10  *
11  * \version $Id$
12  * \author Bernardo Innocenti <bernie@develer.com>
13  * \author Stefano Fedrigo <aleph@develer.com>
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.10  2005/11/04 16:20:02  bernie
19  *#* Fix reference to README.devlib in header.
20  *#*
21  *#* Revision 1.9  2004/12/31 17:47:45  bernie
22  *#* Rename UNUSED() to UNUSED_ARG().
23  *#*
24  *#* Revision 1.8  2004/11/16 21:16:56  bernie
25  *#* Update to new naming scheme in mware/gfx.c.
26  *#*
27  *#* Revision 1.7  2004/10/03 19:05:04  bernie
28  *#* text_widthf(), text_vwidthf(): New functions.
29  *#*
30  *#* Revision 1.6  2004/09/14 20:59:04  bernie
31  *#* text_xprintf(): Support all styles; Pixel-wise text centering.
32  *#*
33  *#* Revision 1.5  2004/08/25 14:12:09  rasky
34  *#* Aggiornato il comment block dei log RCS
35  *#*
36  *#* Revision 1.4  2004/08/05 18:46:44  bernie
37  *#* Documentation improvements.
38  *#*
39  *#* Revision 1.3  2004/08/03 15:57:18  aleph
40  *#* Add include to fix warning for vsprintf()
41  *#*
42  *#* Revision 1.2  2004/06/03 11:27:09  bernie
43  *#* Add dual-license information.
44  *#*
45  *#* Revision 1.1  2004/05/23 15:43:16  bernie
46  *#* Import mware modules.
47  *#*
48  *#* Revision 1.2  2004/03/26 18:50:50  bernie
49  *#* Move _PROGMEM stuff to compiler.h
50  *#*
51  *#* Revision 1.1  2004/03/19 16:52:28  bernie
52  *#* Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
53  *#*
54  *#*/
55
56 #include "text.h"
57 #include "formatwr.h" /* _formatted_write() */
58 #include "font.h"
59 #include "gfx.h"
60 #include <stdio.h> /* vsprintf() */
61 #include <stdarg.h>
62 #include <string.h> /* strlen() */
63
64 /*!
65  * Render string \a str in Bitmap \a bm at current cursor position
66  *
67  * \note Text formatting functions are also available with an _P suffix
68  *       accepting the source string from program memory.  This feature
69  *       is only available (and useful) on Harvard microprocessors such
70  *       as the AVR.
71  *
72  * \see text_putchar()
73  */
74 int PGM_FUNC(text_puts)(const char * PGM_ATTR str, struct Bitmap *bm)
75 {
76         char c;
77
78         while ((c = PGM_READ_CHAR(str++)))
79                 text_putchar(c, bm);
80
81         return 0;
82 }
83
84
85 /*!
86  * vprintf()-like formatter to render text in a Bitmap.
87  *
88  * Perform vprintf()-like formatting on the \a fmt format string using the
89  * variable-argument list \a ap.
90  * Render the resulting string in Bitmap \a bm starting at the current
91  * cursor position.
92  *
93  * \see text_puts() text_putchar() text_printf()
94  */
95 int PGM_FUNC(text_vprintf)(struct Bitmap *bm, const char * PGM_ATTR fmt, va_list ap)
96 {
97         return PGM_FUNC(_formatted_write)(fmt, (void (*)(char, void *))text_putchar, bm, ap);
98 }
99
100 /*!
101  * printf()-like formatter to render text in a Bitmap.
102  *
103  * Perform printf()-like formatting on the \a fmt format string.
104  * Render the resulting string in Bitmap \a bm starting at the
105  * current cursor position.
106  *
107  * \see text_puts() text_putchar() text_vprintf()
108  */
109 int PGM_FUNC(text_printf)(struct Bitmap *bm, const char * PGM_ATTR fmt, ...)
110 {
111         int len;
112
113         va_list ap;
114         va_start(ap, fmt);
115         len = PGM_FUNC(text_vprintf)(bm, fmt, ap);
116         va_end(ap);
117
118         return len;
119 }
120
121
122 /*!
123  * Render the result of printf()-like formatting in a specified position
124  * of a Bitmap.
125  *
126  * \param bm Bitmap where to render the text
127  * \param row   Starting row in character units (zero based)
128  * \param col   Starting column in character units (zero based)
129  * \param style Formatting style to use.  In addition to any STYLEF_
130  *        flag, it can be TEXT_NORMAL, TEXT_FILL, TEXT_INVERT or
131  *        TEXT_RIGHT, or a combination of these flags ORed together.
132  * \param fmt  String possibly containing printf() formatting commands.
133  *
134  * \see text_puts() text_putchar() text_printf() text_vprintf()
135  * \see text_moveto() text_style()
136  */
137 int PGM_FUNC(text_xprintf)(struct Bitmap *bm,
138                 uint8_t row, uint8_t col, uint16_t style, const char * PGM_ATTR fmt, ...)
139 {
140         int len;
141         uint8_t oldstyle = 0;
142         va_list ap;
143
144         va_start(ap, fmt);
145
146         text_moveto(bm, row, col);
147
148         if (style & STYLEF_MASK)
149                 oldstyle = text_style(style, STYLEF_MASK);
150
151         if (style & (TEXT_CENTER | TEXT_RIGHT))
152         {
153                 uint8_t pad = bm->width - PGM_FUNC(text_vwidthf)(bm, fmt, ap);
154
155                 if (style & TEXT_CENTER)
156                         pad /= 2;
157
158                 if (style & TEXT_FILL)
159                         gfx_rectFillC(bm, 0, row * FONT_HEIGHT, pad, (row + 1) * FONT_HEIGHT,
160                                 (style & STYLEF_INVERT) ? 0xFF : 0x00);
161
162                 text_setcoord(bm, pad, row * FONT_HEIGHT);
163         }
164
165         len = PGM_FUNC(text_vprintf)(bm, fmt, ap);
166         va_end(ap);
167
168         if (style & TEXT_FILL)
169                 gfx_rectFillC(bm, bm->penX, row * FONT_HEIGHT, bm->width, (row + 1) * FONT_HEIGHT,
170                         (style & STYLEF_INVERT) ? 0xFF : 0x00);
171
172         /* Restore old style */
173         if (style & STYLEF_MASK)
174                 text_style(oldstyle, STYLEF_MASK);
175
176         return len;
177 }
178
179
180 /*!
181  * Return the width in pixels of a vprintf()-formatted string.
182  */
183 int PGM_FUNC(text_vwidthf)(
184         UNUSED_ARG(struct Bitmap *, bm),
185         const char * PGM_ATTR fmt,
186         va_list ap)
187 {
188         return PGM_FUNC(vsprintf)(NULL, fmt, ap) * FONT_WIDTH;
189 }
190
191
192 /*!
193  * Return the width in pixels of a printf()-formatted string.
194  */
195 int PGM_FUNC(text_widthf)(struct Bitmap *bm, const char * PGM_ATTR fmt, ...)
196 {
197         int width;
198
199         va_list ap;
200         va_start(ap, fmt);
201         width = PGM_FUNC(text_vwidthf)(bm, fmt, ap);
202         va_end(ap);
203
204         return width;
205 }