K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 1 | // 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 | |
Jeremy Apthorp | 17c873c | 2019-12-02 20:27:39 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 9 | #include "base/logging.h" |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 10 | #include "ppapi/cpp/rect.h" |
| 11 | #include "ppapi/cpp/size.h" |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 12 | #include "ppapi/cpp/var.h" |
| 13 | #include "ppapi/cpp/var_dictionary.h" |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 14 | |
| 15 | namespace chrome_pdf { |
| 16 | |
Jeremy Chinsen | e819dea | 2019-08-07 21:58:59 | [diff] [blame] | 17 | namespace { |
| 18 | |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 19 | constexpr char kDefaultPageOrientation[] = "defaultPageOrientation"; |
| 20 | |
Jeremy Chinsen | e819dea | 2019-08-07 21:58:59 | [diff] [blame] | 21 | int GetWidestPageWidth(const std::vector<pp::Size>& page_sizes) { |
| 22 | int widest_page_width = 0; |
| 23 | for (const auto& page_size : page_sizes) { |
| 24 | widest_page_width = std::max(widest_page_width, page_size.width()); |
| 25 | } |
| 26 | |
| 27 | return widest_page_width; |
| 28 | } |
| 29 | |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 30 | pp::Rect InsetRect(pp::Rect rect, |
| 31 | const draw_utils::PageInsetSizes& inset_sizes) { |
| 32 | rect.Inset(inset_sizes.left, inset_sizes.top, inset_sizes.right, |
| 33 | inset_sizes.bottom); |
| 34 | return rect; |
| 35 | } |
| 36 | |
Jeremy Chinsen | e819dea | 2019-08-07 21:58:59 | [diff] [blame] | 37 | } // namespace |
| 38 | |
Lei Zhang | 4906c10 | 2019-08-06 00:28:03 | [diff] [blame] | 39 | const draw_utils::PageInsetSizes DocumentLayout::kSingleViewInsets{ |
| 40 | /*left=*/5, /*top=*/3, /*right=*/5, /*bottom=*/7}; |
| 41 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 42 | DocumentLayout::Options::Options() = default; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 43 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 44 | DocumentLayout::Options::Options(const Options& other) = default; |
| 45 | DocumentLayout::Options& DocumentLayout::Options::operator=( |
| 46 | const Options& other) = default; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 47 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 48 | DocumentLayout::Options::~Options() = default; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 49 | |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 50 | pp::Var DocumentLayout::Options::ToVar() const { |
| 51 | pp::VarDictionary dictionary; |
| 52 | dictionary.Set(kDefaultPageOrientation, |
| 53 | static_cast<int32_t>(default_page_orientation_)); |
| 54 | return dictionary; |
| 55 | } |
| 56 | |
| 57 | void DocumentLayout::Options::FromVar(const pp::Var& var) { |
| 58 | pp::VarDictionary dictionary(var); |
| 59 | |
| 60 | int32_t default_page_orientation = |
| 61 | dictionary.Get(kDefaultPageOrientation).AsInt(); |
| 62 | DCHECK_GE(default_page_orientation, |
| 63 | static_cast<int32_t>(PageOrientation::kOriginal)); |
| 64 | DCHECK_LE(default_page_orientation, |
| 65 | static_cast<int32_t>(PageOrientation::kLast)); |
| 66 | default_page_orientation_ = |
| 67 | static_cast<PageOrientation>(default_page_orientation); |
| 68 | } |
| 69 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 70 | void DocumentLayout::Options::RotatePagesClockwise() { |
K Moon | 9a62bf4 | 2019-08-07 20:05:36 | [diff] [blame] | 71 | default_page_orientation_ = RotateClockwise(default_page_orientation_); |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 72 | } |
| 73 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 74 | void DocumentLayout::Options::RotatePagesCounterclockwise() { |
K Moon | 9a62bf4 | 2019-08-07 20:05:36 | [diff] [blame] | 75 | default_page_orientation_ = RotateCounterclockwise(default_page_orientation_); |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 76 | } |
| 77 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 78 | DocumentLayout::DocumentLayout() = default; |
| 79 | |
| 80 | DocumentLayout::~DocumentLayout() = default; |
| 81 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 82 | void DocumentLayout::SetOptions(const Options& options) { |
| 83 | // TODO(kmoon): This is pessimistic, but we still want to consider the layout |
| 84 | // dirty for orientation changes, even if the page rects don't change. |
| 85 | // |
| 86 | // We also probably don't want orientation changes to actually kick in until |
| 87 | // the next call to ComputeLayout(). (In practice, we'll call ComputeLayout() |
| 88 | // shortly after calling SetOptions().) |
| 89 | if (options_.default_page_orientation() != |
| 90 | options.default_page_orientation()) { |
| 91 | dirty_ = true; |
| 92 | } |
| 93 | options_ = options; |
| 94 | } |
| 95 | |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 96 | void DocumentLayout::ComputeSingleViewLayout( |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 97 | const std::vector<pp::Size>& page_sizes) { |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 98 | pp::Size document_size(GetWidestPageWidth(page_sizes), 0); |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 99 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 100 | if (page_layouts_.size() != page_sizes.size()) { |
| 101 | // TODO(kmoon): May want to do less work when shrinking a layout. |
| 102 | page_layouts_.resize(page_sizes.size()); |
| 103 | dirty_ = true; |
| 104 | } |
| 105 | |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 106 | for (size_t i = 0; i < page_sizes.size(); ++i) { |
| 107 | if (i != 0) { |
| 108 | // Add space for bottom separator. |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 109 | document_size.Enlarge(0, kBottomSeparator); |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | const pp::Size& page_size = page_sizes[i]; |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 113 | pp::Rect page_rect = |
| 114 | draw_utils::GetRectForSingleView(page_size, document_size); |
| 115 | CopyRectIfModified(page_rect, &page_layouts_[i].outer_rect); |
| 116 | CopyRectIfModified(InsetRect(page_rect, kSingleViewInsets), |
| 117 | &page_layouts_[i].inner_rect); |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 118 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 119 | draw_utils::ExpandDocumentSize(page_size, &document_size); |
| 120 | } |
| 121 | |
| 122 | if (size_ != document_size) { |
| 123 | size_ = document_size; |
| 124 | dirty_ = true; |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 125 | } |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 126 | } |
| 127 | |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 128 | void DocumentLayout::ComputeTwoUpViewLayout( |
Jeremy Chinsen | 4a65aad | 2019-08-07 00:14:33 | [diff] [blame] | 129 | const std::vector<pp::Size>& page_sizes) { |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 130 | pp::Size document_size(GetWidestPageWidth(page_sizes), 0); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 131 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 132 | if (page_layouts_.size() != page_sizes.size()) { |
| 133 | // TODO(kmoon): May want to do less work when shrinking a layout. |
| 134 | page_layouts_.resize(page_sizes.size()); |
| 135 | dirty_ = true; |
| 136 | } |
| 137 | |
Jeremy Chinsen | 4a65aad | 2019-08-07 00:14:33 | [diff] [blame] | 138 | for (size_t i = 0; i < page_sizes.size(); ++i) { |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 139 | draw_utils::PageInsetSizes page_insets = |
| 140 | draw_utils::GetPageInsetsForTwoUpView( |
Jeremy Chinsen | 4a65aad | 2019-08-07 00:14:33 | [diff] [blame] | 141 | i, page_sizes.size(), kSingleViewInsets, kHorizontalSeparator); |
| 142 | const pp::Size& page_size = page_sizes[i]; |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 143 | |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 144 | pp::Rect page_rect; |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 145 | if (i % 2 == 0) { |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 146 | page_rect = draw_utils::GetLeftRectForTwoUpView( |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 147 | page_size, {document_size.width(), document_size.height()}); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 148 | } else { |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 149 | page_rect = draw_utils::GetRightRectForTwoUpView( |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 150 | page_size, {document_size.width(), document_size.height()}); |
| 151 | document_size.Enlarge( |
| 152 | 0, std::max(page_size.height(), page_sizes[i - 1].height())); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 153 | } |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 154 | CopyRectIfModified(page_rect, &page_layouts_[i].outer_rect); |
| 155 | CopyRectIfModified(InsetRect(page_rect, page_insets), |
| 156 | &page_layouts_[i].inner_rect); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 157 | } |
| 158 | |
Jeremy Chinsen | 4a65aad | 2019-08-07 00:14:33 | [diff] [blame] | 159 | if (page_sizes.size() % 2 == 1) { |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 160 | document_size.Enlarge(0, page_sizes.back().height()); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 161 | } |
| 162 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 163 | document_size.set_width(2 * document_size.width()); |
| 164 | |
| 165 | if (size_ != document_size) { |
| 166 | size_ = document_size; |
| 167 | dirty_ = true; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void DocumentLayout::CopyRectIfModified(const pp::Rect& source_rect, |
| 172 | pp::Rect* destination_rect) { |
| 173 | if (*destination_rect != source_rect) { |
| 174 | *destination_rect = source_rect; |
| 175 | dirty_ = true; |
| 176 | } |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 177 | } |
| 178 | |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 179 | } // namespace chrome_pdf |