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