graph_cairo_draw.cc 65.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-2015 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
31
//
// 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>

Tiago Peixoto's avatar
Tiago Peixoto committed
32
#include <unordered_map>
Tiago Peixoto's avatar
Tiago Peixoto committed
33
34
35
36

#include <cairommconfig.h>
#include <cairomm/context.h>
#include <cairomm/surface.h>
37
#include <pycairo/pycairo.h>
Tiago Peixoto's avatar
Tiago Peixoto committed
38
39
40
41
42
43
44
45
46
47
48
49

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

using namespace std;
using namespace boost;
using namespace graph_tool;

enum vertex_attr_t {
    VERTEX_SHAPE = 100,
    VERTEX_COLOR,
    VERTEX_FILL_COLOR,
    VERTEX_SIZE,
50
    VERTEX_ASPECT,
51
    VERTEX_ANCHOR,
Tiago Peixoto's avatar
Tiago Peixoto committed
52
53
54
    VERTEX_PENWIDTH,
    VERTEX_HALO,
    VERTEX_HALO_COLOR,
55
    VERTEX_HALO_SIZE,
Tiago Peixoto's avatar
Tiago Peixoto committed
56
57
58
    VERTEX_TEXT,
    VERTEX_TEXT_COLOR,
    VERTEX_TEXT_POSITION,
59
60
    VERTEX_TEXT_ROTATION,
    VERTEX_TEXT_OFFSET,
Tiago Peixoto's avatar
Tiago Peixoto committed
61
62
63
    VERTEX_FONT_FAMILY,
    VERTEX_FONT_SLANT,
    VERTEX_FONT_WEIGHT,
64
    VERTEX_FONT_SIZE,
65
66
67
    VERTEX_SURFACE,
    VERTEX_PIE_FRACTIONS,
    VERTEX_PIE_COLORS
Tiago Peixoto's avatar
Tiago Peixoto committed
68
69
70
71
72
73
74
75
76
};

enum edge_attr_t {
    EDGE_COLOR = 200,
    EDGE_PENWIDTH,
    EDGE_START_MARKER,
    EDGE_MID_MARKER,
    EDGE_END_MARKER,
    EDGE_MARKER_SIZE,
77
    EDGE_MID_MARKER_POSITION,
78
    EDGE_CONTROL_POINTS,
79
    EDGE_DASH_STYLE,
Tiago Peixoto's avatar
Tiago Peixoto committed
80
    EDGE_GRADIENT,
81
82
83
84
85
86
87
88
    EDGE_TEXT,
    EDGE_TEXT_COLOR,
    EDGE_TEXT_DISTANCE,
    EDGE_TEXT_PARALLEL,
    EDGE_FONT_FAMILY,
    EDGE_FONT_SLANT,
    EDGE_FONT_WEIGHT,
    EDGE_FONT_SIZE,
89
    EDGE_SLOPPY
Tiago Peixoto's avatar
Tiago Peixoto committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
};

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,
106
107
    SHAPE_DOUBLE_OCTAGON,
    SHAPE_PIE
Tiago Peixoto's avatar
Tiago Peixoto committed
108
109
110
111
112
113
114
115
116
117
118
119
};

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
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
typedef std::tuple<double, double, double, double> color_t;
typedef std::unordered_map<int, boost::any> attrs_t;

typedef boost::mpl::map41<
    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>,
    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>,
    boost::mpl::pair<boost::mpl::int_<EDGE_SLOPPY>, uint8_t> >
Tiago Peixoto's avatar
Tiago Peixoto committed
165
166
        attr_types;

167
namespace std
Tiago Peixoto's avatar
Tiago Peixoto committed
168
169
170
{
ostream& operator<<(ostream& out, const color_t& c)
{
Tiago Peixoto's avatar
Tiago Peixoto committed
171
    out << std::get<0>(c) << " " << std::get<1>(c) << " " << std::get<2>(c) << " " << std::get<3>(c);
Tiago Peixoto's avatar
Tiago Peixoto committed
172
173
174
    return out;
}

175
istream& operator>>(istream& in, color_t& c)
Tiago Peixoto's avatar
Tiago Peixoto committed
176
{
Tiago Peixoto's avatar
Tiago Peixoto committed
177
    in >> std::get<0>(c) >> std::get<1>(c) >> std::get<2>(c) >> std::get<3>(c);
Tiago Peixoto's avatar
Tiago Peixoto committed
178
179
180
181
    return in;
}
}

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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;
}

