A library for working with phylogenetic and population genetic data.
v0.27.0
group.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2021 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lczech@carnegiescience.edu>
20  Department of Plant Biology, Carnegie Institution For Science
21  260 Panama Street, Stanford, CA 94305, USA
22 */
23 
32 
35 
36 #include <algorithm>
37 #include <ostream>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Svg Group
44 // =================================================================================================
45 
46 // -------------------------------------------------------------
47 // Members
48 // -------------------------------------------------------------
49 
51 {
52  // Get bounding box of all elements and the dimensions of the document.
53  SvgBox bbox;
54  for( auto const& elem : content_ ) {
55  bbox = SvgBox::combine( bbox, elem.bounding_box() );
56  }
57  return bbox;
58 }
59 
60 void SvgGroup::write( std::ostream& out, size_t indent, SvgDrawingOptions const& options ) const
61 {
62  // Extra indent, to make it nice if a hyperlink is given.
63  size_t extra_in = 0;
64 
65  // Hyperlink opening tag, if set.
66  if( ! hyperlink_.empty() ) {
67  extra_in = 1;
69  out << "<a";
70  for( auto const& attr : hyperlink_ ) {
71  out << svg_attribute( attr.first, attr.second );
72  }
73  out << ">\n";
74  }
75 
76  // Group opening tag.
77  out << repeat( SvgDocument::indentation_string, indent + extra_in );
78  out << "<g";
79  if( ! id.empty() ) {
80  out << svg_attribute( "id", id );
81  }
82  transform.write( out );
83  out << ">\n";
84 
85  // Print content.
86  for( auto const& elem : content_ ) {
87  elem.write( out, indent + extra_in + 1, options );
88  }
89 
90  // Group closing tag.
91  out << repeat( SvgDocument::indentation_string, indent + extra_in );
92  out << "</g>\n";
93 
94  // Hyperlink closing tag, if set.
95  if( ! hyperlink_.empty() ) {
97  out << "</a>\n";
98  }
99 }
100 
102 {
103  content_.push_back( object );
104  return *this;
105 }
106 
108 {
109  content_.push_back( std::move( object ));
110  return *this;
111 }
112 
114 {
115  std::reverse( content_.begin(), content_.end() );
116  return *this;
117 }
118 
120 {
121  return add( object );
122 }
123 
125 {
126  return add( std::move( object ));
127 }
128 
129 } // namespace utils
130 } // namespace genesis
genesis::utils::indent
std::string indent(std::string const &text, std::string const &indentation)
Indent each line of text with indentation and return the result.
Definition: string.cpp:522
genesis::utils::SvgBox::combine
static SvgBox combine(SvgBox const &lhs, SvgBox const &rhs)
Definition: utils/formats/svg/helper.hpp:170
genesis::utils::SvgGroup::content_
std::vector< SvgObject > content_
Definition: group.hpp:153
genesis::utils::SvgGroup::hyperlink_
std::unordered_map< std::string, std::string > hyperlink_
Definition: group.hpp:155
genesis::utils::SvgBox
Definition: utils/formats/svg/helper.hpp:116
group.hpp
genesis::utils::SvgTransform::write
void write(std::ostream &out) const
Definition: attributes.cpp:304
genesis::utils::SvgDocument::indentation_string
static std::string indentation_string
Definition: svg/document.hpp:59
string.hpp
Provides some commonly used string utility functions.
document.hpp
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::svg_attribute
std::string svg_attribute(std::string const &name, T const &value, std::string const &unit="")
Definition: utils/formats/svg/helper.hpp:218
genesis::utils::SvgGroup::empty
bool empty() const
Definition: group.hpp:135
genesis::utils::repeat
std::string repeat(std::string const &word, size_t times)
Take a string and repeat it a given number of times.
Definition: string.cpp:758
genesis::utils::SvgGroup::bounding_box
SvgBox bounding_box() const
Definition: group.cpp:50
genesis::utils::SvgGroup::transform
SvgTransform transform
Definition: group.hpp:154
genesis::utils::SvgGroup::add
self_type & add(SvgObject const &object)
Add an SvgObject to the document.
Definition: group.cpp:101
genesis::utils::SvgGroup::operator<<
self_type & operator<<(SvgObject const &object)
Shortcut operator for add(), which allows an even more fluent interface.
Definition: group.cpp:119
genesis::utils::SvgDrawingOptions
Definition: utils/formats/svg/helper.hpp:202
genesis::utils::SvgGroup
Definition: group.hpp:50
genesis::utils::SvgObject
Definition: object.hpp:46
genesis::utils::SvgGroup::write
void write(std::ostream &out, size_t indent=0, SvgDrawingOptions const &options=SvgDrawingOptions()) const
Definition: group.cpp:60
genesis::utils::SvgGroup::reverse
self_type & reverse()
Reverse the order to the stored SvgObjects.
Definition: group.cpp:113