Commit 950533a3 authored by Andrey Kosyakov's avatar Andrey Kosyakov Committed by Commit Bot

Round expected page size in LocalFrameView::ForceLayoutForPagination

This fixes a regression after https://crrev.com/c/1861035 that caused a
web page to require more printed pages due to an inconsistency of rounding
directions.
We used to implicitly round the rect returned by LayoutView::DocumentRect(),
as it used to return a snapped-to-int rect. Now that we've started returning
a PhysicalRect, it gets to ResizePageRectsKeepingRatio() as is and gets
truncated there. So let's round sizes explicitly before passing to
ResizePageRectsKeepingRatio().

BUG=650768, b/142742179

Change-Id: I55a9df6977f464452fc4878517796717761c5b07
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1867126Reviewed-by: default avatarXianzhu Wang <wangxianzhu@chromium.org>
Commit-Queue: Andrey Kosyakov <caseq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709153}
parent da9f5498
......@@ -415,6 +415,52 @@ class HeadlessWebContentsPDFStreamTest
};
HEADLESS_ASYNC_DEVTOOLED_TEST_F(HeadlessWebContentsPDFStreamTest);
class HeadlessWebContentsPDFPageSizeRoundingTest
: public HeadlessAsyncDevTooledBrowserTest,
public page::Observer {
public:
void RunDevTooledTest() override {
EXPECT_TRUE(embedded_test_server()->Start());
devtools_client_->GetPage()->AddObserver(this);
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
devtools_client_->GetPage()->Enable(run_loop.QuitClosure());
run_loop.Run();
devtools_client_->GetPage()->Navigate(
embedded_test_server()->GetURL("/red_square.html").spec());
}
void OnLoadEventFired(const page::LoadEventFiredParams&) override {
devtools_client_->GetPage()->GetExperimental()->PrintToPDF(
page::PrintToPDFParams::Builder()
.SetPrintBackground(true)
.SetPaperHeight(41)
.SetPaperWidth(41)
.SetMarginTop(0)
.SetMarginBottom(0)
.SetMarginLeft(0)
.SetMarginRight(0)
.Build(),
base::BindOnce(
&HeadlessWebContentsPDFPageSizeRoundingTest::OnPDFCreated,
base::Unretained(this)));
}
void OnPDFCreated(std::unique_ptr<page::PrintToPDFResult> result) {
protocol::Binary pdf_data = result->GetData();
EXPECT_GT(pdf_data.size(), 0U);
auto pdf_span = base::make_span(pdf_data.data(), pdf_data.size());
int num_pages;
EXPECT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_span, &num_pages, nullptr));
EXPECT_THAT(num_pages, testing::Eq(1));
FinishAsynchronousTest();
}
};
HEADLESS_ASYNC_DEVTOOLED_TEST_F(HeadlessWebContentsPDFPageSizeRoundingTest);
#endif
class HeadlessWebContentsSecurityTest
......
<!DOCTYPE html>
<style>
@page {
margin: 0;
size: 53.984375px 53.984375px;
}
* {
margin: 0;
}
div {
background: red;
width: 53.984375px;
height: 53.984375px;
}
</style>
<div></div>
......@@ -2991,10 +2991,12 @@ void LocalFrameView::ForceLayoutForPagination(
? document_rect.Width()
: document_rect.Height();
if (doc_logical_width > page_logical_width) {
// ResizePageRectsKeepingRatio would truncate the expected page size,
// while we want it rounded -- so make sure it's rounded here.
FloatSize expected_page_size(
std::min<float>(document_rect.Width().ToFloat(),
std::min<float>(document_rect.Width().Round(),
page_size.Width() * maximum_shrink_factor),
std::min<float>(document_rect.Height().ToFloat(),
std::min<float>(document_rect.Height().Round(),
page_size.Height() * maximum_shrink_factor));
FloatSize max_page_size = frame_->ResizePageRectsKeepingRatio(
FloatSize(original_page_size.Width(), original_page_size.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