198
199
200
201
202
203
204
205
206
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
207
208
209
210
211
212
213
214
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
215
        return do_convert(v, std::is_convertible<Type2,Type1>());
Tiago Peixoto's avatar
Tiago Peixoto committed
216
217
    }

Tiago Peixoto's avatar
Tiago Peixoto committed
218
    Type1 do_convert(const Type2& v, std::true_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
219
220
221
222
    {
        return Type1(v);
    }

Tiago Peixoto's avatar
Tiago Peixoto committed
223
    Type1 do_convert(const Type2& v, std::false_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
224
    {
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
        try
        {
            return specific_convert<Type1,Type2>()(v);
        }
        catch (bad_lexical_cast&)
        {
            string name1 = python::detail::gcc_demangle(typeid(Type1).name());
            string name2 = python::detail::gcc_demangle(typeid(Type2).name());
            string val_name;
            try
            {
                val_name = lexical_cast<string>(v);
            }
            catch (bad_lexical_cast)
            {
                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
245
246
247
248
249
250
251
    }

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

Tiago Peixoto's avatar
Tiago Peixoto committed
255
        T1 dispatch(const T2& v, std::true_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
256
257
258
259
        {
            return T1(v);
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
260
        T1 dispatch(const T2& v, std::false_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
        {
            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
283
            if (std::is_same<T1, uint8_t>::value)
Tiago Peixoto's avatar
Tiago Peixoto committed
284
285
286
287
288
289
                return convert<T1,int>()(lexical_cast<int>(v));
            else
                return lexical_cast<T1>(v);
        }
    };

Tiago Peixoto's avatar
Tiago Peixoto committed
290
    // boost::python::object
Tiago Peixoto's avatar
Tiago Peixoto committed
291
    template <class T1>
Tiago Peixoto's avatar
Tiago Peixoto committed
292
    struct specific_convert<T1,boost::python::object>
Tiago Peixoto's avatar
Tiago Peixoto committed
293
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
294
        T1 operator()(const boost::python::object& v) const
Tiago Peixoto's avatar
Tiago Peixoto committed
295
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
296
            boost::python::extract<T1> x(v);
Tiago Peixoto's avatar
Tiago Peixoto committed
297
298
299
300
301
302
303
304
305
            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
306
307
308
                            typename std::enable_if
                            <boost::mpl::not_<
                                 std::is_same<T2,boost::python::object> >::type::value>::type>
Tiago Peixoto's avatar
Tiago Peixoto committed
309
310
311
312
    {
        string operator()(const T2& v) const
        {
            //uint8_t is not char, it is bool!
Tiago Peixoto's avatar
Tiago Peixoto committed
313
            if (std::is_same<T2, uint8_t>::value)
Tiago Peixoto's avatar
Tiago Peixoto committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
                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;
        }
    };

334
    // color_t
Tiago Peixoto's avatar
Tiago Peixoto committed
335
336
337
338
339
340
341
342
    template <class T2>
    struct specific_convert<color_t, vector<T2> >
    {
        specific_convert<double, T2> c;
        color_t operator()(const vector<T2>& cv) const
        {
            if (cv.size() < 4)
                throw bad_lexical_cast();
Tiago Peixoto's avatar
Tiago Peixoto committed
343
            return std::make_tuple(c(cv[0]), c(cv[1]), c(cv[2]), c(cv[3]));
Tiago Peixoto's avatar
Tiago Peixoto committed
344
345
346
        }
    };

347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
    // 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
367
368
369
370

    // vertex_shape_t
    template <class T2>
    struct specific_convert<vertex_shape_t, T2,
Tiago Peixoto's avatar
Tiago Peixoto committed
371
372
373
374
375
                            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
376
377
378
379
380
381
382
383
384
385
386
    {
        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
387
388
389
390
391
                            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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    {
        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)
    {
        typeof(_attrs.begin()) iter = _attrs.find(k);
        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) +
                                 ", wanted: " +
Tiago Peixoto's avatar
Tiago Peixoto committed
427
                                 boost::python::detail::gcc_demangle(typeid(Value).name()) +
Tiago Peixoto's avatar
Tiago Peixoto committed
428
                                 ", got: " +
Tiago Peixoto's avatar
Tiago Peixoto committed
429
                                 boost::python::detail::gcc_demangle(_defaults[k].type().name()));
Tiago Peixoto's avatar
Tiago Peixoto committed
430
431
432
433
434
435
436
437
438
439
440
441
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
        }
    }

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);
}

468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498

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
499
500
501
502
503
504
505
506
507
508
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);
}

