Logo Search packages:      
Sourcecode: libpodofo version File versions  Download package

void PoDoFo::PdfTable::Draw ( double  dX,
double  dY,
PdfPainter pPainter,
const PdfRect rClipRect = PdfRect(),
double *  pdLastX = NULL,
double *  pdLastY = NULL 
) [virtual]

Draw the table with its current settings on a PdfPainter.

Parameters:
dXx coordinate of top left of the table
dYy coordinate of top left of the table
pPainterthe painter to draw on. The painter has to have a page set currently.
rClipRectthe clipping rectangle on the current page
pdLastXthe last used X position by the table on the current page will be written to this value (usually bottom right)
pdLastYthe last used Y positon by the table on the current page will be written to this value (usually bottom right)

Definition at line 93 of file PdfTable.cpp.

References CalculateTableSize(), CheckForNewPage(), DrawHorizontalBorders(), PoDoFo::PdfPainter::DrawImage(), PoDoFo::PdfPainter::DrawLine(), PoDoFo::PdfPainter::DrawMultiLineText(), PoDoFo::PdfPainter::DrawTextAligned(), PoDoFo::ePdfError_InvalidHandle, PoDoFo::PdfPainter::FillRect(), PoDoFo::PdfTableModel::GetAlignment(), PoDoFo::PdfTableModel::GetBackgroundColor(), PoDoFo::PdfTableModel::GetBorderColor(), PoDoFo::PdfTableModel::GetBorderWidth(), PoDoFo::PdfRect::GetBottom(), GetCols(), PoDoFo::PdfTableModel::GetFont(), PoDoFo::PdfPainter::GetFont(), PoDoFo::PdfFont::GetFontMetrics(), PoDoFo::PdfTableModel::GetForegroundColor(), PoDoFo::PdfRect::GetHeight(), PoDoFo::PdfTableModel::GetImage(), PoDoFo::PdfRect::GetLeft(), PoDoFo::PdfFontMetrics::GetLineSpacing(), PoDoFo::PdfPainter::GetPage(), PoDoFo::PdfXObject::GetPageSize(), PoDoFo::PdfCanvas::GetPageSize(), GetRows(), PoDoFo::PdfTableModel::GetText(), PoDoFo::PdfTableModel::GetVerticalAlignment(), PoDoFo::PdfRect::GetWidth(), PoDoFo::PdfTableModel::HasBackgroundColor(), PoDoFo::PdfTableModel::HasBorders(), PoDoFo::PdfTableModel::HasImage(), PoDoFo::PdfTableModel::HasWordWrap(), PoDoFo::PDF_MIN(), PODOFO_RAISE_ERROR, PoDoFo::PdfPainter::Restore(), PoDoFo::PdfPainter::Save(), PoDoFo::PdfPainter::SetClipRect(), PoDoFo::PdfPainter::SetColor(), PoDoFo::PdfPainter::SetFont(), PoDoFo::PdfPainter::SetStrokeWidth(), and PoDoFo::PdfPainter::SetStrokingColor().

