| 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 // 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. |
| 70 // 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. |
| 71 // DCHECK_LE(printable_area.bottom(), physical_size.height()); | 71 // DCHECK_LE(printable_area.bottom(), physical_size.height()); |
| 72 DCHECK_GE(printable_area.x(), 0); | 72 DCHECK_GE(printable_area.x(), 0); |
| 73 DCHECK_GE(printable_area.y(), 0); | 73 DCHECK_GE(printable_area.y(), 0); |
| 74 DCHECK_GE(text_height, 0); | 74 DCHECK_GE(text_height, 0); |
| 75 physical_size_ = physical_size; | 75 physical_size_ = physical_size; |
| 76 printable_area_ = printable_area; | 76 printable_area_ = printable_area; |
| 77 text_height_ = text_height; | 77 text_height_ = text_height; |
| 78 | 78 |
| 79 CalculateSizesWithinRect(printable_area_); | 79 CalculateSizesWithinRect(printable_area_, text_height_); |
| 80 } | 80 } |
| 81 | 81 |
| 82 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { | 82 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { |
| 83 requested_margins_ = requested_margins; | 83 requested_margins_ = requested_margins; |
| 84 if (printable_area_.width() && printable_area_.height()) | 84 if (printable_area_.width() && printable_area_.height()) |
| 85 CalculateSizesWithinRect(printable_area_); | 85 CalculateSizesWithinRect(printable_area_, text_height_); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) { | 88 void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) { |
| 89 requested_margins_ = requested_margins; | 89 requested_margins_ = requested_margins; |
| 90 if (physical_size_.width() && physical_size_.height()) | 90 if (physical_size_.width() && physical_size_.height()) |
| 91 CalculateSizesWithinRect(gfx::Rect(physical_size_)); | 91 CalculateSizesWithinRect(gfx::Rect(physical_size_), 0); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void PageSetup::FlipOrientation() { | 94 void PageSetup::FlipOrientation() { |
| 95 if (physical_size_.width() && physical_size_.height()) { | 95 if (physical_size_.width() && physical_size_.height()) { |
| 96 gfx::Size new_size(physical_size_.height(), physical_size_.width()); | 96 gfx::Size new_size(physical_size_.height(), physical_size_.width()); |
| 97 int new_y = physical_size_.width() - | 97 int new_y = physical_size_.width() - |
| 98 (printable_area_.width() + printable_area_.x()); | 98 (printable_area_.width() + printable_area_.x()); |
| 99 gfx::Rect new_printable_area(printable_area_.y(), | 99 gfx::Rect new_printable_area(printable_area_.y(), |
| 100 new_y, | 100 new_y, |
| 101 printable_area_.height(), | 101 printable_area_.height(), |
| 102 printable_area_.width()); | 102 printable_area_.width()); |
| 103 Init(new_size, new_printable_area, text_height_); | 103 Init(new_size, new_printable_area, text_height_); |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 | 106 |
| 107 void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds) { | 107 void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds, |
| 108 int text_height) { |
| 108 // Calculate the effective margins. The tricky part. | 109 // Calculate the effective margins. The tricky part. |
| 109 effective_margins_.header = std::max(requested_margins_.header, | 110 effective_margins_.header = std::max(requested_margins_.header, |
| 110 bounds.y()); | 111 bounds.y()); |
| 111 effective_margins_.footer = std::max(requested_margins_.footer, | 112 effective_margins_.footer = std::max(requested_margins_.footer, |
| 112 physical_size_.height() - | 113 physical_size_.height() - |
| 113 bounds.bottom()); | 114 bounds.bottom()); |
| 114 effective_margins_.left = std::max(requested_margins_.left, | 115 effective_margins_.left = std::max(requested_margins_.left, |
| 115 bounds.x()); | 116 bounds.x()); |
| 116 effective_margins_.top = std::max(std::max(requested_margins_.top, | 117 effective_margins_.top = std::max(std::max(requested_margins_.top, |
| 117 bounds.y()), | 118 bounds.y()), |
| 118 effective_margins_.header + text_height_); | 119 effective_margins_.header + text_height); |
| 119 effective_margins_.right = std::max(requested_margins_.right, | 120 effective_margins_.right = std::max(requested_margins_.right, |
| 120 physical_size_.width() - | 121 physical_size_.width() - |
| 121 bounds.right()); | 122 bounds.right()); |
| 122 effective_margins_.bottom = | 123 effective_margins_.bottom = |
| 123 std::max(std::max(requested_margins_.bottom, | 124 std::max(std::max(requested_margins_.bottom, |
| 124 physical_size_.height() - bounds.bottom()), | 125 physical_size_.height() - bounds.bottom()), |
| 125 effective_margins_.footer + text_height_); | 126 effective_margins_.footer + text_height); |
| 126 | 127 |
| 127 // Calculate the overlay area. If the margins are excessive, the overlay_area | 128 // Calculate the overlay area. If the margins are excessive, the overlay_area |
| 128 // size will be (0, 0). | 129 // size will be (0, 0). |
| 129 overlay_area_.set_x(effective_margins_.left); | 130 overlay_area_.set_x(effective_margins_.left); |
| 130 overlay_area_.set_y(effective_margins_.header); | 131 overlay_area_.set_y(effective_margins_.header); |
| 131 overlay_area_.set_width(std::max(0, | 132 overlay_area_.set_width(std::max(0, |
| 132 physical_size_.width() - | 133 physical_size_.width() - |
| 133 effective_margins_.right - | 134 effective_margins_.right - |
| 134 overlay_area_.x())); | 135 overlay_area_.x())); |
| 135 overlay_area_.set_height(std::max(0, | 136 overlay_area_.set_height(std::max(0, |
| 136 physical_size_.height() - | 137 physical_size_.height() - |
| 137 effective_margins_.footer - | 138 effective_margins_.footer - |
| 138 overlay_area_.y())); | 139 overlay_area_.y())); |
| 139 | 140 |
| 140 // Calculate the content area. If the margins are excessive, the content_area | 141 // Calculate the content area. If the margins are excessive, the content_area |
| 141 // size will be (0, 0). | 142 // size will be (0, 0). |
| 142 content_area_.set_x(effective_margins_.left); | 143 content_area_.set_x(effective_margins_.left); |
| 143 content_area_.set_y(effective_margins_.top); | 144 content_area_.set_y(effective_margins_.top); |
| 144 content_area_.set_width(std::max(0, | 145 content_area_.set_width(std::max(0, |
| 145 physical_size_.width() - | 146 physical_size_.width() - |
| 146 effective_margins_.right - | 147 effective_margins_.right - |
| 147 content_area_.x())); | 148 content_area_.x())); |
| 148 content_area_.set_height(std::max(0, | 149 content_area_.set_height(std::max(0, |
| 149 physical_size_.height() - | 150 physical_size_.height() - |
| 150 effective_margins_.bottom - | 151 effective_margins_.bottom - |
| 151 content_area_.y())); | 152 content_area_.y())); |
| 152 } | 153 } |
| 153 | 154 |
| 154 } // namespace printing | 155 } // namespace printing |
| OLD | NEW |