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

void PoDoFo::PdfFontType1::EmbedFontFile ( PdfObject pDescriptor ) [protected, virtual]

Embed the font file directly into the PDF file.

Parameters:
pDescriptorfont descriptor object

Implements PoDoFo::PdfFontSimple.

Definition at line 105 of file PdfFontType1.cpp.

References PoDoFo::PdfDictionary::AddKey(), PoDoFo::PdfVecObjects::CreateObject(), PoDoFo::ePdfError_FileNotFound, PoDoFo::ePdfError_InvalidHandle, PoDoFo::ePdfError_OutOfMemory, PoDoFo::PdfVariant::GetDictionary(), PoDoFo::PdfFontMetrics::GetFilename(), PoDoFo::PdfFontMetrics::GetFontData(), PoDoFo::PdfFontMetrics::GetFontDataLen(), PoDoFo::PdfElement::GetObject(), PoDoFo::PdfObject::GetOwner(), PoDoFo::PdfObject::GetStream(), PODOFO_RAISE_ERROR, PODOFO_RAISE_ERROR_INFO, PoDoFo::PdfObject::Reference(), and PoDoFo::PdfStream::Set().

Referenced by EmbedSubsetFont().

{
    pdf_long    lSize    = 0;
    pdf_int64   lLength1 = 0L;
    pdf_int64   lLength2 = 0L;
    pdf_int64   lLength3 = 0L;
    PdfObject*  pContents;
    const char* pBuffer;
    char*       pAllocated = NULL;

      if (m_isBase14) 
      {
            m_bWasEmbedded = false;
            return;
      }

    m_bWasEmbedded = true;

    pContents = this->GetObject()->GetOwner()->CreateObject();
    if( !pContents )
    {
        PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
    }
        
    pDescriptor->GetDictionary().AddKey( "FontFile", pContents->Reference() );

    // if the data was loaded from memory - use it from there
    // otherwise, load from disk
    if ( m_pMetrics->GetFontDataLen() && m_pMetrics->GetFontData() ) 
    {
        pBuffer = m_pMetrics->GetFontData();
        lSize   = m_pMetrics->GetFontDataLen();
    }
    else
    {
        FILE* hFile = fopen( m_pMetrics->GetFilename(), "rb" );
        if( !hFile )
        {
            PODOFO_RAISE_ERROR_INFO( ePdfError_FileNotFound, m_pMetrics->GetFilename() );
        }

        fseek( hFile, 0L, SEEK_END );
        lSize = ftell( hFile );
        fseek( hFile, 0L, SEEK_SET );

        pAllocated = static_cast<char*>(malloc( sizeof(char) * lSize ));
        if( !pAllocated )
        {
            fclose( hFile );
            PODOFO_RAISE_ERROR( ePdfError_OutOfMemory );
        }

        fread( pAllocated, sizeof(char), lSize, hFile );
        fclose( hFile );

        pBuffer = pAllocated;
    }

      // Remove binary segment headers from pfb
      unsigned char *pBinary = reinterpret_cast<unsigned char*>(const_cast<char*>(pBuffer));
      while( *pBinary == 0x80 )     // binary segment header
      {
            const int   cHeaderLength  = 6;
            int               iSegmentType   = pBinary[1];  // binary segment type
            long        lSegmentLength = 0L;
            long        lSegmentDelta  = static_cast<long>(&pBuffer[lSize] - reinterpret_cast<const char*>(pBinary) );

            switch( iSegmentType )
            {
                  case 1:                                                     // ASCII text
                        lSegmentLength = pBinary[2] +             // little endian
                                                 pBinary[3] * 256L + 
                                                 pBinary[4] * 65536L +
                                                 pBinary[5] * 16777216L;
                        if( lLength1 == 0L )
                              lLength1 = lSegmentLength;
                        else
                              lLength3 = lSegmentLength;
                        lSize -= cHeaderLength;
                        memmove( pBinary, &pBinary[cHeaderLength], lSegmentDelta );
                        pBinary = &pBinary[lSegmentLength];
                        break;
                  case 2:                                                     // binary data
                        lSegmentLength = pBinary[2] +             // little endian
                                                 pBinary[3] * 256L + 
                                                 pBinary[4] * 65536L +
                                                 pBinary[5] * 16777216L;
                        lLength2 = lSegmentLength;
                        lSize -= cHeaderLength;
                        memmove( pBinary, &pBinary[cHeaderLength], lSegmentDelta );
                        pBinary = &pBinary[lSegmentLength];
                        break;
                  case 3:                                                     // end-of-file
                        pContents->GetStream()->Set( pBuffer, lSize - 2L );
                        if( pAllocated )
                              free( pAllocated );

                        pContents->GetDictionary().AddKey( "Length1", PdfVariant( lLength1 ) );
                pContents->GetDictionary().AddKey( "Length2", PdfVariant( lLength2 ) );
                pContents->GetDictionary().AddKey( "Length3", PdfVariant( lLength3 ) );

                        return;
                  default:
                        break;
            }
      }

      // Parse the font data buffer to get the values for length1, length2 and length3
      lLength1 = FindInBuffer( "eexec", pBuffer, lSize );
      if( lLength1 > 0 )
            lLength1 += 6; // 6 == eexec + lf
      else
            lLength1 = 0;

      if( lLength1 )
      {
            lLength2 = FindInBuffer( "cleartomark", pBuffer, lSize );
            if( lLength2 > 0 )
                  lLength2 = lSize - lLength1 - 520; // 520 == 512 + strlen(cleartomark)
            else
                  lLength1 = 0;
      }

      lLength3 = lSize - lLength2 - lLength1;
    
      // TODO: Pdf Supports only Type1 fonts with binary encrypted sections and not the hex format
      pContents->GetStream()->Set( pBuffer, lSize );
    if( pAllocated )
        free( pAllocated );

    pContents->GetDictionary().AddKey( "Length1", PdfVariant( lLength1 ) );
    pContents->GetDictionary().AddKey( "Length2", PdfVariant( lLength2 ) );
    pContents->GetDictionary().AddKey( "Length3", PdfVariant( lLength3 ) );
}

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index