From 2dc3dba97be275f7647016855c301ff2a4b50001 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 7 Sep 2015 10:13:15 -0400 Subject: [PATCH] Set up a new way to specify the color used to perform a stroke or fill. GDI restrictions necessitate this. --- gtkarea/draw.c | 23 +++++------------------ gtkarea/main.c | 13 +++++++++++-- gtkarea/ui.h | 12 ++++-------- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/gtkarea/draw.c b/gtkarea/draw.c index 7a10460b..50cd88f6 100644 --- a/gtkarea/draw.c +++ b/gtkarea/draw.c @@ -3,7 +3,6 @@ struct uiDrawContext { cairo_t *cr; - gboolean hasPath; }; uiDrawContext *newContext(cairo_t *cr) @@ -12,46 +11,35 @@ uiDrawContext *newContext(cairo_t *cr) c = (uiDrawContext *) g_malloc0(sizeof (uiDrawContext)); c->cr = cr; - c->hasPath = FALSE; return c; } -static void prepPath(uiDrawContext *c) +void uiDrawBeginPathRGB(uiDrawContext *c, uint8_t r, uint8_t g, uint8_t b) { - if (c->hasPath) - return; + cairo_set_source_rgb(c->cr, + ((double) r) / 255, + ((double) g) / 255, + ((double) b) / 255); cairo_new_path(c->cr); - c->hasPath = TRUE; } void uiDrawMoveTo(uiDrawContext *c, intmax_t x, intmax_t y) { - prepPath(c); cairo_move_to(c->cr, (double) x + 0.5, (double) y + 0.5); } void uiDrawLineTo(uiDrawContext *c, intmax_t x, intmax_t y) { - prepPath(c); cairo_line_to(c->cr, (double) x + 0.5, (double) y + 0.5); } void uiDrawCloseFigure(uiDrawContext *c) { - prepPath(c); cairo_close_path(c->cr); } -#define R(c) (((c) >> 16) & 0xFF) -#define G(c) (((c) >> 8) & 0xFF) -#define B(c) ((c) & 0xFF) - void uiDrawStroke(uiDrawContext *c, uiDrawStrokeParams *p) { - cairo_set_source_rgb(c->cr, - ((double) R(p->RGB)) / 255, - ((double) G(p->RGB)) / 255, - ((double) B(p->RGB)) / 255); switch (p->Cap) { case uiDrawLineCapFlat: cairo_set_line_cap(c->cr, CAIRO_LINE_CAP_BUTT); @@ -78,5 +66,4 @@ void uiDrawStroke(uiDrawContext *c, uiDrawStrokeParams *p) // TODO comment the /2 here cairo_set_line_width(c->cr, ((double) p->Thickness) / 2); cairo_stroke(c->cr); - c->hasPath = FALSE; } diff --git a/gtkarea/main.c b/gtkarea/main.c index 6d43c190..0940c918 100644 --- a/gtkarea/main.c +++ b/gtkarea/main.c @@ -16,25 +16,34 @@ static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p) { uiDrawStrokeParams sp; + uiDrawBeginPathRGB(p->Context, 0xFF, 0x00, 0x00); uiDrawMoveTo(p->Context, p->ClipX + 5, p->ClipY + 5); uiDrawLineTo(p->Context, (p->ClipX + p->ClipWidth) - 5, (p->ClipY + p->ClipHeight) - 5); - sp.RGB = 0xFF0000; sp.Cap = uiDrawLineCapFlat; sp.Join = uiDrawLineJoinMiter; sp.Thickness = 1; sp.MiterLimit = uiDrawDefaultMiterLimit; uiDrawStroke(p->Context, &sp); + uiDrawBeginPathRGB(p->Context, 0x00, 0x00, 0x80); uiDrawMoveTo(p->Context, p->ClipX, p->ClipY); uiDrawLineTo(p->Context, p->ClipX + p->ClipWidth, p->ClipY); uiDrawLineTo(p->Context, 50, 150); uiDrawLineTo(p->Context, 50, 50); uiDrawCloseFigure(p->Context); - sp.RGB = 0x000080; sp.Cap = uiDrawLineCapFlat; sp.Join = uiDrawLineJoinRound; sp.Thickness = 5; uiDrawStroke(p->Context, &sp); + + uiDrawBeginPathRGB(p->Context, 0x00, 0x80, 0x00); + uiDrawMoveTo(p->Context, 5, 10); + uiDrawLineTo(p->Context, 5, 50); + sp.Cap = uiDrawLineCapFlat; + sp.Join = uiDrawLineJoinMiter; + sp.Thickness = 1; + sp.MiterLimit = uiDrawDefaultMiterLimit; + uiDrawStroke(p->Context, &sp); } static uintmax_t handlerHScrollMax(uiAreaHandler *a, uiArea *area) diff --git a/gtkarea/ui.h b/gtkarea/ui.h index 0a45f187..14c261d2 100644 --- a/gtkarea/ui.h +++ b/gtkarea/ui.h @@ -34,13 +34,10 @@ struct uiAreaDrawParams { // TODO dotting/dashing typedef struct uiDrawStrokeParams uiDrawStrokeParams; -typedef struct uiDrawFillParams uiDrawFillParams; typedef enum uiDrawLineCap uiDrawLineCap; typedef enum uiDrawLineJoin uiDrawLineJoin; typedef enum uiDrawFillMode uiDrawFillMode; -typedef uint32_t uiRGB; - enum uiDrawLineCap { uiDrawLineCapFlat, uiDrawLineCapRound, @@ -67,7 +64,6 @@ enum uiDrawFillMode { }; struct uiDrawStrokeParams { - uiRGB RGB; uiDrawLineCap Cap; uiDrawLineJoin Join; intmax_t Thickness; @@ -75,15 +71,15 @@ struct uiDrawStrokeParams { double MiterLimit; }; -struct uiDrawFillParams { - uiRGB RGB; - uiDrawFillMode FillMode; -}; +void uiDrawBeginPathRGB(uiDrawContext *, uint8_t, uint8_t, uint8_t); +//TODO void uiDrawBeginPathRGBA(uiDrawContext *, uint8_t, uint8_t, uint8_t, uint8_t); void uiDrawMoveTo(uiDrawContext *, intmax_t, intmax_t); void uiDrawLineTo(uiDrawContext *, intmax_t, intmax_t); void uiDrawCloseFigure(uiDrawContext *); + void uiDrawStroke(uiDrawContext *, uiDrawStrokeParams *); +//TODO void uiDrawFill(uiDrawContext *, uiDrawFillMode); // path functions // cairo gdi core graphics