RDKit
Open-source cheminformatics and machine learning.
DrawTextFT.h
Go to the documentation of this file.
1 //
2 // @@ All Rights Reserved @@
3 // This file is part of the RDKit.
4 // The contents are covered by the terms of the BSD license
5 // which is included in the file license.txt, found at the root
6 // of the RDKit source tree.
7 //
8 //
9 // Original author: David Cosgrove (CozChemIx) on 06/05/2020.
10 //
11 // This is an abstract base class derived from DrawText that does drawing
12 // using FreeType.
13 
14 #ifndef RDKIT_DRAWTEXTFT_H
15 #define RDKIT_DRAWTEXTFT_H
16 
17 #include <string>
18 
19 #include <ft2build.h>
20 #include FT_FREETYPE_H
21 #include FT_BBOX_H
22 #include FT_OUTLINE_H
23 
24 #include <RDGeneral/export.h>
26 
27 namespace RDKit {
28 
29 struct StringRect;
30 
31 // ****************************************************************************
33  public:
34  DrawTextFT(double max_fnt_sz, double min_fnt_sz,
35  const std::string &font_file);
36  ~DrawTextFT() override;
37 
38  void drawChar(char c, const Point2D &cds) override;
39 
40  virtual int MoveToFunctionImpl(const FT_Vector *to) = 0;
41  virtual int LineToFunctionImpl(const FT_Vector *to) = 0;
42  virtual int ConicToFunctionImpl(const FT_Vector *control,
43  const FT_Vector *to) = 0;
44  virtual int CubicToFunctionImpl(const FT_Vector *controlOne,
45  const FT_Vector *controlTwo,
46  const FT_Vector *to) = 0;
47 
48  // unless over-ridden by the c'tor, this will return a hard-coded
49  // file from $RDBASE.
50  std::string getFontFile() const override;
51  void setFontFile(const std::string &font_file) override;
52 
53  protected:
54  double fontCoordToDrawCoord(FT_Pos fc) const;
55  void fontPosToDrawPos(FT_Pos fx, FT_Pos fy, double &dx, double &dy) const;
56  // adds x_trans_ and y_trans_ to coords returns x advance distance
57  virtual double extractOutline();
58 
59  private:
60  FT_Library library_;
61  FT_Face face_;
62  std::string font_file_; // over-rides default if not empty.
63  double x_trans_, y_trans_;
64  mutable FT_Pos
65  string_y_max_; // maximum y value of string drawn, for inverting y
66  double em_scale_;
67 
68  // return a vector of StringRects, one for each char in text, with
69  // super- and subscripts taken into account. Sizes in pixel coords,
70  // i.e. scaled by fontScale().
71  void getStringRects(const std::string &text,
72  std::vector<std::shared_ptr<StringRect>> &rects,
73  std::vector<TextDrawType> &draw_modes,
74  std::vector<char> &draw_chars) const override;
75 
76  // calculate the bounding box of the glyph for c in
77  // font units (0 -> face_->units_per_EM (2048 for roboto font).
78  void calcGlyphBBox(char c, FT_Pos &x_min, FT_Pos &y_min, FT_Pos &x_max,
79  FT_Pos &y_max, FT_Pos &advance) const;
80 };
81 
82 // Callbacks for FT_Outline_Decompose. user should be a pointer to
83 // an instance of DrawTextFT.
84 int moveToFunction(const FT_Vector *to, void *user);
85 int lineToFunction(const FT_Vector *to, void *user);
86 int conicToFunction(const FT_Vector *control, const FT_Vector *to, void *user);
87 int cubicToFunction(const FT_Vector *controlOne, const FT_Vector *controlTwo,
88  const FT_Vector *to, void *user);
89 
90 } // namespace RDKit
91 
92 #endif // RDKIT_DRAWTEXTFT_H
virtual int LineToFunctionImpl(const FT_Vector *to)=0
virtual double extractOutline()
DrawTextFT(double max_fnt_sz, double min_fnt_sz, const std::string &font_file)
void drawChar(char c, const Point2D &cds) override
~DrawTextFT() override
virtual int CubicToFunctionImpl(const FT_Vector *controlOne, const FT_Vector *controlTwo, const FT_Vector *to)=0
virtual int MoveToFunctionImpl(const FT_Vector *to)=0
void setFontFile(const std::string &font_file) override
double fontCoordToDrawCoord(FT_Pos fc) const
std::string getFontFile() const override
void fontPosToDrawPos(FT_Pos fx, FT_Pos fy, double &dx, double &dy) const
virtual int ConicToFunctionImpl(const FT_Vector *control, const FT_Vector *to)=0
#define RDKIT_MOLDRAW2D_EXPORT
Definition: export.h:265
Std stuff.
Definition: Abbreviations.h:18
int conicToFunction(const FT_Vector *control, const FT_Vector *to, void *user)
int cubicToFunction(const FT_Vector *controlOne, const FT_Vector *controlTwo, const FT_Vector *to, void *user)
int lineToFunction(const FT_Vector *to, void *user)
int moveToFunction(const FT_Vector *to, void *user)