bpp-core3  3.0.0
XFigGraphicDevice.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "XFigGraphicDevice.h"
6 
7 using namespace bpp;
8 using namespace std;
9 
10 const unsigned int XFigGraphicDevice::FONTFLAG_LATEX = 0;
11 const unsigned int XFigGraphicDevice::FONTFLAG_POSTSCRIPT = 4;
12 
14 {
15  content_.clear();
16 }
17 
19 {
20  // Print to file:
21  // Header:
22  out_ << "#FIG 3.2 Produced by the Bio++ Graphic Device System" << endl;
23  out_ << "Portrait" << endl;
24  out_ << "Flush left" << endl;
25  out_ << "Metric" << endl;
26  out_ << "A4" << endl;
27  out_ << "100" << endl;
28  out_ << "Single" << endl;
29  out_ << "0" << endl;
30  // out << "254 2" << endl; // 1fig unit = 0.1mm
31  out_ << "72 2" << endl; // 1fig unit = 1pt
32 
33  // Color definitions:
34  out_ << "#Color definitions:" << endl;
35  vector<unsigned int> codes = colorManager_.getCodes();
36  vector<RGBColor> colors = colorManager_.getColors();
37  for (unsigned int i = 32; i < colorManager_.getNumberOfColors(); i++)
38  {
39  string hexCode = colors[i].toHex();
40  out_ << "0 " << codes[i] << " " << hexCode << endl;
41  }
42 
43  // Print content:
44  out_ << "#Drawing content:" << endl;
45  for (unsigned int i = 0; i < content_.size(); i++)
46  {
47  out_ << content_[i] << endl;
48  }
49 }
50 
52 {
53  fgColorCode_ = colorManager_.getCode(color);
55 }
56 
58 {
59  bgColorCode_ = colorManager_.getCode(color);
61 }
62 
64 {
65  if (fontFlag_ == FONTFLAG_LATEX)
66  fontCode_ = latexFontManager_.getCode(font);
67  else if (fontFlag_ == FONTFLAG_POSTSCRIPT)
68  fontCode_ = postscriptFontManager_.getCode(font);
69  else
70  fontCode_ = 0;
71  fontSize_ = font.getSize();
73 }
74 
75 void XFigGraphicDevice::drawLine(double x1, double y1, double x2, double y2)
76 {
77  ostringstream oss;
78  oss << "2 1 " << lineTypeCode_ << " " << getCurrentPointSize()
79  << " " << fgColorCode_
80  << " " << bgColorCode_
81  << " " << getCurrentLayer()
82  << " " << "-1 -1 -1 0 0 0 0 0 2" << endl;
83  oss << round(x_(x1)) << " " << round(y_(y1)) << endl;
84  oss << round(x_(x2)) << " " << round(y_(y2));
85  content_.push_back(oss.str());
86 }
87 
88 void XFigGraphicDevice::drawRect(double x, double y, double width, double height, short fill)
89 {
90  ostringstream oss;
91  oss << "2 2 0 " << getCurrentPointSize()
92  << " " << fgColorCode_
93  << " " << bgColorCode_
94  << " " << getCurrentLayer()
95  << " " << "-1"
96  << " " << getFillCode(fill) << " -1 0 0 0 0 0 5" << endl;
97  oss << round(x) << " " << round(y) << endl;
98  oss << round(x_(x + width)) << " " << round(y_(y)) << endl;
99  oss << round(x_(x + width)) << " " << round(y_(y + height)) << endl;
100  oss << round(x_(x)) << " " << round(y_(y + height)) << endl;
101  oss << round(x_(x)) << " " << round(y_(y));
102  content_.push_back(oss.str());
103 }
104 
105 void XFigGraphicDevice::drawCircle(double x, double y, double radius, short fill)
106 {
107  ostringstream oss;
108  oss << "1 3 0 " << getCurrentPointSize()
109  << " " << fgColorCode_
110  << " " << bgColorCode_
111  << " " << getCurrentLayer()
112  << " " << "-1"
113  << " " << getFillCode(fill) << " -1 1 0 "
114  << round(x_(x)) << " " << round(y_(y)) << " "
115  << round(x_(radius)) << " " << round(y_(radius)) << " "
116  << round(x_(x + radius)) << " " << round(y_(y)) << " "
117  << round(x_(x + radius)) << " " << round(y_(y)) << endl;
118  content_.push_back(oss.str());
119 }
120 
121 void XFigGraphicDevice::drawText(double x, double y, const std::string& text, short hpos, short vpos, double angle)
122 {
123  int xrel = static_cast<int>(round(x_(x)));
124  short sub = 0;
125  if (hpos == TEXT_HORIZONTAL_LEFT)
126  sub = 0;
127  else if (hpos == TEXT_HORIZONTAL_CENTER)
128  sub = 1;
129  else if (hpos == TEXT_HORIZONTAL_RIGHT)
130  sub = 2;
131  else
132  throw UnvalidFlagException("XFigGraphicDevice::drawText(). Bad horizontal text alignment flag: " + TextTools::toString(hpos));
133 
134  int yrel = 0;
135  if (vpos == TEXT_VERTICAL_BOTTOM)
136  yrel = static_cast<int>(round(y_(y - 1.)));
137  else if (vpos == TEXT_VERTICAL_CENTER)
138  yrel = static_cast<int>(round(y + fontSize_ / 2 - 1));
139  else if (vpos == TEXT_VERTICAL_TOP)
140  yrel = static_cast<int>(round(y - fontSize_));
141  else
142  throw UnvalidFlagException("XFigGraphicDevice::drawText(). Bad vertical text alignment flag: " + TextTools::toString(vpos));
143 
144  ostringstream oss;
145  oss << "4 " << sub << " " << fgColorCode_ << " " << 50 << " " << -1 << " " << fontCode_ << " " << fontSize_ << " "
146  << angle << " " << fontFlag_ << " " << -1 << " " << -1 << " " << xrel << " " << yrel << " " << text << "\\001";
147  content_.push_back(oss.str());
148 }
149 
151 {
152  if (fill == FILL_EMPTY)
153  return -1;
154  if (fill == FILL_FILLED)
155  return 20;
156  if (fill == FILL_PATTERN)
157  {
158  // TODO: define a field names currentPattern_, etc.
159  }
160  // Temp:
161  return 20;
162 }
void end()
End the painting.
Data structure for fonts.
Definition: Font.h:21
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:21
static const unsigned int FONTFLAG_LATEX
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:84
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.
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
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.