bpp-core3  3.0.0
XFigGraphicDevice.cpp
Go to the documentation of this file.
1 //
2 // File: XFigGraphicDevice.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2008-03-03 00:00:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (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 "XFigGraphicDevice.h"
43 
44 using namespace bpp;
45 using namespace std;
46 
47 const unsigned int XFigGraphicDevice::FONTFLAG_LATEX = 0;
48 const unsigned int XFigGraphicDevice::FONTFLAG_POSTSCRIPT = 4;
49 
51 {
52  content_.clear();
53 }
54 
56 {
57  // Print to file:
58  // Header:
59  out_ << "#FIG 3.2 Produced by the Bio++ Graphic Device System" << endl;
60  out_ << "Portrait" << endl;
61  out_ << "Flush left" << endl;
62  out_ << "Metric" << endl;
63  out_ << "A4" << endl;
64  out_ << "100" << endl;
65  out_ << "Single" << endl;
66  out_ << "0" << endl;
67  // out << "254 2" << endl; // 1fig unit = 0.1mm
68  out_ << "72 2" << endl; // 1fig unit = 1pt
69 
70  // Color definitions:
71  out_ << "#Color definitions:" << endl;
72  vector<unsigned int> codes = colorManager_.getCodes();
73  vector<RGBColor> colors = colorManager_.getColors();
74  for (unsigned int i = 32; i < colorManager_.getNumberOfColors(); i++)
75  {
76  string hexCode = colors[i].toHex();
77  out_ << "0 " << codes[i] << " " << hexCode << endl;
78  }
79 
80  // Print content:
81  out_ << "#Drawing content:" << endl;
82  for (unsigned int i = 0; i < content_.size(); i++)
83  {
84  out_ << content_[i] << endl;
85  }
86 }
87 
89 {
90  fgColorCode_ = colorManager_.getCode(color);
92 }
93 
95 {
96  bgColorCode_ = colorManager_.getCode(color);
98 }
99 
101 {
102  if (fontFlag_ == FONTFLAG_LATEX)
103  fontCode_ = latexFontManager_.getCode(font);
104  else if (fontFlag_ == FONTFLAG_POSTSCRIPT)
105  fontCode_ = postscriptFontManager_.getCode(font);
106  else
107  fontCode_ = 0;
108  fontSize_ = font.getSize();
110 }
111 
112 void XFigGraphicDevice::drawLine(double x1, double y1, double x2, double y2)
113 {
114  ostringstream oss;
115  oss << "2 1 " << lineTypeCode_ << " " << getCurrentPointSize()
116  << " " << fgColorCode_
117  << " " << bgColorCode_
118  << " " << getCurrentLayer()
119  << " " << "-1 -1 -1 0 0 0 0 0 2" << endl;
120  oss << round(x_(x1)) << " " << round(y_(y1)) << endl;
121  oss << round(x_(x2)) << " " << round(y_(y2));
122  content_.push_back(oss.str());
123 }
124 
125 void XFigGraphicDevice::drawRect(double x, double y, double width, double height, short fill)
126 {
127  ostringstream oss;
128  oss << "2 2 0 " << getCurrentPointSize()
129  << " " << fgColorCode_
130  << " " << bgColorCode_
131  << " " << getCurrentLayer()
132  << " " << "-1"
133  << " " << getFillCode(fill) << " -1 0 0 0 0 0 5" << endl;
134  oss << round(x) << " " << round(y) << endl;
135  oss << round(x_(x + width)) << " " << round(y_(y)) << endl;
136  oss << round(x_(x + width)) << " " << round(y_(y + height)) << endl;
137  oss << round(x_(x)) << " " << round(y_(y + height)) << endl;
138  oss << round(x_(x)) << " " << round(y_(y));
139  content_.push_back(oss.str());
140 }
141 
142 void XFigGraphicDevice::drawCircle(double x, double y, double radius, short fill)
143 {
144  ostringstream oss;
145  oss << "1 3 0 " << getCurrentPointSize()
146  << " " << fgColorCode_
147  << " " << bgColorCode_
148  << " " << getCurrentLayer()
149  << " " << "-1"
150  << " " << getFillCode(fill) << " -1 1 0 "
151  << round(x_(x)) << " " << round(y_(y)) << " "
152  << round(x_(radius)) << " " << round(y_(radius)) << " "
153  << round(x_(x + radius)) << " " << round(y_(y)) << " "
154  << round(x_(x + radius)) << " " << round(y_(y)) << endl;
155  content_.push_back(oss.str());
156 }
157 
158 void XFigGraphicDevice::drawText(double x, double y, const std::string& text, short hpos, short vpos, double angle)
159 {
160  int xrel = static_cast<int>(round(x_(x)));
161  short sub = 0;
162  if (hpos == TEXT_HORIZONTAL_LEFT)
163  sub = 0;
164  else if (hpos == TEXT_HORIZONTAL_CENTER)
165  sub = 1;
166  else if (hpos == TEXT_HORIZONTAL_RIGHT)
167  sub = 2;
168  else
169  throw UnvalidFlagException("XFigGraphicDevice::drawText(). Bad horizontal text alignment flag: " + TextTools::toString(hpos));
170 
171  int yrel = 0;
172  if (vpos == TEXT_VERTICAL_BOTTOM)
173  yrel = static_cast<int>(round(y_(y - 1.)));
174  else if (vpos == TEXT_VERTICAL_CENTER)
175  yrel = static_cast<int>(round(y + fontSize_ / 2 - 1));
176  else if (vpos == TEXT_VERTICAL_TOP)
177  yrel = static_cast<int>(round(y - fontSize_));
178  else
179  throw UnvalidFlagException("XFigGraphicDevice::drawText(). Bad vertical text alignment flag: " + TextTools::toString(vpos));
180 
181  ostringstream oss;
182  oss << "4 " << sub << " " << fgColorCode_ << " " << 50 << " " << -1 << " " << fontCode_ << " " << fontSize_ << " "
183  << angle << " " << fontFlag_ << " " << -1 << " " << -1 << " " << xrel << " " << yrel << " " << text << "\\001";
184  content_.push_back(oss.str());
185 }
186 
188 {
189  if (fill == FILL_EMPTY)
190  return -1;
191  if (fill == FILL_FILLED)
192  return 20;
193  if (fill == FILL_PATTERN)
194  {
195  // TODO: define a field names currentPattern_, etc.
196  }
197  // Temp:
198  return 20;
199 }
void setCurrentForegroundColor(const RGBColor &color)
void setCurrentBackgroundColor(const RGBColor &color)
void setCurrentFont(const Font &font)
Data structure for fonts.
Definition: Font.h:59
const unsigned int & getSize() const
Definition: Font.h:120
Describe a color according to its red, green and blue componants.
Definition: RgbColor.h:60
void drawLine(double x1, double y1, double x2, double y2)
Draw a line between two points.
static const unsigned int FONTFLAG_POSTSCRIPT
void setCurrentFont(const Font &font)
void drawRect(double x, double y, double width, double height, short fill=FILL_EMPTY)
Draw a rectangle.
void setCurrentForegroundColor(const RGBColor &color)
static const unsigned int FONTFLAG_LATEX
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 setCurrentBackgroundColor(const RGBColor &color)
void begin()
Start the painting.
void drawCircle(double x, double y, double radius, short fill=FILL_EMPTY)
Draw a circle.
void end()
End the painting.
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153