509
double get_user_dist(Cairo::Context& cr, double norm = 1.)
Tiago Peixoto's avatar
Tiago Peixoto committed
510
{
511
    double x = norm / sqrt(2.), y = norm / sqrt(2.);
Tiago Peixoto's avatar
Tiago Peixoto committed
512
513
514
515
    cr.device_to_user_distance(x, y);
    return sqrt(x * x + y * y);
}

516
517
518
519
520
521
522
523
524
525
526
527
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;
}

Tiago Peixoto's avatar
Tiago Peixoto committed
528
529
530
531
532
533
534
535
536
537
538
539
540


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

    {}

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

544
        string text = _attrs.template get<string>(VERTEX_TEXT);
Tiago Peixoto's avatar
Tiago Peixoto committed
545
546
        if (text != "")
        {
547
            double text_pos = _attrs.template get<double>(VERTEX_TEXT_POSITION);
548
            if (text_pos == -1)
Tiago Peixoto's avatar
Tiago Peixoto committed
549
            {
550
551
552
                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)));
553
                cr.set_font_size(get_user_dist(cr, _attrs.template get<double>(VERTEX_FONT_SIZE)));
Tiago Peixoto's avatar
Tiago Peixoto committed
554
555
556
                Cairo::TextExtents extents;
                cr.get_text_extents(text, extents);
                double s = max(extents.width, extents.height) * 1.4;
557
                vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
558
                if (shape >= SHAPE_DOUBLE_CIRCLE)
Tiago Peixoto's avatar
Tiago Peixoto committed
559
560
                {
                    s /= 0.7;
561
                    double pw = _attrs.template get<double>(VERTEX_PENWIDTH);
562
                    pw = get_user_dist(cr, pw);
Tiago Peixoto's avatar
Tiago Peixoto committed
563
564
565
566
567
568
569
570
                    s += pw;
                }
                size = max(size, s);
            }
        }
        return size;
    }

571
572
    pos_t get_anchor(const pos_t& origin, Cairo::Context& cr,
                     bool force_border=false)
Tiago Peixoto's avatar
Tiago Peixoto committed
573
    {
574
        int anchor_type =_attrs.template get<int32_t>(VERTEX_ANCHOR);
575
        if (anchor_type == 0 && !force_border)
576
577
            return _pos;

Tiago Peixoto's avatar
Tiago Peixoto committed
578
579
580
581
582
583
584
        double angle = atan2(_pos.second - origin.second,
                             _pos.first - origin.first);
        if (angle < 0)
            angle += 2 * M_PI;
        double r = get_size(cr) / 2;
        double dr = r;

585
        double pw = _attrs.template get<double>(VERTEX_PENWIDTH);
586
        pw = get_user_dist(cr, pw);
Tiago Peixoto's avatar
Tiago Peixoto committed
587
588
589
590
        r += pw / 2.5;

        pos_t anchor;
        size_t nsides = 0;
591
        vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
Tiago Peixoto's avatar
Tiago Peixoto committed
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
        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;
            dr = get_polygon_anchor(nsides, r, angle);
            break;
        case SHAPE_CIRCLE:
        case SHAPE_DOUBLE_CIRCLE:
613
        case SHAPE_PIE:
Tiago Peixoto's avatar
Tiago Peixoto committed
614
615
616
617
            dr = r;
            break;
        default:
            throw ValueException("Invalid vertex shape: " +
618
                                 lexical_cast<string>(int(_attrs.template get<vertex_shape_t>(VERTEX_SHAPE))));
Tiago Peixoto's avatar
Tiago Peixoto committed
619
620
        }

621
622
        double aspect = _attrs.template get<double>(VERTEX_ASPECT);

Tiago Peixoto's avatar
Tiago Peixoto committed
623
        anchor = _pos;
624
        anchor.first -= dr * cos(angle) * aspect;
Tiago Peixoto's avatar
Tiago Peixoto committed
625
        anchor.second -= dr * sin(angle);
626

Tiago Peixoto's avatar
Tiago Peixoto committed
627
628
629
630
631
632
633
634
        return anchor;
    }

    pos_t get_pos()
    {
        return _pos;
    }

635
    void draw(Cairo::Context& cr, bool outline=false)
