RDKit
Open-source cheminformatics and machine learning.
MolDraw2DSVG.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 an SVG file
14 
15 #include <RDGeneral/export.h>
16 #ifndef MOLDRAW2DSVG_H
17 #define MOLDRAW2DSVG_H
18 
19 #include <iostream>
20 #include <sstream>
21 #include "MolDraw2D.h"
22 
23 // ****************************************************************************
24 
25 namespace RDKit {
26 
28  public:
29  // initialize to use a particular ostream
30  MolDraw2DSVG(int width, int height, std::ostream &os, int panelWidth = -1,
31  int panelHeight = -1)
32  : MolDraw2D(width, height, panelWidth, panelHeight), d_os(os) {
33  initDrawing();
34  };
35  // initialize to use the internal stringstream
36  MolDraw2DSVG(int width, int height, int panelWidth = -1, int panelHeight = -1)
37  : MolDraw2D(width, height, panelWidth, panelHeight), d_os(d_ss) {
38  initDrawing();
39  };
40 
41  // set font size in molecule coordinate units. That's probably Angstrom for
42  // RDKit. It will turned into drawing units using scale_, which might be
43  // changed as a result, to make sure things still appear in the window.
44  virtual void setFontSize(double new_size) override;
45  virtual void setColour(const DrawColour &col) override;
46 
47  // not sure if this goes here or if we should do a dtor since initDrawing() is
48  // called in the ctor,
49  // but we'll start here
50  void finishDrawing();
51 
52  virtual void drawLine(const Point2D &cds1, const Point2D &cds2) override;
53  virtual void drawString(const std::string &str, const Point2D &cds) override;
54  virtual void drawPolygon(const std::vector<Point2D> &cds) override;
55  virtual void drawEllipse(const Point2D &cds1, const Point2D &cds2) override;
56  virtual void clearDrawing() override;
57 
58  virtual void drawWavyLine(const Point2D &cds1, const Point2D &cds2,
59  const DrawColour &col1, const DrawColour &col2,
60  unsigned int nSegments = 16,
61  double vertOffset = 0.05) override;
62 
63  // using the current scale, work out the size of the label in molecule
64  // coordinates
65  virtual void getStringSize(const std::string &label, double &label_width,
66  double &label_height) const override;
67 
68  // this only makes sense if the object was initialized without a stream
69  std::string getDrawingText() const { return d_ss.str(); };
70 
71  // adds additional tags to the atoms and bonds in the SVG. This should be
72  // invoked *after* the molecule has been drawn
73  using MolDraw2D::tagAtoms; // Avoid overload warning.
74  void tagAtoms(const ROMol &mol) { tagAtoms(mol, 0.2); }
75  void tagAtoms(const ROMol &mol, double radius,
76  const std::map<std::string, std::string> &events = {});
77 
78  // adds metadata describing the molecule to the SVG. This allows
79  // molecules to be re-built from SVG with MolFromSVG
80  void addMoleculeMetadata(const ROMol &mol, int confId = -1) const;
81  void addMoleculeMetadata(const std::vector<ROMol *> &mols,
82  const std::vector<int> confIds = {}) const;
83 
84  private:
85  std::ostream &d_os;
86  std::stringstream d_ss;
87  std::string d_activeClass;
88 
89  virtual void drawChar(char c, const Point2D &cds) override;
90  void initDrawing();
91 
92  protected:
93  virtual void drawBond(
94  const ROMol &mol, const Bond *bond, int at1_idx, int at2_idx,
95  const std::vector<int> *highlight_atoms = nullptr,
96  const std::map<int, DrawColour> *highlight_atom_map = nullptr,
97  const std::vector<int> *highlight_bonds = nullptr,
98  const std::map<int, DrawColour> *highlight_bond_map = nullptr) override;
99 };
100 } // namespace RDKit
101 #endif // MOLDRAW2DSVG_H
RDKIT_MOLDRAW2D_EXPORT
#define RDKIT_MOLDRAW2D_EXPORT
Definition: export.h:398
RDKit::MolDraw2DSVG::MolDraw2DSVG
MolDraw2DSVG(int width, int height, int panelWidth=-1, int panelHeight=-1)
Definition: MolDraw2DSVG.h:36
RDKit::MolDraw2D::tagAtoms
virtual void tagAtoms(const ROMol &mol)
Definition: MolDraw2D.h:429
RDKit::MolDraw2DSVG::getDrawingText
std::string getDrawingText() const
Definition: MolDraw2DSVG.h:69
MolDraw2D.h
RDKit::DrawColour
Definition: MolDraw2D.h:37
RDKit::ROMol
Definition: ROMol.h:171
RDKit::MolDraw2DSVG::MolDraw2DSVG
MolDraw2DSVG(int width, int height, std::ostream &os, int panelWidth=-1, int panelHeight=-1)
Definition: MolDraw2DSVG.h:30
RDKit
Std stuff.
Definition: Atom.h:30
RDKit::MolDraw2D
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition: MolDraw2D.h:165
RDKit::MolDraw2DSVG
Definition: MolDraw2DSVG.h:27
RDGeom::Point2D
Definition: point.h:258
RDKit::MolDraw2DSVG::tagAtoms
void tagAtoms(const ROMol &mol)
Definition: MolDraw2DSVG.h:74
export.h