graph_cairo_draw.cc 75.2 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2018 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
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
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "graph.hh"

#ifdef HAVE_CAIROMM

#include "graph_filtering.hh"

#include <boost/python.hpp>
#include <boost/utility/enable_if.hpp>

#include "graph_selectors.hh"
#include "graph_properties.hh"

#include <iostream>
31
#include <array>
Tiago Peixoto's avatar
Tiago Peixoto committed
32

33
#include "hash_map_wrap.hh"
34
#include "demangle.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
35

36
37
38
#include "coroutine.hh"
#include "graph_python_interface.hh"

Tiago Peixoto's avatar
Tiago Peixoto committed
39
40
41
#include <cairommconfig.h>
#include <cairomm/context.h>
#include <cairomm/surface.h>
42
#include PYCAIRO_HEADER
Tiago Peixoto's avatar
Tiago Peixoto committed
43
44
45

#include <boost/mpl/map/map50.hpp>

46
47
#include <chrono>

Tiago Peixoto's avatar
Tiago Peixoto committed
48
49
50
51
52
53
54
55
56
using namespace std;
using namespace boost;
using namespace graph_tool;

enum vertex_attr_t {
    VERTEX_SHAPE = 100,
    VERTEX_COLOR,
    VERTEX_FILL_COLOR,
    VERTEX_SIZE,
57
    VERTEX_ASPECT,
58
    VERTEX_ROTATION,
59
    VERTEX_ANCHOR,
Tiago Peixoto's avatar
Tiago Peixoto committed
60
61
62
    VERTEX_PENWIDTH,
    VERTEX_HALO,
    VERTEX_HALO_COLOR,
63
    VERTEX_HALO_SIZE,
Tiago Peixoto's avatar
Tiago Peixoto committed
64
65
66
    VERTEX_TEXT,
    VERTEX_TEXT_COLOR,
    VERTEX_TEXT_POSITION,
67
68
    VERTEX_TEXT_ROTATION,
    VERTEX_TEXT_OFFSET,
Tiago Peixoto's avatar
Tiago Peixoto committed
69
70
71
    VERTEX_FONT_FAMILY,
    VERTEX_FONT_SLANT,
    VERTEX_FONT_WEIGHT,
72
    VERTEX_FONT_SIZE,
73
74
    VERTEX_SURFACE,
    VERTEX_PIE_FRACTIONS,
75
    VERTEX_PIE_COLORS,
Tiago Peixoto's avatar
Tiago Peixoto committed
76
77
78
79
80
81
82
83
84
};

enum edge_attr_t {
    EDGE_COLOR = 200,
    EDGE_PENWIDTH,
    EDGE_START_MARKER,
    EDGE_MID_MARKER,
    EDGE_END_MARKER,
    EDGE_MARKER_SIZE,
85
    EDGE_MID_MARKER_POSITION,
86
    EDGE_CONTROL_POINTS,
87
    EDGE_DASH_STYLE,
Tiago Peixoto's avatar
Tiago Peixoto committed
88
    EDGE_GRADIENT,
89
90
91
92
93
94
95
96
    EDGE_TEXT,
    EDGE_TEXT_COLOR,
    EDGE_TEXT_DISTANCE,
    EDGE_TEXT_PARALLEL,
    EDGE_FONT_FAMILY,
    EDGE_FONT_SLANT,
    EDGE_FONT_WEIGHT,
    EDGE_FONT_SIZE,
97
98
    EDGE_SLOPPY,
    EDGE_SEAMLESS
Tiago Peixoto's avatar
Tiago Peixoto committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
};

enum vertex_shape_t {
    SHAPE_CIRCLE = 300,
    SHAPE_TRIANGLE,
    SHAPE_SQUARE,
    SHAPE_PENTAGON,
    SHAPE_HEXAGON,
    SHAPE_HEPTAGON,
    SHAPE_OCTAGON,
    SHAPE_DOUBLE_CIRCLE,
    SHAPE_DOUBLE_TRIANGLE,
    SHAPE_DOUBLE_SQUARE,
    SHAPE_DOUBLE_PENTAGON,
    SHAPE_DOUBLE_HEXAGON,
    SHAPE_DOUBLE_HEPTAGON,
115
    SHAPE_DOUBLE_OCTAGON,
116
117
    SHAPE_PIE,
    SHAPE_NONE
Tiago Peixoto's avatar
Tiago Peixoto committed
118
119
120
121
122
123
124
125
126
127
128
129
};

enum edge_marker_t {
    MARKER_SHAPE_NONE = 400,
    MARKER_SHAPE_ARROW,
    MARKER_SHAPE_CIRCLE,
    MARKER_SHAPE_SQUARE,
    MARKER_SHAPE_DIAMOND,
    MARKER_SHAPE_BAR
};

typedef pair<double, double> pos_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
130
typedef std::tuple<double, double, double, double> color_t;
131
typedef gt_hash_map<int, boost::any> attrs_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
132

133
typedef boost::mpl::map43<
Tiago Peixoto's avatar
Tiago Peixoto committed
134
135
136
137
138
    boost::mpl::pair<boost::mpl::int_<VERTEX_SHAPE>, vertex_shape_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_FILL_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_SIZE>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_ASPECT>, double>,
139
    boost::mpl::pair<boost::mpl::int_<VERTEX_ROTATION>, double>,
Tiago Peixoto's avatar
Tiago Peixoto committed
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
    boost::mpl::pair<boost::mpl::int_<VERTEX_ANCHOR>, int32_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_PENWIDTH>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_HALO>, uint8_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_HALO_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_HALO_SIZE>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT>, string>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_POSITION>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_ROTATION>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_OFFSET>, vector<double> >,
    boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_FAMILY>, string>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_SLANT>, int32_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_WEIGHT>, int32_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_SIZE>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_SURFACE>, boost::python::object>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_PIE_FRACTIONS>, vector<double> >,
    boost::mpl::pair<boost::mpl::int_<VERTEX_PIE_COLORS>, vector<color_t> >,
    boost::mpl::pair<boost::mpl::int_<EDGE_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_PENWIDTH>, double>,
    boost::mpl::pair<boost::mpl::int_<EDGE_START_MARKER>, edge_marker_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_MID_MARKER>, edge_marker_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_END_MARKER>, edge_marker_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_MARKER_SIZE>, double>,
    boost::mpl::pair<boost::mpl::int_<EDGE_MID_MARKER_POSITION>, double>,
    boost::mpl::pair<boost::mpl::int_<EDGE_CONTROL_POINTS>, vector<double> >,
    boost::mpl::pair<boost::mpl::int_<EDGE_DASH_STYLE>, vector<double> >,
    boost::mpl::pair<boost::mpl::int_<EDGE_GRADIENT>, vector<double> >,
    boost::mpl::pair<boost::mpl::int_<EDGE_TEXT>, string>,
    boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_DISTANCE>, double>,
    boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_PARALLEL>, uint8_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_FONT_FAMILY>, string>,
    boost::mpl::pair<boost::mpl::int_<EDGE_FONT_SLANT>, int32_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_FONT_WEIGHT>, int32_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_FONT_SIZE>, double>,
