Correctly compute text width for prop fonts; Make styles a per-bitmap attribute.
[bertos.git] / gfx / gfx.h
1 /*!
2  * \file
3  * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
4  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
5  * This file is part of DevLib - See README.devlib for information.
6  *
7  * \version $Id$
8  *
9  * \author Bernardo Innocenti <bernie@develer.com>
10  * \author Stefano Fedrigo <aleph@develer.com>
11  *
12  * \brief General pourpose graphics routines
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.11  2006/03/07 22:18:04  bernie
18  *#* Correctly compute text width for prop fonts; Make styles a per-bitmap attribute.
19  *#*
20  *#* Revision 1.10  2006/02/17 22:24:40  bernie
21  *#* Fix undefined preprocessor symbol.
22  *#*
23  *#* Revision 1.9  2006/02/15 09:10:15  bernie
24  *#* Implement prop fonts; Fix algo styles.
25  *#*
26  *#* Revision 1.8  2006/02/10 12:28:33  bernie
27  *#* Add font support in bitmaps; Make bitmap formats public.
28  *#*
29  *#* Revision 1.7  2006/01/26 00:36:48  bernie
30  *#* Const correctness for some new functions.
31  *#*
32  *#* Revision 1.6  2006/01/23 23:13:04  bernie
33  *#* RECT_WIDTH(), RECT_HEIGHT(), RASTER_SIZE(): New macros.
34  *#*
35  *#* Revision 1.5  2006/01/17 02:31:29  bernie
36  *#* Add bitmap format support; Improve some comments.
37  *#*/
38
39 #ifndef GFX_GFX_H
40 #define GFX_GFX_H
41
42 #include <cfg/compiler.h>
43 #include <cfg/cpu.h>   /* CPU_HARVARD */
44
45 #include <appconfig.h> /* CONFIG_GFX_* */
46
47 /**
48  * \name Known pixel formats for bitmap representation.
49  * \{
50  */
51 #define BITMAP_FMT_PLANAR_H_MSB  1  /**< Planar pixels, horizontal bytes, MSB left. */
52 #define BITMAP_FMT_PLANAR_V_LSB  2  /**< Planar pixels, vertical bytes, LSB top. */
53 /* \} */
54
55 #if !defined(CONFIG_BITMAP_FMT) || (CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_H_MSB && CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_V_LSB)
56         #error CONFIG_BITMAP_FMT must be defined to either BITMAP_FMT_PLANAR_H_LSB or BITMAP_FMT_PLANAR_V_LSB
57 #endif
58 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
59         #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
60 #endif
61 #if !defined(CONFIG_GFX_TEXT) || (CONFIG_GFX_TEXT != 0 && CONFIG_GFX_TEXT != 1)
62         #error CONFIG_GFX_TEXT must be defined to either 0 or 1
63 #endif
64
65 EXTERN_C_BEGIN
66
67 /*! Common type for coordinates expressed in pixel units */
68 typedef int coord_t;
69
70 #if CONFIG_GFX_VCOORDS
71 /*! Common type for coordinates expressed in logical units */
72 typedef float vcoord_t;
73 #endif /* CONFIG_GFX_VCOORDS */
74
75
76 /**
77  * Describe a rectangular area with coordinates expressed in pixels.
78  *
79  * The rectangle is represented in terms of its top/left and
80  * right/bottom borders.
81  *
82  * In some cases, rectangles are assumed to obey to the
83  * following invariants:
84  *
85  *    xmin <= xmax
86  *    ymin <= ymax
87  *
88  * Oddly, the xmin and ymin coordinates are inclusive, while the
89  * xmax and ymax coordinates are non-inclusive.  This design
90  * decision makes several computations simpler and lets you
91  * specify empty (0x0) rectangles without breaking the
92  * invariants.
93  *
94  * Computing the size of a rectangle can be done by simply
95  * subtracting the maximum X or Y coordinate from the minimum
96  * X or Y coordinate.
97  */
98 typedef struct Rect { coord_t xmin, ymin, xmax, ymax; } Rect;
99
100 /**
101  * Return the width of a rectangle in pixels.
102  *
103  * \note The argument \a r is evaluated twice.
104  */
105 #define RECT_WIDTH(r)   ((r)->xmax - (r)->xmin)
106
107 /**
108  * Return the height of a rectangle in pixels.
109  *
110  * \note The argument \a r is evaluated twice.
111  */
112 #define RECT_HEIGHT(r)  ((r)->ymax - (r)->ymin)
113
114 /* Fwd decl */
115 struct Font;
116
117 /*!
118  * Control structure to draw in a bitmap
119  */
120 typedef struct Bitmap
121 {
122         uint8_t *raster;        /*!< Pointer to byte array to hold the data */
123         coord_t width, height;  /*!< Width/Height in pixels */
124         coord_t stride;         /*!< Bytes per row. */
125         coord_t penX, penY;     /*!< Current pen position MoveTo()/LineTo() */
126
127         Rect cr;                /*!< Clip drawing inside this rectangle */
128
129 #if CONFIG_GFX_TEXT
130         const struct Font *font;/**< Current font for text rendering. */
131
132         /**
133          * Algorithmic text style flags.
134          *
135          * The text rendering routine can apply a few simple transformations
136          * to the current font in order to generate common styles such as
137          * bold, italic and underline from plain glyphs.
138          */
139         uint8_t styles;
140 #endif
141 #if CONFIG_GFX_VCOORDS
142         /*!
143          * \name Logical coordinate system
144          * \{
145          */
146         vcoord_t orgX, orgY;
147         vcoord_t scaleX, scaleY;
148         /*\}*/
149 #endif /* CONFIG_GFX_VCOORDS */
150
151 } Bitmap;
152
153 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
154         /**
155          * Compute the size in bytes of a raster suitable for
156          * holding a bitmap of \a width x \a height pixels.
157          */
158         #define RASTER_SIZE(width, height) ( ((width) + 7 / 8) * (height) )
159
160 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
161         /**
162          * Compute the size in bytes of a raster suitable for
163          * holding a bitmap of \a width x \a height pixels.
164          */
165         #define RASTER_SIZE(width, height) ( (width) * (((height) + 7) / 8) )
166 #else
167         #error Unknown value of CONFIG_BITMAP_FMT
168 #endif /* CONFIG_BITMAP_FMT */
169
170
171 /* Function prototypes */
172 void gfx_bitmapInit (Bitmap *bm, uint8_t *raster, coord_t w, coord_t h);
173 void gfx_bitmapClear(Bitmap *bm);
174 void gfx_blit       (Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy);
175 void gfx_blitRaster (Bitmap *dst, coord_t dx, coord_t dy, const uint8_t *raster, coord_t w, coord_t h, coord_t stride);
176 void gfx_line       (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
177 void gfx_rectDraw   (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
178 void gfx_rectFillC  (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color);
179 void gfx_rectFill   (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
180 void gfx_rectClear  (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
181 void gfx_moveTo     (Bitmap *bm, coord_t x,  coord_t y);
182 void gfx_lineTo     (Bitmap *bm, coord_t x,  coord_t y);
183 void gfx_setClipRect(Bitmap *bm, coord_t xmin, coord_t ymin, coord_t xmax, coord_t ymax);
184
185 #if CPU_HARVARD
186         #include <mware/pgm.h>
187         void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster);
188 #endif
189
190 #if CONFIG_GFX_TEXT
191 INLINE void gfx_setFont(Bitmap *bm, const struct Font *font)
192 {
193         bm->font = font;
194 }
195 #endif
196
197 #if CONFIG_GFX_VCOORDS
198 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
199 coord_t gfx_transformX(Bitmap *bm, vcoord_t x);
200 coord_t gfx_transformY(Bitmap *bm, vcoord_t y);
201 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
202 #endif /* CONFIG_GFX_VCOORDS */
203
204 EXTERN_C_END
205
206 #endif /* GFX_GFX_H */