From 611b043fff03e996e8c99f4afd5819f1dbf02d4f Mon Sep 17 00:00:00 2001 From: Mikko Mononen Date: Fri, 23 May 2014 09:37:36 +0300 Subject: [PATCH] Fix for issue #77 - line caps did not consider AA width --- example/demo.c | 36 +++++++++++++++++++++++++++++++++++- src/nanovg.c | 16 ++++++++-------- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/example/demo.c b/example/demo.c index dda0c9d..5eeebde 100644 --- a/example/demo.c +++ b/example/demo.c @@ -936,6 +936,37 @@ void drawWidths(struct NVGcontext* vg, float x, float y, float width) nvgRestore(vg); } +void drawCaps(struct NVGcontext* vg, float x, float y, float width) +{ + int i; + int caps[3] = {NVG_BUTT, NVG_ROUND, NVG_SQUARE}; + float lineWidth = 8.0f; + + nvgSave(vg); + + nvgBeginPath(vg); + nvgRect(vg, x-lineWidth/2, y, width+lineWidth, 40); + nvgFillColor(vg, nvgRGBA(255,255,255,32)); + nvgFill(vg); + + nvgBeginPath(vg); + nvgRect(vg, x, y, width, 40); + nvgFillColor(vg, nvgRGBA(255,255,255,32)); + nvgFill(vg); + + nvgStrokeWidth(vg, lineWidth); + for (i = 0; i < 3; i++) { + nvgLineCap(vg, caps[i]); + nvgStrokeColor(vg, nvgRGBA(0,0,0,255)); + nvgBeginPath(vg); + nvgMoveTo(vg, x, y + i*10 + 5); + nvgLineTo(vg, x+width, y + i*10 + 5); + nvgStroke(vg); + } + + nvgRestore(vg); +} + void renderDemo(struct NVGcontext* vg, float mx, float my, float width, float height, float t, int blowup, struct DemoData* data) { @@ -949,9 +980,12 @@ void renderDemo(struct NVGcontext* vg, float mx, float my, float width, float he // Line joints drawLines(vg, 50, height-50, 600, 50, t); - // Line width; + // Line caps drawWidths(vg, 10, 50, 30); + // Line caps + drawCaps(vg, 10, 300, 30); + nvgSave(vg); if (blowup) { nvgRotate(vg, sinf(t*0.3f)*5.0f/180.0f*NVG_PI); diff --git a/src/nanovg.c b/src/nanovg.c index aec8ef9..f8609b8 100644 --- a/src/nanovg.c +++ b/src/nanovg.c @@ -1564,11 +1564,11 @@ static int nvg__expandStrokeAndFill(struct NVGcontext* ctx, int feats, float w, dly = -dx; if (lineCap == NVG_BUTT || lineCap == NVG_SQUARE) { if (lineCap == NVG_BUTT) { - px = p0->x; - py = p0->y; + px = p0->x + dx*ctx->fringeWidth*0.5f; + py = p0->y + dy*ctx->fringeWidth*0.5f; } else /*if (lineCap == NVG_SQUARE)*/ { - px = p0->x - dx*w; - py = p0->y - dy*w; + px = p0->x - dx*(w - ctx->fringeWidth); + py = p0->y - dy*(w - ctx->fringeWidth); } nvg__vset(dst, px + dlx*lw - dx*aa, py + dly*lw - dy*aa, lu,0); dst++; nvg__vset(dst, px - dlx*rw - dx*aa, py - dly*rw - dy*aa, ru,0); dst++; @@ -1616,11 +1616,11 @@ static int nvg__expandStrokeAndFill(struct NVGcontext* ctx, int feats, float w, dly = -dx; if (lineCap == NVG_BUTT || lineCap == NVG_SQUARE) { if (lineCap == NVG_BUTT) { - px = p1->x; - py = p1->y; + px = p1->x - dx*ctx->fringeWidth*0.5f; + py = p1->y - dy*ctx->fringeWidth*0.5f; } else /*if (lineCap == NVG_SQUARE)*/ { - px = p1->x + dx*w; - py = p1->y + dy*w; + px = p1->x + dx*(w - ctx->fringeWidth); + py = p1->y + dy*(w - ctx->fringeWidth); } nvg__vset(dst, px + dlx*lw, py + dly * lw, lu,1); dst++; nvg__vset(dst, px - dlx*rw, py - dly * rw, ru,1); dst++;