summaryrefslogtreecommitdiffstats
path: root/src/display/drawing-shape.cpp
blob: ac0ff2ccb1904f5e8a1076d14181bc9d3626b9a1 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
 * @file
 * @brief Shape (styled path) belonging to an SVG drawing
 *//*
 * Authors:
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 *
 * Copyright (C) 2011 Authors
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include <glib.h>
#include <2geom/curves.h>
#include <2geom/pathvector.h>
#include <2geom/svg-path.h>
#include <2geom/svg-path-parser.h>

#include "display/cairo-utils.h"
#include "display/canvas-arena.h"
#include "display/canvas-bpath.h"
#include "display/curve.h"
#include "display/drawing.h"
#include "display/drawing-context.h"
#include "display/drawing-group.h"
#include "display/drawing-shape.h"
#include "helper/geom-curves.h"
#include "helper/geom.h"
#include "libnr/nr-convert2geom.h"
#include "preferences.h"
#include "style.h"
#include "svg/svg.h"

namespace Inkscape {

DrawingShape::DrawingShape(Drawing &drawing)
    : DrawingItem(drawing)
    , _curve(NULL)
    , _style(NULL)
    , _last_pick(NULL)
    , _repick_after(0)
{}

DrawingShape::~DrawingShape()
{
    if (_style)
        sp_style_unref(_style);
    if (_curve)
        _curve->unref();
}

void
DrawingShape::setPath(SPCurve *curve)
{
    _markForRendering();

    if (_curve) {
        _curve->unref();
        _curve = NULL;
    }
    if (curve) {
        _curve = curve;
        curve->ref();
    }

    _markForUpdate(STATE_ALL, false);
}

void
DrawingShape::setStyle(SPStyle *style)
{
    _setStyleCommon(_style, style);
    _nrstyle.set(style);
}

unsigned
DrawingShape::_updateItem(Geom::IntRect const &area, UpdateContext const &ctx, unsigned flags, unsigned reset)
{
    Geom::OptRect boundingbox;

    unsigned beststate = STATE_ALL;

    // update markers
    for (ChildrenList::iterator i = _children.begin(); i != _children.end(); ++i) {
        i->update(area, ctx, flags, reset);
    }

    if (!(flags & STATE_RENDER)) {
        /* We do not have to create rendering structures */
        if (flags & STATE_BBOX) {
            if (_curve) {
                boundingbox = bounds_exact_transformed(_curve->get_pathvector(), ctx.ctm);
                if (boundingbox) {
                    _bbox = boundingbox->roundOutwards();
                } else {
                    _bbox = Geom::OptIntRect();
                }
            }
            if (beststate & STATE_BBOX) {
                for (ChildrenList::iterator i = _children.begin(); i != _children.end(); ++i) {
                    _bbox.unionWith(i->geometricBounds());
                }
            }
        }
        return (flags | _state);
    }

    boundingbox = Geom::OptRect();
    bool outline = _drawing.outline();

    // clear Cairo data to force update
    _nrstyle.update();

    if (_curve) {
        boundingbox = bounds_exact_transformed(_curve->get_pathvector(), ctx.ctm);

        if (boundingbox && (_nrstyle.stroke.type != NRStyle::PAINT_NONE || outline)) {
            float width, scale;
            scale = ctx.ctm.descrim();
            width = std::max(0.125f, _nrstyle.stroke_width * scale);
            if ( fabs(_nrstyle.stroke_width * scale) > 0.01 ) { // FIXME: this is always true
                boundingbox->expandBy(width);
            }
            // those pesky miters, now
            float miterMax = width * _nrstyle.miter_limit;
            if ( miterMax > 0.01 ) {
                // grunt mode. we should compute the various miters instead
                // (one for each point on the curve)
                boundingbox->expandBy(miterMax);
            }
        }
    }

    _bbox = boundingbox ? boundingbox->roundOutwards() : Geom::OptIntRect();

    if (!_curve || 
        !_style ||
        _curve->is_empty() ||
        (( _nrstyle.fill.type != NRStyle::PAINT_NONE ) &&
         ( _nrstyle.stroke.type != NRStyle::PAINT_NONE && !outline) ))
    {
        return STATE_ALL;
    }

    if (beststate & STATE_BBOX) {
        for (ChildrenList::iterator i = _children.begin(); i != _children.end(); ++i) {
            _bbox.unionWith(i->geometricBounds());
        }
    }

    return STATE_ALL;
}

unsigned
DrawingShape::_renderItem(DrawingContext &ct, Geom::IntRect const &area, unsigned flags, DrawingItem *stop_at)
{
    if (!_curve || !_style) return RENDER_OK;
    if (!area.intersects(_bbox)) return RENDER_OK; // skip if not within bounding box

    bool outline = _drawing.outline();

    if (outline) {
        guint32 rgba = _drawing.outlinecolor;

        {   Inkscape::DrawingContext::Save save(ct);
            ct.transform(_ctm);
            ct.path(_curve->get_pathvector());
        }
        {   Inkscape::DrawingContext::Save save(ct);
            ct.setSource(rgba);
            ct.setLineWidth(0.5);
            ct.setTolerance(1.25);
            ct.stroke();
        }
    } else {
        bool has_stroke, has_fill;
        // we assume the context has no path
        Inkscape::DrawingContext::Save save(ct);
        ct.transform(_ctm);

        // update fill and stroke paints.
        // this cannot be done during nr_arena_shape_update, because we need a Cairo context
        // to render svg:pattern
        has_fill   = _nrstyle.prepareFill(ct, _item_bbox);
        has_stroke = _nrstyle.prepareStroke(ct, _item_bbox);
        has_stroke &= (_nrstyle.stroke_width != 0);

        if (has_fill || has_stroke) {
            // TODO: remove segments outside of bbox when no dashes present
            ct.path(_curve->get_pathvector());
            if (has_fill) {
                _nrstyle.applyFill(ct);
                ct.fillPreserve();
            }
            if (has_stroke) {
                _nrstyle.applyStroke(ct);
                ct.strokePreserve();
            }
            ct.newPath(); // clear path
        } // has fill or stroke pattern
    }

    // marker rendering
    for (ChildrenList::iterator i = _children.begin(); i != _children.end(); ++i) {
        i->render(ct, area, flags, stop_at);
    }
    return RENDER_OK;
}

void
DrawingShape::_clipItem(DrawingContext &ct, Geom::IntRect const &area)
{
    if (!_curve) return;

    Inkscape::DrawingContext::Save save(ct);
    // handle clip-rule
    if (_style) {
        if (_style->clip_rule.computed == SP_WIND_RULE_EVENODD) {
            ct.setFillRule(CAIRO_FILL_RULE_EVEN_ODD);
        } else {
            ct.setFillRule(CAIRO_FILL_RULE_WINDING);
        }
    }
    ct.transform(_ctm);
    ct.path(_curve->get_pathvector());
    ct.fill();
}

DrawingItem *
DrawingShape::_pickItem(Geom::Point const &p, double delta, unsigned flags)
{
    if (_repick_after > 0)
        --_repick_after;

    if (_repick_after > 0) // we are a slow, huge path
        return _last_pick; // skip this pick, returning what was returned last time

    if (!_curve) return NULL;
    if (!_style) return NULL;

    bool outline = _drawing.outline();
    bool pick_as_clip = flags & PICK_AS_CLIP;

    if (SP_SCALE24_TO_FLOAT(_style->opacity.value) == 0 && !outline && !pick_as_clip) 
        // fully transparent, no pick unless outline mode
        return NULL;

    GTimeVal tstart, tfinish;
    g_get_current_time (&tstart);

    double width;
    if (pick_as_clip) {
        width = 0; // no width should be applied to clip picking
                   // this overrides display mode and stroke style considerations
    } else if (outline) {
        width = 0.5; // in outline mode, everything is stroked with the same 0.5px line width
    } else if (_nrstyle.stroke.type != NRStyle::PAINT_NONE && _nrstyle.stroke.opacity > 1e-3) {
        // for normal picking calculate the distance corresponding top the stroke width
        // FIXME BUG: this is incorrect for transformed strokes
        float const scale = _ctm.descrim();
        width = std::max(0.125f, _nrstyle.stroke_width * scale) / 2;
    } else {
        width = 0;
    }

    double dist = Geom::infinity();
    int wind = 0;
    bool needfill = pick_as_clip || (_nrstyle.fill.type != NRStyle::PAINT_NONE &&
        _nrstyle.fill.opacity > 1e-3 && !outline);
    bool wind_evenodd = pick_as_clip ? (_style->clip_rule.computed == SP_WIND_RULE_EVENODD) :
        (_style->fill_rule.computed == SP_WIND_RULE_EVENODD);

    // actual shape picking
    if (_drawing.arena()) {
        Geom::Rect viewbox = _drawing.arena()->item.canvas->getViewbox();
        viewbox.expandBy (width);
        pathv_matrix_point_bbox_wind_distance(_curve->get_pathvector(), _ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, &viewbox);
    } else {
        pathv_matrix_point_bbox_wind_distance(_curve->get_pathvector(), _ctm, p, NULL, needfill? &wind : NULL, &dist, 0.5, NULL);
    }

    g_get_current_time (&tfinish);
    glong this_pick = (tfinish.tv_sec - tstart.tv_sec) * 1000000 + (tfinish.tv_usec - tstart.tv_usec);
    //g_print ("pick time %lu\n", this_pick);

    if (this_pick > 10000) { // slow picking, remember to skip several new picks
        _repick_after = this_pick / 5000;
    }

    // covered by fill?
    if (needfill) {
        if (wind_evenodd) {
            if (wind & 0x1) {
                _last_pick = this;
                return this;
            }
        } else {
            if (wind != 0) {
                _last_pick = this;
                return this;
            }
        }
    }

    // close to the edge, as defined by strokewidth and delta?
    // this ignores dashing (as if the stroke is solid) and always works as if caps are round
    if (needfill || width > 0) { // if either fill or stroke visible,
        if ((dist - width) < delta) {
            _last_pick = this;
            return this;
        }
    }

    // if not picked on the shape itself, try its markers
    for (ChildrenList::iterator i = _children.begin(); i != _children.end(); ++i) {
        DrawingItem *ret = i->pick(p, delta, flags & ~PICK_STICKY);
        if (ret) {
            _last_pick = this;
            return this;
        }
    }

    _last_pick = NULL;
    return NULL;
}

bool
DrawingShape::_canClip()
{
    return true;
}

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