A toolkit for working with phylogenetic data.
v0.19.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 #include <stdexcept>
39 
40 namespace genesis {
41 namespace utils {
42 
43 // =================================================================================================
44 // Svg Text
45 // =================================================================================================
46 
47 // -------------------------------------------------------------
48 // Constructors and Rule of Five
49 // -------------------------------------------------------------
50 
52  std::string const& text,
53  SvgPoint const& position,
54  SvgFont const& font,
55  SvgFill const& fill,
56  SvgStroke const& stroke
57 )
58  : text(text)
59  , position(position)
60  , font(font)
61  , fill(fill)
62  , stroke(stroke)
63  , kerning( 0.0 )
64  , letter_spacing( 0.0 )
65  , word_spacing( 0.0 )
66 {}
67 
68 // -------------------------------------------------------------
69 // Members
70 // -------------------------------------------------------------
71 
73 {
74  // Resuting box positions.
75  double t, b, l, r;
76 
77  // Dimensions.
78  auto fs = font.size;
79  double width = text.size() * fs / 1.8;
80  double height = fs * 1.2;
81 
82  // Horizontal.
83  if( anchor == Anchor::kEnd ) {
84  l = position.x - width;
85 
86  } else if( anchor == Anchor::kMiddle ) {
87  l = position.x - width / 2.0;
88 
89  } else {
90  l = position.x;
91  }
92  r = l + width;
93 
94  // Vertical.
95  // Allow for letters below the ground line.
96  b = position.y + height * 0.3;
97  t = b - height;
98 
99  return { SvgPoint( l, t ), SvgPoint( r, b ) };
100 }
101 
102 void SvgText::write( std::ostream& out, size_t indent, SvgDrawingOptions const& options ) const
103 {
104  out << repeat( SvgDocument::indentation_string, indent );
105  out << "<text";
106 
107  if( ! id.empty() ) {
108  out << svg_attribute( "id", id );
109  }
110 
111  out << svg_attribute( "x", position.x + options.offset_x );
112  out << svg_attribute( "y", position.y + options.offset_y );
113 
114  font.write( out );
115  fill.write( out );
116  stroke.write( out );
117 
118  if( anchor != Anchor::kNone ) {
119  out << anchor_to_string( anchor );
120  }
123  }
126  }
127 
128  if( kerning != 0.0 ) {
129  out << svg_attribute( "kerning", kerning );
130  }
131  if( letter_spacing != 0.0 ) {
132  out << svg_attribute( "letter-spacing", letter_spacing );
133  }
134  if( word_spacing != 0.0 ) {
135  out << svg_attribute( "word-spacing", word_spacing );
136  }
137 
138  if( dx != "" ) {
139  out << svg_attribute( "dx", dx );
140  }
141  if( dy != "" ) {
142  out << svg_attribute( "dy", dy );
143  }
144  transform.write( out );
145 
146  out << ">";
147  out << xml_escape( text );
148 
149  // if( alignment_baseline == AlignmentBaseline::kNone ) {
150  // out << xml_escape( text );
151  // } else {
152  // out << "<tspan";
153  // out << alignment_baseline_to_string( alignment_baseline );
154  // out << ">";
155  // out << xml_escape( text );
156  // out << "</tspan>";
157  // }
158 
159  out << "</text>\n";
160 }
161 
162 // -------------------------------------------------------------
163 // Helper Functions
164 // -------------------------------------------------------------
165 
167 {
168  switch( value ) {
169  case Anchor::kNone:
170  return std::string();
171  case Anchor::kStart:
172  return svg_attribute( "text-anchor", "start" );
173  case Anchor::kMiddle:
174  return svg_attribute( "text-anchor", "middle" );
175  case Anchor::kEnd:
176  return svg_attribute( "text-anchor", "end" );
177  default:
178  throw std::invalid_argument( "Invalid Svg attribute Anchor for Svg Text Element." );
179  }
180 }
181 
183 {
184  switch( value ) {
186  return std::string();
188  return svg_attribute( "dominant-baseline", "auto" );
190  return svg_attribute( "dominant-baseline", "use-script" );
192  return svg_attribute( "dominant-baseline", "no-change" );
194  return svg_attribute( "dominant-baseline", "reset-size" );
196  return svg_attribute( "dominant-baseline", "ideographic" );
198  return svg_attribute( "dominant-baseline", "alphabetic" );
200  return svg_attribute( "dominant-baseline", "hanging" );
202  return svg_attribute( "dominant-baseline", "mathematical" );
204  return svg_attribute( "dominant-baseline", "central" );
206  return svg_attribute( "dominant-baseline", "middle" );
208  return svg_attribute( "dominant-baseline", "text-after-edge" );
210  return svg_attribute( "dominant-baseline", "text-before-edge" );
212  return svg_attribute( "dominant-baseline", "inherit" );
213  default:
214  throw std::invalid_argument(
215  "Invalid Svg attribute Dominant Baseline for Svg Text Element."
216  );
217  }
218 }
219 
221 {
222  switch( value ) {
224  return std::string();
226  return svg_attribute( "alignment-baseline", "auto" );
228  return svg_attribute( "alignment-baseline", "baseline" );
230  return svg_attribute( "alignment-baseline", "before-edge" );
232  return svg_attribute( "alignment-baseline", "text-before-edge" );
234  return svg_attribute( "alignment-baseline", "middle" );
236  return svg_attribute( "alignment-baseline", "central" );
238  return svg_attribute( "alignment-baseline", "after-edge" );
240  return svg_attribute( "alignment-baseline", "text-after-edge" );
242  return svg_attribute( "alignment-baseline", "ideographic" );
244  return svg_attribute( "alignment-baseline", "alphabetic" );
246  return svg_attribute( "alignment-baseline", "hanging" );
248  return svg_attribute( "alignment-baseline", "mathematical" );
250  return svg_attribute( "alignment-baseline", "inherit" );
251  default:
252  throw std::invalid_argument(
253  "Invalid Svg attribute Alignment Baseline for Svg Text Element."
254  );
255  }
256 }
257 
258 } // namespace utils
259 } // namespace genesis
std::string dy
Definition: text.hpp:163
SvgTransform transform
Definition: text.hpp:165
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...
static std::string alignment_baseline_to_string(AlignmentBaseline value)
Definition: text.cpp:220
void write(std::ostream &out) const
Definition: attributes.cpp:163
void write(std::ostream &out, size_t indent=0, SvgDrawingOptions const &options=SvgDrawingOptions()) const
Definition: text.cpp:102
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:377
std::string xml_escape(std::string const &txt)
Escape special XML characters.
SvgBox bounding_box() const
Definition: text.cpp:72
AlignmentBaseline alignment_baseline
Definition: text.hpp:156
std::string dx
Definition: text.hpp:162
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:231
Provides some commonly used string utility functions.
std::string text
Definition: text.hpp:147
void write(std::ostream &out) const
Definition: attributes.cpp:79
DominantBaseline dominant_baseline
Definition: text.hpp:155
void write(std::ostream &out) const
Definition: attributes.cpp:304
static std::string anchor_to_string(Anchor value)
Definition: text.cpp:166
static std::string dominant_baseline_to_string(DominantBaseline value)
Definition: text.cpp:182
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:51
static std::string indentation_string
void write(std::ostream &out) const
Definition: attributes.cpp:210