MCPcopy Create free account
hub / github.com/MeshInspector/MeshLib / drawTextInRect

Method drawTextInRect

source/MRIOExtras/MRPdf.cpp:729–783  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

727}
728
729void Pdf::drawTextInRect( const std::string& text, const Box2f& rect, const TextParams& params )
730{
731 if ( !checkDocument_( "draw text (rect)" ) )
732 return;
733
734 if ( text.empty() || !params.colorText.a )
735 return;
736
737 auto widths = calcTextLineWidths_( text, rect.size().x, params );
738
739 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetFontAndSize( state_->activePage, state_->getFont( params.fontName ), params.fontSize ) );
740 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetTextLeading( state_->activePage, params.fontSize * lineSpacingScale ) );
741
742 const float verticalOffset = ( rect.size().y - getTextHeight( params.fontSize, int( widths.size() ) ) ) / 2.f;
743
744 Vector4f c = Vector4f( params.colorText );
745 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetRGBFill( state_->activePage, c.x, c.y, c.z ) );
746
747 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_BeginText( state_->activePage ) );
748
749 // this is the vertical alignment in the center of the cell
750 const float realBaseline = params.fontSize / 2.f * ( 1 + MR_HPDF_CHECK_ERROR( HPDF_Font_GetCapHeight( state_->getFont( params.fontName ) ) ) / 1000.f );
751 const auto bb = MR_HPDF_CHECK_ERROR( HPDF_Font_GetBBox( state_->getFont( params.fontName ) ) );
752 const float fontGlyphShift = params.fontSize * bb.top / 1000.f - realBaseline;
753
754 HPDF_TextAlignment textAlignment;
755 if ( params.alignment == AlignmentHorizontal::Left )
756 textAlignment = HPDF_TALIGN_LEFT;
757 else if ( params.alignment == AlignmentHorizontal::Right )
758 textAlignment = HPDF_TALIGN_RIGHT;
759 else
760 textAlignment = HPDF_TALIGN_CENTER;
761 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_TextRect( state_->activePage, rect.min.x, rect.max.y - verticalOffset + fontGlyphShift,
762 rect.max.x, rect.min.y - verticalOffset + fontGlyphShift - params.fontSize * lineSpacingScale, text.c_str(), textAlignment, nullptr ) ); // TODO FIX replace - params.fontSize * lineSpacingScale to true calculated value
763 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_EndText( state_->activePage ) );
764
765 if ( params.underline )
766 {
767 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetLineWidth( state_->activePage, params.fontSize / 10.f ) );
768 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_SetRGBStroke( state_->activePage, c.x, c.y, c.z ) );
769
770 for ( int i = 0; i < widths.size(); ++i )
771 {
772 float posX = rect.min.x;
773 if ( params.alignment == AlignmentHorizontal::Right )
774 posX = rect.max.x - widths[i];
775 else if ( params.alignment == AlignmentHorizontal::Center )
776 posX = rect.center().x - widths[i] / 2.f;
777 const float posY = rect.max.y - verticalOffset - realBaseline - params.fontSize * ( ( lineSpacingScale - 1.f ) * underlineShiftScale + i * lineSpacingScale );
778 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_MoveTo( state_->activePage, posX, posY ) );
779 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_LineTo( state_->activePage, posX + widths[i], posY ) );
780 }
781 MR_HPDF_CHECK_RES_STATUS( HPDF_Page_Stroke( state_->activePage ) );
782 }
783}
784
785void Pdf::drawRect( const Box2f& rect, const Color& fillColor, const Color& strokeColor )
786{

Callers

nothing calls this directly

Calls 5

getTextHeightFunction · 0.85
getFontMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45
centerMethod · 0.45

Tested by

no test coverage detected