Board 0.9.6
Board.h
Go to the documentation of this file.
1/* -*- mode: c++ -*- */
27#ifndef BOARD_BOARD_H
28#define BOARD_BOARD_H
29
30#include <ostream>
31#include <string>
32#include <vector>
33
34#include <BoardConfig.h>
35#include <board/Arrow.h>
36#include <board/Bezier.h>
37#include <board/BoardFontText.h>
38#include <board/Dot.h>
39#include <board/Ellipse.h>
40#include <board/Exception.h>
41#include <board/Group.h>
42#include <board/Image.h>
43#include <board/Line.h>
44#include <board/Path.h>
45#include <board/Point.h>
46#include <board/Polyline.h>
47#include <board/RoughVisitor.h>
48#include <board/Shape.h>
49#include <board/ShapeList.h>
50#include <board/SketchFilter.h>
51#include <board/Style.h>
52#include <board/Text.h>
53
54namespace LibBoard
55{
56
61struct Board : public ShapeList {
62
63public:
64 static const double Degree;
65
69 enum class AspectRatio
70 {
71 Ignore,
73 };
74
80 Board(const Color & backgroundColor = Color::Null);
81
87 Board(const Board & other);
88
89 ~Board() override;
90
98 Board & operator=(const Board & other);
99
107 Board & operator<<(const Shape & shape);
108
112 template <typename S> //
113 Board & operator<<(const std::vector<S> & shapes);
114
120 void clear(const Color & color = Color::Null);
121
129 inline void clear(unsigned char red, unsigned char green, unsigned char blue);
130
137 Board & rotate(double angle, const Point & center) override;
138
144 Board & rotate(double angle) override;
145
152 Board & translate(double dx, double dy) override;
153
160 Board & scale(double sx, double sy) override;
161
167 Board & scale(double s) override;
168
175 Board rotated(double angle, const Point & center);
176
183 Board rotated(double angle);
184
191 Board translated(double dx, double dy);
192
199 Board scaled(double sx, double sy);
200
206 Board scaled(double s);
207
214 void drawDot(double x, double y);
215
223 void drawLine(double x1, double y1, double x2, double y2);
224
231 void drawLine(Point p, Point q);
232
242 void drawArrow(double x1, double y1, double x2, double y2, Arrow::ExtremityType type = Arrow::ExtremityType::Plain);
243
252
263 void drawTriangle(double x1, double y1, double x2, double y2, double x3, double y3);
264
272 void drawTriangle(const Point & p1, const Point & p2, const Point & p3);
273
284 void fillTriangle(double x1, double y1, double x2, double y2, double x3, double y3);
285
297 void fillGouraudTriangle(const Point & p1, const Color & color1, //
298 const Point & p2, const Color & color2, //
299 const Point & p3, const Color & color3, unsigned char divisions = 3);
300
315 inline void fillGouraudTriangle(const double x1, const double y1, const Color & color1, //
316 const double x2, const double y2, const Color & color2, //
317 const double x3, const double y3, const Color & color3, unsigned char divisions = 3);
318
330 void fillGouraudTriangle(const Point & p1, const float brightness1, //
331 const Point & p2, const float brightness2, //
332 const Point & p3, const float brightness3, unsigned char divisions = 3);
333
349 inline void fillGouraudTriangle(const double x1, const double y1, const float brightness1, //
350 const double x2, const double y2, const float brightness2, //
351 const double x3, const double y3, const float brightness3, //
352 unsigned char divisions = 3);
353
361 void fillTriangle(const Point & p1, const Point & p2, const Point & p3);
362
371 void drawRectangle(double left, double top, double width, double height);
372
378 void drawRectangle(const Rect & rect);
379
388 void fillRectangle(double left, double top, double width, double height);
389
394 void fillRectangle(const Rect & rect);
395
403 void drawCircle(double x, double y, double radius);
404
412 void fillCircle(double x, double y, double radius);
413
422 void drawEllipse(double x, double y, double xRadius, double yRadius);
423
432 void fillEllipse(double x, double y, double xRadius, double yRadius);
433
439 void drawPolyline(const std::vector<Point> & points);
440
446 void drawClosedPolyline(const std::vector<Point> & points);
447
453 void fillPolyline(const std::vector<Point> & points);
454
462 void drawText(double x, double y, const char * text);
463
470 void drawText(Point p, const char * text);
471
479 void drawText(double x, double y, const std::string & text);
480
487 void drawText(Point p, const std::string & str);
488
496 Board & setFont(const Fonts::Font font, double fontSize);
497
504 Board & setFontSize(double fontSize);
505
515 Board & setPenColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255);
516
527 Board & setPenColorRGBf(float red, float green, float blue, float alpha = 1.0f);
528
536 Board & setPenColor(const Color & color);
537
547 Board & setFillColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255);
548
558 Board & setFillColorRGBf(float red, float green, float blue, float alpha = 1.0f);
559
569 Board & setFillColorHSV(float hue, float saturation, float value, float alpha = 1.0f);
570
578 Board & setFillColor(const Color & color);
579
586 Board & setLineWidth(double width);
587
595
601 inline Board & setLineCap(LineCap cap);
602
608 inline Board & setLineJoin(LineJoin join);
609
615 void setBackgroundColor(const Color & color);
616
622 void drawBoundingBox(LineWidthFlag lineWidthFlag);
623
632 void setClippingRectangle(double x, double y, double width, double height);
633
639 void setClippingRectangle(const Rect & rect);
640
646 void setClippingPath(const std::vector<Point> & points);
647
652 void setClippingPath(const Path & path);
653
664 void addDuplicates(const Shape & shape, std::size_t times, double dx, double dy, double scale = 1.0);
665
678 void addDuplicates(const Shape & shape, std::size_t times, double dx, double dy, double scaleX, double scaleY, double angle = 0.0);
679
690 void save(const char * filename, PageSize size = PageSize::BoundingBox, double margin = 0.0, Unit unit = Unit::Millimeter) const;
691
703 void save(const char * filename, double pageWidth, double pageHeight, double margin = 0.0, Unit unit = Unit::Millimeter) const;
704
715 void saveEPS(std::ostream & out, PageSize size = PageSize::BoundingBox, double margin = 0.0, Unit unit = Unit::Millimeter, const std::string & title = std::string()) const;
716
727 void saveEPS(const char * filename, PageSize size = PageSize::BoundingBox, double margin = 0.0, Unit unit = Unit::Millimeter, const std::string & title = std::string()) const;
728
739 void saveEPS(std::ostream & out, double pageWidth, double pageHeight, double margin = 0.0, Unit unit = Unit::Millimeter, const std::string & title = std::string()) const;
740
752 void saveEPS(const char * filename, double pageWidth, double pageHeight, double margin = 0.0, Unit unit = Unit::Millimeter, const std::string & title = std::string()) const;
753
763 void saveFIG(const char * filename, PageSize size = PageSize::BoundingBox, double margin = 0.0, Unit unit = Unit::Millimeter) const;
764
774 void saveFIG(std::ostream & out, PageSize size = PageSize::BoundingBox, double margin = 0.0, Unit unit = Unit::Millimeter) const;
775
785 void saveFIG(const char * filename, double pageWidth, double pageHeight, double margin = 0.0, Unit unit = Unit::Millimeter) const;
786
796 void saveFIG(std::ostream & out, double pageWidth, double pageHeight, double margin = 0.0, Unit unit = Unit::Millimeter) const;
797
807 void saveSVG(const char * filename, PageSize size = PageSize::BoundingBox, double margin = 0.0, Unit unit = Unit::Millimeter) const;
808
818 void saveSVG(std::ostream & out, PageSize size = PageSize::BoundingBox, double margin = 0.0, Unit unit = Unit::Millimeter) const;
819
829 void saveSVG(const char * filename, double pageWidth, double pageHeight, double margin = 0.0, Unit unit = Unit::Millimeter) const;
830
840 void saveSVG(std::ostream & out, double pageWidth, double pageHeight, double margin = 0.0, Unit unit = Unit::Millimeter) const;
841
850 void saveTikZ(const char * filename, PageSize size = PageSize::BoundingBox, double margin = 0.0) const;
851
860 void saveTikZ(std::ostream & out, PageSize size = PageSize::BoundingBox, double margin = 0.0) const;
861
870 void saveTikZ(const char * filename, double pageWidth, double pageHeight, double margin = 0.0) const;
871
880 void saveTikZ(std::ostream & out, double pageWidth, double pageHeight, double margin = 0.0) const;
881
887 inline const Color & penColor() const;
888
894 inline const Color & fillColor() const;
895
901 inline const Style & style() const;
902
906 static void enableLineWidthScaling();
907
911 static void disableLineWidthScaling();
912
916 static void setLineWidthScaling(bool);
917
924 static Rect pageRect(PageSize size, Unit unit);
925
926protected:
927 static double toMillimeter(double x, Unit unit);
928
932 struct State {
935 double fontSize;
936 State();
937 };
941};
942
943// Helper functions
944
962Group grid(Point topLeft, size_t columns, size_t rows, double width, double height, //
963 Color penColor, Color fillColor, //
964 double lineWidth, //
965 const LineStyle style = SolidStyle, //
966 const LineCap cap = ButtCap, //
967 const LineJoin join = MiterJoin);
968
981Group grid(Point topLeft, size_t columns, size_t rows, double width, double height, const Style & style = Style::defaultStyle());
982
989Group cross(Point p, const Style & style = Style::defaultStyle());
990
1002Group array(Point topLeft, const std::vector<Color> & colors, unsigned int columns, unsigned int rows, double pixelWidth, double pixelHeight = 0.0, double lineWidth = 0.0);
1003
1015Group framed(const Shape & shape, //
1016 double margin = 0.0, //
1017 const Color & penColor = Style::defaultPenColor(), //
1018 const Color & fillColor = Style::defaultFillColor(), //
1019 double lineWidth = Style::defaultLineWidth(), //
1020 LineStyle lineStyle = Style::defaultLineStyle(), //
1021 int sketchyCount = 0);
1022
1035Group tiling(const Shape & shape, Point topLeftCorner, int columns, int rows, double spacing = 0.0, LineWidthFlag lineWidthFlag = UseLineWidth);
1036
1047Group circled(const Shape & shape, double margin = 0.0, //
1048 Color penColor = Style::defaultPenColor(), Color fillColor = Style::defaultFillColor(), //
1049 double lineWidth = Style::defaultLineWidth(), LineStyle lineStyle = Style::defaultLineStyle());
1050
1057Polyline bezierControls(const Bezier & bezier, const Style & style = Style::defaultStyle());
1058
1059// Inline methods and functions
1060
1061inline void Board::clear(unsigned char red, unsigned char green, unsigned char blue)
1062{
1063 clear(Color(red, green, blue));
1064}
1065
1067{
1069 return *this;
1070}
1071
1073{
1074 _state.style.lineCap = cap;
1075 return *this;
1076}
1077
1079{
1080 _state.style.lineJoin = join;
1081 return *this;
1082}
1083
1084const Color & Board::penColor() const
1085{
1086 return _state.style.penColor;
1087}
1088
1090{
1091 return _state.style.fillColor;
1092}
1093
1094const Style & Board::style() const
1095{
1096 return _state.style;
1097}
1098
1099inline void Board::fillGouraudTriangle(const double x1, const double y1, const Color & color1, //
1100 const double x2, const double y2, const Color & color2, //
1101 const double x3, const double y3, const Color & color3, //
1102 unsigned char divisions)
1103{
1104 fillGouraudTriangle(Point(x1, y1), color1, Point(x2, y2), color2, Point(x3, y3), color3, divisions);
1105}
1106
1107inline void Board::fillGouraudTriangle(const double x1, const double y1, const float brightness1, //
1108 const double x2, const double y2, const float brightness2, //
1109 const double x3, const double y3, const float brightness3, //
1110 unsigned char divisions)
1111{
1112 fillGouraudTriangle(Point(x1, y1), brightness1, Point(x2, y2), brightness2, Point(x3, y3), brightness3, divisions);
1113}
1114
1115template <typename S> //
1116Board & Board::operator<<(const std::vector<S> & shapes)
1117{
1118 return static_cast<Board &>(ShapeList::operator<<(shapes));
1119}
1120
1121} // namespace LibBoard
1122
1123// TODO : Add rectangle tilling from a vector of colors (2A array) ( https://github.com/c-koi/libboard/pull/7 )
1124
1125#endif
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
Group class.
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
The Exception type. @copyright This source code is part of the Board project, a C++ library whose pur...
Group class.
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
The Point structure. @copyright This source code is part of the Board project, a C++ library whose pu...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
ShapeList class.
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
@copyright This source code is part of the Board project, a C++ library whose purpose is to allow sim...
Group text()
Definition board_font_text.cpp:17
Structure representing an RGB triple.
Definition Color.h:43
static const Color Null
Definition Color.h:155
Font
Definition PSFonts.h:51
Definition Board.h:55
Polyline bezierControls(const Bezier &bezier, const Style &style=Style::defaultStyle())
Produce a polyline of the Biezer vertices and control points.
Definition Board.cpp:1032
LineStyle
Definition Style.h:47
@ SolidStyle
Definition Style.h:48
Unit
Length units.
Definition Globals.h:81
PageSize
Page sizes.
Definition Globals.h:59
@ BoundingBox
No page size. The bounding box of the drawing will be used.
Group tiling(const Shape &shape, Point topLeftCorner, int columns, int rows, double spacing=0.0, LineWidthFlag lineWidthFlag=UseLineWidth)
Definition Board.cpp:995
Group cross(Point p, const Style &style=Style::defaultStyle())
Definition Board.cpp:1024
Group array(Point topLeft, const std::vector< Color > &colors, unsigned int columns, unsigned int rows, double pixelWidth, double pixelHeight=0.0, double lineWidth=0.0)
Produce a rectangular color matrix.
Definition Board.cpp:1053
LineCap
Definition Style.h:35
@ ButtCap
Definition Style.h:36
Group framed(const Shape &shape, double margin=0.0, const Color &penColor=Style::defaultPenColor(), const Color &fillColor=Style::defaultFillColor(), double lineWidth=Style::defaultLineWidth(), LineStyle lineStyle=Style::defaultLineStyle(), int sketchyCount=0)
Surround a shape with a rectangular frame.
Definition Board.cpp:1081
Group circled(const Shape &shape, double margin=0.0, Color penColor=Style::defaultPenColor(), Color fillColor=Style::defaultFillColor(), double lineWidth=Style::defaultLineWidth(), LineStyle lineStyle=Style::defaultLineStyle())
Surround a shape with a cirle.
Definition Board.cpp:1015
Group grid(Point topLeft, size_t columns, size_t rows, double width, double height, Color penColor, Color fillColor, double lineWidth, const LineStyle style=SolidStyle, const LineCap cap=ButtCap, const LineJoin join=MiterJoin)
Definition Board.cpp:967
LineWidthFlag
Definition Style.h:57
@ UseLineWidth
Definition Style.h:59
LineJoin
Definition Style.h:41
@ MiterJoin
Definition Style.h:42
ExtremityType
The ExtremityType enum.
Definition Arrow.h:44
A Bezier curve described by two series of 2D points (curve points and control points).
Definition Bezier.h:42
Definition Board.h:932
State()
Definition Board.cpp:97
double fontSize
Definition Board.h:935
Fonts::Font font
Definition Board.h:934
Style style
Definition Board.h:933
Class for EPS, FIG or SVG drawings.
Definition Board.h:61
Board & translate(double dx, double dy) override
Translate the board by a given vector.
Definition Board.cpp:169
Board & setFont(const Fonts::Font font, double fontSize)
Definition Board.cpp:280
static Rect pageRect(PageSize size, Unit unit)
Standard page size as a rectangle Rect(0,0,width,height)
Definition Board.cpp:896
void setClippingRectangle(double x, double y, double width, double height)
Definition Board.cpp:455
void fillPolyline(const std::vector< Point > &points)
Definition Board.cpp:377
State _state
Definition Board.h:938
Board & setFillColorHSV(float hue, float saturation, float value, float alpha=1.0f)
Definition Board.cpp:262
void setBackgroundColor(const Color &color)
Definition Board.cpp:293
Color _backgroundColor
Definition Board.h:939
void fillCircle(double x, double y, double radius)
Definition Board.cpp:352
AspectRatio
The AspectRatio enum.
Definition Board.h:70
@ Preserve
Preserve aspect ratio, e.g. for resize operations.
@ Ignore
Ignore aspect ratio, e.g. for resize operations.
void drawLine(double x1, double y1, double x2, double y2)
Definition Board.cpp:303
Board & setFontSize(double fontSize)
Definition Board.cpp:287
void fillGouraudTriangle(const Point &p1, const Color &color1, const Point &p2, const Color &color2, const Point &p3, const Color &color3, unsigned char divisions=3)
Definition Board.cpp:406
const Color & penColor() const
penColor
Definition Board.h:1084
void drawBoundingBox(LineWidthFlag lineWidthFlag)
Definition Board.cpp:448
void setClippingPath(const std::vector< Point > &points)
Definition Board.cpp:469
Board & operator<<(const Shape &shape)
Definition Board.cpp:126
void addDuplicates(const Shape &shape, std::size_t times, double dx, double dy, double scale=1.0)
Definition Board.cpp:491
void drawRectangle(double left, double top, double width, double height)
Definition Board.cpp:323
static void enableLineWidthScaling()
Definition Board.cpp:134
Board & scale(double sx, double sy) override
Scale the board in the x and y direction.
Definition Board.cpp:176
static double toMillimeter(double x, Unit unit)
Definition Board.cpp:914
void save(const char *filename, PageSize size=PageSize::BoundingBox, double margin=0.0, Unit unit=Unit::Millimeter) const
Definition Board.cpp:958
Board & setLineCap(LineCap cap)
Definition Board.h:1072
Board & setPenColor(const Color &color)
Definition Board.cpp:244
Board & operator=(const Board &other)
Definition Board.cpp:108
void saveFIG(const char *filename, PageSize size=PageSize::BoundingBox, double margin=0.0, Unit unit=Unit::Millimeter) const
Definition Board.cpp:636
void drawArrow(double x1, double y1, double x2, double y2, Arrow::ExtremityType type=Arrow::ExtremityType::Plain)
Definition Board.cpp:313
void fillRectangle(double left, double top, double width, double height)
Definition Board.cpp:333
static void setLineWidthScaling(bool)
Definition Board.cpp:144
Board & setPenColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition Board.cpp:232
void fillEllipse(double x, double y, double xRadius, double yRadius)
Definition Board.cpp:362
static const double Degree
Definition Board.h:64
void drawDot(double x, double y)
Definition Board.cpp:298
void drawText(double x, double y, const char *text)
Definition Board.cpp:428
~Board() override
Definition Board.cpp:132
Board & setPenColorRGBf(float red, float green, float blue, float alpha=1.0f)
Definition Board.cpp:238
void saveEPS(std::ostream &out, PageSize size=PageSize::BoundingBox, double margin=0.0, Unit unit=Unit::Millimeter, const std::string &title=std::string()) const
Definition Board.cpp:539
Board & setFillColor(const Color &color)
Definition Board.cpp:268
void fillTriangle(double x1, double y1, double x2, double y2, double x3, double y3)
Definition Board.cpp:394
void saveTikZ(const char *filename, PageSize size=PageSize::BoundingBox, double margin=0.0) const
Definition Board.cpp:856
Board translated(double dx, double dy)
Definition Board.cpp:217
const Style & style() const
Definition Board.h:1094
void drawPolyline(const std::vector< Point > &points)
Definition Board.cpp:367
Board scaled(double sx, double sy)
Definition Board.cpp:222
Board & rotate(double angle, const Point &center) override
Rotate the board by an angle around a point.
Definition Board.cpp:155
void drawTriangle(double x1, double y1, double x2, double y2, double x3, double y3)
Definition Board.cpp:382
Board & setFillColorRGBi(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha=255)
Definition Board.cpp:250
void drawEllipse(double x, double y, double xRadius, double yRadius)
Definition Board.cpp:357
const Color & fillColor() const
fillColor
Definition Board.h:1089
Board & setLineWidth(double width)
Definition Board.cpp:274
Board & setLineStyle(LineStyle style)
Definition Board.h:1066
void drawCircle(double x, double y, double radius)
Definition Board.cpp:347
Board & setFillColorRGBf(float red, float green, float blue, float alpha=1.0f)
Definition Board.cpp:256
Board & setLineJoin(LineJoin join)
Definition Board.h:1078
Path _clippingPath
Definition Board.h:940
static void disableLineWidthScaling()
Definition Board.cpp:139
void drawClosedPolyline(const std::vector< Point > &points)
Definition Board.cpp:372
void saveSVG(const char *filename, PageSize size=PageSize::BoundingBox, double margin=0.0, Unit unit=Unit::Millimeter) const
Definition Board.cpp:765
Board rotated(double angle, const Point &center)
Definition Board.cpp:207
A group of shapes. A group is basically a ShapeList except that when rendered in either an SVG of a F...
Definition Group.h:40
A path, according to Postscript and SVG definition.
Definition Path.h:45
Struct representing a 2D point.
Definition Point.h:42
A polygonal line described by a series of 2D points.
Definition Polyline.h:38
Struct representing a rectangle on the plane.
Definition Rect.h:40
A group of shapes.
Definition ShapeList.h:47
Shape & top()
Definition ShapeList.cpp:438
std::size_t size() const
The number of shapes in the list (at top level).
Definition ShapeList.h:669
ShapeList & operator<<(const Shape &shape)
Definition ShapeList.cpp:150
ShapeList & clear()
Definition ShapeList.cpp:90
Abstract structure for a 2D shape.
Definition Shape.h:64
virtual Point center(LineWidthFlag lineWidthFlag=IgnoreLineWidth) const
Definition Shape.cpp:59
Definition Style.h:69
LineJoin lineJoin
Definition Style.h:75
Color fillColor
Definition Style.h:71
LineStyle lineStyle
Definition Style.h:73
Color penColor
Definition Style.h:70
static const Color & defaultPenColor()
defaultPenColor
Definition Style.h:279
static const Color & defaultFillColor()
defaultFillColor
Definition Style.h:284
static const double & defaultLineWidth()
defaultLineWidth
Definition Style.h:274
static const Style & defaultStyle()
defaultStyle
Definition Style.h:269
static const LineStyle & defaultLineStyle()
defaultLineStyle
Definition Style.h:289
LineCap lineCap
Definition Style.h:74