Tiago Peixoto's avatar
Tiago Peixoto committed
636
637
    {
        color_t color, fillcolor;
638
        double size, pw, aspect;
Tiago Peixoto's avatar
Tiago Peixoto committed
639
        size = get_size(cr);
640
641
        aspect = _attrs.template get<double>(VERTEX_ASPECT);

642
643
        if (!outline)
            cr.save();
Tiago Peixoto's avatar
Tiago Peixoto committed
644
645
        cr.translate(_pos.first, _pos.second);

646
        if (_attrs.template get<uint8_t>(VERTEX_HALO) && !outline)
Tiago Peixoto's avatar
Tiago Peixoto committed
647
        {
648
            color_t c = _attrs.template get<color_t>(VERTEX_HALO_COLOR);
649
            double hs = _attrs.template get<double>(VERTEX_HALO_SIZE);
Tiago Peixoto's avatar
Tiago Peixoto committed
650
            cr.set_source_rgba(get<0>(c), get<1>(c), get<2>(c), get<3>(c));
651
652
            cr.save();
            cr.scale(aspect, 1.0);
653
            cr.arc(0, 0, size * hs / 2, 0, 2 * M_PI);
Tiago Peixoto's avatar
Tiago Peixoto committed
654
            cr.fill();
655
            cr.restore();
Tiago Peixoto's avatar
Tiago Peixoto committed
656
657
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
658
659
        boost::python::object osrc = _attrs.template get<boost::python::object>(VERTEX_SURFACE);
        if (osrc == boost::python::object())
Tiago Peixoto's avatar
Tiago Peixoto committed
660
        {
661
            pw =_attrs.template get<double>(VERTEX_PENWIDTH);
662
            pw = get_user_dist(cr, pw);
663
664
665
666
667
668
669
670
671
            cr.set_line_width(pw);

            color = _attrs.template get<color_t>(VERTEX_COLOR);
            cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
                               get<3>(color));

            size_t nsides = 0;
            vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
            switch (shape)
Tiago Peixoto's avatar
Tiago Peixoto committed
672
            {
673
674
            case SHAPE_CIRCLE:
            case SHAPE_DOUBLE_CIRCLE:
675
676
                cr.save();
                cr.scale(aspect, 1.0);
677
678
                cr.arc(0, 0, size / 2., 0, 2 * M_PI);
                cr.close_path();
679
                cr.restore();
680
                if (shape == SHAPE_DOUBLE_CIRCLE && !outline)
681
                {
682
                    cr.stroke();
683
                    cr.save();
684
685
686
687
                    cr.scale(aspect, 1.0);
                    cr.arc(0, 0, min(size / 2 - 2 * pw,
                                     size * 0.8 / 2),
                           0, 2 * M_PI);
688
689
                    cr.restore();
                }
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
                break;
            case SHAPE_PIE:
                {
                    if (!outline)
                    {
                        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);
                    }
                    else
                    {
                        cr.save();
                        cr.arc(0, 0, size / 2., 0, 2 * M_PI);
                        cr.close_path();
                        cr.restore();
                    }
                }
                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;
723
724
                cr.save();
                cr.scale(aspect, 1.0);
725
                draw_polygon(nsides, size / 2, cr);
726
                cr.restore();
727
728
729
730
731
732
733
734
735
736
737
738
739
                if (shape >= SHAPE_DOUBLE_TRIANGLE && !outline)
                {
                    cr.stroke();
                    cr.save();
                    cr.scale(aspect, 1.0);
                    draw_polygon(nsides, min(size / 2 - 2 * pw,
                                             size * 0.8 / 2), cr);
                    cr.restore();
                }
                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
740
741
            }

742
            if (!outline && shape != SHAPE_PIE)
743
744
745
746
747
            {
                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
748

749
                cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
750
                                       get<3>(color));
751
752
                cr.stroke();
            }
753
        }
754
755
        else
        {
756
            if (!outline)
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
            {
                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);
775
                cr.fill();
776
            }
777
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
778

779
        string text = _attrs.template get<string>(VERTEX_TEXT);
780
        if (text != "" && !outline)
Tiago Peixoto's avatar
Tiago Peixoto committed
781
782
        {
            cr.save();
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798

            double text_pos = 0;
            double text_rotation = 0;
            vector<double> text_offset;

            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);

            cr.rotate(text_rotation);

Tiago Peixoto's avatar
Tiago Peixoto committed
799
800
            Cairo::TextExtents extents;
            cr.get_text_extents(text, extents);
801
802
            Cairo::FontExtents fextents;
            cr.get_font_extents(fextents);
