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