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