175
176
    boost::mpl::pair<boost::mpl::int_<EDGE_SLOPPY>, uint8_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_SEAMLESS>, uint8_t> >
Tiago Peixoto's avatar
Tiago Peixoto committed
177
178
        attr_types;

179
namespace std
Tiago Peixoto's avatar
Tiago Peixoto committed
180
181
182
{
ostream& operator<<(ostream& out, const color_t& c)
{
Tiago Peixoto's avatar
Tiago Peixoto committed
183
    out << std::get<0>(c) << " " << std::get<1>(c) << " " << std::get<2>(c) << " " << std::get<3>(c);
Tiago Peixoto's avatar
Tiago Peixoto committed
184
185
186
    return out;
}

187
istream& operator>>(istream& in, color_t& c)
Tiago Peixoto's avatar
Tiago Peixoto committed
188
{
Tiago Peixoto's avatar
Tiago Peixoto committed
189
    in >> std::get<0>(c) >> std::get<1>(c) >> std::get<2>(c) >> std::get<3>(c);
Tiago Peixoto's avatar
Tiago Peixoto committed
190
191
192
193
    return in;
}
}

194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
istream& operator>>(istream& in, vertex_shape_t& c)
{
    int tmp;
    in >> tmp;
    c = vertex_shape_t(tmp);
    return in;
}

istream& operator>>(istream& in, edge_marker_t& c)
{
    int tmp;
    in >> tmp;
    c = edge_marker_t(tmp);
    return in;
}

210
211
212
213
214
215
216
217
218
namespace boost
{
template <>
string lexical_cast<string,python::object>(const python::object& val)
{
    return python::extract<string>(val);
}
}

Tiago Peixoto's avatar
Tiago Peixoto committed
219
220
221
222
223
224
225
226
template <class T1, class T2>
struct specific_convert;

template <class Type1, class Type2>
struct Converter
{
    Type1 operator()(const Type2& v) const
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
227
        return do_convert(v, std::is_convertible<Type2,Type1>());
Tiago Peixoto's avatar
Tiago Peixoto committed
228
229
    }

