1 /**
2  * @file lv_draw_vector.h
3  *
4  */
5 
6 #ifndef LV_DRAW_VECTOR_H
7 #define LV_DRAW_VECTOR_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "../misc/lv_array.h"
17 #include "../misc/lv_matrix.h"
18 #include "lv_draw_image.h"
19 
20 #if LV_USE_VECTOR_GRAPHIC
21 
22 #if !LV_USE_MATRIX
23 #error "lv_draw_vector needs LV_USE_MATRIX = 1"
24 #endif
25 
26 /**********************
27  *      TYPEDEFS
28  **********************/
29 typedef enum {
30     LV_VECTOR_FILL_NONZERO = 0,
31     LV_VECTOR_FILL_EVENODD,
32 } lv_vector_fill_t;
33 
34 typedef enum {
35     LV_VECTOR_STROKE_CAP_BUTT = 0,
36     LV_VECTOR_STROKE_CAP_SQUARE,
37     LV_VECTOR_STROKE_CAP_ROUND,
38 } lv_vector_stroke_cap_t;
39 
40 typedef enum {
41     LV_VECTOR_STROKE_JOIN_MITER = 0,
42     LV_VECTOR_STROKE_JOIN_BEVEL,
43     LV_VECTOR_STROKE_JOIN_ROUND,
44 } lv_vector_stroke_join_t;
45 
46 typedef enum {
47     LV_VECTOR_PATH_QUALITY_MEDIUM = 0, /* default*/
48     LV_VECTOR_PATH_QUALITY_HIGH,
49     LV_VECTOR_PATH_QUALITY_LOW,
50 } lv_vector_path_quality_t;
51 
52 typedef enum {
53     LV_VECTOR_BLEND_SRC_OVER = 0,
54     LV_VECTOR_BLEND_SRC_IN,
55     LV_VECTOR_BLEND_DST_OVER,
56     LV_VECTOR_BLEND_DST_IN,
57     LV_VECTOR_BLEND_SCREEN,
58     LV_VECTOR_BLEND_MULTIPLY,
59     LV_VECTOR_BLEND_NONE,
60     LV_VECTOR_BLEND_ADDITIVE,
61     LV_VECTOR_BLEND_SUBTRACTIVE,
62 } lv_vector_blend_t;
63 
64 typedef enum {
65     LV_VECTOR_PATH_OP_MOVE_TO = 0,
66     LV_VECTOR_PATH_OP_LINE_TO,
67     LV_VECTOR_PATH_OP_QUAD_TO,
68     LV_VECTOR_PATH_OP_CUBIC_TO,
69     LV_VECTOR_PATH_OP_CLOSE,
70 } lv_vector_path_op_t;
71 
72 typedef enum {
73     LV_VECTOR_DRAW_STYLE_SOLID = 0,
74     LV_VECTOR_DRAW_STYLE_PATTERN,
75     LV_VECTOR_DRAW_STYLE_GRADIENT,
76 } lv_vector_draw_style_t;
77 
78 typedef enum {
79     LV_VECTOR_GRADIENT_SPREAD_PAD = 0,
80     LV_VECTOR_GRADIENT_SPREAD_REPEAT,
81     LV_VECTOR_GRADIENT_SPREAD_REFLECT,
82 } lv_vector_gradient_spread_t;
83 
84 typedef enum {
85     LV_VECTOR_GRADIENT_STYLE_LINEAR = 0,
86     LV_VECTOR_GRADIENT_STYLE_RADIAL,
87 } lv_vector_gradient_style_t;
88 
89 struct _lv_fpoint_t {
90     float x;
91     float y;
92 };
93 
94 /**********************
95  * GLOBAL PROTOTYPES
96  **********************/
97 
98 /**
99  * Transform the coordinates of a point using given matrix
100  * @param matrix           pointer to a matrix
101  * @param point            pointer to a point
102  */
103 void lv_matrix_transform_point(const lv_matrix_t * matrix, lv_fpoint_t * point);
104 
105 /**
106  * Transform all the coordinates of a path using given matrix
107  * @param matrix           pointer to a matrix
108  * @param path             pointer to a path
109  */
110 void lv_matrix_transform_path(const lv_matrix_t * matrix, lv_vector_path_t * path);
111 
112 /**
113  * Create a vector graphic path object
114  * @param quality       the quality hint of path
115  * @return              pointer to the created path object
116  */
117 lv_vector_path_t * lv_vector_path_create(lv_vector_path_quality_t quality);
118 
119 /**
120  * Copy a path data to another
121  * @param target_path       pointer to a path
122  * @param path              pointer to source path
123  */
124 void lv_vector_path_copy(lv_vector_path_t * target_path, const lv_vector_path_t * path);
125 
126 /**
127  * Clear path data
128  * @param path              pointer to a path
129  */
130 void lv_vector_path_clear(lv_vector_path_t * path);
131 
132 /**
133  * Delete the graphic path object
134  * @param path              pointer to a path
135  */
136 void lv_vector_path_delete(lv_vector_path_t * path);
137 
138 /**
139  * Begin a new sub path and set a point to path
140  * @param path              pointer to a path
141  * @param p                 pointer to a `lv_fpoint_t` variable
142  */
143 void lv_vector_path_move_to(lv_vector_path_t * path, const lv_fpoint_t * p);
144 
145 /**
146  * Add a line to the path from last point to the point
147  * @param path              pointer to a path
148  * @param p                 pointer to a `lv_fpoint_t` variable
149  */
150 void lv_vector_path_line_to(lv_vector_path_t * path, const lv_fpoint_t * p);
151 
152 /**
153  * Add a quadratic bezier line to the path from last point to the point
154  * @param path              pointer to a path
155  * @param p1                pointer to a `lv_fpoint_t` variable for control point
156  * @param p2                pointer to a `lv_fpoint_t` variable for end point
157  */
158 void lv_vector_path_quad_to(lv_vector_path_t * path, const lv_fpoint_t * p1, const lv_fpoint_t * p2);
159 
160 /**
161  * Add a cubic bezier line to the path from last point to the point
162  * @param path              pointer to a path
163  * @param p1                pointer to a `lv_fpoint_t` variable for first control point
164  * @param p2                pointer to a `lv_fpoint_t` variable for second control point
165  * @param p3                pointer to a `lv_fpoint_t` variable for end point
166  */
167 void lv_vector_path_cubic_to(lv_vector_path_t * path, const lv_fpoint_t * p1, const lv_fpoint_t * p2,
168                              const lv_fpoint_t * p3);
169 
170 /**
171  * Close the sub path
172  * @param path              pointer to a path
173  */
174 void lv_vector_path_close(lv_vector_path_t * path);
175 
176 /**
177  * Get the bounding box of a path
178  * @param path              pointer to a path
179  * @param area              pointer to a `lv_area_t` variable for bounding box
180  */
181 void lv_vector_path_get_bounding(const lv_vector_path_t * path, lv_area_t * area);
182 
183 /**
184  * Add a rectangle to the path
185  * @param path              pointer to a path
186  * @param rect              pointer to a `lv_area_t` variable
187  * @param rx                the horizontal radius for rounded rectangle
188  * @param ry                the vertical radius for rounded rectangle
189  */
190 void lv_vector_path_append_rect(lv_vector_path_t * path, const lv_area_t * rect, float rx, float ry);
191 
192 /**
193  * Add a circle to the path
194  * @param path              pointer to a path
195  * @param c                 pointer to a `lv_fpoint_t` variable for center of the circle
196  * @param rx                the horizontal radius for circle
197  * @param ry                the vertical radius for circle
198  */
199 void lv_vector_path_append_circle(lv_vector_path_t * path, const lv_fpoint_t * c, float rx, float ry);
200 
201 /**
202  * Add a arc to the path
203  * @param path              pointer to a path
204  * @param c                 pointer to a `lv_fpoint_t` variable for center of the circle
205  * @param radius            the radius for arc
206  * @param start_angle       the start angle for arc
207  * @param sweep             the sweep angle for arc, could be negative
208  * @param pie               true: draw a pie, false: draw a arc
209  */
210 void lv_vector_path_append_arc(lv_vector_path_t * path, const lv_fpoint_t * c, float radius, float start_angle,
211                                float sweep, bool pie);
212 
213 /**
214  * Add an sub path to the path
215  * @param path              pointer to a path
216  * @param subpath           pointer to another path which will be added
217  */
218 void lv_vector_path_append_path(lv_vector_path_t * path, const lv_vector_path_t * subpath);
219 
220 /**
221  * Create a vector graphic descriptor
222  * @param layer         pointer to a layer
223  * @return              pointer to the created descriptor
224  */
225 lv_vector_dsc_t * lv_vector_dsc_create(lv_layer_t * layer);
226 
227 /**
228  * Delete the vector graphic descriptor
229  * @param dsc              pointer to a vector graphic descriptor
230  */
231 void lv_vector_dsc_delete(lv_vector_dsc_t * dsc);
232 
233 /**
234  * Set a matrix to current transformation matrix
235  * @param dsc              pointer to a vector graphic descriptor
236  * @param matrix           pointer to a matrix
237  */
238 void lv_vector_dsc_set_transform(lv_vector_dsc_t * dsc, const lv_matrix_t * matrix);
239 
240 /**
241  * Set blend mode for descriptor
242  * @param dsc              pointer to a vector graphic descriptor
243  * @param blend            the blend mode to be set in `lv_vector_blend_t`
244  */
245 void lv_vector_dsc_set_blend_mode(lv_vector_dsc_t * dsc, lv_vector_blend_t blend);
246 
247 /**
248  * Set fill color for descriptor
249  * @param dsc              pointer to a vector graphic descriptor
250  * @param color            the color to be set in lv_color32_t format
251  */
252 void lv_vector_dsc_set_fill_color32(lv_vector_dsc_t * dsc, lv_color32_t color);
253 
254 /**
255  * Set fill color for descriptor
256  * @param dsc              pointer to a vector graphic descriptor
257  * @param color            the color to be set in lv_color_t format
258  */
259 void lv_vector_dsc_set_fill_color(lv_vector_dsc_t * dsc, lv_color_t color);
260 
261 /**
262  * Set fill opacity for descriptor
263  * @param dsc              pointer to a vector graphic descriptor
264  * @param opa              the opacity to be set in lv_opa_t format
265  */
266 void lv_vector_dsc_set_fill_opa(lv_vector_dsc_t * dsc, lv_opa_t opa);
267 
268 /**
269  * Set fill rule for descriptor
270  * @param dsc              pointer to a vector graphic descriptor
271  * @param rule             the fill rule to be set in lv_vector_fill_t format
272  */
273 void lv_vector_dsc_set_fill_rule(lv_vector_dsc_t * dsc, lv_vector_fill_t rule);
274 
275 /**
276  * Set fill image for descriptor
277  * @param dsc              pointer to a vector graphic descriptor
278  * @param img_dsc          pointer to a `lv_draw_image_dsc_t` variable
279  */
280 void lv_vector_dsc_set_fill_image(lv_vector_dsc_t * dsc, const lv_draw_image_dsc_t * img_dsc);
281 
282 /**
283  * Set fill linear gradient for descriptor
284  * @param dsc pointer to a vector graphic descriptor
285  * @param x1 the x for start point
286  * @param y1 the y for start point
287  * @param x2 the x for end point
288  * @param y2 the y for end point
289  */
290 void lv_vector_dsc_set_fill_linear_gradient(lv_vector_dsc_t * dsc, float x1, float y1, float x2, float y2);
291 
292 /**
293 
294  * Set fill radial gradient radius for descriptor
295  * @param dsc pointer to a vector graphic descriptor
296  * @param cx the x for center of the circle
297  * @param cy the y for center of the circle
298  * @param radius the radius for circle
299  */
300 void lv_vector_dsc_set_fill_radial_gradient(lv_vector_dsc_t * dsc, float cx, float cy, float radius);
301 
302 /**
303  * Set fill radial gradient spread for descriptor
304  * @param dsc pointer to a vector graphic descriptor
305  * @param spread the gradient spread to be set in lv_vector_gradient_spread_t format
306  */
307 void lv_vector_dsc_set_fill_gradient_spread(lv_vector_dsc_t * dsc, lv_vector_gradient_spread_t spread);
308 
309 /**
310  * Set fill gradient color stops for descriptor
311  * @param dsc              pointer to a vector graphic descriptor
312  * @param stops            an array of `lv_gradient_stop_t` variables
313  * @param count            the number of stops in the array, range: 0..LV_GRADIENT_MAX_STOPS
314  */
315 void lv_vector_dsc_set_fill_gradient_color_stops(lv_vector_dsc_t * dsc, const lv_gradient_stop_t * stops,
316                                                  uint16_t count);
317 
318 /**
319  * Set a matrix to current fill transformation matrix
320  * @param dsc              pointer to a vector graphic descriptor
321  * @param matrix           pointer to a matrix
322  */
323 void lv_vector_dsc_set_fill_transform(lv_vector_dsc_t * dsc, const lv_matrix_t * matrix);
324 
325 /**
326  * Set stroke color for descriptor
327  * @param dsc              pointer to a vector graphic descriptor
328  * @param color            the color to be set in lv_color32_t format
329  */
330 void lv_vector_dsc_set_stroke_color32(lv_vector_dsc_t * dsc, lv_color32_t color);
331 
332 /**
333  * Set stroke color for descriptor
334  * @param dsc              pointer to a vector graphic descriptor
335  * @param color            the color to be set in lv_color_t format
336  */
337 void lv_vector_dsc_set_stroke_color(lv_vector_dsc_t * dsc, lv_color_t color);
338 
339 /**
340  * Set stroke opacity for descriptor
341  * @param dsc              pointer to a vector graphic descriptor
342  * @param opa              the opacity to be set in lv_opa_t format
343  */
344 void lv_vector_dsc_set_stroke_opa(lv_vector_dsc_t * dsc, lv_opa_t opa);
345 
346 /**
347  * Set stroke line width for descriptor
348  * @param dsc              pointer to a vector graphic descriptor
349  * @param width            the stroke line width
350  */
351 void lv_vector_dsc_set_stroke_width(lv_vector_dsc_t * dsc, float width);
352 
353 /**
354  * Set stroke line dash pattern for descriptor
355  * @param dsc              pointer to a vector graphic descriptor
356  * @param dash_pattern     an array of values that specify the segments of dash line
357  * @param dash_count       the length of dash pattern array
358  */
359 void lv_vector_dsc_set_stroke_dash(lv_vector_dsc_t * dsc, float * dash_pattern, uint16_t dash_count);
360 
361 /**
362  * Set stroke line cap style for descriptor
363  * @param dsc              pointer to a vector graphic descriptor
364  * @param cap              the line cap to be set in lv_vector_stroke_cap_t format
365  */
366 void lv_vector_dsc_set_stroke_cap(lv_vector_dsc_t * dsc, lv_vector_stroke_cap_t cap);
367 
368 /**
369  * Set stroke line join style for descriptor
370  * @param dsc              pointer to a vector graphic descriptor
371  * @param join             the line join to be set in lv_vector_stroke_join_t format
372  */
373 void lv_vector_dsc_set_stroke_join(lv_vector_dsc_t * dsc, lv_vector_stroke_join_t join);
374 
375 /**
376  * Set stroke miter limit for descriptor
377  * @param dsc              pointer to a vector graphic descriptor
378  * @param miter_limit      the stroke miter_limit
379  */
380 void lv_vector_dsc_set_stroke_miter_limit(lv_vector_dsc_t * dsc, uint16_t miter_limit);
381 
382 /**
383  * Set stroke linear gradient for descriptor
384  * @param dsc              pointer to a vector graphic descriptor
385  * @param x1               the x for start point
386  * @param y1               the y for start point
387  * @param x2               the x for end point
388  * @param y2               the y for end point
389  */
390 void lv_vector_dsc_set_stroke_linear_gradient(lv_vector_dsc_t * dsc, float x1, float y1, float x2, float y2);
391 /**
392  * Set stroke radial gradient for descriptor
393  * @param dsc              pointer to a vector graphic descriptor
394  * @param cx               the x for center of the circle
395  * @param cy               the y for center of the circle
396  * @param radius           the radius for circle
397  */
398 void lv_vector_dsc_set_stroke_radial_gradient(lv_vector_dsc_t * dsc, float cx, float cy, float radius);
399 
400 /**
401  * Set stroke color stops for descriptor
402  * @param dsc              pointer to a vector graphic descriptor
403  * @param spread           the gradient spread to be set in lv_vector_gradient_spread_t format
404  */
405 void lv_vector_dsc_set_stroke_gradient_spread(lv_vector_dsc_t * dsc, lv_vector_gradient_spread_t spread);
406 
407 /**
408  * Set stroke color stops for descriptor
409  * @param dsc              pointer to a vector graphic descriptor
410  * @param stops            an array of `lv_gradient_stop_t` variables
411  * @param count            the number of stops in the array
412  */
413 void lv_vector_dsc_set_stroke_gradient_color_stops(lv_vector_dsc_t * dsc, const lv_gradient_stop_t * stops,
414                                                    uint16_t count);
415 
416 /**
417  * Set a matrix to current stroke transformation matrix
418  * @param dsc              pointer to a vector graphic descriptor
419  * @param matrix           pointer to a matrix
420  */
421 void lv_vector_dsc_set_stroke_transform(lv_vector_dsc_t * dsc, const lv_matrix_t * matrix);
422 
423 /**
424  * Set current transformation matrix to identity matrix
425  * @param dsc           pointer to a vector graphic descriptor
426  */
427 void lv_vector_dsc_identity(lv_vector_dsc_t * dsc);
428 
429 /**
430  * Change the scale factor of current transformation matrix
431  * @param dsc           pointer to a vector graphic descriptor
432  * @param scale_x       the scale factor for the X direction
433  * @param scale_y       the scale factor for the Y direction
434  */
435 void lv_vector_dsc_scale(lv_vector_dsc_t * dsc, float scale_x, float scale_y);
436 
437 /**
438  * Rotate current transformation matrix with origin
439  * @param dsc           pointer to a vector graphic descriptor
440  * @param degree        angle to rotate
441  */
442 void lv_vector_dsc_rotate(lv_vector_dsc_t * dsc, float degree);
443 
444 /**
445  * Translate current transformation matrix to new position
446  * @param dsc           pointer to a vector graphic descriptor
447  * @param tx            the amount of translate in x direction
448  * @param tx            the amount of translate in y direction
449  */
450 void lv_vector_dsc_translate(lv_vector_dsc_t * dsc, float tx, float ty);
451 
452 /**
453  * Change the skew factor of current transformation matrix
454  * @param dsc           pointer to a vector graphic descriptor
455  * @param skew_x        the skew factor for x direction
456  * @param skew_y        the skew factor for y direction
457  */
458 void lv_vector_dsc_skew(lv_vector_dsc_t * dsc, float skew_x, float skew_y);
459 
460 /**
461  * Add a graphic path to the draw list
462  * @param dsc           pointer to a vector graphic descriptor
463  * @param path          pointer to a path
464  */
465 void lv_vector_dsc_add_path(lv_vector_dsc_t * dsc, const lv_vector_path_t * path);
466 
467 /**
468  * Clear a rectangle area use current fill color
469  * @param dsc           pointer to a vector graphic descriptor
470  * @param rect          the area to clear in the buffer
471  */
472 void lv_vector_clear_area(lv_vector_dsc_t * dsc, const lv_area_t * rect);
473 
474 /**
475  * Draw all the vector graphic paths
476  * @param dsc           pointer to a vector graphic descriptor
477  */
478 void lv_draw_vector(lv_vector_dsc_t * dsc);
479 
480 /* Traverser for task list */
481 typedef void (*vector_draw_task_cb)(void * ctx, const lv_vector_path_t * path, const lv_vector_draw_dsc_t * dsc);
482 
483 #endif /* LV_USE_VECTOR_GRAPHIC */
484 
485 #ifdef __cplusplus
486 } /* extern "C" */
487 #endif
488 
489 #endif /* LV_DRAW_VECTOR_H */
490