pdf_renderer.h

Go to the documentation of this file.
00001 /*
00002  * File Name: pdf_renderer.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_RENDERER_H_
00028 #define PDF_RENDERER_H_
00029 
00030 #include "goo/GooList.h"
00031 #include "poppler/Object.h"
00032 #include "poppler/SplashOutputDev.h"
00033 #include "splash/SplashTypes.h"
00034 #include "render_result_impl.h"
00035 
00036 #include "mutex.h"
00037 #include "pdf_page.h"
00038 #include "pdf_collection.h"
00039 #include "pdf_prerender_policy.h"
00040 
00041 namespace pdf
00042 {
00043 
00044 class PDFViewAttributes
00045 {
00046 public:
00047     PDFViewAttributes()
00048         : display_width(200)
00049         , display_height(200)
00050         , color_depth(8)
00051         , device_dpi_v(72.0)
00052         , device_dpi_h(72.0)
00053     {}
00054 
00055     PDFViewAttributes(const PDFViewAttributes &attr)
00056         : display_width(attr.display_width)
00057         , display_height(attr.display_height)
00058         , color_depth(attr.color_depth)
00059         , device_dpi_v(attr.device_dpi_v)
00060         , device_dpi_h(attr.device_dpi_h)
00061     {}
00062 
00063     ~PDFViewAttributes() {}
00064 
00065     PDFViewAttributes& operator=(const PDFViewAttributes& right)
00066     {
00067         if (*this == right)
00068         {
00069             return *this;
00070         }
00071 
00072         display_width = right.display_width;
00073         display_height = right.display_height;
00074         color_depth  = right.color_depth;
00075         device_dpi_v = right.device_dpi_v;
00076         device_dpi_h = right.device_dpi_h;
00077 
00078         return *this;
00079     }
00080 
00081     bool operator==(const PDFViewAttributes &right)
00082     {
00083         return (this->display_width == right.display_width
00084             && this->display_height == right.display_height
00085             && this->color_depth == right.color_depth
00086             && this->device_dpi_v == right.device_dpi_v
00087             && this->device_dpi_h == right.device_dpi_h);
00088     }
00089 
00090     void set_device_dpi_h(double h) {device_dpi_h = h;}
00091     double get_device_dpi_h() const {return device_dpi_h;}
00092     void set_device_dpi_v(double v) {device_dpi_v = v;}
00093     double get_device_dpi_v() const {return device_dpi_v;}
00094 
00095     void set_display_width(const int w) {display_width = w;}
00096     int get_display_width() const {return display_width;}
00097     void set_display_height(const int h) {display_height = h;}
00098     int get_display_height() const {return display_height;}
00099 
00100     void set_color_depth(const unsigned int c) {color_depth = c;}
00101     unsigned int get_color_depth() const {return color_depth;}
00102 
00103 private:
00104     // the display size
00105     int    display_width;
00106     int    display_height;
00107 
00108     // the color depth
00109     unsigned int color_depth;
00110 
00111     // the device vertical dpi
00112     double device_dpi_v;
00113 
00114     // the device horizontal dpi
00115     double device_dpi_h;
00116 };
00117 
00118 // Define the render status for a task
00119 typedef enum RenderStatus_
00120 {
00121     TASK_RENDER_DONE = 0,
00122     TASK_RENDER_OOM,
00123     TASK_RENDER_INVALID_PAGE
00124 } RenderStatus;
00125 
00126 class PDFController;
00127 class PDFRenderTask;
00128 /// @brief The document renderer class.
00129 class PDFRenderer
00130 {
00131 public:
00132     PDFRenderer();
00133     virtual ~PDFRenderer(void);
00134 
00135     /// Initialize the pdf renderer
00136     bool initialize(PDFController* doc);
00137 
00138     /// Destroy the renderer
00139     void destroy();
00140 
00141     /// Post the render task into the task list of thread
00142     void post_render_task(int page_num, 
00143                           const PDFRenderAttributes &render_attr,
00144                           PluginRenderResultImpl *render_res,
00145                           const unsigned int ref_id = PRERENDER_REF_ID);
00146 
00147     /// Handle the page ready event
00148     /// if param page is not null, it means the page has been successfully
00149     /// rendered. If reference number of the page is invalid(-1), it means
00150     /// this page is rendered by prerendering. We don't have to notify UDS.
00151     /// if param page is null, it means the page has not been successfully
00152     /// rendered (caused by out of memory).
00153     void handle_page_ready(RenderResultPtr render_res,
00154                            PagePtr page,
00155                            RenderStatus stat);
00156 
00157     /// Get the view attributes
00158     PDFViewAttributes& get_view_attr() { return view_attr; }
00159 
00160     /// Get the current render attributes
00161     const PDFRenderAttributes& get_render_attr() { return cur_render_attr; }
00162 
00163     /// Generate a page by input context
00164     PagePtr gen_page(int page_num, const PDFRenderAttributes &attr);
00165 
00166     /// Generate a page by default context
00167     PagePtr gen_page(int page_num);
00168 
00169     /// Get the mutex of rendering
00170     Mutex & get_render_mutex() { return render_mutex; }
00171 
00172     /// Render the cover page for UDS
00173     bool render_cover_page(const int width,
00174                            const int height,
00175                            PluginBitmapAttributes *output);
00176 public:
00177     static SplashColor background_color;
00178 
00179 private:
00180     // Generate render task without existing page
00181     PDFRenderTask* gen_render_task(int page_num,
00182                                    const PDFRenderAttributes &page_attr,
00183                                    PluginRenderResultImpl *render_res = 0,
00184                                    int ref_id = PRERENDER_REF_ID);
00185 
00186     // Generate render task with existing page
00187     PDFRenderTask* gen_render_task(PagePtr page,
00188                                    const PDFRenderAttributes &page_attr,
00189                                    PluginRenderResultImpl *render_res = 0,
00190                                    int ref_id = PRERENDER_REF_ID);
00191 
00192     // Initialize all of the PDFPage instances
00193     // The index table of all pages is constructed
00194     void init_pages_index_table();
00195 
00196     // Record the initial time
00197     void init_start_time();
00198 
00199     void calc_real_zoom(int page_number,
00200                         const PDFRenderAttributes &origin_attr,
00201                         PDFRenderAttributes &real_attr);
00202 
00203     // Get the output devices
00204     SplashOutputDev* get_splash_output_dev() const { return splash_output_dev; }
00205     SplashOutputDev* get_thumbnail_output_dev() const { return thumbnail_output_dev; }
00206     TextOutputDev* get_text_output_dev() const { return text_output_dev; }
00207 
00208     // Post prerender task
00209     void post_prerender_task(const size_t page_number,
00210                              const PDFRenderAttributes &page_attr);
00211 
00212     // Post prerender hyperlinks task
00213     void post_prerender_hyperlinks_task(PagePtr page);
00214 
00215 private:
00216     // Reference to the PDFController instance
00217     PDFController   *doc_controller;
00218 
00219     // The output device of image
00220     SplashOutputDev *splash_output_dev;
00221 
00222     // The output device of text
00223     TextOutputDev   *text_output_dev;
00224 
00225     // The output device of thumbnail image
00226     SplashOutputDev *thumbnail_output_dev;
00227 
00228     // the view attributes
00229     PDFViewAttributes view_attr;
00230 
00231     // default render settings
00232     PDFRenderAttributes cur_render_attr;
00233 
00234     // the mutex used in rendering
00235     Mutex render_mutex;
00236 
00237     friend class PDFRenderTask;
00238     friend class PDFPage;
00239 };
00240 
00241 };
00242 
00243 #endif //PDF_RENDERER_H_
00244 
Generated by  doxygen 1.6.2-20100208