MeshLib C++ Docs
Loading...
Searching...
No Matches
MRPdf.h
Go to the documentation of this file.
1#pragma once
2
3#include "config.h"
4#ifndef MRIOEXTRAS_NO_PDF
5#include "exports.h"
6#include "MRMesh/MRMeshFwd.h"
7#include "MRMesh/MRVector2.h"
8#include "MRMesh/MRBox.h"
9#include "MRMesh/MRColor.h"
10#include "MRMesh/MRImage.h"
11#include "MRMesh/MRExpected.h"
12
13#include <filesystem>
14#include <vector>
15#include <variant>
16#include <optional>
17
18namespace MR
19{
22
23
44
45using PdfGeneralFont = std::variant<PdfBuildinFont, std::filesystem::path>;
46
66
70class Pdf
71{
72public:
74 MRIOEXTRAS_API Pdf( const PdfParameters& params = PdfParameters() );
75 MRIOEXTRAS_API Pdf( Pdf&& other ) noexcept;
76 MRIOEXTRAS_API Pdf& operator=( Pdf other ) noexcept;
78 MRIOEXTRAS_API ~Pdf();
79
81 {
82 Left,
83 Center,
84 Right
85 };
86
106 MRIOEXTRAS_API void addText( const std::string& text, bool isTitle = false );
107 MRIOEXTRAS_API void addText( const std::string& text, const TextParams& params );
109 MRIOEXTRAS_API float getTextWidth( const std::string& text, const TextParams& params );
110
117 MRIOEXTRAS_API void addTable( const std::vector<std::pair<std::string, float>>& table );
118
120 {
122 std::string rangeMin;
123 std::string rangeMax;
124 std::string percent;
125 };
126 MRIOEXTRAS_API void addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats );
127
158 MRIOEXTRAS_API void addImageFromFile( const std::filesystem::path& imagePath, const ImageParams& params );
159 MRIOEXTRAS_API void addImage( const Image& image, const ImageParams& params );
160
162 MRIOEXTRAS_API void newPage();
164 void setNewPageAction( std::function<void(Pdf&)> action ) { newPageAction_ = action; }
165
167 MRIOEXTRAS_API void saveToFile( const std::filesystem::path& documentPath );
168
169 void setCursorPosX( float posX ) { cursorX_ = posX; }
170 void setCursorPosY( float posY ) { cursorY_ = posY; }
171 float getCursorPosX() const { return cursorX_; }
172 float getCursorPosY() const { return cursorY_; }
173
174 MRIOEXTRAS_API Vector2f getPageSize() const;
175 MRIOEXTRAS_API Box2f getPageWorkArea() const;
176
178 MRIOEXTRAS_API operator bool() const;
179
180
183 struct Cell {
184 struct Empty {};
185 using Value = std::variant<int, float, bool, std::string, Empty>;
187
188 Cell() : data( Empty() ) {}
189
190 template<typename T>
191 Cell( const T& value ) : data( value ) {}
192
195 MRIOEXTRAS_API std::string toString( const std::string& fmtStr = "{}" ) const;
196 };
198 MRIOEXTRAS_API void newTable( int columnCount );
200 MRIOEXTRAS_API Expected<void> setTableColumnWidths( const std::vector<float>& widths );
202 MRIOEXTRAS_API Expected<void> addTableTitles( const std::vector<std::string>& titles );
204 MRIOEXTRAS_API Expected<void> setColumnValuesFormat( const std::vector<std::string>& formats );
206 MRIOEXTRAS_API Expected<void> addRow( const std::vector<Cell>& cells );
209 MRIOEXTRAS_API float getTableTextWidth( const std::string& text );
211 {
212 std::optional<Color> colorText;
213 std::optional<Color> colorCellBg;
214 std::optional<Color> colorCellBorder;
215 std::optional<std::string> text;
216 };
217 using TableCustomRule = std::function<CellCustomParams( int row, int column, const std::string& cellValueText)>;
219 void setTableCustomRule( TableCustomRule rule ) { tableCustomRule_ = rule; }
220
222
225 MRIOEXTRAS_API void drawTextInRect( const std::string& text, const Box2f& rect, const TextParams& params );
226
235 MRIOEXTRAS_API void drawTextCell( const std::string& text, const TextCellParams& params );
236
238 MRIOEXTRAS_API void drawRect( const Box2f& rect, const Color& fillColor, const Color& strokeColor );
239private:
240
242 void reset_();
243
245 std::vector<float> calcTextLineWidths_( const std::string& text, float width, const TextParams& params );
246
247 struct HPDF_Image_Wraper;
249 void addImage_( const HPDF_Image_Wraper& image, const ImageParams& params );
250
251 struct State;
252 std::unique_ptr<State> state_;
253
254 PdfParameters params_;
255
256 std::function<void( Pdf& )> newPageAction_;
257
258 float cursorX_ = 0;
259 float cursorY_ = 0;
260
261 bool checkDocument_( const std::string& logAction );
262 void moveCursorToNewLine_();
263 void checkAndCreateNewPageIfNeed_( float height );
264
266 int rowCounter_ = 0;
267 struct ColumnInfo
268 {
269 float width = 100;
270 std::string valueFormat = "{}";
271 };
272 std::vector<ColumnInfo> columnsInfo_;
273 TableCustomRule tableCustomRule_;
274 struct TableGeneralParams
275 {
277 Color colorTitleBg{ 42, 102, 246 };
278
280 Color colorCellBg1{ 170, 194, 251 };
281 Color colorCellBg2{ 212, 224, 253 };
282
284
285 float fontSize = 12.f;
286 } tableParams_;
287};
288
289}
290#endif
Definition MRPdf.h:71
auto width(const Box< V > &box)
returns size along x axis
Definition MRBox.h:354
auto height(const Box< V > &box)
returns size along y axis
Definition MRBox.h:361
std::string caption
caption if not empty - add caption under marks (if exist) or image.
Definition MRPdf.h:136
MRIOEXTRAS_API void drawTextCell(const std::string &text, const TextCellParams &params)
AlignmentHorizontal
Definition MRPdf.h:81
static constexpr Color transparent() noexcept
Definition MRColor.h:37
std::string rangeMin
Definition MRPdf.h:122
std::variant< int, float, bool, std::string, Empty > Value
Definition MRPdf.h:185
float fontSize
Definition MRPdf.h:92
float getCursorPosY() const
Definition MRPdf.h:172
MRIOEXTRAS_API Pdf & operator=(Pdf other) noexcept
Color colorBackground
Definition MRPdf.h:233
std::string valueFormat
Definition MRPdf.h:270
MRIOEXTRAS_API void addText(const std::string &text, const TextParams &params)
PdfBuildinFont
Definition MRPdf.h:27
Cell(const T &value)
Definition MRPdf.h:191
MRIOEXTRAS_API void newTable(int columnCount)
set up new table (clear table customization, reset parameters to default values)
std::string rangeMax
Definition MRPdf.h:123
MRIOEXTRAS_API void drawRect(const Box2f &rect, const Color &fillColor, const Color &strokeColor)
draw rect (filled with border)
float getCursorPosX() const
Definition MRPdf.h:171
MRIOEXTRAS_API void addText(const std::string &text, bool isTitle=false)
void setCursorPosX(float posX)
Definition MRPdf.h:169
enum MR::Pdf::ImageParams::UniformScale uniformScale
float fontSize
Definition MRPdf.h:285
void setTableCustomRule(TableCustomRule rule)
add rule to customize table cells
Definition MRPdf.h:219
Color colorCellBg2
Definition MRPdf.h:281
MRIOEXTRAS_API void addImageFromFile(const std::filesystem::path &imagePath, const ImageParams &params)
Add image from file in current cursor position. If image bigger than page size, autoscale image to pa...
std::variant< PdfBuildinFont, std::filesystem::path > PdfGeneralFont
Definition MRPdf.h:45
enum MR::Pdf::ImageParams::AlignmentVertical alignmentVertical
MRIOEXTRAS_API Expected< void > setColumnValuesFormat(const std::vector< std::string > &formats)
set format for conversion values to string for each column
Value data
Definition MRPdf.h:186
Color colorText
Definition MRPdf.h:95
Color colorCellText
Definition MRPdf.h:279
static constexpr Color black() noexcept
Definition MRColor.h:29
MRIOEXTRAS_API float getTextWidth(const std::string &text, const TextParams &params)
return text width
float titleSize
Definition MRPdf.h:52
Vector2f size
Definition MRPdf.h:134
Color colorTitleText
Definition MRPdf.h:276
MRIOEXTRAS_API void newPage()
Add new pageand move cursor on it.
tl::expected< T, E > Expected
Definition MRExpected.h:31
std::function< CellCustomParams(int row, int column, const std::string &cellValueText)> TableCustomRule
Definition MRPdf.h:217
std::optional< Color > colorCellBg
Definition MRPdf.h:213
MRIOEXTRAS_API void addPaletteStatsTable(const std::vector< PaletteRowStats > &paletteStats)
Cell()
Definition MRPdf.h:188
Color colorCellBg1
Definition MRPdf.h:280
bool underline
Definition MRPdf.h:96
MRIOEXTRAS_API Box2f getPageWorkArea() const
float textSize
Definition MRPdf.h:53
PdfGeneralFont defaultFont
Font name.
Definition MRPdf.h:58
PdfGeneralFont defaultFontBold
Definition MRPdf.h:59
AlignmentHorizontal alignmentHorizontal
Definition MRPdf.h:151
Color colorLines
Definition MRPdf.h:283
std::optional< Color > colorText
Definition MRPdf.h:212
MRIOEXTRAS_API Pdf(const PdfParameters &params=PdfParameters())
Ctor. Create a document, but not a page. To create a new page use newPage() method.
std::optional< Color > colorCellBorder
Definition MRPdf.h:214
PdfGeneralFont tableFont
Definition MRPdf.h:63
MRIOEXTRAS_API Expected< void > addRow(const std::vector< Cell > &cells)
add in pdf table row with values
float width
Definition MRPdf.h:269
MRIOEXTRAS_API void addImage(const Image &image, const ImageParams &params)
Color colorBorder
Definition MRPdf.h:232
MRIOEXTRAS_API void saveToFile(const std::filesystem::path &documentPath)
Save document to file.
std::optional< std::string > text
Definition MRPdf.h:215
PdfGeneralFont tableFontBold
Definition MRPdf.h:64
MRIOEXTRAS_API void addTable(const std::vector< std::pair< std::string, float > > &table)
void setCursorPosY(float posY)
Definition MRPdf.h:170
MRIOEXTRAS_API void drawTextInRect(const std::string &text, const Box2f &rect, const TextParams &params)
basic drawing methods without automatic cursor position control
UniformScale
set height to keep same scale as width scale
Definition MRPdf.h:139
Box2f rect
Definition MRPdf.h:231
MRIOEXTRAS_API Expected< void > setTableColumnWidths(const std::vector< float > &widths)
set table column widths
AlignmentVertical
Definition MRPdf.h:146
Color color
Definition MRPdf.h:121
MRIOEXTRAS_API float getTableTextWidth(const std::string &text)
MRIOEXTRAS_API Vector2f getPageSize() const
Color colorTitleBg
Definition MRPdf.h:277
MRIOEXTRAS_API std::string toString(const std::string &fmtStr="{}") const
AlignmentHorizontal alignment
Definition MRPdf.h:94
TextParams textParams
Definition MRPdf.h:229
PdfGeneralFont fontName
Definition MRPdf.h:90
MRIOEXTRAS_API ~Pdf()
Dtor.
void setNewPageAction(std::function< void(Pdf &)> action)
set function to customize new page after creation
Definition MRPdf.h:164
std::string percent
Definition MRPdf.h:124
MRIOEXTRAS_API Pdf(Pdf &&other) noexcept
MRIOEXTRAS_API Expected< void > addTableTitles(const std::vector< std::string > &titles)
add in pdf table row with titles
static constexpr Color white() noexcept
Definition MRColor.h:28
@ other
Angle, normally float. Measure in radians.
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRColor.h:12
Definition MRImage.h:19
Parameters of document style.
Definition MRPdf.h:51
Definition MRPdf.h:211
Definition MRPdf.h:184
Definition MRPdf.h:183
Parameters to adding image from file.
Definition MRPdf.h:130
Definition MRPdf.h:120
Definition MRPdf.h:228
parameters to drawing text
Definition MRPdf.h:89