Skip to content

Commit f43c97c

Browse files
committed
Merge remote-tracking branch 'origin/master' into Unify_ctrl/supper_hotkeys_usage
2 parents bb97abe + 51e8a7e commit f43c97c

File tree

3 files changed

+77
-25
lines changed

3 files changed

+77
-25
lines changed

source/MRIOExtras/MRPdf.cpp

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ namespace
190190
// size of A4 page in pixels (uses 72 PPI)
191191
// https://www.papersizes.org/a-sizes-in-pixels.htm
192192
// TODO need get this value from PoDoFo
193+
// true value is 595.276 x 841.89
194+
// https://github.com/libharu/libharu/wiki/API%3A-Page#user-content-HPDF_Page_SetSize
193195
constexpr HPDF_REAL pageWidth = 595.;
194196
constexpr HPDF_REAL pageHeight = 842.;
195-
constexpr HPDF_REAL scaleFactor = static_cast<HPDF_REAL>(17. / 6.); // ~2.8(3)
197+
constexpr HPDF_REAL scaleFactor = static_cast<HPDF_REAL>(17. / 6.); // ~2.8(3) conversion mm to points (pixels)
196198

197199
constexpr HPDF_REAL borderFieldLeft = 20 * scaleFactor;
198200
constexpr HPDF_REAL borderFieldRight = pageWidth - 10 * scaleFactor;
@@ -481,9 +483,12 @@ void Pdf::addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats
481483

482484
void Pdf::addImageFromFile( const std::filesystem::path& imagePath, const ImageParams& params )
483485
{
484-
if ( !state_->document )
486+
if ( !checkDocument_( "add image" ) )
487+
return;
488+
489+
if ( imagePath.empty() )
485490
{
486-
spdlog::warn( "Can't add image to pdf page: no valid document" );
491+
spdlog::warn( "Pdf: can't add image from file: empty path." );
487492
return;
488493
}
489494

@@ -495,24 +500,47 @@ void Pdf::addImageFromFile( const std::filesystem::path& imagePath, const ImageP
495500
}
496501

497502
const HPDF_REAL additionalHeight = labelHeight * !params.caption.empty();
498-
HPDF_REAL imageWidth = params.size.x;
499-
if ( imageWidth == 0.f )
500-
imageWidth = (HPDF_REAL)MR_HPDF_CHECK_ERROR( HPDF_Image_GetWidth( pdfImage ) );
501-
else if ( imageWidth < 0.f )
502-
imageWidth = borderFieldRight - cursorX_;
503-
HPDF_REAL imageHeight = params.size.y;
504-
if ( params.uniformScaleFromWidth )
505-
imageHeight = imageWidth * MR_HPDF_CHECK_ERROR( HPDF_Image_GetHeight( pdfImage ) ) / MR_HPDF_CHECK_ERROR( HPDF_Image_GetWidth( pdfImage ) );
506-
else if ( imageHeight == 0.f )
507-
imageHeight = (HPDF_REAL)MR_HPDF_CHECK_ERROR( HPDF_Image_GetHeight( pdfImage ) );
508-
else if ( imageHeight < 0.f )
509-
imageHeight = cursorY_ - borderFieldBottom - additionalHeight;
503+
HPDF_REAL width = params.size.x;
504+
HPDF_REAL height = params.size.y;
505+
HPDF_REAL realWidth = (HPDF_REAL) MR_HPDF_CHECK_ERROR( HPDF_Image_GetWidth( pdfImage ) );
506+
HPDF_REAL realHeight = ( HPDF_REAL )MR_HPDF_CHECK_ERROR( HPDF_Image_GetHeight( pdfImage ) );
507+
508+
if ( width <= 0.f )
509+
width = borderFieldRight - cursorX_;
510+
if ( height<= 0.f )
511+
height = cursorY_ - borderFieldBottom - additionalHeight;
512+
513+
HPDF_REAL imageWidth = width;
514+
HPDF_REAL imageHeight = height;
515+
516+
float posX = cursorX_;
517+
float posY = cursorY_;
518+
if ( params.uniformScale == ImageParams::UniformScale::FromWidth )
519+
{
520+
imageHeight = realHeight / realWidth * imageWidth;
521+
if ( params.alignmentVertical == ImageParams::AlignmentVertical::Top )
522+
posY -= imageHeight;
523+
else if ( params.alignmentVertical == ImageParams::AlignmentVertical::Bottom )
524+
posY -= height;
525+
else
526+
posY -= height / 2.f + imageHeight / 2.f;
527+
}
528+
else if ( params.uniformScale == ImageParams::UniformScale::FromHeight )
529+
{
530+
imageWidth = realWidth / realHeight * imageHeight;
531+
if ( params.alignmentHorizontal == AlignmentHorizontal::Right )
532+
posX = posX + width - imageWidth;
533+
else if ( params.alignmentHorizontal == AlignmentHorizontal::Center )
534+
posX = posX + width / 2.f - imageWidth / 2.f;
535+
536+
posY -= imageHeight;
537+
}
510538

511539
if ( cursorY_ - imageHeight - additionalHeight < borderFieldBottom )
512540
newPage();
513541

514-
cursorY_ -= imageHeight;
515-
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_DrawImage( state_->activePage, pdfImage, cursorX_, cursorY_, imageWidth, imageHeight ) );
542+
cursorY_ = posY;
543+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_DrawImage( state_->activePage, pdfImage, posX, posY, imageWidth, imageHeight ) );
516544

