RDKit: DrawText.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
DrawText.h
Go to the documentation of this file.
1//
2// Copyright (C) 2020-2022 David Cosgrove and other RDKit contributors
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10//
11// Original author: David Cosgrove (CozChemIx).
12//
13// This is an abstract base class for drawing text into a MolDraw2D
14// object.
15
16#ifndef RDKIT_DRAWTEXT_H
17#define RDKIT_DRAWTEXT_H
18
19#include <string>
20#include <vector>
21
22#include <RDGeneral/export.h>
23#include <Geometry/point.h>
24#include <GraphMol/MolDraw2D/MolDraw2DHelpers.h>
25#include <GraphMol/MolDraw2D/StringRect.h>
26
27using RDGeom::Point2D;
28
29namespace RDKit {
30class MolDraw2D;
31namespace MolDraw2D_detail {
32
33// for aligning the drawing of text to the passed in coords.
34 enum class TextDrawType : unsigned char {
35 TextDrawNormal = 0,
36 TextDrawSuperscript,
37 TextDrawSubscript
38};
39std::ostream &operator<<(std::ostream &oss, const OrientType &o);
40std::ostream &operator<<(std::ostream &oss, const TextAlignType &tat);
41std::ostream &operator<<(std::ostream &oss, const TextDrawType &tdt);
42
43// ****************************************************************************
44// This is an implementation class, not intended to be used by the great
45// unwashed. If you want to draw a string used MolDraw2D::drawString().
46 class RDKIT_MOLDRAW2D_EXPORT DrawText {
47 public:
48 virtual ~DrawText() = 0;
49
50 static constexpr double DEFAULT_FONT_SCALE =
51 0.6; // seems to be a good number
52
53 DrawText(double max_fnt_sz, double min_fnt_sz);
54 DrawText(const DrawText &) = delete;
55 DrawText(DrawText &&) = delete;
56 DrawText &operator=(const DrawText &) = delete;
57 DrawText &operator=(DrawText &&) = delete;
58
59 DrawColour const &colour() const;
60 void setColour(const DrawColour &col);
61
62 // size in "pixels" i.e scale() * base_font_size_.
63 double fontSize() const;
64 void setFontSize(double new_size);
65 double baseFontSize() const;
66 void setBaseFontSize(double new_size);
67 double maxFontSize() const;
68 void setMaxFontSize(double new_max);
69 double minFontSize() const;
70 void setMinFontSize(double new_max);
71 double fontScale() const;
72 // returns false if min or max font size is hit, true otherwise.
73 // ignoreLimits ignores minFontSize and maxFontSize.
74 bool setFontScale(double new_scale, bool ignoreLimits = false);
75
76 // these are only relevant for the FreeType DrawText classes.
77 virtual std::string getFontFile() const { return ""; }
78 virtual void setFontFile(const std::string &font_file) {
79 RDUNUSED_PARAM(font_file);
80 }
81
82 //! using the current scale, work out the size of the label
83 /*!
84 Bear in mind when implementing this, that, for example, NH2 will appear as
85 NH<sub>2</sub> to convey that the 2 is a subscript, and this needs to
86 be accounted for in the width and height.
87 */
88 virtual void getStringSize(const std::string &label, double &label_width,
89 double &label_height) const;
90 // returns the extremes of the label, in draw (pixel) coords. dontSplit
91 // true suppresses the call to atomLabelToPieces.
92 void getStringExtremes(const std::string &label, OrientType orient,
93 double &x_min, double &y_min, double &x_max,
94 double &y_max, bool dontSplit = false) const;
95 // get the rectangles that go round each character of the string. If
96 // dontSplit is false, it assumes it's an atom label and splits it
97 // in a sensible way for that. If the OrientType is C, it applies
98 // textAlign, otherwise the text is aligned set appropriately for the
99 // OrientType.
100 void getStringRects(const std::string &text, OrientType orient,
101 std::vector<std::shared_ptr<StringRect>> &rects,
102 std::vector<TextDrawType> &draw_modes,
103 std::vector<char> &draw_chars, bool dontSplit = false,
104 TextAlignType textAlign = TextAlignType::MIDDLE) const;
105
106 //! drawString centres the string on cds.
107 virtual void drawString(const std::string &str, const Point2D &cds,
108 TextAlignType align);
109 // Aligns them according to OrientType. This version assumes it's an
110 // atomic symbol and calls atomLabelToPieces. This will behave
111 // badly with general text. Surround the string with <lit></lit>
112 // if that's an issue, or use the version above.
113 void drawString(const std::string &label, const Point2D &cds,
114 OrientType orient);
115
116 // put the label on end2, and then move end2 so that it is at
117 // the intersection of a string rectangle and the line from end1 to
118 // end2, if there is an intersection. Mostly for trimming bonds
119 // back from atom labels. end1 and end2 in draw coords.
120 void adjustLineForString(const std::string &label, OrientType orient,
121 const Point2D &end1, Point2D &end2) const;
122
123 // draw the char, with the bottom left hand corner at cds
124 virtual void drawChar(char c, const Point2D &cds) = 0;
125
126 // puts a colourful rectangle around each character in the string.
127 // For debugging, mostly.
128 void drawStringRects(const std::string &label, OrientType orient,
129 TextAlignType talign, const Point2D &cds,
130 MolDraw2D &mol_draw) const;
131
132 // cds in draw coords
133 // does the label at cds intersect the given StringRect.
134 bool doesRectIntersect(const std::string &label, OrientType orient,
135 const Point2D &cds, const StringRect &rect) const;
136 // does the vector of StringRects, each translated by cds, intersect the
137 // given StringRect.
138 bool doesRectIntersect(const std::vector<std::shared_ptr<StringRect>> &rects,
139 const Point2D &cds, const StringRect &rect) const;
140 bool doesLineIntersect(const std::string &label, OrientType orient,
141 const Point2D &cds, const Point2D &end1,
142 const Point2D &end2, double padding) const;
143 bool doesLineIntersect(const std::vector<std::shared_ptr<StringRect>> &rects,
144 const Point2D &cds, const Point2D &end1,
145 const Point2D &end2, double padding) const;
146 bool doesStringIntersect(
147 const std::vector<std::shared_ptr<StringRect>> &rects,
148 const Point2D &cds1, const std::string &label2, OrientType orient2,
149 const Point2D &cds2) const;
150 bool doesStringIntersect(const std::string &label1, OrientType orient1,
151 const Point2D &cds1, const std::string &label2,
152 OrientType orient2, const Point2D &cds2) const;
153
154 virtual void alignString(
155 TextAlignType align, const std::vector<TextDrawType> &draw_modes,
156 std::vector<std::shared_ptr<StringRect>> &rects) const;
157 // adjust the string rectangles up and down for super- and subscripts
158 void adjustStringRectsForSuperSubScript(
159 const std::vector<TextDrawType> &draw_modes,
160 std::vector<std::shared_ptr<StringRect>> &rects) const;
161 // return a scale factor appropriate for the character and draw type
162 // (normal or super- or subscript)
163 double selectScaleFactor(char c, TextDrawType draw_type) const;
164
165 // amount to scale subscripts and superscripts by
166 constexpr static double SUBS_SCALE = 0.66;
167 constexpr static double SUPER_SCALE = 0.66;
168
169 DrawColour colour_;
170 double font_scale_;
171 double max_font_size_;
172 double min_font_size_;
173 double base_font_size_ = DEFAULT_FONT_SCALE;
174
175 // return a vector of StringRects, one for each char in text, with
176 // super- and subscripts taken into account. Sizes in pixel coords,
177 // i.e. scaled by fontScale().
178 virtual void getStringRects(const std::string &text,
179 std::vector<std::shared_ptr<StringRect>> &rects,
180 std::vector<TextDrawType> &draw_modes,
181 std::vector<char> &draw_chars) const = 0;
182 void drawChars(const Point2D &a_cds,
183 const std::vector<std::shared_ptr<StringRect>> &rects,
184 const std::vector<TextDrawType> &draw_modes,
185 const std::vector<char> &draw_chars);
186};
187
188//! establishes whether to put string draw mode into super- or sub-script
189//! mode based on contents of instring from i onwards. Increments i
190//! appropriately
191//! \returns true or false depending on whether it did something or not
192 RDKIT_MOLDRAW2D_EXPORT bool setStringDrawMode(const std::string &instring,
193 TextDrawType &draw_mode,
194 size_t &i);
195
196// take the label for the given atom and return the individual pieces
197// that need to be drawn for it. So NH<sub>2</sub> will return
198// "N", "H<sub>2</sub>".
199std::vector<std::string> atomLabelToPieces(const std::string &label,
200 OrientType orient);
201
202} // namespace MolDraw2D_detail
203} // namespace RDKit
204
205#endif // RDKIT_DRAWTEXT_H
RDUNUSED_PARAM
#define RDUNUSED_PARAM(x)
Definition Invariant.h:196
MolDraw2DHelpers.h
StringRect.h
RDGeom::Point2D
Definition point.h:284
RDKit::MolDraw2D_detail::DrawText::alignString
virtual void alignString(TextAlignType align, const std::vector< TextDrawType > &draw_modes, std::vector< std::shared_ptr< StringRect > > &rects) const
RDKit::MolDraw2D_detail::DrawText::doesLineIntersect
bool doesLineIntersect(const std::vector< std::shared_ptr< StringRect > > &rects, const Point2D &cds, const Point2D &end1, const Point2D &end2, double padding) const
RDKit::MolDraw2D_detail::DrawText::DrawText
DrawText(DrawText &&)=delete
RDKit::MolDraw2D_detail::DrawText::getStringRects
void getStringRects(const std::string &text, OrientType orient, std::vector< std::shared_ptr< StringRect > > &rects, std::vector< TextDrawType > &draw_modes, std::vector< char > &draw_chars, bool dontSplit=false, TextAlignType textAlign=TextAlignType::MIDDLE) const
RDKit::MolDraw2D_detail::DrawText::doesRectIntersect
bool doesRectIntersect(const std::vector< std::shared_ptr< StringRect > > &rects, const Point2D &cds, const StringRect &rect) const
RDKit::MolDraw2D_detail::DrawText::fontScale
double fontScale() const
RDKit::MolDraw2D_detail::DrawText::fontSize
double fontSize() const
RDKit::MolDraw2D_detail::DrawText::DrawText
DrawText(const DrawText &)=delete
RDKit::MolDraw2D_detail::DrawText::max_font_size_
double max_font_size_
Definition DrawText.h:171
RDKit::MolDraw2D_detail::DrawText::min_font_size_
double min_font_size_
Definition DrawText.h:172
RDKit::MolDraw2D_detail::DrawText::setFontScale
bool setFontScale(double new_scale, bool ignoreLimits=false)
RDKit::MolDraw2D_detail::DrawText::minFontSize
double minFontSize() const
RDKit::MolDraw2D_detail::DrawText::~DrawText
virtual ~DrawText()=0
RDKit::MolDraw2D_detail::DrawText::colour_
DrawColour colour_
Definition DrawText.h:169
RDKit::MolDraw2D_detail::DrawText::SUPER_SCALE
static constexpr double SUPER_SCALE
Definition DrawText.h:167
RDKit::MolDraw2D_detail::DrawText::setBaseFontSize
void setBaseFontSize(double new_size)
RDKit::MolDraw2D_detail::DrawText::DEFAULT_FONT_SCALE
static constexpr double DEFAULT_FONT_SCALE
Definition DrawText.h:50
RDKit::MolDraw2D_detail::DrawText::selectScaleFactor
double selectScaleFactor(char c, TextDrawType draw_type) const
RDKit::MolDraw2D_detail::DrawText::doesLineIntersect
bool doesLineIntersect(const std::string &label, OrientType orient, const Point2D &cds, const Point2D &end1, const Point2D &end2, double padding) const
RDKit::MolDraw2D_detail::DrawText::adjustLineForString
void adjustLineForString(const std::string &label, OrientType orient, const Point2D &end1, Point2D &end2) const
RDKit::MolDraw2D_detail::DrawText::getStringSize
virtual void getStringSize(const std::string &label, double &label_width, double &label_height) const
using the current scale, work out the size of the label
RDKit::MolDraw2D_detail::DrawText::font_scale_
double font_scale_
Definition DrawText.h:170
RDKit::MolDraw2D_detail::DrawText::baseFontSize
double baseFontSize() const
RDKit::MolDraw2D_detail::DrawText::getFontFile
virtual std::string getFontFile() const
Definition DrawText.h:77
RDKit::MolDraw2D_detail::DrawText::setFontSize
void setFontSize(double new_size)
RDKit::MolDraw2D_detail::DrawText::doesRectIntersect
bool doesRectIntersect(const std::string &label, OrientType orient, const Point2D &cds, const StringRect &rect) const
RDKit::MolDraw2D_detail::DrawText::getStringRects
virtual void getStringRects(const std::string &text, std::vector< std::shared_ptr< StringRect > > &rects, std::vector< TextDrawType > &draw_modes, std::vector< char > &draw_chars) const =0
RDKit::MolDraw2D_detail::DrawText::doesStringIntersect
bool doesStringIntersect(const std::vector< std::shared_ptr< StringRect > > &rects, const Point2D &cds1, const std::string &label2, OrientType orient2, const Point2D &cds2) const
RDKit::MolDraw2D_detail::DrawText::DrawText
DrawText(double max_fnt_sz, double min_fnt_sz)
RDKit::MolDraw2D_detail::DrawText::drawChars
void drawChars(const Point2D &a_cds, const std::vector< std::shared_ptr< StringRect > > &rects, const std::vector< TextDrawType > &draw_modes, const std::vector< char > &draw_chars)
RDKit::MolDraw2D_detail::DrawText::drawChar
virtual void drawChar(char c, const Point2D &cds)=0
RDKit::MolDraw2D_detail::DrawText::drawString
virtual void drawString(const std::string &str, const Point2D &cds, TextAlignType align)
drawString centres the string on cds.
RDKit::MolDraw2D_detail::DrawText::doesStringIntersect
bool doesStringIntersect(const std::string &label1, OrientType orient1, const Point2D &cds1, const std::string &label2, OrientType orient2, const Point2D &cds2) const
RDKit::MolDraw2D_detail::DrawText::setFontFile
virtual void setFontFile(const std::string &font_file)
Definition DrawText.h:78
RDKit::MolDraw2D_detail::DrawText::adjustStringRectsForSuperSubScript
void adjustStringRectsForSuperSubScript(const std::vector< TextDrawType > &draw_modes, std::vector< std::shared_ptr< StringRect > > &rects) const
RDKit::MolDraw2D_detail::DrawText::maxFontSize
double maxFontSize() const
RDKit::MolDraw2D_detail::DrawText::drawStringRects
void drawStringRects(const std::string &label, OrientType orient, TextAlignType talign, const Point2D &cds, MolDraw2D &mol_draw) const
RDKit::MolDraw2D_detail::DrawText::colour
DrawColour const & colour() const
RDKit::MolDraw2D_detail::DrawText::base_font_size_
double base_font_size_
Definition DrawText.h:173
RDKit::MolDraw2D_detail::DrawText::setColour
void setColour(const DrawColour &col)
RDKit::MolDraw2D_detail::DrawText::setMinFontSize
void setMinFontSize(double new_max)
RDKit::MolDraw2D_detail::DrawText::drawString
void drawString(const std::string &label, const Point2D &cds, OrientType orient)
RDKit::MolDraw2D_detail::DrawText::operator=
DrawText & operator=(const DrawText &)=delete
RDKit::MolDraw2D_detail::DrawText::operator=
DrawText & operator=(DrawText &&)=delete
RDKit::MolDraw2D_detail::DrawText::getStringExtremes
void getStringExtremes(const std::string &label, OrientType orient, double &x_min, double &y_min, double &x_max, double &y_max, bool dontSplit=false) const
RDKit::MolDraw2D_detail::DrawText::SUBS_SCALE
static constexpr double SUBS_SCALE
Definition DrawText.h:166
RDKit::MolDraw2D_detail::DrawText::setMaxFontSize
void setMaxFontSize(double new_max)
RDKit::MolDraw2D
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition MolDraw2D.h:47
export.h
RDKIT_MOLDRAW2D_EXPORT
#define RDKIT_MOLDRAW2D_EXPORT
Definition export.h:313
RDKit::MolDraw2D_detail
Definition AtomSymbol.h:30
RDKit::MolDraw2D_detail::atomLabelToPieces
std::vector< std::string > atomLabelToPieces(const std::string &label, OrientType orient)
RDKit::MolDraw2D_detail::TextDrawType
TextDrawType
Definition DrawText.h:34
RDKit::MolDraw2D_detail::TextDrawType::TextDrawSubscript
@ TextDrawSubscript
Definition DrawText.h:37
RDKit::MolDraw2D_detail::TextDrawType::TextDrawSuperscript
@ TextDrawSuperscript
Definition DrawText.h:36
RDKit::MolDraw2D_detail::TextDrawType::TextDrawNormal
@ TextDrawNormal
Definition DrawText.h:35
RDKit::MolDraw2D_detail::setStringDrawMode
RDKIT_MOLDRAW2D_EXPORT bool setStringDrawMode(const std::string &instring, TextDrawType &draw_mode, size_t &i)
RDKit::MolDraw2D_detail::OrientType
OrientType
Definition MolDraw2DHelpers.h:26
RDKit::MolDraw2D_detail::operator<<
std::ostream & operator<<(std::ostream &oss, const OrientType &o)
RDKit::MolDraw2D_detail::TextAlignType
TextAlignType
Definition MolDraw2DHelpers.h:33
RDKit::MolDraw2D_detail::TextAlignType::MIDDLE
@ MIDDLE
Definition MolDraw2DHelpers.h:34
RDKit
Std stuff.
Definition Abbreviations.h:19
point.h
RDKit::DrawColour
Definition MolDraw2DHelpers.h:40
RDKit::MolDraw2D_detail::StringRect
Definition StringRect.h:24