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 | |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 11 | #include "base/logging.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" |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 14 | #include "ppapi/cpp/rect.h" |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 15 | #include "ppapi/cpp/size.h" |
| 16 | |
| 17 | namespace chrome_pdf { |
| 18 | |
| 19 | // Layout of pages within a PDF document. Pages are placed as rectangles |
| 20 | // (possibly rotated) in a non-overlapping vertical sequence. |
| 21 | // |
| 22 | // All layout units are pixels. |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 23 | // |
| 24 | // The |Options| class controls the behavior of the layout, such as the default |
| 25 | // orientation of pages. |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 26 | class DocumentLayout final { |
| 27 | public: |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 28 | // Options controlling layout behavior. |
| 29 | class Options final { |
| 30 | public: |
| 31 | Options(); |
| 32 | |
| 33 | Options(const Options& other); |
| 34 | Options& operator=(const Options& other); |
| 35 | |
| 36 | ~Options(); |
| 37 | |
K Moon | 9a62bf4 | 2019-08-07 20:05:36 | [diff] [blame] | 38 | PageOrientation default_page_orientation() const { |
| 39 | return default_page_orientation_; |
| 40 | } |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 41 | |
| 42 | // Rotates default page orientation 90 degrees clockwise. |
| 43 | void RotatePagesClockwise(); |
| 44 | |
| 45 | // Rotates default page orientation 90 degrees counterclockwise. |
| 46 | void RotatePagesCounterclockwise(); |
| 47 | |
| 48 | private: |
K Moon | 9a62bf4 | 2019-08-07 20:05:36 | [diff] [blame] | 49 | PageOrientation default_page_orientation_ = PageOrientation::kOriginal; |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 50 | }; |
| 51 | |
Lei Zhang | 4906c10 | 2019-08-06 00:28:03 | [diff] [blame] | 52 | static const draw_utils::PageInsetSizes kSingleViewInsets; |
| 53 | static constexpr int32_t kBottomSeparator = 4; |
| 54 | static constexpr int32_t kHorizontalSeparator = 1; |
| 55 | |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 56 | DocumentLayout(); |
| 57 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 58 | DocumentLayout(const DocumentLayout& other) = delete; |
| 59 | DocumentLayout& operator=(const DocumentLayout& other) = delete; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 60 | |
| 61 | ~DocumentLayout(); |
| 62 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 63 | // Returns the layout options. |
| 64 | const Options& options() const { return options_; } |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 65 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 66 | // Sets the layout options. |
| 67 | void set_options(const Options& options) { options_ = options; } |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 68 | |
| 69 | // Returns the layout's total size. |
| 70 | const pp::Size& size() const { return size_; } |
| 71 | |
| 72 | // Sets the layout's total size. |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 73 | // |
| 74 | // TODO(kmoon): Get rid of this method. |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 75 | void set_size(const pp::Size& size) { size_ = size; } |
| 76 | |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 77 | size_t page_count() const { return page_layouts_.size(); } |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 78 | |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 79 | // Gets the layout rectangle for a page. Only valid after computing a layout. |
| 80 | const pp::Rect& page_rect(size_t page_index) const { |
| 81 | DCHECK_LT(page_index, page_count()); |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 82 | return page_layouts_[page_index].outer_rect; |
| 83 | } |
| 84 | |
| 85 | // Gets the layout rectangle for a page's bounds (which excludes additional |
| 86 | // regions like page shadows). Only valid after computing a layout. |
| 87 | const pp::Rect& page_bounds_rect(size_t page_index) const { |
| 88 | DCHECK_LT(page_index, page_count()); |
| 89 | return page_layouts_[page_index].inner_rect; |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Computes layout that represent |page_sizes| formatted for single view. |
| 93 | // |
| 94 | // TODO(kmoon): Control layout type using an option. |
| 95 | void ComputeSingleViewLayout(const std::vector<pp::Size>& page_sizes); |
| 96 | |
| 97 | // Computes layout that represent |page_sizes| formatted for two-up view. |
| 98 | // |
| 99 | // TODO(kmoon): Control layout type using an option. |
| 100 | void ComputeTwoUpViewLayout(const std::vector<pp::Size>& page_sizes); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 101 | |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 102 | private: |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 103 | // Layout of a single page. |
| 104 | struct PageLayout { |
| 105 | // Bounding rectangle for the page with decorations. |
| 106 | pp::Rect outer_rect; |
| 107 | |
| 108 | // Bounding rectangle for the page without decorations. |
| 109 | pp::Rect inner_rect; |
| 110 | }; |
| 111 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 112 | Options options_; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 113 | |
| 114 | // Layout's total size. |
| 115 | pp::Size size_; |
K Moon | ff7ec67 | 2019-08-14 19:19:56 | [diff] [blame] | 116 | |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 117 | std::vector<PageLayout> page_layouts_; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // namespace chrome_pdf |
| 121 | |
| 122 | #endif // PDF_DOCUMENT_LAYOUT_H_ |