blob: ec57a3f0bf0f557415c10058243d910294dde3b7 [file] [log] [blame]
K Moonbd80ce72019-07-26 19:27:501// 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 Moonff7ec672019-08-14 19:19:568#include <cstddef>
Jeremy Chinsend6fd27ce2019-08-06 00:40:179#include <vector>
10
Hans Wennborg29adea92020-06-22 16:13:5311#include "base/check_op.h"
K. Moone2dda8f22021-10-05 00:37:2912#include "base/i18n/rtl.h"
Lei Zhang4906c102019-08-06 00:28:0313#include "pdf/draw_utils/coordinates.h"
K Moon9a62bf42019-08-07 20:05:3614#include "pdf/page_orientation.h"
Ankit Kumar 🌪️00798692020-08-26 22:57:1415#include "ui/gfx/geometry/rect.h"
Ankit Kumar 🌪️e35101152020-07-30 09:57:5916#include "ui/gfx/geometry/size.h"
K Moonbd80ce72019-07-26 19:27:5017
Gouarb Kunduc4338a62020-07-30 21:30:4818namespace base {
19class Value;
20}
21
K Moonbd80ce72019-07-26 19:27:5022namespace 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 Mooneb9e0002019-08-06 19:25:3228//
Daniel Hosseiniane257d962021-04-23 21:18:3529// The `Options` class controls the behavior of the layout, such as the default
K Mooneb9e0002019-08-06 19:25:3230// orientation of pages.
K Moonbd80ce72019-07-26 19:27:5031class DocumentLayout final {
32 public:
Daniel Hosseinianbe882062021-04-22 01:22:3933 // 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 Mooneb9e0002019-08-06 19:25:3239 // 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 Yingst28f9f5c2020-01-16 19:52:5649 friend bool operator==(const Options& lhs, const Options& rhs) {
K. Moone2dda8f22021-10-05 00:37:2950 return lhs.direction() == rhs.direction() &&
51 lhs.default_page_orientation() == rhs.default_page_orientation() &&
52 lhs.page_spread() == rhs.page_spread();
Hui Yingst28f9f5c2020-01-16 19:52:5653 }
54
55 friend bool operator!=(const Options& lhs, const Options& rhs) {
56 return !(lhs == rhs);
57 }
58
Gouarb Kunduc4338a62020-07-30 21:30:4859 // Serializes layout options to a base::Value.
60 base::Value ToValue() const;
K Moon23d56442019-10-03 05:06:2361
Gouarb Kundu18d80932020-09-23 04:29:1662 // Deserializes layout options from a base::Value.
63 void FromValue(const base::Value& value);
K Moon23d56442019-10-03 05:06:2364
K. Moone2dda8f22021-10-05 00:37:2965 // 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 Moon9a62bf42019-08-07 20:05:3673 PageOrientation default_page_orientation() const {
74 return default_page_orientation_;
75 }
K Mooneb9e0002019-08-06 19:25:3276
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 Hosseinianbe882062021-04-22 01:22:3983 PageSpread page_spread() const { return page_spread_; }
Hui Yingst28f9f5c2020-01-16 19:52:5684
85 // Changes two-up view status.
Daniel Hosseinianbe882062021-04-22 01:22:3986 void set_page_spread(PageSpread spread) { page_spread_ = spread; }
Hui Yingst28f9f5c2020-01-16 19:52:5687
K Mooneb9e0002019-08-06 19:25:3288 private:
K. Moone2dda8f22021-10-05 00:37:2989 base::i18n::TextDirection direction_ = base::i18n::UNKNOWN_DIRECTION;
K Moon9a62bf42019-08-07 20:05:3690 PageOrientation default_page_orientation_ = PageOrientation::kOriginal;
Daniel Hosseinianbe882062021-04-22 01:22:3991 PageSpread page_spread_ = PageSpread::kOneUp;
K Mooneb9e0002019-08-06 19:25:3292 };
93
Lei Zhang4906c102019-08-06 00:28:0394 static const draw_utils::PageInsetSizes kSingleViewInsets;
95 static constexpr int32_t kBottomSeparator = 4;
96 static constexpr int32_t kHorizontalSeparator = 1;
97
K Moonbd80ce72019-07-26 19:27:5098 DocumentLayout();
99
K Mooneb9e0002019-08-06 19:25:32100 DocumentLayout(const DocumentLayout& other) = delete;
101 DocumentLayout& operator=(const DocumentLayout& other) = delete;
K Moonbd80ce72019-07-26 19:27:50102
103 ~DocumentLayout();
104
K Mooneb9e0002019-08-06 19:25:32105 // Returns the layout options.
106 const Options& options() const { return options_; }
K Moonbd80ce72019-07-26 19:27:50107
K Moon6d326b92019-09-19 22:42:07108 // 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 Moonbd80ce72019-07-26 19:27:50122
123 // Returns the layout's total size.
Ankit Kumar 🌪️e35101152020-07-30 09:57:59124 const gfx::Size& size() const { return size_; }
K Moonbd80ce72019-07-26 19:27:50125
K Moone4bd7522019-08-23 00:12:56126 size_t page_count() const { return page_layouts_.size(); }
Jeremy Chinsen08beb482019-08-07 01:58:54127
K Moonff7ec672019-08-14 19:19:56128 // Gets the layout rectangle for a page. Only valid after computing a layout.
Ankit Kumar 🌪️00798692020-08-26 22:57:14129 const gfx::Rect& page_rect(size_t page_index) const {
K Moonff7ec672019-08-14 19:19:56130 DCHECK_LT(page_index, page_count());
K Moone4bd7522019-08-23 00:12:56131 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 🌪️00798692020-08-26 22:57:14136 const gfx::Rect& page_bounds_rect(size_t page_index) const {
K Moone4bd7522019-08-23 00:12:56137 DCHECK_LT(page_index, page_count());
138 return page_layouts_[page_index].inner_rect;
K Moonff7ec672019-08-14 19:19:56139 }
140
Daniel Hosseiniane257d962021-04-23 21:18:35141 // Computes the layout for a given list of `page_sizes` based on `options_`.
Daniel Hosseinianbcbedda2021-04-23 19:09:51142 void ComputeLayout(const std::vector<gfx::Size>& page_sizes);
Jeremy Chinsend6fd27ce2019-08-06 00:40:17143
K Moonbd80ce72019-07-26 19:27:50144 private:
K Moone4bd7522019-08-23 00:12:56145 // Layout of a single page.
146 struct PageLayout {
147 // Bounding rectangle for the page with decorations.
Ankit Kumar 🌪️00798692020-08-26 22:57:14148 gfx::Rect outer_rect;
K Moone4bd7522019-08-23 00:12:56149
150 // Bounding rectangle for the page without decorations.
Ankit Kumar 🌪️00798692020-08-26 22:57:14151 gfx::Rect inner_rect;
K Moone4bd7522019-08-23 00:12:56152 };
153
Daniel Hosseinianbcbedda2021-04-23 19:09:51154 // 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 Hosseiniane257d962021-04-23 21:18:35158 // Copies `source_rect` to `destination_rect`, setting `dirty_` to true if
159 // `destination_rect` is modified as a result.
Ankit Kumar 🌪️00798692020-08-26 22:57:14160 void CopyRectIfModified(const gfx::Rect& source_rect,
161 gfx::Rect& destination_rect);
K Moon6d326b92019-09-19 22:42:07162
K Mooneb9e0002019-08-06 19:25:32163 Options options_;
K Moonbd80ce72019-07-26 19:27:50164
K Moon6d326b92019-09-19 22:42:07165 // Indicates if the layout has changed in an externally-observable way,
Daniel Hosseiniane257d962021-04-23 21:18:35166 // usually as a result of calling `ComputeLayout()` with different inputs.
K Moon6d326b92019-09-19 22:42:07167 //
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 Moonbd80ce72019-07-26 19:27:50174 // Layout's total size.
Ankit Kumar 🌪️e35101152020-07-30 09:57:59175 gfx::Size size_;
K Moonff7ec672019-08-14 19:19:56176
K Moone4bd7522019-08-23 00:12:56177 std::vector<PageLayout> page_layouts_;
K Moonbd80ce72019-07-26 19:27:50178};
179
180} // namespace chrome_pdf
181
182#endif // PDF_DOCUMENT_LAYOUT_H_