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 
25 
26 namespace RDKit {
27 
28 struct StringRect;
29 
30 // ****************************************************************************
31 class DrawTextFT : public DrawText {
32  public:
33  DrawTextFT(double max_fnt_sz, double min_fnt_sz,
34  const std::string &font_file);
36 
37  void drawChar(char c, const Point2D &cds) override;
38 
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  // unless over-ridden by the c'tor, this will return a hard-coded
48  // file from $RDBASE.
49  std::string getFontFile() const override;
50  void setFontFile(const std::string &font_file) override;
51 
52  protected:
53  double fontCoordToDrawCoord(FT_Pos fc) const;
54  void fontPosToDrawPos(FT_Pos fx, FT_Pos fy, double &dx, double &dy) const;
55  // adds x_trans_ and y_trans_ to coords returns x advance distance
56  virtual double extractOutline();
57 
58  private:
59  FT_Library library_;
60  FT_Face face_;
61  std::string font_file_; // over-rides default if not empty.
62  double x_trans_, y_trans_;
63  mutable FT_Pos
64  string_y_max_; // maximum y value of string drawn, for inverting y
65  double em_scale_;
66 
67  // return a vector of StringRects, one for each char in text, with
68  // super- and subscripts taken into account. Sizes in pixel coords,
69  // i.e. scaled by fontScale().
70  void getStringRects(const std::string &text,
71  std::vector<std::shared_ptr<StringRect>> &rects,
72  std::vector<TextDrawType> &draw_modes,
73  std::vector<char> &draw_chars) const override;
74 
75  // calculate the bounding box of the glyph for c in
76  // font units (0 -> face_->units_per_EM (2048 for roboto font).
77  void calcGlyphBBox(char c, FT_Pos &x_min, FT_Pos &y_min, FT_Pos &x_max,
78  FT_Pos &y_max, FT_Pos &advance) const;
79 };
80 
81 // Callbacks for FT_Outline_Decompose. user should be a pointer to
82 // an instance of DrawTextFT.
83 int moveToFunction(const FT_Vector *to, void *user);
84 int lineToFunction(const FT_Vector *to, void *user);
85 int conicToFunction(const FT_Vector *control, const FT_Vector *to, void *user);
86 int cubicToFunction(const FT_Vector *controlOne, const FT_Vector *controlTwo,
87  const FT_Vector *to, void *user);
88 
89 } // namespace RDKit
90 
91 #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
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
Std stuff.
Definition: Abbreviations.h:17
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)