Add include to fix warning for vsprintf()
[bertos.git] / mware / text_format.c
1 /*!
2  * \file
3  *
4  * <!--
5  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
6  * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
7  * This file is part of DevLib - See devlib/README for information.
8  * -->
9  *
10  * \version $Id$
11  *
12  * \author Bernardo Innocenti <bernie@develer.com>
13  * \author Stefano Fedrigo <aleph@develer.com>
14  *
15  * \brief printf-family routines for text output
16  */
17
18 /*
19  * $Log$
20  * Revision 1.3  2004/08/03 15:57:18  aleph
21  * Add include to fix warning for vsprintf()
22  *
23  * Revision 1.2  2004/06/03 11:27:09  bernie
24  * Add dual-license information.
25  *
26  * Revision 1.1  2004/05/23 15:43:16  bernie
27  * Import mware modules.
28  *
29  * Revision 1.2  2004/03/26 18:50:50  bernie
30  * Move _PROGMEM stuff to compiler.h
31  *
32  * Revision 1.1  2004/03/19 16:52:28  bernie
33  * Move printf() like functions from text.c to text_format.c and add PROGMEM versions.
34  *
35  */
36
37 #include "text.h"
38 #include "formatwr.h" /* _formatted_write() */
39 #include "font.h"
40 #include "gfx.h"
41 #include <stdio.h> /* vsprintf() */
42 #include <stdarg.h>
43 #include <string.h> /* strlen() */
44
45 /*!
46  * Render string <code>str</code> in bitmap
47  */
48 int PGM_FUNC(text_puts)(const char * PGM_ATTR str, struct Bitmap *bm)
49 {
50         char c;
51
52         while ((c = PGM_READ_CHAR(str++)))
53                 text_putchar(c, bm);
54
55         return 0;
56 }
57
58
59 int PGM_FUNC(text_vprintf)(struct Bitmap *bm, const char * PGM_ATTR fmt, va_list ap)
60 {
61         return PGM_FUNC(_formatted_write)(fmt, (void (*)(char, void *))text_putchar, bm, ap);
62 }
63
64
65 int PGM_FUNC(text_printf)(struct Bitmap *bm, const char * PGM_ATTR fmt, ...)
66 {
67         int len;
68
69         va_list ap;
70         va_start(ap, fmt);
71         len = PGM_FUNC(text_vprintf)(bm, fmt, ap);
72         va_end(ap);
73
74         return len;
75 }
76
77
78 int PGM_FUNC(text_xprintf)(struct Bitmap *bm,
79                 uint8_t row, uint8_t col, uint8_t mode, const char * PGM_ATTR fmt, ...) 
80 {
81         int len;
82         uint8_t oldstyle = 0;
83         va_list ap;
84
85         va_start(ap, fmt);
86
87         text_moveto(bm, row, col);
88
89         if (mode & TEXT_INVERT)
90                 oldstyle = text_style(STYLEF_INVERT, STYLEF_INVERT);
91
92         if (mode & (TEXT_CENTER | TEXT_RIGHT))
93         {
94                 uint8_t pad;
95
96                 pad = bm->width/FONT_WIDTH - PGM_FUNC(vsprintf)(NULL, fmt, ap);
97
98                 if (mode & TEXT_CENTER)
99                         pad /= 2;
100
101                 while (pad--)
102                         text_putchar(' ', bm);
103         }
104
105         len = PGM_FUNC(text_vprintf)(bm, fmt, ap);
106         va_end(ap);
107
108         if (mode & (TEXT_FILL | TEXT_CENTER))
109                 while (bm->penX + FONT_WIDTH < bm->width)
110                         text_putchar(' ', bm);
111
112         if (mode & TEXT_INVERT)
113                 text_style(oldstyle, STYLEF_MASK);
114
115         return len;
116 }
117