Skip to content

Commit 52e5f05

Browse files
committed
fix
1 parent 3fd1b50 commit 52e5f05

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

source/MRIOExtras/MRPdf.cpp

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
#include <hpdf.h>
1717

18+
namespace
19+
{
20+
1821
std::string GetHpdfErrorDescription( HPDF_STATUS errorCode );
1922

2023
void pdfPrintError( const char* funcName, HPDF_Doc doc, HPDF_STATUS status )
@@ -32,24 +35,25 @@ void pdfPrintError( const char* funcName, HPDF_Doc doc, HPDF_STATUS status )
3235
}
3336
}
3437

35-
#define HPDF_CHECK_RES_STATUS( func_call ) \
38+
#define MR_HPDF_CHECK_RES_STATUS( ... ) \
3639
do { \
37-
HPDF_STATUS status = func_call; \
40+
HPDF_STATUS status = __VA_ARGS__; \
3841
if (status != HPDF_OK) { \
39-
pdfPrintError( #func_call, state_->document, status ); \
42+
pdfPrintError( #__VA_ARGS__, state_->document, status ); \
4043
} \
4144
} while (0)
4245

43-
#define HPDF_CHECK_ERROR( func_call ) \
44-
([&]() -> decltype( func_call ) { \
45-
auto&& result = ( func_call ); \
46+
#define MR_HPDF_CHECK_ERROR( ... ) \
47+
([&]() -> -> decltype(auto) { \
48+
decltype(auto) result = __VA_ARGS__; \
4649
HPDF_STATUS status = HPDF_GetError( state_->document ); \
4750
if (status != HPDF_OK) { \
48-
pdfPrintError( #func_call, state_->document, status ); \
51+
pdfPrintError( #__VA_ARGS__, state_->document, status ); \
4952
} \
5053
return result; \
5154
})()
5255

56+
// error text getting from https://github.com/libharu/libharu/wiki/Error-handling
5357
std::string GetHpdfErrorDescription( HPDF_STATUS errorCode )
5458
{
5559
switch ( errorCode )
@@ -154,6 +158,8 @@ std::string GetHpdfErrorDescription( HPDF_STATUS errorCode )
154158
}
155159
}
156160

161+
}
162+
157163
namespace MR
158164
{
159165

@@ -191,12 +197,7 @@ int calcTextLinesCount( HPDF_Doc doc, HPDF_Page page, const std::string& text )
191197
int count = 0;
192198
for ( ; substrStart < text.size(); ++count )
193199
{
194-
HPDF_UINT lineSize = HPDF_Page_MeasureText( page, text.data() + substrStart, pageWorkWidth, HPDF_TRUE, &r );
195-
HPDF_STATUS errorStatus = HPDF_GetError( doc );
196-
if ( errorStatus != HPDF_OK )
197-
{
198-
spdlog::error( "Pdf: Error in HPDF_Page_MeasureText() call. Failed with error: {} {}", errorStatus, GetHpdfErrorDescription( errorStatus ) );
199-
}
200+
HPDF_UINT lineSize = MR_HPDF_CHECK_ERROR( HPDF_Page_MeasureText( page, text.data() + substrStart, pageWorkWidth, HPDF_TRUE, &r ) );
200201
if ( lineSize == 0 )
201202
break;
202203
substrStart += lineSize;
@@ -245,7 +246,7 @@ Pdf::Pdf( const PdfParameters& params /*= PdfParameters()*/ )
245246
return;
246247
}
247248

248-
HPDF_CHECK_RES_STATUS( HPDF_SetCompressionMode( state_->document, HPDF_COMP_ALL ) );
249+
MR_HPDF_CHECK_RES_STATUS( HPDF_SetCompressionMode( state_->document, HPDF_COMP_ALL ) );
249250

250251
state_->activePage = HPDF_AddPage( state_->document );
251252
if ( !state_->activePage )
@@ -256,7 +257,7 @@ Pdf::Pdf( const PdfParameters& params /*= PdfParameters()*/ )
256257
return;
257258
}
258259

259-
HPDF_CHECK_RES_STATUS( HPDF_Page_SetSize( state_->activePage, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT ) );
260+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetSize( state_->activePage, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT ) );
260261

261262
state_->defaultFont = HPDF_GetFont( state_->document, params_.defaultFontName.c_str(), NULL );
262263
if ( !state_->defaultFont )
@@ -273,7 +274,7 @@ Pdf::Pdf( const PdfParameters& params /*= PdfParameters()*/ )
273274
return;
274275
}
275276

276-
HPDF_CHECK_RES_STATUS( HPDF_Page_SetFontAndSize( state_->activePage, state_->defaultFont, params_.textSize ) );
277+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetFontAndSize( state_->activePage, state_->defaultFont, params_.textSize ) );
277278

278279
cursorX_ = borderFieldLeft;
279280
cursorY_ = borderFieldTop;
@@ -336,7 +337,7 @@ void Pdf::addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats
336337
return;
337338
}
338339

