15
15
16
16
#include < hpdf.h>
17
17
18
+ namespace
19
+ {
20
+
18
21
std::string GetHpdfErrorDescription ( HPDF_STATUS errorCode );
19
22
20
23
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 )
32
35
}
33
36
}
34
37
35
- #define HPDF_CHECK_RES_STATUS ( func_call ) \
38
+ #define MR_HPDF_CHECK_RES_STATUS ( ... ) \
36
39
do { \
37
- HPDF_STATUS status = func_call ; \
40
+ HPDF_STATUS status = __VA_ARGS__ ; \
38
41
if (status != HPDF_OK) { \
39
- pdfPrintError ( #func_call , state_->document , status ); \
42
+ pdfPrintError ( #__VA_ARGS__ , state_->document , status ); \
40
43
} \
41
44
} while (0 )
42
45
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__ ; \
46
49
HPDF_STATUS status = HPDF_GetError ( state_->document ); \
47
50
if (status != HPDF_OK) { \
48
- pdfPrintError ( #func_call , state_->document , status ); \
51
+ pdfPrintError ( #__VA_ARGS__ , state_->document , status ); \
49
52
} \
50
53
return result; \
51
54
})()
52
55
56
+ // error text getting from https://github.com/libharu/libharu/wiki/Error-handling
53
57
std::string GetHpdfErrorDescription ( HPDF_STATUS errorCode )
54
58
{
55
59
switch ( errorCode )
@@ -154,6 +158,8 @@ std::string GetHpdfErrorDescription( HPDF_STATUS errorCode )
154
158
}
155
159
}
156
160
161
+ }
162
+
157
163
namespace MR
158
164
{
159
165
@@ -191,12 +197,7 @@ int calcTextLinesCount( HPDF_Doc doc, HPDF_Page page, const std::string& text )
191
197
int count = 0 ;
192
198
for ( ; substrStart < text.size (); ++count )
193
199
{
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 ) );
200
201
if ( lineSize == 0 )
201
202
break ;
202
203
substrStart += lineSize;
@@ -245,7 +246,7 @@ Pdf::Pdf( const PdfParameters& params /*= PdfParameters()*/ )
245
246
return ;
246
247
}
247
248
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 ) );
249
250
250
251
state_->activePage = HPDF_AddPage ( state_->document );
251
252
if ( !state_->activePage )
@@ -256,7 +257,7 @@ Pdf::Pdf( const PdfParameters& params /*= PdfParameters()*/ )
256
257
return ;
257
258
}
258
259
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 ) );
260
261
261
262
state_->defaultFont = HPDF_GetFont ( state_->document , params_.defaultFontName .c_str (), NULL );
262
263
if ( !state_->defaultFont )
@@ -273,7 +274,7 @@ Pdf::Pdf( const PdfParameters& params /*= PdfParameters()*/ )
273
274
return ;
274
275
}
275
276
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 ) );
277
278
278
279
cursorX_ = borderFieldLeft;
279
280
cursorY_ = borderFieldTop;
@@ -336,7 +337,7 @@ void Pdf::addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats
336
337
return ;
337
338
}
338
339
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 ) );
340
341
341
342
size_t longestMin = std::max_element ( paletteStats.begin (), paletteStats.end (), [] ( const PaletteRowStats& lhv, const PaletteRowStats& rhv )
342
343
{
@@ -349,14 +350,14 @@ void Pdf::addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats
349
350
size_t maxWidth = std::max ( std::max ( longestMin, longestMax ), size_t ( 9 ) ); // "Range Min".length() == 9
350
351
std::string dummy = fmt::format ( " {: >{}}" , " _" , maxWidth );
351
352
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 () ) );
353
354
354
355
float bordersX[5 ];
355
356
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" ) );
357
358
bordersX[2 ] = bordersX[1 ] + tableCellPaddingX * 4 .f + rangeLimColumnWidth;
358
359
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" ) );
360
361
361
362
362
363
const auto textHeight = static_cast < HPDF_REAL >( params_.textSize * lineSpacingScale );
@@ -365,32 +366,32 @@ void Pdf::addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats
365
366
if ( cursorY_ - cellHeight * paletteStats.size () < borderFieldBottom )
366
367
newPage ();
367
368
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 ) );
369
370
370
371
auto drawCellBorders = [&] ()
371
372
{
372
373
for ( int i = 0 ; i < 4 ; ++i )
373
374
{
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 ) );
376
377
}
377
378
};
378
379
379
380
drawCellBorders ();
380
381
381
382
auto drawTextInCell = [&] ( int cellIndex, const std::string& text, bool alignCenter = false )
382
383
{
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,
384
385
bordersX[cellIndex + 1 ] - tableCellPaddingX, cursorY_ - textHeight - tableCellPaddingY,
385
386
text.c_str (), alignCenter ? HPDF_TALIGN_CENTER : HPDF_TALIGN_RIGHT, nullptr ) );
386
387
};
387
388
388
- HPDF_CHECK_RES_STATUS ( HPDF_Page_BeginText ( state_->activePage ) );
389
+ MR_HPDF_CHECK_RES_STATUS ( HPDF_Page_BeginText ( state_->activePage ) );
389
390
drawTextInCell ( 0 , " Color" , true );
390
391
drawTextInCell ( 1 , " Range Min" , true );
391
392
drawTextInCell ( 2 , " Range Max" , true );
392
393
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 ) );
394
395
cursorY_ -= cellHeight;
395
396
396
397
UniqueTemporaryFolder pathFolder ( {} );
@@ -403,15 +404,15 @@ void Pdf::addPaletteStatsTable( const std::vector<PaletteRowStats>& paletteStats
403
404
404
405
mrImage.pixels = std::vector<Color>( mrImage.resolution .x * mrImage.resolution .y , paletteStats[i].color );
405
406
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
407
408
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 ) );
409
410
410
- HPDF_CHECK_RES_STATUS ( HPDF_Page_BeginText ( state_->activePage ) );
411
+ MR_HPDF_CHECK_RES_STATUS ( HPDF_Page_BeginText ( state_->activePage ) );
411
412
drawTextInCell ( 1 , paletteStats[i].rangeMin );
412
413
drawTextInCell ( 2 , paletteStats[i].rangeMax );
413
414
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 ) );
415
416
cursorY_ -= cellHeight;
416
417
}
417
418
@@ -427,7 +428,7 @@ void Pdf::addImageFromFile( const std::filesystem::path& imagePath, const ImageP
427
428
return ;
428
429
}
429
430
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 () ) );
431
432
if ( !pdfImage )
432
433
{
433
434
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
437
438
const HPDF_REAL additionalHeight = labelHeight * !params.caption .empty ();
438
439
HPDF_REAL imageWidth = params.size .x ;
439
440
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 ) );
441
442
else if ( imageWidth < 0 .f )
442
443
imageWidth = borderFieldRight - cursorX_;
443
444
HPDF_REAL imageHeight = params.size .y ;
444
445
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 ) );
446
447
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 ) );
448
449
else if ( imageHeight < 0 .f )
449
450
imageHeight = cursorY_ - borderFieldBottom - additionalHeight;
450
451
451
452
if ( cursorY_ - imageHeight - additionalHeight < borderFieldBottom )
452
453
newPage ();
453
454
454
455
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 ) );
456
457
457
458
if ( !params.caption .empty () )
458
459
{
459
460
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 ) );
464
465
cursorY_ -= labelHeight;
465
466
}
466
467
@@ -484,7 +485,7 @@ void Pdf::newPage()
484
485
485
486
cursorX_ = borderFieldLeft;
486
487
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) );
488
489
}
489
490
490
491
void Pdf::saveToFile ( const std::filesystem::path& documentPath )
@@ -541,7 +542,7 @@ void Pdf::addText_( const std::string& text, const TextParams& textParams )
541
542
return ;
542
543
}
543
544
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 ) );
545
546
546
547
int strNum = calcTextLinesCount ( state_->document , state_->activePage , text );
547
548
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 )
550
551
if ( cursorY_ - textHeight < borderFieldBottom )
551
552
newPage ();
552
553
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 ) );
555
556
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 ) );
558
559
559
560
if ( textParams.drawBorder )
560
561
{
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 ) );
563
564
}
564
565
565
566
cursorY_ -= textHeight;
0 commit comments