bpp-core3  3.0.0
SvgGraphicDevice.cpp
Go to the documentation of this file.
1 //
2 // File: SvgGraphicDevice.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2008-03-10 00:00:00
6 //
7 
8 /*
9  Copyright or © or Copr. CNRS, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide utilitary
12  classes. This file belongs to the Bio++ Project.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 
42 #include "SvgGraphicDevice.h"
43 
44 using namespace bpp;
45 using namespace std;
46 
48 {
49  layers_.clear();
50  minx_ = maxx_ = miny_ = maxy_ = 0;
51 }
52 
54 {
55  // Header:
56  out_ << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" << endl;
57  out_ << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD Svg 1.1//EN\"" << endl;
58  out_ << "\"http://www.w3.org/Graphics/Svg/1.1/DTD/svg11.dtd\">" << endl;
59  out_ << "<svg width=\"" << (maxx_ - minx_) << "\" height=\"" << (maxy_ - miny_) << "\" version=\"1.1\"" << endl;
60  out_ << " xmlns=\"http://www.w3.org/2000/svg\"" << endl;
61  if (inkscapeEnabled_)
62  out_ << " xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\"";
63  out_ << " >" << endl;
64 
65  out_ << "<g transform=\"translate(" << (-minx_) << "," << (-miny_) << ")\">" << endl;
66 
67  for (map<int, vector<string> >::iterator it = layers_.begin(); it != layers_.end(); it++)
68  {
69  out_ << "<g id=\"layer" << it->first << "\"";
70  if (inkscapeEnabled_)
71  {
72  out_ << " inkscape:groupmode=\"layer\"";
73  }
74  out_ << " >" << endl;
75  vector<string>* v = &it->second;
76  for (unsigned int i = 0; i < v->size(); i++)
77  {
78  out_ << (*v)[i] << endl;
79  }
80  out_ << "</g>" << endl;
81  }
82  out_ << "</g>" << endl;
83 
84  out_ << "</svg>" << endl;
85 }
86 
87 void SvgGraphicDevice::drawLine(double x1, double y1, double x2, double y2)
88 {
89  x1 = x_(x1);
90  x2 = x_(x2);
91  y1 = y_(y1);
92  y2 = y_(y2);
93  string style = "stroke:" + colorToText(getCurrentForegroundColor()) + ";stroke-width:" + TextTools::toString(getCurrentPointSize());
94  if (getCurrentLineType() == LINE_DASHED)
95  style += ";stroke-dasharray:4,4";
96  else if (getCurrentLineType() == LINE_DOTTED)
97  style += ";stroke-dasharray:1,2";
98  ostringstream oss;
99  oss << "<line x1=\"" << x1 << "\" y1=\"" << y1 << "\" x2=\"" << x2 << "\" y2=\"" << y2 << "\" style=\"" << style << "\" />";
100  layers_[getCurrentLayer()].push_back(oss.str());
101  if (x1 < minx_)
102  minx_ = x1;
103  if (x2 < minx_)
104  minx_ = x2;
105  if (y1 < miny_)
106  miny_ = y1;
107  if (y2 < miny_)
108  miny_ = y2;
109  if (x1 > maxx_)
110  maxx_ = x1;
111  if (x2 > maxx_)
112  maxx_ = x2;
113  if (y1 > maxy_)
114  maxy_ = y1;
115  if (y2 > maxy_)
116  maxy_ = y2;
117 }
118 
119 void SvgGraphicDevice::drawRect(double x, double y, double width, double height, short fill)
120 {
121  x = x_(x);
122  y = y_(y);
123  width = x_(width);
124  height = y_(height);
125  string style = "stroke:" + colorToText(getCurrentForegroundColor()) + ";stroke-width:" + TextTools::toString(getCurrentPointSize());
126  if (fill == FILL_FILLED)
127  {
128  style += ";fill:" + colorToText(getCurrentBackgroundColor());
129  }
130  ostringstream oss;
131  oss << "<rect x=\"" << x << "\" y=\"" << y << "\" width=\"" << width << "\" height=\"" << height << "\" style=\"" << style << "\" />";
132  layers_[getCurrentLayer()].push_back(oss.str());
133  if (x < minx_)
134  minx_ = x;
135  if (y < miny_)
136  miny_ = y;
137  if (x + width > maxx_)
138  maxx_ = x + width;
139  if (y + height > maxy_)
140  maxx_ = y + height;
141 }
142 
143 void SvgGraphicDevice::drawCircle(double x, double y, double radius, short fill)
144 {
145  x = x_(x);
146  y = y_(y);
147  radius = x_(radius);
148  string style = "stroke:" + colorToText(getCurrentForegroundColor()) + ";stroke-width:" + TextTools::toString(getCurrentPointSize());
149  if (fill == FILL_FILLED)
150  {
151  style += ";fill:" + colorToText(getCurrentBackgroundColor());
152  }
153  ostringstream oss;
154  oss << "<rect cx=\"" << x << "\" cy=\"" << y << "\" cr=\"" << radius << "\" style=\"" << style << "\" />";
155  layers_[getCurrentLayer()].push_back(oss.str());
156 }
157 
158 void SvgGraphicDevice::drawText(double x, double y, const std::string& text, short hpos, short vpos, double angle)
159 {
160  x = x_(x);
161  y = y_(y);
162  string style = "font-family:" + getCurrentFont().getFamily() + ";font-style:" + fontStyles_[getCurrentFont().getStyle()] + ";font-weight:" + fontWeights_[getCurrentFont().getWeight()] + ";font-size:" + TextTools::toString(getCurrentFont().getSize()) + "px";
163  style += ";dominant-baseline:";
164  if (vpos == TEXT_VERTICAL_BOTTOM)
165  style += "before-edge";
166  else if (vpos == TEXT_VERTICAL_TOP)
167  style += "after-edge";
168  else if (vpos == TEXT_VERTICAL_CENTER)
169  style += "middle";
170  else
171  throw UnvalidFlagException("SvgGraphicDevice::drawText. Invalid vertical alignment option.");
172  style += ";text-anchor:";
173  if (hpos == TEXT_HORIZONTAL_LEFT)
174  style += "start";
175  else if (hpos == TEXT_HORIZONTAL_RIGHT)
176  style += "end";
177  else if (hpos == TEXT_HORIZONTAL_CENTER)
178  style += "middle";
179  else
180  throw UnvalidFlagException("SvgGraphicDevice::drawText. Invalid horizontal alignment option.");
181  style += ";fill:" + colorToText(getCurrentForegroundColor());
182 
183  ostringstream oss;
184  oss << "<text x=\"" << x << "\" y=\"" << y << "\" rotate=\"" << angle << "\" style=\"" << style << "\" >" << text << "</text>";
185  layers_[getCurrentLayer()].push_back(oss.str());
186 }
void drawCircle(double x, double y, double radius, short fill=FILL_EMPTY)
Draw a circle.
void drawLine(double x1, double y1, double x2, double y2)
Draw a line between two points.
void drawRect(double x, double y, double width, double height, short fill=FILL_EMPTY)
Draw a rectangle.
void drawText(double x, double y, const std::string &text, short hpos=TEXT_HORIZONTAL_LEFT, short vpos=TEXT_VERTICAL_BOTTOM, double angle=0)
Draw some characters.
void begin()
Start the painting.
void end()
End the painting.
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153