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