1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Authors:
3 *
4 * Liam P White
5 *
6 * Copyright (C) 2014 Authors
7 *
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10
11 #include "live_effects/parameter/enum.h"
12 #include "live_effects/fill-conversion.h"
13 #include "helper/geom-pathstroke.h"
14
15 #include "desktop-style.h"
16
17 #include "display/curve.h"
18
19 #include "object/sp-item-group.h"
20 #include "object/sp-shape.h"
21 #include "style.h"
22
23 #include "svg/css-ostringstream.h"
24 #include "svg/svg-color.h"
25
26 #include <2geom/elliptical-arc.h>
27
28 #include "lpe-jointype.h"
29
30 // TODO due to internal breakage in glibmm headers, this must be last:
31 #include <glibmm/i18n.h>
32
33 namespace Inkscape {
34 namespace LivePathEffect {
35
36 static const Util::EnumData<unsigned> JoinTypeData[] = {
37 // clang-format off
38 {JOIN_BEVEL, N_("Beveled"), "bevel"},
39 {JOIN_ROUND, N_("Rounded"), "round"},
40 {JOIN_MITER, N_("Miter"), "miter"},
41 {JOIN_MITER_CLIP, N_("Miter Clip"), "miter-clip"},
42 {JOIN_EXTRAPOLATE, N_("Extrapolated arc"), "extrp_arc"},
43 {JOIN_EXTRAPOLATE1, N_("Extrapolated arc Alt1"), "extrp_arc1"},
44 {JOIN_EXTRAPOLATE2, N_("Extrapolated arc Alt2"), "extrp_arc2"},
45 {JOIN_EXTRAPOLATE3, N_("Extrapolated arc Alt3"), "extrp_arc3"},
46 // clang-format on
47 };
48
49 static const Util::EnumData<unsigned> CapTypeData[] = {
50 {BUTT_FLAT, N_("Butt"), "butt"},
51 {BUTT_ROUND, N_("Rounded"), "round"},
52 {BUTT_SQUARE, N_("Square"), "square"},
53 {BUTT_PEAK, N_("Peak"), "peak"},
54 //{BUTT_LEANED, N_("Leaned"), "leaned"}
55 };
56
57 static const Util::EnumDataConverter<unsigned> CapTypeConverter(CapTypeData, sizeof(CapTypeData)/sizeof(*CapTypeData));
58 static const Util::EnumDataConverter<unsigned> JoinTypeConverter(JoinTypeData, sizeof(JoinTypeData)/sizeof(*JoinTypeData));
59
LPEJoinType(LivePathEffectObject * lpeobject)60 LPEJoinType::LPEJoinType(LivePathEffectObject *lpeobject) :
61 Effect(lpeobject),
62 line_width(_("Line width"), _("Thickness of the stroke"), "line_width", &wr, this, 1.),
63 linecap_type(_("Line cap"), _("The end shape of the stroke"), "linecap_type", CapTypeConverter, &wr, this, BUTT_FLAT),
64 linejoin_type(_("Join:"), _("Determines the shape of the path's corners"), "linejoin_type", JoinTypeConverter, &wr, this, JOIN_EXTRAPOLATE),
65 //start_lean(_("Start path lean"), _("Start path lean"), "start_lean", &wr, this, 0.),
66 //end_lean(_("End path lean"), _("End path lean"), "end_lean", &wr, this, 0.),
67 miter_limit(_("Miter limit:"), _("Maximum length of the miter join (in units of stroke width)"), "miter_limit", &wr, this, 100.),
68 attempt_force_join(_("Force miter"), _("Overrides the miter limit and forces a join."), "attempt_force_join", &wr, this, true)
69 {
70 show_orig_path = true;
71 registerParameter(&linecap_type);
72 registerParameter(&line_width);
73 registerParameter(&linejoin_type);
74 //registerParameter(&start_lean);
75 //registerParameter(&end_lean);
76 registerParameter(&miter_limit);
77 registerParameter(&attempt_force_join);
78 //start_lean.param_set_range(-1,1);
79 //start_lean.param_set_increments(0.1, 0.1);
80 //start_lean.param_set_digits(4);
81 //end_lean.param_set_range(-1,1);
82 //end_lean.param_set_increments(0.1, 0.1);
83 //end_lean.param_set_digits(4);
84 }
85
86 LPEJoinType::~LPEJoinType()
87 = default;
88
doOnApply(SPLPEItem const * lpeitem)89 void LPEJoinType::doOnApply(SPLPEItem const* lpeitem)
90 {
91 if (!SP_IS_SHAPE(lpeitem)) {
92 return;
93 }
94
95 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
96 SPShape* item = SP_SHAPE(lpeitem);
97 double width = (lpeitem && lpeitem->style) ? lpeitem->style->stroke_width.computed : 1.;
98
99 lpe_shape_convert_stroke_and_fill(item);
100
101 Glib::ustring pref_path = (Glib::ustring)"/live_effects/" +
102 (Glib::ustring)LPETypeConverter.get_key(effectType()).c_str() +
103 (Glib::ustring)"/" +
104 (Glib::ustring)"line_width";
105
106 bool valid = prefs->getEntry(pref_path).isValid();
107
108 if (!valid) {
109 line_width.param_set_value(width);
110 }
111
112 line_width.write_to_SVG();
113 }
114
transform_multiply(Geom::Affine const & postmul,bool)115 void LPEJoinType::transform_multiply(Geom::Affine const &postmul, bool /*set*/)
116 {
117 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
118 bool transform_stroke = prefs ? prefs->getBool("/options/transform/stroke", true) : true;
119 if (transform_stroke) {
120 line_width.param_transform_multiply(postmul, false);
121 }
122 }
123
124 //from LPEPowerStroke -- sets stroke color from existing fill color
125
doOnRemove(SPLPEItem const * lpeitem)126 void LPEJoinType::doOnRemove(SPLPEItem const* lpeitem)
127 {
128 if (!SP_IS_SHAPE(lpeitem)) {
129 return;
130 }
131
132 lpe_shape_revert_stroke_and_fill(SP_SHAPE(lpeitem), line_width);
133 }
134
doEffect_path(Geom::PathVector const & path_in)135 Geom::PathVector LPEJoinType::doEffect_path(Geom::PathVector const & path_in)
136 {
137 Geom::PathVector ret;
138 for (const auto & i : path_in) {
139 Geom::PathVector tmp = Inkscape::outline(i, line_width,
140 (attempt_force_join ? std::numeric_limits<double>::max() : miter_limit),
141 static_cast<LineJoinType>(linejoin_type.get_value()),
142 static_cast<LineCapType>(linecap_type.get_value()));
143 ret.insert(ret.begin(), tmp.begin(), tmp.end());
144 }
145
146 return ret;
147 }
148
149 } // namespace LivePathEffect
150 } // namespace Inkscape
151
152 /*
153 Local Variables:
154 mode:c++
155 c-file-style:"stroustrup"
156 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
157 indent-tabs-mode:nil
158 fill-column:99
159 End:
160 */
161 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8 :
162