summaryrefslogtreecommitdiffstats
path: root/src/display/drawing.cpp
blob: c30609582b6ce2229d70d94dc4a59eb58a728786 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// SPDX-License-Identifier: GPL-2.0-or-later
/**
 * @file
 * SVG drawing for display.
 *//*
 * Authors:
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 *   Johan Engelen <j.b.c.engelen@alumnus.utwente.nl>
 *
 * Copyright (C) 2011-2012 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "display/drawing.h"
#include "display/control/canvas-item-drawing.h"
#include "nr-filter-gaussian.h"
#include "nr-filter-types.h"

//grayscale colormode:
#include "cairo-templates.h"
#include "drawing-context.h"


namespace Inkscape {

// hardcoded grayscale color matrix values as default
static const gdouble grayscale_value_matrix[20] = {
    0.21, 0.72, 0.072, 0, 0,
    0.21, 0.72, 0.072, 0, 0,
    0.21, 0.72, 0.072, 0, 0,
    0   , 0   , 0    , 1, 0
};

Drawing::Drawing(Inkscape::CanvasItemDrawing *canvas_item_drawing)
    : _canvas_item_drawing(canvas_item_drawing)
    , _grayscale_colormatrix(std::vector<gdouble>(grayscale_value_matrix, grayscale_value_matrix + 20))
{
    // _canvas_item_drawing can be null. Used this way by Eraser tool.
}

Drawing::~Drawing()
{
    delete _root;
}

void
Drawing::setRoot(DrawingItem *item)
{
    delete _root;
    _root = item;
    if (item) {
        assert(item->_child_type == DrawingItem::CHILD_ORPHAN);
        item->_child_type = DrawingItem::CHILD_ROOT;
    }
}

RenderMode
Drawing::renderMode() const
{
    return _exact ? RenderMode::NORMAL : _rendermode;
}
ColorMode
Drawing::colorMode() const
{
    return (outline() || _exact) ? ColorMode::NORMAL : _colormode;
}
bool
Drawing::outline() const
{
    return renderMode() == RenderMode::OUTLINE;
}
bool
Drawing::visibleHairlines() const
{
    return renderMode() == RenderMode::VISIBLE_HAIRLINES;
}
bool
Drawing::outlineOverlay() const
{
    return renderMode() == RenderMode::OUTLINE_OVERLAY;
}
bool
Drawing::renderFilters() const
{
    return renderMode() == RenderMode::NORMAL || renderMode() == RenderMode::VISIBLE_HAIRLINES || renderMode() == RenderMode::OUTLINE_OVERLAY;
}
int
Drawing::blurQuality() const
{
    if (renderMode() == RenderMode::NORMAL) {
        return _exact ? BLUR_QUALITY_BEST : _blur_quality;
    } else {
        return BLUR_QUALITY_WORST;
    }
}
int
Drawing::filterQuality() const
{
    if (renderMode() == RenderMode::NORMAL) {
        return _exact ? Filters::FILTER_QUALITY_BEST : _filter_quality;
    } else {
        return Filters::FILTER_QUALITY_WORST;
    }
}

void
Drawing::setRenderMode(RenderMode mode)
{
    _rendermode = mode;
}
void
Drawing::setColorMode(ColorMode mode)
{
    _colormode = mode;
}
void
Drawing::setBlurQuality(int q)
{
    _blur_quality = q;
}
void
Drawing::setFilterQuality(int q)
{
    _filter_quality = q;
}
void
Drawing::setExact(bool e)
{
    _exact = e;
}

void Drawing::setOutlineSensitive(bool e) { _outline_sensitive = e; };

Geom::OptIntRect const &
Drawing::cacheLimit() const
{
    return _cache_limit;
}
void
Drawing::setCacheLimit(Geom::OptIntRect const &r)
{
    _cache_limit = r;
    for (auto _cached_item : _cached_items) {
        _cached_item->_markForUpdate(DrawingItem::STATE_CACHE, false);
    }
}
void
Drawing::setCacheBudget(size_t bytes)
{
    _cache_budget = bytes;
    _pickItemsForCaching();
}

void
Drawing::setGrayscaleMatrix(gdouble value_matrix[20]) {
    _grayscale_colormatrix = Filters::FilterColorMatrix::ColorMatrixMatrix( 
        std::vector<gdouble> (value_matrix, value_matrix + 20) );
}

void
Drawing::update(Geom::IntRect const &area, unsigned flags, unsigned reset)
{
    if (_root) {
        auto ctx = _canvas_item_drawing ? _canvas_item_drawing->get_context() : UpdateContext();
        _root->update(area, ctx, flags, reset);
    }
    if ((flags & DrawingItem::STATE_CACHE) || (flags & DrawingItem::STATE_ALL)) {
        // process the updated cache scores
        _pickItemsForCaching();
    }
}

void
Drawing::render(DrawingContext &dc, Geom::IntRect const &area, unsigned flags, int antialiasing)
{
    if (_root) {
        int prev_a = _root->_antialias;
        if(antialiasing >= 0)
            _root->setAntialiasing(antialiasing);
        _root->render(dc, area, flags);
        _root->setAntialiasing(prev_a);
    }

    if (colorMode() == ColorMode::GRAYSCALE) {
        // apply grayscale filter on top of everything
        cairo_surface_t *input = dc.rawTarget();
        cairo_surface_t *out = ink_cairo_surface_create_identical(input);
        ink_cairo_surface_filter(input, out, _grayscale_colormatrix);
        Geom::Point origin = dc.targetLogicalBounds().min();
        dc.setSource(out, origin[Geom::X], origin[Geom::Y]);
        dc.setOperator(CAIRO_OPERATOR_SOURCE);
        dc.paint();
        dc.setOperator(CAIRO_OPERATOR_OVER);
    
        cairo_surface_destroy(out);
    }
}

DrawingItem *
Drawing::pick(Geom::Point const &p, double delta, unsigned flags)
{
    if (_root) {
        return _root->pick(p, delta, flags);
    } else {
        std::cerr << "Drawing::pick: _root is null!" << std::endl;
    }
    return nullptr;
}

void
Drawing::_pickItemsForCaching()
{
    size_t used = 0;
    CandidateList::iterator i;
    for (i = _candidate_items.begin(); i != _candidate_items.end(); ++i) {
        if (used + i->cache_size > _cache_budget) break;
        used += i->cache_size;
    }

    std::set<DrawingItem*> to_cache;
    for (CandidateList::iterator j = _candidate_items.begin(); j != i; ++j) {
        j->item->setCached(true);
        to_cache.insert(j->item);
    }
    // Everything which is now in _cached_items but not in to_cache must be uncached
    // Note that calling setCached on an item modifies _cached_items
    // TODO: find a way to avoid the set copy
    std::set<DrawingItem*> to_uncache;
    std::set_difference(_cached_items.begin(), _cached_items.end(),
                        to_cache.begin(), to_cache.end(),
                        std::inserter(to_uncache, to_uncache.end()));
    for (auto j : to_uncache) {
        j->setCached(false);
    }
}

/*
 * Return average color over area. Used by Calligraphic, Dropper, and Spray tools.
 */
void
Drawing::average_color(Geom::IntRect const &area, double &R, double &G, double &B, double &A)
{
    auto surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, area.width(), area.height());
    Inkscape::DrawingContext dc(surface->cobj(), area.min());
    render(dc, area);

    ink_cairo_surface_average_color_premul(surface->cobj(), R, G, B, A);
}


} // end namespace Inkscape

/*
  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 :