From cc827919b8b997e32d97e6b72cc1d7964789f283 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 13 Oct 2015 11:16:06 -0400 Subject: [PATCH] Added uiDrawClip() and implemented it on GTK+. --- test/drawtests.c | 42 +++++++++++++++++++++++++++++++++++++++++- ui.h | 3 ++- unix/draw.c | 14 ++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/test/drawtests.c b/test/drawtests.c index f601a24b..07f6f404 100644 --- a/test/drawtests.c +++ b/test/drawtests.c @@ -1299,7 +1299,46 @@ static void drawCSArcNegative(uiAreaDrawParams *p) uiDrawFreePath(path); } -// TODO clip +// clip +static void drawCSClip(uiAreaDrawParams *p) +{ + uiDrawBrush source; + uiDrawStrokeParams sp; + uiDrawPath *path; + + crsourcergba(&source, 0, 0, 0, 1); + sp.Cap = uiDrawLineCapFlat; + sp.Join = uiDrawLineJoinMiter; + sp.MiterLimit = uiDrawDefaultMiterLimit; + + path = uiDrawNewPath(uiDrawFillModeWinding); + + uiDrawPathNewFigureWithArc(path, + 128.0, 128.0, + 76.8, + 0, 2 * M_PI, + 0); + uiDrawPathEnd(path); + uiDrawClip(p->Context, path); + uiDrawFreePath(path); + + path = uiDrawNewPath(uiDrawFillModeWinding); + uiDrawPathAddRectangle(path, 0, 0, 256, 256); + uiDrawPathEnd(path); + uiDrawFill(p->Context, path, &source); + uiDrawFreePath(path); + + crsourcergba(&source, 0, 1, 0, 1); + path = uiDrawNewPath(uiDrawFillModeWinding); + uiDrawPathNewFigure(path, 0, 0); + uiDrawPathLineTo(path, 256, 256); + uiDrawPathNewFigure(path, 256, 0); + uiDrawPathLineTo(path, 0, 256); + uiDrawPathEnd(path); + sp.Thickness = 10.0; + uiDrawStroke(p->Context, path, &source, &sp); + uiDrawFreePath(path); +} // TODO clip image @@ -1772,6 +1811,7 @@ static const struct drawtest tests[] = { { "Direct2D: How to Draw and Fill a Complex Shape", drawD2DComplexShape }, { "cairo samples: arc", drawCSArc }, { "cairo samples: arc negative", drawCSArcNegative }, + { "cairo samples: clip", drawCSClip }, { "cairo samples: curve rectangle", drawCSCurveRectangle }, { "cairo samples: curve to", drawCSCurveTo }, { "cairo samples: fill and stroke2", drawCSFillAndStroke2 }, diff --git a/ui.h b/ui.h index 2c4916e2..9cebd73d 100644 --- a/ui.h +++ b/ui.h @@ -425,7 +425,8 @@ _UI_EXTERN void uiDrawMatrixTransformSize(uiDrawMatrix *m, double *x, double *y) _UI_EXTERN void uiDrawTransform(uiDrawContext *c, uiDrawMatrix *m); -// TODO put uiDrawClip here +// TODO add a uiDrawPathStrokeToFill() or something like that +_UI_EXTERN void uiDrawClip(uiDrawContext *c, uiDrawPath *path); _UI_EXTERN void uiDrawSave(uiDrawContext *c); _UI_EXTERN void uiDrawRestore(uiDrawContext *c); diff --git a/unix/draw.c b/unix/draw.c index 677b1ccd..5cf1e546 100644 --- a/unix/draw.c +++ b/unix/draw.c @@ -422,6 +422,20 @@ void uiDrawTransform(uiDrawContext *c, uiDrawMatrix *m) cairo_transform(c->cr, &cm); } +void uiDrawClip(uiDrawContext *c, uiDrawPath *path) +{ + runPath(path, c->cr); + switch (path->fillMode) { + case uiDrawFillModeWinding: + cairo_set_fill_rule(c->cr, CAIRO_FILL_RULE_WINDING); + break; + case uiDrawFillModeAlternate: + cairo_set_fill_rule(c->cr, CAIRO_FILL_RULE_EVEN_ODD); + break; + } + cairo_clip(c->cr); +} + void uiDrawSave(uiDrawContext *c) { cairo_save(c->cr);