A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
attributes.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_FORMATS_SVG_ATTRIBUTES_H_
2 #define GENESIS_UTILS_FORMATS_SVG_ATTRIBUTES_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
37 
38 #include <iosfwd>
39 #include <string>
40 #include <vector>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Svg Stroke
47 // =================================================================================================
48 
49 struct SvgStroke
50 {
51 public:
52 
53  // -------------------------------------------------------------------------
54  // Typedefs and Enums
55  // -------------------------------------------------------------------------
56 
58 
59  enum class Type
60  {
61  kColor,
62  kGradient,
63  kNone,
64  kOmit
65  };
66 
67  enum class LineCap
68  {
69  kOmit,
70  kButt,
71  kSquare,
72  kRound
73  };
74 
75  enum class LineJoin
76  {
77  kOmit,
78  kMiter,
79  kRound,
80  kBevel
81  };
82 
83  // -------------------------------------------------------------------------
84  // Constructors and Rule of Five
85  // -------------------------------------------------------------------------
86 
87  explicit SvgStroke( Type type = Type::kColor );
88  explicit SvgStroke( Color color, double width = 1.0 );
89  explicit SvgStroke( std::string gradient_id );
90 
91  ~SvgStroke() = default;
92 
93  SvgStroke( SvgStroke const& ) = default;
94  SvgStroke( SvgStroke&& ) = default;
95 
96  SvgStroke& operator= ( SvgStroke const& ) = default;
97  SvgStroke& operator= ( SvgStroke&& ) = default;
98 
99  // -------------------------------------------------------------------------
100  // Drawing Function
101  // -------------------------------------------------------------------------
102 
103  void write( std::ostream& out ) const;
104 
105  // -------------------------------------------------------------------------
106  // Properties
107  // -------------------------------------------------------------------------
108 
110 
111  std::string gradient_id;
112 
114 
115  double width;
116  std::string width_unit;
117 
120  double miterlimit;
121 
122  std::vector< double > dash_array;
123  double dash_offset;
124 
125 };
126 
127 // =================================================================================================
128 // Svg Fill
129 // =================================================================================================
130 
131 struct SvgFill
132 {
133 public:
134 
135  // -------------------------------------------------------------------------
136  // Typedefs and Enums
137  // -------------------------------------------------------------------------
138 
140 
141  enum class Type
142  {
143  kColor,
144  kGradient,
145  kNone,
146  kOmit
147  };
148 
149  enum class Rule
150  {
151  kNone,
152  kNonZero,
153  kEvenOdd
154  };
155 
156  // -------------------------------------------------------------------------
157  // Constructors and Rule of Five
158  // -------------------------------------------------------------------------
159 
160  explicit SvgFill( Type type = Type::kColor );
161  explicit SvgFill( Color color );
162  explicit SvgFill( std::string gradient_id );
163 
164  ~SvgFill() = default;
165 
166  SvgFill( SvgFill const& ) = default;
167  SvgFill( SvgFill&& ) = default;
168 
169  SvgFill& operator= ( SvgFill const& ) = default;
170  SvgFill& operator= ( SvgFill&& ) = default;
171 
172  // -------------------------------------------------------------------------
173  // Drawing Function
174  // -------------------------------------------------------------------------
175 
176  void write( std::ostream& out ) const;
177 
178  // -------------------------------------------------------------------------
179  // Properties
180  // -------------------------------------------------------------------------
181 
183 
184  std::string gradient_id;
185 
188 
189 };
190 
191 // =================================================================================================
192 // Svg Font
193 // =================================================================================================
194 
195 struct SvgFont
196 {
197 public:
198 
199  // -------------------------------------------------------------------------
200  // Typedefs and Enums
201  // -------------------------------------------------------------------------
202 
204 
205  // -------------------------------------------------------------------------
206  // Constructors and Rule of Five
207  // -------------------------------------------------------------------------
208 
209  explicit SvgFont( double size = 10, std::string const& family = "Verdana" );
210 
211  ~SvgFont() = default;
212 
213  SvgFont( SvgFont const& ) = default;
214  SvgFont( SvgFont&& ) = default;
215 
216  SvgFont& operator= ( SvgFont const& ) = default;
217  SvgFont& operator= ( SvgFont&& ) = default;
218 
219  // -------------------------------------------------------------------------
220  // Drawing Function
221  // -------------------------------------------------------------------------
222 
223  void write( std::ostream& out ) const;
224 
225  // -------------------------------------------------------------------------
226  // Properties
227  // -------------------------------------------------------------------------
228 
229  double size;
230  std::string family;
231 
232 };
233 
234 // =================================================================================================
235 // Svg Transformation
236 // =================================================================================================
237 
239 {
240 public:
241 
242  // -------------------------------------------------------------------------
243  // Typedefs and Enums
244  // -------------------------------------------------------------------------
245 
247 
248  // -------------------------------------------------------------------------
249  // Subclass Translate
250  // -------------------------------------------------------------------------
251 
252  struct Translate
253  {
254  public:
255 
256  // -------------------------------------------------------------
257  // Constructors and Rule of Five
258  // -------------------------------------------------------------
259 
260  Translate() = default;
261 
262  Translate( double tx, double ty )
263  : tx(tx)
264  , ty(ty)
265  {}
266 
268  : Translate( offset.x, offset.y )
269  {}
270 
271  ~Translate() = default;
272 
273  Translate( Translate const& ) = default;
274  Translate( Translate&& ) = default;
275 
276  Translate& operator= ( Translate const& ) = default;
277  Translate& operator= ( Translate&& ) = default;
278 
279  // -------------------------------------------------------------
280  // Operators and Write
281  // -------------------------------------------------------------
282 
283  void write( std::ostream& out ) const;
284 
285  // -------------------------------------------------------------
286  // Data Members
287  // -------------------------------------------------------------
288 
289  double tx = 0.0;
290  double ty = 0.0;
291  };
292 
293  // -------------------------------------------------------------------------
294  // Subclass Rotate
295  // -------------------------------------------------------------------------
296 
297  struct Rotate
298  {
299  public:
300 
301  // -------------------------------------------------------------
302  // Constructors and Rule of Five
303  // -------------------------------------------------------------
304 
305  Rotate() = default;
306 
307  Rotate( double angle )
308  : a(angle)
309  {}
310 
311  Rotate( double angle, double cx, double cy )
312  : a(angle)
313  , cx(cx)
314  , cy(cy)
315  {}
316 
317  Rotate( double angle, SvgPoint offset )
318  : Rotate( angle, offset.x, offset.y )
319  {}
320 
321  ~Rotate() = default;
322 
323  Rotate( Rotate const& ) = default;
324  Rotate( Rotate&& ) = default;
325 
326  Rotate& operator= ( Rotate const& ) = default;
327  Rotate& operator= ( Rotate&& ) = default;
328 
329  // -------------------------------------------------------------
330  // Operators and Write
331  // -------------------------------------------------------------
332 
333  void write( std::ostream& out ) const;
334 
335  // -------------------------------------------------------------
336  // Data Members
337  // -------------------------------------------------------------
338 
339  double a = 0.0;
340  double cx = 0.0;
341  double cy = 0.0;
342  };
343 
344  // -------------------------------------------------------------------------
345  // Subclass Scale
346  // -------------------------------------------------------------------------
347 
348  struct Scale
349  {
350  public:
351 
352  // -------------------------------------------------------------
353  // Constructors and Rule of Five
354  // -------------------------------------------------------------
355 
356  Scale() = default;
357 
358  Scale( double sxy )
359  : sx(sxy)
360  , sy(sxy)
361  {}
362 
363  Scale( double sx, double sy )
364  : sx(sx)
365  , sy(sy)
366  {}
367 
368  ~Scale() = default;
369 
370  Scale( Scale const& ) = default;
371  Scale( Scale&& ) = default;
372 
373  Scale& operator= ( Scale const& ) = default;
374  Scale& operator= ( Scale&& ) = default;
375 
376  // -------------------------------------------------------------
377  // Operators and Write
378  // -------------------------------------------------------------
379 
380  void write( std::ostream& out ) const;
381 
382  // -------------------------------------------------------------
383  // Data Members
384  // -------------------------------------------------------------
385 
386  double sx = 1.0;
387  double sy = 1.0;
388  };
389 
390  // -------------------------------------------------------------------------
391  // Subclass Skew
392  // -------------------------------------------------------------------------
393 
394  struct Skew
395  {
396  public:
397 
398  // -------------------------------------------------------------
399  // Constructors and Rule of Five
400  // -------------------------------------------------------------
401 
402  Skew() = default;
403 
404  Skew( double a )
405  : ax(a)
406  , ay(a)
407  {}
408 
409  Skew( double ax, double ay )
410  : ax(ax)
411  , ay(ay)
412  {}
413 
414  ~Skew() = default;
415 
416  Skew( Skew const& ) = default;
417  Skew( Skew&& ) = default;
418 
419  Skew& operator= ( Skew const& ) = default;
420  Skew& operator= ( Skew&& ) = default;
421 
422  // -------------------------------------------------------------
423  // Operators and Write
424  // -------------------------------------------------------------
425 
426  void write( std::ostream& out ) const;
427 
428  // -------------------------------------------------------------
429  // Data Members
430  // -------------------------------------------------------------
431 
432  double ax = 0.0;
433  double ay = 0.0;
434  };
435 
436  // -------------------------------------------------------------------------
437  // Subclass Matrix
438  // -------------------------------------------------------------------------
439 
440  struct Matrix
441  {
442  public:
443 
444  // -------------------------------------------------------------
445  // Constructors and Rule of Five
446  // -------------------------------------------------------------
447 
448  Matrix() = default;
449 
450  Matrix( double a, double b, double c, double d, double e, double f )
451  : a(a)
452  , b(b)
453  , c(c)
454  , d(d)
455  , e(e)
456  , f(f)
457  {}
458 
459  ~Matrix() = default;
460 
461  Matrix( Matrix const& ) = default;
462  Matrix( Matrix&& ) = default;
463 
464  Matrix& operator= ( Matrix const& ) = default;
465  Matrix& operator= ( Matrix&& ) = default;
466 
467  // -------------------------------------------------------------
468  // Operators and Write
469  // -------------------------------------------------------------
470 
471  void write( std::ostream& out ) const;
472 
473  // -------------------------------------------------------------
474  // Data Members
475  // -------------------------------------------------------------
476 
477  double a = 1.0;
478  double b = 0.0;
479  double c = 0.0;
480  double d = 1.0;
481  double e = 0.0;
482  double f = 0.0;
483  };
484 
485  // -------------------------------------------------------------------------
486  // Transformation
487  // -------------------------------------------------------------------------
488 
494  {
495  public:
496 
497  // -------------------------------------------------------------
498  // Typedefs and Enums
499  // -------------------------------------------------------------
500 
502 
503  // -------------------------------------------------------------
504  // Constructors and Rule of Five
505  // -------------------------------------------------------------
506 
507  template< typename T >
508  Transformation( T const& object )
509  : pimpl_( make_unique< Model<T> >( object ))
510  {}
511 
513  : pimpl_( other.pimpl_->clone() )
514  {}
515 
516  Transformation( Transformation&& ) = default;
517 
519  {
520  std::swap( pimpl_, other.pimpl_ );
521  return *this;
522  }
523 
524  ~Transformation() = default;
525 
526  // -------------------------------------------------------------
527  // Members
528  // -------------------------------------------------------------
529 
530  void write(
531  std::ostream& out
532  ) const {
533  pimpl_->write_( out );
534  }
535 
536  // -------------------------------------------------------------
537  // Internal Members
538  // -------------------------------------------------------------
539 
540  private:
541 
542  struct Concept
543  {
544  virtual ~Concept() {}
545 
546  virtual void write_(
547  std::ostream& out
548  ) const = 0;
549 
550  virtual std::unique_ptr< Concept > clone() const = 0;
551  };
552 
553  template< typename T >
554  struct Model : Concept
555  {
556  Model( T const& value )
557  : object_( value )
558  {}
559 
560  void write_(
561  std::ostream& out
562  ) const {
563  object_.write( out );
564  }
565 
566  std::unique_ptr< Concept > clone() const
567  {
568  return make_unique< Model<T> >( object_ );
569  }
570 
571  T object_;
572  };
573 
574  std::unique_ptr< Concept > pimpl_;
575 
576  };
577 
578  // -------------------------------------------------------------------------
579  // Constructors and Rule of Five
580  // -------------------------------------------------------------------------
581 
582  SvgTransform() = default;
583  ~SvgTransform() = default;
584 
585  SvgTransform( SvgTransform const& ) = default;
586  SvgTransform( SvgTransform&& ) = default;
587 
588  SvgTransform& operator= ( SvgTransform const& ) = default;
589  SvgTransform& operator= ( SvgTransform&& ) = default;
590 
591  // -------------------------------------------------------------------------
592  // Drawing Function
593  // -------------------------------------------------------------------------
594 
595  void append( Transformation&& t );
596  void append( Transformation const& t );
597 
598  void write( std::ostream& out ) const;
599 
600  void clear()
601  {
602  transformations.clear();
603  }
604 
605  // -------------------------------------------------------------------------
606  // Properties
607  // -------------------------------------------------------------------------
608 
609  std::vector<Transformation> transformations;
610 
611 };
612 
613 } // namespace utils
614 } // namespace genesis
615 
616 #endif // include guard
void offset(Histogram &h, double value)
Definition: operations.cpp:47
SvgFont & operator=(SvgFont const &)=default
void append(Transformation &&t)
Definition: attributes.cpp:294
Matrix(double a, double b, double c, double d, double e, double f)
Definition: attributes.hpp:450
void write(std::ostream &out) const
Definition: attributes.cpp:235
void swap(SequenceSet &lhs, SequenceSet &rhs)
void write(std::ostream &out) const
Definition: attributes.cpp:163
void write(std::ostream &out) const
Definition: attributes.cpp:282
Transformation & operator=(Transformation other)
Definition: attributes.hpp:518
void write(std::ostream &out) const
Definition: attributes.cpp:250
Skew & operator=(Skew const &)=default
Header of Color class.
SvgTransform & operator=(SvgTransform const &)=default
Provides some valuable additions to STD.
Matrix & operator=(Matrix const &)=default
Translate & operator=(Translate const &)=default
SvgFill(Type type=Type::kColor)
Definition: attributes.cpp:141
Rotate & operator=(Rotate const &)=default
Rotate(double angle, double cx, double cy)
Definition: attributes.hpp:311
Rotate(double angle, SvgPoint offset)
Definition: attributes.hpp:317
std::unique_ptr< T > make_unique(Args &&...args)
Returns a std::unique_ptr for a given type.
Definition: std.hpp:68
void write(std::ostream &out) const
Definition: attributes.hpp:530
void write(std::ostream &out) const
Definition: attributes.cpp:79
std::vector< Transformation > transformations
Definition: attributes.hpp:609
Internal helper class used as an abstraction to be able to store Transformations without need for inh...
Definition: attributes.hpp:493
void write(std::ostream &out) const
Definition: attributes.cpp:304
Transformation(Transformation const &other)
Definition: attributes.hpp:512
SvgFont(double size=10, std::string const &family="Verdana")
Definition: attributes.cpp:201
Scale & operator=(Scale const &)=default
void write(std::ostream &out) const
Definition: attributes.cpp:265
void write(std::ostream &out) const
Definition: attributes.cpp:224
std::vector< double > dash_array
Definition: attributes.hpp:122
SvgStroke & operator=(SvgStroke const &)=default
SvgFill & operator=(SvgFill const &)=default
SvgStroke(Type type=Type::kColor)
Definition: attributes.cpp:50
void write(std::ostream &out) const
Definition: attributes.cpp:210