From de0814fec20af57c79d549ae01e5b31c3c287071 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Wed, 29 Dec 2021 18:18:55 +0100 Subject: [PATCH] removed pattern status, commands return status instead; vkvg_pattern_get_color_stop_count and vkvg_pattern_get_color_stop_rgba implementation --- include/vkvg.h | 72 ++++++++++++++++++++++++++++++++++++++-------- src/vkvg_pattern.c | 70 +++++++++++++++++++++++--------------------- src/vkvg_pattern.h | 1 - 3 files changed, 98 insertions(+), 45 deletions(-) diff --git a/include/vkvg.h b/include/vkvg.h index da8bc6a..53b9433 100644 --- a/include/vkvg.h +++ b/include/vkvg.h @@ -108,6 +108,7 @@ typedef enum { VKVG_STATUS_NO_CURRENT_POINT, /*!< path command expecting a current point to be defined failed*/ VKVG_STATUS_INVALID_MATRIX, /*!< invalid matrix (not invertible)*/ VKVG_STATUS_INVALID_STATUS, /*!< */ + VKVG_STATUS_INVALID_INDEX, /*!< */ VKVG_STATUS_NULL_POINTER, /*!< NULL pointer*/ VKVG_STATUS_INVALID_STRING, /*!< */ VKVG_STATUS_INVALID_PATH_DATA, /*!< */ @@ -1045,42 +1046,56 @@ void vkvg_rel_quadratic_to (VkvgContext ctx, float x1, float y1, float x2, float vkvg_public void vkvg_rectangle(VkvgContext ctx, float x, float y, float w, float h); /** - * @brief + * @brief Stroke command + * + * Perform a stroke of the current path and reset it. * * @param ctx a valid vkvg @ref context */ vkvg_public void vkvg_stroke (VkvgContext ctx); /** - * @brief + * @brief Stroke command that preserve current path. + * + * Perform a stroke of the current path and preserve it after the operation. * * @param ctx a valid vkvg @ref context */ vkvg_public void vkvg_stroke_preserve (VkvgContext ctx); /** - * @brief + * @brief Fill command + * + * Perform the filling of the current path. If no path is defined, this command has no effect. + * The current path is reseted after this operation. * * @param ctx a valid vkvg @ref context */ vkvg_public void vkvg_fill (VkvgContext ctx); /** - * @brief + * @brief Fill command that preserve current path. + * + * Same as @ref vkvg_fill, but don't reset the current path after the operation. * * @param ctx a valid vkvg @ref context */ vkvg_public void vkvg_fill_preserve (VkvgContext ctx); /** - * @brief + * @brief Paint command. + * + * perform a fill operation on the current path or on the full surface if no path is defined with + * the currently active pattern. * * @param ctx a valid vkvg @ref context */ vkvg_public void vkvg_paint (VkvgContext ctx); /** - * @brief + * @brief clear surface + * + * Color and clipping are reset to 0. * * @param ctx a valid vkvg @ref context */ @@ -1498,16 +1513,16 @@ VkvgPattern vkvg_pattern_create_linear (float x0, float y0, float x1, float y1); /** * @brief edit an existing linear gradient. * - * edit control points of an existing linear gradient. If supplied pattern is not a linear gradient, - * @ref VKVG_STATUS_PATTERN_TYPE_MISMATCH is set for pattern. + * edit control points of an existing linear gradient. * * @param x0 x coordinate of the start point * @param y0 y coordinate of the start point * @param x1 x coordinate of the end point * @param y1 y coordinate of the end point + * @return VKVG_STATUS_SUCCESS, or VKVG_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a linear gradient. */ vkvg_public -void vkvg_pattern_edit_linear (VkvgPattern pat, float x0, float y0, float x1, float y1); +vkvg_status_t vkvg_pattern_edit_linear(VkvgPattern pat, float x0, float y0, float x1, float y1); /** * @brief get the gradient end points for a linear gradient * @@ -1517,9 +1532,10 @@ void vkvg_pattern_edit_linear (VkvgPattern pat, float x0, float y0, float x1, fl * @param y0 y coordinate of the start point * @param x1 x coordinate of the end point * @param y1 y coordinate of the end point + * @return VKVG_STATUS_SUCCESS, or VKVG_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a linear gradient. */ vkvg_public -void vkvg_pattern_get_linear_points (VkvgPattern pat, float* x0, float* y0, float* x1, float* y1); +vkvg_status_t vkvg_pattern_get_linear_points(VkvgPattern pat, float* x0, float* y0, float* x1, float* y1); /** * @brief create a new radial gradient. * @@ -1549,11 +1565,42 @@ VkvgPattern vkvg_pattern_create_radial (float cx0, float cy0, float radius0, * @param cx1 x coordinate for the center of the end circle, the outer circle. * @param cy1 y coordinate for the center of the end circle, the outer circle. * @param radius1 radius for the center of the end circle, the outer circle. + * @return VKVG_STATUS_SUCCESS, or VKVG_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a radial gradient. */ vkvg_public -void vkvg_pattern_edit_radial (VkvgPattern pat, +vkvg_status_t vkvg_pattern_edit_radial(VkvgPattern pat, float cx0, float cy0, float radius0, float cx1, float cy1, float radius1); +/** + * @brief get color stop count. + * + * Retrieve the color stop count of a gradient pattern. + * + * @param pat a valid pattern pointer. + * @param count a valid integer pointer to old the current stop count returned. + * @return VKVG_STATUS_SUCCESS, or VKVG_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a gradient. + */ +vkvg_public +vkvg_status_t vkvg_pattern_get_color_stop_count (VkvgPattern pat, uint32_t* count); +/** + * @brief get color stop. + * + * Gets the color and offset information at the given index for a gradient pattern. Values of index range from 0 to n-1 where n is the number + * returned by @ref vkvg_pattern_get_color_stop_count(). + * + * @param pat a valid pattern pointer. + * @param index index of the stop to return data for. + * @param offset a valid float pointer to old the stop offset. + * @param r a valid float pointer to old the red component. + * @param g a valid float pointer to old the green component. + * @param b a valid float pointer to old the blue component. + * @param a a valid float pointer to old the alpha component. + * @return VKVG_STATUS_SUCCESS, VKVG_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a gradient, VKVG_STATUS_INVALID_INDEX if index is out of bounds. + */ +vkvg_public +vkvg_status_t vkvg_pattern_get_color_stop_rgba (VkvgPattern pat, uint32_t index, + float* offset, float* r, float* g, float* b, float* a); + /** * @brief dispose pattern. * @@ -1577,7 +1624,7 @@ void vkvg_pattern_destroy (VkvgPattern pat); * @param a the alpha chanel of the color stop */ vkvg_public -void vkvg_pattern_add_color_stop (VkvgPattern pat, float offset, float r, float g, float b, float a); +vkvg_status_t vkvg_pattern_add_color_stop(VkvgPattern pat, float offset, float r, float g, float b, float a); /** * @brief control the extend of the pattern * @@ -1585,6 +1632,7 @@ void vkvg_pattern_add_color_stop (VkvgPattern pat, float offset, float r, float * * @param pat the pattern to set extend for. * @param extend one value of the @ref vkvg_extend_t enumeration. + * @return VKVG_STATUS_SUCCESS, or VKVG_STATUS_PATTERN_TYPE_MISMATCH if the pattern is not a gradient. */ vkvg_public void vkvg_pattern_set_extend (VkvgPattern pat, vkvg_extend_t extend); diff --git a/src/vkvg_pattern.c b/src/vkvg_pattern.c index 3460b00..8060122 100644 --- a/src/vkvg_pattern.c +++ b/src/vkvg_pattern.c @@ -36,33 +36,26 @@ VkvgPattern vkvg_pattern_create_for_surface (VkvgSurface surf){ return pat; } -void vkvg_pattern_get_linear_points (VkvgPattern pat, float* x0, float* y0, float* x1, float* y1) { - if (pat->status) - return; +vkvg_status_t vkvg_pattern_get_linear_points (VkvgPattern pat, float* x0, float* y0, float* x1, float* y1) { + if (pat->type != VKVG_PATTERN_TYPE_LINEAR) + return VKVG_STATUS_PATTERN_TYPE_MISMATCH; - if (pat->type != VKVG_PATTERN_TYPE_LINEAR) { - pat->status = VKVG_STATUS_PATTERN_TYPE_MISMATCH; - return; - } vkvg_gradient_t* grad = (vkvg_gradient_t*)pat->data; *x0 = grad->cp[0].x; *y0 = grad->cp[0].y; *x1 = grad->cp[0].z; *y1 = grad->cp[0].w; + return VKVG_STATUS_SUCCESS; } -void vkvg_pattern_edit_linear (VkvgPattern pat, float x0, float y0, float x1, float y1){ - if (pat->status) - return; - - if (pat->type != VKVG_PATTERN_TYPE_LINEAR) { - pat->status = VKVG_STATUS_PATTERN_TYPE_MISMATCH; - return; - } +vkvg_status_t vkvg_pattern_edit_linear (VkvgPattern pat, float x0, float y0, float x1, float y1){ + if (pat->type != VKVG_PATTERN_TYPE_LINEAR) + return VKVG_STATUS_PATTERN_TYPE_MISMATCH; vkvg_gradient_t* grad = (vkvg_gradient_t*)pat->data; grad->cp[0] = (vec4){{x0}, {y0}, {x1}, {y1}}; + return VKVG_STATUS_SUCCESS; } VkvgPattern vkvg_pattern_create_linear (float x0, float y0, float x1, float y1){ VkvgPattern pat = (vkvg_pattern_t*)calloc(1, sizeof(vkvg_pattern_t)); @@ -73,8 +66,8 @@ VkvgPattern vkvg_pattern_create_linear (float x0, float y0, float x1, float y1){ pat->data = (void*)calloc(1,sizeof(vkvg_gradient_t)); if (!pat->data) { - pat->status = VKVG_STATUS_NO_MEMORY; - return pat; + free (pat); + return NULL; } vkvg_pattern_edit_linear(pat, x0, y0, x1, y1); @@ -83,16 +76,11 @@ VkvgPattern vkvg_pattern_create_linear (float x0, float y0, float x1, float y1){ return pat; } -void vkvg_pattern_edit_radial (VkvgPattern pat, +vkvg_status_t vkvg_pattern_edit_radial (VkvgPattern pat, float cx0, float cy0, float radius0, float cx1, float cy1, float radius1) { - if (pat->status) - return; - - if (pat->type != VKVG_PATTERN_TYPE_RADIAL) { - pat->status = VKVG_STATUS_PATTERN_TYPE_MISMATCH; - return; - } + if (pat->type != VKVG_PATTERN_TYPE_RADIAL) + return VKVG_STATUS_PATTERN_TYPE_MISMATCH; vkvg_gradient_t* grad = (vkvg_gradient_t*)pat->data; @@ -110,6 +98,7 @@ void vkvg_pattern_edit_radial (VkvgPattern pat, grad->cp[0] = (vec4){{c0.x}, {c0.y},{radius0},{0}}; grad->cp[1] = (vec4){{c1.x}, {c1.y},{radius1},{0}}; + return VKVG_STATUS_SUCCESS; } VkvgPattern vkvg_pattern_create_radial (float cx0, float cy0, float radius0, float cx1, float cy1, float radius1){ @@ -121,8 +110,8 @@ VkvgPattern vkvg_pattern_create_radial (float cx0, float cy0, float radius0, pat->data = (void*)calloc(1,sizeof(vkvg_gradient_t)); if (!pat->data) { - pat->status = VKVG_STATUS_NO_MEMORY; - return pat; + free (pat); + return NULL; } vkvg_pattern_edit_radial (pat, cx0, cy0, radius0, cx1, cy1, radius1); @@ -131,9 +120,6 @@ VkvgPattern vkvg_pattern_create_radial (float cx0, float cy0, float radius0, return pat; } -vkvg_status_t vkvg_pattern_status (VkvgPattern pat) { - return pat->status; -} VkvgPattern vkvg_pattern_reference (VkvgPattern pat) { pat->references++; return pat; @@ -141,9 +127,9 @@ VkvgPattern vkvg_pattern_reference (VkvgPattern pat) { uint32_t vkvg_pattern_get_reference_count (VkvgPattern pat) { return pat->references; } -void vkvg_pattern_add_color_stop (VkvgPattern pat, float offset, float r, float g, float b, float a) { +vkvg_status_t vkvg_pattern_add_color_stop (VkvgPattern pat, float offset, float r, float g, float b, float a) { if (pat->type == VKVG_PATTERN_TYPE_SURFACE || pat->type == VKVG_PATTERN_TYPE_SOLID) - return; + return VKVG_STATUS_PATTERN_TYPE_MISMATCH; vkvg_gradient_t* grad = (vkvg_gradient_t*)pat->data; vkvg_color_t c = {r,g,b,a}; @@ -167,6 +153,26 @@ vkvg_filter_t vkvg_pattern_get_filter (VkvgPattern pat){ vkvg_pattern_type_t vkvg_pattern_get_type (VkvgPattern pat){ return pat->type; } +vkvg_status_t vkvg_pattern_get_color_stop_count (VkvgPattern pat, uint32_t* count) { + if (pat->type == VKVG_PATTERN_TYPE_SURFACE || pat->type == VKVG_PATTERN_TYPE_SOLID) + return VKVG_STATUS_PATTERN_TYPE_MISMATCH; + vkvg_gradient_t* grad = (vkvg_gradient_t*)pat->data; + *count = grad->count; + return VKVG_STATUS_SUCCESS; +} +vkvg_status_t vkvg_pattern_get_color_stop_rgba (VkvgPattern pat, uint32_t index, + float* offset, float* r, float* g, float* b, float* a) { + if (pat->type == VKVG_PATTERN_TYPE_SURFACE || pat->type == VKVG_PATTERN_TYPE_SOLID) + return VKVG_STATUS_PATTERN_TYPE_MISMATCH; + vkvg_gradient_t* grad = (vkvg_gradient_t*)pat->data; + *offset = grad->stops[index]; + vkvg_color_t c = grad->colors[index]; + *r = c.r; + *g = c.g; + *b = c.b; + *a = c.a; + return VKVG_STATUS_SUCCESS; +} void vkvg_pattern_destroy(VkvgPattern pat) { diff --git a/src/vkvg_pattern.h b/src/vkvg_pattern.h index 3188d9e..e437030 100644 --- a/src/vkvg_pattern.h +++ b/src/vkvg_pattern.h @@ -32,7 +32,6 @@ typedef struct _vkvg_pattern_t { vkvg_filter_t filter; void* data; uint32_t references; - vkvg_status_t status; }vkvg_pattern_t; typedef struct _vkvg_gradient_t { -- 2.47.3