Commit 82caa0a0 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Further reduce parameter count for chrome_pdf::RenderPDFPageToBitmap().

Put all the bools into a struct.

Change-Id: I046688c20c1cc13192fa54b2f629531df839d7ce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2430655
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarK. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812113}
parent 2cd75bc1
...@@ -354,6 +354,12 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { ...@@ -354,6 +354,12 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
double max_width_in_pixels = double max_width_in_pixels =
ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi); ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi);
constexpr chrome_pdf::RenderOptions options = {
.stretch_to_bounds = false,
.keep_aspect_ratio = true,
.autorotate = false,
.use_color = true,
};
for (int i = 0; i < num_pages; ++i) { for (int i = 0; i < num_pages; ++i) {
base::Optional<gfx::SizeF> size_in_points = base::Optional<gfx::SizeF> size_in_points =
chrome_pdf::GetPDFPageSizeByIndex(pdf_span, i); chrome_pdf::GetPDFPageSizeByIndex(pdf_span, i);
...@@ -374,9 +380,9 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { ...@@ -374,9 +380,9 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
total_height_in_pixels += height_in_pixels; total_height_in_pixels += height_in_pixels;
gfx::Rect rect(width_in_pixels, height_in_pixels); gfx::Rect rect(width_in_pixels, height_in_pixels);
PdfRenderSettings settings(rect, gfx::Point(0, 0), gfx::Size(kDpi, kDpi),
/*autorotate=*/false, PdfRenderSettings settings(rect, gfx::Point(), gfx::Size(kDpi, kDpi),
/*use_color=*/true, options.autorotate, options.use_color,
PdfRenderSettings::Mode::NORMAL); PdfRenderSettings::Mode::NORMAL);
int int_max = std::numeric_limits<int>::max(); int int_max = std::numeric_limits<int>::max();
...@@ -392,7 +398,7 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest { ...@@ -392,7 +398,7 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap( ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap(
pdf_span, i, page_bitmap_data.data(), settings.area.size(), pdf_span, i, page_bitmap_data.data(), settings.area.size(),
settings.dpi, false, true, settings.autorotate, settings.use_color)); settings.dpi, options));
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(),
......
...@@ -63,9 +63,15 @@ void PdfThumbnailer::GetThumbnail(printing::mojom::ThumbParamsPtr params, ...@@ -63,9 +63,15 @@ void PdfThumbnailer::GetThumbnail(printing::mojom::ThumbParamsPtr params,
} }
// Convert PDF bytes into a bitmap thumbnail. // Convert PDF bytes into a bitmap thumbnail.
if (!chrome_pdf::RenderPDFPageToBitmap( chrome_pdf::RenderOptions options = {
pdf_buffer, 0, result.getPixels(), params->size_px, params->dpi, .stretch_to_bounds = params->stretch,
params->stretch, params->keep_aspect, kAutorotate, kUseColor)) { .keep_aspect_ratio = params->keep_aspect,
.autorotate = kAutorotate,
.use_color = kUseColor,
};
if (!chrome_pdf::RenderPDFPageToBitmap(pdf_buffer, 0, result.getPixels(),
params->size_px, params->dpi,
options)) {
DLOG(ERROR) << "Failed to render PDF buffer as bitmap image"; DLOG(ERROR) << "Failed to render PDF buffer as bitmap image";
std::move(callback).Run(SkBitmap()); std::move(callback).Run(SkBitmap());
return; return;
......
...@@ -48,16 +48,21 @@ base::ReadOnlySharedMemoryRegion RenderPdfPagesToPwgRaster( ...@@ -48,16 +48,21 @@ base::ReadOnlySharedMemoryRegion RenderPdfPagesToPwgRaster(
pwg_data = pwg_encoder::PwgEncoder::GetDocumentHeader(); pwg_data = pwg_encoder::PwgEncoder::GetDocumentHeader();
pwg_encoder::BitmapImage image(settings.area.size(), pwg_encoder::BitmapImage image(settings.area.size(),
pwg_encoder::BitmapImage::BGRA); pwg_encoder::BitmapImage::BGRA);
const chrome_pdf::RenderOptions options = {
.stretch_to_bounds = false,
.keep_aspect_ratio = true,
.autorotate = settings.autorotate,
.use_color = settings.use_color,
};
for (int i = 0; i < total_page_count; ++i) { for (int i = 0; i < total_page_count; ++i) {
int page_number = i; int page_number = i;
if (bitmap_settings.reverse_page_order) if (bitmap_settings.reverse_page_order)
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_data, page_number,
pdf_data, page_number, image.pixel_data(), image.size(), image.pixel_data(), image.size(),
settings.dpi, false, true, settings.autorotate, settings.dpi, options)) {
settings.use_color)) {
return invalid_pwg_region; return invalid_pwg_region;
} }
......
...@@ -354,6 +354,12 @@ class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest { ...@@ -354,6 +354,12 @@ class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest {
EXPECT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_span, &num_pages, nullptr)); EXPECT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_span, &num_pages, nullptr));
EXPECT_EQ(std::ceil(kDocHeight / kPaperHeight), num_pages); EXPECT_EQ(std::ceil(kDocHeight / kPaperHeight), num_pages);
constexpr chrome_pdf::RenderOptions options = {
.stretch_to_bounds = false,
.keep_aspect_ratio = true,
.autorotate = true,
.use_color = true,
};
for (int i = 0; i < num_pages; i++) { for (int i = 0; i < num_pages; i++) {
base::Optional<gfx::SizeF> size_in_points = base::Optional<gfx::SizeF> size_in_points =
chrome_pdf::GetPDFPageSizeByIndex(pdf_span, i); chrome_pdf::GetPDFPageSizeByIndex(pdf_span, i);
...@@ -365,14 +371,13 @@ class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest { ...@@ -365,14 +371,13 @@ class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest {
gfx::Rect rect(kPaperWidth * kDpi, kPaperHeight * kDpi); gfx::Rect rect(kPaperWidth * kDpi, kPaperHeight * kDpi);
printing::PdfRenderSettings settings( printing::PdfRenderSettings settings(
rect, gfx::Point(0, 0), gfx::Size(kDpi, kDpi), /*autorotate=*/true, rect, gfx::Point(), gfx::Size(kDpi, kDpi), options.autorotate,
/*use_color=*/true, printing::PdfRenderSettings::Mode::NORMAL); options.use_color, printing::PdfRenderSettings::Mode::NORMAL);
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_span, i, page_bitmap_data.data(), settings.area.size(), pdf_span, i, page_bitmap_data.data(), settings.area.size(),
settings.dpi, /*stretch_to_bounds=*/false, settings.dpi, options));
/*keep_aspect_ratio=*/true, settings.autorotate, 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
......
...@@ -119,16 +119,14 @@ bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer, ...@@ -119,16 +119,14 @@ bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
void* bitmap_buffer, void* bitmap_buffer,
const gfx::Size& bitmap_size, const gfx::Size& bitmap_size,
const gfx::Size& dpi, const gfx::Size& dpi,
bool stretch_to_bounds, const RenderOptions& options) {
bool keep_aspect_ratio,
bool autorotate,
bool use_color) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true); ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
PDFEngineExports* engine_exports = PDFEngineExports::Get(); PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFEngineExports::RenderingSettings settings( PDFEngineExports::RenderingSettings settings(
dpi, gfx::Rect(bitmap_size), dpi, gfx::Rect(bitmap_size),
/*fit_to_bounds=*/true, stretch_to_bounds, keep_aspect_ratio, /*fit_to_bounds=*/true, options.stretch_to_bounds,
/*center_in_bounds=*/true, autorotate, use_color); options.keep_aspect_ratio,
/*center_in_bounds=*/true, options.autorotate, options.use_color);
return engine_exports->RenderPDFPageToBitmap(pdf_buffer, page_number, return engine_exports->RenderPDFPageToBitmap(pdf_buffer, page_number,
settings, bitmap_buffer); settings, bitmap_buffer);
} }
......
...@@ -127,6 +127,18 @@ base::Optional<gfx::SizeF> GetPDFPageSizeByIndex( ...@@ -127,6 +127,18 @@ base::Optional<gfx::SizeF> GetPDFPageSizeByIndex(
base::span<const uint8_t> pdf_buffer, base::span<const uint8_t> pdf_buffer,
int page_number); int page_number);
struct RenderOptions {
// Whether the output should be stretched to fit the supplied bitmap.
bool stretch_to_bounds;
// If any scaling is needed, whether the original aspect ratio of the page is
// preserved while scaling.
bool keep_aspect_ratio;
// Whether the final image should be rotated to match the output bound.
bool autorotate;
// Specifies color or grayscale.
bool use_color;
};
// 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.
...@@ -134,23 +146,14 @@ base::Optional<gfx::SizeF> GetPDFPageSizeByIndex( ...@@ -134,23 +146,14 @@ base::Optional<gfx::SizeF> GetPDFPageSizeByIndex(
// |bitmap_buffer| is the output buffer for bitmap. // |bitmap_buffer| is the output buffer for bitmap.
// |bitmap_size| is the size of the output bitmap. // |bitmap_size| is the size of the output bitmap.
// |dpi| is the 2D resolution. // |dpi| is the 2D resolution.
// |stretch_to_bounds| specifies whether the output should be stretched to fit // |options| is the options to render with.
// the supplied |bitmap_size|.
// |keep_aspect_ratio| If any scaling is needed, this parameter specifies
// whether the original aspect ratio of the page is preserved while scaling.
// |autorotate| specifies whether the final image should be rotated to match
// the output bound.
// |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(base::span<const uint8_t> pdf_buffer, bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
int page_number, int page_number,
void* bitmap_buffer, void* bitmap_buffer,
const gfx::Size& bitmap_size, const gfx::Size& bitmap_size,
const gfx::Size& dpi, const gfx::Size& dpi,
bool stretch_to_bounds, const RenderOptions& options);
bool keep_aspect_ratio,
bool autorotate,
bool use_color);
// Convert multiple PDF pages into a N-up PDF. // Convert multiple PDF pages into a N-up PDF.
// |input_buffers| is the vector of buffers with each buffer contains a PDF. // |input_buffers| is the vector of buffers with each buffer contains a PDF.
......
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