Tiago Peixoto's avatar
Tiago Peixoto committed
803
804
805

            if (text_pos < 0)
            {
806
807
                cr.translate(-extents.width / 2 - extents.x_bearing + text_offset[0],
                             extents.height / 2 + text_offset[1]);
Tiago Peixoto's avatar
Tiago Peixoto committed
808
809
810
811
812
813
            }
            else
            {
                pos_t origin;
                origin.first = _pos.first + size * cos(text_pos);
                origin.second = _pos.second + size * sin(text_pos);
814
815

                pos_t anchor = get_anchor(origin, cr, true);
Tiago Peixoto's avatar
Tiago Peixoto committed
816
                double angle = atan2(_pos.second - anchor.second,
817
                                     _pos.first - anchor.first) + M_PI;
Tiago Peixoto's avatar
Tiago Peixoto committed
818
819
                anchor.first = size * 1.2 * cos(angle);
                anchor.second = size * 1.2 * sin(angle);
820
821
822
823

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

Tiago Peixoto's avatar
Tiago Peixoto committed
824
825
                if (anchor.first < 0)
                    anchor.first -= extents.width;
826

Tiago Peixoto's avatar
Tiago Peixoto committed
827
828
                cr.translate(anchor.first, anchor.second);
            }
829
            color = _attrs.template get<color_t>(VERTEX_TEXT_COLOR);
Tiago Peixoto's avatar
Tiago Peixoto committed
830
831
832
833
834
835
836
            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();
        }

837
838
839
840
841
        if (!outline)
            cr.restore();
        else
            cr.translate(-_pos.first, -_pos.second);

Tiago Peixoto's avatar
Tiago Peixoto committed
842
843
    }

844
845
846
    template <class, class>
    friend class EdgeShape;

Tiago Peixoto's avatar
Tiago Peixoto committed
847
848
private:
    pos_t _pos;
