RDKit
Open-source cheminformatics and machine learning.
DrawTextFT.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 derived from DrawText that does drawing
14 // using FreeType.
15 
16 #ifndef RDKIT_DRAWTEXTFT_H
17 #define RDKIT_DRAWTEXTFT_H
18 
19 #include <string>
20 
21 #include <ft2build.h>
22 #include FT_FREETYPE_H
23 #include FT_BBOX_H
24 #include FT_OUTLINE_H
25 
26 #include <RDGeneral/export.h>
28 
29 namespace RDKit {
30 
31 struct StringRect;
32 
33 namespace MolDraw2D_detail {
34 
35 // ****************************************************************************
37  public:
38  virtual ~DrawTextFT() override;
39  virtual int MoveToFunctionImpl(const FT_Vector *to) = 0;
40  virtual int LineToFunctionImpl(const FT_Vector *to) = 0;
41  virtual int ConicToFunctionImpl(const FT_Vector *control,
42  const FT_Vector *to) = 0;
43  virtual int CubicToFunctionImpl(const FT_Vector *controlOne,
44  const FT_Vector *controlTwo,
45  const FT_Vector *to) = 0;
46 
47  DrawTextFT(double max_fnt_sz, double min_fnt_sz,
48  const std::string &font_file);
49  DrawTextFT(const DrawTextFT &) = delete;
50  DrawTextFT(DrawTextFT &&) = delete;
51  DrawTextFT &operator=(const DrawTextFT &) = delete;
53 
54  void drawChar(char c, const Point2D &cds) override;
55 
56  // unless over-ridden by the c'tor, this will return a hard-coded
57  // file from $RDBASE.
58  std::string getFontFile() const override;
59  void setFontFile(const std::string &font_file) override;
60 
61  double fontCoordToDrawCoord(FT_Pos fc) const;
62  void fontPosToDrawPos(FT_Pos fx, FT_Pos fy, double &dx, double &dy) const;
63  // adds x_trans_ and y_trans_ to coords returns x advance distance
64  virtual double extractOutline();
65 
66  FT_Library library_;
67  FT_Face face_;
68  std::string font_file_; // over-rides default if not empty.
69  double x_trans_, y_trans_;
70  mutable FT_Pos
71  string_y_max_; // maximum y value of string drawn, for inverting y
72  double em_scale_;
73 
74  // return a vector of StringRects, one for each char in text, with
75  // super- and subscripts taken into account. Sizes in pixel coords,
76  // i.e. scaled by fontScale().
77  void getStringRects(const std::string &text,
78  std::vector<std::shared_ptr<StringRect>> &rects,
79  std::vector<TextDrawType> &draw_modes,
80  std::vector<char> &draw_chars) const override;
81 
82  // calculate the bounding box of the glyph for c in
83  // font units (0 -> face_->units_per_EM (2048 for roboto font).
84  void calcGlyphBBox(char c, FT_Pos &x_min, FT_Pos &y_min, FT_Pos &x_max,
85  FT_Pos &y_max, FT_Pos &advance) const;
86 };
87 
88 // Callbacks for FT_Outline_Decompose. user should be a pointer to
89 // an instance of DrawTextFT.
90 int moveToFunction(const FT_Vector *to, void *user);
91 int lineToFunction(const FT_Vector *to, void *user);
92 int conicToFunction(const FT_Vector *control, const FT_Vector *to, void *user);
93 int cubicToFunction(const FT_Vector *controlOne, const FT_Vector *controlTwo,
94  const FT_Vector *to, void *user);
95 
96 } // namespace MolDraw2D_detail
97 } // namespace RDKit
98 
99 #endif // RDKIT_DRAWTEXTFT_H
void fontPosToDrawPos(FT_Pos fx, FT_Pos fy, double &dx, double &dy) const
DrawTextFT(DrawTextFT &&)=delete
virtual int ConicToFunctionImpl(const FT_Vector *control, const FT_Vector *to)=0
DrawTextFT(double max_fnt_sz, double min_fnt_sz, const std::string &font_file)
void setFontFile(const std::string &font_file) override
virtual int LineToFunctionImpl(const FT_Vector *to)=0
double fontCoordToDrawCoord(FT_Pos fc) const
DrawTextFT & operator=(const DrawTextFT &)=delete
virtual int MoveToFunctionImpl(const FT_Vector *to)=0
void calcGlyphBBox(char c, FT_Pos &x_min, FT_Pos &y_min, FT_Pos &x_max, FT_Pos &y_max, FT_Pos &advance) const
DrawTextFT(const DrawTextFT &)=delete
void drawChar(char c, const Point2D &cds) override
DrawTextFT & operator=(DrawTextFT &&)=delete
void getStringRects(const std::string &text, std::vector< std::shared_ptr< StringRect >> &rects, std::vector< TextDrawType > &draw_modes, std::vector< char > &draw_chars) const override
std::string getFontFile() const override
virtual int CubicToFunctionImpl(const FT_Vector *controlOne, const FT_Vector *controlTwo, const FT_Vector *to)=0
#define RDKIT_MOLDRAW2D_EXPORT
Definition: export.h:273
int moveToFunction(const FT_Vector *to, void *user)
int cubicToFunction(const FT_Vector *controlOne, const FT_Vector *controlTwo, const FT_Vector *to, void *user)
int conicToFunction(const FT_Vector *control, const FT_Vector *to, void *user)
int lineToFunction(const FT_Vector *to, void *user)
Std stuff.
Definition: Abbreviations.h:18