PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
drawable.h
Go to the documentation of this file.
1#ifndef PATCHWORK_DRAW_H
2#define PATCHWORK_DRAW_H 1
3
4#include "cmd.h"
5#include "font.h"
6#include "pixel.h"
7#include "polygon.h"
8#include "rect.h"
9#include "surface.h"
10
11#include <stdint.h>
12
13#if defined(__cplusplus)
14extern "C"
15{
16#endif
17
18typedef struct image image_t;
19
20/**
21 * @brief Drawable.
22 * @defgroup libpatchwork_draw Drawable
23 * @ingroup libpatchwork
24 *
25 * A drawable implements a generic system for drawing operations to a pixel buffer.
26 *
27 * @{
28 */
29
30/**
31 * @brief Drawable structure.
32 * @struct drawable_t
33 */
42
43/**
44 * @brief Alignment type.
45 * @typedef align_t
46 */
47typedef enum
48{
52} align_t;
53
54/**
55 * @brief Direction type.
56 * @typedef direction_t
57 */
64
65/**
66 * @brief Draw a filled rectangle.
67 *
68 * Will fit the rectangle to the drawable's content rectangle.
69 *
70 * @param draw The drawable to draw to.
71 * @param rect The rectangle to draw.
72 * @param pixel The pixel color to fill with.
73 */
74void draw_rect(drawable_t* draw, const rect_t* rect, pixel_t pixel);
75
76/**
77 * @brief Draw a filled polygon.
78 *
79 * Will clip the polygon to fit within the drawable's content rectangle.
80 *
81 * @param draw The drawable to draw to.
82 * @param points The points of the polygon.
83 * @param pointCount The number of points in the polygon, must be at least 3.
84 * @param pixel The pixel color to fill with.
85 */
86void draw_polygon(drawable_t* draw, const point_t* points, uint64_t pointCount, pixel_t pixel);
87
88/**
89 * @brief Draw a line between two points.
90 *
91 * Will clip the line to fit within the drawable's content rectangle.
92 *
93 * @param draw The drawable to draw to.
94 * @param start The starting point of the line, inclusive.
95 * @param end The ending point of the line, inclusive.
96 * @param pixel The pixel color to draw with.
97 * @param thickness The thickness of the line, must be at least 1.
98 */
99void draw_line(drawable_t* draw, const point_t* start, const point_t* end, pixel_t pixel, uint32_t thickness);
100
101/**
102 * @brief Draw a skeuomorphic frame.
103 *
104 * Will draw a frame with the given width, using the foreground color for the top and left sides, and the background
105 * color for the bottom and right sides.
106 *
107 * Will fit the rectangle to the drawable's content rectangle.
108 *
109 * @param draw The drawable to draw to.
110 * @param rect The rectangle to draw the frame around.
111 * @param width The width of the frame.
112 * @param foreground The pixel color for the top and left sides.
113 * @param background The pixel color for the bottom and right sides.
114 */
115void draw_frame(drawable_t* draw, const rect_t* rect, uint64_t width, pixel_t foreground, pixel_t background);
116
117/**
118 * @brief Draw a dashed outline just inside the given rectangle.
119 *
120 * Will fit the rectangle to the drawable's content rectangle.
121 *
122 * @param draw The drawable to draw to.
123 * @param rect The rectangle to draw the outline inside.
124 * @param pixel The pixel color to draw with.
125 * @param length The length of the dashes.
126 * @param width The width of the outline.
127 */
128void draw_dashed_outline(drawable_t* draw, const rect_t* rect, pixel_t pixel, uint32_t length, int32_t width);
129
130/**
131 * @brief Draw a filled border bezel just inside the given rectangle.
132 *
133 * Will fit the rectangle to the drawable's content rectangle.
134 *
135 * @param draw The drawable to draw to.
136 * @param rect The rectangle to draw the bezel around.
137 * @param width The width of the bezel.
138 * @param pixel The pixel color to draw with.
139 */
140void draw_bezel(drawable_t* draw, const rect_t* rect, uint64_t width, pixel_t pixel);
141
142/**
143 * @brief Draw a gradient filled rectangle.
144 *
145 * Will fit the rectangle to the drawable's content rectangle.
146 *
147 * @param draw The drawable to draw to.
148 * @param rect The rectangle to draw.
149 * @param start The starting pixel color.
150 * @param end The ending pixel color.
151 * @param direction The direction of the gradient.
152 * @param shouldAddNoise Whether to add noise to the gradient to reduce banding.
153 */
154void draw_gradient(drawable_t* draw, const rect_t* rect, pixel_t start, pixel_t end, direction_t direction,
155 bool shouldAddNoise);
156
157/**
158 * @brief Transfer pixels from one drawable to another.
159 *
160 * @param dest The destination drawable.
161 * @param src The source drawable.
162 * @param destRect The rectangle that will be filled in the destination.
163 * @param srcPoint The top-left point in the source drawable to copy from.
164 */
165void draw_transfer(drawable_t* dest, drawable_t* src, const rect_t* destRect, const point_t* srcPoint);
166
167/**
168 * @brief Transfer pixels from one drawable to another with alpha blending.
169 *
170 * @param dest The destination drawable.
171 * @param src The source drawable.
172 * @param destRect The rectangle that will be filled in the destination.
173 * @param srcPoint The top-left point in the source drawable to copy from.
174 */
175void draw_transfer_blend(drawable_t* dest, drawable_t* src, const rect_t* destRect, const point_t* srcPoint);
176
177/**
178 * @brief Draw an image,
179 *
180 * @param draw The drawable to draw to.
181 * @param image The image to draw.
182 * @param destRect The rectangle that will be filled in the drawable.
183 * @param srcPoint The top-left point in the image to copy from.
184 */
185void draw_image(drawable_t* draw, image_t* image, const rect_t* destRect, const point_t* srcPoint);
186
187/**
188 * @brief Draw an image with alpha blending.
189 *
190 * @param draw The drawable to draw to.
191 * @param image The image to draw.
192 * @param destRect The rectangle that will be filled in the drawable.
193 * @param srcPoint The top-left point in the image to copy from.
194 */
195void draw_image_blend(drawable_t* draw, image_t* image, const rect_t* destRect, const point_t* srcPoint);
196
197/**
198 * @brief Draw a string.
199 *
200 * Will not draw a background, only the glyphs of the string.
201 *
202 * @param draw The drawable to draw to.
203 * @param font The font to use. If `NULL`, the default font for the display will be used.
204 * @param point The top-left point to start drawing the string at.
205 * @param pixel The pixel color to draw with.
206 * @param string The string to draw, null-termination is ignored.
207 * @param length The length of the string to draw.
208 */
209void draw_string(drawable_t* draw, const font_t* font, const point_t* point, pixel_t pixel, const char* string,
210 uint64_t length);
211
212/**
213 * @brief Draw text to a drawable.
214 *
215 * Will clip the text to fit within the rectangle, adding an ellipsis (`...`) if the text is too long.
216 *
217 * @param draw The drawable to draw to.
218 * @param rect The rectangle to draw the text within.
219 * @param font The font to use. If `NULL`, the default font for the display will be used.
220 * @param xAlign The horizontal alignment of the text within the rectangle.
221 * @param yAlign The vertical alignment of the text within the rectangle.
222 * @param pixel The pixel color to draw with.
223 * @param text The text to draw, null-terminated.
224 */
225void draw_text(drawable_t* draw, const rect_t* rect, const font_t* font, align_t xAlign, align_t yAlign, pixel_t pixel,
226 const char* text);
227
228/**
229 * @brief Draw multiline text to a drawable.
230 *
231 * Will wrap lines to fit within the rectangle. Newlines (`\n`) are also supported.
232 *
233 * @param draw The drawable to draw to.
234 * @param rect The rectangle to draw the text within.
235 * @param font The font to use. If `NULL`, the default font for the display will be used.
236 * @param xAlign The horizontal alignment of the text within the rectangle.
237 * @param yAlign The vertical alignment of the text within the rectangle.
238 * @param pixel The pixel color to draw with.
239 * @param text The text to draw, null-terminated.
240 */
241void draw_text_multiline(drawable_t* draw, const rect_t* rect, const font_t* font, align_t xAlign, align_t yAlign,
242 pixel_t pixel, const char* text);
243
244/**
245 * @brief Draw a ridge effect.
246 *
247 * Will draw a inverted frame inside another frame inside the given rectangle, creating a ridge effect.
248 *
249 * @param draw The drawable to draw to.
250 * @param rect The rectangle to draw the ridge effect within.
251 * @param width The total width of the ridge effect.
252 * @param foreground The pixel color for the top and left sides of the outer frame and the bottom and right sides of the
253 * inner frame.
254 * @param background The pixel color for the bottom and right sides of the outer frame and the top and left sides of the
255 * inner frame.
256 */
257void draw_ridge(drawable_t* draw, const rect_t* rect, uint64_t width, pixel_t foreground, pixel_t background);
258
259/**
260 * @brief Draw a separator line.
261 *
262 * Will draw a separator line within the given rectangle, either horizontally or vertically.
263 *
264 * @param draw The drawable to draw to.
265 * @param rect The rectangle to draw the separator within.
266 * @param highlight The pixel color for the highlight side of the separator (top or left).
267 * @param shadow The pixel color for the shadow side of the separator (bottom or right).
268 * @param direction The direction of the separator line.
269 */
270void draw_separator(drawable_t* draw, const rect_t* rect, pixel_t highlight, pixel_t shadow, direction_t direction);
271
272/**
273 * @brief Invalidate a rectangle in the drawable.
274 *
275 * Marks the given rectangle as invalid, so that it will be updated on the next flush.
276 *
277 * Flushing is handled by each element or other system using the drawable, not the drawable itself.
278 *
279 * @param draw The drawable.
280 * @param rect The rectangle to invalidate, or `NULL` to invalidate the entire content rectangle.
281 */
282void draw_invalidate(drawable_t* draw, const rect_t* rect);
283
284/** @} */
285
286#if defined(__cplusplus)
287}
288#endif
289
290#endif
void draw_text_multiline(drawable_t *draw, const rect_t *rect, const font_t *font, align_t xAlign, align_t yAlign, pixel_t pixel, const char *text)
Draw multiline text to a drawable.
Definition drawable.c:812
void draw_line(drawable_t *draw, const point_t *start, const point_t *end, pixel_t pixel, uint32_t thickness)
Draw a line between two points.
Definition drawable.c:179
align_t
Alignment type.
Definition drawable.h:48
void draw_transfer_blend(drawable_t *dest, drawable_t *src, const rect_t *destRect, const point_t *srcPoint)
Transfer pixels from one drawable to another with alpha blending.
Definition drawable.c:507
void draw_polygon(drawable_t *draw, const point_t *points, uint64_t pointCount, pixel_t pixel)
Draw a filled polygon.
Definition drawable.c:48
void draw_gradient(drawable_t *draw, const rect_t *rect, pixel_t start, pixel_t end, direction_t direction, bool shouldAddNoise)
Draw a gradient filled rectangle.
Definition drawable.c:382
void draw_string(drawable_t *draw, const font_t *font, const point_t *point, pixel_t pixel, const char *string, uint64_t length)
Draw a string.
Definition drawable.c:590
void draw_image_blend(drawable_t *draw, image_t *image, const rect_t *destRect, const point_t *srcPoint)
Draw an image with alpha blending.
Definition drawable.c:550
void draw_rect(drawable_t *draw, const rect_t *rect, pixel_t pixel)
Draw a filled rectangle.
Definition drawable.c:7
void draw_ridge(drawable_t *draw, const rect_t *rect, uint64_t width, pixel_t foreground, pixel_t background)
Draw a ridge effect.
Definition drawable.c:992
void draw_image(drawable_t *draw, image_t *image, const rect_t *destRect, const point_t *srcPoint)
Draw an image,.
Definition drawable.c:545
void draw_separator(drawable_t *draw, const rect_t *rect, pixel_t highlight, pixel_t shadow, direction_t direction)
Draw a separator line.
Definition drawable.c:1006
void draw_transfer(drawable_t *dest, drawable_t *src, const rect_t *destRect, const point_t *srcPoint)
Transfer pixels from one drawable to another.
Definition drawable.c:462
void draw_text(drawable_t *draw, const rect_t *rect, const font_t *font, align_t xAlign, align_t yAlign, pixel_t pixel, const char *text)
Draw text to a drawable.
Definition drawable.c:667
void draw_dashed_outline(drawable_t *draw, const rect_t *rect, pixel_t pixel, uint32_t length, int32_t width)
Draw a dashed outline just inside the given rectangle.
Definition drawable.c:259
direction_t
Direction type.
Definition drawable.h:59
void draw_bezel(drawable_t *draw, const rect_t *rect, uint64_t width, pixel_t pixel)
Draw a filled border bezel just inside the given rectangle.
Definition drawable.c:339
void draw_invalidate(drawable_t *draw, const rect_t *rect)
Invalidate a rectangle in the drawable.
Definition drawable.c:1060
void draw_frame(drawable_t *draw, const rect_t *rect, uint64_t width, pixel_t foreground, pixel_t background)
Draw a skeuomorphic frame.
Definition drawable.c:204
@ ALIGN_MIN
Definition drawable.h:51
@ ALIGN_CENTER
Definition drawable.h:49
@ ALIGN_MAX
Definition drawable.h:50
@ DIRECTION_DIAGONAL
Definition drawable.h:62
@ DIRECTION_HORIZONTAL
Definition drawable.h:61
@ DIRECTION_VERTICAL
Definition drawable.h:60
uint32_t pixel_t
Definition pixel.h:11
static void start()
Definition main.c:542
static image_t * image
Definition main.c:5
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__INT32_TYPE__ int32_t
Definition stdint.h:14
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Opaque display structure.
Definition internal.h:61
Drawable structure.
Definition drawable.h:35
pixel_t * buffer
Definition drawable.h:38
uint32_t stride
Definition drawable.h:37
rect_t contentRect
Definition drawable.h:39
display_t * disp
Definition drawable.h:36
rect_t invalidRect
Definition drawable.h:40
Definition rect.h:13