[M120 Migration][VD] Remove accessing oom_score_adj in zygote process
[platform/framework/web/chromium-efl.git] / printing / page_setup.cc
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "printing/page_setup.h"
6
7 #include <algorithm>
8 #include <tuple>
9
10 #include "base/check_op.h"
11
12 namespace printing {
13
14 namespace {
15
16 // Checks whether `printable_area` can be used to form a valid symmetrical
17 // printable area, so that margin_left equals margin_right, and margin_top
18 // equals margin_bottom.  For example if
19 // printable_area.x() * 2 >= page_size.width(), then the
20 // content_width = page_size.width() - 2 * printable_area.x() would be zero or
21 // negative, which is invalid.
22 // `page_size` is the physical page size that includes margins.
23 bool IsValidPrintableArea(const gfx::Size& page_size,
24                           const gfx::Rect& printable_area) {
25   return !printable_area.IsEmpty() && printable_area.x() >= 0 &&
26          printable_area.y() >= 0 &&
27          printable_area.right() <= page_size.width() &&
28          printable_area.bottom() <= page_size.height() &&
29          printable_area.x() * 2 < page_size.width() &&
30          printable_area.y() * 2 < page_size.height() &&
31          printable_area.right() * 2 > page_size.width() &&
32          printable_area.bottom() * 2 > page_size.height();
33 }
34
35 }  // namespace
36
37 PageMargins::PageMargins()
38     : header(0), footer(0), left(0), right(0), top(0), bottom(0) {}
39
40 PageMargins::PageMargins(int header,
41                          int footer,
42                          int left,
43                          int right,
44                          int top,
45                          int bottom)
46     : header(header),
47       footer(footer),
48       left(left),
49       right(right),
50       top(top),
51       bottom(bottom) {}
52
53 bool PageMargins::operator==(const PageMargins& other) const {
54   return std::tie(header, footer, left, right, top, bottom) ==
55          std::tie(other.header, other.footer, other.left, other.right,
56                   other.top, other.bottom);
57 }
58
59 void PageMargins::Clear() {
60   header = 0;
61   footer = 0;
62   left = 0;
63   right = 0;
64   top = 0;
65   bottom = 0;
66 }
67
68 PageSetup::PageSetup() {
69   Clear();
70 }
71
72 PageSetup::PageSetup(const gfx::Size& physical_size,
73                      const gfx::Rect& printable_area,
74                      const PageMargins& requested_margins,
75                      bool forced_margins,
76                      int text_height)
77     : requested_margins_(requested_margins), forced_margins_(forced_margins) {
78   Init(physical_size, printable_area, text_height);
79 }
80
81 PageSetup::PageSetup(const PageSetup& other) = default;
82
83 PageSetup::~PageSetup() = default;
84
85 bool PageSetup::operator==(const PageSetup& other) const {
86   return std::tie(physical_size_, printable_area_, overlay_area_, content_area_,
87                   effective_margins_, requested_margins_, forced_margins_,
88                   text_height_) ==
89          std::tie(other.physical_size_, other.printable_area_,
90                   other.overlay_area_, other.content_area_,
91                   other.effective_margins_, other.requested_margins_,
92                   other.forced_margins_, other.text_height_);
93 }
94
95 // static
96 gfx::Rect PageSetup::GetSymmetricalPrintableArea(
97     const gfx::Size& page_size,
98     const gfx::Rect& printable_area) {
99   if (!IsValidPrintableArea(page_size, printable_area))
100     return gfx::Rect();
101
102   int left_right_margin =
103       std::max(printable_area.x(), page_size.width() - printable_area.right());
104   int top_bottom_margin = std::max(
105       printable_area.y(), page_size.height() - printable_area.bottom());
106   int width = page_size.width() - 2 * left_right_margin;
107   int height = page_size.height() - 2 * top_bottom_margin;
108
109   gfx::Rect symmetrical_printable_area = gfx::Rect(page_size);
110   symmetrical_printable_area.ClampToCenteredSize(gfx::Size(width, height));
111
112   return symmetrical_printable_area;
113 }
114
115 void PageSetup::Clear() {
116   physical_size_.SetSize(0, 0);
117   printable_area_.SetRect(0, 0, 0, 0);
118   overlay_area_.SetRect(0, 0, 0, 0);
119   content_area_.SetRect(0, 0, 0, 0);
120   effective_margins_.Clear();
121   text_height_ = 0;
122   forced_margins_ = false;
123 }
124
125 void PageSetup::Init(const gfx::Size& physical_size,
126                      const gfx::Rect& printable_area,
127                      int text_height) {
128   DCHECK_LE(printable_area.right(), physical_size.width());
129   // I've seen this assert triggers on Canon GP160PF PCL 5e and HP LaserJet 5.
130   // Since we don't know the dpi here, just disable the check.
131   // DCHECK_LE(printable_area.bottom(), physical_size.height());
132   DCHECK_GE(printable_area.x(), 0);
133   DCHECK_GE(printable_area.y(), 0);
134   DCHECK_GE(text_height, 0);
135   physical_size_ = physical_size;
136   printable_area_ = printable_area;
137   text_height_ = text_height;
138
139   SetRequestedMarginsAndCalculateSizes(requested_margins_);
140 }
141
142 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) {
143   forced_margins_ = false;
144   SetRequestedMarginsAndCalculateSizes(requested_margins);
145 }
146
147 void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) {
148   forced_margins_ = true;
149   SetRequestedMarginsAndCalculateSizes(requested_margins);
150 }
151
152 void PageSetup::FlipOrientation() {
153   if (physical_size_.width() && physical_size_.height()) {
154     gfx::Size new_size(physical_size_.height(), physical_size_.width());
155     int new_y = physical_size_.width() -
156                 (printable_area_.width() + printable_area_.x());
157     gfx::Rect new_printable_area(printable_area_.y(), new_y,
158                                  printable_area_.height(),
159                                  printable_area_.width());
160     Init(new_size, new_printable_area, text_height_);
161   }
162 }
163
164 void PageSetup::SetRequestedMarginsAndCalculateSizes(
165     const PageMargins& requested_margins) {
166   requested_margins_ = requested_margins;
167   if (physical_size_.width() && physical_size_.height()) {
168     if (forced_margins_)
169       CalculateSizesWithinRect(gfx::Rect(physical_size_), 0);
170     else
171       CalculateSizesWithinRect(printable_area_, text_height_);
172   }
173 }
174
175 void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds,
176                                          int text_height) {
177   // Calculate the effective margins. The tricky part.
178   effective_margins_.header = std::max(requested_margins_.header, bounds.y());
179   effective_margins_.footer = std::max(
180       requested_margins_.footer, physical_size_.height() - bounds.bottom());
181   effective_margins_.left = std::max(requested_margins_.left, bounds.x());
182   effective_margins_.top = std::max({requested_margins_.top, bounds.y(),
183                                      effective_margins_.header + text_height});
184   effective_margins_.right = std::max(requested_margins_.right,
185                                       physical_size_.width() - bounds.right());
186   effective_margins_.bottom = std::max(
187       {requested_margins_.bottom, physical_size_.height() - bounds.bottom(),
188        effective_margins_.footer + text_height});
189
190   // Calculate the overlay area. If the margins are excessive, the overlay_area
191   // size will be (0, 0).
192   overlay_area_.set_x(effective_margins_.left);
193   overlay_area_.set_y(effective_margins_.header);
194   overlay_area_.set_width(std::max(
195       0,
196       physical_size_.width() - effective_margins_.right - overlay_area_.x()));
197   overlay_area_.set_height(std::max(
198       0,
199       physical_size_.height() - effective_margins_.footer - overlay_area_.y()));
200
201   // Calculate the content area. If the margins are excessive, the content_area
202   // size will be (0, 0).
203   content_area_.set_x(effective_margins_.left);
204   content_area_.set_y(effective_margins_.top);
205   content_area_.set_width(std::max(
206       0,
207       physical_size_.width() - effective_margins_.right - content_area_.x()));
208   content_area_.set_height(std::max(
209       0,
210       physical_size_.height() - effective_margins_.bottom - content_area_.y()));
211 }
212
213 }  // namespace printing