]> O.S.I.I.S - jp/vkvg.git/commitdiff
vkvg_rounded_rectangle
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sun, 2 Jan 2022 16:05:37 +0000 (17:05 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sun, 2 Jan 2022 18:51:30 +0000 (19:51 +0100)
include/vkvg.h
src/vkvg_context.c

index 131c8110eefc6314244b60a61aa99bd5211370ce..c2744b85fe640ec854c47414ddcf0c10aecc4392 100644 (file)
@@ -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
  *
index 0421b1887b3b0d78e3d5ced1503260853f6f3028..b16d2a485ed53182bb2bdadd7f80664392ff1214 100644 (file)
@@ -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}}}};