diff options
| author | Tavmjong Bah <tavmjong@free.fr> | 2018-12-11 13:23:47 +0000 |
|---|---|---|
| committer | Tavmjong Bah <tavmjong@free.fr> | 2018-12-11 13:23:47 +0000 |
| commit | bf7925c4fde9cb28f5ae432478fd1be6a5bb8727 (patch) | |
| tree | 45657c1b83b60d7f4b8f13a68e4e3875239c7709 /src | |
| parent | Turn #defines into enum. (diff) | |
| download | inkscape-bf7925c4fde9cb28f5ae432478fd1be6a5bb8727.tar.gz inkscape-bf7925c4fde9cb28f5ae432478fd1be6a5bb8727.zip | |
Reduce code redundancy.
Diffstat (limited to 'src')
| -rw-r--r-- | src/display/nr-style.cpp | 127 | ||||
| -rw-r--r-- | src/display/nr-style.h | 26 |
2 files changed, 53 insertions, 100 deletions
diff --git a/src/display/nr-style.cpp b/src/display/nr-style.cpp index b25cc80c6..8acc14421 100644 --- a/src/display/nr-style.cpp +++ b/src/display/nr-style.cpp @@ -342,93 +342,69 @@ void NRStyle::set(SPStyle *style, SPStyle *context_style) update(); } -bool NRStyle::prepareFill(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern) +cairo_pattern_t* NRStyle::preparePaint(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern, Paint& paint) { - // update fill pattern - if (!fill_pattern) { - switch (fill.type) { + cairo_pattern_t* cpattern = nullptr; + + switch (paint.type) { case PAINT_SERVER: if (pattern) { - fill_pattern = pattern->renderPattern(fill.opacity); + cpattern = pattern->renderPattern(paint.opacity); } else { - fill_pattern = fill.server->pattern_new(dc.raw(), paintbox, fill.opacity); + cpattern = paint.server->pattern_new(dc.raw(), paintbox, paint.opacity); } break; case PAINT_COLOR: { - SPColor const &c = fill.color; - fill_pattern = cairo_pattern_create_rgba( - c.v.c[0], c.v.c[1], c.v.c[2], fill.opacity); - } + SPColor const &c = paint.color; + cpattern = cairo_pattern_create_rgba( + c.v.c[0], c.v.c[1], c.v.c[2], paint.opacity); + double red = 0; + double green = 0; + double blue = 0; + double alpha = 0; + cairo_pattern_get_rgba(cpattern, &red, &green, &blue, &alpha); + } break; default: break; - } } - if (!fill_pattern) return false; - return true; + return cpattern; } -void NRStyle::applyFill(Inkscape::DrawingContext &dc) +bool NRStyle::prepareFill(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern) { - dc.setSource(fill_pattern); - dc.setFillRule(fill_rule); + if (!fill_pattern) fill_pattern = preparePaint(dc, paintbox, pattern, fill); + return fill_pattern != nullptr; +} + +bool NRStyle::prepareStroke(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern) +{ + if (!stroke_pattern) stroke_pattern = preparePaint(dc, paintbox, pattern, stroke); + return stroke_pattern != nullptr; } bool NRStyle::prepareTextDecorationFill(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern) { - // update text decoration pattern - if (!text_decoration_fill_pattern) { - switch (text_decoration_fill.type) { - case PAINT_SERVER: - if (pattern) { - text_decoration_fill_pattern = pattern->renderPattern( - text_decoration_fill.opacity); - } else { - text_decoration_fill_pattern = text_decoration_fill.server->pattern_new(dc.raw(), - paintbox, text_decoration_fill.opacity); - } - break; - case PAINT_COLOR: { - SPColor const &c = text_decoration_fill.color; - text_decoration_fill_pattern = cairo_pattern_create_rgba( - c.v.c[0], c.v.c[1], c.v.c[2], text_decoration_fill.opacity); - } break; - default: break; - } - } - if (!text_decoration_fill_pattern) return false; - return true; + if (!text_decoration_fill_pattern) text_decoration_fill_pattern = preparePaint(dc, paintbox, pattern, text_decoration_fill); + return text_decoration_fill_pattern != nullptr; } -void NRStyle::applyTextDecorationFill(Inkscape::DrawingContext &dc) +bool NRStyle::prepareTextDecorationStroke(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern) { - dc.setSource(text_decoration_fill_pattern); - // Fill rule does not matter, no intersections. + if (!text_decoration_stroke_pattern) text_decoration_stroke_pattern = preparePaint(dc, paintbox, pattern, text_decoration_stroke); + return text_decoration_stroke_pattern != nullptr; } -bool NRStyle::prepareStroke(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern) +void NRStyle::applyFill(Inkscape::DrawingContext &dc) { - if (!stroke_pattern) { - switch (stroke.type) { - case PAINT_SERVER: - if (pattern) { - stroke_pattern = pattern->renderPattern(stroke.opacity); - } else { - stroke_pattern = stroke.server->pattern_new(dc.raw(), paintbox, stroke.opacity); - } - break; - case PAINT_COLOR: { - SPColor const &c = stroke.color; - stroke_pattern = cairo_pattern_create_rgba(c.v.c[0], c.v.c[1], c.v.c[2], - stroke.opacity); - } - break; - default: - break; - } - } - if (!stroke_pattern) return false; - return true; + dc.setSource(fill_pattern); + dc.setFillRule(fill_rule); +} + +void NRStyle::applyTextDecorationFill(Inkscape::DrawingContext &dc) +{ + dc.setSource(text_decoration_fill_pattern); + // Fill rule does not matter, no intersections. } void NRStyle::applyStroke(Inkscape::DrawingContext &dc) @@ -441,31 +417,6 @@ void NRStyle::applyStroke(Inkscape::DrawingContext &dc) cairo_set_dash(dc.raw(), dash, n_dash, dash_offset); // fixme } -bool NRStyle::prepareTextDecorationStroke(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern) -{ - if (!text_decoration_stroke_pattern) { - switch (text_decoration_stroke.type) { - case PAINT_SERVER: - if (pattern) { - text_decoration_stroke_pattern = pattern->renderPattern( - text_decoration_stroke.opacity); - } else { - text_decoration_stroke_pattern = text_decoration_stroke.server->pattern_new( - dc.raw(), paintbox, text_decoration_stroke.opacity); - } - break; - case PAINT_COLOR: { - SPColor const &c = text_decoration_stroke.color; - text_decoration_stroke_pattern = cairo_pattern_create_rgba( - c.v.c[0], c.v.c[1], c.v.c[2], text_decoration_stroke.opacity); - } break; - default: break; - } - } - if (!text_decoration_stroke_pattern) return false; - return true; -} - void NRStyle::applyTextDecorationStroke(Inkscape::DrawingContext &dc) { dc.setSource(text_decoration_stroke_pattern); diff --git a/src/display/nr-style.h b/src/display/nr-style.h index ba1a919ef..857083283 100644 --- a/src/display/nr-style.h +++ b/src/display/nr-style.h @@ -30,24 +30,14 @@ struct NRStyle { NRStyle(); ~NRStyle(); - void set(SPStyle *style, SPStyle *context_style = nullptr); - bool prepareFill(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern); - bool prepareStroke(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern); - bool prepareTextDecorationFill(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern); - bool prepareTextDecorationStroke(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern); - void applyFill(Inkscape::DrawingContext &dc); - void applyStroke(Inkscape::DrawingContext &dc); - void applyTextDecorationFill(Inkscape::DrawingContext &dc); - void applyTextDecorationStroke(Inkscape::DrawingContext &dc); - void update(); - enum PaintType { PAINT_NONE, PAINT_COLOR, PAINT_SERVER }; - struct Paint { + class Paint { + public: Paint() : type(PAINT_NONE), color(0), server(nullptr), opacity(1.0) {} ~Paint() { clear(); } @@ -61,6 +51,18 @@ struct NRStyle { void set(SPPaintServer *ps); }; + void set(SPStyle *style, SPStyle *context_style = nullptr); + cairo_pattern_t* preparePaint(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern, Paint& paint); + bool prepareFill(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern); + bool prepareStroke(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern); + bool prepareTextDecorationFill(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern); + bool prepareTextDecorationStroke(Inkscape::DrawingContext &dc, Geom::OptRect const &paintbox, Inkscape::DrawingPattern *pattern); + void applyFill(Inkscape::DrawingContext &dc); + void applyStroke(Inkscape::DrawingContext &dc); + void applyTextDecorationFill(Inkscape::DrawingContext &dc); + void applyTextDecorationStroke(Inkscape::DrawingContext &dc); + void update(); + Paint fill; Paint stroke; float stroke_width; |
