A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
text.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 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 <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
36 
37 #include <ostream>
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Svg Text
44 // =================================================================================================
45 
46 // -------------------------------------------------------------
47 // Constructors and Rule of Five
48 // -------------------------------------------------------------
49 
51  std::string const& text,
52  SvgPoint const& position,
53  SvgFont const& font,
54  SvgFill const& fill,
55  SvgStroke const& stroke
56 )
57  : text(text)
58  , position(position)
59  , font(font)
60  , fill(fill)
61  , stroke(stroke)
62  , kerning( 0.0 )
63  , letter_spacing( 0.0 )
64  , word_spacing( 0.0 )
65 {}
66 
67 // -------------------------------------------------------------
68 // Members
69 // -------------------------------------------------------------
70 
72 {
73  // Resuting box positions.
74  double t, b, l, r;
75 
76  // Dimensions.
77  auto fs = font.size;
78  double width = text.size() * fs / 1.8;
79  double height = fs * 1.2;
80 
81  // Horizontal.
82  if( anchor == Anchor::kEnd ) {
83  l = position.x - width;
84 
85  } else if( anchor == Anchor::kMiddle ) {
86  l = position.x - width / 2.0;
87 
88  } else {
89  l = position.x;
90  }
91  r = l + width;
92 
93  // Vertical.
94  // Allow for letters below the ground line.
95  b = position.y + height * 0.3;
96  t = b - height;
97 
98  return { SvgPoint( l, t ), SvgPoint( r, b ) };
99 }
100 
101 void SvgText::write( std::ostream& out, size_t indent, SvgDrawingOptions const& options ) const
102 {
103  out << repeat( SvgDocument::indentation_string, indent );
104  out << "<text";
105 
106  if( ! id.empty() ) {
107  out << svg_attribute( "id", id );
108  }
109 
110  out << svg_attribute( "x", position.x + options.offset_x );
111  out << svg_attribute( "y", position.y + options.offset_y );
112 
113  font.write( out );
114  fill.write( out );
115  stroke.write( out );
116 
117  switch( anchor ) {
118  case Anchor::kNone:
119  break;
120  case Anchor::kStart:
121  out << svg_attribute( "text-anchor", "start" );
122  break;
123  case Anchor::kMiddle:
124  out << svg_attribute( "text-anchor", "middle" );
125  break;
126  case Anchor::kEnd:
127  out << svg_attribute( "text-anchor", "end" );
128  break;
129  }
130 
131  if( kerning != 0.0 ) {
132  out << svg_attribute( "kerning", kerning );
133  }
134  if( letter_spacing != 0.0 ) {
135  out << svg_attribute( "letter-spacing", letter_spacing );
136  }
137  if( word_spacing != 0.0 ) {
138  out << svg_attribute( "word-spacing", word_spacing );
139  }
140 
141  if( dx != "" ) {
142  out << svg_attribute( "dx", dx );
143  }
144  if( dy != "" ) {
145  out << svg_attribute( "dy", dy );
146  }
147  transform.write( out );
148 
149  out << ">";
150  out << xml_escape( text );
151  out << "</text>\n";
152 }
153 
154 } // namespace utils
155 } // namespace genesis
std::string dy
Definition: text.hpp:117
SvgTransform transform
Definition: text.hpp:119
double height(Tree const &tree)
Get the height of the tree, i.e., the longest distance from the root to a leaf, measured using the br...
void write(std::ostream &out) const
Definition: attributes.cpp:165
void write(std::ostream &out, size_t indent=0, SvgDrawingOptions const &options=SvgDrawingOptions()) const
Definition: text.cpp:101
std::string svg_attribute(std::string const &name, T const &value, std::string const &unit="")
std::string repeat(std::string const &word, size_t times)
Take a string and repeat it a given number of times.
Definition: string.cpp:378
std::string xml_escape(std::string const &txt)
Escape special XML characters.
SvgBox bounding_box() const
Definition: text.cpp:71
std::string dx
Definition: text.hpp:116
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:158
Provides some commonly used string utility functions.
std::string text
Definition: text.hpp:103
void write(std::ostream &out) const
Definition: attributes.cpp:79
void write(std::ostream &out) const
Definition: attributes.cpp:306
SvgText(std::string const &text="", SvgPoint const &position=SvgPoint(), SvgFont const &font=SvgFont(), SvgFill const &fill=SvgFill(), SvgStroke const &stroke=SvgStroke(SvgStroke::Type::kOmit))
Definition: text.cpp:50
static std::string indentation_string
void write(std::ostream &out) const
Definition: attributes.cpp:212