Skip to content

Commit

Permalink
Round expected page size in LocalFrameView::ForceLayoutForPagination
Browse files Browse the repository at this point in the history
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/+/1867126
Reviewed-by: Xianzhu Wang <wangxianzhu@chromium.org>
Commit-Queue: Andrey Kosyakov <caseq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709153}
  • Loading branch information
caseq authored and Commit Bot committed Oct 24, 2019
1 parent da9f549 commit 950533a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
46 changes: 46 additions & 0 deletions headless/lib/headless_web_contents_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions headless/test/data/red_square.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!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>
6 changes: 4 additions & 2 deletions third_party/blink/renderer/core/frame/local_frame_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down

0 comments on commit 950533a

Please sign in to comment.