Chemical Data Processing Library C++ API - Version 1.4.0
TextBlockPrimitive2D.hpp
Go to the documentation of this file.
1 /*
2  * TextBlockPrimitive2D.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_VIS_TEXTBLOCKPRIMITIVE2D_HPP
30 #define CDPL_VIS_TEXTBLOCKPRIMITIVE2D_HPP
31 
32 #include <string>
33 #include <vector>
34 #include <cstddef>
35 #include <memory>
36 
38 #include "CDPL/Vis/Pen.hpp"
39 #include "CDPL/Vis/Font.hpp"
40 #include "CDPL/Math/Vector.hpp"
41 
42 
43 namespace rapidxml
44 {
45 
46  template <typename C> class xml_node;
47 }
48 
49 
50 namespace CDPL
51 {
52 
53  namespace Vis
54  {
55 
69  {
70 
71  public:
75  typedef std::shared_ptr<TextBlockPrimitive2D> SharedPointer;
76 
81 
87  void render(Renderer2D& renderer) const;
88 
145  void setText(const std::string& text);
146 
150  void clearText();
151 
156  const std::string& getText() const;
157 
162  bool hasText() const;
163 
171  void setPosition(const Math::Vector2D& pos);
172 
181  void setPosition(double x, double y);
182 
188  const Math::Vector2D& getPosition() const;
189 
194  void setPen(const Pen& pen);
195 
200  const Pen& getPen() const;
201 
207  void setFont(const Font& font);
208 
213  const Font& getFont() const;
214 
222  void setAlignment(unsigned int alignment);
223 
231  unsigned int getAlignment() const;
232 
241  void setLineSpacing(double spacing);
242 
247  double getLineSpacing() const;
248 
255  void layout(FontMetrics& font_metrics);
256 
262 
268  void getBounds(Rectangle2D& bounds, FontMetrics* font_metrics) const;
269 
270  private:
271  struct Style
272  {
273 
274  bool bold{false};
275  bool italic{false};
276  bool underlined{false};
277  bool overlined{false};
278  bool strikedOut{false};
279  bool subscripted{false};
280  bool superscripted{false};
281  };
282 
283  struct TextFragment
284  {
285 
286  TextFragment(std::string& text, const Style& style, const Color& color,
287  bool def_color, std::size_t line):
288  style(style), color(color), defColor(def_color), line(line)
289  {
290  this->text.swap(text);
291  }
292 
293  std::string text;
294  Style style;
295  Color color;
296  bool defColor;
297  std::size_t line;
298  double xPos;
299  double yPos;
300  };
301 
302  typedef rapidxml::xml_node<char> XMLNode;
303  typedef std::vector<Color> ColorStack;
304  typedef std::vector<Style> StyleStack;
305  typedef std::vector<TextFragment> TextFragmentList;
306 
307  void processText(const std::string& text);
308  void processNode(XMLNode* node);
309  void getColor(XMLNode* node);
310  void applyStyle(Font& font, const Style& style) const;
311 
312  Pen pen;
313  Font font;
314  Math::Vector2D position;
315  std::string text;
316  unsigned int alignment;
317  double lineSpacing;
318  ColorStack colorStack;
319  StyleStack styleStack;
320  Color currColor;
321  Style currStyle;
322  std::size_t currLine;
323  TextFragmentList textFragments;
324  };
325  } // namespace Vis
326 } // namespace CDPL
327 
328 #endif // CDPL_VIS_TEXTBLOCKPRIMITIVE2D_HPP
Definition of class CDPL::Vis::Font.
Definition of class CDPL::Vis::GraphicsPrimitive2D.
Definition of class CDPL::Vis::Pen.
Definition of vector data types.
#define CDPL_VIS_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
Interface providing methods for querying information about the metrics of a font.
Definition: FontMetrics.hpp:71
Specifies a font for drawing text.
Definition: Font.hpp:54
The abstract base class of all 2D graphics primitives.
Definition: GraphicsPrimitive2D.hpp:57
std::shared_ptr< GraphicsPrimitive2D > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated GraphicsPrimitive2D instances.
Definition: GraphicsPrimitive2D.hpp:63
Specifies how to draw lines and outlines of shapes.
Definition: Pen.hpp:53
Specifies an axis aligned rectangular area in 2D space.
Definition: Rectangle2D.hpp:51
Interface providing methods for low level 2D drawing operations.
Definition: Renderer2D.hpp:86
2D graphics primitive representing a block of styled text.
Definition: TextBlockPrimitive2D.hpp:69
const Pen & getPen() const
Returns the pen defining the default text color.
void setText(const std::string &text)
Specifies the block's text content.
TextBlockPrimitive2D()
Construct an empty text block primitive at position (0, 0).
const Math::Vector2D & getPosition() const
Returns the position of the text block.
bool hasText() const
Tells whether the text block has any visible text (includes whitespace).
double getLineSpacing() const
Returns the active line spacing value.
std::shared_ptr< TextBlockPrimitive2D > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated TextBlockPrimitive2D instances.
Definition: TextBlockPrimitive2D.hpp:75
void render(Renderer2D &renderer) const
Renders the text block via renderer using the configured font and styling.
void clearText()
Clears the block's text content.
GraphicsPrimitive2D::SharedPointer clone() const
Creates a dynamically allocated copy of this primitive.
void setPosition(double x, double y)
Sets the position of the text block to (x, y).
void setLineSpacing(double spacing)
Sets the line spacing value.
void setAlignment(unsigned int alignment)
Specifies the way lines should be horizontally aligned.
void layout(FontMetrics &font_metrics)
Performs a layout of the text for the currently specified font using the provided Vis::FontMetrics in...
unsigned int getAlignment() const
Returns the active horizontal text alignment specification.
void setFont(const Font &font)
Sets the font used for text rendering.
const Font & getFont() const
Returns the font used for text rendering.
void getBounds(Rectangle2D &bounds, FontMetrics *font_metrics) const
Computes the axis-aligned bounding rectangle of the text block.
const std::string & getText() const
Returns the block's text content.
void setPen(const Pen &pen)
Sets the pen defining the default text color.
void setPosition(const Math::Vector2D &pos)
Sets the position of the text block in 2D space.
CVector< double, 2 > Vector2D
Bounded 2 element vector holding floating-point values of type double.
Definition: Vector.hpp:3213
CDPL_VIS_API const Color & getColor(const Chem::Atom &atom)
Returns the value of the Vis::AtomProperty::COLOR property of the atom atom.
The namespace of the Chemical Data Processing Library.