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