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