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 | #ifndef PDF_DOCUMENT_LAYOUT_H_ |
| 6 | #define PDF_DOCUMENT_LAYOUT_H_ |
| 7 | |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 8 | #include <cstddef> |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Hans Wennborg | 29adea9 | 2020-06-22 16:13:53 | [diff] [blame] | 11 | #include "base/check_op.h" |
K. Moon | e2dda8f2 | 2021-10-05 00:37:29 | [diff] [blame^] | 12 | #include "base/i18n/rtl.h" |
Lei Zhang | 4906c10 | 2019-08-06 00:28:03 | [diff] [blame] | 13 | #include "pdf/draw_utils/coordinates.h" |
K Moon | 9a62bf4 | 2019-08-07 20:05:36 | [diff] [blame] | 14 | #include "pdf/page_orientation.h" |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 15 | #include "ui/gfx/geometry/rect.h" |
Ankit Kumar 🌪️ | e3510115 | 2020-07-30 09:57:59 | [diff] [blame] | 16 | #include "ui/gfx/geometry/size.h" |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 17 | |
Gouarb Kundu | c4338a6 | 2020-07-30 21:30:48 | [diff] [blame] | 18 | namespace base { |
| 19 | class Value; |
| 20 | } |
| 21 | |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 22 | namespace chrome_pdf { |
| 23 | |
| 24 | // Layout of pages within a PDF document. Pages are placed as rectangles |
| 25 | // (possibly rotated) in a non-overlapping vertical sequence. |
| 26 | // |
| 27 | // All layout units are pixels. |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 28 | // |
Daniel Hosseinian | e257d96 | 2021-04-23 21:18:35 | [diff] [blame] | 29 | // The `Options` class controls the behavior of the layout, such as the default |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 30 | // orientation of pages. |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 31 | class DocumentLayout final { |
| 32 | public: |
Daniel Hosseinian | be88206 | 2021-04-22 01:22:39 | [diff] [blame] | 33 | // TODO(crbug.com/1144505): Add `kTwoUpEven` page spread support. |
| 34 | enum class PageSpread { |
| 35 | kOneUp = 0, // One page per spread. |
| 36 | kTwoUpOdd = 1, // Two pages per spread, with odd pages first. |
| 37 | }; |
| 38 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 39 | // Options controlling layout behavior. |
| 40 | class Options final { |
| 41 | public: |
| 42 | Options(); |
| 43 | |
| 44 | Options(const Options& other); |
| 45 | Options& operator=(const Options& other); |
| 46 | |
| 47 | ~Options(); |
| 48 | |
Hui Yingst | 28f9f5c | 2020-01-16 19:52:56 | [diff] [blame] | 49 | friend bool operator==(const Options& lhs, const Options& rhs) { |
K. Moon | e2dda8f2 | 2021-10-05 00:37:29 | [diff] [blame^] | 50 | return lhs.direction() == rhs.direction() && |
| 51 | lhs.default_page_orientation() == rhs.default_page_orientation() && |
| 52 | lhs.page_spread() == rhs.page_spread(); |
Hui Yingst | 28f9f5c | 2020-01-16 19:52:56 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | friend bool operator!=(const Options& lhs, const Options& rhs) { |
| 56 | return !(lhs == rhs); |
| 57 | } |
| 58 | |
Gouarb Kundu | c4338a6 | 2020-07-30 21:30:48 | [diff] [blame] | 59 | // Serializes layout options to a base::Value. |
| 60 | base::Value ToValue() const; |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 61 | |
Gouarb Kundu | 18d8093 | 2020-09-23 04:29:16 | [diff] [blame] | 62 | // Deserializes layout options from a base::Value. |
| 63 | void FromValue(const base::Value& value); |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 64 | |
K. Moon | e2dda8f2 | 2021-10-05 00:37:29 | [diff] [blame^] | 65 | // Page layout direction. This is tied to the direction of the user's UI, |
| 66 | // rather than the direction of individual pages. |
| 67 | base::i18n::TextDirection direction() const { return direction_; } |
| 68 | |
| 69 | void set_direction(base::i18n::TextDirection direction) { |
| 70 | direction_ = direction; |
| 71 | } |
| 72 | |
K Moon | 9a62bf4 | 2019-08-07 20:05:36 | [diff] [blame] | 73 | PageOrientation default_page_orientation() const { |
| 74 | return default_page_orientation_; |
| 75 | } |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 76 | |
| 77 | // Rotates default page orientation 90 degrees clockwise. |
| 78 | void RotatePagesClockwise(); |
| 79 | |
| 80 | // Rotates default page orientation 90 degrees counterclockwise. |
| 81 | void RotatePagesCounterclockwise(); |
| 82 | |
Daniel Hosseinian | be88206 | 2021-04-22 01:22:39 | [diff] [blame] | 83 | PageSpread page_spread() const { return page_spread_; } |
Hui Yingst | 28f9f5c | 2020-01-16 19:52:56 | [diff] [blame] | 84 | |
| 85 | // Changes two-up view status. |
Daniel Hosseinian | be88206 | 2021-04-22 01:22:39 | [diff] [blame] | 86 | void set_page_spread(PageSpread spread) { page_spread_ = spread; } |
Hui Yingst | 28f9f5c | 2020-01-16 19:52:56 | [diff] [blame] | 87 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 88 | private: |
K. Moon | e2dda8f2 | 2021-10-05 00:37:29 | [diff] [blame^] | 89 | base::i18n::TextDirection direction_ = base::i18n::UNKNOWN_DIRECTION; |
K Moon | 9a62bf4 | 2019-08-07 20:05:36 | [diff] [blame] | 90 | PageOrientation default_page_orientation_ = PageOrientation::kOriginal; |
Daniel Hosseinian | be88206 | 2021-04-22 01:22:39 | [diff] [blame] | 91 | PageSpread page_spread_ = PageSpread::kOneUp; |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 92 | }; |
| 93 | |
Lei Zhang | 4906c10 | 2019-08-06 00:28:03 | [diff] [blame] | 94 | static const draw_utils::PageInsetSizes kSingleViewInsets; |
| 95 | static constexpr int32_t kBottomSeparator = 4; |
| 96 | static constexpr int32_t kHorizontalSeparator = 1; |
| 97 | |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 98 | DocumentLayout(); |
| 99 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 100 | DocumentLayout(const DocumentLayout& other) = delete; |
| 101 | DocumentLayout& operator=(const DocumentLayout& other) = delete; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 102 | |
| 103 | ~DocumentLayout(); |
| 104 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 105 | // Returns the layout options. |
| 106 | const Options& options() const { return options_; } |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 107 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 108 | // Sets the layout options. If certain options with immediate effect change |
| 109 | // (such as the default page orientation), the layout will be marked dirty. |
| 110 | // |
| 111 | // TODO(kmoon): We shouldn't have layout options that take effect immediately. |
| 112 | void SetOptions(const Options& options); |
| 113 | |
| 114 | // Returns true if the layout has been modified since the last call to |
| 115 | // clear_dirty(). The initial state is false (clean), which assumes |
| 116 | // appropriate default behavior for an initially empty layout. |
| 117 | bool dirty() const { return dirty_; } |
| 118 | |
| 119 | // Clears the dirty() state of the layout. This should be called after any |
| 120 | // layout changes have been applied. |
| 121 | void clear_dirty() { dirty_ = false; } |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 122 | |
| 123 | // Returns the layout's total size. |
Ankit Kumar 🌪️ | e3510115 | 2020-07-30 09:57:59 | [diff] [blame] | 124 | const gfx::Size& size() const { return size_; } |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 125 | |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 126 | size_t page_count() const { return page_layouts_.size(); } |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 127 | |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 128 | // Gets the layout rectangle for a page. Only valid after computing a layout. |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 129 | const gfx::Rect& page_rect(size_t page_index) const { |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 130 | DCHECK_LT(page_index, page_count()); |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 131 | return page_layouts_[page_index].outer_rect; |
| 132 | } |
| 133 | |
| 134 | // Gets the layout rectangle for a page's bounds (which excludes additional |
| 135 | // regions like page shadows). Only valid after computing a layout. |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 136 | const gfx::Rect& page_bounds_rect(size_t page_index) const { |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 137 | DCHECK_LT(page_index, page_count()); |
| 138 | return page_layouts_[page_index].inner_rect; |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 139 | } |
| 140 | |
Daniel Hosseinian | e257d96 | 2021-04-23 21:18:35 | [diff] [blame] | 141 | // Computes the layout for a given list of `page_sizes` based on `options_`. |
Daniel Hosseinian | bcbedda | 2021-04-23 19:09:51 | [diff] [blame] | 142 | void ComputeLayout(const std::vector<gfx::Size>& page_sizes); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 143 | |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 144 | private: |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 145 | // Layout of a single page. |
| 146 | struct PageLayout { |
| 147 | // Bounding rectangle for the page with decorations. |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 148 | gfx::Rect outer_rect; |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 149 | |
| 150 | // Bounding rectangle for the page without decorations. |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 151 | gfx::Rect inner_rect; |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 152 | }; |
| 153 | |
Daniel Hosseinian | bcbedda | 2021-04-23 19:09:51 | [diff] [blame] | 154 | // Helpers for ComputeLayout() handling different page spreads. |
| 155 | void ComputeOneUpLayout(const std::vector<gfx::Size>& page_sizes); |
| 156 | void ComputeTwoUpOddLayout(const std::vector<gfx::Size>& page_sizes); |
| 157 | |
Daniel Hosseinian | e257d96 | 2021-04-23 21:18:35 | [diff] [blame] | 158 | // Copies `source_rect` to `destination_rect`, setting `dirty_` to true if |
| 159 | // `destination_rect` is modified as a result. |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 160 | void CopyRectIfModified(const gfx::Rect& source_rect, |
| 161 | gfx::Rect& destination_rect); |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 162 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 163 | Options options_; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 164 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 165 | // Indicates if the layout has changed in an externally-observable way, |
Daniel Hosseinian | e257d96 | 2021-04-23 21:18:35 | [diff] [blame] | 166 | // usually as a result of calling `ComputeLayout()` with different inputs. |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 167 | // |
| 168 | // Some operations that may trigger layout changes: |
| 169 | // * Changing page sizes |
| 170 | // * Adding or removing pages |
| 171 | // * Changing page orientations |
| 172 | bool dirty_ = false; |
| 173 | |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 174 | // Layout's total size. |
Ankit Kumar 🌪️ | e3510115 | 2020-07-30 09:57:59 | [diff] [blame] | 175 | gfx::Size size_; |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 176 | |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 177 | std::vector<PageLayout> page_layouts_; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | } // namespace chrome_pdf |
| 181 | |
| 182 | #endif // PDF_DOCUMENT_LAYOUT_H_ |