summaryrefslogtreecommitdiffstats
path: root/src/path/path-simplify.cpp
blob: 7bc7ddf1a3b24ae76e057f254e908a66d0b9f986 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * Simplify paths (reduce node count).
 *//*
 * Authors:
 * see git history
 *  Created by fred on Fri Dec 05 2003.
 *  tweaked endlessly by bulia byak <buliabyak@users.sf.net>
 *
 * Copyright (C) 2018 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#ifdef HAVE_CONFIG_H
#endif

#include <vector>

#include "path-simplify.h"
#include "path-util.h"

#include "document-undo.h"
#include "preferences.h"

#include "livarot/Path.h"

#include "object/sp-item-group.h"
#include "object/sp-path.h"

using Inkscape::DocumentUndo;

// Return number of paths simplified (can be greater than one if group).
int
path_simplify(SPItem *item, float threshold, bool justCoalesce, double size)
{
    //If this is a group, do the children instead
    SPGroup* group = dynamic_cast<SPGroup *>(item);
    if (group) {
        int pathsSimplified = 0;
        std::vector<SPItem*> items = sp_item_group_item_list(group);
        for (auto item : items) {
            pathsSimplified += path_simplify(item, threshold, justCoalesce, size);
        }
        return pathsSimplified;
    }

    SPPath* path = dynamic_cast<SPPath *>(item);
    if (!path) {
        return 0;
    }

    // There is actually no option in the preferences dialog for this!
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    bool simplifyIndividualPaths = prefs->getBool("/options/simplifyindividualpaths/value");
    if (simplifyIndividualPaths) {
        Geom::OptRect itemBbox = item->documentVisualBounds();
        if (itemBbox) {
            size = L2(itemBbox->dimensions());
        } else {
            size = 0;
        }
    }

    // Correct virtual size by full transform (bug #166937).
    size /= item->i2doc_affine().descrim();

    // Save the transform, to re-apply it after simplification.
    Geom::Affine const transform(item->transform);

    /*
       reset the transform, effectively transforming the item by transform.inverse();
       this is necessary so that the item is transformed twice back and forth,
       allowing all compensations to cancel out regardless of the preferences
    */
    item->doWriteTransform(Geom::identity());

    // SPLivarot: Start  -----------------

    // Get path to simplify (note that the path *before* LPE calculation is needed)
    Path *orig = Path_for_item_before_LPE(item, false);
    if (orig == nullptr) {
        return 0;
    }

    if ( justCoalesce ) {
        orig->Coalesce(threshold * size);
    } else {
        orig->ConvertEvenLines(threshold * size);
        orig->Simplify(threshold * size);
    }

    // Path
    gchar *str = orig->svg_dump_path();

    // SPLivarot: End  -------------------

    char const *patheffect = item->getRepr()->attribute("inkscape:path-effect");
    if (patheffect) {
        item->setAttribute("inkscape:original-d", str);
    } else {
        item->setAttribute("d", str);
    }
    g_free(str);

    // reapply the transform
    item->doWriteTransform(transform);

    // clean up
    if (orig) delete orig;

    return 1;
}

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