blob: fb342d538f6db4d9a6ad4c6ed5b73dc74357b03c [file] [log] [blame]
[email protected]c48bee22011-03-29 02:36:261// Copyright (c) 2011 The Chromium Authors. All rights reserved.
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
[email protected]8ff1d422009-07-07 21:31:395#include "printing/page_setup.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]c48bee22011-03-29 02:36:267#include <algorithm>
8
initial.commit09911bf2008-07-26 23:55:299#include "base/logging.h"
10
11namespace printing {
12
13PageMargins::PageMargins()
14 : header(0),
15 footer(0),
16 left(0),
17 right(0),
18 top(0),
19 bottom(0) {
20}
21
22void PageMargins::Clear() {
23 header = 0;
24 footer = 0;
25 left = 0;
26 right = 0;
27 top = 0;
28 bottom = 0;
29}
30
31bool PageMargins::Equals(const PageMargins& rhs) const {
32 return header == rhs.header &&
33 footer == rhs.footer &&
34 left == rhs.left &&
35 top == rhs.top &&
36 right == rhs.right &&
37 bottom == rhs.bottom;
38}
39
[email protected]1c23b4e82011-10-15 22:30:4840PageSetup::PageSetup() {
41 Clear();
initial.commit09911bf2008-07-26 23:55:2942}
43
vmpstr04b8358f2016-02-26 01:38:2944PageSetup::PageSetup(const PageSetup& other) = default;
45
Chris Watkinsc360b972017-12-01 05:50:2346PageSetup::~PageSetup() = default;
[email protected]d2f05d02011-01-27 18:51:0147
initial.commit09911bf2008-07-26 23:55:2948void PageSetup::Clear() {
49 physical_size_.SetSize(0, 0);
50 printable_area_.SetRect(0, 0, 0, 0);
51 overlay_area_.SetRect(0, 0, 0, 0);
52 content_area_.SetRect(0, 0, 0, 0);
53 effective_margins_.Clear();
54 text_height_ = 0;
[email protected]b89615d2011-11-04 00:29:2155 forced_margins_ = false;
initial.commit09911bf2008-07-26 23:55:2956}
57
58bool PageSetup::Equals(const PageSetup& rhs) const {
59 return physical_size_ == rhs.physical_size_ &&
60 printable_area_ == rhs.printable_area_ &&
61 overlay_area_ == rhs.overlay_area_ &&
62 content_area_ == rhs.content_area_ &&
63 effective_margins_.Equals(rhs.effective_margins_) &&
64 requested_margins_.Equals(rhs.requested_margins_) &&
65 text_height_ == rhs.text_height_;
66}
67
68void PageSetup::Init(const gfx::Size& physical_size,
69 const gfx::Rect& printable_area,
70 int text_height) {
71 DCHECK_LE(printable_area.right(), physical_size.width());
[email protected]8ecd00f2008-08-14 21:16:2472 // I've seen this assert triggers on Canon GP160PF PCL 5e and HP LaserJet 5.
73 // Since we don't know the dpi here, just disable the check.
74 // DCHECK_LE(printable_area.bottom(), physical_size.height());
initial.commit09911bf2008-07-26 23:55:2975 DCHECK_GE(printable_area.x(), 0);
76 DCHECK_GE(printable_area.y(), 0);
77 DCHECK_GE(text_height, 0);
78 physical_size_ = physical_size;
79 printable_area_ = printable_area;
80 text_height_ = text_height;
81
[email protected]b89615d2011-11-04 00:29:2182 SetRequestedMarginsAndCalculateSizes(requested_margins_);
initial.commit09911bf2008-07-26 23:55:2983}
84
85void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) {
[email protected]b89615d2011-11-04 00:29:2186 forced_margins_ = false;
87 SetRequestedMarginsAndCalculateSizes(requested_margins);
[email protected]1c23b4e82011-10-15 22:30:4888}
89
90void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) {
[email protected]b89615d2011-11-04 00:29:2191 forced_margins_ = true;
92 SetRequestedMarginsAndCalculateSizes(requested_margins);
initial.commit09911bf2008-07-26 23:55:2993}
94
[email protected]c48bee22011-03-29 02:36:2695void PageSetup::FlipOrientation() {
96 if (physical_size_.width() && physical_size_.height()) {
97 gfx::Size new_size(physical_size_.height(), physical_size_.width());
98 int new_y = physical_size_.width() -
99 (printable_area_.width() + printable_area_.x());
100 gfx::Rect new_printable_area(printable_area_.y(),
101 new_y,
102 printable_area_.height(),
103 printable_area_.width());
104 Init(new_size, new_printable_area, text_height_);
105 }
106}
107
[email protected]b89615d2011-11-04 00:29:21108void PageSetup::SetRequestedMarginsAndCalculateSizes(
109 const PageMargins& requested_margins) {
110 requested_margins_ = requested_margins;
111 if (physical_size_.width() && physical_size_.height()) {
112 if (forced_margins_)
113 CalculateSizesWithinRect(gfx::Rect(physical_size_), 0);
114 else
115 CalculateSizesWithinRect(printable_area_, text_height_);
116 }
117}
118
[email protected]d69d322d2011-10-18 00:15:21119void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds,
120 int text_height) {
[email protected]1c23b4e82011-10-15 22:30:48121 // Calculate the effective margins. The tricky part.
122 effective_margins_.header = std::max(requested_margins_.header,
123 bounds.y());
124 effective_margins_.footer = std::max(requested_margins_.footer,
125 physical_size_.height() -
126 bounds.bottom());
127 effective_margins_.left = std::max(requested_margins_.left,
128 bounds.x());
129 effective_margins_.top = std::max(std::max(requested_margins_.top,
130 bounds.y()),
[email protected]d69d322d2011-10-18 00:15:21131 effective_margins_.header + text_height);
[email protected]1c23b4e82011-10-15 22:30:48132 effective_margins_.right = std::max(requested_margins_.right,
133 physical_size_.width() -
134 bounds.right());
135 effective_margins_.bottom =
136 std::max(std::max(requested_margins_.bottom,
137 physical_size_.height() - bounds.bottom()),
[email protected]d69d322d2011-10-18 00:15:21138 effective_margins_.footer + text_height);
[email protected]1c23b4e82011-10-15 22:30:48139
140 // Calculate the overlay area. If the margins are excessive, the overlay_area
141 // size will be (0, 0).
142 overlay_area_.set_x(effective_margins_.left);
143 overlay_area_.set_y(effective_margins_.header);
144 overlay_area_.set_width(std::max(0,
145 physical_size_.width() -
146 effective_margins_.right -
147 overlay_area_.x()));
148 overlay_area_.set_height(std::max(0,
149 physical_size_.height() -
150 effective_margins_.footer -
151 overlay_area_.y()));
152
153 // Calculate the content area. If the margins are excessive, the content_area
154 // size will be (0, 0).
155 content_area_.set_x(effective_margins_.left);
156 content_area_.set_y(effective_margins_.top);
157 content_area_.set_width(std::max(0,
158 physical_size_.width() -
159 effective_margins_.right -
160 content_area_.x()));
161 content_area_.set_height(std::max(0,
162 physical_size_.height() -
163 effective_margins_.bottom -
164 content_area_.y()));
165}
166
initial.commit09911bf2008-07-26 23:55:29167} // namespace printing