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