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