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 {
double max_width_in_pixels =
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) {
base::Optional<gfx::SizeF> size_in_points =
chrome_pdf::GetPDFPageSizeByIndex(pdf_span, i);
......@@ -374,9 +380,9 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
total_height_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,
/*use_color=*/true,
PdfRenderSettings settings(rect, gfx::Point(), gfx::Size(kDpi, kDpi),
options.autorotate, options.use_color,
PdfRenderSettings::Mode::NORMAL);
int int_max = std::numeric_limits<int>::max();
......@@ -392,7 +398,7 @@ class PrintPreviewPdfGeneratedBrowserTest : public InProcessBrowserTest {
ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap(
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,
settings.area.size().height());
bitmap_data.insert(bitmap_data.end(),
......
......@@ -63,9 +63,15 @@ void PdfThumbnailer::GetThumbnail(printing::mojom::ThumbParamsPtr params,
}
// Convert PDF bytes into a bitmap thumbnail.
if (!chrome_pdf::RenderPDFPageToBitmap(
pdf_buffer, 0, result.getPixels(), params->size_px, params->dpi,
params->stretch, params->keep_aspect, kAutorotate, kUseColor)) {
chrome_pdf::RenderOptions options = {
.stretch_to_bounds = params->stretch,
.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";
std::move(callback).Run(SkBitmap());
return;
......
......@@ -48,16 +48,21 @@ base::ReadOnlySharedMemoryRegion RenderPdfPagesToPwgRaster(
pwg_data = pwg_encoder::PwgEncoder::GetDocumentHeader();
pwg_encoder::BitmapImage image(settings.area.size(),
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) {
int page_number = i;
if (bitmap_settings.reverse_page_order)
page_number = total_page_count - 1 - page_number;
if (!chrome_pdf::RenderPDFPageToBitmap(
pdf_data, page_number, image.pixel_data(), image.size(),
settings.dpi, false, true, settings.autorotate,
settings.use_color)) {
if (!chrome_pdf::RenderPDFPageToBitmap(pdf_data, page_number,
image.pixel_data(), image.size(),
settings.dpi, options)) {
return invalid_pwg_region;
}
......
......@@ -354,6 +354,12 @@ class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest {
EXPECT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_span, &num_pages, nullptr));
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++) {
base::Optional<gfx::SizeF> size_in_points =
chrome_pdf::GetPDFPageSizeByIndex(pdf_span, i);
......@@ -365,14 +371,13 @@ class HeadlessWebContentsPDFTest : public HeadlessAsyncDevTooledBrowserTest {
gfx::Rect rect(kPaperWidth * kDpi, kPaperHeight * kDpi);
printing::PdfRenderSettings settings(
rect, gfx::Point(0, 0), gfx::Size(kDpi, kDpi), /*autorotate=*/true,
/*use_color=*/true, printing::PdfRenderSettings::Mode::NORMAL);
rect, gfx::Point(), gfx::Size(kDpi, kDpi), options.autorotate,
options.use_color, printing::PdfRenderSettings::Mode::NORMAL);
std::vector<uint8_t> page_bitmap_data(kColorChannels *
settings.area.size().GetArea());
EXPECT_TRUE(chrome_pdf::RenderPDFPageToBitmap(
pdf_span, i, page_bitmap_data.data(), settings.area.size(),
settings.dpi, /*stretch_to_bounds=*/false,
/*keep_aspect_ratio=*/true, settings.autorotate, settings.use_color));
settings.dpi, options));
EXPECT_EQ(0x56, page_bitmap_data[0]); // B
EXPECT_EQ(0x34, page_bitmap_data[1]); // G
EXPECT_EQ(0x12, page_bitmap_data[2]); // R
......
......@@ -119,16 +119,14 @@ bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
void* bitmap_buffer,
const gfx::Size& bitmap_size,
const gfx::Size& dpi,
bool stretch_to_bounds,
bool keep_aspect_ratio,
bool autorotate,
bool use_color) {
const RenderOptions& options) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFEngineExports::RenderingSettings settings(
dpi, gfx::Rect(bitmap_size),
/*fit_to_bounds=*/true, stretch_to_bounds, keep_aspect_ratio,
/*center_in_bounds=*/true, autorotate, use_color);
/*fit_to_bounds=*/true, options.stretch_to_bounds,
options.keep_aspect_ratio,
/*center_in_bounds=*/true, options.autorotate, options.use_color);
return engine_exports->RenderPDFPageToBitmap(pdf_buffer, page_number,
settings, bitmap_buffer);
}
......
......@@ -127,6 +127,18 @@ base::Optional<gfx::SizeF> GetPDFPageSizeByIndex(
base::span<const uint8_t> pdf_buffer,
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.
// |pdf_buffer| is the buffer that contains the entire PDF document to be
// rendered.
......@@ -134,23 +146,14 @@ base::Optional<gfx::SizeF> GetPDFPageSizeByIndex(
// |bitmap_buffer| is the output buffer for bitmap.
// |bitmap_size| is the size of the output bitmap.
// |dpi| is the 2D resolution.
// |stretch_to_bounds| specifies whether the output should be stretched to fit
// 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.
// |options| is the options to render with.
// Returns false if the document or the page number are not valid.
bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
int page_number,
void* bitmap_buffer,
const gfx::Size& bitmap_size,
const gfx::Size& dpi,
bool stretch_to_bounds,
bool keep_aspect_ratio,
bool autorotate,
bool use_color);
const RenderOptions& options);
// Convert multiple PDF pages into a N-up 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