wxIScan
wxpoppler.h
Go to the documentation of this file.
00001 /***************************************************************
00002  * Name:      wxpoppler.h
00003  * Purpose:   wxWidgets wrapper around libpoppler for PDF file
00004  *            viewing.
00005  * Author:    Daniel Nell (daniel.nell@nellresearch.de)
00006  * Created:   2008-07-08
00007  * Copyright: Daniel Nell (www.nellresearch.de)
00008  * License:
00009  **************************************************************/
00010 
00011 #ifndef WXPOPPLER_H
00012 #define WXPOPPLER_H
00013 
00014 #include <poppler.h>
00015 #if __WXPOPPLER_DYNAMIC__
00016 #   include <wx/dynlib.h>
00017 #endif // __WXPOPPLER_DYNAMIC__
00018 #include <wx/image.h>
00019 
00020 
00021 /////////////////////////////////////////////////////////////////////////////
00022 //  Defines
00023 //
00024 #if __WXPOPPLER_DYNAMIC__
00025 #   define WXPOPPPLER_LIBPOPPLER    "poppler-glib"
00026 #endif // __WXPOPPLER_DYNAMIC__
00027 
00028 
00029 /////////////////////////////////////////////////////////////////////////////
00030 //  Class wxPoppler
00031 //
00032 /// \brief A class to handle PDF files using libpoppler.
00033 ///
00034 /// ...
00035 class wxPoppler
00036 {
00037   public:
00038     /// \brief Standard constructor.
00039     wxPoppler();
00040 
00041     /// \brief Virtual destructor.
00042     virtual ~wxPoppler();
00043 
00044   public:
00045     /// \brief Opens a PDF file.
00046     ///
00047     /// \param strFileName          The name of the PDF file.
00048     ///
00049     /// \return                     Returns true on success and false otherwise.
00050     virtual bool Open( const wxString& strFileName )
00051     {
00052         return Open( NULL, 0, strFileName );
00053     }
00054 
00055     /// \brief Opens a PDF file in memory.
00056     ///
00057     /// \param pcData               A pointer to the buffer containing the PDF document.
00058     /// \param nLength              The length (in Byte) of the PDF document.
00059     ///
00060     /// \return                     Returns true on success and false otherwise.
00061     virtual bool Open( char *pcData, const int nLength )
00062     {
00063         return Open( pcData, nLength, wxEmptyString );
00064     }
00065 
00066     /// \brief Get the number of pages in the PDF document.
00067     virtual int  GetPageCount()
00068     {
00069         return poppler_document_get_n_pages( m_pPdfDocument );
00070     }
00071 
00072     /// \brief Select the given page number to render by a consecutive call to RenderPage().
00073     ///
00074     /// \param nIndex               Zero based page number.
00075     ///
00076     /// \return                     Returns true on success and false otherwise.
00077     virtual bool SelectPage( int nIndex );
00078 
00079     /// \brief Render the (previously selected) PDF page and store the result in m_oImage.
00080     virtual bool RenderPage();                        ;
00081 
00082     /// \brief Set the dpi parameter to the given value. Returns the old dpi value.
00083     ///
00084     /// \param nDpi                 The new dpi value.
00085     ///
00086     /// \return                     Returns the old dpi value.
00087     virtual int SetDpi( int nDpi )
00088     {
00089         int nOldDpi= m_nDpi;
00090 
00091         m_nDpi= nDpi;
00092         return nOldDpi;
00093     }
00094 
00095     /// \brief Get the rendered PDF page as wxImage object.
00096     virtual wxImage GetImage()
00097     {
00098         return m_oImage;
00099     }
00100 
00101   protected:
00102     /// \brief Opens a PDF stream from file or memory depending on the parameterization.
00103     ///
00104     /// \param pcData               A pointer to the memory buffer.
00105     /// \param nLength              The length of the PDF data in the memory buffer.
00106     /// \param strFileName          The name of the PDF file.
00107     ///
00108     /// \return                     Returns true on success and false otherwise.
00109     ///
00110     /// If there is both a memory buffer (in pcData parameter) and its lenght
00111     /// (in nLength parameter) given the PDF stream is read from the memory
00112     /// buffer. Otherwise it is read from the file given in the strFileName
00113     /// parameter.
00114     ///
00115     /// Note:
00116     ///         This function is protected. It is implicitely called by the public
00117     ///          overloaded Open() functions.
00118     bool Open( char *pcData, const int nLength, const wxString& strFileName );
00119 
00120   public:
00121     static int m_nGlobalDpi;                              ///< Holds the standart image resolution in dots per inch for presetting m_nDpi.
00122 
00123   protected:
00124     wxImage m_oImage;                                     ///< Image object rendered by RenderPage().
00125     int m_nDpi;                                           ///< Holds the image resolution in dots per inch.
00126     PopplerPage *m_pPdfPage;                              ///< A pointer to the current PDF page.
00127     PopplerDocument *m_pPdfDocument;                      ///< A pointer to the current PDF document.
00128 
00129 #if __WXPOPPLER_DYNAMIC__
00130   private:
00131     static wxDynamicLibrary m_oWxPopplerDynamicLibrary;
00132 
00133     typedef PopplerDocument * t_poppler_document_new_from_data( char *, int, const char *, GError ** );
00134     typedef PopplerDocument * t_poppler_document_new_from_file( const char *,const char *, GError ** );
00135     typedef PopplerPage * t_poppler_document_get_page( PopplerDocument *, int );
00136     typedef void t_poppler_page_render_to_pixbuf( PopplerPage *, int, int, int, int, double, int, GdkPixbuf * );
00137     typedef void t_poppler_page_get_size( PopplerPage *, double *, double * );
00138     typedef int t_poppler_document_get_n_pages( PopplerDocument * );
00139 
00140     static t_poppler_document_new_from_data *poppler_document_new_from_data;
00141     static t_poppler_document_new_from_file *poppler_document_new_from_file;
00142     static t_poppler_document_get_page *poppler_document_get_page;
00143     static t_poppler_page_render_to_pixbuf *poppler_page_render_to_pixbuf;
00144     static t_poppler_page_get_size *poppler_page_get_size;
00145     static t_poppler_document_get_n_pages *poppler_document_get_n_pages;
00146 #endif // __WXPOPPLER_DYNAMIC__
00147 };
00148 
00149 
00150 #if wxUSE_PDF
00151 /////////////////////////////////////////////////////////////////////////////
00152 //  Enum wxBitmapTypePdf
00153 //
00154 /// \brief ...
00155 ///
00156 /// ...
00157 enum wxBitmapTypePdf
00158 {
00159     wxBITMAP_TYPE_PDF = wxBITMAP_TYPE_ANY + 1047
00160 };
00161 
00162 
00163 /////////////////////////////////////////////////////////////////////////////
00164 //  Class wxPDFHandler
00165 //
00166 /// \brief Image handler to handle PDF files in a way e. g. the BMP or JPEG
00167 ///        file handler does it.
00168 ///
00169 /// ...
00170 ///
00171 /// NOTE:
00172 ///
00173 /// Only opening of PDF files is implemented, yet. But it is possible to
00174 /// implement saving, too, using wxPdfDocument.
00175 ///
00176 class wxPDFHandler : public wxImageHandler
00177 {
00178   public:
00179     /// Standard constructor.
00180     wxPDFHandler()
00181       : m_nPageCount( 0 )
00182     {
00183         m_name= wxT( "wxPDFHandler" );
00184         m_extension= wxT( "pdf" );
00185         m_type= (wxBitmapType)wxBITMAP_TYPE_PDF;
00186         m_mime= wxT( "application/pdf" );
00187     }
00188 
00189 
00190 #if wxUSE_STREAMS
00191   public:
00192     /// \brief Loads an image from a stream, putting the resulting data
00193     ///        into image. If the image file contains more than one image,
00194     ///        nIndex indicates which image to read from the stream.
00195     ///
00196     /// \param poImage      A pointer to the image object which
00197     ///                     is to be affected by this operation.
00198     /// \param oIStream     Opened input stream for reading image data.
00199     /// \param bVerbose     If set to true, errors reported by the image
00200     ///                     handler will produce wxLogMessages.
00201     /// \param nIndex       The index of the image in the file (starting
00202     ///                     from zero).
00203     virtual bool LoadFile( wxImage* poImage, wxInputStream& oIStream, bool bVerbose= true, int nIndex= 0 );
00204 
00205     /// \brief Saves a given image in the output stream.
00206     ///
00207     /// \param poImage      A pointer to the image object which
00208     ///                     is to be affected by this operation.
00209     /// \param oOStream     Opened output stream for writing the data.
00210     virtual bool SaveFile(wxImage* poImage, wxOutputStream& oOStream, bool bVerbose= true );
00211 
00212 #  if wxCHECK_VERSION( 2, 9, 0 )
00213 #  else  // wxCHECK_VERSION( 2, 9, 0 )
00214     /// \brief If the image file contains more than one image, this
00215     ///        function will return the number of available images.
00216     ///
00217     /// \param oIStream     Opened input stream for reading image data
00218     ///                     (ignored).
00219     virtual int GetImageCount( wxInputStream& WXUNUSED( oIStream ) ){ return m_nPageCount; }
00220 #  endif // wxCHECK_VERSION( 2, 9, 0 )
00221 
00222 
00223   protected:
00224     /// Check if the file contains a PDF document.
00225     ///
00226     /// \param oIStream     Opened input stream for reading image data.
00227     virtual bool DoCanRead( wxInputStream& oIStream );
00228 
00229 #  if wxCHECK_VERSION( 2, 9, 0 )
00230     /// \brief If the image file contains more than one image, this
00231     ///        function will return the number of available images.
00232     ///
00233     /// \param oIStream     Opened input stream for reading image data
00234     ///                     (ignored).
00235     virtual int DoGetImageCount( wxInputStream& WXUNUSED( oIStream ) ){ return m_nPageCount; }
00236 #  else  // wxCHECK_VERSION( 2, 9, 0 )
00237 #  endif // wxCHECK_VERSION( 2, 9, 0 )
00238 #endif // wxUSE_STREAMS
00239 
00240   private:
00241     int m_nPageCount;   ///< Number of pages contained in the PDF file.
00242 
00243   private:
00244     DECLARE_DYNAMIC_CLASS( wxPDFHandler )
00245 };
00246 #endif // wxUSE_PDF
00247 
00248 #endif // WXPOPPLER_H