[go: nahoru, domu]

blob: c56cbc6bca7e888eb963f1a8e624aa904a00b770 [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
K Moonff7ec672019-08-14 19:19:5611#include "base/logging.h"
Lei Zhang4906c102019-08-06 00:28:0312#include "pdf/draw_utils/coordinates.h"
K Moon9a62bf42019-08-07 20:05:3613#include "pdf/page_orientation.h"
Jeremy Chinsend6fd27ce2019-08-06 00:40:1714#include "ppapi/cpp/rect.h"
K Moonbd80ce72019-07-26 19:27:5015#include "ppapi/cpp/size.h"
16
K Moon23d56442019-10-03 05:06:2317namespace pp {
18class Var;
19} // namespace pp
20
K Moonbd80ce72019-07-26 19:27:5021namespace chrome_pdf {
22
23// Layout of pages within a PDF document. Pages are placed as rectangles
24// (possibly rotated) in a non-overlapping vertical sequence.
25//
26// All layout units are pixels.
K Mooneb9e0002019-08-06 19:25:3227//
28// The |Options| class controls the behavior of the layout, such as the default
29// orientation of pages.
K Moonbd80ce72019-07-26 19:27:5030class DocumentLayout final {
31 public:
K Mooneb9e0002019-08-06 19:25:3232 // Options controlling layout behavior.
33 class Options final {
34 public:
35 Options();
36
37 Options(const Options& other);
38 Options& operator=(const Options& other);
39
40 ~Options();
41
Hui Yingst28f9f5c2020-01-16 19:52:5642 friend bool operator==(const Options& lhs, const Options& rhs) {
43 return lhs.two_up_view_enabled() == rhs.two_up_view_enabled() &&
44 lhs.default_page_orientation() == rhs.default_page_orientation();
45 }
46
47 friend bool operator!=(const Options& lhs, const Options& rhs) {
48 return !(lhs == rhs);
49 }
50
K Moon23d56442019-10-03 05:06:2351 // Serializes layout options to a pp::Var.
52 pp::Var ToVar() const;
53
54 // Deserializes layout options from a pp::Var.
55 void FromVar(const pp::Var& var);
56
K Moon9a62bf42019-08-07 20:05:3657 PageOrientation default_page_orientation() const {
58 return default_page_orientation_;
59 }
K Mooneb9e0002019-08-06 19:25:3260
61 // Rotates default page orientation 90 degrees clockwise.
62 void RotatePagesClockwise();
63
64 // Rotates default page orientation 90 degrees counterclockwise.
65 void RotatePagesCounterclockwise();
66
Hui Yingst28f9f5c2020-01-16 19:52:5667 bool two_up_view_enabled() const { return two_up_view_enabled_; }
68
69 // Changes two-up view status.
70 void set_two_up_view_enabled(bool enable) { two_up_view_enabled_ = enable; }
71
K Mooneb9e0002019-08-06 19:25:3272 private:
K Moon9a62bf42019-08-07 20:05:3673 PageOrientation default_page_orientation_ = PageOrientation::kOriginal;
Hui Yingst28f9f5c2020-01-16 19:52:5674 bool two_up_view_enabled_ = false;
K Mooneb9e0002019-08-06 19:25:3275 };
76
Lei Zhang4906c102019-08-06 00:28:0377 static const draw_utils::PageInsetSizes kSingleViewInsets;
78 static constexpr int32_t kBottomSeparator = 4;
79 static constexpr int32_t kHorizontalSeparator = 1;
80
K Moonbd80ce72019-07-26 19:27:5081 DocumentLayout();
82
K Mooneb9e0002019-08-06 19:25:3283 DocumentLayout(const DocumentLayout& other) = delete;
84 DocumentLayout& operator=(const DocumentLayout& other) = delete;
K Moonbd80ce72019-07-26 19:27:5085
86 ~DocumentLayout();
87
K Mooneb9e0002019-08-06 19:25:3288 // Returns the layout options.
89 const Options& options() const { return options_; }
K Moonbd80ce72019-07-26 19:27:5090
K Moon6d326b92019-09-19 22:42:0791 // Sets the layout options. If certain options with immediate effect change
92 // (such as the default page orientation), the layout will be marked dirty.
93 //
94 // TODO(kmoon): We shouldn't have layout options that take effect immediately.
95 void SetOptions(const Options& options);
96
97 // Returns true if the layout has been modified since the last call to
98 // clear_dirty(). The initial state is false (clean), which assumes
99 // appropriate default behavior for an initially empty layout.
100 bool dirty() const { return dirty_; }
101
102 // Clears the dirty() state of the layout. This should be called after any
103 // layout changes have been applied.
104 void clear_dirty() { dirty_ = false; }
K Moonbd80ce72019-07-26 19:27:50105
106 // Returns the layout's total size.
107 const pp::Size& size() const { return size_; }
108
K Moone4bd7522019-08-23 00:12:56109 size_t page_count() const { return page_layouts_.size(); }
Jeremy Chinsen08beb482019-08-07 01:58:54110
K Moonff7ec672019-08-14 19:19:56111 // Gets the layout rectangle for a page. Only valid after computing a layout.
112 const pp::Rect& page_rect(size_t page_index) const {
113 DCHECK_LT(page_index, page_count());
K Moone4bd7522019-08-23 00:12:56114 return page_layouts_[page_index].outer_rect;
115 }
116
117 // Gets the layout rectangle for a page's bounds (which excludes additional
118 // regions like page shadows). Only valid after computing a layout.
119 const pp::Rect& page_bounds_rect(size_t page_index) const {
120 DCHECK_LT(page_index, page_count());
121 return page_layouts_[page_index].inner_rect;
K Moonff7ec672019-08-14 19:19:56122 }
123
124 // Computes layout that represent |page_sizes| formatted for single view.
125 //
126 // TODO(kmoon): Control layout type using an option.
127 void ComputeSingleViewLayout(const std::vector<pp::Size>& page_sizes);
128
129 // Computes layout that represent |page_sizes| formatted for two-up view.
130 //
131 // TODO(kmoon): Control layout type using an option.
132 void ComputeTwoUpViewLayout(const std::vector<pp::Size>& page_sizes);
Jeremy Chinsend6fd27ce2019-08-06 00:40:17133
K Moonbd80ce72019-07-26 19:27:50134 private:
K Moone4bd7522019-08-23 00:12:56135 // Layout of a single page.
136 struct PageLayout {
137 // Bounding rectangle for the page with decorations.
138 pp::Rect outer_rect;
139
140 // Bounding rectangle for the page without decorations.
141 pp::Rect inner_rect;
142 };
143
K Moon6d326b92019-09-19 22:42:07144 // Copies |source_rect| to |destination_rect|, setting |dirty_| to true if
145 // |destination_rect| is modified as a result.
146 void CopyRectIfModified(const pp::Rect& source_rect,
147 pp::Rect* destination_rect);
148
K Mooneb9e0002019-08-06 19:25:32149 Options options_;
K Moonbd80ce72019-07-26 19:27:50150
K Moon6d326b92019-09-19 22:42:07151 // Indicates if the layout has changed in an externally-observable way,
152 // usually as a result of calling |ComputeLayout()| with different inputs.
153 //
154 // Some operations that may trigger layout changes:
155 // * Changing page sizes
156 // * Adding or removing pages
157 // * Changing page orientations
158 bool dirty_ = false;
159
K Moonbd80ce72019-07-26 19:27:50160 // Layout's total size.
161 pp::Size size_;
K Moonff7ec672019-08-14 19:19:56162
K Moone4bd7522019-08-23 00:12:56163 std::vector<PageLayout> page_layouts_;
K Moonbd80ce72019-07-26 19:27:50164};
165
166} // namespace chrome_pdf
167
168#endif // PDF_DOCUMENT_LAYOUT_H_