From 9035800664f66f4bb81d186ca38baa342aa88a9b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Thu, 30 Jan 2020 15:21:45 +0100 Subject: [PATCH] dashed straight lines ok, offset should be improved --- src/vkvg_context.c | 64 +++++++++++++++++++++++++++------------------- tests/dashes.c | 12 ++++++--- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/vkvg_context.c b/src/vkvg_context.c index b03b33e..eba7161 100644 --- a/src/vkvg_context.c +++ b/src/vkvg_context.c @@ -615,7 +615,6 @@ void _draw_stoke_cap (VkvgContext ctx, float hw, vec2 p0, vec2 n, bool isStart) _add_tri_indices_for_rect(ctx, firstIdx); }else{ - vec2_inv (&n); vec2 vhw = vec2_mult(n, hw); if (ctx->lineCap == VKVG_LINE_CAP_SQUARE) @@ -656,8 +655,7 @@ static float curDashOffset = 0.f; //cur dash offset between defined path p static vec2 normal = {0}; void _draw_dashed_segment (VkvgContext ctx, vec2 pL, vec2 p, vec2 pR, float hw) { - Vertex v = {0}; - v.uv.z = -1; + Vertex v = {{0},{0,0,-1}}; if (!dashOn)//we test in fact the next dash start, if dashOn = true => next segment is a void. _build_vb_step (ctx, v, hw, pL, p, pR, false); @@ -672,7 +670,7 @@ void _draw_dashed_segment (VkvgContext ctx, vec2 pL, vec2 p, vec2 pR, float hw) _draw_stoke_cap (ctx, hw, p0, normal, dashOn); dashOn ^= true; - curDashOffset += ctx->dashes[curDash++]; + curDashOffset += ctx->dashes[curDash++] * ctx->lineWidth; if (curDash == ctx->dashCount) curDash = 0; } @@ -695,11 +693,6 @@ void vkvg_stroke_preserve (VkvgContext ctx) while (ptrPath < ctx->pathPtr){ uint32_t ptrCurve = 0; - //used for dashed lines - dashOn = true; - curDash = 0; //current dash index - curDashOffset = 0.f; //cur dash offset between defined path point and last dash segment(on/off) start - //--- VKVG_IBO_INDEX_TYPE firstIdx = (VKVG_IBO_INDEX_TYPE)(ctx->vertCount - ctx->curVertOffset); curPathPointIdx = ctx->pathes[ptrPath]&PATH_ELT_MASK; @@ -738,25 +731,42 @@ void vkvg_stroke_preserve (VkvgContext ctx) } }else{*/ iL = lastPathPointIdx; - if (ctx->dashCount > 0) { - while (curPathPointIdx < lastPathPointIdx){ - iR = curPathPointIdx+1; - _draw_dashed_segment(ctx, ctx->points[iL], ctx->points[curPathPointIdx], ctx->points[iR], hw); - iL = curPathPointIdx++; - } - if (_path_is_closed(ctx,ptrPath)){ - iR = ctx->pathes[ptrPath] & PATH_ELT_MASK; - _draw_dashed_segment(ctx, ctx->points[iL++], ctx->points[curPathPointIdx++], ctx->points[iR], hw); - } - if (!dashOn) - _draw_stoke_cap (ctx, hw, ctx->points[curPathPointIdx], normal, false); - } else { - while (curPathPointIdx < lastPathPointIdx){ - iR = curPathPointIdx+1; - _build_vb_step (ctx, v, hw, ctx->points[iL], ctx->points[curPathPointIdx], ctx->points[iR], false); - iL = curPathPointIdx++; - } + if (ctx->dashCount > 0) { + dashOn = true; + curDash = 0; //current dash index + float totDashLength = 0; + for (uint32_t i=0;idashCount;i++) + totDashLength+=ctx->dashes[i]; + totDashLength*=ctx->lineWidth; + if (EQUF(ctx->dashOffset, 0)||EQUF(totDashLength, 0)) + curDashOffset = 0; + else + curDashOffset = fmodf(ctx->dashOffset, totDashLength); //cur dash offset between defined path point and last dash segment(on/off) start + + while (curPathPointIdx < lastPathPointIdx){ + iR = curPathPointIdx+1; + _draw_dashed_segment(ctx, ctx->points[iL], ctx->points[curPathPointIdx], ctx->points[iR], hw); + iL = curPathPointIdx++; + } + if (_path_is_closed(ctx,ptrPath)){ + iR = ctx->pathes[ptrPath] & PATH_ELT_MASK; + _draw_dashed_segment(ctx, ctx->points[iL++], ctx->points[curPathPointIdx++], ctx->points[iR], hw); } + if (!dashOn){ + uint32_t prevDash = curDash-1; + if (prevDash < 0) + curDash = ctx->dashCount-1; + float m = fminf (ctx->dashes[prevDash] * ctx->lineWidth - curDashOffset, ctx->dashes[curDash] * ctx->lineWidth); + vec2 p = vec2_sub(ctx->points[iR], vec2_mult(normal, m)); + _draw_stoke_cap (ctx, hw, p, normal, false); + } + } else { + while (curPathPointIdx < lastPathPointIdx){ + iR = curPathPointIdx+1; + _build_vb_step (ctx, v, hw, ctx->points[iL], ctx->points[curPathPointIdx], ctx->points[iR], false); + iL = curPathPointIdx++; + } + } //} /*if (_path_is_closed(ctx,ptrPath)){ diff --git a/tests/dashes.c b/tests/dashes.c index a72bb3d..33aa557 100644 --- a/tests/dashes.c +++ b/tests/dashes.c @@ -1,12 +1,15 @@ #include "test.h" - +static float offset = 0; void test(){ + offset += 0.1f; vkvg_surface_clear(surf); VkvgContext ctx = vkvg_create(surf); - const float dashes[] = {60.0f, 40}; + //const float dashes[] = {160.0f, 80}; + //const float dashes[] = {160.0f, 30}; + const float dashes[] = {5, 2}; vkvg_set_line_cap(ctx, VKVG_LINE_CAP_ROUND); - vkvg_set_dash(ctx, dashes, 2, 0.f); + vkvg_set_dash(ctx, dashes, 2, offset); vkvg_set_line_width(ctx, 20); vkvg_set_source_rgb(ctx, 0, 0, 1); @@ -16,12 +19,13 @@ void test(){ vkvg_rel_line_to (ctx, 200, 0); vkvg_rel_line_to (ctx, 0, 200); vkvg_rel_line_to (ctx, -700, 0); + vkvg_close_path(ctx); vkvg_stroke (ctx); vkvg_destroy(ctx); } -int main(int argc, char *argv[]) { +int main() { perform_test (test, 1024, 768); -- 2.47.3