]> O.S.I.I.S - jp/vkvg.git/commitdiff
dashed straight lines ok, offset should be improved
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 30 Jan 2020 14:21:45 +0000 (15:21 +0100)
committerj-p <jp_bruyere@hotmail.com>
Tue, 5 May 2020 20:14:27 +0000 (22:14 +0200)
src/vkvg_context.c
tests/dashes.c

index b03b33e99d6b31230ba0f0eaacdf4518b40387d7..eba716149dd9e8254ed308bfefb7f8c2813dab5e 100644 (file)
@@ -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;i<ctx->dashCount;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)){
index a72bb3da309d359b488be2e9dacfb76f4606a434..33aa55707a8e37f9cc17332698ef7a93d5f9d0bc 100644 (file)
@@ -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);