A library for working with phylogenetic and population genetic data.
v0.32.0
text.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 
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 transform.apply( SvgBox{ SvgPoint( l, t ), SvgPoint( r, b ) });
100 }
101 
102 void SvgText::write( std::ostream& out, size_t indent, SvgDrawingOptions const& options ) const
103 {
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
genesis::utils::SvgText::DominantBaseline::kIdeographic
@ kIdeographic
genesis::utils::SvgText::write
void write(std::ostream &out, size_t indent=0, SvgDrawingOptions const &options=SvgDrawingOptions()) const
Definition: text.cpp:102
genesis::utils::SvgText::DominantBaseline::kNoChange
@ kNoChange
genesis::utils::SvgPoint::x
double x
Definition: utils/formats/svg/helper.hpp:69
genesis::utils::SvgText::anchor
Anchor anchor
Definition: text.hpp:154
helper.hpp
genesis::utils::SvgText::SvgText
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
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::SvgText::DominantBaseline::kTextBeforeEdge
@ kTextBeforeEdge
genesis::utils::SvgText::bounding_box
SvgBox bounding_box() const
Definition: text.cpp:72
genesis::utils::SvgText::font
SvgFont font
Definition: text.hpp:150
genesis::tree::height
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...
Definition: tree/common_tree/functions.cpp:169
genesis::utils::SvgPoint::y
double y
Definition: utils/formats/svg/helper.hpp:70
genesis::utils::SvgText::AlignmentBaseline::kBaseline
@ kBaseline
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::SvgText::position
SvgPoint position
Definition: text.hpp:148
genesis::utils::SvgText::Anchor::kNone
@ kNone
genesis::utils::SvgText::AlignmentBaseline::kIdeographic
@ kIdeographic
genesis::utils::SvgText::stroke
SvgStroke stroke
Definition: text.hpp:152
genesis::utils::SvgText::dx
std::string dx
Definition: text.hpp:162
genesis::utils::SvgText::word_spacing
double word_spacing
Definition: text.hpp:160
genesis::utils::SvgText::Anchor::kEnd
@ kEnd
genesis::utils::SvgText::AlignmentBaseline::kTextBeforeEdge
@ kTextBeforeEdge
genesis::utils::SvgText::DominantBaseline::kCentral
@ kCentral
genesis::utils::SvgDrawingOptions::offset_y
double offset_y
Definition: utils/formats/svg/helper.hpp:216
genesis::utils::SvgBox
Definition: utils/formats/svg/helper.hpp:127
genesis::utils::SvgDrawingOptions::offset_x
double offset_x
Definition: utils/formats/svg/helper.hpp:215
genesis::utils::SvgText::Anchor::kStart
@ kStart
genesis::utils::SvgTransform::write
void write(std::ostream &out) const
Definition: attributes.cpp:394
genesis::utils::SvgText::DominantBaseline::kHanging
@ kHanging
genesis::utils::SvgText::AlignmentBaseline::kMathematical
@ kMathematical
genesis::utils::SvgDocument::indentation_string
static std::string indentation_string
Definition: svg/document.hpp:60
genesis::utils::SvgFont::size
double size
Definition: attributes.hpp:229
genesis::utils::SvgPoint
Definition: utils/formats/svg/helper.hpp:57
genesis::utils::SvgStroke
Definition: attributes.hpp:49
string.hpp
Provides some commonly used string utility functions.
genesis::utils::SvgText::DominantBaseline
DominantBaseline
Definition: text.hpp:65
document.hpp
genesis::utils::SvgText::DominantBaseline::kMiddle
@ kMiddle
genesis::utils::SvgText::DominantBaseline::kNone
@ kNone
genesis::utils::SvgText::DominantBaseline::kAlphabetic
@ kAlphabetic
genesis::utils::SvgText::DominantBaseline::kMathematical
@ kMathematical
genesis::utils::SvgText::text
std::string text
Definition: text.hpp:147
genesis::utils::SvgText::AlignmentBaseline::kCentral
@ kCentral
genesis::utils::SvgText::DominantBaseline::kInherit
@ kInherit
genesis::utils::SvgText::fill
SvgFill fill
Definition: text.hpp:151
genesis::utils::SvgText::dominant_baseline_to_string
static std::string dominant_baseline_to_string(DominantBaseline value)
Definition: text.cpp:182
genesis::utils::xml_escape
std::string xml_escape(std::string const &txt)
Escape special XML characters.
Definition: utils/formats/xml/helper.cpp:45
genesis::utils::SvgText::AlignmentBaseline::kInherit
@ kInherit
genesis::utils::SvgText::dy
std::string dy
Definition: text.hpp:163
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::SvgText::Anchor::kMiddle
@ kMiddle
genesis::utils::SvgText::letter_spacing
double letter_spacing
Definition: text.hpp:159
genesis::utils::SvgFill
Definition: attributes.hpp:131
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::SvgText::anchor_to_string
static std::string anchor_to_string(Anchor value)
Definition: text.cpp:166
genesis::utils::SvgText::AlignmentBaseline::kAfterEdge
@ kAfterEdge
genesis::utils::SvgText::dominant_baseline
DominantBaseline dominant_baseline
Definition: text.hpp:155
genesis::utils::SvgFill::write
void write(std::ostream &out) const
Definition: attributes.cpp:166
genesis::utils::SvgFont
Definition: attributes.hpp:195
genesis::utils::SvgText::AlignmentBaseline::kNone
@ kNone
genesis::utils::SvgText::Anchor
Anchor
Definition: text.hpp:57
genesis::utils::SvgText::alignment_baseline_to_string
static std::string alignment_baseline_to_string(AlignmentBaseline value)
Definition: text.cpp:220
genesis::utils::SvgText::AlignmentBaseline::kMiddle
@ kMiddle
genesis::utils::SvgText::AlignmentBaseline::kAuto
@ kAuto
genesis::utils::SvgText::AlignmentBaseline::kBeforeEdge
@ kBeforeEdge
text.hpp
genesis::utils::SvgText::DominantBaseline::kTextAfterEdge
@ kTextAfterEdge
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::SvgText::AlignmentBaseline::kAlphabetic
@ kAlphabetic
genesis::utils::SvgText::transform
SvgTransform transform
Definition: text.hpp:165
genesis::utils::SvgText::kerning
double kerning
Definition: text.hpp:158
genesis::utils::SvgStroke::write
void write(std::ostream &out) const
Definition: attributes.cpp:82
genesis::utils::SvgText::AlignmentBaseline
AlignmentBaseline
Definition: text.hpp:83
genesis::utils::SvgDrawingOptions
Definition: utils/formats/svg/helper.hpp:213
genesis::utils::SvgText::AlignmentBaseline::kHanging
@ kHanging
genesis::utils::SvgText::DominantBaseline::kUseScript
@ kUseScript
genesis::utils::SvgText::DominantBaseline::kResetSize
@ kResetSize
genesis::utils::SvgText::DominantBaseline::kAuto
@ kAuto
genesis::utils::SvgText::AlignmentBaseline::kTextAfterEdge
@ kTextAfterEdge
genesis::utils::SvgText::alignment_baseline
AlignmentBaseline alignment_baseline
Definition: text.hpp:156
genesis::utils::SvgFont::write
void write(std::ostream &out) const
Definition: attributes.cpp:213