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 
20 #include <cairo.h>
21 
22 // ****************************************************************************
23 
24 namespace RDKit {
25 
27  public:
28  // does not take ownership of the drawing context
29  MolDraw2DCairo(int width, int height, cairo_t *cr, int panelWidth = -1,
30  int panelHeight = -1)
31  : MolDraw2D(width, height, panelWidth, panelHeight), dp_cr(cr) {
32  cairo_reference(dp_cr);
33  initDrawing();
34  };
35  MolDraw2DCairo(int width, int height, int panelWidth = -1,
36  int panelHeight = -1)
37  : MolDraw2D(width, height, panelWidth, panelHeight) {
38  cairo_surface_t *surf =
39  cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
40  dp_cr = cairo_create(surf);
41  cairo_surface_destroy(surf); // dp_cr has a reference to this now;
42  initDrawing();
43  };
45  if (dp_cr) {
46  if (cairo_get_reference_count(dp_cr) > 0) {
47  cairo_destroy(dp_cr);
48  }
49  dp_cr = NULL;
50  }
51  }
52 
53  // set font size in molecule coordinate units. That's probably Angstrom for
54  // RDKit. It will turned into drawing units using scale_, which might be
55  // changed as a result, to make sure things still appear in the window.
56  void setFontSize(double new_size);
57  void setColour(const DrawColour &col);
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);
65  void drawChar(char c, const Point2D &cds);
66  // void drawString( const std::string &str, const Point2D &cds );
67  void drawPolygon(const std::vector<Point2D> &cds);
68  void clearDrawing();
69 
70  void drawWavyLine(const Point2D &cds1, const Point2D &cds2,
71  const DrawColour &col1, const DrawColour &col2,
72  unsigned int nSegments = 16, double vertOffset = 0.05);
73 
74  // using the current scale, work out the size of the label in molecule
75  // coordinates
76  void getStringSize(const std::string &label, double &label_width,
77  double &label_height) const;
78 
79  // returns the PNG data in a string
80  std::string getDrawingText() const;
81  // writes the PNG data to a file
82  void writeDrawingText(const std::string &fName) const;
83 
84  private:
85  cairo_t *dp_cr;
86 
87  void initDrawing();
88 };
89 }
90 #endif // MOLDRAW2DCAIRO_H
MolDraw2DCairo(int width, int height, int panelWidth=-1, int panelHeight=-1)
MolDraw2DCairo(int width, int height, cairo_t *cr, int panelWidth=-1, int panelHeight=-1)
Std stuff.
Definition: Atom.h:30
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition: MolDraw2D.h:133
#define RDKIT_MOLDRAW2D_EXPORT
Definition: export.h:385
boost::tuple< float, float, float > DrawColour
Definition: MolDraw2D.h:39