@@ -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
193195constexpr HPDF_REAL pageWidth = 595 .;
194196constexpr 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
197199constexpr HPDF_REAL borderFieldLeft = 20 * scaleFactor;
198200constexpr HPDF_REAL borderFieldRight = pageWidth - 10 * scaleFactor;
@@ -481,9 +483,12 @@ void Pdf::addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats
481483
482484void 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
550582void Pdf::saveToFile ( const std::filesystem::path& documentPath )
0 commit comments