summaryrefslogtreecommitdiffstats
path: root/printing
diff options
context:
space:
mode:
authorkmadhusu@chromium.org <kmadhusu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-04 00:29:21 +0000
committerkmadhusu@chromium.org <kmadhusu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-04 00:29:21 +0000
commitb89615d44dda84e025b625f21beb6787c75650be (patch)
treee60d46498a008d48e0c0793af607b78d2f411795 /printing
parente1444cbc545780f86766f842756ba17f5a8970ae (diff)
downloadchromium_src-b89615d44dda84e025b625f21beb6787c75650be.zip
chromium_src-b89615d44dda84e025b625f21beb6787c75650be.tar.gz
chromium_src-b89615d44dda84e025b625f21beb6787c75650be.tar.bz2
PrintPreview: [LINUX] Update the margin values after flipping the paper orientation.
BUG=101419 TEST=Please refer to bug description. Review URL: http://codereview.chromium.org/8351063 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@108598 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
-rw-r--r--printing/page_setup.cc33
-rw-r--r--printing/page_setup.h10
-rw-r--r--printing/page_setup_unittest.cc84
-rw-r--r--printing/print_settings.cc7
4 files changed, 124 insertions, 10 deletions
diff --git a/printing/page_setup.cc b/printing/page_setup.cc
index b870323e..ad0156e 100644
--- a/printing/page_setup.cc
+++ b/printing/page_setup.cc
@@ -19,6 +19,14 @@ PageMargins::PageMargins()
bottom(0) {
}
+void PageMargins::Rotate() {
+ int temp_right = right;
+ right = bottom;
+ bottom = left;
+ left = top;
+ top = temp_right;
+}
+
void PageMargins::Clear() {
header = 0;
footer = 0;
@@ -50,6 +58,7 @@ void PageSetup::Clear() {
content_area_.SetRect(0, 0, 0, 0);
effective_margins_.Clear();
text_height_ = 0;
+ forced_margins_ = false;
}
bool PageSetup::Equals(const PageSetup& rhs) const {
@@ -76,19 +85,17 @@ void PageSetup::Init(const gfx::Size& physical_size,
printable_area_ = printable_area;
text_height_ = text_height;
- CalculateSizesWithinRect(printable_area_, text_height_);
+ SetRequestedMarginsAndCalculateSizes(requested_margins_);
}
void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) {
- requested_margins_ = requested_margins;
- if (printable_area_.width() && printable_area_.height())
- CalculateSizesWithinRect(printable_area_, text_height_);
+ forced_margins_ = false;
+ SetRequestedMarginsAndCalculateSizes(requested_margins);
}
void PageSetup::ForceRequestedMargins(const PageMargins& requested_margins) {
- requested_margins_ = requested_margins;
- if (physical_size_.width() && physical_size_.height())
- CalculateSizesWithinRect(gfx::Rect(physical_size_), 0);
+ forced_margins_ = true;
+ SetRequestedMarginsAndCalculateSizes(requested_margins);
}
void PageSetup::FlipOrientation() {
@@ -100,10 +107,22 @@ void PageSetup::FlipOrientation() {
new_y,
printable_area_.height(),
printable_area_.width());
+ requested_margins_.Rotate();
Init(new_size, new_printable_area, text_height_);
}
}
+void PageSetup::SetRequestedMarginsAndCalculateSizes(
+ const PageMargins& requested_margins) {
+ requested_margins_ = requested_margins;
+ if (physical_size_.width() && physical_size_.height()) {
+ if (forced_margins_)
+ CalculateSizesWithinRect(gfx::Rect(physical_size_), 0);
+ else
+ CalculateSizesWithinRect(printable_area_, text_height_);
+ }
+}
+
void PageSetup::CalculateSizesWithinRect(const gfx::Rect& bounds,
int text_height) {
// Calculate the effective margins. The tricky part.
diff --git a/printing/page_setup.h b/printing/page_setup.h
index 9173475..8ed342a 100644
--- a/printing/page_setup.h
+++ b/printing/page_setup.h
@@ -15,6 +15,9 @@ class PRINTING_EXPORT PageMargins {
public:
PageMargins();
+ // Rotates the margin values in anti-clockwise direction.
+ void Rotate();
+
void Clear();
// Equality operator.
@@ -64,6 +67,10 @@ class PRINTING_EXPORT PageSetup {
}
private:
+ // Store |requested_margins_| and update page setup values.
+ void SetRequestedMarginsAndCalculateSizes(
+ const PageMargins& requested_margins);
+
// Calculate overlay_area_, effective_margins_, and content_area_, based on
// a constraint of |bounds| and |text_height|.
void CalculateSizesWithinRect(const gfx::Rect& bounds, int text_height);
@@ -87,6 +94,9 @@ class PRINTING_EXPORT PageSetup {
// Requested margins.
PageMargins requested_margins_;
+ // True when |effective_margins_| respects |printable_area_| else false.
+ bool forced_margins_;
+
// Space that must be kept free for the overlays.
int text_height_;
};
diff --git a/printing/page_setup_unittest.cc b/printing/page_setup_unittest.cc
index 54e436c..00c8f2a 100644
--- a/printing/page_setup_unittest.cc
+++ b/printing/page_setup_unittest.cc
@@ -190,3 +190,87 @@ TEST(PageSetupTest, OutOfRangeMargins) {
EXPECT_EQ(setup.effective_margins().right, 0);
EXPECT_EQ(setup.effective_margins().bottom, 0);
}
+
+TEST(PageSetupTest, FlipOrientation) {
+ // Margins.
+ printing::PageMargins margins;
+ margins.header = 2;
+ margins.footer = 3;
+ margins.left = 4;
+ margins.top = 14;
+ margins.right = 6;
+ margins.bottom = 7;
+ int kTextHeight = 5;
+
+ // Page description.
+ gfx::Size page_size(100, 70);
+ gfx::Rect printable_area(8, 9, 92, 50);
+
+ // Make the calculations.
+ printing::PageSetup setup;
+ setup.SetRequestedMargins(margins);
+ setup.Init(page_size, printable_area, kTextHeight);
+
+ gfx::Rect overlay_area(8, 9, 86, 50);
+ gfx::Rect content_area(8, 14, 86, 40);
+
+ EXPECT_EQ(page_size, setup.physical_size());
+ EXPECT_EQ(overlay_area, setup.overlay_area());
+ EXPECT_EQ(content_area, setup.content_area());
+
+ EXPECT_EQ(setup.effective_margins().left, 8);
+ EXPECT_EQ(setup.effective_margins().top, 14);
+ EXPECT_EQ(setup.effective_margins().right, 6);
+ EXPECT_EQ(setup.effective_margins().bottom, 16);
+
+ // Flip the orientation
+ setup.FlipOrientation();
+
+ // Expected values.
+ gfx::Size flipped_page_size(70, 100);
+ gfx::Rect flipped_printable_area(9, 0, 50, 92);
+ gfx::Rect flipped_overlay_area(14, 2, 45, 90);
+ gfx::Rect flipped_content_area(14, 7, 45, 80);
+
+ // Test values.
+ EXPECT_EQ(flipped_page_size, setup.physical_size());
+ EXPECT_EQ(flipped_overlay_area, setup.overlay_area()) << " " <<
+ flipped_page_size.ToString() << " " << flipped_printable_area.ToString();
+ EXPECT_EQ(flipped_content_area, setup.content_area()) << " " <<
+ flipped_page_size.ToString() << " " << flipped_printable_area.ToString();
+ EXPECT_EQ(flipped_printable_area, setup.printable_area());
+
+ // Margin values are updated as per the flipped values.
+ EXPECT_EQ(setup.effective_margins().left, 14);
+ EXPECT_EQ(setup.effective_margins().top, 7);
+ EXPECT_EQ(setup.effective_margins().right, 11);
+ EXPECT_EQ(setup.effective_margins().bottom, 13);
+
+ // Force requested margins and flip the orientation.
+ setup.Init(page_size, printable_area, kTextHeight);
+ setup.ForceRequestedMargins(margins);
+ EXPECT_EQ(setup.effective_margins().left, 4);
+ EXPECT_EQ(setup.effective_margins().top, 14);
+ EXPECT_EQ(setup.effective_margins().right, 6);
+ EXPECT_EQ(setup.effective_margins().bottom, 7);
+
+ // Flip the orientation
+ setup.FlipOrientation();
+
+ // Expected values.
+ gfx::Rect new_printable_area(9, 0, 50, 92);
+ gfx::Rect new_overlay_area(14, 2, 49, 95);
+ gfx::Rect new_content_area(14, 6, 49, 90);
+
+ // Test values.
+ EXPECT_EQ(flipped_page_size, setup.physical_size());
+ EXPECT_EQ(new_overlay_area, setup.overlay_area());
+ EXPECT_EQ(new_content_area, setup.content_area());
+ EXPECT_EQ(new_printable_area, setup.printable_area());
+
+ // Margins values are changed respectively.
+ EXPECT_EQ(setup.effective_margins().left,14);
+ EXPECT_EQ(setup.effective_margins().top, 6);
+ EXPECT_EQ(setup.effective_margins().right, 7);
+ EXPECT_EQ(setup.effective_margins().bottom, 4);
+}
diff --git a/printing/print_settings.cc b/printing/print_settings.cc
index 70ace6d..ddf3108 100644
--- a/printing/print_settings.cc
+++ b/printing/print_settings.cc
@@ -153,9 +153,6 @@ void PrintSettings::SetPrinterPrintableArea(
header_footer_text_height = ConvertUnit(kSettingHeaderFooterInterstice,
kPointsPerInch, units_per_inch);
}
- page_setup_device_units_.Init(physical_size_device_units,
- printable_area_device_units,
- header_footer_text_height);
PageMargins margins;
switch (margin_type) {
@@ -211,6 +208,10 @@ void PrintSettings::SetPrinterPrintableArea(
page_setup_device_units_.SetRequestedMargins(margins);
else
page_setup_device_units_.ForceRequestedMargins(margins);
+
+ page_setup_device_units_.Init(physical_size_device_units,
+ printable_area_device_units,
+ header_footer_text_height);
}
void PrintSettings::SetCustomMargins(