35 #ifndef _RD_DRAWING_TO_SVG_H_ 36 #define _RD_DRAWING_TO_SVG_H_ 43 std::string getColor(
int atNum) {
44 std::string res =
"#000000";
75 void drawLine(std::vector<int>::const_iterator &pos, std::ostringstream &sstr,
76 unsigned int lineWidthMult) {
78 width *= lineWidthMult;
81 std::string dashString =
"";
86 dashString =
";stroke-dasharray:2, 6";
89 dashString =
";stroke-dasharray:6, 6";
93 std::string c1 = getColor(an1);
94 std::string c2 = getColor(an2);
97 sstr <<
"d='M " << *pos <<
"," << *(pos + 1) <<
" " << *(pos + 2) <<
"," 98 << *(pos + 3) <<
"' ";
100 sstr <<
"style='fill:none;fill-rule:evenodd;stroke:" << c1
101 <<
";stroke-width:" << width
102 <<
"px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" 103 << dashString <<
"'";
110 int mx = xp1 + (xp2 - xp1) / 2;
111 int my = yp1 + (yp2 - yp1) / 2;
112 sstr <<
"<svg:path ";
113 sstr <<
"d='M " << xp1 <<
"," << yp1 <<
" " << mx <<
"," << my <<
"' ";
114 sstr <<
"style='fill:none;fill-rule:evenodd;stroke:" << c1
115 <<
";stroke-width:" << width
116 <<
"px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" 117 << dashString <<
"'";
119 sstr <<
"<svg:path ";
120 sstr <<
"d='M " << mx <<
"," << my <<
" " << xp2 <<
"," << yp2 <<
"' ";
121 sstr <<
"style='fill:none;fill-rule:evenodd;stroke:" << c2
122 <<
";stroke-width:" << width
123 <<
"px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" 124 << dashString <<
"'";
129 void drawAtom(std::vector<int>::const_iterator &pos, std::ostringstream &sstr,
130 unsigned int fontSz,
bool includeAtomCircles =
false) {
135 std::string label =
"";
136 for (
unsigned int i = 0; i < slen; ++i) {
137 label += (char)*pos++;
140 if (label.length()) {
141 int width = fontSz * label.length();
143 sstr <<
"<svg:g transform='translate(" << xp <<
"," << yp
145 sstr <<
"style='opacity:1.0;fill:#FFFFFF;stroke:none'";
146 sstr <<
" width='" << width <<
"' height='" << height <<
"'";
147 sstr <<
" x='-" << width / 2 <<
"' y='-" << height / 2 <<
"'";
148 sstr <<
"> </svg:rect>\n";
150 sstr <<
" style='font-size:" << fontSz
151 <<
"px;font-style:normal;font-weight:normal;line-height:125%;letter-" 152 "spacing:0px;word-spacing:0px;fill-opacity:1;stroke:none;font-" 153 "family:sans-serif;text-anchor:middle" 154 <<
";fill:" << getColor(atNum) <<
"'";
155 sstr <<
" y='" << .75 * fontSz / 2 <<
"'>";
156 sstr <<
"<svg:tspan>";
157 sstr << label <<
"</svg:tspan>";
158 sstr <<
"</svg:text>";
159 sstr <<
"</svg:g>\n";
161 if (includeAtomCircles) {
163 sstr<<
"<svg:circle cx='"<<xp<<
"' cy='"<<yp<<
"' r='5' />\n";
165 sstr <<
"<svg:circle cx='" << xp <<
"' cy='" << yp
166 <<
"' r='0' style='fill:none;stroke:none' />\n";
173 unsigned int lineWidthMult = 2,
174 unsigned int fontSize = 50,
175 bool includeAtomCircles =
false) {
176 std::vector<int>::const_iterator pos = drawing.begin() + 2;
178 std::cerr <<
"no bounds token found" << std::endl;
182 unsigned int width, height;
185 std::ostringstream sstr;
186 sstr <<
"<?xml version='1.0' encoding='iso-8859-1'?>\n";
187 sstr <<
"<svg:svg version='1.1' baseProfile='full'\n \ 188 xmlns:svg='http://www.w3.org/2000/svg'\n \ 189 xmlns:xlink='http://www.w3.org/1999/xlink'\n \ 190 xml:space='preserve'\n";
191 sstr <<
"width='" << width <<
"px' height='" << height <<
"px' >\n";
192 sstr <<
"<svg:rect ";
193 sstr <<
"style='opacity:1.0;fill:#FFFFFF;stroke:none'";
194 sstr <<
" width='" << width <<
"' height='" << height <<
"'";
195 sstr <<
" x='0' y='0'";
196 sstr <<
"> </svg:rect>\n";
197 sstr <<
"<svg:g transform='translate(" << width * .05 <<
"," << height * .05
198 <<
") scale(.85,.85)'>";
199 while (pos != drawing.end()) {
206 drawAtom(pos, sstr, fontSize, includeAtomCircles);
209 std::cerr <<
"unrecognized token: " << token << std::endl;
212 sstr <<
"</svg:g></svg:svg>";
std::string DrawingToSVG(const std::vector< int > &drawing, unsigned int lineWidthMult=2, unsigned int fontSize=50, bool includeAtomCircles=false)
Includes a bunch of functionality for handling Atom and Bond queries.
void drawLine(std::vector< ElementType > &res, int atnum1, int atnum2, int lineWidth, int dashed, double x1, double y1, double x2, double y2)