blob: 79f65d44d5441b63907b06a184aeb2c3eb2190d7 [file] [log] [blame]
K Moonbd80ce72019-07-26 19:27:501// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "pdf/document_layout.h"
6
7#include "base/logging.h"
K Moonbd80ce72019-07-26 19:27:508
9namespace chrome_pdf {
10
Jeremy Chinsene819dea2019-08-07 21:58:5911namespace {
12
13int GetWidestPageWidth(const std::vector<pp::Size>& page_sizes) {
14 int widest_page_width = 0;
15 for (const auto& page_size : page_sizes) {
16 widest_page_width = std::max(widest_page_width, page_size.width());
17 }
18
19 return widest_page_width;
20}
21
22} // namespace
23
Lei Zhang4906c102019-08-06 00:28:0324const draw_utils::PageInsetSizes DocumentLayout::kSingleViewInsets{
25 /*left=*/5, /*top=*/3, /*right=*/5, /*bottom=*/7};
26
K Mooneb9e0002019-08-06 19:25:3227DocumentLayout::Options::Options() = default;
K Moonbd80ce72019-07-26 19:27:5028
K Mooneb9e0002019-08-06 19:25:3229DocumentLayout::Options::Options(const Options& other) = default;
30DocumentLayout::Options& DocumentLayout::Options::operator=(
31 const Options& other) = default;
K Moonbd80ce72019-07-26 19:27:5032
K Mooneb9e0002019-08-06 19:25:3233DocumentLayout::Options::~Options() = default;
K Moonbd80ce72019-07-26 19:27:5034
K Mooneb9e0002019-08-06 19:25:3235void DocumentLayout::Options::RotatePagesClockwise() {
K Moon9a62bf42019-08-07 20:05:3636 default_page_orientation_ = RotateClockwise(default_page_orientation_);
K Moonbd80ce72019-07-26 19:27:5037}
38
K Mooneb9e0002019-08-06 19:25:3239void DocumentLayout::Options::RotatePagesCounterclockwise() {
K Moon9a62bf42019-08-07 20:05:3640 default_page_orientation_ = RotateCounterclockwise(default_page_orientation_);
K Moonbd80ce72019-07-26 19:27:5041}
42
K Mooneb9e0002019-08-06 19:25:3243DocumentLayout::DocumentLayout() = default;
44
45DocumentLayout::~DocumentLayout() = default;
46
Jeremy Chinsen08beb482019-08-07 01:58:5447std::vector<pp::Rect> DocumentLayout::GetSingleViewLayout(
48 const std::vector<pp::Size>& page_sizes) {
Jeremy Chinsene819dea2019-08-07 21:58:5949 set_size({GetWidestPageWidth(page_sizes), 0});
Jeremy Chinsen08beb482019-08-07 01:58:5450
Jeremy Chinsene819dea2019-08-07 21:58:5951 std::vector<pp::Rect> formatted_rects(page_sizes.size());
Jeremy Chinsen08beb482019-08-07 01:58:5452 for (size_t i = 0; i < page_sizes.size(); ++i) {
53 if (i != 0) {
54 // Add space for bottom separator.
55 EnlargeHeight(kBottomSeparator);
56 }
57
58 const pp::Size& page_size = page_sizes[i];
59 formatted_rects[i] =
60 draw_utils::GetRectForSingleView(page_size, size_, kSingleViewInsets);
61 AppendPageRect(page_size);
62 }
63
64 return formatted_rects;
65}
66
Jeremy Chinsend6fd27ce2019-08-06 00:40:1767std::vector<pp::Rect> DocumentLayout::GetTwoUpViewLayout(
Jeremy Chinsen4a65aad2019-08-07 00:14:3368 const std::vector<pp::Size>& page_sizes) {
Jeremy Chinsene819dea2019-08-07 21:58:5969 set_size({GetWidestPageWidth(page_sizes), 0});
Jeremy Chinsend6fd27ce2019-08-06 00:40:1770
Jeremy Chinsen4a65aad2019-08-07 00:14:3371 std::vector<pp::Rect> formatted_rects(page_sizes.size());
Jeremy Chinsen4a65aad2019-08-07 00:14:3372 for (size_t i = 0; i < page_sizes.size(); ++i) {
Jeremy Chinsend6fd27ce2019-08-06 00:40:1773 draw_utils::PageInsetSizes page_insets =
74 draw_utils::GetPageInsetsForTwoUpView(
Jeremy Chinsen4a65aad2019-08-07 00:14:3375 i, page_sizes.size(), kSingleViewInsets, kHorizontalSeparator);
76 const pp::Size& page_size = page_sizes[i];
Jeremy Chinsend6fd27ce2019-08-06 00:40:1777
78 if (i % 2 == 0) {
79 formatted_rects[i] = draw_utils::GetLeftRectForTwoUpView(
80 page_size, {size_.width(), size_.height()}, page_insets);
81 } else {
82 formatted_rects[i] = draw_utils::GetRightRectForTwoUpView(
83 page_size, {size_.width(), size_.height()}, page_insets);
Jeremy Chinsen4a65aad2019-08-07 00:14:3384 EnlargeHeight(std::max(page_size.height(), page_sizes[i - 1].height()));
Jeremy Chinsend6fd27ce2019-08-06 00:40:1785 }
86 }
87
Jeremy Chinsen4a65aad2019-08-07 00:14:3388 if (page_sizes.size() % 2 == 1) {
89 EnlargeHeight(page_sizes.back().height());
Jeremy Chinsend6fd27ce2019-08-06 00:40:1790 }
91
92 return formatted_rects;
93}
94
K Moonbd80ce72019-07-26 19:27:5095void DocumentLayout::EnlargeHeight(int height) {
96 DCHECK_GE(height, 0);
97 size_.Enlarge(0, height);
98}
99
100void DocumentLayout::AppendPageRect(const pp::Size& page_rect) {
101 // TODO(kmoon): Inline draw_utils::ExpandDocumentSize().
102 draw_utils::ExpandDocumentSize(page_rect, &size_);
103}
104
105} // namespace chrome_pdf