1 //
2 // Copyright (C) 2015-2020 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
11 #include <RDGeneral/export.h>
12 #ifndef RDKITMOLDRAW2DDETAILS_H
13 #define RDKITMOLDRAW2DDETAILS_H
14
15 #include <vector>
16
17 #include <Geometry/point.h>
18 #include <GraphMol/RDKitBase.h>
19 #include <GraphMol/MolDraw2D/MolDraw2D.h>
20
21 #include <boost/tuple/tuple.hpp>
22 #include <boost/format.hpp>
23
24 // ****************************************************************************
25 using RDGeom::Point2D;
26
27 namespace RDKit {
28 namespace MolDraw2D_detail {
29 // data taken from the helvetica font info in
30 // $RDBASE/rdkit/sping/PDF/pdfmetrics.py
31 const int char_widths[] = {
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34 0, 0, 0, 0, 278, 278, 355, 556, 556, 889, 667, 222, 333, 333,
35 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556,
36 556, 556, 278, 278, 584, 584, 584, 556, 1015, 667, 667, 722, 722, 667,
37 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, 667, 778, 722, 667,
38 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556, 222, 556,
39 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556,
40 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334,
41 584, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43 0, 0, 0, 0, 0, 0, 0, 333, 556, 556, 167, 556, 556, 556,
44 556, 191, 333, 556, 333, 333, 500, 500, 0, 556, 556, 556, 278, 0,
45 537, 350, 222, 333, 333, 556, 1000, 1000, 0, 611, 0, 333, 333, 333,
46 333, 333, 333, 333, 333, 0, 333, 333, 0, 333, 333, 333, 1000, 0,
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 1000, 0, 370, 0, 0, 0, 0, 556, 778, 1000, 365, 0, 0,
49 0, 0, 0, 889, 0, 0, 0, 278, 0, 0, 222, 611, 944, 611,
50 0, 0, 834};
51
52 // angles in degrees.
53 RDKIT_MOLDRAW2D_EXPORT void arcPoints(const Point2D &cds1, const Point2D &cds2,
54 std::vector<Point2D> &res,
55 float startAng = 0, float extent = 360);
56
57 //! add R/S, relative stereo, and E/Z annotations to atoms and bonds
58 RDKIT_MOLDRAW2D_EXPORT void addStereoAnnotation(
59 const ROMol &mol, bool includeRelativeCIP = false);
60
61 //! add annotations with atom indices.
addAtomIndices(const ROMol & mol)62 RDKIT_MOLDRAW2D_EXPORT inline void addAtomIndices(const ROMol &mol) {
63 // we don't need this in the global set of tags since it will only be used
64 // here
65 if (mol.hasProp("_atomIndicesAdded")) return;
66 bool computed = true;
67 mol.setProp("_atomIndicesAdded", 1, computed);
68 for (auto atom : mol.atoms()) {
69 auto lab = std::to_string(atom->getIdx());
70 if (atom->hasProp(common_properties::atomNote)) {
71 lab += "," + atom->getProp<std::string>(common_properties::atomNote);
72 }
73 atom->setProp(common_properties::atomNote, lab);
74 }
75 };
76
77 //! add annotations with bond indices.
addBondIndices(const ROMol & mol)78 RDKIT_MOLDRAW2D_EXPORT inline void addBondIndices(const ROMol &mol) {
79 // we don't need this in the global set of tags since it will only be used
80 // here
81 if (mol.hasProp("_bondIndicesAdded")) return;
82 bool computed = true;
83 mol.setProp("_bondIndicesAdded", 1, computed);
84 for (auto bond : mol.bonds()) {
85 auto lab = std::to_string(bond->getIdx());
86 if (bond->hasProp(common_properties::bondNote)) {
87 lab += "," + bond->getProp<std::string>(common_properties::bondNote);
88 }
89 bond->setProp(common_properties::bondNote, lab);
90 }
91 };
92
93 RDKIT_MOLDRAW2D_EXPORT std::vector<Point2D> getBracketPoints(
94 const Point2D &p1, const Point2D &p2, const Point2D &refPt,
95 const std::vector<std::pair<Point2D, Point2D>> &bondSegments,
96 double bracketFrac = 0.1);
97 RDKIT_MOLDRAW2D_EXPORT void drawShapes(MolDraw2D &drawer,
98 const std::vector<MolDrawShape> &shapes);
99
100 // there are a several empirically determined constants here.
101 RDKIT_MOLDRAW2D_EXPORT std::vector<Point2D> handdrawnLine(
102 Point2D cds1, Point2D cds2, double scale, bool shiftBegin = false,
103 bool shiftEnd = false, unsigned nSteps = 4, double deviation = 0.03,
104 double endShift = 0.5);
105
106 } // namespace MolDraw2D_detail
107 } // namespace RDKit
108
109 #endif
110