Inkscape
Vector Graphics Editor
gradient-image.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
14#include "gradient-image.h"
15
16#include <cairomm/context.h>
17#include <cairomm/surface.h>
18#include <gdkmm/pixbuf.h>
19#include <gtkmm/drawingarea.h>
20#include <sigc++/functors/mem_fun.h>
21
22#include "display/cairo-utils.h"
23#include "object/sp-gradient.h"
24#include "object/sp-stop.h"
25
26namespace Inkscape::UI::Widget {
27
29{
30 set_name("GradientImage");
31 set_draw_func(sigc::mem_fun(*this, &GradientImage::draw_func));
32 set_gradient(gradient);
33}
34
35void GradientImage::draw_func(Cairo::RefPtr<Cairo::Context> const &cr,
36 int const width, int const height)
37{
38 auto ct = cr->cobj();
40}
41
42void
44{
45 if (_gradient == gradient) return;
46
47 if (_gradient) {
50 }
51
52 _gradient = gradient;
53
54 if (gradient) {
55 _release_connection = gradient->connectRelease(sigc::mem_fun(*this, &GradientImage::gradient_release));
57 }
58
59 queue_draw();
60}
61
62void
64{
65 set_gradient(nullptr);
66}
67
68void
69GradientImage::gradient_modified(SPObject const * /*object*/, guint /*flags*/)
70{
71 queue_draw();
72}
73
74} // namespace Inkscape::UI::Widget
75
76void
77sp_gradient_draw(SPGradient * const gr, int const width, int const height,
78 cairo_t * const ct)
79{
81 cairo_set_source(ct, check);
82 cairo_paint(ct);
83 cairo_pattern_destroy(check);
84
85 if (gr) {
87 if (!p) {
88 return;
89 }
90 cairo_set_source(ct, p);
91 cairo_paint(ct);
92 cairo_pattern_destroy(p);
93 }
94}
95
98{
99 cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
100 cairo_t *ct = cairo_create(s);
101 sp_gradient_draw(gr, width, height, ct);
102 cairo_destroy(ct);
103 cairo_surface_flush(s);
104
105 // no need to free s - the call below takes ownership
107 return pixbuf;
108}
109
110Glib::RefPtr<Gdk::Pixbuf>
112{
113 return Glib::wrap(sp_gradient_to_pixbuf(gr, width, height));
114}
115
116Glib::RefPtr<Gdk::Pixbuf>
118{
119 cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
120 cairo_t *ct = cairo_create(s);
121
122 /* Checkerboard background */
124 cairo_rectangle(ct, 0, 0, width, height);
125 cairo_set_source(ct, check);
126 cairo_fill_preserve(ct);
127 cairo_pattern_destroy(check);
128
129 if (stop) {
130 /* Alpha area */
131 cairo_rectangle(ct, 0, 0, width/2, height);
133 cairo_fill(ct);
134
135 /* Solid area */
136 cairo_rectangle(ct, width/2, 0, width, height);
137 ink_cairo_set_source_rgba32(ct, stop->get_rgba32() | 0xff);
138 cairo_fill(ct);
139 }
140
141 cairo_destroy(ct);
142 cairo_surface_flush(s);
143
144 Cairo::RefPtr<Cairo::Surface> sref = Cairo::RefPtr<Cairo::Surface>(new Cairo::Surface(s));
145 Glib::RefPtr<Gdk::Pixbuf> pixbuf =
146 Gdk::Pixbuf::create(sref, 0, 0, width, height);
147
148 cairo_surface_destroy(s);
149
150 return pixbuf;
151}
152
153/*
154 Local Variables:
155 mode:c++
156 c-file-style:"stroustrup"
157 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
158 indent-tabs-mode:nil
159 fill-column:99
160 End:
161*/
162// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8 :
cairo_pattern_t * ink_cairo_pattern_create_checkerboard(guint32 rgba, bool use_alpha)
void ink_cairo_set_source_rgba32(cairo_t *ct, guint32 rgba)
GdkPixbuf * ink_pixbuf_create_from_cairo_surface(cairo_surface_t *s)
Converts the Cairo surface to a GdkPixbuf pixel format, without allocating extra memory.
Cairo integration helpers.
struct _GdkPixbuf GdkPixbuf
Definition: cairo-utils.h:21
void gradient_modified(SPObject const *obj, guint flags)
void draw_func(Cairo::RefPtr< Cairo::Context > const &cr, int width, int height)
void gradient_release(SPObject const *obj)
Gradient.
Definition: sp-gradient.h:86
cairo_pattern_t * create_preview_pattern(double width)
SPObject is an abstract base class of all of the document nodes at the SVG document level.
Definition: sp-object.h:146
sigc::connection connectRelease(sigc::slot< void(SPObject *)> slot)
Connects to the release request signal.
Definition: sp-object.h:223
sigc::connection connectModified(sigc::slot< void(SPObject *, unsigned int)> slot)
Connects to the modification notification signal.
Definition: sp-object.h:691
Gradient stop.
Definition: sp-stop.h:28
guint32 get_rgba32() const
Return stop's color as 32bit value.
Definition: sp-stop.cpp:165
_cairo_pattern cairo_pattern_t
struct _cairo_surface cairo_surface_t
Glib::RefPtr< Gdk::Pixbuf > sp_gradstop_to_pixbuf_ref(SPStop *stop, int width, int height)
GdkPixbuf * sp_gradient_to_pixbuf(SPGradient *gr, int width, int height)
void sp_gradient_draw(SPGradient *const gr, int const width, int const height, cairo_t *const ct)
Glib::RefPtr< Gdk::Pixbuf > sp_gradient_to_pixbuf_ref(SPGradient *gr, int width, int height)
Custom widgets.
Definition: desktop.h:127
static constexpr int height
void cairo_rectangle(cairo_t *cr, Geom::Rect const &r)
struct _cairo cairo_t
Definition: path-cairo.h:16
TODO: insert short description here.
double height
double width
std::unique_ptr< Toolbar >(* create)(SPDesktop *desktop)
Definition: toolbars.cpp:63