From: Jean-Philippe Bruyère Date: Sun, 2 Jan 2022 16:05:37 +0000 (+0100) Subject: vkvg_rounded_rectangle X-Git-Tag: v0.3.0-beta~54 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=b099a0cb89155a5f7097465b170a8ce4113e327e;p=jp%2Fvkvg.git vkvg_rounded_rectangle --- diff --git a/include/vkvg.h b/include/vkvg.h index 131c811..c2744b8 100644 --- a/include/vkvg.h +++ b/include/vkvg.h @@ -1042,7 +1042,8 @@ void vkvg_quadratic_to (VkvgContext ctx, float x1, float y1, float x2, float y2) * @param y2 The Y coordinate of the end point of the curve relative to the current point. */ vkvg_public -void vkvg_rel_quadratic_to (VkvgContext ctx, float x1, float y1, float x2, float y2);/** +void vkvg_rel_quadratic_to (VkvgContext ctx, float x1, float y1, float x2, float y2); +/** * @brief Add an axis aligned rectangle subpath to the current path. * * Adds a closed sub-path rectangle of the given size to the current path at position (x, y). @@ -1055,6 +1056,20 @@ void vkvg_rel_quadratic_to (VkvgContext ctx, float x1, float y1, float x2, float */ vkvg_public vkvg_status_t vkvg_rectangle(VkvgContext ctx, float x, float y, float w, float h); +/** +* @brief Add an axis aligned rectangle with rounded corners to the current path. +* +* Adds a closed sub-path rectangle of the given size to the current path at position (x, y). +* @param ctx The vkvg context pointer. +* @param x The x coordinate of the top left corner of the rectangle to emit. +* @param y The y coordinate of the top left corner of the rectangle to emit. +* @param w The width in pixel of the rectangle to draw. +* @param h The height in pixel of the rectangle to draw. +* @param radius The radius of the corners. +* @return VKVG_STATUS_SUCCESS or VKVG_STATUS_INVALID_RECT if width or height is equal to 0. +*/ +vkvg_public +vkvg_status_t vkvg_rounded_rectangle (VkvgContext ctx, float x, float y, float w, float h, float radius); /** * @brief Stroke command * diff --git a/src/vkvg_context.c b/src/vkvg_context.c index 0421b18..b16d2a4 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -547,6 +547,31 @@ vkvg_status_t vkvg_rectangle (VkvgContext ctx, float x, float y, float w, float _finish_path(ctx); return VKVG_STATUS_SUCCESS; } +vkvg_status_t vkvg_rounded_rectangle (VkvgContext ctx, float x, float y, float w, float h, float radius){ + if (ctx->status) + return ctx->status; + _finish_path (ctx); + + if (w <= 0 || h <= 0) + return VKVG_STATUS_INVALID_RECT; + + if ((radius > w / 2.0f) || (radius > h / 2.0f)) + radius = fmin (w / 2.0f, h / 2.0f); + + vkvg_move_to(ctx, x, y + radius); + vkvg_arc(ctx, x + radius, y + radius, radius, (float)M_PI, (float)-M_PI_2); + vkvg_line_to(ctx, x + w - radius, y); + vkvg_arc(ctx, x + w - radius, y + radius, radius, (float)-M_PI_2, 0); + vkvg_line_to(ctx, x + w, y + h - radius); + vkvg_arc(ctx, x + w - radius, y + h - radius, radius, 0, (float)M_PI_2); + vkvg_line_to(ctx, x + radius, y + h); + vkvg_arc(ctx, x + radius, y + h - radius, radius, (float)M_PI_2, (float)M_PI); + vkvg_line_to(ctx, x, y + radius); + vkvg_close_path(ctx); + + return VKVG_STATUS_SUCCESS; +} + static const VkClearAttachment clearStencil = {VK_IMAGE_ASPECT_STENCIL_BIT, 1, {{{0}}}}; static const VkClearAttachment clearColorAttach = {VK_IMAGE_ASPECT_COLOR_BIT, 0, {{{0}}}};