339-
HPDF_CHECK_RES_STATUS( HPDF_Page_SetFontAndSize( state_->activePage, state_->tableFont, params_.textSize ) );
340+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetFontAndSize( state_->activePage, state_->tableFont, params_.textSize ) );
340341

341342
size_t longestMin = std::max_element( paletteStats.begin(), paletteStats.end(), [] ( const PaletteRowStats& lhv, const PaletteRowStats& rhv )
342343
{
@@ -349,14 +350,14 @@ void Pdf::addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats
349350
size_t maxWidth = std::max( std::max( longestMin, longestMax ), size_t( 9 ) ); // "Range Min".length() == 9
350351
std::string dummy = fmt::format( "{: >{}}", "_", maxWidth );
351352

352-
const float rangeLimColumnWidth = HPDF_CHECK_ERROR( HPDF_Page_TextWidth( state_->activePage, dummy.c_str() ) );
353+
const float rangeLimColumnWidth = MR_HPDF_CHECK_ERROR( HPDF_Page_TextWidth( state_->activePage, dummy.c_str() ) );
353354

354355
float bordersX[5];
355356
bordersX[0] = borderFieldLeft;
356-
bordersX[1] = bordersX[0] + tableCellPaddingX * 4.f + HPDF_CHECK_ERROR( HPDF_Page_TextWidth( state_->activePage, "Color" ) );
357+
bordersX[1] = bordersX[0] + tableCellPaddingX * 4.f + MR_HPDF_CHECK_ERROR( HPDF_Page_TextWidth( state_->activePage, "Color" ) );
357358
bordersX[2] = bordersX[1] + tableCellPaddingX * 4.f + rangeLimColumnWidth;
358359
bordersX[3] = bordersX[2] + tableCellPaddingX * 4.f + rangeLimColumnWidth;
359-
bordersX[4] = bordersX[3] + tableCellPaddingX * 4.f + HPDF_CHECK_ERROR( HPDF_Page_TextWidth( state_->activePage, "% of All" ) );
360+
bordersX[4] = bordersX[3] + tableCellPaddingX * 4.f + MR_HPDF_CHECK_ERROR( HPDF_Page_TextWidth( state_->activePage, "% of All" ) );
360361

361362

362363
const auto textHeight = static_cast< HPDF_REAL >( params_.textSize * lineSpacingScale );
@@ -365,32 +366,32 @@ void Pdf::addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats
365366
if ( cursorY_ - cellHeight * paletteStats.size() < borderFieldBottom )
366367
newPage();
367368

368-
HPDF_CHECK_RES_STATUS( HPDF_Page_SetTextLeading( state_->activePage, params_.textSize * lineSpacingScale ) );
369+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetTextLeading( state_->activePage, params_.textSize * lineSpacingScale ) );
369370

370371
auto drawCellBorders = [&] ()
371372
{
372373
for ( int i = 0; i < 4; ++i )
373374
{
374-
HPDF_CHECK_RES_STATUS( HPDF_Page_Rectangle( state_->activePage, bordersX[i], cursorY_ - cellHeight, bordersX[i + 1] - bordersX[i], cellHeight ) );
375-
HPDF_CHECK_RES_STATUS( HPDF_Page_Stroke( state_->activePage ) );
375+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_Rectangle( state_->activePage, bordersX[i], cursorY_ - cellHeight, bordersX[i + 1] - bordersX[i], cellHeight ) );
376+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_Stroke( state_->activePage ) );
376377
}
377378
};
378379

379380
drawCellBorders();
380381

381382
auto drawTextInCell = [&] ( int cellIndex, const std::string& text, bool alignCenter = false )
382383
{
383-
HPDF_CHECK_RES_STATUS( HPDF_Page_TextRect( state_->activePage, bordersX[cellIndex] + tableCellPaddingX, cursorY_ - tableCellPaddingY,
384+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_TextRect( state_->activePage, bordersX[cellIndex] + tableCellPaddingX, cursorY_ - tableCellPaddingY,
384385
bordersX[cellIndex + 1] - tableCellPaddingX, cursorY_ - textHeight - tableCellPaddingY,
385386
text.c_str(), alignCenter ? HPDF_TALIGN_CENTER : HPDF_TALIGN_RIGHT, nullptr ) );
386387
};
387388

388-
HPDF_CHECK_RES_STATUS( HPDF_Page_BeginText( state_->activePage ) );
389+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_BeginText( state_->activePage ) );
389390
drawTextInCell( 0, "Color", true );
390391
drawTextInCell( 1, "Range Min", true );
391392
drawTextInCell( 2, "Range Max", true );
392393
drawTextInCell( 3, "% of All", true );
393-
HPDF_CHECK_RES_STATUS( HPDF_Page_EndText( state_->activePage ) );
394+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_EndText( state_->activePage ) );
394395
cursorY_ -= cellHeight;
395396

