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.
5 #include "printing/page_setup.h"
10 #include "base/check_op.h"
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();
37 PageMargins::PageMargins()
38 : header(0), footer(0), left(0), right(0), top(0), bottom(0) {}
40 PageMargins::PageMargins(int header,
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);
59 void PageMargins::Clear() {
68 PageSetup::PageSetup() {
72 PageSetup::PageSetup(const gfx::Size& physical_size,
73 const gfx::Rect& printable_area,
74 const PageMargins& requested_margins,
77 : requested_margins_(requested_margins), forced_margins_(forced_margins) {
78 Init(physical_size, printable_area, text_height);
81 PageSetup::PageSetup(const PageSetup& other) = default;
83 PageSetup::~PageSetup() = default;
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_,
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_);
96 gfx::Rect PageSetup::GetSymmetricalPrintableArea(
97 const gfx::Size& page_size,
98 const gfx::Rect& printable_area) {
99 if (!IsValidPrintableArea(page_size, printable_area))
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;
109 gfx::Rect symmetrical_printable_area = gfx::Rect(page_size);
110 symmetrical_printable_area.ClampToCenteredSize(gfx::Size(width, height));
112 return symmetrical_printable_area;
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();
122 forced_margins_ = false;
125 void PageSetup::Init(const gfx::Size& physical_size,
126 const gfx::Rect& printable_area,
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;
139 SetRequestedMarginsAndCalculateSizes(requested_margins_);
142 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) {
143 forced_margins_ = false;
144 SetRequestedMarginsAndCalculateSizes(requested_margins);
147 void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) {
148 forced_margins_ = true;
149 SetRequestedMarginsAndCalculateSizes(requested_margins);
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_);
164 void PageSetup::SetRequestedMarginsAndCalculateSizes(
165 const PageMargins& requested_margins) {
166 requested_margins_ = requested_margins;
167 if (physical_size_.width() && physical_size_.height()) {
169 CalculateSizesWithinRect(gfx::Rect(physical_size_), 0);
171 CalculateSizesWithinRect(printable_area_, text_height_);
175 void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds,
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});
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(
196 physical_size_.width() - effective_margins_.right - overlay_area_.x()));
197 overlay_area_.set_height(std::max(
199 physical_size_.height() - effective_margins_.footer - overlay_area_.y()));
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(
207 physical_size_.width() - effective_margins_.right - content_area_.x()));
208 content_area_.set_height(std::max(
210 physical_size_.height() - effective_margins_.bottom - content_area_.y()));
213 } // namespace printing