Printing: Load the source PDF only once.
Instead of loading the PDF once per operation, which is slightly more than once per page. Review-Url: https://codereview.chromium.org/2508563003 Cr-Commit-Position: refs/heads/master@{#432594}
This commit is contained in:
53
pdf/pdf.cc
53
pdf/pdf.cc
@ -82,8 +82,7 @@ const void* PPP_GetInterface(const char* interface_name) {
|
||||
}
|
||||
|
||||
#if defined(OS_WIN)
|
||||
bool RenderPDFPageToDC(const void* pdf_buffer,
|
||||
int buffer_size,
|
||||
bool RenderPDFPageToDC(void* pdf_handle,
|
||||
int page_number,
|
||||
HDC dc,
|
||||
int dpi,
|
||||
@ -107,8 +106,8 @@ bool RenderPDFPageToDC(const void* pdf_buffer,
|
||||
pp::Rect(bounds_origin_x, bounds_origin_y, bounds_width, bounds_height),
|
||||
fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
|
||||
autorotate);
|
||||
bool ret = engine_exports->RenderPDFPageToDC(pdf_buffer, buffer_size,
|
||||
page_number, settings, dc);
|
||||
bool ret =
|
||||
engine_exports->RenderPDFPageToDC(pdf_handle, page_number, settings, dc);
|
||||
if (!g_sdk_initialized_via_pepper)
|
||||
ShutdownSDK();
|
||||
|
||||
@ -126,39 +125,51 @@ void SetPDFUseGDIPrinting(bool enable) {
|
||||
#endif // defined(OS_WIN)
|
||||
|
||||
bool GetPDFDocInfo(const void* pdf_buffer,
|
||||
int buffer_size, int* page_count,
|
||||
double* max_page_width) {
|
||||
int buffer_size,
|
||||
int* page_count,
|
||||
double* max_page_width,
|
||||
void** pdf_handle) {
|
||||
if (!g_sdk_initialized_via_pepper) {
|
||||
if (!InitializeSDK())
|
||||
return false;
|
||||
}
|
||||
PDFEngineExports* engine_exports = PDFEngineExports::Get();
|
||||
bool ret = engine_exports->GetPDFDocInfo(
|
||||
pdf_buffer, buffer_size, page_count, max_page_width);
|
||||
bool ret = engine_exports->GetPDFDocInfo(pdf_buffer, buffer_size, page_count,
|
||||
max_page_width, pdf_handle);
|
||||
if (!g_sdk_initialized_via_pepper)
|
||||
ShutdownSDK();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool GetPDFPageSizeByIndex(const void* pdf_buffer,
|
||||
int pdf_buffer_size, int page_number,
|
||||
double* width, double* height) {
|
||||
void ReleasePDFHandle(void* pdf_handle) {
|
||||
if (!g_sdk_initialized_via_pepper) {
|
||||
if (!chrome_pdf::InitializeSDK())
|
||||
if (!InitializeSDK())
|
||||
return;
|
||||
}
|
||||
PDFEngineExports* engine_exports = PDFEngineExports::Get();
|
||||
engine_exports->ReleasePDFHandle(pdf_handle);
|
||||
if (!g_sdk_initialized_via_pepper)
|
||||
ShutdownSDK();
|
||||
}
|
||||
|
||||
bool GetPDFPageSizeByIndex(void* pdf_handle,
|
||||
int page_number,
|
||||
double* width,
|
||||
double* height) {
|
||||
if (!g_sdk_initialized_via_pepper) {
|
||||
if (!InitializeSDK())
|
||||
return false;
|
||||
}
|
||||
chrome_pdf::PDFEngineExports* engine_exports =
|
||||
chrome_pdf::PDFEngineExports::Get();
|
||||
bool ret = engine_exports->GetPDFPageSizeByIndex(
|
||||
pdf_buffer, pdf_buffer_size, page_number, width, height);
|
||||
PDFEngineExports* engine_exports = PDFEngineExports::Get();
|
||||
bool ret = engine_exports->GetPDFPageSizeByIndex(pdf_handle, page_number,
|
||||
width, height);
|
||||
if (!g_sdk_initialized_via_pepper)
|
||||
chrome_pdf::ShutdownSDK();
|
||||
ShutdownSDK();
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool RenderPDFPageToBitmap(const void* pdf_buffer,
|
||||
int pdf_buffer_size,
|
||||
bool RenderPDFPageToBitmap(void* pdf_handle,
|
||||
int page_number,
|
||||
void* bitmap_buffer,
|
||||
int bitmap_width,
|
||||
@ -173,8 +184,8 @@ bool RenderPDFPageToBitmap(const void* pdf_buffer,
|
||||
PDFEngineExports::RenderingSettings settings(
|
||||
dpi, dpi, pp::Rect(bitmap_width, bitmap_height), true, false, true, true,
|
||||
autorotate);
|
||||
bool ret = engine_exports->RenderPDFPageToBitmap(
|
||||
pdf_buffer, pdf_buffer_size, page_number, settings, bitmap_buffer);
|
||||
bool ret = engine_exports->RenderPDFPageToBitmap(pdf_handle, page_number,
|
||||
settings, bitmap_buffer);
|
||||
if (!g_sdk_initialized_via_pepper)
|
||||
ShutdownSDK();
|
||||
|
||||
|
40
pdf/pdf.h
40
pdf/pdf.h
@ -36,9 +36,7 @@ void PPP_ShutdownModule();
|
||||
const void* PPP_GetInterface(const char* interface_name);
|
||||
|
||||
#if defined(OS_WIN)
|
||||
// |pdf_buffer| is the buffer that contains the entire PDF document to be
|
||||
// rendered.
|
||||
// |buffer_size| is the size of |pdf_buffer| in bytes.
|
||||
// |pdf_handle| is the handle to the PDF document.
|
||||
// |page_number| is the 0-based index of the page to be rendered.
|
||||
// |dc| is the device context to render into.
|
||||
// |dpi| and |dpi_y| is the resolution. If the value is -1, the dpi from the DC
|
||||
@ -63,8 +61,7 @@ const void* PPP_GetInterface(const char* interface_name);
|
||||
// |autorotate| specifies whether the final image should be rotated to match
|
||||
// the output bound.
|
||||
// Returns false if the document or the page number are not valid.
|
||||
bool RenderPDFPageToDC(const void* pdf_buffer,
|
||||
int buffer_size,
|
||||
bool RenderPDFPageToDC(void* pdf_handle,
|
||||
int page_number,
|
||||
HDC dc,
|
||||
int dpi,
|
||||
@ -84,29 +81,35 @@ void SetPDFEnsureTypefaceCharactersAccessible(
|
||||
void SetPDFUseGDIPrinting(bool enable);
|
||||
#endif // defined(OS_WIN)
|
||||
|
||||
// |page_count| and |max_page_width| are optional and can be NULL.
|
||||
// All the out parameters are optional and can be NULL.
|
||||
// Returns false if the document is not valid.
|
||||
// Returns true on success. In which case, if |pdf_handle| is not NULL, then
|
||||
// the handle is guaranteed to be valid and not NULL. The caller takes
|
||||
// ownership of |pdf_handle| and must call ReleasePDFHandle() on it when done.
|
||||
bool GetPDFDocInfo(const void* pdf_buffer,
|
||||
int buffer_size, int* page_count,
|
||||
double* max_page_width);
|
||||
int buffer_size,
|
||||
int* page_count,
|
||||
double* max_page_width,
|
||||
void** pdf_handle);
|
||||
|
||||
// Releases the handle received from GetPDFDocInfo().
|
||||
// |pdf_handle| can be NULL.
|
||||
void ReleasePDFHandle(void* pdf_handle);
|
||||
|
||||
// Gets the dimensions of a specific page in a document.
|
||||
// |pdf_buffer| is the buffer that contains the entire PDF document to be
|
||||
// rendered.
|
||||
// |pdf_buffer_size| is the size of |pdf_buffer| in bytes.
|
||||
// |pdf_handle| is the handle to the PDF document.
|
||||
// |page_number| is the page number that the function will get the dimensions
|
||||
// of.
|
||||
// |width| is the output for the width of the page in points.
|
||||
// |height| is the output for the height of the page in points.
|
||||
// Returns false if the document or the page number are not valid.
|
||||
bool GetPDFPageSizeByIndex(const void* pdf_buffer,
|
||||
int pdf_buffer_size, int page_number,
|
||||
double* width, double* height);
|
||||
bool GetPDFPageSizeByIndex(void* pdf_handle,
|
||||
int page_number,
|
||||
double* width,
|
||||
double* height);
|
||||
|
||||
// Renders PDF page into 4-byte per pixel BGRA color bitmap.
|
||||
// |pdf_buffer| is the buffer that contains the entire PDF document to be
|
||||
// rendered.
|
||||
// |pdf_buffer_size| is the size of |pdf_buffer| in bytes.
|
||||
// |pdf_handle| is the handle to the PDF document.
|
||||
// |page_number| is the 0-based index of the page to be rendered.
|
||||
// |bitmap_buffer| is the output buffer for bitmap.
|
||||
// |bitmap_width| is the width of the output bitmap.
|
||||
@ -115,8 +118,7 @@ bool GetPDFPageSizeByIndex(const void* pdf_buffer,
|
||||
// |autorotate| specifies whether the final image should be rotated to match
|
||||
// the output bound.
|
||||
// Returns false if the document or the page number are not valid.
|
||||
bool RenderPDFPageToBitmap(const void* pdf_buffer,
|
||||
int pdf_buffer_size,
|
||||
bool RenderPDFPageToBitmap(void* pdf_handle,
|
||||
int page_number,
|
||||
void* bitmap_buffer,
|
||||
int bitmap_width,
|
||||
|
@ -333,9 +333,8 @@ class PDFEngineExports {
|
||||
static PDFEngineExports* Get();
|
||||
|
||||
#if defined(OS_WIN)
|
||||
// See the definition of RenderPDFPageToDC in pdf.cc for details.
|
||||
virtual bool RenderPDFPageToDC(const void* pdf_buffer,
|
||||
int buffer_size,
|
||||
// See the definitions of the corresponding functions in pdf.h for details.
|
||||
virtual bool RenderPDFPageToDC(void* pdf_handle,
|
||||
int page_number,
|
||||
const RenderingSettings& settings,
|
||||
HDC dc) = 0;
|
||||
@ -346,9 +345,7 @@ class PDFEngineExports {
|
||||
virtual void SetPDFUseGDIPrinting(bool enable) = 0;
|
||||
#endif // defined(OS_WIN)
|
||||
|
||||
// See the definition of RenderPDFPageToBitmap in pdf.cc for details.
|
||||
virtual bool RenderPDFPageToBitmap(const void* pdf_buffer,
|
||||
int pdf_buffer_size,
|
||||
virtual bool RenderPDFPageToBitmap(void* pdf_handle,
|
||||
int page_number,
|
||||
const RenderingSettings& settings,
|
||||
void* bitmap_buffer) = 0;
|
||||
@ -356,12 +353,15 @@ class PDFEngineExports {
|
||||
virtual bool GetPDFDocInfo(const void* pdf_buffer,
|
||||
int buffer_size,
|
||||
int* page_count,
|
||||
double* max_page_width) = 0;
|
||||
double* max_page_width,
|
||||
void** pdf_handle) = 0;
|
||||
|
||||
// See the definition of GetPDFPageSizeByIndex in pdf.cc for details.
|
||||
virtual bool GetPDFPageSizeByIndex(const void* pdf_buffer,
|
||||
int pdf_buffer_size, int page_number,
|
||||
double* width, double* height) = 0;
|
||||
virtual void ReleasePDFHandle(void* pdf_handle) = 0;
|
||||
|
||||
virtual bool GetPDFPageSizeByIndex(void* pdf_handle,
|
||||
int page_number,
|
||||
double* width,
|
||||
double* height) = 0;
|
||||
};
|
||||
|
||||
} // namespace chrome_pdf
|
||||
|
@ -4004,19 +4004,17 @@ PDFEngineExports* PDFEngineExports::Get() {
|
||||
}
|
||||
|
||||
#if defined(OS_WIN)
|
||||
bool PDFiumEngineExports::RenderPDFPageToDC(const void* pdf_buffer,
|
||||
int buffer_size,
|
||||
bool PDFiumEngineExports::RenderPDFPageToDC(void* pdf_handle,
|
||||
int page_number,
|
||||
const RenderingSettings& settings,
|
||||
HDC dc) {
|
||||
FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, buffer_size, nullptr);
|
||||
FPDF_DOCUMENT doc = pdf_handle;
|
||||
if (!doc)
|
||||
return false;
|
||||
FPDF_PAGE page = FPDF_LoadPage(doc, page_number);
|
||||
if (!page) {
|
||||
FPDF_CloseDocument(doc);
|
||||
if (!page)
|
||||
return false;
|
||||
}
|
||||
|
||||
RenderingSettings new_settings = settings;
|
||||
// calculate the page size
|
||||
if (new_settings.dpi_x == -1)
|
||||
@ -4070,7 +4068,6 @@ bool PDFiumEngineExports::RenderPDFPageToDC(const void* pdf_buffer,
|
||||
}
|
||||
RestoreDC(dc, save_state);
|
||||
FPDF_ClosePage(page);
|
||||
FPDF_CloseDocument(doc);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -4086,20 +4083,16 @@ void PDFiumEngineExports::SetPDFUseGDIPrinting(bool enable) {
|
||||
#endif // defined(OS_WIN)
|
||||
|
||||
bool PDFiumEngineExports::RenderPDFPageToBitmap(
|
||||
const void* pdf_buffer,
|
||||
int pdf_buffer_size,
|
||||
void* pdf_handle,
|
||||
int page_number,
|
||||
const RenderingSettings& settings,
|
||||
void* bitmap_buffer) {
|
||||
FPDF_DOCUMENT doc =
|
||||
FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
|
||||
FPDF_DOCUMENT doc = pdf_handle;
|
||||
if (!doc)
|
||||
return false;
|
||||
FPDF_PAGE page = FPDF_LoadPage(doc, page_number);
|
||||
if (!page) {
|
||||
FPDF_CloseDocument(doc);
|
||||
if (!page)
|
||||
return false;
|
||||
}
|
||||
|
||||
pp::Rect dest;
|
||||
int rotate = CalculatePosition(page, settings, &dest);
|
||||
@ -4118,49 +4111,49 @@ bool PDFiumEngineExports::RenderPDFPageToBitmap(
|
||||
FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
|
||||
FPDFBitmap_Destroy(bitmap);
|
||||
FPDF_ClosePage(page);
|
||||
FPDF_CloseDocument(doc);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PDFiumEngineExports::GetPDFDocInfo(const void* pdf_buffer,
|
||||
int buffer_size,
|
||||
int* page_count,
|
||||
double* max_page_width) {
|
||||
double* max_page_width,
|
||||
void** pdf_handle) {
|
||||
FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, buffer_size, nullptr);
|
||||
if (!doc)
|
||||
return false;
|
||||
|
||||
int page_count_local = FPDF_GetPageCount(doc);
|
||||
if (page_count) {
|
||||
if (page_count)
|
||||
*page_count = page_count_local;
|
||||
}
|
||||
|
||||
if (max_page_width) {
|
||||
*max_page_width = 0;
|
||||
for (int page_number = 0; page_number < page_count_local; page_number++) {
|
||||
double page_width = 0;
|
||||
double page_height = 0;
|
||||
FPDF_GetPageSizeByIndex(doc, page_number, &page_width, &page_height);
|
||||
if (page_width > *max_page_width) {
|
||||
*max_page_width = page_width;
|
||||
}
|
||||
*max_page_width = std::max(*max_page_width, page_width);
|
||||
}
|
||||
}
|
||||
FPDF_CloseDocument(doc);
|
||||
|
||||
if (pdf_handle)
|
||||
*pdf_handle = doc; // Caller takes ownership.
|
||||
else
|
||||
FPDF_CloseDocument(pdf_handle);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PDFiumEngineExports::GetPDFPageSizeByIndex(
|
||||
const void* pdf_buffer,
|
||||
int pdf_buffer_size,
|
||||
int page_number,
|
||||
double* width,
|
||||
double* height) {
|
||||
FPDF_DOCUMENT doc =
|
||||
FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
|
||||
if (!doc)
|
||||
return false;
|
||||
bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
|
||||
FPDF_CloseDocument(doc);
|
||||
return success;
|
||||
void PDFiumEngineExports::ReleasePDFHandle(void* pdf_handle) {
|
||||
FPDF_CloseDocument(pdf_handle);
|
||||
}
|
||||
|
||||
bool PDFiumEngineExports::GetPDFPageSizeByIndex(void* pdf_handle,
|
||||
int page_number,
|
||||
double* width,
|
||||
double* height) {
|
||||
FPDF_DOCUMENT doc = pdf_handle;
|
||||
return doc && FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
|
||||
}
|
||||
|
||||
} // namespace chrome_pdf
|
||||
|
@ -771,8 +771,7 @@ class PDFiumEngineExports : public PDFEngineExports {
|
||||
|
||||
// PDFEngineExports:
|
||||
#if defined(OS_WIN)
|
||||
bool RenderPDFPageToDC(const void* pdf_buffer,
|
||||
int buffer_size,
|
||||
bool RenderPDFPageToDC(void* pdf_handle,
|
||||
int page_number,
|
||||
const RenderingSettings& settings,
|
||||
HDC dc) override;
|
||||
@ -781,17 +780,17 @@ class PDFiumEngineExports : public PDFEngineExports {
|
||||
|
||||
void SetPDFUseGDIPrinting(bool enable) override;
|
||||
#endif // defined(OS_WIN)
|
||||
bool RenderPDFPageToBitmap(const void* pdf_buffer,
|
||||
int pdf_buffer_size,
|
||||
bool RenderPDFPageToBitmap(void* pdf_handle,
|
||||
int page_number,
|
||||
const RenderingSettings& settings,
|
||||
void* bitmap_buffer) override;
|
||||
bool GetPDFDocInfo(const void* pdf_buffer,
|
||||
int buffer_size,
|
||||
int* page_count,
|
||||
double* max_page_width) override;
|
||||
bool GetPDFPageSizeByIndex(const void* pdf_buffer,
|
||||
int pdf_buffer_size,
|
||||
double* max_page_width,
|
||||
void** pdf_handle) override;
|
||||
void ReleasePDFHandle(void* pdf_handle) override;
|
||||
bool GetPDFPageSizeByIndex(void* pdf_handle,
|
||||
int page_number,
|
||||
double* width,
|
||||
double* height) override;
|
||||
|
Reference in New Issue
Block a user