summaryrefslogtreecommitdiffstats
path: root/src/live_effects/lpe-show_handles.cpp
blob: c40f075017202ac601b01d0d6509a226dcc17dd9 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *   Jabier Arraiza Cenoz
 *
 * Copyright (C) Jabier Arraiza Cenoz 2014 <jabier.arraiza@marker.es>
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <gtkmm.h>
#include "live_effects/lpe-show_handles.h"
#include <2geom/sbasis-to-bezier.h>
#include <2geom/svg-path-parser.h>
#include "helper/geom.h"
#include "desktop-style.h"
#include "display/curve.h"
#include "svg/svg.h"

#include "object/sp-shape.h"
#include "style.h"

// TODO due to internal breakage in glibmm headers, this must be last:
#include <glibmm/i18n.h>

namespace Inkscape {
namespace LivePathEffect {

LPEShowHandles::LPEShowHandles(LivePathEffectObject *lpeobject)
    : Effect(lpeobject),
      nodes(_("Show nodes"), _("Show nodes"), "nodes", &wr, this, true),
      handles(_("Show handles"), _("Show handles"), "handles", &wr, this, true),
      original_path(_("Show path"), _("Show path"), "original_path", &wr, this, true),
      show_center_node(_("Show center of node"), _("Show center of node"), "show_center_node", &wr, this, false),
      original_d(_("Show original"), _("Show original"), "original_d", &wr, this, false),
      scale_nodes_and_handles(_("Scale nodes and handles"), _("Scale nodes and handles"), "scale_nodes_and_handles", &wr, this, 10)
{
    registerParameter(&nodes);
    registerParameter(&handles);
    registerParameter(&original_path);
    registerParameter(&show_center_node);
    registerParameter(&original_d);
    registerParameter(&scale_nodes_and_handles);
    scale_nodes_and_handles.param_set_range(0, 500.);
    scale_nodes_and_handles.param_set_increments(1, 1);
    scale_nodes_and_handles.param_set_digits(2);
    stroke_width = 1.0;
}

bool LPEShowHandles::alerts_off = false;

/**
 * Sets default styles to element
 * this permanently remove.some styles of the element
 */

void LPEShowHandles::doOnApply(SPLPEItem const* lpeitem)
{
    if(!alerts_off) {
        char *msg = _("The \"show handles\" path effect will remove any custom style on the object you are applying it to. If this is not what you want, click Cancel.");
        Gtk::MessageDialog dialog(msg, false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_OK_CANCEL, true);
        gint response = dialog.run();
        alerts_off = true;
        if(response == GTK_RESPONSE_CANCEL) {
            SPLPEItem* item = const_cast<SPLPEItem*>(lpeitem);
            item->removeCurrentPathEffect(false);
            return;
        }
    }
    SPLPEItem* item = const_cast<SPLPEItem*>(lpeitem);
    SPCSSAttr *css = sp_repr_css_attr_new ();
    sp_repr_css_set_property (css, "stroke", "black");
    sp_repr_css_set_property (css, "stroke-width", "1");
    sp_repr_css_set_property (css, "stroke-linecap", "butt");
    sp_repr_css_set_property(css, "fill", "none");

    sp_desktop_apply_css_recursive(item, css, true);
    sp_repr_css_attr_unref (css);
}

void LPEShowHandles::doBeforeEffect (SPLPEItem const* lpeitem)
{
    SPItem const* item = SP_ITEM(lpeitem);
    stroke_width = item->style->stroke_width.computed;
}

Geom::PathVector LPEShowHandles::doEffect_path (Geom::PathVector const & path_in)
{
    Geom::PathVector path_out;
    Geom::PathVector original_pathv = pathv_to_linear_and_cubic_beziers(path_in);
    if(original_path) {
        for (const auto & i : path_in) {
            path_out.push_back(i);
        }
    }
    if(!outline_path.empty()) {
        outline_path.clear();
    }
    if (original_d) {
        SPCurve * shape_curve = current_shape->getCurveForEdit();
        if (shape_curve) {
            Geom::PathVector original_curve = shape_curve->get_pathvector();
            if(original_path) {
                for (const auto & i : original_curve) {
                    path_out.push_back(i);
                }
            }
            original_pathv.insert(original_pathv.end(), original_curve.begin(), original_curve.end());
        }
        generateHelperPath(original_pathv);
        shape_curve->unref();
    } else {
        generateHelperPath(original_pathv);
    }
    for (const auto & i : outline_path) {
        path_out.push_back(i);
    }

    return path_out;
}

void
LPEShowHandles::generateHelperPath(Geom::PathVector result)
{
    if(!handles && !nodes) {
        return;
    }

    Geom::CubicBezier const *cubic = nullptr;
    for (auto & path_it : result) {
        //Si está vacío...
        if (path_it.empty()) {
            continue;
        }
        //Itreadores
        Geom::Path::iterator curve_it1 = path_it.begin(); // incoming curve
        Geom::Path::iterator curve_it2 = ++(path_it.begin()); // outgoing curve
        Geom::Path::iterator curve_endit = path_it.end_default(); // this determines when the loop has to stop

        if (path_it.closed()) {
            // if the path is closed, maybe we have to stop a bit earlier because the
            // closing line segment has zerolength.
            Geom::Curve const &closingline = path_it.back_closed(); // the closing line segment is always of type
            // Geom::LineSegment.
            if (are_near(closingline.initialPoint(), closingline.finalPoint())) {
                // closingline.isDegenerate() did not work, because it only checks for
                // *exact* zero length, which goes wrong for relative coordinates and
                // rounding errors...
                // the closing line segment has zero-length. So stop before that one!
                curve_endit = path_it.end_open();
            }
        }
        if(nodes) {
            Geom::NodeType nodetype = Geom::NODE_CUSP;
            if(path_it.closed()) {
                nodetype = Geom::get_nodetype(path_it.finalCurve(), *curve_it1);
            } 
            drawNode(curve_it1->initialPoint(), nodetype);
        }
        while (curve_it1 != curve_endit) {
            cubic = dynamic_cast<Geom::CubicBezier const *>(&*curve_it1);
            if (cubic) {
                if(handles) {
                    if(!are_near((*cubic)[0],(*cubic)[1])) {
                        drawHandle((*cubic)[1]);
                        drawHandleLine((*cubic)[0],(*cubic)[1]);
                    }
                    if(!are_near((*cubic)[3],(*cubic)[2])) {
                        drawHandle((*cubic)[2]);
                        drawHandleLine((*cubic)[3],(*cubic)[2]);
                    }
                }
            }
            if(nodes && (curve_it2 != curve_endit || !path_it.closed())) {
                Geom::NodeType nodetype = Geom::get_nodetype(*curve_it1, *curve_it2);
                drawNode(curve_it1->finalPoint(), nodetype);
            }
            ++curve_it1;
            if(curve_it2 != curve_endit) {
                ++curve_it2;
            }
        }
    }
}

void
LPEShowHandles::drawNode(Geom::Point p, Geom::NodeType nodetype)
{
    if(stroke_width * scale_nodes_and_handles > 0.0) {
        Geom::Rotate rotate = Geom::Rotate(0);
        if ( nodetype == Geom::NODE_CUSP) {
            rotate = Geom::Rotate::from_degrees(45);
        }
        double diameter = stroke_width * scale_nodes_and_handles;
        char const * svgd;
        if (show_center_node) {
            svgd = "M 0.05,0 A 0.05,0.05 0 0 1 0,0.05 0.05,0.05 0 0 1 -0.05,0 0.05,0.05 0 0 1 0,-0.05 0.05,0.05 0 0 1 0.05,0 Z M -0.5,-0.5 0.5,-0.5 0.5,0.5 -0.5,0.5 Z";
        } else {
            svgd = "M -0.5,-0.5 0.5,-0.5 0.5,0.5 -0.5,0.5 Z";
        }
        Geom::PathVector pathv = sp_svg_read_pathv(svgd);
        pathv *= rotate * Geom::Scale(diameter) * Geom::Translate(p);
        outline_path.push_back(pathv[0]);
        if (show_center_node) {
            outline_path.push_back(pathv[1]);
        }
    }
}

void
LPEShowHandles::drawHandle(Geom::Point p)
{
    if(stroke_width * scale_nodes_and_handles > 0.0) {
        double diameter = stroke_width * scale_nodes_and_handles;
        char const * svgd;
        svgd = "M 0.7,0.35 A 0.35,0.35 0 0 1 0.35,0.7 0.35,0.35 0 0 1 0,0.35 0.35,0.35 0 0 1 0.35,0 0.35,0.35 0 0 1 0.7,0.35 Z";
        Geom::PathVector pathv = sp_svg_read_pathv(svgd);
        pathv *= Geom::Scale (diameter) * Geom::Translate(p - Geom::Point(diameter * 0.35,diameter * 0.35));
        outline_path.push_back(pathv[0]);
    }
}


void
LPEShowHandles::drawHandleLine(Geom::Point p,Geom::Point p2)
{
    Geom::Path path;
    double diameter = stroke_width * scale_nodes_and_handles;
    if(diameter > 0.0 && Geom::distance(p,p2) > (diameter * 0.35)) {
        Geom::Ray ray2(p, p2);
        p2 =  p2 - Geom::Point::polar(ray2.angle(),(diameter * 0.35));
    }
    path.start( p );
    path.appendNew<Geom::LineSegment>( p2 );
    outline_path.push_back(path);
}

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