| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/printing/page_setup.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 namespace printing { | |
| 10 | |
| 11 PageMargins::PageMargins() | |
| 12 : header(0), | |
| 13 footer(0), | |
| 14 left(0), | |
| 15 right(0), | |
| 16 top(0), | |
| 17 bottom(0) { | |
| 18 } | |
| 19 | |
| 20 void PageMargins::Clear() { | |
| 21 header = 0; | |
| 22 footer = 0; | |
| 23 left = 0; | |
| 24 right = 0; | |
| 25 top = 0; | |
| 26 bottom = 0; | |
| 27 } | |
| 28 | |
| 29 bool PageMargins::Equals(const PageMargins& rhs) const { | |
| 30 return header == rhs.header && | |
| 31 footer == rhs.footer && | |
| 32 left == rhs.left && | |
| 33 top == rhs.top && | |
| 34 right == rhs.right && | |
| 35 bottom == rhs.bottom; | |
| 36 } | |
| 37 | |
| 38 PageSetup::PageSetup() : text_height_(0) { | |
| 39 } | |
| 40 | |
| 41 void PageSetup::Clear() { | |
| 42 physical_size_.SetSize(0, 0); | |
| 43 printable_area_.SetRect(0, 0, 0, 0); | |
| 44 overlay_area_.SetRect(0, 0, 0, 0); | |
| 45 content_area_.SetRect(0, 0, 0, 0); | |
| 46 effective_margins_.Clear(); | |
| 47 text_height_ = 0; | |
| 48 } | |
| 49 | |
| 50 bool PageSetup::Equals(const PageSetup& rhs) const { | |
| 51 return physical_size_ == rhs.physical_size_ && | |
| 52 printable_area_ == rhs.printable_area_ && | |
| 53 overlay_area_ == rhs.overlay_area_ && | |
| 54 content_area_ == rhs.content_area_ && | |
| 55 effective_margins_.Equals(rhs.effective_margins_) && | |
| 56 requested_margins_.Equals(rhs.requested_margins_) && | |
| 57 text_height_ == rhs.text_height_; | |
| 58 } | |
| 59 | |
| 60 void PageSetup::Init(const gfx::Size& physical_size, | |
| 61 const gfx::Rect& printable_area, | |
| 62 int text_height) { | |
| 63 DCHECK_LE(printable_area.right(), physical_size.width()); | |
| 64 // I've seen this assert triggers on Canon GP160PF PCL 5e and HP LaserJet 5. | |
| 65 // Since we don't know the dpi here, just disable the check. | |
| 66 // DCHECK_LE(printable_area.bottom(), physical_size.height()); | |
| 67 DCHECK_GE(printable_area.x(), 0); | |
| 68 DCHECK_GE(printable_area.y(), 0); | |
| 69 DCHECK_GE(text_height, 0); | |
| 70 physical_size_ = physical_size; | |
| 71 printable_area_ = printable_area; | |
| 72 text_height_ = text_height; | |
| 73 | |
| 74 // Calculate the effective margins. The tricky part. | |
| 75 effective_margins_.header = std::max(requested_margins_.header, | |
| 76 printable_area_.y()); | |
| 77 effective_margins_.footer = std::max(requested_margins_.footer, | |
| 78 physical_size.height() - | |
| 79 printable_area_.bottom()); | |
| 80 effective_margins_.left = std::max(requested_margins_.left, | |
| 81 printable_area_.x()); | |
| 82 effective_margins_.top = std::max(std::max(requested_margins_.top, | |
| 83 printable_area_.y()), | |
| 84 effective_margins_.header + text_height); | |
| 85 effective_margins_.right = std::max(requested_margins_.right, | |
| 86 physical_size.width() - | |
| 87 printable_area_.right()); | |
| 88 effective_margins_.bottom = std::max(std::max(requested_margins_.bottom, | |
| 89 physical_size.height() - | |
| 90 printable_area_.bottom()), | |
| 91 effective_margins_.footer + text_height); | |
| 92 | |
| 93 // Calculate the overlay area. If the margins are excessive, the overlay_area | |
| 94 // size will be (0, 0). | |
| 95 overlay_area_.set_x(effective_margins_.left); | |
| 96 overlay_area_.set_y(effective_margins_.header); | |
| 97 overlay_area_.set_width(std::max(0, | |
| 98 physical_size.width() - | |
| 99 effective_margins_.right - | |
| 100 overlay_area_.x())); | |
| 101 overlay_area_.set_height(std::max(0, | |
| 102 physical_size.height() - | |
| 103 effective_margins_.footer - | |
| 104 overlay_area_.y())); | |
| 105 | |
| 106 // Calculate the content area. If the margins are excessive, the content_area | |
| 107 // size will be (0, 0). | |
| 108 content_area_.set_x(effective_margins_.left); | |
| 109 content_area_.set_y(effective_margins_.top); | |
| 110 content_area_.set_width(std::max(0, | |
| 111 physical_size.width() - | |
| 112 effective_margins_.right - | |
| 113 content_area_.x())); | |
| 114 content_area_.set_height(std::max(0, | |
| 115 physical_size.height() - | |
| 116 effective_margins_.bottom - | |
| 117 content_area_.y())); | |
| 118 } | |
| 119 | |
| 120 void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { | |
| 121 requested_margins_ = requested_margins; | |
| 122 if (physical_size_.width() && physical_size_.height()) | |
| 123 Init(physical_size_, printable_area_, text_height_); | |
| 124 } | |
| 125 | |
| 126 } // namespace printing | |
| OLD | NEW |