1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
4 */
5
6 #include <gtkmm.h>
7
8 #include "live_effects/lpe-simplify.h"
9
10 #include "display/curve.h"
11 #include "helper/geom.h"
12 #include "path/path-util.h"
13 #include "svg/svg.h"
14 #include "ui/icon-names.h"
15 #include "ui/tools/node-tool.h"
16
17 #include <2geom/svg-path-parser.h>
18
19 // TODO due to internal breakage in glibmm headers, this must be last:
20 #include <glibmm/i18n.h>
21
22 namespace Inkscape {
23 namespace LivePathEffect {
24
LPESimplify(LivePathEffectObject * lpeobject)25 LPESimplify::LPESimplify(LivePathEffectObject *lpeobject)
26 : Effect(lpeobject)
27 , steps(_("Steps:"), _("Change number of simplify steps "), "steps", &wr, this, 1)
28 , threshold(_("Roughly threshold:"), _("Roughly threshold:"), "threshold", &wr, this, 0.002)
29 , smooth_angles(_("Smooth angles:"), _("Max degree difference on handles to perform a smooth"), "smooth_angles",
30 &wr, this, 0.)
31 , helper_size(_("Helper size:"), _("Helper size"), "helper_size", &wr, this, 5)
32 , simplify_individual_paths(_("Paths separately"), _("Simplifying paths (separately)"), "simplify_individual_paths",
33 &wr, this, false, "", INKSCAPE_ICON("on-outline"), INKSCAPE_ICON("off-outline"))
34 , simplify_just_coalesce(_("Just coalesce"), _("Simplify just coalesce"), "simplify_just_coalesce", &wr, this,
35 false, "", INKSCAPE_ICON("on-outline"), INKSCAPE_ICON("off-outline"))
36 {
37 registerParameter(&steps);
38 registerParameter(&threshold);
39 registerParameter(&smooth_angles);
40 registerParameter(&helper_size);
41 registerParameter(&simplify_individual_paths);
42 registerParameter(&simplify_just_coalesce);
43
44 threshold.param_set_range(0.0001, Geom::infinity());
45 threshold.param_set_increments(0.0001, 0.0001);
46 threshold.param_set_digits(6);
47
48 steps.param_set_range(0, 100);
49 steps.param_set_increments(1, 1);
50 steps.param_set_digits(0);
51
52 smooth_angles.param_set_range(0.0, 360.0);
53 smooth_angles.param_set_increments(10, 10);
54 smooth_angles.param_set_digits(2);
55
56 helper_size.param_set_range(0.0, 999.0);
57 helper_size.param_set_increments(5, 5);
58 helper_size.param_set_digits(2);
59
60 radius_helper_nodes = 6.0;
61 apply_to_clippath_and_mask = true;
62 }
63
64 LPESimplify::~LPESimplify() = default;
65
66 void
doBeforeEffect(SPLPEItem const * lpeitem)67 LPESimplify::doBeforeEffect (SPLPEItem const* lpeitem)
68 {
69 if(!hp.empty()) {
70 hp.clear();
71 }
72 bbox = SP_ITEM(lpeitem)->visualBounds();
73 radius_helper_nodes = helper_size;
74 }
75
76 Gtk::Widget *
newWidget()77 LPESimplify::newWidget()
78 {
79 // use manage here, because after deletion of Effect object, others might still be pointing to this widget.
80 Gtk::Box *vbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
81
82 vbox->set_border_width(5);
83 vbox->set_homogeneous(false);
84 vbox->set_spacing(2);
85 std::vector<Parameter *>::iterator it = param_vector.begin();
86 Gtk::Box * buttons = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL,0));
87 while (it != param_vector.end()) {
88 if ((*it)->widget_is_visible) {
89 Parameter * param = *it;
90 Gtk::Widget * widg = dynamic_cast<Gtk::Widget *>(param->param_newWidget());
91 if (param->param_key == "simplify_individual_paths" ||
92 param->param_key == "simplify_just_coalesce") {
93 Glib::ustring * tip = param->param_getTooltip();
94 if (widg) {
95 buttons->pack_start(*widg, true, true, 2);
96 if (tip) {
97 widg->set_tooltip_text(*tip);
98 } else {
99 widg->set_tooltip_text("");
100 widg->set_has_tooltip(false);
101 }
102 }
103 } else {
104 Glib::ustring * tip = param->param_getTooltip();
105 if (widg) {
106 Gtk::Box * horizontal_box = dynamic_cast<Gtk::Box *>(widg);
107 std::vector< Gtk::Widget* > child_list = horizontal_box->get_children();
108 Gtk::Entry* entry_widg = dynamic_cast<Gtk::Entry *>(child_list[1]);
109 entry_widg->set_width_chars(8);
110 vbox->pack_start(*widg, true, true, 2);
111 if (tip) {
112 widg->set_tooltip_text(*tip);
113 } else {
114 widg->set_tooltip_text("");
115 widg->set_has_tooltip(false);
116 }
117 }
118 }
119 }
120
121 ++it;
122 }
123 vbox->pack_start(*buttons,true, true, 2);
124 if(Gtk::Widget* widg = defaultParamSet()) {
125 vbox->pack_start(*widg, true, true, 2);
126 }
127 return dynamic_cast<Gtk::Widget *>(vbox);
128 }
129
130 void
doEffect(SPCurve * curve)131 LPESimplify::doEffect(SPCurve *curve)
132 {
133 Geom::PathVector const original_pathv = pathv_to_linear_and_cubic_beziers(curve->get_pathvector());
134 gdouble size = Geom::L2(bbox->dimensions());
135 //size /= Geom::Affine(0,0,0,0,0,0).descrim();
136 Path* pathliv = Path_for_pathvector(original_pathv);
137 if(simplify_individual_paths) {
138 size = Geom::L2(Geom::bounds_fast(original_pathv)->dimensions());
139 }
140 size /= sp_lpe_item->i2doc_affine().descrim();
141 for (int unsigned i = 0; i < steps; i++) {
142 if ( simplify_just_coalesce ) {
143 pathliv->Coalesce(threshold * size);
144 } else {
145 pathliv->ConvertEvenLines(threshold * size);
146 pathliv->Simplify(threshold * size);
147 }
148 }
149 Geom::PathVector result = Geom::parse_svg_path(pathliv->svg_dump_path());
150 generateHelperPathAndSmooth(result);
151 curve->set_pathvector(result);
152 update_helperpath();
153 }
154
155 void
generateHelperPathAndSmooth(Geom::PathVector & result)156 LPESimplify::generateHelperPathAndSmooth(Geom::PathVector &result)
157 {
158 if(steps < 1) {
159 return;
160 }
161 Geom::PathVector tmp_path;
162 Geom::CubicBezier const *cubic = nullptr;
163 for (auto & path_it : result) {
164 if (path_it.empty()) {
165 continue;
166 }
167
168 Geom::Path::iterator curve_it1 = path_it.begin(); // incoming curve
169 Geom::Path::iterator curve_it2 = ++(path_it.begin());// outgoing curve
170 Geom::Path::iterator curve_endit = path_it.end_default(); // this determines when the loop has to stop
171 SPCurve *nCurve = new SPCurve();
172 if (path_it.closed()) {
173 // if the path is closed, maybe we have to stop a bit earlier because the
174 // closing line segment has zerolength.
175 const Geom::Curve &closingline =
176 path_it.back_closed(); // the closing line segment is always of type
177 // Geom::LineSegment.
178 if (are_near(closingline.initialPoint(), closingline.finalPoint())) {
179 // closingline.isDegenerate() did not work, because it only checks for
180 // *exact* zero length, which goes wrong for relative coordinates and
181 // rounding errors...
182 // the closing line segment has zero-length. So stop before that one!
183 curve_endit = path_it.end_open();
184 }
185 }
186 if(helper_size > 0) {
187 drawNode(curve_it1->initialPoint());
188 }
189 nCurve->moveto(curve_it1->initialPoint());
190 Geom::Point start = Geom::Point(0,0);
191 while (curve_it1 != curve_endit) {
192 cubic = dynamic_cast<Geom::CubicBezier const *>(&*curve_it1);
193 Geom::Point point_at1 = curve_it1->initialPoint();
194 Geom::Point point_at2 = curve_it1->finalPoint();
195 Geom::Point point_at3 = curve_it1->finalPoint();
196 Geom::Point point_at4 = curve_it1->finalPoint();
197
198 if(start == Geom::Point(0,0)) {
199 start = point_at1;
200 }
201
202 if (cubic) {
203 point_at1 = (*cubic)[1];
204 point_at2 = (*cubic)[2];
205 }
206
207 if(path_it.closed() && curve_it2 == curve_endit) {
208 point_at4 = start;
209 }
210 if(curve_it2 != curve_endit) {
211 cubic = dynamic_cast<Geom::CubicBezier const *>(&*curve_it2);
212 if (cubic) {
213 point_at4 = (*cubic)[1];
214 }
215 }
216 Geom::Ray ray1(point_at2, point_at3);
217 Geom::Ray ray2(point_at3, point_at4);
218 double angle1 = Geom::deg_from_rad(ray1.angle());
219 double angle2 = Geom::deg_from_rad(ray2.angle());
220 if((smooth_angles >= std::abs(angle2 - angle1)) && !are_near(point_at4,point_at3) && !are_near(point_at2,point_at3)) {
221 double dist = Geom::distance(point_at2,point_at3);
222 Geom::Angle angleFixed = ray2.angle();
223 angleFixed -= Geom::Angle::from_degrees(180.0);
224 point_at2 = Geom::Point::polar(angleFixed, dist) + point_at3;
225 }
226 nCurve->curveto(point_at1, point_at2, curve_it1->finalPoint());
227 cubic = dynamic_cast<Geom::CubicBezier const *>(nCurve->last_segment());
228 if (cubic) {
229 point_at1 = (*cubic)[1];
230 point_at2 = (*cubic)[2];
231 if(helper_size > 0) {
232 if(!are_near((*cubic)[0],(*cubic)[1])) {
233 drawHandle((*cubic)[1]);
234 drawHandleLine((*cubic)[0],(*cubic)[1]);
235 }
236 if(!are_near((*cubic)[3],(*cubic)[2])) {
237 drawHandle((*cubic)[2]);
238 drawHandleLine((*cubic)[3],(*cubic)[2]);
239 }
240 }
241 }
242 if(helper_size > 0) {
243 drawNode(curve_it1->finalPoint());
244 }
245 ++curve_it1;
246 ++curve_it2;
247 }
248 if (path_it.closed()) {
249 nCurve->closepath_current();
250 }
251 tmp_path.push_back(nCurve->get_pathvector()[0]);
252 nCurve->reset();
253 delete nCurve;
254 }
255 result = tmp_path;
256 }
257
258 void
drawNode(Geom::Point p)259 LPESimplify::drawNode(Geom::Point p)
260 {
261 double r = radius_helper_nodes;
262 char const * svgd;
263 svgd = "M 0.55,0.5 A 0.05,0.05 0 0 1 0.5,0.55 0.05,0.05 0 0 1 0.45,0.5 0.05,0.05 0 0 1 0.5,0.45 0.05,0.05 0 0 1 0.55,0.5 Z M 0,0 1,0 1,1 0,1 Z";
264 Geom::PathVector pathv = sp_svg_read_pathv(svgd);
265 pathv *= Geom::Scale(r) * Geom::Translate(p - Geom::Point(0.5*r,0.5*r));
266 hp.push_back(pathv[0]);
267 hp.push_back(pathv[1]);
268 }
269
270 void
drawHandle(Geom::Point p)271 LPESimplify::drawHandle(Geom::Point p)
272 {
273 double r = radius_helper_nodes;
274 char const * svgd;
275 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";
276 Geom::PathVector pathv = sp_svg_read_pathv(svgd);
277 pathv *= Geom::Scale(r) * Geom::Translate(p - Geom::Point(0.35*r,0.35*r));
278 hp.push_back(pathv[0]);
279 }
280
281
282 void
drawHandleLine(Geom::Point p,Geom::Point p2)283 LPESimplify::drawHandleLine(Geom::Point p,Geom::Point p2)
284 {
285 Geom::Path path;
286 path.start( p );
287 double diameter = radius_helper_nodes;
288 if(helper_size > 0 && Geom::distance(p,p2) > (diameter * 0.35)) {
289 Geom::Ray ray2(p, p2);
290 p2 = p2 - Geom::Point::polar(ray2.angle(),(diameter * 0.35));
291 }
292 path.appendNew<Geom::LineSegment>( p2 );
293 hp.push_back(path);
294 }
295
296 void
addCanvasIndicators(SPLPEItem const *,std::vector<Geom::PathVector> & hp_vec)297 LPESimplify::addCanvasIndicators(SPLPEItem const */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
298 {
299 hp_vec.push_back(hp);
300 }
301
302
303 }; //namespace LivePathEffect
304 }; /* namespace Inkscape */
305
306 /*
307 Local Variables:
308 mode:c++
309 c-file-style:"stroustrup"
310 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
311 indent-tabs-mode:nil
312 fill-column:99
313 End:
314 */
315 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
316