PdfOutputDevice::PdfOutputDevice( const char* pszFilename, bool bTruncate ) { this->Init(); if( !pszFilename ) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } std::ios_base::openmode openmode = std::fstream::binary | std::ios_base::in | std::ios_base::out; if( bTruncate ) openmode |= std::ios_base::trunc; std::fstream *pStream = new std::fstream( pszFilename, openmode ); if( pStream->fail() ) { delete pStream; PODOFO_RAISE_ERROR_INFO( ePdfError_FileNotFound, pszFilename ); } m_pStream = pStream; m_pReadStream = pStream; PdfLocaleImbue( *m_pStream ); if( !bTruncate ) { m_pStream->seekp( 0, std::ios_base::end ); m_ulPosition = m_pStream->tellp(); m_ulLength = m_ulPosition; } }
PdfOutputDevice::PdfOutputDevice( const char* pszFilename ) { this->Init(); if( !pszFilename ) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } std::fstream *pStream = new std::fstream(pszFilename, std::fstream::binary|std::ios_base::in | std::ios_base::out | std::ios_base::trunc); if(pStream->fail()) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } m_pStream = pStream; m_pReadStream = pStream; PdfLocaleImbue(*m_pStream); /* m_hFile = fopen( pszFilename, "wb" ); if( !m_hFile ) { PODOFO_RAISE_ERROR_INFO( ePdfError_FileNotFound, pszFilename ); } */ }
PdfOutputDevice::PdfOutputDevice( const std::ostream* pOutStream ) { this->Init(); m_pStream = const_cast< std::ostream* >( pOutStream ); m_pStreamOwned = false; m_pStreamSavedLocale = m_pStream->getloc(); PdfLocaleImbue(*m_pStream); }
PdfInputDevice::PdfInputDevice( const std::istream* pInStream ) { this->Init(); m_pStream = const_cast< std::istream* >( pInStream ); if( !m_pStream->good() ) { PODOFO_RAISE_ERROR( ePdfError_FileNotFound ); } PdfLocaleImbue(*m_pStream); }
PdfXObject::PdfXObject( const char* pszSubType, PdfVecObjects* pParent ) : PdfElement( "XObject", pParent ) { ostringstream out; PdfLocaleImbue(out); // Implementation note: the identifier is always // Prefix+ObjectNo. Prefix is /XOb for XObject. out << "XOb" << m_pObject->Reference().ObjectNumber(); m_Identifier = PdfName( out.str().c_str() ); m_Reference = m_pObject->Reference(); m_pObject->GetDictionary().AddKey( PdfName::KeySubtype, PdfName( pszSubType ) ); }
PdfExtGState::PdfExtGState( PdfDocument* pParent ) : PdfElement( "ExtGState", pParent ) { std::ostringstream out; // We probably aren't doing anything locale sensitive here, but it's // best to be sure. PdfLocaleImbue(out); // Implementation note: the identifier is always // Prefix+ObjectNo. Prefix is /Ft for fonts. out << "ExtGS" << m_pObject->Reference().ObjectNumber(); m_Identifier = PdfName( out.str().c_str() ); this->Init(); }
PdfXObject::PdfXObject( PdfObject* pObject ) : PdfElement( "XObject", pObject ), PdfCanvas() { ostringstream out; PdfLocaleImbue(out); // Implementation note: the identifier is always // Prefix+ObjectNo. Prefix is /XOb for XObject. out << "XOb" << m_pObject->Reference().ObjectNumber(); m_pResources = pObject->GetIndirectKey( "Resources" ); m_Identifier = PdfName( out.str().c_str() ); m_rRect = PdfRect( m_pObject->GetIndirectKey( "BBox" )->GetArray() ); m_Reference = m_pObject->Reference(); }
PdfXObject::PdfXObject( const char* pszSubType, PdfObject* pObject ) : PdfElement( "XObject", pObject ) { ostringstream out; PdfLocaleImbue(out); if( m_pObject->GetDictionary().GetKeyAsName( PdfName::KeySubtype ) != pszSubType ) { PODOFO_RAISE_ERROR( ePdfError_InvalidDataType ); } // Implementation note: the identifier is always // Prefix+ObjectNo. Prefix is /XOb for XObject. out << "XOb" << m_pObject->Reference().ObjectNumber(); m_Identifier = PdfName( out.str().c_str() ); m_Reference = m_pObject->Reference(); }
PdfTilingPattern::PdfTilingPattern( EPdfTilingPatternType eTilingType, double strokeR, double strokeG, double strokeB, bool doFill, double fillR, double fillG, double fillB, double offsetX, double offsetY, PdfImage *pImage, PdfVecObjects* pParent) : PdfElement( "Pattern", pParent ) { std::ostringstream out; // We probably aren't doing anything locale sensitive here, but it's // best to be sure. PdfLocaleImbue(out); // Implementation note: the identifier is always // Prefix+ObjectNo. Prefix is /Ft for fonts. out << "Ptrn" << this->GetObject()->Reference().ObjectNumber(); m_Identifier = PdfName( out.str().c_str() ); this->Init( eTilingType, strokeR, strokeG, strokeB, doFill, fillR, fillG, fillB, offsetX, offsetY, pImage); }
void PdfFont::InitVars() { ostringstream out; PdfLocaleImbue(out); m_pMetrics->SetFontSize( 12.0 ); m_pMetrics->SetFontScale( 100.0 ); m_pMetrics->SetFontCharSpace( 0.0 ); // Peter Petrov 24 Spetember 2008 m_bWasEmbedded = false; m_bUnderlined = false; m_bStrikedOut = false; // Implementation note: the identifier is always // Prefix+ObjectNo. Prefix is /Ft for fonts. out << "Ft" << m_pObject->Reference().ObjectNumber(); m_Identifier = PdfName( out.str().c_str() ); // replace all spaces in the base font name as suggested in // the PDF reference section 5.5.2# int curPos = 0; std::string sTmp = m_pMetrics->GetFontname(); const char* pszPrefix = m_pMetrics->GetSubsetFontnamePrefix(); if( pszPrefix ) { std::string sPrefix = pszPrefix; sTmp = sPrefix + sTmp; } for(unsigned int i = 0; i < sTmp.size(); i++) { if(sTmp[i] != ' ') sTmp[curPos++] = sTmp[i]; } sTmp.resize(curPos); m_BaseFont = PdfName( sTmp.c_str() ); }
void PdfXObject::InitXObject( const PdfRect & rRect, const char* pszPrefix ) { PdfVariant var; ostringstream out; PdfLocaleImbue(out); // Initialize static data if( s_matrix.empty() ) { // This matrix is the same for all PdfXObjects so cache it s_matrix.push_back( PdfVariant( 1LL ) ); s_matrix.push_back( PdfVariant( 0LL ) ); s_matrix.push_back( PdfVariant( 0LL ) ); s_matrix.push_back( PdfVariant( 1LL ) ); s_matrix.push_back( PdfVariant( 0LL ) ); s_matrix.push_back( PdfVariant( 0LL ) ); } rRect.ToVariant( var ); m_pObject->GetDictionary().AddKey( "BBox", var ); m_pObject->GetDictionary().AddKey( PdfName::KeySubtype, PdfName("Form") ); m_pObject->GetDictionary().AddKey( "FormType", PdfVariant( 1LL ) ); // only 1 is only defined in the specification. m_pObject->GetDictionary().AddKey( "Matrix", s_matrix ); // The PDF specification suggests that we send all available PDF Procedure sets m_pObject->GetDictionary().AddKey( "Resources", PdfObject( PdfDictionary() ) ); m_pResources = m_pObject->GetDictionary().GetKey( "Resources" ); m_pResources->GetDictionary().AddKey( "ProcSet", PdfCanvas::GetProcSet() ); // Implementation note: the identifier is always // Prefix+ObjectNo. Prefix is /XOb for XObject. if ( pszPrefix == NULL ) out << "XOb" << m_pObject->Reference().ObjectNumber(); else out << pszPrefix << m_pObject->Reference().ObjectNumber(); m_Identifier = PdfName( out.str().c_str() ); m_Reference = m_pObject->Reference(); }
void PdfAcroForm::Init( EPdfAcroFormDefaulAppearance eDefaultAppearance ) { // Add default appearance: black text, 12pt times // -> only if we do not have a DA key yet // Peter Petrov 27 April 2008 //this->GetObject()->GetDictionary().AddKey( PdfName("NeedAppearances"), PdfVariant(true) ); if( !this->GetObject()->GetDictionary().HasKey("DA") && eDefaultAppearance == ePdfAcroFormDefaultAppearance_BlackText12pt ) { //PdfFont* pFont = pParent->GetDocument()->CreateFont( "Helvetica", false ); // TODO: It is no good idea to always embedd arial // but handling of non embedded helvetica is currently broken PdfFont* pFont = m_pDocument->CreateFont( "Arial" ); // embedd is default true PdfObject* pResource; PdfObject* pFontDict; // Create DR key if( !this->GetObject()->GetDictionary().HasKey( PdfName("DR") ) ) this->GetObject()->GetDictionary().AddKey( PdfName("DR"), PdfDictionary() ); pResource = this->GetObject()->GetDictionary().GetKey( PdfName("DR") ); if( !pResource->GetDictionary().HasKey( PdfName("Font") ) ) pResource->GetDictionary().AddKey( PdfName("Font"), PdfDictionary() ); pFontDict = pResource->GetDictionary().GetKey( PdfName("Font") ); pFontDict->GetDictionary().AddKey( pFont->GetIdentifier(), pFont->GetObject()->Reference() ); // Create DA key std::ostringstream oss; PdfLocaleImbue(oss); oss << "0 0 0 rg /" << pFont->GetIdentifier().GetName() << " 12 Tf"; this->GetObject()->GetDictionary().AddKey( PdfName("DA"), PdfString( oss.str() ) ); } }
PdfInputDevice::PdfInputDevice( const char* pBuffer, size_t lLen ) { this->Init(); if( !pBuffer ) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } try { m_pStream = static_cast< std::istream* >( new std::istringstream( std::string( pBuffer, lLen ), std::ios::binary ) ); if( !m_pStream || !m_pStream->good() ) { PODOFO_RAISE_ERROR( ePdfError_FileNotFound ); } m_StreamOwned = true; } catch(...) { // should probably check the exact error, but for now it's a good error PODOFO_RAISE_ERROR( ePdfError_FileNotFound ); } PdfLocaleImbue(*m_pStream); }
void PdfTilingPattern::Init( EPdfTilingPatternType eTilingType, double strokeR, double strokeG, double strokeB, bool doFill, double fillR, double fillG, double fillB, double offsetX, double offsetY, PdfImage *pImage) { if (eTilingType == ePdfTilingPatternType_Image && pImage == NULL) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } if (eTilingType != ePdfTilingPatternType_Image && pImage != NULL) { PODOFO_RAISE_ERROR( ePdfError_InvalidHandle ); } PdfRect rRect; rRect.SetLeft(0); rRect.SetBottom(0); if (pImage) { rRect.SetWidth(pImage->GetWidth()); rRect.SetHeight(-pImage->GetHeight()); } else { rRect.SetWidth(8); rRect.SetHeight(8); } PdfVariant var; rRect.ToVariant( var ); this->GetObject()->GetDictionary().AddKey( PdfName("PatternType"), static_cast<pdf_int64>(1L) ); // Tiling pattern this->GetObject()->GetDictionary().AddKey( PdfName("PaintType"), static_cast<pdf_int64>(1L) ); // Colored this->GetObject()->GetDictionary().AddKey( PdfName("TilingType"), static_cast<pdf_int64>(1L) ); // Constant spacing this->GetObject()->GetDictionary().AddKey( PdfName("BBox"), var ); this->GetObject()->GetDictionary().AddKey( PdfName("XStep"), static_cast<pdf_int64>(rRect.GetWidth()) ); this->GetObject()->GetDictionary().AddKey( PdfName("YStep"), static_cast<pdf_int64>(rRect.GetHeight()) ); this->GetObject()->GetDictionary().AddKey( PdfName("Resources"), PdfObject( PdfDictionary() ) ); if (offsetX < -1e-9 || offsetX > 1e-9 || offsetY < -1e-9 || offsetY > 1e-9) { PdfArray array; array.push_back (static_cast<pdf_int64>(1)); array.push_back (static_cast<pdf_int64>(0)); array.push_back (static_cast<pdf_int64>(0)); array.push_back (static_cast<pdf_int64>(1)); array.push_back (offsetX); array.push_back (offsetY); this->GetObject()->GetDictionary().AddKey( PdfName("Matrix"), array ); } std::ostringstream out; out.flags( std::ios_base::fixed ); out.precision( 1L /* clPainterDefaultPrecision */ ); PdfLocaleImbue(out); if (pImage) { AddToResources(pImage->GetIdentifier(), pImage->GetObjectReference(), PdfName("XObject")); out << rRect.GetWidth() << " 0 0 " << rRect.GetHeight() << " " << rRect.GetLeft() << " " << rRect.GetBottom() << " cm" << std::endl; out << "/" << pImage->GetIdentifier().GetName() << " Do" << std::endl; } else { if (doFill) { out << fillR << " " << fillG << " " << fillB << " rg" << " "; out << rRect.GetLeft() << " " << rRect.GetBottom() << " " << rRect.GetWidth() << " " << rRect.GetHeight() << " re" << " "; out << "f" << " "; //fill rect } out << strokeR << " " << strokeG << " " << strokeB << " RG" << " "; out << "2 J" << " "; // line capability style out << "0.5 w" << " "; //line width double left, bottom, right, top, whalf, hhalf; left = rRect.GetLeft(); bottom = rRect.GetBottom(); right = left + rRect.GetWidth(); top = bottom + rRect.GetHeight(); whalf = rRect.GetWidth() / 2; hhalf = rRect.GetHeight() / 2; switch (eTilingType) { case ePdfTilingPatternType_BDiagonal: out << left << " " << bottom << " m " << right << " " << top << " l "; out << left - whalf << " " << top - hhalf << " m " << left + whalf << " " << top + hhalf << " l "; out << right - whalf << " " << bottom - hhalf << " m " << right + whalf << " " << bottom + hhalf << " l" << std::endl; break; case ePdfTilingPatternType_Cross: out << left << " " << bottom + hhalf << " m " << right << " " << bottom + hhalf << " l "; out << left + whalf << " " << bottom << " m " << left + whalf << " " << top << " l" << std::endl; break; case ePdfTilingPatternType_DiagCross: out << left << " " << bottom << " m " << right << " " << top << " l "; out << left << " " << top << " m " << right << " " << bottom << " l" << std::endl; break; case ePdfTilingPatternType_FDiagonal: out << left << " " << top << " m " << right << " " << bottom << " l "; out << left - whalf << " " << bottom + hhalf << " m " << left + whalf << " " << bottom - hhalf << " l "; out << right - whalf << " " << top + hhalf << " m " << right + whalf << " " << top - hhalf << " l" << std::endl; break; case ePdfTilingPatternType_Horizontal: out << left << " " << bottom + hhalf << " m " << right << " " << bottom + hhalf << " l "; break; case ePdfTilingPatternType_Vertical: out << left + whalf << " " << bottom << " m " << left + whalf << " " << top << " l" << std::endl; break; case ePdfTilingPatternType_Image: /* This is handled above, based on the 'pImage' variable */ default: PODOFO_RAISE_ERROR (ePdfError_InvalidEnumValue); break; } out << "S"; //stroke path } TVecFilters vecFlate; vecFlate.push_back( ePdfFilter_FlateDecode ); std::string str = out.str(); PdfMemoryInputStream stream(str.c_str(), str.length()); this->GetObject()->GetStream()->Set(&stream, vecFlate); }