A library for working with phylogenetic and population genetic data.
v0.32.0
group.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2022 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 
58  // We apply the group transformations here. This could potentally oversize the box again,
59  // as we might rotate stuff etc, but it's good enough for now.
60  if( bbox.empty() ) {
61  return bbox;
62  }
63  return transform.apply( bbox );
64 }
65 
66 void SvgGroup::write( std::ostream& out, size_t indent, SvgDrawingOptions const& options ) const
67 {
68  // Extra indent, to make it nice if a hyperlink is given.
69  size_t extra_in = 0;
70 
71  // Hyperlink opening tag, if set.
72  if( ! hyperlink_.empty() ) {
73  extra_in = 1;
75  out << "<a";
76  for( auto const& attr : hyperlink_ ) {
77  out << svg_attribute( attr.first, attr.second );
78  }
79  out << ">\n";
80  }
81 
82  // Group opening tag.
83  out << repeat( SvgDocument::indentation_string, indent + extra_in );
84  out << "<g";
85  if( ! id.empty() ) {
86  out << svg_attribute( "id", id );
87  }
88  transform.write( out );
89  out << ">\n";
90 
91  // Print content.
92  for( auto const& elem : content_ ) {
93  elem.write( out, indent + extra_in + 1, options );
94  }
95 
96  // Group closing tag.
97  out << repeat( SvgDocument::indentation_string, indent + extra_in );
98  out << "</g>\n";
99 
100  // Hyperlink closing tag, if set.
101  if( ! hyperlink_.empty() ) {
103  out << "</a>\n";
104  }
105 }
106 
108 {
109  content_.push_back( object );
110  return *this;
111 }
112 
114 {
115  content_.push_back( std::move( object ));
116  return *this;
117 }
118 
120 {
121  std::reverse( content_.begin(), content_.end() );
122  return *this;
123 }
124 
126 {
127  return add( object );
128 }
129 
131 {
132  return add( std::move( object ));
133 }
134 
135 } // namespace utils
136 } // 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:719
genesis::utils::SvgBox::combine
static SvgBox combine(SvgBox const &lhs, SvgBox const &rhs)
Definition: utils/formats/svg/helper.hpp:181
genesis::utils::SvgGroup::content_
std::vector< SvgObject > content_
Definition: group.hpp:158
genesis::utils::SvgTransform::apply
SvgPoint apply(SvgPoint const &p) const
Apply all transformations to a point, and return the new transformed coordinate.
Definition: attributes.cpp:363
genesis::utils::SvgGroup::hyperlink_
std::unordered_map< std::string, std::string > hyperlink_
Definition: group.hpp:160
genesis::utils::SvgBox
Definition: utils/formats/svg/helper.hpp:127
group.hpp
genesis::utils::SvgTransform::write
void write(std::ostream &out) const
Definition: attributes.cpp:394
genesis::utils::SvgDocument::indentation_string
static std::string indentation_string
Definition: svg/document.hpp:60
string.hpp
Provides some commonly used string utility functions.
document.hpp
genesis::utils::SvgBox::empty
bool empty() const
Definition: utils/formats/svg/helper.hpp:172
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:229
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:1001
genesis::utils::SvgGroup::bounding_box
SvgBox bounding_box() const
Definition: group.cpp:50
genesis::utils::SvgGroup::transform
SvgTransform transform
Definition: group.hpp:159
genesis::utils::SvgGroup::add
self_type & add(SvgObject const &object)
Add an SvgObject to the document.
Definition: group.cpp:107
genesis::utils::SvgGroup::operator<<
self_type & operator<<(SvgObject const &object)
Shortcut operator for add(), which allows an even more fluent interface.
Definition: group.cpp:125
genesis::utils::SvgDrawingOptions
Definition: utils/formats/svg/helper.hpp:213
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:66
genesis::utils::SvgGroup::reverse
self_type & reverse()
Reverse the order to the stored SvgObjects.
Definition: group.cpp:119