pdf_prerender_policy.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: pdf_prerender_policy.cpp
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 #include "pdf_prerender_policy.h"
00028 #include "pdf_page.h"
00029 #include "pdf_doc_controller.h"
00030 
00031 namespace pdf
00032 {
00033 
00034 static const int ALLOWED_HYPERLINKS_NUMBER = 3;
00035 
00036 // Compare the priority between two pages
00037 int compare_priority(const int src_page,
00038                      const int dst_page,
00039                      PDFPrerenderPolicy *policy)
00040 {
00041     int src_pri = policy->get_requests().get_priority(src_page);
00042     int dst_pri = policy->get_requests().get_priority(dst_page);
00043     // the value of priority is lower, the priority is higher
00044     if (src_pri > dst_pri)
00045     {
00046         return -1;
00047     }
00048     else if (src_pri < dst_pri)
00049     {
00050         return 1;
00051     }
00052     return 0;
00053 }
00054 
00055 bool operator < (const PDFPage & left, const PDFPage & right)
00056 {
00057     // check bitmap
00058     // Has Bitmap < Doesn't have Bitmap
00059     if (left.get_bitmap() != 0 && right.get_bitmap() == 0)
00060     {
00061         return true;
00062     }
00063     else if (left.get_bitmap() == 0 && right.get_bitmap() != 0)
00064     {
00065         return false;
00066     }
00067 
00068     // check lock
00069     // Unlocked one < Locked one
00070     if (!left.locked() && right.locked())
00071     {
00072         return true;
00073     }
00074     else if (left.locked() && !right.locked())
00075     {
00076         return false;
00077     }
00078 
00079     // check GLOBAL requests list
00080     // compare the priority of two pages
00081     int ret_priority = compare_priority(left.get_page_num(),
00082                                         right.get_page_num(),
00083                                         left.get_doc_controller()->get_prerender_policy());
00084     if (ret_priority < 0)
00085     {
00086         return true;
00087     }
00088     return false;
00089 }
00090 
00091 bool operator >= (const PDFPage & left, const PDFPage & right)
00092 {
00093     return !(left < right);
00094 }
00095 
00096 void add_request_page(const int current_page,
00097                       const int offset,
00098                       const int total,
00099                       bool forward,
00100                       std::vector<size_t> & result)
00101 {
00102     int next_page = forward ? (current_page + offset) : (current_page - offset);
00103     if ((next_page > 0 && next_page <= total))
00104     {
00105         result.push_back(next_page);
00106     }
00107 }
00108 
00109 PDFPrerenderPolicy::PDFPrerenderPolicy()
00110 : is_prerender(true)
00111 {
00112 }
00113 
00114 PDFPrerenderPolicy::~PDFPrerenderPolicy()
00115 {
00116 }
00117 
00118 PDFPrerenderPolicyNormal::PDFPrerenderPolicyNormal()
00119 : PDFPrerenderPolicy()
00120 {
00121 }
00122 
00123 PDFPrerenderPolicyNormal::~PDFPrerenderPolicyNormal()
00124 {
00125 }
00126 
00127 int PDFPrerenderPolicyNormal::get_allowed_hyperlinks_number()
00128 {
00129     return ALLOWED_HYPERLINKS_NUMBER;
00130 }
00131 
00132 void PDFPrerenderPolicyNormal::generate_requests_list(const int current_page,
00133                                                       const int previous_page,
00134                                                       const int total,
00135                                                       std::vector<size_t> & result)
00136 {
00137     // clear the requests and put the current page at the front of the vector
00138     result.clear();
00139     result.push_back(current_page);
00140 
00141     // get the step of previous page and current page
00142     int step = current_page - previous_page;
00143 
00144     // the current page should always be added
00145     switch (step)
00146     {
00147     case -1:
00148     case 1:
00149         {
00150             // prerender the next pages
00151             next_pages_first(current_page, total, step > 0, result);
00152         }
00153         break;
00154     case -5:
00155     case 5:
00156         {
00157             // prerender the next 5 pages
00158             faraway_pages_first(current_page, total, step > 0, result);
00159         }
00160     case 0:
00161         {
00162             // prerender the nearby pages
00163             nearby_pages_first(current_page, total, result);
00164         }
00165         break;
00166     default:
00167         {
00168             // prerender the nearby pages
00169             nearby_pages_first(current_page, total, result);
00170         }
00171         break;
00172     }
00173 
00174     // update the requests queue
00175     requests.update(result);
00176 }
00177 
00178 void PDFPrerenderPolicyNormal::next_pages_first(const int current_page,
00179                                                 const int total,
00180                                                 bool forward,
00181                                                 std::vector<size_t> & result)
00182 {
00183     // current + 1
00184     add_request_page(current_page, 1, total, forward, result);
00185 
00186     // current - 1
00187     add_request_page(current_page, -1, total, forward, result);
00188 
00189     // current + 2
00190     add_request_page(current_page, 2, total, forward, result);
00191 
00192     // current + 3
00193     add_request_page(current_page, 3, total, forward, result);
00194 
00195     // current + 5
00196     add_request_page(current_page, 5, total, forward, result);
00197 }
00198 
00199 void PDFPrerenderPolicyNormal::faraway_pages_first(const int current_page,
00200                                                    const int total,
00201                                                    bool forward,
00202                                                    std::vector<size_t> & result)
00203 {
00204     // current + 5
00205     add_request_page(current_page, 5, total, forward, result);
00206 
00207     // current + 1
00208     add_request_page(current_page, 1, total, forward, result);
00209 
00210     // current - 1
00211     add_request_page(current_page, -1, total, forward, result);
00212 
00213     // current + 2
00214     add_request_page(current_page, 2, total, forward, result);
00215 
00216     // current + 3
00217     add_request_page(current_page, 3, total, forward, result);
00218 }
00219 
00220 void PDFPrerenderPolicyNormal::nearby_pages_first(const int current_page,
00221                                                   const int total,
00222                                                   std::vector<size_t> & result)
00223 {
00224     // current + 1
00225     add_request_page(current_page, 1, total, true, result);
00226 
00227     // current - 1
00228     add_request_page(current_page, -1, total, true, result);
00229 }
00230 
00231 }
00232 
Generated by  doxygen 1.6.2-20100208