Commit 0b00ad4c authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Convert functions in pdf/pdf.h to take base::span for inputs.

Change-Id: Ic9aae2a702866818c2b06aaae5710813b6335bfa
Reviewed-on: https://chromium-review.googlesource.com/1135942Reviewed-by: default avatarJianzhou Feng <jzfeng@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577409}
parent 22100358
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/containers/span.h"
#include "base/files/file.h" #include "base/files/file.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
...@@ -341,8 +342,10 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { ...@@ -341,8 +342,10 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
std::string pdf_data; std::string pdf_data;
ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data)); ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data));
ASSERT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(), pdf_data.size(),
&num_pages, &max_width_in_points)); auto pdf_span = base::as_bytes(base::make_span(pdf_data));
ASSERT_TRUE(
chrome_pdf::GetPDFDocInfo(pdf_span, &num_pages, &max_width_in_points));
ASSERT_GT(num_pages, 0); ASSERT_GT(num_pages, 0);
double max_width_in_pixels = double max_width_in_pixels =
...@@ -351,8 +354,7 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { ...@@ -351,8 +354,7 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
for (int i = 0; i < num_pages; ++i) { for (int i = 0; i < num_pages; ++i) {
double width_in_points, height_in_points; double width_in_points, height_in_points;
ASSERT_TRUE(chrome_pdf::GetPDFPageSizeByIndex( ASSERT_TRUE(chrome_pdf::GetPDFPageSizeByIndex(
pdf_data.data(), pdf_data.size(), i, &width_in_points, pdf_span, i, &width_in_points, &height_in_points));
&height_in_points));
double width_in_pixels = ConvertUnitDouble( double width_in_pixels = ConvertUnitDouble(
width_in_points, kPointsPerInch, kDpi); width_in_points, kPointsPerInch, kDpi);
...@@ -386,10 +388,9 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { ...@@ -386,10 +388,9 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
settings.area.size().GetArea()); settings.area.size().GetArea());
ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap( ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap(
pdf_data.data(), pdf_data.size(), i, page_bitmap_data.data(), pdf_span, i, page_bitmap_data.data(), settings.area.size().width(),
settings.area.size().width(), settings.area.size().height(), settings.area.size().height(), settings.dpi.width(),
settings.dpi.width(), settings.dpi.height(), settings.autorotate, settings.dpi.height(), settings.autorotate, settings.use_color));
settings.use_color));
FillPng(&page_bitmap_data, width_in_pixels, max_width_in_pixels, FillPng(&page_bitmap_data, width_in_pixels, max_width_in_pixels,
settings.area.size().height()); settings.area.size().height());
bitmap_data.insert(bitmap_data.end(), bitmap_data.insert(bitmap_data.end(),
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <limits> #include <limits>
#include <utility> #include <utility>
#include "base/containers/span.h"
#include "base/lazy_instance.h" #include "base/lazy_instance.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "components/services/pdf_compositor/public/cpp/pdf_service_mojo_utils.h" #include "components/services/pdf_compositor/public/cpp/pdf_service_mojo_utils.h"
...@@ -111,7 +112,9 @@ void PdfToEmfConverter::LoadPdf(base::ReadOnlySharedMemoryRegion pdf_region) { ...@@ -111,7 +112,9 @@ void PdfToEmfConverter::LoadPdf(base::ReadOnlySharedMemoryRegion pdf_region) {
return; return;
int page_count = 0; int page_count = 0;
chrome_pdf::GetPDFDocInfo(pdf_mapping_.memory(), size, &page_count, nullptr); auto pdf_span = base::make_span(
reinterpret_cast<const uint8_t*>(pdf_mapping_.memory()), size);
chrome_pdf::GetPDFDocInfo(pdf_span, &page_count, nullptr);
total_page_count_ = page_count; total_page_count_ = page_count;
} }
...@@ -149,10 +152,12 @@ base::ReadOnlySharedMemoryRegion PdfToEmfConverter::RenderPdfPageToMetafile( ...@@ -149,10 +152,12 @@ base::ReadOnlySharedMemoryRegion PdfToEmfConverter::RenderPdfPageToMetafile(
int offset_y = postscript ? pdf_render_settings_.offsets.y() : 0; int offset_y = postscript ? pdf_render_settings_.offsets.y() : 0;
base::ReadOnlySharedMemoryRegion invalid_emf_region; base::ReadOnlySharedMemoryRegion invalid_emf_region;
auto pdf_span =
base::make_span(reinterpret_cast<const uint8_t*>(pdf_mapping_.memory()),
pdf_mapping_.size());
if (!chrome_pdf::RenderPDFPageToDC( if (!chrome_pdf::RenderPDFPageToDC(
pdf_mapping_.memory(), pdf_mapping_.size(), page_number, pdf_span, page_number, metafile.context(),
metafile.context(), pdf_render_settings_.dpi.width(), pdf_render_settings_.dpi.width(), pdf_render_settings_.dpi.height(),
pdf_render_settings_.dpi.height(),
pdf_render_settings_.area.x() - offset_x, pdf_render_settings_.area.x() - offset_x,
pdf_render_settings_.area.y() - offset_y, pdf_render_settings_.area.y() - offset_y,
pdf_render_settings_.area.width(), pdf_render_settings_.area.height(), pdf_render_settings_.area.width(), pdf_render_settings_.area.height(),
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "base/containers/span.h"
#include "components/pwg_encoder/bitmap_image.h" #include "components/pwg_encoder/bitmap_image.h"
#include "components/pwg_encoder/pwg_encoder.h" #include "components/pwg_encoder/pwg_encoder.h"
#include "components/services/pdf_compositor/public/cpp/pdf_service_mojo_utils.h" #include "components/services/pdf_compositor/public/cpp/pdf_service_mojo_utils.h"
...@@ -29,13 +30,16 @@ base::ReadOnlySharedMemoryRegion RenderPdfPagesToPwgRaster( ...@@ -29,13 +30,16 @@ base::ReadOnlySharedMemoryRegion RenderPdfPagesToPwgRaster(
if (!pdf_mapping.IsValid()) if (!pdf_mapping.IsValid())
return invalid_pwg_region; return invalid_pwg_region;
auto pdf_data =
base::make_span(reinterpret_cast<const uint8_t*>(pdf_mapping.memory()),
pdf_mapping.size());
// Get the page count and reserve 64 KB per page in |pwg_data| below. // Get the page count and reserve 64 KB per page in |pwg_data| below.
static constexpr size_t kEstimatedSizePerPage = 64 * 1024; static constexpr size_t kEstimatedSizePerPage = 64 * 1024;
static constexpr size_t kMaxPageCount = static constexpr size_t kMaxPageCount =
std::numeric_limits<size_t>::max() / kEstimatedSizePerPage; std::numeric_limits<size_t>::max() / kEstimatedSizePerPage;
int total_page_count = 0; int total_page_count = 0;
if (!chrome_pdf::GetPDFDocInfo(pdf_mapping.memory(), pdf_mapping.size(), if (!chrome_pdf::GetPDFDocInfo(pdf_data, &total_page_count, nullptr) ||
&total_page_count, nullptr) ||
total_page_count <= 0 || total_page_count <= 0 ||
static_cast<size_t>(total_page_count) >= kMaxPageCount) { static_cast<size_t>(total_page_count) >= kMaxPageCount) {
return invalid_pwg_region; return invalid_pwg_region;
...@@ -53,10 +57,9 @@ base::ReadOnlySharedMemoryRegion RenderPdfPagesToPwgRaster( ...@@ -53,10 +57,9 @@ base::ReadOnlySharedMemoryRegion RenderPdfPagesToPwgRaster(
page_number = total_page_count - 1 - page_number; page_number = total_page_count - 1 - page_number;
if (!chrome_pdf::RenderPDFPageToBitmap( if (!chrome_pdf::RenderPDFPageToBitmap(
pdf_mapping.memory(), pdf_mapping.size(), page_number, pdf_data, page_number, image.pixel_data(), image.size().width(),
image.pixel_data(), image.size().width(), image.size().height(), image.size().height(), settings.dpi.width(), settings.dpi.height(),
settings.dpi.width(), settings.dpi.height(), settings.autorotate, settings.autorotate, settings.use_color)) {
settings.use_color)) {
return invalid_pwg_region; return invalid_pwg_region;
} }
......
...@@ -313,17 +313,16 @@ class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest { ...@@ -313,17 +313,16 @@ class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest {
std::string pdf_data; std::string pdf_data;
EXPECT_TRUE(base::Base64Decode(base64, &pdf_data)); EXPECT_TRUE(base::Base64Decode(base64, &pdf_data));
auto pdf_span = base::as_bytes(base::make_span(pdf_data));
int num_pages; int num_pages;
EXPECT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(), pdf_data.size(), EXPECT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_span, &num_pages, nullptr));
&num_pages, nullptr));
EXPECT_EQ(std::ceil(kDocHeight / kPaperHeight), num_pages); EXPECT_EQ(std::ceil(kDocHeight / kPaperHeight), num_pages);
for (int i = 0; i < num_pages; i++) { for (int i = 0; i < num_pages; i++) {
double width_in_points; double width_in_points;
double height_in_points; double height_in_points;
EXPECT_TRUE(chrome_pdf::GetPDFPageSizeByIndex( EXPECT_TRUE(chrome_pdf::GetPDFPageSizeByIndex(
pdf_data.data(), pdf_data.size(), i, &width_in_points, pdf_span, i, &width_in_points, &height_in_points));
&height_in_points));
EXPECT_EQ(static_cast<int>(width_in_points), EXPECT_EQ(static_cast<int>(width_in_points),
static_cast<int>(kPaperWidth * printing::kPointsPerInch)); static_cast<int>(kPaperWidth * printing::kPointsPerInch));
EXPECT_EQ(static_cast<int>(height_in_points), EXPECT_EQ(static_cast<int>(height_in_points),
...@@ -336,10 +335,9 @@ class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest { ...@@ -336,10 +335,9 @@ class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest {
std::vector<uint8_t> page_bitmap_data(kColorChannels * std::vector<uint8_t> page_bitmap_data(kColorChannels *
settings.area.size().GetArea()); settings.area.size().GetArea());
EXPECT_TRUE(chrome_pdf::RenderPDFPageToBitmap( EXPECT_TRUE(chrome_pdf::RenderPDFPageToBitmap(
pdf_data.data(), pdf_data.size(), i, page_bitmap_data.data(), pdf_span, i, page_bitmap_data.data(), settings.area.size().width(),
settings.area.size().width(), settings.area.size().height(), settings.area.size().height(), settings.dpi.width(),
settings.dpi.width(), settings.dpi.height(), settings.autorotate, settings.dpi.height(), settings.autorotate, settings.use_color));
settings.use_color));
EXPECT_EQ(0x56, page_bitmap_data[0]); // B EXPECT_EQ(0x56, page_bitmap_data[0]); // B
EXPECT_EQ(0x34, page_bitmap_data[1]); // G EXPECT_EQ(0x34, page_bitmap_data[1]); // G
EXPECT_EQ(0x12, page_bitmap_data[2]); // R EXPECT_EQ(0x12, page_bitmap_data[2]); // R
......
...@@ -8,10 +8,6 @@ ...@@ -8,10 +8,6 @@
#include <utility> #include <utility>
#if defined(OS_WIN)
#include <windows.h>
#endif
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "pdf/out_of_process_instance.h" #include "pdf/out_of_process_instance.h"
...@@ -51,8 +47,7 @@ class ScopedSdkInitializer { ...@@ -51,8 +47,7 @@ class ScopedSdkInitializer {
} // namespace } // namespace
#if defined(OS_WIN) #if defined(OS_WIN)
bool RenderPDFPageToDC(const void* pdf_buffer, bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
int buffer_size,
int page_number, int page_number,
HDC dc, HDC dc,
int dpi_x, int dpi_x,
...@@ -77,8 +72,8 @@ bool RenderPDFPageToDC(const void* pdf_buffer, ...@@ -77,8 +72,8 @@ bool RenderPDFPageToDC(const void* pdf_buffer,
pp::Rect(bounds_origin_x, bounds_origin_y, bounds_width, bounds_height), pp::Rect(bounds_origin_x, bounds_origin_y, bounds_width, bounds_height),
fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds, fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
autorotate, use_color); autorotate, use_color);
return engine_exports->RenderPDFPageToDC(pdf_buffer, buffer_size, page_number, return engine_exports->RenderPDFPageToDC(pdf_buffer, page_number, settings,
settings, dc); dc);
} }
void SetPDFEnsureTypefaceCharactersAccessible( void SetPDFEnsureTypefaceCharactersAccessible(
...@@ -95,8 +90,7 @@ void SetPDFUsePrintMode(int mode) { ...@@ -95,8 +90,7 @@ void SetPDFUsePrintMode(int mode) {
} }
#endif // defined(OS_WIN) #endif // defined(OS_WIN)
bool GetPDFDocInfo(const void* pdf_buffer, bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int buffer_size,
int* page_count, int* page_count,
double* max_page_width) { double* max_page_width) {
ScopedSdkInitializer scoped_sdk_initializer; ScopedSdkInitializer scoped_sdk_initializer;
...@@ -104,12 +98,10 @@ bool GetPDFDocInfo(const void* pdf_buffer, ...@@ -104,12 +98,10 @@ bool GetPDFDocInfo(const void* pdf_buffer,
return false; return false;
PDFEngineExports* engine_exports = PDFEngineExports::Get(); PDFEngineExports* engine_exports = PDFEngineExports::Get();
return engine_exports->GetPDFDocInfo(pdf_buffer, buffer_size, page_count, return engine_exports->GetPDFDocInfo(pdf_buffer, page_count, max_page_width);
max_page_width);
} }
bool GetPDFPageSizeByIndex(const void* pdf_buffer, bool GetPDFPageSizeByIndex(base::span<const uint8_t> pdf_buffer,
int pdf_buffer_size,
int page_number, int page_number,
double* width, double* width,
double* height) { double* height) {
...@@ -119,12 +111,11 @@ bool GetPDFPageSizeByIndex(const void* pdf_buffer, ...@@ -119,12 +111,11 @@ bool GetPDFPageSizeByIndex(const void* pdf_buffer,
chrome_pdf::PDFEngineExports* engine_exports = chrome_pdf::PDFEngineExports* engine_exports =
chrome_pdf::PDFEngineExports::Get(); chrome_pdf::PDFEngineExports::Get();
return engine_exports->GetPDFPageSizeByIndex(pdf_buffer, pdf_buffer_size, return engine_exports->GetPDFPageSizeByIndex(pdf_buffer, page_number, width,
page_number, width, height); height);
} }
bool RenderPDFPageToBitmap(const void* pdf_buffer, bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
int pdf_buffer_size,
int page_number, int page_number,
void* bitmap_buffer, void* bitmap_buffer,
int bitmap_width, int bitmap_width,
...@@ -141,8 +132,8 @@ bool RenderPDFPageToBitmap(const void* pdf_buffer, ...@@ -141,8 +132,8 @@ bool RenderPDFPageToBitmap(const void* pdf_buffer,
PDFEngineExports::RenderingSettings settings( PDFEngineExports::RenderingSettings settings(
dpi_x, dpi_y, pp::Rect(bitmap_width, bitmap_height), true, false, true, dpi_x, dpi_y, pp::Rect(bitmap_width, bitmap_height), true, false, true,
true, autorotate, use_color); true, autorotate, use_color);
return engine_exports->RenderPDFPageToBitmap( return engine_exports->RenderPDFPageToBitmap(pdf_buffer, page_number,
pdf_buffer, pdf_buffer_size, page_number, settings, bitmap_buffer); settings, bitmap_buffer);
} }
bool ConvertPdfPagesToNupPdf( bool ConvertPdfPagesToNupPdf(
......
...@@ -33,7 +33,6 @@ enum PrintingMode { ...@@ -33,7 +33,6 @@ enum PrintingMode {
// |pdf_buffer| is the buffer that contains the entire PDF document to be // |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered. // rendered.
// |buffer_size| is the size of |pdf_buffer| in bytes.
// |page_number| is the 0-based index of the page to be rendered. // |page_number| is the 0-based index of the page to be rendered.
// |dc| is the device context to render into. // |dc| is the device context to render into.
// |dpi_x| and |dpi_y| is the resolution. // |dpi_x| and |dpi_y| is the resolution.
...@@ -58,8 +57,7 @@ enum PrintingMode { ...@@ -58,8 +57,7 @@ enum PrintingMode {
// the output bound. // the output bound.
// |use_color| specifies color or grayscale. // |use_color| specifies color or grayscale.
// Returns false if the document or the page number are not valid. // Returns false if the document or the page number are not valid.
bool RenderPDFPageToDC(const void* pdf_buffer, bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
int buffer_size,
int page_number, int page_number,
HDC dc, HDC dc,
int dpi_x, int dpi_x,
...@@ -85,22 +83,19 @@ void SetPDFUsePrintMode(int mode); ...@@ -85,22 +83,19 @@ void SetPDFUsePrintMode(int mode);
// |page_count| and |max_page_width| 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 false if the document is not valid.
bool GetPDFDocInfo(const void* pdf_buffer, bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int buffer_size,
int* page_count, int* page_count,
double* max_page_width); double* max_page_width);
// Gets the dimensions of a specific page in a document. // Gets the dimensions of a specific page in a document.
// |pdf_buffer| is the buffer that contains the entire PDF document to be // |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered. // 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 // |page_number| is the page number that the function will get the dimensions
// of. // of.
// |width| is the output for the width of the page in points. // |width| is the output for the width of the page in points.
// |height| is the output for the height 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. // Returns false if the document or the page number are not valid.
bool GetPDFPageSizeByIndex(const void* pdf_buffer, bool GetPDFPageSizeByIndex(base::span<const uint8_t> pdf_buffer,
int pdf_buffer_size,
int page_number, int page_number,
double* width, double* width,
double* height); double* height);
...@@ -108,7 +103,6 @@ bool GetPDFPageSizeByIndex(const void* pdf_buffer, ...@@ -108,7 +103,6 @@ bool GetPDFPageSizeByIndex(const void* pdf_buffer,
// Renders PDF page into 4-byte per pixel BGRA color bitmap. // Renders PDF page into 4-byte per pixel BGRA color bitmap.
// |pdf_buffer| is the buffer that contains the entire PDF document to be // |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered. // 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. // |page_number| is the 0-based index of the page to be rendered.
// |bitmap_buffer| is the output buffer for bitmap. // |bitmap_buffer| is the output buffer for bitmap.
// |bitmap_width| is the width of the output bitmap. // |bitmap_width| is the width of the output bitmap.
...@@ -118,8 +112,7 @@ bool GetPDFPageSizeByIndex(const void* pdf_buffer, ...@@ -118,8 +112,7 @@ bool GetPDFPageSizeByIndex(const void* pdf_buffer,
// the output bound. // the output bound.
// |use_color| specifies color or grayscale. // |use_color| specifies color or grayscale.
// Returns false if the document or the page number are not valid. // Returns false if the document or the page number are not valid.
bool RenderPDFPageToBitmap(const void* pdf_buffer, bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
int pdf_buffer_size,
int page_number, int page_number,
void* bitmap_buffer, void* bitmap_buffer,
int bitmap_width, int bitmap_width,
......
...@@ -7,12 +7,6 @@ ...@@ -7,12 +7,6 @@
#include <stdint.h> #include <stdint.h>
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
#include <memory> #include <memory>
#include <set> #include <set>
#include <string> #include <string>
...@@ -22,6 +16,7 @@ ...@@ -22,6 +16,7 @@
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "build/build_config.h"
#include "ppapi/c/dev/pp_cursor_type_dev.h" #include "ppapi/c/dev/pp_cursor_type_dev.h"
#include "ppapi/c/dev/ppp_printing_dev.h" #include "ppapi/c/dev/ppp_printing_dev.h"
#include "ppapi/c/ppb_input_event.h" #include "ppapi/c/ppb_input_event.h"
...@@ -34,6 +29,10 @@ ...@@ -34,6 +29,10 @@
#include "ui/base/window_open_disposition.h" #include "ui/base/window_open_disposition.h"
#include "ui/gfx/geometry/point_f.h" #include "ui/gfx/geometry/point_f.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
#if defined(OS_WIN) #if defined(OS_WIN)
typedef void (*PDFEnsureTypefaceCharactersAccessible)(const LOGFONT* font, typedef void (*PDFEnsureTypefaceCharactersAccessible)(const LOGFONT* font,
const wchar_t* text, const wchar_t* text,
...@@ -445,8 +444,7 @@ class PDFEngineExports { ...@@ -445,8 +444,7 @@ class PDFEngineExports {
#if defined(OS_WIN) #if defined(OS_WIN)
// See the definition of RenderPDFPageToDC in pdf.cc for details. // See the definition of RenderPDFPageToDC in pdf.cc for details.
virtual bool RenderPDFPageToDC(const void* pdf_buffer, virtual bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
int buffer_size,
int page_number, int page_number,
const RenderingSettings& settings, const RenderingSettings& settings,
HDC dc) = 0; HDC dc) = 0;
...@@ -459,8 +457,7 @@ class PDFEngineExports { ...@@ -459,8 +457,7 @@ class PDFEngineExports {
#endif // defined(OS_WIN) #endif // defined(OS_WIN)
// See the definition of RenderPDFPageToBitmap in pdf.cc for details. // See the definition of RenderPDFPageToBitmap in pdf.cc for details.
virtual bool RenderPDFPageToBitmap(const void* pdf_buffer, virtual bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
int pdf_buffer_size,
int page_number, int page_number,
const RenderingSettings& settings, const RenderingSettings& settings,
void* bitmap_buffer) = 0; void* bitmap_buffer) = 0;
...@@ -483,14 +480,12 @@ class PDFEngineExports { ...@@ -483,14 +480,12 @@ class PDFEngineExports {
void** dest_pdf_buffer, void** dest_pdf_buffer,
size_t* dest_pdf_buffer_size) = 0; size_t* dest_pdf_buffer_size) = 0;
virtual bool GetPDFDocInfo(const void* pdf_buffer, virtual bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int buffer_size,
int* page_count, int* page_count,
double* max_page_width) = 0; double* max_page_width) = 0;
// See the definition of GetPDFPageSizeByIndex in pdf.cc for details. // See the definition of GetPDFPageSizeByIndex in pdf.cc for details.
virtual bool GetPDFPageSizeByIndex(const void* pdf_buffer, virtual bool GetPDFPageSizeByIndex(base::span<const uint8_t> pdf_buffer,
int pdf_buffer_size,
int page_number, int page_number,
double* width, double* width,
double* height) = 0; double* height) = 0;
......
...@@ -95,12 +95,10 @@ int CalculatePosition(FPDF_PAGE page, ...@@ -95,12 +95,10 @@ int CalculatePosition(FPDF_PAGE page,
} }
ScopedFPDFDocument LoadPdfData(base::span<const uint8_t> pdf_buffer) { ScopedFPDFDocument LoadPdfData(base::span<const uint8_t> pdf_buffer) {
ScopedFPDFDocument doc; if (!base::IsValueInRangeForNumericType<int>(pdf_buffer.size()))
if (base::IsValueInRangeForNumericType<int>(pdf_buffer.size())) { return nullptr;
doc.reset( return ScopedFPDFDocument(
FPDF_LoadMemDocument(pdf_buffer.data(), pdf_buffer.size(), nullptr)); FPDF_LoadMemDocument(pdf_buffer.data(), pdf_buffer.size(), nullptr));
}
return doc;
} }
ScopedFPDFDocument CreatePdfDoc( ScopedFPDFDocument CreatePdfDoc(
...@@ -188,13 +186,12 @@ PDFiumEngineExports::PDFiumEngineExports() {} ...@@ -188,13 +186,12 @@ PDFiumEngineExports::PDFiumEngineExports() {}
PDFiumEngineExports::~PDFiumEngineExports() {} PDFiumEngineExports::~PDFiumEngineExports() {}
#if defined(OS_WIN) #if defined(OS_WIN)
bool PDFiumEngineExports::RenderPDFPageToDC(const void* pdf_buffer, bool PDFiumEngineExports::RenderPDFPageToDC(
int buffer_size, base::span<const uint8_t> pdf_buffer,
int page_number, int page_number,
const RenderingSettings& settings, const RenderingSettings& settings,
HDC dc) { HDC dc) {
ScopedFPDFDocument doc( ScopedFPDFDocument doc = LoadPdfData(pdf_buffer);
FPDF_LoadMemDocument(pdf_buffer, buffer_size, nullptr));
if (!doc) if (!doc)
return false; return false;
ScopedFPDFPage page(FPDF_LoadPage(doc.get(), page_number)); ScopedFPDFPage page(FPDF_LoadPage(doc.get(), page_number));
...@@ -276,13 +273,11 @@ void PDFiumEngineExports::SetPDFUsePrintMode(int mode) { ...@@ -276,13 +273,11 @@ void PDFiumEngineExports::SetPDFUsePrintMode(int mode) {
#endif // defined(OS_WIN) #endif // defined(OS_WIN)
bool PDFiumEngineExports::RenderPDFPageToBitmap( bool PDFiumEngineExports::RenderPDFPageToBitmap(
const void* pdf_buffer, base::span<const uint8_t> pdf_buffer,
int pdf_buffer_size,
int page_number, int page_number,
const RenderingSettings& settings, const RenderingSettings& settings,
void* bitmap_buffer) { void* bitmap_buffer) {
ScopedFPDFDocument doc( ScopedFPDFDocument doc = LoadPdfData(pdf_buffer);
FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr));
if (!doc) if (!doc)
return false; return false;
ScopedFPDFPage page(FPDF_LoadPage(doc.get(), page_number)); ScopedFPDFPage page(FPDF_LoadPage(doc.get(), page_number));
...@@ -342,12 +337,10 @@ bool PDFiumEngineExports::ConvertPdfDocumentToNupPdf( ...@@ -342,12 +337,10 @@ bool PDFiumEngineExports::ConvertPdfDocumentToNupPdf(
dest_pdf_buffer_size); dest_pdf_buffer_size);
} }
bool PDFiumEngineExports::GetPDFDocInfo(const void* pdf_buffer, bool PDFiumEngineExports::GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int buffer_size,
int* page_count, int* page_count,
double* max_page_width) { double* max_page_width) {
ScopedFPDFDocument doc( ScopedFPDFDocument doc = LoadPdfData(pdf_buffer);
FPDF_LoadMemDocument(pdf_buffer, buffer_size, nullptr));
if (!doc) if (!doc)
return false; return false;
...@@ -373,13 +366,12 @@ bool PDFiumEngineExports::GetPDFDocInfo(const void* pdf_buffer, ...@@ -373,13 +366,12 @@ bool PDFiumEngineExports::GetPDFDocInfo(const void* pdf_buffer,
return true; return true;
} }
bool PDFiumEngineExports::GetPDFPageSizeByIndex(const void* pdf_buffer, bool PDFiumEngineExports::GetPDFPageSizeByIndex(
int pdf_buffer_size, base::span<const uint8_t> pdf_buffer,
int page_number, int page_number,
double* width, double* width,
double* height) { double* height) {
ScopedFPDFDocument doc( ScopedFPDFDocument doc = LoadPdfData(pdf_buffer);
FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr));
if (!doc) if (!doc)
return false; return false;
return FPDF_GetPageSizeByIndex(doc.get(), page_number, width, height) != 0; return FPDF_GetPageSizeByIndex(doc.get(), page_number, width, height) != 0;
......
...@@ -21,8 +21,7 @@ class PDFiumEngineExports : public PDFEngineExports { ...@@ -21,8 +21,7 @@ class PDFiumEngineExports : public PDFEngineExports {
// PDFEngineExports: // PDFEngineExports:
#if defined(OS_WIN) #if defined(OS_WIN)
bool RenderPDFPageToDC(const void* pdf_buffer, bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
int buffer_size,
int page_number, int page_number,
const RenderingSettings& settings, const RenderingSettings& settings,
HDC dc) override; HDC dc) override;
...@@ -32,8 +31,7 @@ class PDFiumEngineExports : public PDFEngineExports { ...@@ -32,8 +31,7 @@ class PDFiumEngineExports : public PDFEngineExports {
void SetPDFUseGDIPrinting(bool enable) override; void SetPDFUseGDIPrinting(bool enable) override;
void SetPDFUsePrintMode(int mode) override; void SetPDFUsePrintMode(int mode) override;
#endif // defined(OS_WIN) #endif // defined(OS_WIN)
bool RenderPDFPageToBitmap(const void* pdf_buffer, bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
int pdf_buffer_size,
int page_number, int page_number,
const RenderingSettings& settings, const RenderingSettings& settings,
void* bitmap_buffer) override; void* bitmap_buffer) override;
...@@ -50,12 +48,10 @@ class PDFiumEngineExports : public PDFEngineExports { ...@@ -50,12 +48,10 @@ class PDFiumEngineExports : public PDFEngineExports {
size_t page_size_height, size_t page_size_height,
void** dest_pdf_buffer, void** dest_pdf_buffer,
size_t* dest_pdf_buffer_size) override; size_t* dest_pdf_buffer_size) override;
bool GetPDFDocInfo(const void* pdf_buffer, bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int buffer_size,
int* page_count, int* page_count,
double* max_page_width) override; double* max_page_width) override;
bool GetPDFPageSizeByIndex(const void* pdf_buffer, bool GetPDFPageSizeByIndex(base::span<const uint8_t> pdf_buffer,
int pdf_buffer_size,
int page_number, int page_number,
double* width, double* width,
double* height) override; double* height) override;
......
...@@ -61,15 +61,12 @@ TEST_F(PDFiumEngineExportsTest, GetPDFDocInfo) { ...@@ -61,15 +61,12 @@ TEST_F(PDFiumEngineExportsTest, GetPDFDocInfo) {
std::string pdf_data; std::string pdf_data;
ASSERT_TRUE(base::ReadFileToString(pdf_path, &pdf_data)); ASSERT_TRUE(base::ReadFileToString(pdf_path, &pdf_data));
EXPECT_FALSE(GetPDFDocInfo(nullptr, 0, nullptr, nullptr)); auto pdf_span = base::as_bytes(base::make_span(pdf_data));
ASSERT_TRUE(GetPDFDocInfo(pdf_span, nullptr, nullptr));
ASSERT_TRUE(
GetPDFDocInfo(pdf_data.data(), pdf_data.size(), nullptr, nullptr));
int page_count; int page_count;
double max_page_width; double max_page_width;
ASSERT_TRUE(GetPDFDocInfo(pdf_data.data(), pdf_data.size(), &page_count, ASSERT_TRUE(GetPDFDocInfo(pdf_span, &page_count, &max_page_width));
&max_page_width));
EXPECT_EQ(2, page_count); EXPECT_EQ(2, page_count);
EXPECT_DOUBLE_EQ(200.0, max_page_width); EXPECT_DOUBLE_EQ(200.0, max_page_width);
} }
...@@ -82,17 +79,16 @@ TEST_F(PDFiumEngineExportsTest, GetPDFPageSizeByIndex) { ...@@ -82,17 +79,16 @@ TEST_F(PDFiumEngineExportsTest, GetPDFPageSizeByIndex) {
std::string pdf_data; std::string pdf_data;
ASSERT_TRUE(base::ReadFileToString(pdf_path, &pdf_data)); ASSERT_TRUE(base::ReadFileToString(pdf_path, &pdf_data));
EXPECT_FALSE(GetPDFPageSizeByIndex(nullptr, 0, 0, nullptr, nullptr)); auto pdf_span = base::as_bytes(base::make_span(pdf_data));
EXPECT_FALSE(GetPDFPageSizeByIndex(pdf_span, 0, nullptr, nullptr));
int page_count; int page_count;
ASSERT_TRUE( ASSERT_TRUE(GetPDFDocInfo(pdf_span, &page_count, nullptr));
GetPDFDocInfo(pdf_data.data(), pdf_data.size(), &page_count, nullptr));
ASSERT_EQ(2, page_count); ASSERT_EQ(2, page_count);
for (int page_number = 0; page_number < page_count; ++page_number) { for (int page_number = 0; page_number < page_count; ++page_number) {
double width; double width;
double height; double height;
ASSERT_TRUE(GetPDFPageSizeByIndex(pdf_data.data(), pdf_data.size(), ASSERT_TRUE(GetPDFPageSizeByIndex(pdf_span, page_number, &width, &height));
page_number, &width, &height));
EXPECT_DOUBLE_EQ(200.0, width); EXPECT_DOUBLE_EQ(200.0, width);
EXPECT_DOUBLE_EQ(200.0, height); EXPECT_DOUBLE_EQ(200.0, height);
} }
...@@ -118,15 +114,16 @@ TEST_F(PDFiumEngineExportsTest, ConvertPdfPagesToNupPdf) { ...@@ -118,15 +114,16 @@ TEST_F(PDFiumEngineExportsTest, ConvertPdfPagesToNupPdf) {
pdf_buffers, 2, 512, 792, &output_pdf_buffer, &output_pdf_buffer_size)); pdf_buffers, 2, 512, 792, &output_pdf_buffer, &output_pdf_buffer_size));
ASSERT_GT(output_pdf_buffer_size, 0U); ASSERT_GT(output_pdf_buffer_size, 0U);
ASSERT_NE(output_pdf_buffer, nullptr); ASSERT_NE(output_pdf_buffer, nullptr);
base::span<const uint8_t> output_pdf_span = base::make_span(
reinterpret_cast<uint8_t*>(output_pdf_buffer), output_pdf_buffer_size);
int page_count; int page_count;
ASSERT_TRUE(GetPDFDocInfo(output_pdf_buffer, output_pdf_buffer_size, ASSERT_TRUE(GetPDFDocInfo(output_pdf_span, &page_count, nullptr));
&page_count, nullptr));
ASSERT_EQ(1, page_count); ASSERT_EQ(1, page_count);
double width; double width;
double height; double height;
ASSERT_TRUE(GetPDFPageSizeByIndex(output_pdf_buffer, output_pdf_buffer_size, ASSERT_TRUE(GetPDFPageSizeByIndex(output_pdf_span, 0, &width, &height));
0, &width, &height));
EXPECT_DOUBLE_EQ(792.0, width); EXPECT_DOUBLE_EQ(792.0, width);
EXPECT_DOUBLE_EQ(512.0, height); EXPECT_DOUBLE_EQ(512.0, height);
...@@ -152,15 +149,17 @@ TEST_F(PDFiumEngineExportsTest, ConvertPdfDocumentToNupPdf) { ...@@ -152,15 +149,17 @@ TEST_F(PDFiumEngineExportsTest, ConvertPdfDocumentToNupPdf) {
pdf_buffer, 4, 512, 792, &output_pdf_buffer, &output_pdf_buffer_size)); pdf_buffer, 4, 512, 792, &output_pdf_buffer, &output_pdf_buffer_size));
ASSERT_GT(output_pdf_buffer_size, 0U); ASSERT_GT(output_pdf_buffer_size, 0U);
ASSERT_NE(output_pdf_buffer, nullptr); ASSERT_NE(output_pdf_buffer, nullptr);
base::span<const uint8_t> output_pdf_span = base::make_span(
reinterpret_cast<uint8_t*>(output_pdf_buffer), output_pdf_buffer_size);
int page_count; int page_count;
ASSERT_TRUE(GetPDFDocInfo(output_pdf_buffer, output_pdf_buffer_size, ASSERT_TRUE(GetPDFDocInfo(output_pdf_span, &page_count, nullptr));
&page_count, nullptr));
ASSERT_EQ(2, page_count); ASSERT_EQ(2, page_count);
for (int page_number = 0; page_number < page_count; ++page_number) { for (int page_number = 0; page_number < page_count; ++page_number) {
double width; double width;
double height; double height;
ASSERT_TRUE(GetPDFPageSizeByIndex(output_pdf_buffer, output_pdf_buffer_size, ASSERT_TRUE(
page_number, &width, &height)); GetPDFPageSizeByIndex(output_pdf_span, page_number, &width, &height));
EXPECT_DOUBLE_EQ(512.0, width); EXPECT_DOUBLE_EQ(512.0, width);
EXPECT_DOUBLE_EQ(792.0, height); EXPECT_DOUBLE_EQ(792.0, height);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment