0

Revert "Printing: Load the source PDF only once."

This reverts commit fe899c8e19.

BUG=669797
TBR=thestig@chromium.org

Review-Url: https://codereview.chromium.org/2541843005
Cr-Commit-Position: refs/heads/master@{#435646}
This commit is contained in:
tsepez
2016-12-01 09:56:10 -08:00
committed by Commit bot
parent e8ad7b81c2
commit abff09f770
11 changed files with 114 additions and 125 deletions

@ -82,7 +82,8 @@ const void* PPP_GetInterface(const char* interface_name) {
}
#if defined(OS_WIN)
bool RenderPDFPageToDC(void* pdf_handle,
bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
HDC dc,
int dpi,
@ -106,8 +107,8 @@ bool RenderPDFPageToDC(void* pdf_handle,
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_handle, page_number, settings, dc);
bool ret = engine_exports->RenderPDFPageToDC(pdf_buffer, buffer_size,
page_number, settings, dc);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();
@ -127,49 +128,40 @@ void SetPDFUseGDIPrinting(bool enable) {
bool GetPDFDocInfo(const void* pdf_buffer,
int buffer_size,
int* page_count,
double* max_page_width,
void** pdf_handle) {
double* max_page_width) {
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, pdf_handle);
max_page_width);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();
return ret;
}
void ReleasePDFHandle(void* pdf_handle) {
if (!g_sdk_initialized_via_pepper) {
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,
bool GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
double* width,
double* height) {
if (!g_sdk_initialized_via_pepper) {
if (!InitializeSDK())
if (!chrome_pdf::InitializeSDK())
return false;
}
PDFEngineExports* engine_exports = PDFEngineExports::Get();
bool ret = engine_exports->GetPDFPageSizeByIndex(pdf_handle, page_number,
width, height);
chrome_pdf::PDFEngineExports* engine_exports =
chrome_pdf::PDFEngineExports::Get();
bool ret = engine_exports->GetPDFPageSizeByIndex(pdf_buffer, pdf_buffer_size,
page_number, width, height);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();
chrome_pdf::ShutdownSDK();
return ret;
}
bool RenderPDFPageToBitmap(void* pdf_handle,
bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
void* bitmap_buffer,
int bitmap_width,
@ -184,8 +176,8 @@ bool RenderPDFPageToBitmap(void* pdf_handle,
PDFEngineExports::RenderingSettings settings(
dpi, dpi, pp::Rect(bitmap_width, bitmap_height), true, false, true, true,
autorotate);
bool ret = engine_exports->RenderPDFPageToBitmap(pdf_handle, page_number,
settings, bitmap_buffer);
bool ret = engine_exports->RenderPDFPageToBitmap(
pdf_buffer, pdf_buffer_size, page_number, settings, bitmap_buffer);
if (!g_sdk_initialized_via_pepper)
ShutdownSDK();

@ -36,7 +36,9 @@ void PPP_ShutdownModule();
const void* PPP_GetInterface(const char* interface_name);
#if defined(OS_WIN)
// |pdf_handle| is the handle to the PDF document.
// |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered.
// |buffer_size| is the size of |pdf_buffer| in bytes.
// |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
@ -61,7 +63,8 @@ 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(void* pdf_handle,
bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
HDC dc,
int dpi,
@ -81,35 +84,32 @@ void SetPDFEnsureTypefaceCharactersAccessible(
void SetPDFUseGDIPrinting(bool enable);
#endif // defined(OS_WIN)
// All the out parameters are optional and can be NULL.
// |page_count| and |max_page_width| 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,
void** pdf_handle);
// Releases the handle received from GetPDFDocInfo().
// |pdf_handle| can be NULL.
void ReleasePDFHandle(void* pdf_handle);
double* max_page_width);
// Gets the dimensions of a specific page in a document.
// |pdf_handle| is the handle to the PDF 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.
// |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(void* pdf_handle,
bool GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
double* width,
double* height);
// Renders PDF page into 4-byte per pixel BGRA color bitmap.
// |pdf_handle| is the handle to the PDF 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.
// |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.
@ -118,7 +118,8 @@ bool GetPDFPageSizeByIndex(void* pdf_handle,
// |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(void* pdf_handle,
bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
void* bitmap_buffer,
int bitmap_width,

@ -333,8 +333,9 @@ class PDFEngineExports {
static PDFEngineExports* Get();
#if defined(OS_WIN)
// See the definitions of the corresponding functions in pdf.h for details.
virtual bool RenderPDFPageToDC(void* pdf_handle,
// See the definition of RenderPDFPageToDC in pdf.cc for details.
virtual bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
const RenderingSettings& settings,
HDC dc) = 0;
@ -345,7 +346,9 @@ class PDFEngineExports {
virtual void SetPDFUseGDIPrinting(bool enable) = 0;
#endif // defined(OS_WIN)
virtual bool RenderPDFPageToBitmap(void* pdf_handle,
// See the definition of RenderPDFPageToBitmap in pdf.cc for details.
virtual bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
const RenderingSettings& settings,
void* bitmap_buffer) = 0;
@ -353,12 +356,11 @@ class PDFEngineExports {
virtual bool GetPDFDocInfo(const void* pdf_buffer,
int buffer_size,
int* page_count,
double* max_page_width,
void** pdf_handle) = 0;
double* max_page_width) = 0;
virtual void ReleasePDFHandle(void* pdf_handle) = 0;
virtual bool GetPDFPageSizeByIndex(void* pdf_handle,
// 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;

@ -4007,17 +4007,19 @@ PDFEngineExports* PDFEngineExports::Get() {
}
#if defined(OS_WIN)
bool PDFiumEngineExports::RenderPDFPageToDC(void* pdf_handle,
bool PDFiumEngineExports::RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
const RenderingSettings& settings,
HDC dc) {
FPDF_DOCUMENT doc = pdf_handle;
FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, buffer_size, nullptr);
if (!doc)
return false;
FPDF_PAGE page = FPDF_LoadPage(doc, page_number);
if (!page)
if (!page) {
FPDF_CloseDocument(doc);
return false;
}
RenderingSettings new_settings = settings;
// calculate the page size
if (new_settings.dpi_x == -1)
@ -4071,6 +4073,7 @@ bool PDFiumEngineExports::RenderPDFPageToDC(void* pdf_handle,
}
RestoreDC(dc, save_state);
FPDF_ClosePage(page);
FPDF_CloseDocument(doc);
return true;
}
@ -4086,16 +4089,20 @@ void PDFiumEngineExports::SetPDFUseGDIPrinting(bool enable) {
#endif // defined(OS_WIN)
bool PDFiumEngineExports::RenderPDFPageToBitmap(
void* pdf_handle,
const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
const RenderingSettings& settings,
void* bitmap_buffer) {
FPDF_DOCUMENT doc = pdf_handle;
FPDF_DOCUMENT doc =
FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
if (!doc)
return false;
FPDF_PAGE page = FPDF_LoadPage(doc, page_number);
if (!page)
if (!page) {
FPDF_CloseDocument(doc);
return false;
}
pp::Rect dest;
int rotate = CalculatePosition(page, settings, &dest);
@ -4114,49 +4121,48 @@ 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,
void** pdf_handle) {
double* max_page_width) {
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);
*max_page_width = std::max(*max_page_width, page_width);
if (page_width > *max_page_width) {
*max_page_width = page_width;
}
}
}
if (pdf_handle)
*pdf_handle = doc; // Caller takes ownership.
else
FPDF_CloseDocument(pdf_handle);
FPDF_CloseDocument(doc);
return true;
}
void PDFiumEngineExports::ReleasePDFHandle(void* pdf_handle) {
FPDF_CloseDocument(pdf_handle);
}
bool PDFiumEngineExports::GetPDFPageSizeByIndex(void* pdf_handle,
bool PDFiumEngineExports::GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
double* width,
double* height) {
FPDF_DOCUMENT doc = pdf_handle;
return doc && FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
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;
}
} // namespace chrome_pdf

@ -771,7 +771,8 @@ class PDFiumEngineExports : public PDFEngineExports {
// PDFEngineExports:
#if defined(OS_WIN)
bool RenderPDFPageToDC(void* pdf_handle,
bool RenderPDFPageToDC(const void* pdf_buffer,
int buffer_size,
int page_number,
const RenderingSettings& settings,
HDC dc) override;
@ -780,17 +781,17 @@ class PDFiumEngineExports : public PDFEngineExports {
void SetPDFUseGDIPrinting(bool enable) override;
#endif // defined(OS_WIN)
bool RenderPDFPageToBitmap(void* pdf_handle,
bool RenderPDFPageToBitmap(const void* pdf_buffer,
int pdf_buffer_size,
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,
void** pdf_handle) override;
void ReleasePDFHandle(void* pdf_handle) override;
bool GetPDFPageSizeByIndex(void* pdf_handle,
double* max_page_width) override;
bool GetPDFPageSizeByIndex(const void* pdf_buffer,
int pdf_buffer_size,
int page_number,
double* width,
double* height) override;