[go: nahoru, domu]

blob: 1a20fcc8cf6d2674bf84365ef6fa7af38b1d9739 [file] [log] [blame]
Avi Drissmandb497b32022-09-15 19:47:281// Copyright 2011 The Chromium Authors
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
sverrir@google.com8ff1d422009-07-07 21:31:395#include "printing/page_setup.h"
initial.commit09911bf2008-07-26 23:55:296
thestig@chromium.orgc48bee22011-03-29 02:36:267#include <algorithm>
Peter Kasting70a70cc2023-05-12 17:53:178#include <tuple>
thestig@chromium.orgc48bee22011-03-29 02:36:269
Hans Wennborge0d04ff22020-04-24 20:17:5510#include "base/check_op.h"
initial.commit09911bf2008-07-26 23:55:2911
12namespace printing {
13
Lei Zhang3d64dc62018-09-28 17:08:3614namespace {
15
Daniel Hosseinian3553e272021-04-24 00:51:1816// Checks whether `printable_area` can be used to form a valid symmetrical
Lei Zhang3d64dc62018-09-28 17:08:3617// 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.
Daniel Hosseinian3553e272021-04-24 00:51:1822// `page_size` is the physical page size that includes margins.
Lei Zhang3d64dc62018-09-28 17:08:3623bool 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
initial.commit09911bf2008-07-26 23:55:2937PageMargins::PageMargins()
Lei Zhang01a1d3c2019-05-21 04:59:1038 : header(0), footer(0), left(0), right(0), top(0), bottom(0) {}
initial.commit09911bf2008-07-26 23:55:2939
Alan Screenc568a1ad2021-07-30 16:00:5640PageMargins::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
Peter Kasting70a70cc2023-05-12 17:53:1753bool 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}
Alan Screenb411fe62023-02-16 18:53:2058
initial.commit09911bf2008-07-26 23:55:2959void PageMargins::Clear() {
60 header = 0;
61 footer = 0;
62 left = 0;
63 right = 0;
64 top = 0;
65 bottom = 0;
66}
67
vandebo@chromium.org1c23b4e2011-10-15 22:30:4868PageSetup::PageSetup() {
69 Clear();
initial.commit09911bf2008-07-26 23:55:2970}
71
Alan Screen535f4d92021-07-30 21:23:5972PageSetup::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
vmpstr04b8358f2016-02-26 01:38:2981PageSetup::PageSetup(const PageSetup& other) = default;
82
Chris Watkinsc360b972017-12-01 05:50:2383PageSetup::~PageSetup() = default;
erg@google.comd2f05d02011-01-27 18:51:0184
Peter Kasting70a70cc2023-05-12 17:53:1785bool 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}
Alan Screenb411fe62023-02-16 18:53:2094
Lei Zhang3d64dc62018-09-28 17:08:3695// static
96gfx::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
initial.commit09911bf2008-07-26 23:55:29115void 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;
kmadhusu@chromium.orgb89615d2011-11-04 00:29:21122 forced_margins_ = false;
initial.commit09911bf2008-07-26 23:55:29123}
124
initial.commit09911bf2008-07-26 23:55:29125void 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());
maruel@google.com8ecd00f2008-08-14 21:16:24129 // 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());
initial.commit09911bf2008-07-26 23:55:29132 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
kmadhusu@chromium.orgb89615d2011-11-04 00:29:21139 SetRequestedMarginsAndCalculateSizes(requested_margins_);
initial.commit09911bf2008-07-26 23:55:29140}
141
142void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) {
kmadhusu@chromium.orgb89615d2011-11-04 00:29:21143 forced_margins_ = false;
144 SetRequestedMarginsAndCalculateSizes(requested_margins);
vandebo@chromium.org1c23b4e2011-10-15 22:30:48145}
146
147void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) {
kmadhusu@chromium.orgb89615d2011-11-04 00:29:21148 forced_margins_ = true;
149 SetRequestedMarginsAndCalculateSizes(requested_margins);
initial.commit09911bf2008-07-26 23:55:29150}
151
thestig@chromium.orgc48bee22011-03-29 02:36:26152void 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());
Lei Zhang01a1d3c2019-05-21 04:59:10157 gfx::Rect new_printable_area(printable_area_.y(), new_y,
thestig@chromium.orgc48bee22011-03-29 02:36:26158 printable_area_.height(),
159 printable_area_.width());
160 Init(new_size, new_printable_area, text_height_);
161 }
162}
163
kmadhusu@chromium.orgb89615d2011-11-04 00:29:21164void 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
vandebo@chromium.orgd69d322d2011-10-18 00:15:21175void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds,
176 int text_height) {
vandebo@chromium.org1c23b4e2011-10-15 22:30:48177 // Calculate the effective margins. The tricky part.
Lei Zhang01a1d3c2019-05-21 04:59:10178 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());
Peter Kasting14eab5c2019-09-12 18:18:27182 effective_margins_.top = std::max({requested_margins_.top, bounds.y(),
183 effective_margins_.header + text_height});
vandebo@chromium.org1c23b4e2011-10-15 22:30:48184 effective_margins_.right = std::max(requested_margins_.right,
Lei Zhang01a1d3c2019-05-21 04:59:10185 physical_size_.width() - bounds.right());
Peter Kasting14eab5c2019-09-12 18:18:27186 effective_margins_.bottom = std::max(
187 {requested_margins_.bottom, physical_size_.height() - bounds.bottom(),
188 effective_margins_.footer + text_height});
vandebo@chromium.org1c23b4e2011-10-15 22:30:48189
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);
Lei Zhang01a1d3c2019-05-21 04:59:10194 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()));
vandebo@chromium.org1c23b4e2011-10-15 22:30:48200
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);
Lei Zhang01a1d3c2019-05-21 04:59:10205 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()));
vandebo@chromium.org1c23b4e2011-10-15 22:30:48211}
212
initial.commit09911bf2008-07-26 23:55:29213} // namespace printing