Tiago Peixoto's avatar
Tiago Peixoto committed
230
    Type1 do_convert(const Type2& v, std::true_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
231
232
233
234
    {
        return Type1(v);
    }

Tiago Peixoto's avatar
Tiago Peixoto committed
235
    Type1 do_convert(const Type2& v, std::false_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
236
    {
237
238
239
240
241
242
        try
        {
            return specific_convert<Type1,Type2>()(v);
        }
        catch (bad_lexical_cast&)
        {
243
244
            string name1 = name_demangle(typeid(Type1).name());
            string name2 = name_demangle(typeid(Type2).name());
245
246
247
248
249
            string val_name;
            try
            {
                val_name = lexical_cast<string>(v);
            }
250
            catch (bad_lexical_cast&)
251
252
253
254
255
256
            {
                val_name = "<no lexical cast available>";
            }
            throw GraphException("error converting from type '" + name2 +
                                 "' to type '" + name1 + "', val: " + val_name);
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
257
258
259
260
261
262
263
    }

    template <class T1, class T2, class Enable = void>
    struct specific_convert
    {
        T1 operator()(const T2& v) const
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
264
            return dispatch(v, typename std::is_convertible<T2, T1>::type());
Tiago Peixoto's avatar
Tiago Peixoto committed
265
266
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
267
        T1 dispatch(const T2& v, std::true_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
268
269
270
271
        {
            return T1(v);
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
272
        T1 dispatch(const T2& v, std::false_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
        {
            return lexical_cast<T1>(v);
        }
    };

    template <class T1> // noop
    struct specific_convert<T1, T1>
    {
        T1 operator()(const T1& v) const
        {
            return v;
        }
    };

    // specific specializations
    // string
    template <class T1>
    struct specific_convert<T1, string>
    {
        T1 operator()(const string& v) const
        {
            //uint8_t is not char, it is bool!
Tiago Peixoto's avatar
Tiago Peixoto committed
295
            if (std::is_same<T1, uint8_t>::value)
Tiago Peixoto's avatar
Tiago Peixoto committed
296
297
298
299
300
301
                return convert<T1,int>()(lexical_cast<int>(v));
            else
                return lexical_cast<T1>(v);
        }
    };

Tiago Peixoto's avatar
Tiago Peixoto committed
302
    // boost::python::object
Tiago Peixoto's avatar
Tiago Peixoto committed
303
    template <class T1>
Tiago Peixoto's avatar
Tiago Peixoto committed
304
    struct specific_convert<T1,boost::python::object>
Tiago Peixoto's avatar
Tiago Peixoto committed
305
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
306
        T1 operator()(const boost::python::object& v) const
Tiago Peixoto's avatar
Tiago Peixoto committed
307
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
308
            boost::python::extract<T1> x(v);
Tiago Peixoto's avatar
Tiago Peixoto committed
309
310
311
312
313
314
315
316
317
            if (x.check())
                return x();
            else
                throw bad_lexical_cast();
        }
    };

    template <class T2>
    struct specific_convert<string, T2,
Tiago Peixoto's avatar
Tiago Peixoto committed
318
319
320
                            typename std::enable_if
                            <boost::mpl::not_<
                                 std::is_same<T2,boost::python::object> >::type::value>::type>
Tiago Peixoto's avatar
Tiago Peixoto committed
321
322
323
324
    {
        string operator()(const T2& v) const
        {
            //uint8_t is not char, it is bool!
Tiago Peixoto's avatar
Tiago Peixoto committed
325
            if (std::is_same<T2, uint8_t>::value)
Tiago Peixoto's avatar
Tiago Peixoto committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
                return convert<string, int>()(lexical_cast<int>(v));
            else
                return lexical_cast<string>(v);
        }
    };

    // vectors
    template <class T1, class T2>
    struct specific_convert<vector<T1>, vector<T2> >
    {
        vector<T1> operator()(const vector<T2>& v) const
        {
            vector<T1> v2(v.size());
            convert<T1,T2> c;
            for (size_t i = 0; i < v.size(); ++i)
                v2[i] = c(v[i]);
            return v2;
        }
    };

346
    // color_t
Tiago Peixoto's avatar
Tiago Peixoto committed
347
348
349
350
351
352
    template <class T2>
    struct specific_convert<color_t, vector<T2> >
    {
        specific_convert<double, T2> c;
        color_t operator()(const vector<T2>& cv) const
        {
353
354
            if (cv.size() < 3)
                return std::make_tuple(0., 0., 0., 0.);
Tiago Peixoto's avatar
Tiago Peixoto committed
355
            if (cv.size() < 4)
356
                return std::make_tuple(c(cv[0]), c(cv[1]), c(cv[2]), 1.);
Tiago Peixoto's avatar
Tiago Peixoto committed
357
            return std::make_tuple(c(cv[0]), c(cv[1]), c(cv[2]), c(cv[3]));
Tiago Peixoto's avatar
Tiago Peixoto committed
358
359
360
        }
    };

361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
    // vector<color_t>
    template <class T2>
    struct specific_convert<vector<color_t>, vector<T2> >
    {
        specific_convert<double, T2> c;
        vector<color_t> operator()(const vector<T2>& cv) const
        {
            if (cv.size() < 4)
                throw bad_lexical_cast();
            vector<color_t> color;
            for (size_t i = 0; i < cv.size() / 4; ++i)
            {
                if (4 * i + 3 >= cv.size())
                    throw bad_lexical_cast();
                color.push_back(std::make_tuple(c(cv[4*i]), c(cv[4*i + 1]),
                                                c(cv[4*i + 2]), c(cv[4*i + 3])));
            }
            return color;
        }
    };
Tiago Peixoto's avatar
Tiago Peixoto committed
381
382
383
384

    // vertex_shape_t
    template <class T2>
    struct specific_convert<vertex_shape_t, T2,
Tiago Peixoto's avatar
Tiago Peixoto committed
385
386
387
388
389
                            typename std::enable_if
                            <boost::mpl::and_<typename boost::mpl::not_<
                                                  std::is_same<T2,string> >::type,
                                              typename boost::mpl::not_<
                                                  std::is_same<T2,boost::python::object> >::type>::type::value>::type>
Tiago Peixoto's avatar
Tiago Peixoto committed
390
391
392
393
394
395
396
397
398
399
400
    {
        specific_convert<int, T2> c;
        vertex_shape_t operator()(const T2& v) const
        {
            return static_cast<vertex_shape_t>(c(v));
        }
    };

    // edge_marker_t
    template <class T2>
    struct specific_convert<edge_marker_t, T2,
Tiago Peixoto's avatar
Tiago Peixoto committed
401
402
403
404
405
                            typename std::enable_if
                            <boost::mpl::and_<typename boost::mpl::not_<
                                                  std::is_same<T2,string> >::type,
                                              typename boost::mpl::not_<
                                                  std::is_same<T2,boost::python::object> >::type>::type::value>::type>
Tiago Peixoto's avatar
Tiago Peixoto committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
    {
        specific_convert<int, T2> c;
        edge_marker_t operator()(const T2& v) const
        {
            return static_cast<edge_marker_t>(c(v));
        }
    };
};


template <class Descriptor>
class AttrDict
{
public:
    AttrDict(Descriptor descriptor, attrs_t& attrs, attrs_t& defaults)
        : _descriptor(descriptor), _attrs(attrs), _defaults(defaults) {}

    template <class Value>
    Value get(int k)
    {
426
        auto iter = _attrs.find(k);
Tiago Peixoto's avatar
Tiago Peixoto committed
427
428
429
430
431
432
433
434
435
436
437
438
439
        if (iter != _attrs.end())
        {
            typedef DynamicPropertyMapWrap<Value, Descriptor, Converter> pmap_t;
            pmap_t pmap(any_cast<pmap_t>(iter->second));
            return pmap.get(_descriptor);
        }
        try
        {
            return any_cast<Value>(_defaults[k]);
        }
        catch (bad_any_cast&)
        {
            throw ValueException("Error getting attribute " + lexical_cast<string>(k) +
440
441
                                 ", wanted: " + name_demangle(typeid(Value).name()) +
                                 ", got: " + name_demangle(_defaults[k].type().name()));
Tiago Peixoto's avatar
Tiago Peixoto committed
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
        }
    }

private:
    Descriptor _descriptor;
    attrs_t& _attrs;
    attrs_t& _defaults;
};

void draw_polygon(size_t N, double radius, Cairo::Context& cr)
{
    cr.save();
    cr.rotate(M_PI * (1. / 2 - 1. / N));
    cr.move_to(radius, 0);
    for (size_t i = 0; i < N; ++i)
    {
        double angle = (2 * M_PI * (i + 1)) / N;
        cr.line_to(radius * cos(angle), radius * sin(angle));
    }
    cr.close_path();
    cr.restore();
}

double get_polygon_anchor(size_t N, double radius, double angle)
{
    double theta = angle - M_PI * (1. / 2 - 1. / N);
    if (N % 2 == 0)
        theta += M_PI / N;
    if (theta > 2 * M_PI)
        theta -= 2 * M_PI;
    if (theta < 2 * M_PI)
        theta += 2 * M_PI;
    theta = fmod(theta, 2. * M_PI / N);
    if (theta > M_PI / N)
        theta -= 2. * M_PI / N;
    return radius * cos(M_PI / N) / cos(theta);
}

480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510

void draw_pie(double radius, const vector<double>& f,
              const vector<color_t>& colors, Cairo::Context& cr)
{
    if (colors.empty())
        throw ValueException("No pie colors!");
    double s = 0;
    for (size_t i = 0; i < f.size(); ++i)
        s += f[i];
    double last = 0;
    double pos = 0;
    cr.save();
    cr.begin_new_path();
    for (size_t i = 0; i < f.size(); ++i)
    {
        pos += f[i];
        double angle = (2 * pos * M_PI) / s;
        cr.move_to(0, 0);
        cr.arc(0, 0, radius, last, angle);
        last = angle;
        size_t j = i % colors.size();
        cr.set_source_rgba(get<0>(colors[j]),
                           get<1>(colors[j]),
                           get<2>(colors[j]),
                           get<3>(colors[j]));
        cr.fill();
    }
    cr.restore();
}


Tiago Peixoto's avatar
Tiago Peixoto committed
511
512
513
514
515
516
517
518
519
520
void move_radially(pos_t& pos, const pos_t& origin, double dr)
{
    double angle = atan2(pos.second - origin.second,
                         pos.first - origin.first);
    if (angle < 0)
        angle += 2 * M_PI;
    pos.first += dr * cos(angle);
    pos.second += dr * sin(angle);
}

521
double get_user_dist(Cairo::Context& cr, double norm = 1.)
Tiago Peixoto's avatar
Tiago Peixoto committed
522
{
523
    double x = norm / sqrt(2.), y = norm / sqrt(2.);
Tiago Peixoto's avatar
Tiago Peixoto committed
524
525
526
527
    cr.device_to_user_distance(x, y);
    return sqrt(x * x + y * y);
}

528
529
530
531
532
533
534
535
536
537
538
539
void get_surface_size(Cairo::RefPtr<Cairo::Surface> sfc,
                      double& width, double& height)
{
    Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(sfc);
    double x1, x2, y1, y2;

    cr->get_clip_extents(x1, y1, x2, y2);

    width = x2 - x1;
    height = y2 - y1;
}

540
541
542
543
double dist(const pos_t& p1, const pos_t& p2)
{
    return sqrt(pow(p1.first - p2.first, 2) + pow(p1.second - p2.second, 2));
};
Tiago Peixoto's avatar
Tiago Peixoto committed
544

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
double get_spline_len(const vector<double>& cts)
{
    double len = 0;
    for (size_t i = 0; i + 7 < cts.size(); i += 6)
    {
        double dx = cts[i + 6] - cts[i];
        double dy = cts[i + 6 + 1] - cts[i + 1];
        len += sqrt(dx * dx + dy * dy);
    }
    return len;
}

pos_t get_spline_point(const vector<double>& cts, double d)
{
    pos_t p;
    double pos = 0;
    for (size_t i = 0; i + 7 < cts.size(); i += 6)
    {
        double dx = cts[i + 6] - cts[i];
        double dy = cts[i + 6 + 1] - cts[i + 1];
        double l = sqrt(dx * dx + dy * dy);
        if (l < 1e-8)
            continue;
        if (pos + l >= d || i + 13 >= cts.size())
        {
            double t = 1 - (pos + l - d) / l;

            p.first = pow(1 - t, 3) * cts[i] +
                3 * t * pow(1 - t, 2) * cts[i + 2] +
                3 * t * t * (1 - t) * cts[i + 4] +
                t * t * t * cts[i + 6];

            p.second = pow(1 - t, 3) * cts[i + 1] +
                3 * t * pow(1 - t, 2) * cts[i + 3] +
                3 * t * t * (1 - t) * cts[i + 5] +
                t * t * t * cts[i + 7];
            break;
        }
        pos += l;
    }
    return p;
}

pos_t get_spline_diff(const vector<double>& cts, double d)
{
    pos_t diff;
    double pos = 0;
    for (size_t i = 0; i + 7 < cts.size(); i += 6)
    {
        double dx = cts[i + 6] - cts[i];
        double dy = cts[i + 6 + 1] - cts[i + 1];
        double l = sqrt(dx * dx + dy * dy);
        if (l < 1e-8)
            continue;
        if (pos + l >= d || i + 13 >= cts.size())
        {
            double t = 1 - (pos + l - d) / l;

            diff.first = -3 * pow(1 - t, 2) * cts[i] +
                (3 * pow(1 - t, 2) - 6 * t * (1-t)) * cts[i + 2] +
                (-3 * t * t + 6 * t * (1 - t)) * cts[i + 4] +
                3 * t * t * cts[i + 6];

            diff.second= -3 * pow(1 - t, 2) * cts[i + 1] +
                (3 * pow(1 - t, 2) - 6 * t * (1-t)) * cts[i + 3] +
                (-3 * t * t + 6 * t * (1 - t)) * cts[i + 5] +
                3 * t * t * cts[i + 7];
            break;
        }
        pos += l;
    }
    return diff;
}
Tiago Peixoto's avatar
Tiago Peixoto committed
618
619
620
621
622
623
624
625
626
627
628
629

template <class Descriptor>
class VertexShape
{
public:
    VertexShape(pos_t& pos, AttrDict<Descriptor> attrs)
        : _pos(pos), _attrs(attrs)

    {}

    double get_size(Cairo::Context& cr)
    {
630
        double size = _attrs.template get<double>(VERTEX_SIZE);
631
        size = get_user_dist(cr, size);
Tiago Peixoto's avatar
Tiago Peixoto committed
632

633
        string text = _attrs.template get<string>(VERTEX_TEXT);
634
        if (!text.empty())
Tiago Peixoto's avatar
Tiago Peixoto committed
635
        {
636
            double text_pos = _attrs.template get<double>(VERTEX_TEXT_POSITION);
637
            if (text_pos == -1)
Tiago Peixoto's avatar
Tiago Peixoto committed
638
            {
639
640
641
                cr.select_font_face(_attrs.template get<string>(VERTEX_FONT_FAMILY),
                                    static_cast<Cairo::FontSlant>(_attrs.template get<int32_t>(VERTEX_FONT_SLANT)),
                                    static_cast<Cairo::FontWeight>(_attrs.template get<int32_t>(VERTEX_FONT_WEIGHT)));
642
                cr.set_font_size(get_user_dist(cr, _attrs.template get<double>(VERTEX_FONT_SIZE)));
Tiago Peixoto's avatar
Tiago Peixoto committed
643
644
645
                Cairo::TextExtents extents;
                cr.get_text_extents(text, extents);
                double s = max(extents.width, extents.height) * 1.4;
646
                vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
647
                if (shape >= SHAPE_DOUBLE_CIRCLE && shape != SHAPE_PIE)
Tiago Peixoto's avatar
Tiago Peixoto committed
648
649
                {
                    s /= 0.7;
650
                    double pw = _attrs.template get<double>(VERTEX_PENWIDTH);
651
                    pw = get_user_dist(cr, pw);
Tiago Peixoto's avatar
Tiago Peixoto committed
652
653
654
655
656
657
658
659
                    s += pw;
                }
                size = max(size, s);
            }
        }
        return size;
    }

660
661
    pos_t get_anchor(const pos_t& origin, Cairo::Context& cr,
                     bool force_border=false)
Tiago Peixoto's avatar
Tiago Peixoto committed
662
    {
663
        int anchor_type =_attrs.template get<int32_t>(VERTEX_ANCHOR);
664
        if (anchor_type == 0 && !force_border)
665
666
            return _pos;

Tiago Peixoto's avatar
Tiago Peixoto committed
667
668
        double angle = atan2(_pos.second - origin.second,
                             _pos.first - origin.first);
669
670
        double rot = _attrs.template get<double>(VERTEX_ROTATION);

Tiago Peixoto's avatar
Tiago Peixoto committed
671
672
        if (angle < 0)
            angle += 2 * M_PI;
673

Tiago Peixoto's avatar
Tiago Peixoto committed
674
675
676
        double r = get_size(cr) / 2;
        double dr = r;

677
        double pw = _attrs.template get<double>(VERTEX_PENWIDTH);
678
        pw = get_user_dist(cr, pw);
Tiago Peixoto's avatar
Tiago Peixoto committed
679
680
681
682
        r += pw / 2.5;

        pos_t anchor;
        size_t nsides = 0;
683
        vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
Tiago Peixoto's avatar
Tiago Peixoto committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
        switch (shape)
        {
        case SHAPE_TRIANGLE:
        case SHAPE_SQUARE:
        case SHAPE_PENTAGON:
        case SHAPE_HEXAGON:
        case SHAPE_HEPTAGON:
        case SHAPE_OCTAGON:
        case SHAPE_DOUBLE_TRIANGLE:
        case SHAPE_DOUBLE_SQUARE:
        case SHAPE_DOUBLE_PENTAGON:
        case SHAPE_DOUBLE_HEXAGON:
        case SHAPE_DOUBLE_HEPTAGON:
        case SHAPE_DOUBLE_OCTAGON:
            nsides = shape - SHAPE_TRIANGLE + 3;
            if (nsides > 8)
                nsides -= 7;
701
            dr = get_polygon_anchor(nsides, r, angle - rot);
Tiago Peixoto's avatar
Tiago Peixoto committed
702
703
704
            break;
        case SHAPE_CIRCLE:
        case SHAPE_DOUBLE_CIRCLE:
705
        case SHAPE_PIE:
Tiago Peixoto's avatar
Tiago Peixoto committed
706
707
            dr = r;
            break;
708
709
        case SHAPE_NONE:
            break;
Tiago Peixoto's avatar
Tiago Peixoto committed
710
711
        default:
            throw ValueException("Invalid vertex shape: " +
712
                                 lexical_cast<string>(int(_attrs.template get<vertex_shape_t>(VERTEX_SHAPE))));
Tiago Peixoto's avatar
Tiago Peixoto committed
713
714
        }

715
716
        double aspect = _attrs.template get<double>(VERTEX_ASPECT);

Tiago Peixoto's avatar
Tiago Peixoto committed
717
        anchor = _pos;
718
        anchor.first -= dr * cos(angle) * aspect;
Tiago Peixoto's avatar
Tiago Peixoto committed
719
        anchor.second -= dr * sin(angle);
720

Tiago Peixoto's avatar
Tiago Peixoto committed
721
722
723
        return anchor;
    }

724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
    std::pair<pos_t, double> get_anchor_spline(const vector<double>& cts,
                                               Cairo::Context& cr,
                                               bool loop = false,
                                               bool src = false)
    {
        double len = get_spline_len(cts);
        double x, one;
        pos_t p0 = get_spline_point(cts, 0);
        pos_t p1 = get_spline_point(cts, len);
        if (dist(p0, _pos) < dist(p1, _pos) || (loop && src))
        {
            x = 1;
            one = 1;
        }
        else
        {
            x = 0;
            one = -1;
        }

        int anchor_type =_attrs.template get<int32_t>(VERTEX_ANCHOR);
        if (anchor_type == 0)
            return make_pair(_pos, x);

        if (loop)
            x = 0.5;

        double dl = 0.5;
        pos_t pos, anchor;
        pos = get_spline_point(cts, len * x);
        anchor = get_anchor(pos, cr);
        if (dist(anchor, _pos) == 0 || dist(pos, _pos) < dist(anchor, _pos))
            return make_pair(_pos, x * len);

        size_t i = 0;
        while (abs(dist(pos, anchor)) > 1e-6)
        {
761
            double nx = min(max(x - dl * one, 0.), 1.);
762
763
            pos = get_spline_point(cts, len * nx);
            anchor = get_anchor(pos, cr);
764
            size_t j = 0;
765
766
767
            while (dist(pos, _pos) < dist(anchor, _pos)) // x is inside
            {
                dl /= 2;
768
                nx = min(max(x - dl * one, 0.), 1.);
769
770
771
                pos = get_spline_point(cts, len * nx);
                anchor = get_anchor(pos, cr);
                j++;
772
                if (nx == 0. || nx == 1. || j > 100)
773
774
                    break;
            }
775

776
            x = nx;
777
            dl = min(dist(pos, anchor) / len, 0.5);
778
779
780
781
782
783
784
            i++;
            if (i > 1000)
                break;
        }
        return make_pair(pos, x * len);
    }

Tiago Peixoto's avatar
Tiago Peixoto committed
785
786
787
788
789
    pos_t get_pos()
    {
        return _pos;
    }

790
    void draw(Cairo::Context& cr, bool outline=false)
Tiago Peixoto's avatar
Tiago Peixoto committed
791
792
    {
        color_t color, fillcolor;
793
        double size, pw;
Tiago Peixoto's avatar
Tiago Peixoto committed
794
        size = get_size(cr);
795
796
797
798
799
800
801

        std::array<double, 4> clip;
        cr.get_clip_extents(clip[0], clip[1], clip[2], clip[3]);
        if ((_pos.first + 2 * size < clip[0] && _pos.second + 2 * size < clip[1]) ||
            (_pos.first - 2 * size > clip[2] && _pos.second - 2 * size > clip[3]))
            return;

802
803
        double aspect = _attrs.template get<double>(VERTEX_ASPECT);
        double rot = _attrs.template get<double>(VERTEX_ROTATION);
804

805
806
        if (!outline)
            cr.save();
Tiago Peixoto's avatar
Tiago Peixoto committed
807
        cr.translate(_pos.first, _pos.second);
808
        cr.rotate(rot);
Tiago Peixoto's avatar
Tiago Peixoto committed
809

810
        if (!outline && _attrs.template get<uint8_t>(VERTEX_HALO))
Tiago Peixoto's avatar
Tiago Peixoto committed
811
        {
812
            color_t c = _attrs.template get<color_t>(VERTEX_HALO_COLOR);
813
            double hs = _attrs.template get<double>(VERTEX_HALO_SIZE);
Tiago Peixoto's avatar
Tiago Peixoto committed
814
            cr.set_source_rgba(get<0>(c), get<1>(c), get<2>(c), get<3>(c));
815
816
817
818
819
            if (aspect != 1.)
            {
                cr.save();
                cr.scale(aspect, 1.0);
            }
820
            cr.arc(0, 0, size * hs / 2, 0, 2 * M_PI);
Tiago Peixoto's avatar
Tiago Peixoto committed
821
            cr.fill();
822
823
            if (aspect != 1.)
                cr.restore();
Tiago Peixoto's avatar
Tiago Peixoto committed
824
825
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
826
        boost::python::object osrc = _attrs.template get<boost::python::object>(VERTEX_SURFACE);
827
828
829
830
831
832

        pw =_attrs.template get<double>(VERTEX_PENWIDTH);
        pw = get_user_dist(cr, pw);
        cr.set_line_width(pw);

        if (!outline)
Tiago Peixoto's avatar
Tiago Peixoto committed
833
        {
834
835
836
837
            color = _attrs.template get<color_t>(VERTEX_COLOR);
            cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
                               get<3>(color));
        }
838

839
840
841
842
843
844
845
        size_t nsides = 0;
        vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
        switch (shape)
        {
        case SHAPE_CIRCLE:
        case SHAPE_DOUBLE_CIRCLE:
            if (aspect != 1.)
846
            {
847
848
                cr.save();
                cr.scale(aspect, 1.0);
849
            }
850
851
852
853
854
            cr.arc(0, 0, size / 2., 0, 2 * M_PI);
            cr.close_path();
            if (aspect != 1.)
                cr.restore();
            if (shape == SHAPE_DOUBLE_CIRCLE && !outline)
Tiago Peixoto's avatar
Tiago Peixoto committed
855
            {
856
                cr.stroke();
857
858
859
860
861
                if (aspect != 1.)
                {
                    cr.save();
                    cr.scale(aspect, 1.0);
                }
862
863
864
                cr.arc(0, 0, min(size / 2 - 2 * pw,
                                 size * 0.8 / 2),
                       0, 2 * M_PI);
865
866
                if (aspect != 1.)
                    cr.restore();
867
868
869
870
871
            }
            break;
        case SHAPE_PIE:
            {
                if (!outline)
872
                {
873
874
875
                    vector<double> f = _attrs.template get<vector<double> >(VERTEX_PIE_FRACTIONS);
                    vector<color_t> fcolors = _attrs.template get<vector<color_t> >(VERTEX_PIE_COLORS);
                    draw_pie(size / 2 + pw / 2, f, fcolors, cr);
876
                }
877
                else
878
                {
879
880
                    cr.arc(0, 0, size / 2., 0, 2 * M_PI);
                    cr.close_path();
881
                }
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
            }
            break;
        case SHAPE_TRIANGLE:
        case SHAPE_SQUARE:
        case SHAPE_PENTAGON:
        case SHAPE_HEXAGON:
        case SHAPE_HEPTAGON:
        case SHAPE_OCTAGON:
        case SHAPE_DOUBLE_TRIANGLE:
        case SHAPE_DOUBLE_SQUARE:
        case SHAPE_DOUBLE_PENTAGON:
        case SHAPE_DOUBLE_HEXAGON:
        case SHAPE_DOUBLE_HEPTAGON:
        case SHAPE_DOUBLE_OCTAGON:
            nsides = shape - SHAPE_TRIANGLE + 3;
            if (nsides > 8)
                nsides -= 7;
            if (aspect != 1.)
            {
                cr.save();
                cr.scale(aspect, 1.0);
            }
            draw_polygon(nsides, size / 2, cr);
            if (aspect != 1.)
                cr.restore();
            if (shape >= SHAPE_DOUBLE_TRIANGLE && !outline)
            {
                cr.stroke();
910
911
912
913
914
                if (aspect != 1.)
                {
                    cr.save();
                    cr.scale(aspect, 1.0);
                }
915
916
                draw_polygon(nsides, min(size / 2 - 2 * pw,
                                         size * 0.8 / 2), cr);
917
918
                if (aspect != 1.)
                    cr.restore();
Tiago Peixoto's avatar
Tiago Peixoto committed
919
            }
920
921
922
923
924
925
926
            break;
        case SHAPE_NONE:
            break;
        default:
            throw ValueException("Invalid vertex shape: " +
                                 lexical_cast<string>(int(_attrs.template get<vertex_shape_t>(VERTEX_SHAPE))));
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
927

928
929
930
931
932
933
        if (!outline && shape != SHAPE_PIE)
        {
            fillcolor = _attrs.template get<color_t>(VERTEX_FILL_COLOR);
            cr.set_source_rgba(get<0>(fillcolor), get<1>(fillcolor),
                               get<2>(fillcolor), get<3>(fillcolor));
            cr.fill_preserve();
Tiago Peixoto's avatar
Tiago Peixoto committed
934

935
936
937
            cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
                               get<3>(color));
            cr.stroke();
938
        }
939
940

        if (osrc != boost::python::object() && !outline)
941
        {
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
            double swidth, sheight;
            PycairoSurface* src = (PycairoSurface*) osrc.ptr();
            Cairo::RefPtr<Cairo::Surface> surface(new Cairo::Surface(src->surface));
            get_surface_size(surface, swidth, sheight);
            Cairo::RefPtr<Cairo::SurfacePattern> pat(Cairo::SurfacePattern::create(surface));
            //pat->set_extend(Cairo::EXTEND_REPEAT);

            double r = size / sqrt(2);
            double scale = r / max(swidth / aspect, sheight);

            Cairo::Matrix m = Cairo::identity_matrix();
            m.translate(swidth / 2, sheight / 2);
            m.scale(1. / scale, 1. / scale);
            pat->set_matrix(m);

            cr.set_source(pat);
            cr.rectangle(-r * aspect / 2, -r / 2, r * aspect, r);
            cr.fill();
960
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
961

962
        if (!outline)
Tiago Peixoto's avatar
Tiago Peixoto committed
963
        {
964
            string text = _attrs.template get<string>(VERTEX_TEXT);
965
            if (!text.empty())
966
967
            {
                cr.save();
968

969
970
971
                double text_pos = 0;
                double text_rotation = 0;
                vector<double> text_offset;
972

973
974
975
976
977
978
979
980
                cr.select_font_face(_attrs.template get<string>(VERTEX_FONT_FAMILY),
                                    static_cast<Cairo::FontSlant>(_attrs.template get<int32_t>(VERTEX_FONT_SLANT)),
                                    static_cast<Cairo::FontWeight>(_attrs.template get<int32_t>(VERTEX_FONT_WEIGHT)));
                cr.set_font_size(get_user_dist(cr, _attrs.template get<double>(VERTEX_FONT_SIZE)));
                text_pos = _attrs.template get<double>(VERTEX_TEXT_POSITION);
                text_rotation = _attrs.template get<double>(VERTEX_TEXT_ROTATION);
                text_offset = _attrs.template get<vector<double> >(VERTEX_TEXT_OFFSET);
                text_offset.resize(2, 0.0);
981

982
                cr.rotate(text_rotation);
983

984
985
986
987
                Cairo::TextExtents extents;
                cr.get_text_extents(text, extents);
                Cairo::FontExtents fextents;
                cr.get_font_extents(fextents);
Tiago Peixoto's avatar
Tiago Peixoto committed
988

989
990
991
992
993
994
995
996
997
998
                if (text_pos < 0)
                {
                    cr.translate(-extents.width / 2 - extents.x_bearing + text_offset[0],
                                 extents.height / 2 + text_offset[1]);
                }
                else
                {
                    pos_t origin;
                    origin.first = _pos.first + size * cos(text_pos);
                    origin.second = _pos.second + size * sin(text_pos);
999

1000
1001
1002
1003
1004
                    pos_t anchor = get_anchor(origin, cr, true);
                    double angle = atan2(_pos.second - anchor.second,
                                         _pos.first - anchor.first) + M_PI;
                    anchor.first = size * 1.2 * cos(angle);
                    anchor.second = size * 1.2 * sin(angle);
1005

1006
1007
                    anchor.first += text_offset[0];
                    anchor.second += text_offset[1] + extents.height / 2;
1008

1009
1010
                    if (anchor.first < 0)
                        anchor.first -= extents.width;
1011

1012
1013
1014
1015
1016
1017
1018
1019
                    cr.translate(anchor.first, anchor.second);
                }
                color = _attrs.template get<color_t>(VERTEX_TEXT_COLOR);
                cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
                                   get<3>(color));
                cr.show_text(text);
                cr.begin_new_path();
                cr.restore();
Tiago Peixoto's avatar
Tiago Peixoto committed
1020
1021
            }

1022
            cr.restore();
1023
        }
1024
        else
1025
        {
1026
            cr.rotate(-rot);
1027
            cr.translate(-_pos.first, -_pos.second);
1028
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
1029
1030
    }

1031
1032
1033
    template <class, class>
    friend class EdgeShape;

Tiago Peixoto's avatar
Tiago Peixoto committed
1034
1035
private:
    pos_t _pos;
1036
protected:
Tiago Peixoto's avatar
Tiago Peixoto committed
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
    AttrDict<Descriptor> _attrs;
};

template <class Descriptor, class VertexShape>
class EdgeShape
{
public:
    EdgeShape(VertexShape& s, VertexShape& t, AttrDict<Descriptor> attrs)
        : _s(s), _t(t), _attrs(attrs) {}

1047
    void draw(Cairo::Context& cr, double res = 0.)
Tiago Peixoto's avatar
Tiago Peixoto committed
1048
1049
1050
1051
    {
        pos_t pos_begin, pos_end;

        vector<double> controls =
1052
            _attrs.template get<vector<double> >(EDGE_CONTROL_POINTS);
Tiago Peixoto's avatar
Tiago Peixoto committed
1053
1054
        vector<double> gradient =
            _attrs.template get<vector<double> >(EDGE_GRADIENT);
1055
        if (gradient.size() == 1)
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
        {
            auto e_color = _attrs.template get<color_t>(EDGE_COLOR);
            auto s_color = _s._attrs.template get<color_t>(VERTEX_FILL_COLOR);
            auto t_color = _t._attrs.template get<color_t>(VERTEX_FILL_COLOR);
            gradient.resize(10);
            gradient[0] = 0;
            gradient[1] = get<0>(s_color);
            gradient[2] = get<1>(s_color);
            gradient[3] = get<2>(s_color);
            gradient[4] = get<3>(e_color);
            gradient[5] = 1;
            gradient[6] = get<0>(t_color);
            gradient[7] = get<1>(t_color);
            gradient[8] = get<2>(t_color);
            gradient[9] = get<3>(e_color);
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
1072
        bool has_gradient = gradient.size() >= 2;
Tiago Peixoto's avatar
Tiago Peixoto committed
1073

1074
        edge_marker_t start_marker = _attrs.template get<edge_marker_t>(EDGE_START_MARKER);
1075
        edge_marker_t mid_marker = _attrs.template get<edge_marker_t>(EDGE_MID_MARKER);
1076
        edge_marker_t end_marker = _attrs.template get<edge_marker_t>(EDGE_END_MARKER);
1077
1078
        double marker_size = _attrs.template get<double>(EDGE_MARKER_SIZE);
        marker_size = get_user_dist(cr, marker_size);
1079

1080
1081
        pos_begin = _s.get_pos();
        pos_end = _t.get_pos();
1082

1083
1084
        cr.save();

1085
        if (controls.size() >= 8)
Tiago Peixoto's avatar
Tiago Peixoto committed
1086
1087
1088
        {
            double angle = 0;
            double len = 0;
1089
            if (_t.get_pos() != _s.get_pos())
Tiago Peixoto's avatar
Tiago Peixoto committed
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
            {
                angle = atan2(pos_end.second - pos_begin.second,
                              pos_end.first - pos_begin.first);
                len = sqrt(pow(pos_end.first - pos_begin.first, 2) +
                           pow(pos_end.second - pos_begin.second, 2));

                cr.save();
                cr.translate(pos_begin.first, pos_begin.second);
                cr.rotate(angle);
                cr.scale(len, 1.);
            }
            else
            {
                pos_begin = _s.get_pos();
1104
1105
1106
1107
1108
                if (start_marker == MARKER_SHAPE_NONE &&
                    end_marker == MARKER_SHAPE_NONE)
                    len = M_PI * _s.get_size(cr);
                else
                    len = max(M_PI * _s.get_size(cr), 6 * marker_size);
Tiago Peixoto's avatar
Tiago Peixoto committed
1109
1110
1111
1112
1113
                cr.save();
                cr.translate(pos_begin.first, pos_begin.second);
                cr.scale(len / sqrt(2), len / sqrt(2));
            }

1114
1115
1116
            for (size_t i = 0; i < controls.size() / 2; ++i)
                cr.user_to_device(controls[2 * i], controls[2 * i + 1]);
            cr.restore();
1117

1118
1119
            for (size_t i = 0; i < controls.size() / 2; ++i)
                cr.device_to_user(controls[2 * i], controls[2 * i + 1]);
1120
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
1121
1122


1123
        color_t color = _attrs.template get<color_t>(EDGE_COLOR);
1124
        double pw = _attrs.template get<double>(EDGE_PENWIDTH);
1125
        pw = get_user_dist(cr, pw);
Tiago Peixoto's avatar
Tiago Peixoto committed
1126

1127
1128
1129
        pos_t pos_begin_marker = pos_begin;
        pos_t pos_end_marker = pos_end;
        double pos_begin_d = 0, pos_end_d = 0;
1130

1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
        if (controls.size() >= 8)
        {
            if (start_marker != MARKER_SHAPE_NONE)
                tie(pos_begin_marker, pos_begin_d) =
                    _s.get_anchor_spline(controls, cr, pos_begin == pos_end, true);
            if (end_marker != MARKER_SHAPE_NONE)
                tie(pos_end_marker, pos_end_d) =
                    _t.get_anchor_spline(controls, cr, pos_begin == pos_end, false);
        }
        else
        {
            if (start_marker != MARKER_SHAPE_NONE)
                pos_begin_marker = _s.get_anchor(pos_end, cr);
            if (end_marker != MARKER_SHAPE_NONE)
                pos_end_marker = _t.get_anchor(pos_begin, cr);
        }
1147

1148

1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
        bool sloppy = _attrs.template get<uint8_t>(EDGE_SLOPPY);
        if (_s.get_size(cr) < get_user_dist(cr, res) &&
            _t.get_size(cr) < get_user_dist(cr, res))
            sloppy = true;

        bool seamless = _attrs.template get<uint8_t>(EDGE_SEAMLESS);
        if (marker_size < get_user_dist(cr, res) ||
            (start_marker == MARKER_SHAPE_NONE &&
             mid_marker == MARKER_SHAPE_NONE &&
             end_marker == MARKER_SHAPE_NONE))
            seamless = false;

        if (seamless)
1162
1163
1164
        {
            // set the clip region to the correct size for better push/pop_group
            // performance
1165
            double sx1, sy1, sx2, sy2;
1166
1167
            draw_edge_markers(pos_begin_marker, pos_begin_d, pos_end_marker,
                              pos_end_d, controls, marker_size, cr);
1168
            cr.set_line_width(pw);
1169
            draw_edge_line(pos_begin, pos_end, controls, cr);
1170
            cr.get_stroke_extents(sx1, sy1, sx2, sy2);
1171
1172
1173
1174
            sx1 -= pw;
            sx2 += pw;
            sy1 -= pw;
            sy2 += pw;
1175
            cr.begin_new_path();
1176
1177
            cr.rectangle(sx1, sy1, sx2 - sx1, sy2 - sy1);
            _s.draw(cr, true);
1178
1179
            if (pos_begin != pos_end)
                _t.draw(cr, true);
1180
            cr.set_fill_rule(Cairo::FILL_RULE_EVEN_ODD);
1181
1182
1183
1184
            cr.clip();

            // seamlessly blend in separate surface
            cr.push_group();
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
            cr.set_operator(Cairo::OPERATOR_SOURCE);
            draw_edge_markers(pos_begin_marker, pos_begin_d, pos_end_marker,
                              pos_end_d, controls, marker_size, cr);
            if (has_gradient)
            {
                auto gd = Cairo::LinearGradient::create(pos_begin.first,
                                                        pos_begin.second,
                                                        pos_end.first,
                                                        pos_end.second);

                for (size_t i = 0; i < gradient.size() / 5; ++i)
                {
                    size_t pos = i * 5;
                    gd->add_color_stop_rgba(gradient[pos],
                                            gradient[pos + 1],
                                            gradient[pos + 2],
                                            gradient[pos + 3],
                                            gradient[pos + 4]);
                }
                cr.set_source(gd);
            }
            else
            {
                cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color), 1);
            }
1210
            cr.fill();
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
            cr.rectangle(sx1, sy1, sx2 - sx1, sy2 - sy1);
            if (start_marker != MARKER_SHAPE_NONE && start_marker != MARKER_SHAPE_BAR)
            {
                cr.arc(pos_begin_marker.first, pos_begin_marker.second,
                       marker_size / 2, 0, 2 * M_PI);
            }
            if (end_marker != MARKER_SHAPE_NONE && end_marker != MARKER_SHAPE_BAR)
            {
                cr.arc(pos_end_marker.first, pos_end_marker.second,
                       marker_size / 2, 0, 2 * M_PI);
            }
            cr.clip();
            draw_edge_line(pos_begin, pos_end, controls, cr);
1224
            cr.set_line_width(pw);
1225
1226
1227
1228
            cr.stroke();
            vector<double> empty;
            cr.set_dash(empty, 0);
            cr.pop_group_to_source();
1229
            cr.set_operator(Cairo::OPERATOR_OVER);
1230
            cr.reset_clip();
1231
1232
1233
1234
            if (!has_gradient)
                cr.paint_with_alpha(get<3>(color));
            else
                cr.paint();
1235
1236
1237
        }
        else
        {
1238
1239
1240
1241
1242
1243
            if (!sloppy)
            {
                // compose clip region
                double sx1, sy1, sx2, sy2;
                draw_edge_line(pos_begin, pos_end, controls, cr);
                cr.get_stroke_extents(sx1, sy1, sx2, sy2);
1244
1245
1246
1247
                sx1 -= pw;
                sx2 += pw;
                sy1 -= pw;
                sy2 += pw;
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
                cr.begin_new_path();
                cr.rectangle(sx1, sy1, sx2 - sx1, sy2 - sy1);
                draw_edge_markers(pos_begin_marker, pos_begin_d, pos_end_marker,
                                  pos_end_d, controls, marker_size, cr);
                cr.set_fill_rule(Cairo::FILL_RULE_EVEN_ODD);
                cr.clip();

                cr.rectangle(sx1, sy1, sx2 - sx1, sy2 - sy1);
                _s.draw(cr, true);
                cr.clip();

                cr.rectangle(sx1, sy1, sx2 - sx1, sy2 - sy1);
                _t.draw(cr, true);
                cr.clip();

                if (start_marker != MARKER_SHAPE_NONE && start_marker != MARKER_SHAPE_BAR)
                {
                    cr.rectangle(sx1, sy1, sx2 - sx1, sy2 - sy1);
                    cr.arc(pos_begin_marker.first, pos_begin_marker.second,
                           marker_size / 2, 0, 2 * M_PI);
                    cr.clip();
                }

                if (end_marker != MARKER_SHAPE_NONE && end_marker != MARKER_SHAPE_BAR)
                {
                    cr.rectangle(sx1, sy1, sx2 - sx1, sy2 - sy1);
                    cr.arc(pos_end_marker.first, pos_end_marker.second,
                           marker_size / 2, 0, 2 * M_PI);
                    cr.clip();
                }
            }

Tiago Peixoto's avatar
Tiago Peixoto committed
1280
1281
            if (!has_gradient)
            {
1282
1283
                cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
                                   get<3>(color));
Tiago Peixoto's avatar
Tiago Peixoto committed
1284
1285
1286
            }
            else
            {
1287
1288
1289
1290
                auto gd = Cairo::LinearGradient::create(pos_begin.first,
                                                        pos_begin.second,
                                                        pos_end.first,
                                                        pos_end.second);
Tiago Peixoto's avatar
Tiago Peixoto committed
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
                for (size_t i = 0; i < gradient.size() / 5; ++i)
                {
                    size_t pos = i * 5;
                    gd->add_color_stop_rgba(gradient[pos],
                                            gradient[pos + 1],
                                            gradient[pos + 2],