summaryrefslogtreecommitdiffstats
path: root/src/live_effects/lpegroupbbox.cpp
blob: 90fdc3504ebdf8772fbadd912430be7e49de2186 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) Steren Giannini 2008 <steren.giannini@gmail.com>
 *   Abhishek Sharma
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "document.h"
#include "live_effects/lpegroupbbox.h"
#include "object/sp-clippath.h"
#include "object/sp-mask.h"
#include "object/sp-root.h"
#include "object/sp-shape.h"
#include "object/sp-item-group.h"
#include "object/sp-lpe-item.h"

namespace Inkscape {
namespace LivePathEffect {

/**
 * Updates the \c boundingbox_X and \c boundingbox_Y values from the geometric bounding box of \c lpeitem.
 *
 * @pre   lpeitem must have an existing geometric boundingbox (usually this is guaranteed when: \code SP_SHAPE(lpeitem)->curve != NULL \endcode )
 *        It's not possible to run LPEs on items without their original-d having a bbox.
 * @param lpeitem   This is not allowed to be NULL.
 * @param absolute  Determines whether the bbox should be calculated of the untransformed lpeitem (\c absolute = \c false)
 *                  or of the transformed lpeitem (\c absolute = \c true) using sp_item_i2doc_affine.
 * @post Updated values of boundingbox_X and boundingbox_Y. These intervals are set to empty intervals when the precondition is not met.
 */

Geom::OptRect
GroupBBoxEffect::clip_mask_bbox(SPLPEItem *item, Geom::Affine transform)
{
    Geom::OptRect bbox;
    Geom::Affine affine = transform * item->transform;
    SPClipPath *clip_path = item->getClipObject();
    if(clip_path) {
        bbox.unionWith(clip_path->geometricBounds(affine));
    }
    SPMask * mask_path = item->getMaskObject();
    if(mask_path) {
        bbox.unionWith(mask_path->visualBounds(affine));
    }
    SPGroup * group = dynamic_cast<SPGroup *>(item);
    if (group) {
        std::vector<SPItem*> item_list = sp_item_group_item_list(group);
        for (auto iter : item_list) {
            SPLPEItem * subitem = dynamic_cast<SPLPEItem *>(iter);
            if (subitem) {
                bbox.unionWith(clip_mask_bbox(subitem, affine));
            }
        }
    }
    return bbox;
}

void GroupBBoxEffect::original_bbox(SPLPEItem const* lpeitem, bool absolute, bool clip_mask, Geom::Affine base_transform)
{
    // Get item bounding box
    Geom::Affine transform;
    if (absolute) {
        transform = lpeitem->i2doc_affine();
    }
    else {
        transform = base_transform;
    }
    
    Geom::OptRect bbox = lpeitem->geometricBounds(transform);
    if (clip_mask) {
        SPLPEItem * item = const_cast<SPLPEItem *>(lpeitem);
        bbox.unionWith(clip_mask_bbox(item, transform * item->transform.inverse()));
    }
    if (bbox) {
        boundingbox_X = (*bbox)[Geom::X];
        boundingbox_Y = (*bbox)[Geom::Y];
    } else {
        boundingbox_X = Geom::Interval();
        boundingbox_Y = Geom::Interval();
    }
}

} // namespace LivePathEffect
} /* 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 :