pdf_page.h

Go to the documentation of this file.
00001 /*
00002  * File Name: pdf_page.h
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-pdf.
00007  *
00008  * uds-plugin-pdf is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-pdf is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #ifndef PDF_PAGE_H_
00028 #define PDF_PAGE_H_
00029 
00030 #include "pdf_define.h"
00031 #include "pdf_anchor.h"
00032 #include "pdf_searcher.h"
00033 #include "pdf_observer.h"
00034 
00035 namespace pdf
00036 {
00037 typedef PDFInstanceCollection<PluginRectangle> PDFRectangles;
00038 
00039 class PDFRenderAttributes
00040 {
00041 public:
00042     PDFRenderAttributes()
00043         : zoom_setting(PLUGIN_ZOOM_DEFAULT)
00044         , real_zoom_value(PLUGIN_ZOOM_DEFAULT)
00045         , rotate(0)
00046     {}
00047 
00048     PDFRenderAttributes(const PDFRenderAttributes &attr)
00049         : zoom_setting(attr.zoom_setting)
00050         , real_zoom_value(attr.real_zoom_value)
00051         , rotate(attr.rotate)
00052     {}
00053 
00054     ~PDFRenderAttributes() {}
00055 
00056     PDFRenderAttributes& operator=(const PDFRenderAttributes& right)
00057     {
00058         if (*this == right)
00059         {
00060             return *this;
00061         }
00062 
00063         zoom_setting = right.zoom_setting;
00064         real_zoom_value = right.real_zoom_value;
00065         rotate = right.rotate;
00066         return *this;
00067     }
00068 
00069     bool operator==(const PDFRenderAttributes &right) const
00070     {
00071         return ((fabs(this->zoom_setting - right.zoom_setting) < ZERO_RANGE) &&
00072                 (fabs(this->real_zoom_value - right.real_zoom_value) < ZERO_RANGE) &&
00073                 this->rotate == right.rotate);
00074     }
00075 
00076     void set_zoom_setting(double z) {zoom_setting = z;}
00077     double get_zoom_setting() const {return zoom_setting;}
00078 
00079     void set_rotate(int r) {rotate = r;}
00080     int get_rotate() const {return rotate;}
00081 
00082     void set_real_zoom_value(double z) {real_zoom_value = z;}
00083     double get_real_zoom_value() const {return real_zoom_value;}
00084 
00085 private:
00086     // the zoom setting
00087     double zoom_setting;
00088 
00089     // the real zoom value
00090     double real_zoom_value;
00091 
00092     // the rotation degree
00093     int    rotate;
00094 
00095 };
00096 
00097 struct SearchWordRecord
00098 {
00099     int word_index;
00100     int start_char_index;
00101     int end_char_index;
00102 
00103     SearchWordRecord()
00104         : word_index(0)
00105         , start_char_index(0)
00106         , end_char_index(0)
00107     {}
00108 
00109     SearchWordRecord(int word_idx, int start_idx, int end_idx)
00110         : word_index(word_idx)
00111         , start_char_index(start_idx)
00112         , end_char_index(end_idx)
00113     {}
00114 
00115     ~SearchWordRecord() 
00116     {}
00117 };
00118 
00119 typedef PDFInstanceCollection<SearchWordRecord> SearchWords;
00120 
00121 class PDFController;
00122 class PDFRenderer;
00123 class TextWordQueue;
00124 /// The page information.
00125 class PDFPage
00126 {
00127 public:
00128     /// Render status, 
00129     /// it would be used when submitting a new render task
00130     typedef enum
00131     {
00132         RENDER_DONE = 0,
00133         RENDER_RUNNING,
00134         RENDER_STOP
00135     }RenderStatus;
00136 
00137 public:
00138     PDFPage(int page_num, const PDFRenderAttributes &attr);
00139 
00140     ~PDFPage(void);
00141 
00142     ///  Return the size_t value based on the pagenumber
00143     size_t operator()();
00144 
00145     ///  Comparing
00146     bool operator == (const PDFPage &right);
00147     bool operator == (const PDFRenderAttributes &right);
00148 
00149     /// Destroy the resource of the page
00150     unsigned int destroy();
00151 
00152     ///  Lock the page so that it has the highest priority(cannot
00153     /// be removed until UDS unlocks it)
00154     void lock() { b_lock = true; }
00155 
00156     ///  Unlock the page
00157     void unlock() { b_lock = false; }
00158 
00159     ///  Is current page be locked
00160     bool locked() const { return b_lock; }
00161 
00162     ///  Set the render attributes
00163     ///  return true means setting succeed, or there is no change
00164     ///  return false means the page is in rendering, cannot change
00165     ///  the setting now
00166     void set_render_attr(const PDFRenderAttributes &attr);
00167 
00168     ///  Get the render attributes
00169     const PDFRenderAttributes& get_render_attr() const { return render_attr; }
00170 
00171     ///  Get/Set the reference id
00172     int get_ref_id() { return ref_id; }
00173     void set_ref_id(int id) { ref_id = id; }
00174 
00175     int get_page_num() const { return page_number; }
00176     int get_bitmap_width();
00177     int get_bitmap_height();
00178     int get_bitmap_row_stride();
00179     SplashBitmap* get_bitmap() const { return bitmap; }
00180     Links* get_links() const { return links; }
00181     TextPage* get_text() const { return text; }
00182     TextWordList* get_words_list();
00183     PDFController* get_doc_controller() const { return doc_controller; }
00184     void set_doc_controller(PDFController* doc) { doc_controller = doc; }
00185 
00186     ///  Get the data length of the bitmap
00187     unsigned int length();
00188 
00189     ///  Get the data of the bitmap
00190     const unsigned char* get_bitmap_data();
00191 
00192     /// Estimate whether the input is hyperlink page of current page
00193     bool is_hyper_linked_page(int dst_page_num);
00194 
00195     // Render functions
00196     ///  Render a splash page by render attributes passed in
00197     bool render_splash_map(PDFRenderer *renderer, void *abort_data);
00198 
00199     ///  Render a text page by render attributes passed in
00200     bool render_text(PDFRenderer *renderer, bool use_defalt_setting = false);
00201 
00202     // Search functions
00203     ///  Search in the current PDFPage. Make sure the text page is
00204     /// rendered before searching
00205     SearchResult search(SearchContext &ctx, PDFSearchPage &results);
00206 
00207     ///  Get the bounding rectangles in the passed-in range
00208     bool get_bounding_rectangles(const string &start_anchor
00209                                  , const string &end_anchor
00210                                  , PDFRectangles &rects);
00211 
00212     ///  Get rendering status
00213     RenderStatus get_render_status() {return render_status;}
00214 
00215     ///  Get the anchor of (x, y).
00216     void get_anchor_param_from_coordinates(double x, double y, PDFAnchor &param);
00217 
00218     ///  Get the destination's page number of the link
00219     int  get_goto_page_of_link(int link_index);
00220 
00221     ///  Get the destination's anchor of the link.
00222     bool get_goto_anchor_of_link(int link_index, std::string & anchor);
00223 
00224     /// Get the range of link by link index
00225     bool get_range_param_by_link_index(const int link_index,
00226                                        PDFAnchor & start_param,
00227                                        PDFAnchor & end_param);
00228 
00229     /// Get the range of word by word index
00230     bool get_range_param_by_word_index(const int link_index,
00231                                        PDFAnchor & start_param,
00232                                        PDFAnchor & end_param);
00233 
00234     /// Get the text by range
00235     bool get_text_by_range(const PDFAnchor & start_param,
00236                            const PDFAnchor & end_param,
00237                            std::string &result);
00238 
00239     /// Get content area of a page.
00240     /// The content area should be caculated by the result of a thumbnail rendering
00241     bool get_content_area(PDFRenderer *renderer, RenderArea &area);
00242     const RenderArea & get_content_area() const { return content_area; }
00243 
00244     ///  Translate point from device coordination to user's
00245     void coordinates_dev_to_user(const double dx, const double dy, 
00246                                  double *ux, double *uy);
00247 
00248     void coordinates_user_to_dev(const double ux, const double uy, 
00249                                  int *dx, int *dy);
00250 
00251 private:
00252     // Data structures for searching
00253     typedef enum
00254     {
00255         STATUS_HEADER = 0, //for searching words list
00256         STATUS_BODY, //for searching words list
00257         STATUS_TAIL, //for searching words list
00258     }MatchStatus;
00259 
00260 private:
00261     //Initialize the page
00262     void init();
00263 
00264     // Set render status
00265     void set_render_status(RenderStatus s);
00266 
00267     // Update render results
00268     void update_text(TextPage *t);
00269     void update_links(Links *l);
00270     void update_bitmap(SplashBitmap *m);
00271 
00272     // Destroy render results
00273     void destroy_text();
00274     unsigned int destroy_bitmap();
00275     void destroy_links();
00276 
00277     // Search destination string by forward order
00278     // return the index of searching position
00279     PluginRangeImpl* search_string_forward(SearchContext &ctx,
00280                                            TextWordList *words);
00281 
00282     // Search destination string by backward order
00283     // return the index of searching position
00284     PluginRangeImpl* search_string_backward(SearchContext &ctx,
00285                                             TextWordList *words);
00286 
00287     // Compare the single string, it is used when searching single word
00288     bool compare_string(const string &dst          // pattern string
00289                         , const string &src        // source string
00290                         , bool case_sensitive      // is case sensitive
00291                         , bool match_whole_word    // is match whole word
00292                         , bool forward             // is forward
00293                         , bool sub_string          // compare only substring by the start char index
00294                         , const int start_char_idx // start position of the source word
00295                         , int &start_result_idx    // start position of the searched results
00296                         );  
00297 
00298     // Generate the search result
00299     void generate_search_result(SearchContext &ctx
00300                                 , SearchWords &queue
00301                                 , PluginRangeImpl* &result
00302                                 , bool forward);
00303 
00304     // Get content area from bitmap
00305     bool get_content_from_bitmap(SplashBitmap *bitmap, PDFRectangle &rect);
00306 
00307     // Rendering aborting function, dealing with the aborting request
00308     static GBool abort_render_check(void *data);
00309 
00310     // Try to calculate the size of a page
00311     static unsigned int try_calc_length(const double zoom_value
00312                                         , const double crop_width
00313                                         , const double crop_height);
00314 
00315 private:
00316     ///  page number
00317     int     page_number;
00318 
00319     ///  the render-related attributes
00320     PDFRenderAttributes render_attr;
00321 
00322     // render result
00323     SplashBitmap    *bitmap;
00324     Links           *links;
00325     TextPage        *text;
00326 
00327     // Reference of the document
00328     PDFController   *doc_controller;
00329 
00330     // Lock the page
00331     bool   b_lock;
00332 
00333     // Status of rendering process
00334     RenderStatus render_status;
00335 
00336     // Reference id of this page
00337     int ref_id;
00338 
00339     // Content area of a page
00340     RenderArea content_area;
00341 
00342     // CTM and ICTM of a PDF page. It is used for retrieving rectangle of hyperlink
00343     double ctm[6];
00344     double ictm[6];
00345 
00346     friend class PDFRenderTask;
00347     friend class PDFSearcher;
00348 };
00349 
00350 typedef PDFPage* PagePtr;
00351 
00352 };//namespace pdf
00353 
00354 #endif //PDF_PAGE_H_
00355 
00356 
Generated by  doxygen 1.6.2-20100208