2 Copyright (C) 2013 Paul Davis
3 Copyright (C) 2014 Robin Gareus <robin@gareus.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "canvas/xfade_curve.h"
26 #include "canvas/interpolated_curve.h"
27 #include "canvas/utils.h"
29 using namespace ArdourCanvas;
33 XFadeCurve::XFadeCurve (Group* parent)
35 , points_per_segment (32)
36 , _xfadeposition (Start)
37 , _outline_color (0x000000ff)
38 , _fill_color (0x22448880)
42 XFadeCurve::XFadeCurve (Group* parent, XFadePosition pos)
44 , points_per_segment (32)
45 , _xfadeposition (pos)
46 , _outline_color (0x000000ff)
47 , _fill_color (0x22448880)
52 XFadeCurve::compute_bounding_box () const
54 if (!_in.points.empty() && !_out.points.empty()) {
57 Points::const_iterator i;
59 if (!_in.points.empty()) {
60 i = _in.points.begin();
61 bbox.x0 = bbox.x1 = i->x;
62 bbox.y0 = bbox.y1 = i->y;
66 while (i != _in.points.end()) {
67 bbox.x0 = min (bbox.x0, i->x);
68 bbox.y0 = min (bbox.y0, i->y);
69 bbox.x1 = max (bbox.x1, i->x);
70 bbox.y1 = max (bbox.y1, i->y);
74 i = _out.points.begin();
75 bbox.x0 = bbox.x1 = i->x;
76 bbox.y0 = bbox.y1 = i->y;
79 if (!_out.points.empty()) {
80 i = _out.points.begin();
81 while (i != _out.points.end()) {
82 bbox.x0 = min (bbox.x0, i->x);
83 bbox.y0 = min (bbox.y0, i->y);
84 bbox.x1 = max (bbox.x1, i->x);
85 bbox.y1 = max (bbox.y1, i->y);
90 _bounding_box = bbox.expand (1.0);
93 _bounding_box = boost::optional<Rect> ();
96 _bounding_box_dirty = false;
100 XFadeCurve::set_inout (Points const & in, Points const & out)
102 if (_in.points == in && _out.points == out) {
108 _bounding_box_dirty = true;
114 XFadeCurve::set_points_per_segment (uint32_t n)
116 points_per_segment = n;
122 XFadeCurve::interpolate ()
124 _in.samples.clear ();
125 InterpolatedCurve::interpolate (_in.points, points_per_segment, CatmullRomCentripetal, false, _in.samples);
126 _in.n_samples = _in.samples.size();
128 _out.samples.clear ();
129 InterpolatedCurve::interpolate (_out.points, points_per_segment, CatmullRomCentripetal, false, _out.samples);
130 _out.n_samples = _out.samples.size();
134 XFadeCurve::get_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c) const
136 assert(c.points.size() > 1);
137 context->begin_new_path ();
140 if (c.points.size () == 2) {
142 window_space = item_to_window (c.points.front(), false);
143 context->move_to (window_space.x, window_space.y);
144 window_space = item_to_window (c.points.back(), false);
145 context->line_to (window_space.x, window_space.y);
149 /* find left and right-most sample */
150 Points::size_type left = 0;
151 Points::size_type right = c.n_samples;
153 for (Points::size_type idx = 0; idx < c.n_samples - 1; ++idx) {
155 window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
156 if (window_space.x >= area.x0) break;
158 for (Points::size_type idx = c.n_samples; idx > left + 1; --idx) {
159 window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
160 if (window_space.x <= area.x1) break;
164 /* draw line between samples */
165 window_space = item_to_window (Duple (c.samples[left].x, c.samples[left].y), false);
166 context->move_to (window_space.x, window_space.y);
167 for (uint32_t idx = left + 1; idx < right; ++idx) {
168 window_space = item_to_window (Duple (c.samples[idx].x, c.samples[idx].y), false);
169 context->line_to (window_space.x, window_space.y);
172 return context->copy_path ();
176 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
180 window_space = item_to_window (Duple(c.points.back().x, area.height()), false);
181 context->line_to (window_space.x, window_space.y);
182 window_space = item_to_window (Duple(c.points.front().x, area.height()), false);
183 context->line_to (window_space.x, window_space.y);
184 context->close_path();
186 window_space = item_to_window (Duple(c.points.back().x, 0.0), false);
187 context->line_to (window_space.x, window_space.y);
188 window_space = item_to_window (Duple(c.points.front().x, 0.0), false);
189 context->line_to (window_space.x, window_space.y);
190 context->close_path();
195 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
197 if (!_bounding_box) { return; }
198 if (_in.points.size() < 2) { return; }
199 if (_out.points.size() < 2) { return; }
201 Rect self = item_to_window (_bounding_box.get());
202 boost::optional<Rect> d = self.intersection (area);
204 Rect draw = d.get ();
207 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
210 /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
211 * they will still occur, but cairo's clipping will hide them.
213 draw = draw.expand (4.0);
215 Cairo::Path *path_in = get_path(draw, context, _in);
216 Cairo::Path *path_out = get_path(draw, context, _out);
218 Color outline_shaded = _outline_color;
219 outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
221 Color fill_shaded = _fill_color;
222 fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
224 #define IS (_xfadeposition == Start)
226 /* fill primary fade */
227 context->begin_new_path ();
228 context->append_path (IS ? *path_in : *path_out);
229 close_path(draw, context, IS ?_in : _out, false);
230 set_source_rgba (context, _fill_color);
233 /* fill background fade */
235 context->begin_new_path ();
236 context->append_path (IS ? *path_in : *path_out);
237 close_path(draw, context, IS ? _in : _out, true);
238 context->set_fill_rule (Cairo::FILL_RULE_EVEN_ODD);
240 context->begin_new_path ();
241 context->append_path (IS ? *path_out: *path_in);
242 close_path(draw, context, IS ? _out : _in, true);
243 set_source_rgba (context, fill_shaded);
244 context->set_fill_rule (Cairo::FILL_RULE_WINDING);
248 /* draw lines over fills */
249 set_source_rgba (context, IS ? _outline_color : outline_shaded);
250 context->set_line_width (IS ? 1.0 : .5);
252 context->begin_new_path ();
253 context->append_path (*path_in);
256 set_source_rgba (context, IS ? outline_shaded :_outline_color);
257 context->set_line_width (IS ? .5 : 1.0);
259 context->begin_new_path ();
260 context->append_path (*path_out);