517545
if ( !params.caption.empty() )
518546
{
@@ -542,9 +570,13 @@ void Pdf::newPage()
542570
return;
543571
}
544572

573+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetSize( state_->activePage, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT ) );
574+
545575
cursorX_ = borderFieldLeft;
546576
cursorY_ = borderFieldTop;
547-
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetSize( state_->activePage, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT) );
577+
578+
if ( newPageAction_ )
579+
newPageAction_( *this );
548580
}
549581

550582
void Pdf::saveToFile( const std::filesystem::path& documentPath )

source/MRIOExtras/MRPdf.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,16 @@ class Pdf
7070
MRIOEXTRAS_API Pdf( const PdfParameters& params = PdfParameters() );
7171
MRIOEXTRAS_API Pdf( Pdf&& other ) noexcept;
7272
MRIOEXTRAS_API Pdf& operator=( Pdf other ) noexcept; // Sic, passing by value.
73-
/// Dtor. Automatically do close
73+
/// Dtor.
7474
MRIOEXTRAS_API ~Pdf();
7575

76+
enum class AlignmentHorizontal
77+
{
78+
Left,
79+
Center,
80+
Right
81+
};
82+
7683
/**
7784
* Add text block in current cursor position.
7885
* Move cursor.
@@ -110,17 +117,31 @@ class Pdf
110117
/// caption if not empty - add caption under marks (if exist) or image.
111118
std::string caption;
112119
/// set height to keep same scale as width scale
113-
bool uniformScaleFromWidth = false;
120+
enum class UniformScale
121+
{
122+
None,
123+
FromWidth,
124+
FromHeight
125+
} uniformScale = UniformScale::None;
126+
enum class AlignmentVertical
127+
{
128+
Top,
129+
Center,
130+
Bottom
131+
} alignmentVertical = AlignmentVertical::Top;
132+
AlignmentHorizontal alignmentHorizontal = AlignmentHorizontal::Left;
114133
};
115134
/**
116135
* @brief Add image from file in current cursor position.
117136
* If image bigger than page size, autoscale image to page size.
118137
* Move cursor.
119138
*/
120-
MRIOEXTRAS_API void addImageFromFile( const std::filesystem::path& imagePath, const ImageParams& params );
139+
MRIOEXTRAS_API void addImageFromFile( const std::filesystem::path& imagePath, const ImageParams& params );
121140

122141
/// Add new pageand move cursor on it
123142
MRIOEXTRAS_API void newPage();
143+
/// set function to customize new page after creation
144+
void setNewPageAction( std::function<void(Pdf&)> action ) { newPageAction_ = action; }
124145

125146
/// Save document to file
126147
MRIOEXTRAS_API void saveToFile( const std::filesystem::path& documentPath );
@@ -151,7 +172,6 @@ class Pdf
151172
// \param fmtStr format string like fmt::format
152173
MRIOEXTRAS_API std::string toString( const std::string& fmtStr = "{}" ) const;
153174
};
154-
155175
// set up new table (clear table customization, reset parameters to default values)
156176
MRIOEXTRAS_API void newTable( int columnCount );
157177
// set table column widths
@@ -203,6 +223,8 @@ class Pdf
203223

204224
PdfParameters params_;
205225

226+
std::function<void( Pdf& )> newPageAction_;
227+
206228
float cursorX_ = 0;
207229
float cursorY_ = 0;
208230

@@ -231,8 +253,6 @@ class Pdf
231253

232254
float fontSize = 12.f;
233255
} tableParams_;
234-
235-
236256
};
237257

238258
}

source/MRTest/MRPdfTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ TEST( MRMesh, Pdf )
4040
auto colorMapPath = pathFolder / std::filesystem::path( "color_map.png" );
4141
auto res = ImageSave::toAnySupportedFormat( { pixels, Vector2i( colorMapSizeX, colorMapSizeY ) }, colorMapPath );
4242

43-
pdfTest.addImageFromFile( colorMapPath, { {-1, 0}, "test image", true } );
43+
pdfTest.addImageFromFile( colorMapPath, { .size = {-1, 0}, .caption = "test image", .uniformScale = Pdf::ImageParams::UniformScale::FromWidth } );
4444
pdfTest.saveToFile( pathFolder / std::filesystem::path( "test.pdf" ) );
4545
}
4646

0 commit comments

Comments
 (0)