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