Changeset 21

Show
Ignore:
Timestamp:
04/11/06 08:14:31 (3 years ago)
Author:
jordi
Message:

I added the dumb document and main view for testing.

I had to create a Document Interface (IDocument) to handle the dumb document for tests. I moved the Document.{cxx,h} files to PDFDocument.{cxx,h}. The rotation, page change and scaling functions remains in IDocument, as well as the error functions and enumerations. The PDF specific portions of the document has been kept in the PDFDocument class.

The test name is also updated to PDFDocumentTest, and all tests runs OK.

Location:
trunk
Files:
4 added
5 modified
4 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile.am

    r19 r21  
    33 
    44libepdfview_a_SOURCES = \ 
    5     Document.cxx        \ 
    6     Document.h          \ 
    75    DocumentIndex.cxx   \ 
    86    DocumentIndex.h     \ 
     
    108    DocumentPage.cxx    \ 
    119    epdfview.h          \ 
    12     gettext.h 
     10    gettext.h           \ 
     11    IDocument.cxx       \ 
     12    IDocument.h         \ 
     13    MainPter.cxx        \ 
     14    MainPter.h          \ 
     15    PDFDocument.cxx     \ 
     16    PDFDocument.h 
    1317 
    1418libepdfview_a_CXXFLAGS =    \ 
  • trunk/src/PDFDocument.cxx

    r19 r21  
    2626 
    2727// Constants. 
    28 static const gdouble ZoomInFactor = 1.2f; 
    29 static const gdouble ZoomInMax = 4.0f; 
    30 static const gdouble ZoomOutFactor = (1.0f / ZoomInFactor); 
    31 static const gdouble ZoomOutMax = 0.05409; 
    32 static const gint cairoBytesPerPixel = 4; 
     28static const gint CAIRO_BYTES_PER_PIXEL = 4; 
    3329 
    3430// Forward declarations. 
     
    3935static PageMode convertPageMode (Catalog::PageMode pageMode); 
    4036 
    41 /// This is the error domain that will be used to report Document's errors. 
    42 GQuark Document::errorQuark = 0; 
    43  
    44 /// 
    45 /// @brief Gets the Document's error quark. 
    46 /// 
    47 /// The first time it's called will create the new quark value for the  
    48 /// document class. Successive calls will just return the previously created 
    49 /// quark value. 
    50 /// 
    51 /// @return The Document's quark. 
    52 /// 
    53 GQuark 
    54 Document::getErrorQuark () 
    55 { 
    56     if ( 0 == Document::errorQuark ) 
    57         Document::errorQuark = g_quark_from_static_string ("epdfview-document"); 
    58  
    59     return Document::errorQuark; 
    60 } 
    61  
    62 /// 
    63 /// @brief Gives the error's description. 
    64 /// 
    65 /// This function looks the error at @a errorCode and gives an human 
    66 /// readable error string. 
    67 /// 
    68 /// The error message must be freed using g_free(). 
    69 /// 
    70 /// @param errorCode The error code to get the string from. 
    71 /// 
    72 /// @return An human readable error message. 
    73 /// 
    74 gchar * 
    75 Document::getErrorMessage (DocumentError errorCode) 
    76 { 
    77     gchar *errorMessage = NULL; 
    78     switch (errorCode) 
    79     { 
    80         case DocumentErrorNone: 
    81             errorMessage = g_strdup (_("No error")); 
    82             break; 
    83         case DocumentErrorOpenFile: 
    84             errorMessage = g_strdup (_("File not found.")); 
    85             break; 
    86         case DocumentErrorBadCatalog: 
    87             errorMessage = g_strdup (_("Couldn't read the page catalog.")); 
    88             break; 
    89         case DocumentErrorDamaged: 
    90             errorMessage = g_strdup (_("The PDF file is damaged and can't be repaired.")); 
    91             break; 
    92         case DocumentErrorEncrypted: 
    93             errorMessage = g_strdup (_("The file is encrypted and the password was incorrect or not supplied.")); 
    94             break; 
    95         case DocumentErrorHighlightFile: 
    96             errorMessage = g_strdup (_("Nonexistent or invlid highlight file.")); 
    97             break; 
    98         case DocumentErrorBadPrinter: 
    99             errorMessage = g_strdup (_("Invalid printer.")); 
    100             break; 
    101         case DocumentErrorPrinting: 
    102             errorMessage = g_strdup (_("Error during printing.")); 
    103             break; 
    104         case DocumentErrorPermission: 
    105             errorMessage = g_strdup (_("The PDF file doesn't allow that operation.")); 
    106             break; 
    107         case DocumentErrorBadPageNumber: 
    108             errorMessage = g_strdup (_("Invalid page number.")); 
    109             break; 
    110         case DocumentErrorFileIO: 
    111             errorMessage = g_strdup (_("File I/O error.")); 
    112             break; 
    113         default: 
    114             errorMessage = g_strdup_printf (_("Unknown error (%d)"), errorCode);             
    115     } 
    116  
    117     g_assert (NULL != errorMessage && "The error message is NULL"); 
    118     return errorMessage; 
    119 } 
    120  
    12137/// 
    12238/// @brief Constructs a new Document object. 
    12339/// 
    124 Document::Document () 
     40PDFDocument::PDFDocument (): 
     41    IDocument () 
    12542{ 
    12643    m_Author = NULL; 
    12744    m_CreationDate = NULL; 
    12845    m_Creator = NULL; 
    129     m_CurrentPage = 0; 
    13046    m_Document = NULL; 
    131     m_DocumentIndex = new DocumentIndex (); 
    132     m_FileName = NULL; 
    13347    m_Format = NULL; 
    13448    m_Keywords = NULL; 
     
    14054    m_PageNumber = 0; 
    14155    m_Producer = NULL; 
    142     m_Rotation = 0; 
    143     m_Scale = 0.0f; 
    14456    m_Subject = NULL; 
    14557    m_Title = NULL; 
     
    14961/// @brief Deletes all dynamically created objects of Document. 
    15062/// 
    151 Document::~Document () 
     63PDFDocument::~PDFDocument () 
    15264{ 
    15365    delete m_Document; 
    154     delete m_DocumentIndex; 
    15566    delete m_OutputDevice; 
    15667    g_free (m_Author); 
    15768    g_free (m_CreationDate); 
    15869    g_free (m_Creator); 
    159     g_free (m_FileName); 
    16070    g_free (m_Format); 
    16171    g_free (m_Keywords); 
     
    17383/// 
    17484gboolean 
    175 Document::isLoaded () 
     85PDFDocument::isLoaded () 
    17686{ 
    17787    return (NULL != m_Document && m_Document->isOk ()); 
     
    191101/// 
    192102gboolean 
    193 Document::loadFile (const gchar *filename, const gchar *password,  
     103PDFDocument::loadFile (const gchar *filename, const gchar *password,  
    194104                    GError **error) 
    195105{ 
     
    220130        DocumentError errorCode = (DocumentError)newDocument->getErrorCode (); 
    221131        delete newDocument; 
    222         gchar *errorMessage = Document::getErrorMessage (errorCode); 
     132        gchar *errorMessage = IDocument::getErrorMessage (errorCode); 
    223133        g_set_error (error,  
    224134                     EPDFVIEW_DOCUMENT_ERROR, errorCode, 
     
    230140    } 
    231141    
    232     g_free (m_FileName); 
    233     m_FileName = g_strdup (filename); 
     142    setFileName (filename); 
    234143    delete m_Document; 
    235144    m_Document = newDocument; 
     
    250159/// 
    251160const gchar * 
    252 Document::getTitle () 
     161PDFDocument::getTitle () 
    253162{ 
    254163    if ( NULL == m_Title ) 
     
    266175/// 
    267176const gchar * 
    268 Document::getAuthor () 
     177PDFDocument::getAuthor () 
    269178{ 
    270179    if ( NULL == m_Author ) 
     
    282191/// 
    283192const gchar * 
    284 Document::getSubject () 
     193PDFDocument::getSubject () 
    285194{ 
    286195    if ( NULL == m_Subject ) 
     
    298207/// 
    299208const gchar * 
    300 Document::getKeywords () 
     209PDFDocument::getKeywords () 
    301210{ 
    302211    if ( NULL == m_Keywords ) 
     
    314223/// 
    315224const gchar * 
    316 Document::getCreator () 
     225PDFDocument::getCreator () 
    317226{ 
    318227    if ( NULL == m_Creator ) 
     
    330239/// 
    331240const gchar * 
    332 Document::getProducer () 
     241PDFDocument::getProducer () 
    333242{ 
    334243    if ( NULL == m_Producer ) 
     
    337246    } 
    338247    return m_Producer; 
    339 } 
    340  
    341 /// 
    342 /// @brief Get the document's file name. 
    343 /// 
    344 /// @return The file name that contains the document, or an empty string 
    345 ///         if the document isn't loeaded yet. 
    346 /// 
    347 const gchar * 
    348 Document::getFileName () 
    349 { 
    350     if ( NULL == m_FileName ) 
    351     { 
    352         return ""; 
    353     } 
    354     return m_FileName; 
    355248} 
    356249 
     
    363256/// 
    364257const gchar * 
    365 Document::getFormat () 
     258PDFDocument::getFormat () 
    366259{ 
    367260    if ( NULL == m_Format ) 
     
    380273/// 
    381274const gchar * 
    382 Document::getLinearized () 
     275PDFDocument::getLinearized () 
    383276{ 
    384277    if ( NULL == m_Linearized ) 
     
    396289/// 
    397290const gchar * 
    398 Document::getCreationDate () 
     291PDFDocument::getCreationDate () 
    399292{ 
    400293    if ( NULL == m_CreationDate ) 
     
    412305/// 
    413306const gchar * 
    414 Document::getModifiedDate () 
     307PDFDocument::getModifiedDate () 
    415308{ 
    416309    if ( NULL == m_ModifiedDate ) 
     
    428321/// 
    429322PageMode 
    430 Document::getPageMode () 
     323PDFDocument::getPageMode () 
    431324{ 
    432325    return m_PageMode; 
     
    440333/// 
    441334PageLayout 
    442 Document::getPageLayout () 
     335PDFDocument::getPageLayout () 
    443336{ 
    444337    return m_PageLayout; 
     
    452345/// 
    453346gint 
    454 Document::getNumPages () 
     347PDFDocument::getNumPages () 
    455348{ 
    456349    return m_PageNumber; 
    457 } 
    458  
    459 /// 
    460 /// @brief Get the document's current page number. 
    461 /// 
    462 /// @return The number of the current page that we are viewing or 0 if the 
    463 ///         document has not been loaded yet. 
    464 /// 
    465 gint 
    466 Document::getCurrentPageNum () 
    467 { 
    468     return m_CurrentPage; 
    469 } 
    470  
    471 /// 
    472 /// @brief Get the document's index. 
    473 /// 
    474 /// @return The top level DocumentIndex for this document. 
    475 /// 
    476 DocumentIndex & 
    477 Document::getDocumentIndex () 
    478 { 
    479     g_assert (NULL != m_DocumentIndex && "The document index is NULL."); 
    480      
    481     return *(m_DocumentIndex); 
    482350} 
    483351 
     
    490358/// 
    491359void 
    492 Document::loadMetadata (void) 
     360PDFDocument::loadMetadata (void) 
    493361{ 
    494362    g_assert (NULL != m_Document && "The document has not been loaded."); 
     
    534402    // Get the number of pages and set the current to the first. 
    535403    m_PageNumber = m_Document->getNumPages (); 
    536     m_CurrentPage = 1; 
     404    goToFirstPage (); 
    537405 
    538406    // Set the current rotation angle and zoom level. 
     
    782650 
    783651/// 
    784 /// @brief Goes to the document's first page. 
    785 /// 
    786 /// Changes the current page to be the first page of the 
    787 /// document. 
    788 /// 
    789 void 
    790 Document::goToFirstPage () 
    791 { 
    792     goToPage (1); 
    793 } 
    794  
    795 /// 
    796 /// @brief Goes to the document's last page. 
    797 /// 
    798 /// Changes the current page to be the last page of the 
    799 /// document. 
    800 /// 
    801 void 
    802 Document::goToLastPage () 
    803 { 
    804     goToPage (getNumPages ()); 
    805 } 
    806  
    807 /// 
    808 /// @brief Goes the the document's next page. 
    809 /// 
    810 /// Changes the current page to be the next page. If the current page is 
    811 /// already the last, then it will not change. 
    812 /// 
    813 void 
    814 Document::goToNextPage () 
    815 { 
    816     goToPage (getCurrentPageNum() + 1); 
    817 } 
    818  
    819 /// 
    820 /// @brief Goes to a document page. 
    821 /// 
    822 /// Changes the current page to be the page @a pageNum. If @a pageNum is 
    823 /// greater than the document's last page, then the current page becomes 
    824 /// the last page. If @pageNum is instead less then the first page (i.e.,  
    825 /// <= 0) then the current page becomes the first (i.e., 1). 
    826 /// 
    827 /// @param pageNum The page index to go to. 
    828 /// 
    829 void 
    830 Document::goToPage (gint pageNum) 
    831 { 
    832     if ( pageNum > getNumPages () ) 
    833     { 
    834         pageNum = getNumPages (); 
    835     } 
    836     else if ( pageNum < 1 ) 
    837     { 
    838         pageNum = 1; 
    839     } 
    840  
    841     m_CurrentPage = pageNum; 
    842 } 
    843  
    844 /// 
    845 /// @brief Goes to the document's previous page. 
    846 /// 
    847 /// Changes the current page to be the previous page. If the current page 
    848 /// is already the first page, then it will not change. 
    849 /// 
    850 void 
    851 Document::goToPreviousPage () 
    852 { 
    853     goToPage (getCurrentPageNum () - 1); 
    854 } 
    855  
    856 /// 
    857 /// @brief Gets the current rotation degrees. 
    858 /// 
    859 /// @return The current document's rotation degrees. 
    860 /// 
    861 gint 
    862 Document::getRotation () 
    863 { 
    864     return m_Rotation; 
    865 } 
    866  
    867 /// 
    868 /// @brief Rotates 90 to the left. 
    869 /// 
    870 /// Substract 90 degress to the document's rotation. 
    871 /// 
    872 void 
    873 Document::rotateLeft () 
    874 { 
    875     m_Rotation = (m_Rotation - 90) % 360; 
    876     if ( m_Rotation < 0 ) 
    877     { 
    878         m_Rotation += 360; 
    879     } 
    880 } 
    881  
    882 /// 
    883 /// @brief Rotates 90 to the right. 
    884 /// 
    885 /// Adds 90 degress to the document's rotation. 
    886 /// 
    887 void 
    888 Document::rotateRight () 
    889 { 
    890     m_Rotation = (m_Rotation + 90) % 360; 
    891 } 
    892  
    893 /// 
    894 /// @brief Checks if is possible to zoom in. 
    895 /// 
    896 /// Checks if after zooming int the zoom level will be below the max level. 
    897 /// 
    898 gboolean 
    899 Document::canZoomIn (void) 
    900 { 
    901     return ( getZoom () * ZoomInFactor <= ZoomInMax ); 
    902 } 
    903  
    904 /// 
    905 /// @brief Checks if is possible to zoom out. 
    906 /// 
    907 /// Checks if after zoomin out the zoom level will be above the min level. 
    908 /// 
    909 gboolean 
    910 Document::canZoomOut (void) 
    911 { 
    912     return ( getZoom () * ZoomOutFactor >= ZoomOutMax ); 
    913 } 
    914  
    915 /// 
    916 /// @brief Gets the current zoom. 
    917 /// 
    918 /// @return The current zoom or scale level. 
    919 /// 
    920 gfloat 
    921 Document::getZoom (void) 
    922 { 
    923     return m_Scale; 
    924 } 
    925  
    926 /// 
    927 /// @brief Zooms In. 
    928 /// 
    929 /// Adds a fixed ammount to the scale / zoom level. 
    930 /// 
    931 void 
    932 Document::zoomIn (void) 
    933 { 
    934     if (canZoomIn ()) 
    935     { 
    936         m_Scale *= ZoomInFactor; 
    937     } 
    938 } 
    939  
    940 /// 
    941 /// @brief Zooms Out. 
    942 /// 
    943 /// Substracts a fixed amount to the scale / zoom level. 
    944 /// 
    945 void 
    946 Document::zoomOut (void) 
    947 { 
    948     if (canZoomOut ()) 
    949     { 
    950         m_Scale *= ZoomOutFactor; 
    951     } 
    952 } 
    953  
    954 /// 
    955 /// @brief Zooms the document to fit the width and the height. 
    956 /// 
    957 /// It tries to get the best zoom / scale level that will let the  
    958 /// document fit into @a width and @a height. 
    959 /// 
    960 /// @param width The width to fit the document to. 
    961 /// @param height The height to fit the document to. 
    962 /// 
    963 void 
    964 Document::zoomToFit (gint width, gint height) 
    965 { 
    966     gdouble pageWidth; 
    967     gdouble pageHeight; 
    968  
    969     getPageSize (&pageWidth, &pageHeight); 
    970     // In this case we are interested in the minimum zoom level,  
    971     // as is the one that fits best. 
    972     gdouble widthScale = (gdouble)width / pageWidth; 
    973     gdouble heightScale = (gdouble)height / pageHeight; 
    974     m_Scale = MIN (widthScale, heightScale); 
    975 } 
    976  
    977 /// 
    978 /// @brief Zooms the document to fit the width. 
    979 /// 
    980 /// It tries to get the best zooms / sclale level that will let the document 
    981 /// fit into @a width, wihtout looking into any height. 
    982 /// 
    983 /// @param width The width to fit the document to. 
    984 /// 
    985 void 
    986 Document::zoomToWidth (gint width) 
    987 { 
    988     gdouble pageWidth; 
    989     gdouble pageHeight; 
    990  
    991     getPageSize (&pageWidth, &pageHeight); 
    992     m_Scale = (gdouble)width / pageWidth; 
    993 } 
    994  
    995 /// 
    996652/// @brief Gets the current page's unscaled size. 
    997653/// 
     
    1003659/// 
    1004660void 
    1005 Document::getPageSize (gdouble *width, gdouble *height) 
     661PDFDocument::getPageSize (gdouble *width, gdouble *height) 
    1006662{ 
    1007663    g_assert (NULL != m_Document && "Tried to get size of a NULL document."); 
     
    1042698/// 
    1043699DocumentPage * 
    1044 Document::renderPage () 
     700PDFDocument::renderPage () 
    1045701{ 
    1046702    // First create the document's page. 
     
    1058714     
    1059715    // Prepare the output device. 
    1060     gint cairoRowStride = width * cairoBytesPerPixel; 
     716    gint cairoRowStride = width * CAIRO_BYTES_PER_PIXEL; 
    1061717    gint cairoSize = cairoRowStride * height; 
    1062718    guchar *cairoData = new guchar[cairoSize]; 
  • trunk/src/PDFDocument.h

    r19 r21  
    1616// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    1717 
    18 #if !defined (__DOCUMENT_H__) 
    19 #define __DOCUMENT_H__ 
    20  
    21 /// This is the definition of the GQuark used for Document's errors. 
    22 #define EPDFVIEW_DOCUMENT_ERROR     ePDFView::Document::getErrorQuark () 
     18#if !defined (__PDF_DOCUMENT_H__) 
     19#define __PDF_DOCUMENT_H__ 
    2320 
    2421namespace ePDFView  
    2522{ 
    2623    /// 
    27     /// @brief Defines the possible errors loading a document. 
    28     /// 
    29     /// This enumeration matches the definitions at ErrorCodes.h of Poppler. 
    30     /// 
    31     typedef enum 
    32     { 
    33         /// No error. 
    34         DocumentErrorNone = 0, 
    35         /// Couldn't open the PDF file. 
    36         DocumentErrorOpenFile = 1, 
    37         /// Coudln't read the page catalog. 
    38         DocumentErrorBadCatalog = 2, 
    39         /// PDF file is damaged and couldn't be repaired. 
    40         DocumentErrorDamaged = 3, 
    41         /// The file is encrypted and the password was incorrect  
    42         /// or not supplied. 
    43         DocumentErrorEncrypted = 4, 
    44         /// Nonexistent or invalid highlight file. 
    45         DocumentErrorHighlightFile = 5, 
    46         /// Invalid printer. 
    47         DocumentErrorBadPrinter = 6, 
    48         /// Error during printing. 
    49         DocumentErrorPrinting = 7, 
    50         /// PDF File doesn't allow that operation. 
    51         DocumentErrorPermission = 8, 
    52         /// Invalid page number. 
    53         DocumentErrorBadPageNumber = 9, 
    54         /// File I/O Error. 
    55         DocumentErrorFileIO = 10 
    56     } DocumentError; 
    57          
    58     /// 
    59     /// @brief Defines the document's page mode. 
    60     /// 
    61     typedef enum  
    62     { 
    63         PageModeOutlines, 
    64         PageModeThumbs, 
    65         PageModeFullScreen, 
    66         PageModeOC, 
    67         PageModeAttach, 
    68         // Not yet defined. 
    69         PageModeUnset 
    70     } PageMode; 
    71  
    72     /// 
    73     /// @brief Defines the document's page layout. 
    74     /// 
    75     typedef enum 
    76     { 
    77         PageLayoutSinglePage, 
    78         PageLayoutOneColumn, 
    79         PageLayoutTwoColumnLeft, 
    80         PageLayoutTwoColumnRight, 
    81         PageLayoutTwoPageLeft, 
    82         PageLayoutTwoPageRight, 
    83         // Not yet defined. 
    84         PageLayoutUnset 
    85     } PageLayout; 
    86  
    87     /// 
    88     /// @class Document 
     24    /// @class PDFDocument 
    8925    /// @brief Has information about a single document. 
    9026    /// 
     
    9329    /// page, etc. is inside this class. 
    9430    /// 
    95     class Document 
     31    class PDFDocument: public IDocument 
    9632    { 
    9733        public: 
    98             Document (void); 
    99             ~Document (void); 
     34            PDFDocument (void); 
     35            ~PDFDocument (void); 
    10036 
    10137            gboolean isLoaded (void); 
     
    10844            const gchar *getCreator (void); 
    10945            const gchar *getProducer (void); 
    110             const gchar *getFileName (void); 
    11146            const gchar *getFormat (void); 
    11247            const gchar *getLinearized (void); 
     
    11651            PageLayout getPageLayout (void); 
    11752            gint getNumPages (void); 
    118             gint getCurrentPageNum (void); 
    119             DocumentIndex &getDocumentIndex (void); 
    12053 
    121             void goToFirstPage (); 
    122             void goToLastPage (); 
    123             void goToNextPage (); 
    124             void goToPage (gint pageNum); 
    125             void goToPreviousPage (); 
    126  
    127             gint getRotation (void); 
    128             void rotateLeft (void); 
    129             void rotateRight (void); 
    130  
    131             gboolean canZoomIn (void); 
    132             gboolean canZoomOut (void); 
    133             gfloat getZoom (void); 
    134             void zoomIn (void); 
    135             void zoomOut (void); 
    136             void zoomToFit (gint width, gint height); 
    137             void zoomToWidth (gint width); 
    138  
     54            void getPageSize (gdouble *width, gdouble *height); 
    13955            DocumentPage *renderPage (void); 
    140  
    141             static GQuark getErrorQuark (void); 
    142             static gchar *getErrorMessage (DocumentError errorCode); 
    14356 
    14457        protected: