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