00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
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
00058
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
00069
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
00080
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
00138 result.clear();
00139 result.push_back(current_page);
00140
00141
00142 int step = current_page - previous_page;
00143
00144
00145 switch (step)
00146 {
00147 case -1:
00148 case 1:
00149 {
00150
00151 next_pages_first(current_page, total, step > 0, result);
00152 }
00153 break;
00154 case -5:
00155 case 5:
00156 {
00157
00158 faraway_pages_first(current_page, total, step > 0, result);
00159 }
00160 case 0:
00161 {
00162
00163 nearby_pages_first(current_page, total, result);
00164 }
00165 break;
00166 default:
00167 {
00168
00169 nearby_pages_first(current_page, total, result);
00170 }
00171 break;
00172 }
00173
00174
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
00184 add_request_page(current_page, 1, total, forward, result);
00185
00186
00187 add_request_page(current_page, -1, total, forward, result);
00188
00189
00190 add_request_page(current_page, 2, total, forward, result);
00191
00192
00193 add_request_page(current_page, 3, total, forward, result);
00194
00195
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
00205 add_request_page(current_page, 5, total, forward, result);
00206
00207
00208 add_request_page(current_page, 1, total, forward, result);
00209
00210
00211 add_request_page(current_page, -1, total, forward, result);
00212
00213
00214 add_request_page(current_page, 2, total, forward, result);
00215
00216
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
00225 add_request_page(current_page, 1, total, true, result);
00226
00227
00228 add_request_page(current_page, -1, total, true, result);
00229 }
00230
00231 }
00232