Commit 324b5ae9 authored by Jeremy Chinsen's avatar Jeremy Chinsen Committed by Commit Bot

Add draw_utils::CenterRectHorizontally().

In single-view, pages are horizontally centered within the document.
This centering is being done in PDFiumEngine, making it difficult to
test.

This CL moves the centering logic for single-view to
draw_utils::CenterRectHorizontally(). This allows us to test the logic.

Bug: 51472
Change-Id: Idd67cde3c57d4155c9e02ca82a36ad19a6205f08
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1730524
Commit-Queue: Jeremy Chinsen <chinsenj@google.com>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683387}
parent 78fd335e
......@@ -17,6 +17,12 @@ void AdjustBottomGapForRightSidePage(int page_x, pp::Rect* bottom_gap) {
bottom_gap->set_width(bottom_gap->width() / 2);
}
void CenterRectHorizontally(int doc_width, pp::Rect* rect) {
DCHECK_GE(doc_width, rect->width());
rect->set_x((doc_width - rect->width()) / 2);
}
void ExpandDocumentSize(const pp::Size& rect_size, pp::Size* doc_size) {
int width_diff = std::max(0, rect_size.width() - doc_size->width());
doc_size->Enlarge(width_diff, rect_size.height());
......
......@@ -32,6 +32,10 @@ struct PageInsetSizes {
// drawn left page and the empty space to the right of the page.
void AdjustBottomGapForRightSidePage(int page_x, pp::Rect* bottom_gap);
// Given |doc_width|, horizontally center |rect| within the document.
// |doc_width| must be greater than or equal to |rect|.
void CenterRectHorizontally(int doc_width, pp::Rect* rect);
// Given |rect_size|, sets the width of |doc_size| to the max of |rect_size|'s
// width and |doc_size|'s width. Also adds the height of |rect_size| to
// |doc_size|'s height.
......
......@@ -54,6 +54,20 @@ TEST(CoordinateTest, AdjustBottomGapForRightSidePage) {
CompareRect({450, 40, 475, 200}, bottom_gap);
}
TEST(CoordinateTest, CenterRectHorizontally) {
pp::Rect page_rect(10, 20, 400, 300);
CenterRectHorizontally(600, &page_rect);
CompareRect({100, 20, 400, 300}, page_rect);
page_rect.SetRect(300, 450, 500, 700);
CenterRectHorizontally(800, &page_rect);
CompareRect({150, 450, 500, 700}, page_rect);
page_rect.SetRect(800, 100, 200, 250);
CenterRectHorizontally(350, &page_rect);
CompareRect({75, 100, 200, 250}, page_rect);
}
TEST(CoordinateTest, ExpandDocumentSize) {
pp::Size doc_size(100, 400);
......
......@@ -2395,8 +2395,7 @@ void PDFiumEngine::AppendPageRectToPages(const pp::Rect& page_rect,
void PDFiumEngine::LoadPagesInSingleView(std::vector<pp::Rect> page_rects,
bool reload) {
for (size_t i = 0; i < page_rects.size(); ++i) {
// Center pages relative to the entire document.
page_rects[i].set_x((layout_.size().width() - page_rects[i].width()) / 2);
draw_utils::CenterRectHorizontally(layout_.size().width(), &page_rects[i]);
InsetPage(i, page_rects.size(), /*multiplier=*/1, &page_rects[i]);
AppendPageRectToPages(page_rects[i], i, reload);
}
......
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