RDKit
Open-source cheminformatics and machine learning.
MolDraw2DCairo.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2015 Greg Landrum
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 // derived from Dave Cosgrove's MolDraw2D
11 //
12 // This is a concrete class derived from MolDraw2D that uses RDKit to draw a
13 // molecule into a cairo drawing context
14 
15 #include <RDGeneral/export.h>
16 #ifndef MOLDRAW2DCAIRO_H
17 #define MOLDRAW2DCAIRO_H
18 
19 #include <cairo.h>
20 
23 
24 // ****************************************************************************
25 
26 namespace RDKit {
27 
29  public:
30  // does not take ownership of the drawing context
31  MolDraw2DCairo(int width, int height, cairo_t *cr, int panelWidth = -1,
32  int panelHeight = -1, bool noFreetype = false)
33  : MolDraw2D(width, height, panelWidth, panelHeight), dp_cr(cr) {
34  cairo_reference(dp_cr);
35  initDrawing();
36  initTextDrawer(noFreetype);
37  };
38  MolDraw2DCairo(int width, int height, int panelWidth = -1,
39  int panelHeight = -1, bool noFreetype = false)
40  : MolDraw2D(width, height, panelWidth, panelHeight) {
41  cairo_surface_t *surf =
42  cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
43  dp_cr = cairo_create(surf);
44  cairo_surface_destroy(surf); // dp_cr has a reference to this now;
45  initDrawing();
46  initTextDrawer(noFreetype);
47  };
49  if (dp_cr) {
50  if (cairo_get_reference_count(dp_cr) > 0) {
51  cairo_destroy(dp_cr);
52  }
53  dp_cr = nullptr;
54  }
55  }
56 
57  void setColour(const DrawColour &col) override;
58 
59  // not sure if this goes here or if we should do a dtor since initDrawing() is
60  // called in the ctor,
61  // but we'll start here
62  void finishDrawing();
63 
64  void drawLine(const Point2D &cds1, const Point2D &cds2) override;
65  // void drawString( const std::string &str, const Point2D &cds );
66  void drawPolygon(const std::vector<Point2D> &cds) override;
67  void clearDrawing() override;
68 
69  void drawWavyLine(const Point2D &cds1, const Point2D &cds2,
70  const DrawColour &col1, const DrawColour &col2,
71  unsigned int nSegments = 16,
72  double vertOffset = 0.05) override;
73 
74  // returns the PNG data in a string
75  std::string getDrawingText() const;
76  // writes the PNG data to a file
77  void writeDrawingText(const std::string &fName) const;
78 
79 #if defined(WIN32) && !defined(RDK_BUILD_FREETYPE_SUPPORT)
80  bool supportsAnnotations() override { return false; }
81 #endif
82 
83  private:
84  cairo_t *dp_cr;
85 
86  void updateMetadata(const ROMol &mol, int confId) override;
87  void updateMetadata(const ChemicalReaction &rxn) override;
88 
89  void initDrawing() override;
90  void initTextDrawer(bool noFreetype) override;
91  std::string addMetadataToPNG(const std::string &png) const;
92  void updateMetadata(const ROMol &mol) const;
93  void updateMetadata(const ChemicalReaction &rxn) const;
94 };
95 } // namespace RDKit
96 #endif // MOLDRAW2DCAIRO_H
This is a class for storing and applying general chemical reactions.
Definition: Reaction.h:119
void drawPolygon(const std::vector< Point2D > &cds) override
void setColour(const DrawColour &col) override
sets the current draw color
void drawWavyLine(const Point2D &cds1, const Point2D &cds2, const DrawColour &col1, const DrawColour &col2, unsigned int nSegments=16, double vertOffset=0.05) override
draw a wavy line like that used to indicate unknown stereochemistry
void clearDrawing() override
clears the contents of the drawing
void drawLine(const Point2D &cds1, const Point2D &cds2) override
draws a line from cds1 to cds2 using the current drawing style
void writeDrawingText(const std::string &fName) const
MolDraw2DCairo(int width, int height, int panelWidth=-1, int panelHeight=-1, bool noFreetype=false)
std::string getDrawingText() const
MolDraw2DCairo(int width, int height, cairo_t *cr, int panelWidth=-1, int panelHeight=-1, bool noFreetype=false)
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition: MolDraw2D.h:281
#define RDKIT_MOLDRAW2D_EXPORT
Definition: export.h:437
Std stuff.
Definition: Abbreviations.h:17