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