The document renderer class. More...
#include <pdf_renderer.h>
Public Member Functions | |
PDFRenderer () | |
virtual | ~PDFRenderer (void) |
bool | initialize (PDFController *doc) |
Initialize the pdf renderer. | |
void | destroy () |
Destroy the renderer. | |
void | post_render_task (int page_num, const PDFRenderAttributes &render_attr, PluginRenderResultImpl *render_res, const unsigned int ref_id=PRERENDER_REF_ID) |
Post the render task into the task list of thread. | |
void | handle_page_ready (RenderResultPtr render_res, PagePtr page, RenderStatus stat) |
PDFViewAttributes & | get_view_attr () |
Get the view attributes. | |
const PDFRenderAttributes & | get_render_attr () |
Get the current render attributes. | |
PagePtr | gen_page (int page_num, const PDFRenderAttributes &attr) |
Generate a page by input context. | |
PagePtr | gen_page (int page_num) |
Generate a page by default context. | |
Mutex & | get_render_mutex () |
Get the mutex of rendering. | |
bool | render_cover_page (const int width, const int height, PluginBitmapAttributes *output) |
Render the cover page for UDS. | |
Static Public Attributes | |
static SplashColor | background_color = {255, 255, 255, 0} |
Friends | |
class | PDFRenderTask |
class | PDFPage |
The document renderer class.
Definition at line 129 of file pdf_renderer.h.
pdf::PDFRenderer::PDFRenderer | ( | ) |
Definition at line 39 of file pdf_renderer.cpp.
pdf::PDFRenderer::~PDFRenderer | ( | void | ) | [virtual] |
Definition at line 50 of file pdf_renderer.cpp.
References destroy().
00051 { 00052 destroy(); 00053 }
void pdf::PDFRenderer::destroy | ( | ) |
Destroy the renderer.
Definition at line 105 of file pdf_renderer.cpp.
Referenced by pdf::PDFController::close(), initialize(), and ~PDFRenderer().
00106 { 00107 delete splash_output_dev; 00108 splash_output_dev = 0; 00109 00110 delete text_output_dev; 00111 text_output_dev = 0; 00112 00113 delete thumbnail_output_dev; 00114 thumbnail_output_dev = 0; 00115 }
PagePtr pdf::PDFRenderer::gen_page | ( | int | page_num | ) |
Generate a page by default context.
Definition at line 543 of file pdf_renderer.cpp.
References pdf::PagesCache::add_page(), PDFPage, and pdf::PDFPage::set_doc_controller().
00544 { 00545 // construct a default page 00546 PagePtr page = new PDFPage(page_num, cur_render_attr); 00547 page->set_doc_controller(doc_controller); 00548 00549 // put the page into cache 00550 doc_controller->pages_cache.add_page(page); 00551 00552 return page; 00553 }
PagePtr pdf::PDFRenderer::gen_page | ( | int | page_num, | |
const PDFRenderAttributes & | attr | |||
) |
Generate a page by input context.
Definition at line 532 of file pdf_renderer.cpp.
References pdf::PagesCache::add_page(), PDFPage, and pdf::PDFPage::set_doc_controller().
Referenced by pdf::PDFRenderTask::execute().
00533 { 00534 PagePtr page = new PDFPage(page_num, attr); 00535 page->set_doc_controller(doc_controller); 00536 00537 // put the page into cache 00538 doc_controller->pages_cache.add_page(page); 00539 00540 return page; 00541 }
const PDFRenderAttributes& pdf::PDFRenderer::get_render_attr | ( | ) | [inline] |
Get the current render attributes.
Definition at line 161 of file pdf_renderer.h.
Mutex& pdf::PDFRenderer::get_render_mutex | ( | ) | [inline] |
Get the mutex of rendering.
Definition at line 170 of file pdf_renderer.h.
Referenced by pdf::PDFPage::get_content_area(), pdf::PDFPage::render_splash_map(), and pdf::PDFPage::render_text().
PDFViewAttributes& pdf::PDFRenderer::get_view_attr | ( | ) | [inline] |
Get the view attributes.
Definition at line 158 of file pdf_renderer.h.
Referenced by pdf::PDFPage::get_content_area(), pdf::PDFController::get_page_crop_height(), pdf::PDFController::get_page_crop_width(), render_cover_page(), pdf::PDFPage::render_splash_map(), and pdf::PDFPage::render_text().
void pdf::PDFRenderer::handle_page_ready | ( | RenderResultPtr | render_res, | |
PagePtr | page, | |||
RenderStatus | stat | |||
) |
Handle the page ready event if param page is not null, it means the page has been successfully rendered. If reference number of the page is invalid(-1), it means this page is rendered by prerendering. We don't have to notify UDS. if param page is null, it means the page has not been successfully rendered (caused by out of memory).
Referenced by pdf::PDFRenderTask::execute(), and post_render_task().
bool pdf::PDFRenderer::initialize | ( | PDFController * | doc | ) |
Initialize the pdf renderer.
Definition at line 55 of file pdf_renderer.cpp.
References background_color, destroy(), ERRORPRINTF, and pdf::PDFController::get_pdf_doc().
Referenced by pdf::PDFController::open().
00056 { 00057 //Destroy the pre-built renderer. 00058 destroy(); 00059 00060 doc_controller = doc; 00061 //assert(doc_controller); 00062 if (doc_controller == 0) 00063 { 00064 ERRORPRINTF("Null Document Controller"); 00065 return false; 00066 } 00067 00068 SplashColorMode mode = splashModeMono8; 00069 00070 // Create splash output device 00071 splash_output_dev = new SplashOutputDev(mode, 4, gFalse, PDFRenderer::background_color); 00072 if (splash_output_dev == 0) 00073 { 00074 ERRORPRINTF("Cannot Open Splash Output Device"); 00075 return false; 00076 } 00077 00078 // Start output device with the document 00079 splash_output_dev->startDoc(doc_controller->get_pdf_doc()->getXRef()); 00080 00081 // Create text output device 00082 text_output_dev = new TextOutputDev(NULL, gTrue, gFalse, gFalse); 00083 if (text_output_dev == 0) 00084 { 00085 ERRORPRINTF("Cannot Open Text Output Device"); 00086 return false; 00087 } 00088 00089 // Create thumbnail output device 00090 thumbnail_output_dev = new SplashOutputDev(mode, 4, gFalse, PDFRenderer::background_color); 00091 if (thumbnail_output_dev == 0) 00092 { 00093 ERRORPRINTF("Cannot Open Thumbnail Output Device"); 00094 return false; 00095 } 00096 00097 thumbnail_output_dev->startDoc(doc_controller->get_pdf_doc()->getXRef()); 00098 00099 init_pages_index_table(); 00100 00101 return true; 00102 00103 }
void pdf::PDFRenderer::post_render_task | ( | int | page_num, | |
const PDFRenderAttributes & | render_attr, | |||
PluginRenderResultImpl * | render_res, | |||
const unsigned int | ref_id = PRERENDER_REF_ID | |||
) |
Post the render task into the task list of thread.
Definition at line 358 of file pdf_renderer.cpp.
References pdf::PDFPrerenderPolicy::generate_requests_list(), pdf::PDFController::get_cur_page_num(), pdf::PagesCache::get_mutex(), pdf::PDFController::get_page(), pdf::PDFController::get_prerender_policy(), pdf::PDFPage::get_render_attr(), pdf::PDFPage::get_render_status(), pdf::PDFRenderTask::get_user_data(), pdf::PDFRenderAttributes::get_zoom_setting(), handle_page_ready(), pdf::PDFLibrary::instance(), LOGPRINTF, pdf::PDFController::page_count(), pdf::PDFPage::RENDER_DONE, pdf::PDFPage::RENDER_RUNNING, pdf::PluginRenderResultImpl::set_page(), pdf::PDFPage::set_ref_id(), pdf::TASK_RENDER_DONE, pdf::TASK_RENDER_INVALID_PAGE, pdf::PDFLibrary::thread_add_render_task(), and pdf::PDFLibrary::thread_cancel_render_tasks().
00362 { 00363 if (page_num <= 0 || page_num > static_cast<int>(doc_controller->page_count())) 00364 { 00365 handle_page_ready(render_res, 0, TASK_RENDER_INVALID_PAGE); 00366 return; 00367 } 00368 00369 // set the current displaying page 00370 int last_page = doc_controller->get_cur_page_num(); 00371 doc_controller->set_cur_page_num(page_num); 00372 00373 PDFRenderAttributes real_attr; 00374 calc_real_zoom(page_num, render_attr, real_attr); 00375 // update global render setting 00376 cur_render_attr = real_attr; 00377 00378 // generate request queue of rendering based on current page and last page 00379 // at this moment, the previous request queue would be cleared. 00380 std::vector<size_t> requests; 00381 doc_controller->get_prerender_policy()->generate_requests_list(page_num, 00382 last_page, 00383 doc_controller->page_count(), 00384 requests); 00385 00386 // estimate whether the requested page is cached 00387 PagePtr page = doc_controller->get_page(page_num); 00388 PDFRenderTask* render_task = 0; 00389 bool abort_current_task = true; 00390 if (page) 00391 { 00392 { 00393 ScopeMutex m(&(doc_controller->get_pages_cache().get_mutex())); 00394 if (page->get_render_attr() == cur_render_attr) 00395 { 00396 if (page->get_render_status() == PDFPage::RENDER_DONE && 00397 render_res != 0) 00398 { 00399 // update the reference to this page, make it ready to display 00400 // need NOT generate a new render task 00401 page->set_ref_id(ref_id); 00402 render_res->set_page(page); 00403 } 00404 else 00405 { 00406 // create a new render task 00407 render_task = gen_render_task(page, 00408 real_attr, 00409 render_res, 00410 ref_id); 00411 if (page->get_render_status() == PDFPage::RENDER_RUNNING) 00412 { 00413 abort_current_task = false; 00414 } 00415 } 00416 } 00417 else 00418 { 00419 render_task = gen_render_task(page, 00420 real_attr, 00421 render_res, 00422 ref_id); 00423 } 00424 } 00425 } 00426 else 00427 { 00428 // the page is not cached, create a new Render task 00429 render_task = gen_render_task(page_num, 00430 real_attr, 00431 render_res, 00432 ref_id); 00433 } 00434 00435 if (render_task != 0) 00436 { 00437 LOGPRINTF("PDF tries to render page:%d zoom:%f\n\n", 00438 page_num, 00439 real_attr.get_zoom_setting()); 00440 00441 // cancel all of the render tasks in the queue 00442 PDFLibrary::instance().thread_cancel_render_tasks(render_task->get_user_data()); 00443 00444 // add task for normally rendering 00445 PDFLibrary::instance().thread_add_render_task(render_task, 00446 false, 00447 abort_current_task); 00448 } 00449 else if (page != 0 && render_res != 0 && 00450 page->get_render_status() == PDFPage::RENDER_DONE) 00451 { 00452 // if the page is ready, return it to UDS 00453 handle_page_ready(render_res, page, TASK_RENDER_DONE); 00454 } 00455 00456 // prerender the pages, start from 1 in the requests list because 00457 // the normal rendering page is always located at 0. 00458 for (size_t idx = 1; idx < requests.size(); ++idx) 00459 { 00460 int dst_page = requests.at(idx); 00461 post_prerender_task(dst_page, render_attr); 00462 } 00463 }
bool pdf::PDFRenderer::render_cover_page | ( | const int | width, | |
const int | height, | |||
PluginBitmapAttributes * | output | |||
) |
Render the cover page for UDS.
Definition at line 214 of file pdf_renderer.cpp.
References PluginBitmapAttributes::data, ERRORPRINTF, pdf::PDFController::get_page_crop_height(), pdf::PDFController::get_page_crop_width(), pdf::PDFController::get_pdf_doc(), get_view_attr(), and pdf::PDFController::page_count().
00216 { 00217 if (doc_controller->page_count() < 1) 00218 { 00219 // return false if it is an empty document 00220 return false; 00221 } 00222 00223 int cover_num = 1; 00224 // 1. calculate the zoom, fit for best 00225 double crop_width = doc_controller->get_page_crop_width(cover_num); 00226 double crop_height = doc_controller->get_page_crop_height(cover_num); 00227 00228 double zoom = min(static_cast<double>(width) / crop_width, 00229 static_cast<double>(height) / crop_height); 00230 00231 // 2. render the splash bitmap of cover 00232 // lock when rendering 00233 ScopeMutex m(&render_mutex); 00234 00235 RenderRet ret = doc_controller->get_pdf_doc()->displayPage( 00236 get_thumbnail_output_dev() 00237 , cover_num 00238 , zoom * get_view_attr().get_device_dpi_h() 00239 , zoom * get_view_attr().get_device_dpi_v() 00240 , 0 00241 , gFalse //useMediaBox, TODO. 00242 , gFalse //crop, TODO. 00243 , gFalse //doLinks, TODO. 00244 ); 00245 00246 if (ret == Render_Error || ret == Render_Invalid) 00247 { 00248 ERRORPRINTF("Error in rendering cover page:%d\n", cover_num); 00249 return false; 00250 } 00251 00252 SplashBitmap *cover_map = get_thumbnail_output_dev()->takeBitmap(); 00253 if (cover_map != 0) 00254 { 00255 memcpy((void*)output->data, cover_map->getDataPtr(), 00256 cover_map->getRowSize() * cover_map->getHeight()); 00257 delete cover_map; 00258 return true; 00259 } 00260 00261 return false; 00262 }
friend class PDFPage [friend] |
Definition at line 238 of file pdf_renderer.h.
Referenced by gen_page().
friend class PDFRenderTask [friend] |
Definition at line 237 of file pdf_renderer.h.
SplashColor pdf::PDFRenderer::background_color = {255, 255, 255, 0} [static] |
Definition at line 177 of file pdf_renderer.h.
Referenced by initialize().