PdfFont* PdfFontCache::GetDuplicateFontType1( PdfFont * pFont, const char* pszSuffix ) { TCISortedFontList it = m_vecFonts.begin(); std::string id = pFont->GetIdentifier().GetName(); id += pszSuffix; // Search if the object is a cached normal font while( it != m_vecFonts.end() ) { if( (*it).m_pFont->GetIdentifier() == id ) return (*it).m_pFont; ++it; } // Search if the object is a cached font subset it = m_vecFontSubsets.begin(); while( it != m_vecFontSubsets.end() ) { if( (*it).m_pFont->GetIdentifier() == id ) return (*it).m_pFont; ++it; } // Create a copy of the font PODOFO_ASSERT( pFont->GetFontMetrics()->GetFontType() == ePdfFontType_Type1Pfb ); PdfFontMetrics *pMetrics = new PdfFontMetrics( &m_ftLibrary, pFont->GetFontMetrics()->GetFilename() ); PdfFont* newFont = new PdfFontType1( static_cast<PdfFontType1 *>(pFont), pMetrics, pszSuffix, m_pParent ); if( newFont ) { std::string name = newFont->GetFontMetrics()->GetFontname(); name += pszSuffix; TFontCacheElement element; element.m_pFont = newFont; element.m_bBold = newFont->IsBold(); element.m_bItalic = newFont->IsItalic(); element.m_sFontName = name; element.m_pEncoding = newFont->GetEncoding(); m_vecFonts .push_back( element ); // Now sort the font list std::sort( m_vecFonts.begin(), m_vecFonts.end() ); } return newFont; }
void draw( char* pszBuffer, PdfDocument* pDocument ) { PdfPage* pPage; PdfPainter painter; PdfFont* pFont; PdfRect size; double dX = BORDER_LEFT; double dY = BORDER_TOP; char* pszStart = pszBuffer; size = PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ); pFont = pDocument->CreateFont( "Arial" ); pPage = pDocument->CreatePage( size ); if( !pFont ) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } pFont->SetFontSize( FONT_SIZE ); painter.SetPage( pPage ); painter.SetFont( pFont ); while( *pszBuffer ) { if( *pszBuffer == '\n' ) { painter.DrawText( dX, dY, pszStart, pszBuffer-pszStart ); pszStart = (++pszBuffer); dY += pFont->GetFontMetrics()->GetLineSpacing(); if( dY > (size.GetHeight() - BORDER_TOP) ) { pPage = pDocument->CreatePage( size ); painter.SetPage( pPage ); dY = BORDER_TOP; } } else ++pszBuffer; } painter.FinishPage(); }
PdfFont* PdfFontCache::GetFont( PdfObject* pObject ) { TCISortedFontList it = m_vecFonts.begin(); const PdfReference & ref = pObject->Reference(); // Search if the object is a cached normal font while( it != m_vecFonts.end() ) { if( (*it).m_pFont->GetObject()->Reference() == ref ) return (*it).m_pFont; ++it; } // Search if the object is a cached font subset it = m_vecFontSubsets.begin(); while( it != m_vecFontSubsets.end() ) { if( (*it).m_pFont->GetObject()->Reference() == ref ) return (*it).m_pFont; ++it; } // Create a new font PdfFont* pFont = PdfFontFactory::CreateFont( &m_ftLibrary, pObject ); if( pFont ) { TFontCacheElement element; element.m_pFont = pFont; element.m_bBold = pFont->IsBold(); element.m_bItalic = pFont->IsItalic(); element.m_sFontName = pFont->GetFontMetrics()->GetFontname(); element.m_pEncoding = NULL; m_vecFonts .push_back( element ); // Now sort the font list std::sort( m_vecFonts.begin(), m_vecFonts.end() ); } return pFont; }
void draw( char* pszBuffer, PdfDocument* pDocument, bool bUtf8, const char* pszFontName ) { PdfPage* pPage; PdfPainter painter; PdfFont* pFont; PdfRect size; const PdfEncoding* pEncoding; double dX = BORDER_LEFT; double dY = BORDER_TOP; char* pszStart = pszBuffer; if( bUtf8 ) { pEncoding = new PdfIdentityEncoding(); } else { pEncoding = PdfEncodingFactory::GlobalWinAnsiEncodingInstance(); } size = PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ); pFont = pDocument->CreateFont( pszFontName, pEncoding ); pPage = pDocument->CreatePage( size ); dY = size.GetHeight() - dY; if( !pFont ) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } pFont->SetFontSize( FONT_SIZE ); painter.SetPage( pPage ); painter.SetFont( pFont ); while( *pszBuffer ) { if( *pszBuffer == '\n' ) { if( bUtf8 ) { painter.DrawText( dX, dY, PdfString( reinterpret_cast<const pdf_utf8*>(pszStart), pszBuffer-pszStart ) ); } else { painter.DrawText( dX, dY, pszStart, pszBuffer-pszStart ); } pszStart = (++pszBuffer); dY -= pFont->GetFontMetrics()->GetLineSpacing(); if( dY < BORDER_TOP ) { pPage = pDocument->CreatePage( size ); painter.SetPage( pPage ); dY = size.GetHeight() - dY; } } else ++pszBuffer; } painter.FinishPage(); }
void PdfTable::Draw( double dX, double dY, PdfPainter* pPainter, const PdfRect & rClipRect, double* pdLastX, double* pdLastY ) { 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; }