{
    if( !pPainter ) 
    {
        PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
    }

    //RG: TODO Should dCurY variable be initialised with 0? (line 257 may fall through without initialisation!)
    int i = 0;
    int j = 0;
    double  dCurX = 0.0;
    double  dCurY = 0.0;

    double  dWidth = 0.0;
    double  dHeight = 0.0;
    double  dVertical = 0.0;
    double* pdColWidths  = new double[this->GetCols()];
    double* pdRowHeights = new double[this->GetRows()];

      bool bBorders = !m_pModel || (m_pModel && m_pModel->HasBorders() );

    // Calculate all necessary sizes
    this->CalculateTableSize( dX, dY, pPainter->GetPage(), 
                              pdColWidths, pdRowHeights,
                              &dWidth, &dHeight );
 
    if( !(!static_cast<int>(rClipRect.GetBottom()) && 
          !static_cast<int>(rClipRect.GetLeft()) &&
          !static_cast<int>(rClipRect.GetWidth()) && 
          !static_cast<int>(rClipRect.GetWidth())) ) 
        m_curClipRect = rClipRect;
    else
    {
        m_curClipRect = PdfRect( 0.0, dX, 
                                 pPainter->GetPage()->GetPageSize().GetWidth() - dX,
                                 dY );
    }

    // Draw the table
    pPainter->Save();
    PdfFont* pDefaultFont = pPainter->GetFont(); // get the default font
    PdfFont* pFont;

    // draw contents
    if( m_pModel ) 
    {
            pPainter->SetStrokeWidth( m_pModel->GetBorderWidth() );

            if( bBorders ) // draw top border
            this->DrawHorizontalBorders( 0, dX, dY, pPainter, pdColWidths );

        for( j=0;j<m_nRows;j++ )
        {
                  if( this->CheckForNewPage( &dY, &dCurY, pdRowHeights[j], pPainter ) && bBorders )
                // draw top border on new page
                this->DrawHorizontalBorders( j, dX, dY, pPainter, pdColWidths );
    
                  dCurX  = 0.0;     
                  dCurY += pdRowHeights[j];

            for( i=0;i<m_nCols;i++ ) 
            {
                  // set a clipping rectangle
                pPainter->Save();
                pPainter->SetClipRect( dX + dCurX, dY - dCurY, pdColWidths[i], pdRowHeights[j] );

                // Draw background
                        double dBorder = bBorders ? m_pModel->GetBorderWidth()/2.0 : 0.0;
                if( m_pModel->HasBackgroundColor( i, j ) ) 
                {
                    pPainter->Save();
                    pPainter->SetColor( m_pModel->GetBackgroundColor( i, j ) );
                              // Make sure that FillRect only fills inside the border
                              // rectangle and not over the border. This is necessary
                              // because we draw the border first and than the contents.
                    pPainter->FillRect( dX + dCurX + dBorder, dY - dCurY + dBorder, 
                                                    pdColWidths[i] - 2.0 * dBorder, 
                                                            pdRowHeights[j] - 2.0 * dBorder );
                    pPainter->Restore();
                }

                // draw an image
                PdfImage* pImage = m_pModel->GetImage( i, j );
                double dImageWidth = 0.0;
                if( m_pModel->HasImage( i, j ) && pImage )
                {
                    double dScaleX = (pdColWidths[i])  / pImage->GetPageSize().GetWidth();
                    double dScaleY = (pdRowHeights[j] - 2.0 * dBorder) / pImage->GetPageSize().GetHeight();
                    double dScale  = PDF_MIN( dScaleX, dScaleY );

                    dImageWidth = pImage->GetPageSize().GetWidth() * dScale;

                    pPainter->DrawImage( dX + dCurX, dY - dCurY + dBorder, pImage, dScale, dScale );
                }

                // Set the correct font
                pFont = m_pModel->GetFont( i, j );
                pFont = pFont ? pFont : pDefaultFont;
                pPainter->SetFont( pFont );
                        pPainter->SetColor( m_pModel->GetForegroundColor( i, j ) );

                // draw text
                        if( m_pModel->HasWordWrap( i, j ) )
                        {
                              // Make sure we have at least 1 dot free space at each side of the rectangle
                              pPainter->DrawMultiLineText( dX + dCurX + 1.0 + dImageWidth, dY - dCurY, 
                                                 pdColWidths[i] - 2.0 - dImageWidth, pdRowHeights[j],
                                                                         m_pModel->GetText( i, j ), m_pModel->GetAlignment( i, j ),
                                                                         m_pModel->GetVerticalAlignment( i, j ) );
                        }
                        else
                        {
                              // calculate vertical alignment
                              switch( m_pModel->GetVerticalAlignment( i, j ) ) 
                              {
                                    default:
                                    case ePdfVerticalAlignment_Top:
                                          dVertical = 0.0;
                                          break;
                                    case ePdfVerticalAlignment_Center:
                                          dVertical = (pdRowHeights[j] - pFont->GetFontMetrics()->GetLineSpacing()) / 2.0;
                                          break;
                                    case ePdfVerticalAlignment_Bottom:
                                          dVertical = (pdRowHeights[j] - pFont->GetFontMetrics()->GetLineSpacing());
                                          break;
                              }

                              // Make sure we have at least 1 dot free space at each side of the rectangle
                              pPainter->DrawTextAligned( dX + dCurX + 1 + dImageWidth, dY - dCurY + dVertical, 
                                               pdColWidths[i] - 2.0 - dImageWidth, m_pModel->GetText( i, j ), m_pModel->GetAlignment( i, j ) );
                        }
                
                pPainter->Restore();
                        if( bBorders ) // draw left x border
                {
                    // use always the border color of the left to the current cell
                    pPainter->SetStrokingColor( m_pModel->GetBorderColor( i>0 ? i-1 : i, j ) );
                              pPainter->DrawLine( dX + dCurX, dY - dCurY, dX + dCurX, dY - dCurY + pdRowHeights[j] );
                }

                    dCurX += pdColWidths[i];    
            }

                  if( bBorders ) 
                  {
                        // Draw last X border
                if( i > 0 )
                {
                    pPainter->SetStrokingColor( m_pModel->GetBorderColor( i-1, j ) );
                            pPainter->DrawLine( dX + dCurX, dY - dCurY, dX + dCurX, dY - dCurY + pdRowHeights[j] );
                }

                // draw border below row    
                this->DrawHorizontalBorders( j, dX, dY - dCurY, pPainter, pdColWidths );
            }
            }    
      }
    pPainter->Restore();

    if( pdLastX )
        *pdLastX = dX + dWidth;

    if( pdLastY )
        *pdLastY = dY - dCurY;

    // Free allocated memory
    delete [] pdColWidths;
    delete [] pdRowHeights;
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index