396397
UniqueTemporaryFolder pathFolder( {} );
@@ -403,15 +404,15 @@ void Pdf::addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats
403404

404405
mrImage.pixels = std::vector<Color>( mrImage.resolution.x * mrImage.resolution.y, paletteStats[i].color );
405406
std::ignore = ImageSave::toAnySupportedFormat( mrImage, imageCellPath );
406-
HPDF_Image pdfImage = HPDF_CHECK_ERROR( HPDF_LoadPngImageFromFile( state_->document, utf8string( imageCellPath ).c_str() ) ); // TODO FIX need rework without using filesystem
407+
HPDF_Image pdfImage = MR_HPDF_CHECK_ERROR( HPDF_LoadPngImageFromFile( state_->document, utf8string( imageCellPath ).c_str() ) ); // TODO FIX need rework without using filesystem
407408
if ( pdfImage )
408-
HPDF_CHECK_RES_STATUS( HPDF_Page_DrawImage( state_->activePage, pdfImage, bordersX[0], cursorY_ - cellHeight, std::floor( bordersX[1] - bordersX[0] ), cellHeight ) );
409+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_DrawImage( state_->activePage, pdfImage, bordersX[0], cursorY_ - cellHeight, std::floor( bordersX[1] - bordersX[0] ), cellHeight ) );
409410

410-
HPDF_CHECK_RES_STATUS( HPDF_Page_BeginText( state_->activePage ) );
411+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_BeginText( state_->activePage ) );
411412
drawTextInCell( 1, paletteStats[i].rangeMin );
412413
drawTextInCell( 2, paletteStats[i].rangeMax );
413414
drawTextInCell( 3, paletteStats[i].percent );
414-
HPDF_CHECK_RES_STATUS( HPDF_Page_EndText( state_->activePage ) );
415+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_EndText( state_->activePage ) );
415416
cursorY_ -= cellHeight;
416417
}
417418

@@ -427,7 +428,7 @@ void Pdf::addImageFromFile( const std::filesystem::path& imagePath, const ImageP
427428
return;
428429
}
429430

430-
HPDF_Image pdfImage = HPDF_CHECK_ERROR( HPDF_LoadPngImageFromFile( state_->document, utf8string( imagePath ).c_str() ) );
431+
HPDF_Image pdfImage = MR_HPDF_CHECK_ERROR( HPDF_LoadPngImageFromFile( state_->document, utf8string( imagePath ).c_str() ) );
431432
if ( !pdfImage )
432433
{
433434
spdlog::warn( "Pdf: Failed to load image from file \"{}\"", utf8string( imagePath ) );
@@ -437,30 +438,30 @@ void Pdf::addImageFromFile( const std::filesystem::path& imagePath, const ImageP
437438
const HPDF_REAL additionalHeight = labelHeight * !params.caption.empty();
438439
HPDF_REAL imageWidth = params.size.x;
439440
if ( imageWidth == 0.f )
440-
imageWidth = (HPDF_REAL)HPDF_CHECK_ERROR( HPDF_Image_GetWidth( pdfImage ) );
441+
imageWidth = (HPDF_REAL)MR_HPDF_CHECK_ERROR( HPDF_Image_GetWidth( pdfImage ) );
441442
else if ( imageWidth < 0.f )
442443
imageWidth = borderFieldRight - cursorX_;
443444
HPDF_REAL imageHeight = params.size.y;
444445
if ( params.uniformScaleFromWidth )
445-
imageHeight = imageWidth * HPDF_CHECK_ERROR( HPDF_Image_GetHeight( pdfImage ) ) / HPDF_CHECK_ERROR( HPDF_Image_GetWidth( pdfImage ) );
446+
imageHeight = imageWidth * MR_HPDF_CHECK_ERROR( HPDF_Image_GetHeight( pdfImage ) ) / MR_HPDF_CHECK_ERROR( HPDF_Image_GetWidth( pdfImage ) );
446447
else if ( imageHeight == 0.f )
447-
imageHeight = (HPDF_REAL)HPDF_CHECK_ERROR( HPDF_Image_GetHeight( pdfImage ) );
448+
imageHeight = (HPDF_REAL)MR_HPDF_CHECK_ERROR( HPDF_Image_GetHeight( pdfImage ) );
448449
else if ( imageHeight < 0.f )
449450
imageHeight = cursorY_ - borderFieldBottom - additionalHeight;
450451

451452
if ( cursorY_ - imageHeight - additionalHeight < borderFieldBottom )
452453
newPage();
453454

454455
cursorY_ -= imageHeight;
455-
HPDF_CHECK_RES_STATUS( HPDF_Page_DrawImage( state_->activePage, pdfImage, cursorX_, cursorY_, imageWidth, imageHeight ) );
456+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_DrawImage( state_->activePage, pdfImage, cursorX_, cursorY_, imageWidth, imageHeight ) );
456457

457458
if ( !params.caption.empty() )
458459
{
459460
cursorY_ -= textSpacing / 2.;
460-
HPDF_CHECK_RES_STATUS( HPDF_Page_BeginText( state_->activePage ) );
461-
HPDF_CHECK_RES_STATUS( HPDF_Page_SetFontAndSize( state_->activePage, state_->defaultFont, params_.textSize ) );
462-
HPDF_CHECK_RES_STATUS( HPDF_Page_TextRect( state_->activePage, cursorX_, cursorY_, cursorX_ + imageWidth, cursorY_ - labelHeight, params.caption.c_str(), HPDF_TALIGN_CENTER, nullptr ) );
463-
HPDF_CHECK_RES_STATUS( HPDF_Page_EndText( state_->activePage ) );
461+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_BeginText( state_->activePage ) );
462+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetFontAndSize( state_->activePage, state_->defaultFont, params_.textSize ) );
463+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_TextRect( state_->activePage, cursorX_, cursorY_, cursorX_ + imageWidth, cursorY_ - labelHeight, params.caption.c_str(), HPDF_TALIGN_CENTER, nullptr ) );
464+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_EndText( state_->activePage ) );
464465
cursorY_ -= labelHeight;
465466
}
466467

