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