Changeset 155

Show
Ignore:
Timestamp:
05/09/06 06:55:01 (2 years ago)
Author:
jordi
Message:

The PDF document now can actually search for a text in a page and return the rectangles with the position of the found text. The rectangle is kept in a new class called DocumentRectangle? that just holds the rectangle corners' coordinates.

The DocumentLink? class has been also updated to use the new DocumentRectangle? class instead of holding the rectangles coordinates by itself.

Location:
trunk
Files:
2 added
8 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/DocumentLink.cxx

    r148 r155  
    3434{ 
    3535    m_DestinationPage = destinationPage; 
    36     m_X1 = x1; 
    37     m_X2 = x2; 
    38     m_Y1 = y1; 
    39     m_Y2 = y2; 
     36    m_Rect = new DocumentRectangle (x1, y1, x2, y2); 
    4037} 
    4138 
     
    4542DocumentLink::~DocumentLink () 
    4643{ 
     44    delete m_Rect; 
    4745} 
    4846 
     
    7270DocumentLink::positionIsOver (gint x, gint y) 
    7371{ 
    74     return ( (gint)m_X1 <= x && (gint)m_Y1 <= y && 
    75              (gint)m_X2 >= x && (gint)m_Y2 >= y ); 
     72    return ( (gint)m_Rect->getX1 () <= x && (gint)m_Rect->getY1 () <= y && 
     73             (gint)m_Rect->getX2 () >= x && (gint)m_Rect->getY2 () >= y ); 
    7674} 
  • trunk/src/DocumentLink.h

    r148 r155  
    4242            /// The number of the link's destination page. 
    4343            gint m_DestinationPage; 
    44             /// The X coordinate of the link's top-left corner. 
    45             gdouble m_X1; 
    46             /// The X coordinate of the link's bottom-right corner. 
    47             gdouble m_X2; 
    48             /// The Y coordinate of the link's top-left corner. 
    49             gdouble m_Y1; 
    50             /// The Y coordinate of the link's bottom-right corner. 
    51             gdouble m_Y2; 
     44            /// The link rectangle. 
     45            DocumentRectangle *m_Rect; 
    5246    }; 
    5347} 
  • trunk/src/Makefile.am

    r154 r155  
    1313    DocumentPage.h      \ 
    1414    DocumentPage.cxx    \ 
     15    DocumentRectangle.cxx   \ 
     16    DocumentRectangle.h \ 
    1517    epdfview.h          \ 
    1618    FindPter.cxx    \ 
  • trunk/src/PDFDocument.cxx

    r154 r155  
    5757PDFDocument::findTextInPage (gint pageNum, const gchar *textToFind) 
    5858{ 
    59     return NULL; 
     59    GList *results = NULL; 
     60     
     61    if ( NULL == m_Document ) 
     62    {     
     63        return results; 
     64    } 
     65 
     66    PopplerPage *page = poppler_document_get_page (m_Document, pageNum - 1); 
     67    if ( NULL != page ) 
     68    { 
     69        gdouble height = 1.0; 
     70        poppler_page_get_size (page, NULL, &height); 
     71        gdouble scale = getZoom ();         
     72        GList *matches = poppler_page_find_text (page, textToFind); 
     73        for ( GList *match = g_list_first (matches) ; 
     74              NULL != match ; 
     75              match = g_list_next (match) ) 
     76        { 
     77            PopplerRectangle *matchRect = (PopplerRectangle *)match->data; 
     78            DocumentRectangle *rect =  
     79                new DocumentRectangle (matchRect->x1 * scale, 
     80                                       (height - matchRect->y2) * scale, 
     81                                       matchRect->x2 * scale, 
     82                                       (height - matchRect->y1) * scale); 
     83            results = g_list_prepend (results, rect); 
     84        } 
     85    } 
     86 
     87    return g_list_reverse (results); 
    6088} 
    6189 
     
    167195{ 
    168196    g_assert (NULL != m_Document && "The document has not been loaded."); 
    169  
    170197 
    171198    gchar *author = NULL; 
  • trunk/src/epdfview.h

    r154 r155  
    2424#include <Config.h> 
    2525 
     26#include <DocumentRectangle.h> 
    2627#include <DocumentLink.h> 
    2728#include <DocumentOutline.h> 
  • trunk/tests/Makefile.am

    r153 r155  
    2525    FindPterTest.h  \ 
    2626    main.cxx                \ 
     27    PDFDocumentTest.cxx \ 
     28    PDFDocumentTest.h   \ 
    2729    Utils.cxx               \ 
    2830    Utils.h 
     
    3234#   PagePterTest.cxx 
    3335#   PagePterTest.h 
    34 #   PDFDocumentTest.cxx 
    35 #   PDFDocumentTest.h 
    3636 
    3737test_epdfview_CXXFLAGS =                        \ 
  • trunk/tests/PDFDocumentTest.cxx

    r148 r155  
    621621    gchar *testFile = getTestFile ("test1.pdf"); 
    622622    m_Document->load (testFile, NULL); 
     623    g_free (testFile); 
    623624    while ( !m_Observer->loadFinished () ) { } 
    624625    CPPUNIT_ASSERT (m_Observer->notifiedLoaded ()); 
     
    669670    CPPUNIT_ASSERT_EQUAL (5, m_Document->getCurrentPageNum ()); 
    670671} 
     672 
     673/// 
     674/// @brief Tests finding text in a page. 
     675/// 
     676void 
     677PDFDocumentTest::pageFindText () 
     678{ 
     679    gchar *testFile = getTestFile ("test1.pdf"); 
     680    m_Document->load (testFile, NULL); 
     681    g_free (testFile); 
     682    while ( !m_Observer->loadFinished () ) { } 
     683 
     684    // If a text doesn't exist on a page the list of found text is 
     685    // empty. 
     686    CPPUNIT_ASSERT (NULL == m_Document->findTextInPage (4, "nothing")); 
     687    // On the other hand, if something is found the list is filled with 
     688    // the rectangles the text is found. 
     689    GList *results = m_Document->findTextInPage (4, "first"); 
     690    CPPUNIT_ASSERT (NULL != results); 
     691    // In this case it should have found 2 results. 
     692    CPPUNIT_ASSERT_EQUAL ((guint)2, g_list_length (results)); 
     693 
     694    GList *firstResult = g_list_first (results); 
     695    { 
     696        DocumentRectangle *rect = (DocumentRectangle *)firstResult->data; 
     697        CPPUNIT_ASSERT_DOUBLES_EQUAL (82.00, rect->getX1 (), 0.0001); 
     698        CPPUNIT_ASSERT_DOUBLES_EQUAL (137.11, rect->getY1 (), 0.0001); 
     699        CPPUNIT_ASSERT_DOUBLES_EQUAL (100.34, rect->getX2 (), 0.0001); 
     700        CPPUNIT_ASSERT_DOUBLES_EQUAL (146.11, rect->getY2 (), 0.0001); 
     701    } 
     702    GList *secondResult = g_list_next (firstResult); 
     703    { 
     704        DocumentRectangle *rect = (DocumentRectangle *)secondResult->data; 
     705        CPPUNIT_ASSERT_DOUBLES_EQUAL (96.00, rect->getX1 (), 0.0001); 
     706        CPPUNIT_ASSERT_DOUBLES_EQUAL (148.11, rect->getY1 (), 0.0001); 
     707        CPPUNIT_ASSERT_DOUBLES_EQUAL (114.34, rect->getX2 (), 0.0001); 
     708        CPPUNIT_ASSERT_DOUBLES_EQUAL (157.11, rect->getY2 (), 0.0001); 
     709    } 
     710    g_list_free (results); 
     711} 
  • trunk/tests/PDFDocumentTest.h

    r147 r155  
    3737        CPPUNIT_TEST (pageRender); 
    3838        CPPUNIT_TEST (pageLinks); 
     39        CPPUNIT_TEST (pageFindText); 
    3940        CPPUNIT_TEST_SUITE_END (); 
    4041 
     
    5455            void pageRender (void); 
    5556            void pageLinks (void); 
     57            void pageFindText (void); 
    5658             
    5759        private: