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>
23 
24 // ****************************************************************************
25 
26 namespace RDKit {
27 
29  public:
30  // initialize to use a particular ostream
31  MolDraw2DSVG(int width, int height, std::ostream &os, int panelWidth = -1,
32  int panelHeight = -1, bool noFreetype = false)
33  : MolDraw2D(width, height, panelWidth, panelHeight), d_os(os) {
34  initDrawing();
35  initTextDrawer(noFreetype);
36  }
37 
38  // initialize to use the internal stringstream
39  MolDraw2DSVG(int width, int height, int panelWidth = -1, int panelHeight = -1,
40  bool noFreetype = false)
41  : MolDraw2D(width, height, panelWidth, panelHeight), d_os(d_ss) {
42  initDrawing();
43  initTextDrawer(noFreetype);
44  }
45 
46  void setColour(const DrawColour &col) override;
47 
48  // not sure if this goes here or if we should do a dtor since initDrawing() is
49  // called in the ctor,
50  // but we'll start here
51  void finishDrawing();
52 
53  void drawLine(const Point2D &cds1, const Point2D &cds2) override;
54  void drawPolygon(const std::vector<Point2D> &cds) override;
55  void drawEllipse(const Point2D &cds1, const Point2D &cds2) override;
56  void clearDrawing() override;
57 
58  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  // this only makes sense if the object was initialized without a stream
64  std::string getDrawingText() const { return d_ss.str(); }
65 
66  // adds additional tags to the atoms and bonds in the SVG. This should be
67  // invoked *after* the molecule has been drawn
68  using MolDraw2D::tagAtoms; // Avoid overload warning.
69  void tagAtoms(const ROMol &mol) override { tagAtoms(mol, 0.2); }
70  void tagAtoms(const ROMol &mol, double radius,
71  const std::map<std::string, std::string> &events = {});
72 
73  // adds metadata describing the molecule to the SVG. This allows
74  // molecules to be re-built from SVG with MolFromSVG
75  void addMoleculeMetadata(const ROMol &mol, int confId = -1) const;
76  void addMoleculeMetadata(const std::vector<ROMol *> &mols,
77  const std::vector<int> confIds = {}) const;
78 
79  void drawAnnotation(const AnnotationType &annot) override;
80 
81  private:
82  std::ostream &d_os;
83  std::stringstream d_ss;
84  std::string d_activeClass;
85 
86  void initDrawing() override;
87  void initTextDrawer(bool noFreetype) override;
88 
89  protected:
90  void drawBond(const ROMol &mol, const Bond *bond, int at1_idx, int at2_idx,
91  const std::vector<int> *highlight_atoms = nullptr,
92  const std::map<int, DrawColour> *highlight_atom_map = nullptr,
93  const std::vector<int> *highlight_bonds = nullptr,
94  const std::map<int, DrawColour> *highlight_bond_map = nullptr,
95  const std::vector<std::pair<DrawColour, DrawColour>>
96  *bond_colours = nullptr) override;
97  void drawAtomLabel(int atom_num, const DrawColour &draw_colour) override;
98  //! DEPRECATED
99  void drawAnnotation(const std::string &note,
100  const StringRect &note_rect) override {
101  AnnotationType annot;
102  annot.text_ = note;
103  annot.rect_ = note_rect;
104  drawAnnotation(annot);
105  }
106 
107  virtual void outputClasses();
108 };
109 
110 } // namespace RDKit
111 #endif // MOLDRAW2DSVG_H
class for representing a bond
Definition: Bond.h:46
void setColour(const DrawColour &col) override
sets the current draw color
MolDraw2DSVG(int width, int height, int panelWidth=-1, int panelHeight=-1, bool noFreetype=false)
Definition: MolDraw2DSVG.h:39
virtual void outputClasses()
void drawAtomLabel(int atom_num, const DrawColour &draw_colour) override
void clearDrawing() override
clears the contents of the drawing
void tagAtoms(const ROMol &mol) override
Definition: MolDraw2DSVG.h:69
void drawAnnotation(const std::string &note, const StringRect &note_rect) override
DEPRECATED.
Definition: MolDraw2DSVG.h:99
void drawBond(const ROMol &mol, const Bond *bond, int at1_idx, int at2_idx, const std::vector< int > *highlight_atoms=nullptr, const std::map< int, DrawColour > *highlight_atom_map=nullptr, const std::vector< int > *highlight_bonds=nullptr, const std::map< int, DrawColour > *highlight_bond_map=nullptr, const std::vector< std::pair< DrawColour, DrawColour >> *bond_colours=nullptr) override
void addMoleculeMetadata(const std::vector< ROMol * > &mols, const std::vector< int > confIds={}) const
MolDraw2DSVG(int width, int height, std::ostream &os, int panelWidth=-1, int panelHeight=-1, bool noFreetype=false)
Definition: MolDraw2DSVG.h:31
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 drawEllipse(const Point2D &cds1, const Point2D &cds2) override
draw an ellipse
void addMoleculeMetadata(const ROMol &mol, int confId=-1) const
void tagAtoms(const ROMol &mol, double radius, const std::map< std::string, std::string > &events={})
void drawAnnotation(const AnnotationType &annot) override
void drawLine(const Point2D &cds1, const Point2D &cds2) override
void drawPolygon(const std::vector< Point2D > &cds) override
std::string getDrawingText() const
Definition: MolDraw2DSVG.h:64
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition: MolDraw2D.h:337
virtual void tagAtoms(const ROMol &mol)
Definition: MolDraw2D.h:638
#define RDKIT_MOLDRAW2D_EXPORT
Definition: export.h:265
Std stuff.
Definition: Abbreviations.h:18
std::string text_
Definition: MolDraw2D.h:170