[email protected] | c48bee2 | 2011-03-29 02:36:26 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #include "printing/page_setup.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | c48bee2 | 2011-03-29 02:36:26 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include "base/logging.h" |
| 10 | |
| 11 | namespace printing { |
| 12 | |
| 13 | PageMargins::PageMargins() |
| 14 | : header(0), |
| 15 | footer(0), |
| 16 | left(0), |
| 17 | right(0), |
| 18 | top(0), |
| 19 | bottom(0) { |
| 20 | } |
| 21 | |
| 22 | void PageMargins::Clear() { |
| 23 | header = 0; |
| 24 | footer = 0; |
| 25 | left = 0; |
| 26 | right = 0; |
| 27 | top = 0; |
| 28 | bottom = 0; |
| 29 | } |
| 30 | |
| 31 | bool 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] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 40 | PageSetup::PageSetup() { |
| 41 | Clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | } |
| 43 | |
vmpstr | 04b8358f | 2016-02-26 01:38:29 | [diff] [blame] | 44 | PageSetup::PageSetup(const PageSetup& other) = default; |
| 45 | |
Chris Watkins | c360b97 | 2017-12-01 05:50:23 | [diff] [blame] | 46 | PageSetup::~PageSetup() = default; |
[email protected] | d2f05d0 | 2011-01-27 18:51:01 | [diff] [blame] | 47 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | void 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] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 55 | forced_margins_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool 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 | |
| 68 | void 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] | 8ecd00f | 2008-08-14 21:16:24 | [diff] [blame] | 72 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | 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] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 82 | SetRequestedMarginsAndCalculateSizes(requested_margins_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { |
[email protected] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 86 | forced_margins_ = false; |
| 87 | SetRequestedMarginsAndCalculateSizes(requested_margins); |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) { |
[email protected] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 91 | forced_margins_ = true; |
| 92 | SetRequestedMarginsAndCalculateSizes(requested_margins); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | c48bee2 | 2011-03-29 02:36:26 | [diff] [blame] | 95 | void 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] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 108 | void 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] | d69d322d | 2011-10-18 00:15:21 | [diff] [blame] | 119 | void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds, |
| 120 | int text_height) { |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 121 | // 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] | d69d322d | 2011-10-18 00:15:21 | [diff] [blame] | 131 | effective_margins_.header + text_height); |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 132 | 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] | d69d322d | 2011-10-18 00:15:21 | [diff] [blame] | 138 | effective_margins_.footer + text_height); |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 139 | |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | } // namespace printing |