OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/page_setup.h" | 5 #include "printing/page_setup.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 bool PageMargins::Equals(const PageMargins& rhs) const { | 31 bool PageMargins::Equals(const PageMargins& rhs) const { |
32 return header == rhs.header && | 32 return header == rhs.header && |
33 footer == rhs.footer && | 33 footer == rhs.footer && |
34 left == rhs.left && | 34 left == rhs.left && |
35 top == rhs.top && | 35 top == rhs.top && |
36 right == rhs.right && | 36 right == rhs.right && |
37 bottom == rhs.bottom; | 37 bottom == rhs.bottom; |
38 } | 38 } |
39 | 39 |
40 PageSetup::PageSetup() : text_height_(0) { | 40 PageSetup::PageSetup() { |
| 41 Clear(); |
41 } | 42 } |
42 | 43 |
43 PageSetup::~PageSetup() {} | 44 PageSetup::~PageSetup() {} |
44 | 45 |
45 void PageSetup::Clear() { | 46 void PageSetup::Clear() { |
46 physical_size_.SetSize(0, 0); | 47 physical_size_.SetSize(0, 0); |
47 printable_area_.SetRect(0, 0, 0, 0); | 48 printable_area_.SetRect(0, 0, 0, 0); |
48 overlay_area_.SetRect(0, 0, 0, 0); | 49 overlay_area_.SetRect(0, 0, 0, 0); |
49 content_area_.SetRect(0, 0, 0, 0); | 50 content_area_.SetRect(0, 0, 0, 0); |
50 effective_margins_.Clear(); | 51 effective_margins_.Clear(); |
(...skipping 17 matching lines...) Expand all Loading... |
68 // I've seen this assert triggers on Canon GP160PF PCL 5e and HP LaserJet 5. | 69 // I've seen this assert triggers on Canon GP160PF PCL 5e and HP LaserJet 5. |
69 // Since we don't know the dpi here, just disable the check. | 70 // Since we don't know the dpi here, just disable the check. |
70 // DCHECK_LE(printable_area.bottom(), physical_size.height()); | 71 // DCHECK_LE(printable_area.bottom(), physical_size.height()); |
71 DCHECK_GE(printable_area.x(), 0); | 72 DCHECK_GE(printable_area.x(), 0); |
72 DCHECK_GE(printable_area.y(), 0); | 73 DCHECK_GE(printable_area.y(), 0); |
73 DCHECK_GE(text_height, 0); | 74 DCHECK_GE(text_height, 0); |
74 physical_size_ = physical_size; | 75 physical_size_ = physical_size; |
75 printable_area_ = printable_area; | 76 printable_area_ = printable_area; |
76 text_height_ = text_height; | 77 text_height_ = text_height; |
77 | 78 |
| 79 CalculateSizesWithinRect(printable_area_); |
| 80 } |
| 81 |
| 82 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { |
| 83 requested_margins_ = requested_margins; |
| 84 if (printable_area_.width() && printable_area_.height()) |
| 85 CalculateSizesWithinRect(printable_area_); |
| 86 } |
| 87 |
| 88 void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) { |
| 89 requested_margins_ = requested_margins; |
| 90 if (physical_size_.width() && physical_size_.height()) |
| 91 CalculateSizesWithinRect(gfx::Rect(physical_size_)); |
| 92 } |
| 93 |
| 94 void PageSetup::FlipOrientation() { |
| 95 if (physical_size_.width() && physical_size_.height()) { |
| 96 gfx::Size new_size(physical_size_.height(), physical_size_.width()); |
| 97 int new_y = physical_size_.width() - |
| 98 (printable_area_.width() + printable_area_.x()); |
| 99 gfx::Rect new_printable_area(printable_area_.y(), |
| 100 new_y, |
| 101 printable_area_.height(), |
| 102 printable_area_.width()); |
| 103 Init(new_size, new_printable_area, text_height_); |
| 104 } |
| 105 } |
| 106 |
| 107 void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds) { |
78 // Calculate the effective margins. The tricky part. | 108 // Calculate the effective margins. The tricky part. |
79 effective_margins_.header = std::max(requested_margins_.header, | 109 effective_margins_.header = std::max(requested_margins_.header, |
80 printable_area_.y()); | 110 bounds.y()); |
81 effective_margins_.footer = std::max(requested_margins_.footer, | 111 effective_margins_.footer = std::max(requested_margins_.footer, |
82 physical_size.height() - | 112 physical_size_.height() - |
83 printable_area_.bottom()); | 113 bounds.bottom()); |
84 effective_margins_.left = std::max(requested_margins_.left, | 114 effective_margins_.left = std::max(requested_margins_.left, |
85 printable_area_.x()); | 115 bounds.x()); |
86 effective_margins_.top = std::max(std::max(requested_margins_.top, | 116 effective_margins_.top = std::max(std::max(requested_margins_.top, |
87 printable_area_.y()), | 117 bounds.y()), |
88 effective_margins_.header + text_height); | 118 effective_margins_.header + text_height_); |
89 effective_margins_.right = std::max(requested_margins_.right, | 119 effective_margins_.right = std::max(requested_margins_.right, |
90 physical_size.width() - | 120 physical_size_.width() - |
91 printable_area_.right()); | 121 bounds.right()); |
92 effective_margins_.bottom = std::max(std::max(requested_margins_.bottom, | 122 effective_margins_.bottom = |
93 physical_size.height() - | 123 std::max(std::max(requested_margins_.bottom, |
94 printable_area_.bottom()), | 124 physical_size_.height() - bounds.bottom()), |
95 effective_margins_.footer + text_height); | 125 effective_margins_.footer + text_height_); |
96 | 126 |
97 // Calculate the overlay area. If the margins are excessive, the overlay_area | 127 // Calculate the overlay area. If the margins are excessive, the overlay_area |
98 // size will be (0, 0). | 128 // size will be (0, 0). |
99 overlay_area_.set_x(effective_margins_.left); | 129 overlay_area_.set_x(effective_margins_.left); |
100 overlay_area_.set_y(effective_margins_.header); | 130 overlay_area_.set_y(effective_margins_.header); |
101 overlay_area_.set_width(std::max(0, | 131 overlay_area_.set_width(std::max(0, |
102 physical_size.width() - | 132 physical_size_.width() - |
103 effective_margins_.right - | 133 effective_margins_.right - |
104 overlay_area_.x())); | 134 overlay_area_.x())); |
105 overlay_area_.set_height(std::max(0, | 135 overlay_area_.set_height(std::max(0, |
106 physical_size.height() - | 136 physical_size_.height() - |
107 effective_margins_.footer - | 137 effective_margins_.footer - |
108 overlay_area_.y())); | 138 overlay_area_.y())); |
109 | 139 |
110 // Calculate the content area. If the margins are excessive, the content_area | 140 // Calculate the content area. If the margins are excessive, the content_area |
111 // size will be (0, 0). | 141 // size will be (0, 0). |
112 content_area_.set_x(effective_margins_.left); | 142 content_area_.set_x(effective_margins_.left); |
113 content_area_.set_y(effective_margins_.top); | 143 content_area_.set_y(effective_margins_.top); |
114 content_area_.set_width(std::max(0, | 144 content_area_.set_width(std::max(0, |
115 physical_size.width() - | 145 physical_size_.width() - |
116 effective_margins_.right - | 146 effective_margins_.right - |
117 content_area_.x())); | 147 content_area_.x())); |
118 content_area_.set_height(std::max(0, | 148 content_area_.set_height(std::max(0, |
119 physical_size.height() - | 149 physical_size_.height() - |
120 effective_margins_.bottom - | 150 effective_margins_.bottom - |
121 content_area_.y())); | 151 content_area_.y())); |
122 } | 152 } |
123 | 153 |
124 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { | |
125 requested_margins_ = requested_margins; | |
126 if (physical_size_.width() && physical_size_.height()) | |
127 Init(physical_size_, printable_area_, text_height_); | |
128 } | |
129 | |
130 void PageSetup::FlipOrientation() { | |
131 if (physical_size_.width() && physical_size_.height()) { | |
132 gfx::Size new_size(physical_size_.height(), physical_size_.width()); | |
133 int new_y = physical_size_.width() - | |
134 (printable_area_.width() + printable_area_.x()); | |
135 gfx::Rect new_printable_area(printable_area_.y(), | |
136 new_y, | |
137 printable_area_.height(), | |
138 printable_area_.width()); | |
139 Init(new_size, new_printable_area, text_height_); | |
140 } | |
141 } | |
142 | |
143 } // namespace printing | 154 } // namespace printing |
OLD | NEW |