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
|
// SPDX-License-Identifier: GPL-2.0-or-later
/**
* A class to represent a Bezier path.
*/
/*
* Author:
* Tavmjong Bah
*
* Copyright (C) 2020 Tavmjong Bah
*
* Rewrite of SPCanvasBPath
*
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
#include "canvas-item-bpath.h"
#include "color.h" // SP_RGBA_x_F
#include "display/curve.h"
#include "display/cairo-utils.h"
#include "helper/geom.h" // bounds_exact_transformed()
namespace Inkscape {
/**
* Create a null control bpath.
*/
CanvasItemBpath::CanvasItemBpath(CanvasItemGroup *group)
: CanvasItem(group)
{
_name = "CanvasItemBpath:Null";
_pickable = true; // For now, everyone gets events from this class!
}
/**
* Create a control bpath. Path is in document coordinates.
*/
CanvasItemBpath::CanvasItemBpath(CanvasItemGroup *group, Geom::PathVector path, bool phantom_line)
: CanvasItem(group)
, _path(std::move(path))
, _phantom_line(phantom_line)
{
_name = "CanvasItemBpath";
_pickable = true; // For now, everyone gets events from this class!
request_update(); // Render immediately or temporary bpaths won't show.
}
/**
* Set a control bpath. Curve is in document coordinates.
*/
void CanvasItemBpath::set_bpath(SPCurve const *curve, bool phantom_line)
{
set_bpath(curve ? curve->get_pathvector() : Geom::PathVector(), phantom_line);
}
/**
* Set a control bpath. Path is in document coordinates.
*/
void CanvasItemBpath::set_bpath(Geom::PathVector path, bool phantom_line)
{
defer([=, path = std::move(path)] () mutable {
_path = std::move(path);
_phantom_line = phantom_line;
request_update();
});
}
/**
* Set the fill color and fill rule.
*/
void CanvasItemBpath::set_fill(uint32_t fill, SPWindRule fill_rule)
{
defer([=] {
if (_fill == fill && _fill_rule == fill_rule) return;
_fill = fill;
_fill_rule = fill_rule;
request_redraw();
});
}
void CanvasItemBpath::set_dashes(std::vector<double> &&dashes)
{
defer([=, dashes = std::move(dashes)] () mutable {
_dashes = std::move(dashes);
});
}
/**
* Set the stroke width
*/
void CanvasItemBpath::set_stroke_width(double width)
{
defer([=] {
if (_stroke_width == width) return;
_stroke_width = width;
request_redraw();
});
}
/**
* Returns distance between point in canvas units and nearest point on bpath.
*/
double CanvasItemBpath::closest_distance_to(Geom::Point const &p) const
{
double d = Geom::infinity();
// Convert p to document coordinates (quicker than converting path to canvas units).
Geom::Point p_doc = p * affine().inverse();
_path.nearestTime(p_doc, &d);
d *= affine().descrim(); // Uniform scaling and rotation only.
return d;
}
/**
* Returns true if point p (in canvas units) is within tolerance (canvas units) distance of bpath.
*/
bool CanvasItemBpath::contains(Geom::Point const &p, double tolerance)
{
if (tolerance == 0) {
tolerance = 1; // Need a minimum tolerance value or always returns false.
}
// Check for 'inside' a filled bpath if a fill is being used.
if ((_fill & 0xff) != 0) {
Geom::Point p_doc = p * affine().inverse();
if (_path.winding(p_doc) % 2 != 0) {
return true;
}
}
// Otherwise see how close we are to the outside line.
return closest_distance_to(p) < tolerance;
}
/**
* Update and redraw control bpath.
*/
void CanvasItemBpath::_update(bool)
{
// Queue redraw of old area (erase previous content).
request_redraw();
if (_path.empty()) {
_bounds = {};
return;
}
_bounds = expandedBy(bounds_exact_transformed(_path, affine()), 2);
// Queue redraw of new area
request_redraw();
}
/**
* Render bpath to screen via Cairo.
*/
void CanvasItemBpath::_render(Inkscape::CanvasItemBuffer &buf) const
{
bool do_fill = (_fill & 0xff) != 0; // Not invisible.
bool do_stroke = (_stroke & 0xff) != 0; // Not invisible.
if (!do_fill && !do_stroke) {
// Both fill and stroke invisible.
return;
}
buf.cr->save();
// Setup path
buf.cr->set_tolerance(0.5);
buf.cr->begin_new_path();
feed_pathvector_to_cairo(buf.cr->cobj(), _path, affine(), buf.rect,
/* optimize_stroke */ !do_fill, 1);
// Do fill
if (do_fill) {
buf.cr->set_source_rgba(SP_RGBA32_R_F(_fill), SP_RGBA32_G_F(_fill),
SP_RGBA32_B_F(_fill), SP_RGBA32_A_F(_fill));
buf.cr->set_fill_rule(_fill_rule == SP_WIND_RULE_EVENODD ?
Cairo::FILL_RULE_EVEN_ODD : Cairo::FILL_RULE_WINDING);
buf.cr->fill_preserve();
}
// Do stroke
if (do_stroke) {
if (!_dashes.empty()) {
buf.cr->set_dash(_dashes, 0.0); // 0.0 is offset
}
if (_phantom_line) {
buf.cr->set_source_rgba(1.0, 1.0, 1.0, 0.25);
buf.cr->set_line_width(2.0);
buf.cr->stroke_preserve();
}
buf.cr->set_source_rgba(SP_RGBA32_R_F(_stroke), SP_RGBA32_G_F(_stroke),
SP_RGBA32_B_F(_stroke), SP_RGBA32_A_F(_stroke));
buf.cr->set_line_width(_stroke_width);
buf.cr->stroke();
} else {
buf.cr->begin_new_path(); // Clears path
}
buf.cr->restore();
}
} // 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 :
|