849
protected:
Tiago Peixoto's avatar
Tiago Peixoto committed
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
    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) {}

    void draw(Cairo::Context& cr)
    {
        pos_t pos_begin, pos_end;

        vector<double> controls =
865
            _attrs.template get<vector<double> >(EDGE_CONTROL_POINTS);
Tiago Peixoto's avatar
Tiago Peixoto committed
866
867
868
        vector<double> gradient =
            _attrs.template get<vector<double> >(EDGE_GRADIENT);
        bool has_gradient = gradient.size() >= 2;
Tiago Peixoto's avatar
Tiago Peixoto committed
869

870
871
        edge_marker_t start_marker = _attrs.template get<edge_marker_t>(EDGE_START_MARKER);
        edge_marker_t end_marker = _attrs.template get<edge_marker_t>(EDGE_END_MARKER);
872
873
        double marker_size = _attrs.template get<double>(EDGE_MARKER_SIZE);
        marker_size = get_user_dist(cr, marker_size);
874
875
        bool sloppy = _attrs.template get<uint8_t>(EDGE_SLOPPY);

Tiago Peixoto's avatar
Tiago Peixoto committed
876
877
        pos_begin = _s.get_anchor(_t.get_pos(), cr);
        pos_end = _t.get_anchor(_s.get_pos(), cr);
878

879
880
        cr.save();

Tiago Peixoto's avatar
Tiago Peixoto committed
881
882
883
884
        if (controls.size() >= 4)
        {
            double angle = 0;
            double len = 0;
885
            if (_t.get_pos() != _s.get_pos())
Tiago Peixoto's avatar
Tiago Peixoto committed
886
887
888
889
890
891
892
893
894
895
896
            {
                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.);

897
898
                for (size_t i = 0; i < controls.size() / 2; ++i)
                    cr.user_to_device(controls[2 * i], controls[2 * i + 1]);
Tiago Peixoto's avatar
Tiago Peixoto committed
899
900
                cr.restore();

901
902
                for (size_t i = 0; i < controls.size() / 2; ++i)
                    cr.device_to_user(controls[2 * i], controls[2 * i + 1]);
Tiago Peixoto's avatar
Tiago Peixoto committed
903
904
905
906
907
908
909
910
            }
            else
            {
                pos_begin = _s.get_pos();
                len = M_PI * _s.get_size(cr);
                cr.save();
                cr.translate(pos_begin.first, pos_begin.second);
                cr.scale(len / sqrt(2), len / sqrt(2));
911
912
                for (size_t i = 0; i < controls.size() / 2; ++i)
                    cr.user_to_device(controls[2 * i], controls[2 * i + 1]);
Tiago Peixoto's avatar
Tiago Peixoto committed
913
914
                cr.restore();

915
916
                for (size_t i = 0; i < controls.size() / 2; ++i)
                    cr.device_to_user(controls[2 * i], controls[2 * i + 1]);
Tiago Peixoto's avatar
Tiago Peixoto committed
917
918
919
920
921
922
923
924
            }

            size_t N = controls.size();
            pos_begin = _s.get_anchor(make_pair(controls[0],
                                                controls[1]), cr);
            pos_end = _t.get_anchor(make_pair(controls[N - 2],
                                              controls[N - 1]), cr);

925
926
927
928
929
930
            if (_s.get_pos() == _t.get_pos())
            {
                double ox = pos_begin.first - _s.get_pos().first +
                    pos_end.first - _t.get_pos().first;
                double oy = pos_begin.second - _s.get_pos().second +
                    pos_end.second - _t.get_pos().second;
931

932
933
934
935
936
                for (size_t i = 0; i < controls.size() / 2; ++i)
                {
                    controls[2 * i] += ox / 2;
                    controls[2 * i + 1] += oy / 2;
                }
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951

                if ((start_marker != MARKER_SHAPE_NONE && start_marker != MARKER_SHAPE_BAR) ||
                    (end_marker != MARKER_SHAPE_NONE && end_marker != MARKER_SHAPE_BAR))
                {
                    for (size_t i = 0; i < controls.size() / 2; ++i)
                    {
                        pos_t cpos;
                        cpos.first = controls[2 * i];
                        cpos.second = controls[2 * i + 1];
                        move_radially(cpos, _s.get_pos(), marker_size / 2);
                        controls[2 * i] = cpos.first;
                        controls[2 * i + 1] = cpos.second;
                    }
                }

952
953
954
955
956
957
                pos_begin = _s.get_anchor(make_pair(controls[0],
                                                    controls[1]), cr);
                pos_end = _t.get_anchor(make_pair(controls[N - 2],
                                                  controls[N - 1]), cr);
            }
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
958
959


960
        color_t color = _attrs.template get<color_t>(EDGE_COLOR);
Tiago Peixoto's avatar
Tiago Peixoto committed
961
        double pw;
962
        pw = _attrs.template get<double>(EDGE_PENWIDTH);
963
        pw = get_user_dist(cr, pw);
Tiago Peixoto's avatar
Tiago Peixoto committed
964
965
        cr.set_line_width(pw);

966
967
968
969
970
971
972
973
974
975
        pos_t pos_begin_c = pos_begin;
        pos_t pos_end_c = pos_end;

        if ((start_marker != MARKER_SHAPE_NONE && start_marker != MARKER_SHAPE_BAR))
            move_radially(pos_begin_c, _s.get_pos(), marker_size / 2);
        if ((end_marker != MARKER_SHAPE_NONE && end_marker != MARKER_SHAPE_BAR))
            move_radially(pos_end_c, _t.get_pos(), marker_size / 2);



976
977
978
979
980
981
        double a = get<3>(color);
        a *= get<3>(_s._attrs.template get<color_t>(VERTEX_COLOR));
        a *= get<3>(_s._attrs.template get<color_t>(VERTEX_FILL_COLOR));
        a *= get<3>(_t._attrs.template get<color_t>(VERTEX_COLOR));
        a *= get<3>(_t._attrs.template get<color_t>(VERTEX_FILL_COLOR));

Tiago Peixoto's avatar
Tiago Peixoto committed
982
        if (!sloppy && a < 1 && !has_gradient)
983
984
985
986
987
988
989
990
        {
            // set the clip region to the correct size for better push/pop_group
            // performance
            draw_edge_markers(pos_begin, pos_end, controls, marker_size, cr);
            draw_edge_line(pos_begin_c, pos_end_c, controls, cr);
            double sx1, sy1, sx2, sy2;
            cr.get_stroke_extents(sx1, sy1, sx2, sy2);
            cr.begin_new_path();
991
992
993
994
            cr.rectangle(sx1, sy1, sx2 - sx1, sy2 - sy1);
            _s.draw(cr, true);
            _t.draw(cr, true);
            cr.set_fill_rule(Cairo::FILL_RULE_EVEN_ODD);
995
996
997
998
999
            cr.clip();

            // seamlessly blend in separate surface
            cr.push_group();
            draw_edge_markers(pos_begin, pos_end, controls, marker_size, cr);
1000
            cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color), 1);