@@ -484,7 +485,7 @@ void Pdf::newPage()
484485

485486
cursorX_ = borderFieldLeft;
486487
cursorY_ = borderFieldTop;
487-
HPDF_CHECK_RES_STATUS( HPDF_Page_SetSize( state_->activePage, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT) );
488+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetSize( state_->activePage, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT) );
488489
}
489490

490491
void Pdf::saveToFile( const std::filesystem::path& documentPath )
@@ -541,7 +542,7 @@ void Pdf::addText_( const std::string& text, const TextParams& textParams )
541542
return;
542543
}
543544

544-
HPDF_CHECK_RES_STATUS( HPDF_Page_SetFontAndSize( state_->activePage, textParams.font, textParams.fontSize ) );
545+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetFontAndSize( state_->activePage, textParams.font, textParams.fontSize ) );
545546

546547
int strNum = calcTextLinesCount( state_->document, state_->activePage, text );
547548
const auto textHeight = static_cast< HPDF_REAL >( textParams.fontSize * strNum * lineSpacingScale );
@@ -550,16 +551,16 @@ void Pdf::addText_( const std::string& text, const TextParams& textParams )
550551
if ( cursorY_ - textHeight < borderFieldBottom )
551552
newPage();
552553

553-
HPDF_CHECK_RES_STATUS( HPDF_Page_BeginText( state_->activePage ) );
554-
HPDF_CHECK_RES_STATUS( HPDF_Page_SetTextLeading( state_->activePage, textParams.fontSize * lineSpacingScale ) );
554+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_BeginText( state_->activePage ) );
555+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetTextLeading( state_->activePage, textParams.fontSize * lineSpacingScale ) );
555556

556-
HPDF_CHECK_RES_STATUS( HPDF_Page_TextRect( state_->activePage, cursorX_, cursorY_, cursorX_ + pageWorkWidth, cursorY_ - textHeight, text.c_str(), textParams.alignment, nullptr ) );
557-
HPDF_CHECK_RES_STATUS( HPDF_Page_EndText( state_->activePage ) );
557+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_TextRect( state_->activePage, cursorX_, cursorY_, cursorX_ + pageWorkWidth, cursorY_ - textHeight, text.c_str(), textParams.alignment, nullptr ) );
558+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_EndText( state_->activePage ) );
558559

559560
if ( textParams.drawBorder )
560561
{
561-
HPDF_CHECK_RES_STATUS( HPDF_Page_Rectangle( state_->activePage, cursorX_, cursorY_ - textHeight, pageWorkWidth, textHeight ) );
562-
HPDF_CHECK_RES_STATUS( HPDF_Page_Stroke( state_->activePage ) );
562+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_Rectangle( state_->activePage, cursorX_, cursorY_ - textHeight, pageWorkWidth, textHeight ) );
563+
MR_HPDF_CHECK_RES_STATUS( HPDF_Page_Stroke( state_->activePage ) );
563564
}
564565

565566
cursorY_ -= textHeight;

0 commit comments

Comments
 (0)