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{
20
41
42using PdfGeneralFont = std::variant<PdfBuildinFont, std::filesystem::path>;
43
63
67class Pdf
68{
69public:
71 MRIOEXTRAS_API Pdf( const PdfParameters& params = PdfParameters() );
72 MRIOEXTRAS_API Pdf( Pdf&& other ) noexcept;
73 MRIOEXTRAS_API Pdf& operator=( Pdf other ) noexcept; // Sic, passing by value.
75 MRIOEXTRAS_API ~Pdf();
76
78 {
79 Left,
80 Center,
81 Right
82 };
83
84 // parameters to drawing text
103 MRIOEXTRAS_API void addText( const std::string& text, bool isTitle = false );
104 MRIOEXTRAS_API void addText( const std::string& text, const TextParams& params );
106 MRIOEXTRAS_API float getTextWidth( const std::string& text, const TextParams& params );
107
114 MRIOEXTRAS_API void addTable( const std::vector<std::pair<std::string, float>>& table );
115
117 {
119 std::string rangeMin;
120 std::string rangeMax;
121 std::string percent;
122 };
123 MRIOEXTRAS_API void addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats );
124
155 MRIOEXTRAS_API void addImageFromFile( const std::filesystem::path& imagePath, const ImageParams& params );
156 MRIOEXTRAS_API void addImage( const Image& image, const ImageParams& params );
157
159 MRIOEXTRAS_API void newPage();
161 void setNewPageAction( std::function<void(Pdf&)> action ) { newPageAction_ = action; }
162
164 MRIOEXTRAS_API void saveToFile( const std::filesystem::path& documentPath );
165
166 void setCursorPosX( float posX ) { cursorX_ = posX; }
167 void setCursorPosY( float posY ) { cursorY_ = posY; }
168 float getCursorPosX() const { return cursorX_; }
169 float getCursorPosY() const { return cursorY_; }
170
171 MRIOEXTRAS_API Vector2f getPageSize() const;
172 MRIOEXTRAS_API Box2f getPageWorkArea() const;
173
175 MRIOEXTRAS_API operator bool() const;
176
177
178 // Table part
179 // class to convert values to string with set format
180 struct Cell {
181 struct Empty {};
182 using Value = std::variant<int, float, bool, std::string, Empty>;
184
185 Cell() : data( Empty() ) {}
186
187 template<typename T>
188 Cell( const T& value ) : data( value ) {}
189
190 // get strang from contained value
191 // \param fmtStr format string like fmt::format
192 MRIOEXTRAS_API std::string toString( const std::string& fmtStr = "{}" ) const;
193 };
194 // set up new table (clear table customization, reset parameters to default values)
195 MRIOEXTRAS_API void newTable( int columnCount );
196 // set table column widths
197 MRIOEXTRAS_API Expected<void> setTableColumnWidths( const std::vector<float>& widths );
198 // add in pdf table row with titles
199 MRIOEXTRAS_API Expected<void> addTableTitles( const std::vector<std::string>& titles );
200 // set format for conversion values to string for each column
201 MRIOEXTRAS_API Expected<void> setColumnValuesFormat( const std::vector<std::string>& formats );
202 // add in pdf table row with values
203 MRIOEXTRAS_API Expected<void> addRow( const std::vector<Cell>& cells );
204 // parameters to customization table cell
205 // return text width (for table font parameters)
206 MRIOEXTRAS_API float getTableTextWidth( const std::string& text );
208 {
209 std::optional<Color> colorText;
210 std::optional<Color> colorCellBg;
211 std::optional<Color> colorCellBorder;
212 std::optional<std::string> text;
213 };
214 using TableCustomRule = std::function<CellCustomParams( int row, int column, const std::string& cellValueText)>;
215 // add rule to customize table cells
216 void setTableCustomRule( TableCustomRule rule ) { tableCustomRule_ = rule; }
217
218 // basic drawing methods without automatic cursor position control
219
220 // draw text in specific rect on page
221 // text will be cropped by rect
222 MRIOEXTRAS_API void drawTextInRect( const std::string& text, const Box2f& rect, const TextParams& params );
223
232 MRIOEXTRAS_API void drawTextCell( const std::string& text, const TextCellParams& params );
233
234private:
235 // draw rect (filled with border)
236 MRIOEXTRAS_API void drawRect_( const Box2f& rect, const Color& fillColor, const Color& strokeColor );
237
238 // close pdf document without saving. After this impossible add anything in document.
239 void reset_();
240
241 // calculate the width of the lines, taking into account automatic hyphenation
242 std::vector<float> calcTextLineWidths_( const std::string& text, float width, const TextParams& params );
243
244 struct HPDF_Image_Wraper;
245 // base method to add hpdf image in current cursor position.
246 void addImage_( const HPDF_Image_Wraper& image, const ImageParams& params );
247
248 struct State;
249 std::unique_ptr<State> state_;
250
251 PdfParameters params_;
252
253 std::function<void( Pdf& )> newPageAction_;
254
255 float cursorX_ = 0;
256 float cursorY_ = 0;
257
258 bool checkDocument_( const std::string& logAction );
259 void moveCursorToNewLine_();
260 void checkAndCreateNewPageIfNeed_( float height );
261
262 // table parts
263 int rowCounter_ = 0;
264 struct ColumnInfo
265 {
266 float width = 100;
267 std::string valueFormat = "{}";
268 };
269 std::vector<ColumnInfo> columnsInfo_;
270 TableCustomRule tableCustomRule_;
271 struct TableGeneralParams
272 {
273 Color colorTitleText = Color::white();
274 Color colorTitleBg{ 42, 102, 246 };
275
276 Color colorCellText = Color::black();
277 Color colorCellBg1{ 170, 194, 251 };
278 Color colorCellBg2{ 212, 224, 253 };
279
280 Color colorLines = Color::white();
281
282 float fontSize = 12.f;
283 } tableParams_;
284};
285
286}
287#endif
Definition MRPdf.h:68
MRIOEXTRAS_API void drawTextCell(const std::string &text, const TextCellParams &params)
AlignmentHorizontal
Definition MRPdf.h:78
float getCursorPosY() const
Definition MRPdf.h:169
MRIOEXTRAS_API Pdf & operator=(Pdf other) noexcept
MRIOEXTRAS_API void addText(const std::string &text, const TextParams &params)
MRIOEXTRAS_API void newTable(int columnCount)
float getCursorPosX() const
Definition MRPdf.h:168
MRIOEXTRAS_API void addText(const std::string &text, bool isTitle=false)
void setCursorPosX(float posX)
Definition MRPdf.h:166
void setTableCustomRule(TableCustomRule rule)
Definition MRPdf.h:216
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...
MRIOEXTRAS_API Expected< void > setColumnValuesFormat(const std::vector< std::string > &formats)
MRIOEXTRAS_API float getTextWidth(const std::string &text, const TextParams &params)
return text width
MRIOEXTRAS_API void newPage()
Add new pageand move cursor on it.
std::function< CellCustomParams(int row, int column, const std::string &cellValueText)> TableCustomRule
Definition MRPdf.h:214
MRIOEXTRAS_API void addPaletteStatsTable(const std::vector< PaletteRowStats > &paletteStats)
MRIOEXTRAS_API Box2f getPageWorkArea() const
MRIOEXTRAS_API Pdf(const PdfParameters &params=PdfParameters())
Ctor. Create a document, but not a page. To create a new page use newPage() method.
MRIOEXTRAS_API Expected< void > addRow(const std::vector< Cell > &cells)
MRIOEXTRAS_API void addImage(const Image &image, const ImageParams &params)
MRIOEXTRAS_API void saveToFile(const std::filesystem::path &documentPath)
Save document to file.
MRIOEXTRAS_API void addTable(const std::vector< std::pair< std::string, float > > &table)
void setCursorPosY(float posY)
Definition MRPdf.h:167
MRIOEXTRAS_API void drawTextInRect(const std::string &text, const Box2f &rect, const TextParams &params)
MRIOEXTRAS_API Expected< void > setTableColumnWidths(const std::vector< float > &widths)
MRIOEXTRAS_API float getTableTextWidth(const std::string &text)
MRIOEXTRAS_API Vector2f getPageSize() const
MRIOEXTRAS_API ~Pdf()
Dtor.
void setNewPageAction(std::function< void(Pdf &)> action)
set function to customize new page after creation
Definition MRPdf.h:161
MRIOEXTRAS_API Pdf(Pdf &&other) noexcept
MRIOEXTRAS_API Expected< void > addTableTitles(const std::vector< std::string > &titles)
auto width(const Box< V > &box)
returns size along x axis
Definition MRMesh/MRBox.h:341
auto height(const Box< V > &box)
returns size along y axis
Definition MRMesh/MRBox.h:348
Definition MRCameraOrientationPlugin.h:8
PdfBuildinFont
Definition MRPdf.h:24
std::variant< PdfBuildinFont, std::filesystem::path > PdfGeneralFont
Definition MRPdf.h:42
tl::expected< T, E > Expected
Definition MRExpected.h:25
Definition MRMesh/MRColor.h:9
static constexpr Color transparent() noexcept
Definition MRMesh/MRColor.h:34
static constexpr Color black() noexcept
Definition MRMesh/MRColor.h:26
static constexpr Color white() noexcept
Definition MRMesh/MRColor.h:25
Definition MRImage.h:15
Parameters of document style.
Definition MRPdf.h:48
float titleSize
Definition MRPdf.h:49
float textSize
Definition MRPdf.h:50
PdfGeneralFont defaultFont
Font name.
Definition MRPdf.h:55
PdfGeneralFont defaultFontBold
Definition MRPdf.h:56
PdfGeneralFont tableFont
Definition MRPdf.h:60
PdfGeneralFont tableFontBold
Definition MRPdf.h:61
Definition MRPdf.h:208
std::optional< Color > colorCellBg
Definition MRPdf.h:210
std::optional< Color > colorText
Definition MRPdf.h:209
std::optional< Color > colorCellBorder
Definition MRPdf.h:211
std::optional< std::string > text
Definition MRPdf.h:212
Definition MRPdf.h:181
Definition MRPdf.h:180
std::variant< int, float, bool, std::string, Empty > Value
Definition MRPdf.h:182
Cell(const T &value)
Definition MRPdf.h:188
Value data
Definition MRPdf.h:183
Cell()
Definition MRPdf.h:185
MRIOEXTRAS_API std::string toString(const std::string &fmtStr="{}") const
Parameters to adding image from file.
Definition MRPdf.h:127
std::string caption
caption if not empty - add caption under marks (if exist) or image.
Definition MRPdf.h:133
enum MR::Pdf::ImageParams::UniformScale uniformScale
enum MR::Pdf::ImageParams::AlignmentVertical alignmentVertical
Vector2f size
Definition MRPdf.h:131
AlignmentHorizontal alignmentHorizontal
Definition MRPdf.h:148
UniformScale
set height to keep same scale as width scale
Definition MRPdf.h:136
AlignmentVertical
Definition MRPdf.h:143
Definition MRPdf.h:117
std::string rangeMin
Definition MRPdf.h:119
std::string rangeMax
Definition MRPdf.h:120
Color color
Definition MRPdf.h:118
std::string percent
Definition MRPdf.h:121
Definition MRPdf.h:225
Color colorBackground
Definition MRPdf.h:230
Color colorBorder
Definition MRPdf.h:229
Box2f rect
Definition MRPdf.h:228
TextParams textParams
Definition MRPdf.h:226
Definition MRPdf.h:86
float fontSize
Definition MRPdf.h:89
Color colorText
Definition MRPdf.h:92
bool underline
Definition MRPdf.h:93
AlignmentHorizontal alignment
Definition MRPdf.h:91
PdfGeneralFont fontName
Definition MRPdf.h:87