summaryrefslogtreecommitdiffstats
path: root/src/display/drawing-paintserver.h
blob: eb68f6b67267cf662acbb2d40bd3027ad6a12095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef INKSCAPE_DISPLAY_DRAWING_PAINT_SERVER_H
#define INKSCAPE_DISPLAY_DRAWING_PAINT_SERVER_H
/**
 * @file
 * Representation of paint servers used when rendering.
 */
/*
 * Author: PBS <pbs3141@gmail.com>
 * Copyright (C) 2022 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <array>
#include <vector>
#include <cairo.h>
#include <2geom/rect.h>
#include <2geom/affine.h>
#include "object/sp-gradient-spread.h"
#include "object/sp-gradient-units.h"
#include "object/sp-gradient-vector.h"

class SPGradient;

namespace Inkscape {

/**
 * A DrawingPaintServer is a lightweight copy of the resources needed to paint using a paint server.
 *
 * It is built by an SPPaintServer, stored in the DrawingItem tree, and used when rendering.
 *
 * The pattern information is stored in a rendering-backend agnostic way, and the object remains
 * valid even after the original SPPaintServer is modified or destroyed.
 */
class DrawingPaintServer
{
public:
    virtual ~DrawingPaintServer() = 0;

    /// Produce a pattern that can be used for painting with Cairo.
    virtual cairo_pattern_t *create_pattern(cairo_t *ct, Geom::OptRect const &bbox, double opacity) const = 0;

    /// Return whether this paint server could benefit from dithering.
    virtual bool ditherable() const { return false; }

    /// Return whether create_pattern() uses its cairo_t argument. Such pattern cannot be cached, but recreated each time.
    /// Fixme: The only reson this exists is to work around https://gitlab.freedesktop.org/cairo/cairo/-/issues/146.
    virtual bool uses_cairo_ctx() const { return false; }
};

// Todo: Remove, merging with existing implementation for solid colours.
/**
 * A simple solid color, storing an RGB color and an opacity.
 */
class DrawingSolidColor final
    : public DrawingPaintServer
{
public:
    DrawingSolidColor(float *c, double alpha)
        : c({c[0], c[1], c[2]})
        , alpha(alpha) {}

    cairo_pattern_t *create_pattern(cairo_t *, Geom::OptRect const &, double opacity) const override;

private:
    std::array<float, 3> c; ///< RGB color components.
    double alpha;
};

/**
 * The base class for all gradients.
 */
class DrawingGradient
    : public DrawingPaintServer
{
protected:
    DrawingGradient(SPGradientSpread spread, SPGradientUnits units, Geom::Affine const &transform)
        : spread(spread)
        , units(units)
        , transform(transform) {}

    bool ditherable() const override { return true; }

    /// Perform some common initialization steps on the given Cairo pattern.
    void common_setup(cairo_pattern_t *pat, Geom::OptRect const &bbox, double opacity) const;

    SPGradientSpread spread;
    SPGradientUnits units;
    Geom::Affine transform;
};

/**
 * A linear gradient.
 */
class DrawingLinearGradient final
    : public DrawingGradient
{
public:
    DrawingLinearGradient(SPGradientSpread spread, SPGradientUnits units, Geom::Affine const &transform,
                          float x1, float y1, float x2, float y2, std::vector<SPGradientStop> stops)
        : DrawingGradient(spread, units, transform)
        , x1(x1)
        , y1(y1)
        , x2(x2)
        , y2(y2)
        , stops(std::move(stops)) {}

    cairo_pattern_t *create_pattern(cairo_t*, Geom::OptRect const &bbox, double opacity) const override;

private:
    float x1, y1, x2, y2;
    std::vector<SPGradientStop> stops;
};

/**
 * A radial gradient.
 */
class DrawingRadialGradient final
    : public DrawingGradient
{
public:
    DrawingRadialGradient(SPGradientSpread spread, SPGradientUnits units, Geom::Affine const &transform,
                          float fx, float fy, float cx, float cy, float r, float fr, std::vector<SPGradientStop> stops)
        : DrawingGradient(spread, units, transform)
        , fx(fx)
        , fy(fy)
        , cx(cx)
        , cy(cy)
        , r(r)
        , fr(fr)
        , stops(std::move(stops)) {}

    cairo_pattern_t *create_pattern(cairo_t *ct, Geom::OptRect const &bbox, double opacity) const override;

    bool uses_cairo_ctx() const override { return true; }

private:
    float fx, fy, cx, cy, r, fr;
    std::vector<SPGradientStop> stops;
};

/**
 * A mesh gradient.
 */
class DrawingMeshGradient final
    : public DrawingGradient
{
public:
    struct PatchData
    {
        Geom::Point points[4][4];
        char pathtype[4];
        bool tensorIsSet[4];
        Geom::Point tensorpoints[4];
        std::array<float, 3> color[4];
        double opacity[4];
    };

    DrawingMeshGradient(SPGradientSpread spread, SPGradientUnits units, Geom::Affine const &transform,
                        int rows, int cols, std::vector<std::vector<PatchData>> patchdata)
        : DrawingGradient(spread, units, transform)
        , rows(rows)
        , cols(cols)
        , patchdata(std::move(patchdata)) {}

    cairo_pattern_t *create_pattern(cairo_t*, Geom::OptRect const &bbox, double opacity) const override;

private:
    int rows;
    int cols;
    std::vector<std::vector<PatchData>> patchdata;
};

} // namespace Inkscape

#endif // INKSCAPE_DISPLAY_DRAWING_PAINT_SERVER_H
/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :