diff options
author | hamaji@chromium.org <hamaji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-05 06:45:21 +0000 |
---|---|---|
committer | hamaji@chromium.org <hamaji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-05 06:45:21 +0000 |
commit | a12552b019c9b03162fba54aa9f2f8398f0ea32c (patch) | |
tree | 64a1b64484b0e1eb68a9c25bf96f502d3ab792f4 /printing | |
parent | 1322d8777c7c76eae42500407ec55adbc5e5187e (diff) | |
download | chromium_src-a12552b019c9b03162fba54aa9f2f8398f0ea32c.zip chromium_src-a12552b019c9b03162fba54aa9f2f8398f0ea32c.tar.gz chromium_src-a12552b019c9b03162fba54aa9f2f8398f0ea32c.tar.bz2 |
Add PrintedPage::GetCenteredPageContentRect so Mac and Win can share code.
BUG=47277
TEST=printed_page_unittest
Review URL: http://codereview.chromium.org/2878022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51626 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
-rw-r--r-- | printing/printed_document_mac.cc | 15 | ||||
-rw-r--r-- | printing/printed_document_win.cc | 13 | ||||
-rw-r--r-- | printing/printed_page.cc | 13 | ||||
-rw-r--r-- | printing/printed_page.h | 5 | ||||
-rw-r--r-- | printing/printed_page_unittest.cc | 67 | ||||
-rw-r--r-- | printing/printing.gyp | 1 |
6 files changed, 91 insertions, 23 deletions
diff --git a/printing/printed_document_mac.cc b/printing/printed_document_mac.cc index 3f5d75d..924ec0f 100644 --- a/printing/printed_document_mac.cc +++ b/printing/printed_document_mac.cc @@ -26,22 +26,13 @@ void PrintedDocument::RenderPrintedPage( const printing::PageSetup& page_setup( immutable_.settings_.page_setup_device_units()); - gfx::Rect target_rect = page.page_content_rect(); - const gfx::Rect& physical_size = page_setup.physical_size(); - // http://dev.w3.org/csswg/css3-page/#positioning-page-box - if (physical_size.width() > page.page_size().width()) { - int diff = physical_size.width() - page.page_size().width(); - target_rect.set_x(target_rect.x() + diff / 2); - } - if (physical_size.height() > page.page_size().height()) { - int diff = physical_size.height() - page.page_size().height(); - target_rect.set_y(target_rect.y() + diff / 2); - } + gfx::Rect content_area; + page.GetCenteredPageContentRect(page_setup.physical_size(), &content_area); const printing::NativeMetafile* metafile = page.native_metafile(); // Each NativeMetafile is a one-page PDF, and pages use 1-based indexing. const int page_number = 1; - metafile->RenderPage(page_number, context, target_rect.ToCGRect(), + metafile->RenderPage(page_number, context, content_area.ToCGRect(), true, false, false, false); // TODO(stuartmorgan): Print the header and footer. diff --git a/printing/printed_document_win.cc b/printing/printed_document_win.cc index e80ac78a..ac5008c 100644 --- a/printing/printed_document_win.cc +++ b/printing/printed_document_win.cc @@ -51,17 +51,8 @@ void PrintedDocument::RenderPrintedPage( const printing::PageSetup& page_setup( immutable_.settings_.page_setup_device_units()); - gfx::Rect content_area(page.page_content_rect()); - const gfx::Size& physical_size = page_setup.physical_size(); - // http://dev.w3.org/csswg/css3-page/#positioning-page-box - if (physical_size.width() > page.page_size().width()) { - int diff = physical_size.width() - page.page_size().width(); - content_area.set_x(content_area.x() + diff / 2); - } - if (physical_size.height() > page.page_size().height()) { - int diff = physical_size.height() - page.page_size().height(); - content_area.set_y(content_area.y() + diff / 2); - } + gfx::Rect content_area; + page.GetCenteredPageContentRect(page_setup.physical_size(), &content_area); // Save the state to make sure the context this function call does not modify // the device context. diff --git a/printing/printed_page.cc b/printing/printed_page.cc index 8abedeee..1661813 100644 --- a/printing/printed_page.cc +++ b/printing/printed_page.cc @@ -25,4 +25,17 @@ const NativeMetafile* PrintedPage::native_metafile() const { return native_metafile_.get(); } +void PrintedPage::GetCenteredPageContentRect( + const gfx::Size& paper_size, gfx::Rect* content_rect) const { + *content_rect = page_content_rect(); + if (paper_size.width() > page_size().width()) { + int diff = paper_size.width() - page_size().width(); + content_rect->set_x(content_rect->x() + diff / 2); + } + if (paper_size.height() > page_size().height()) { + int diff = paper_size.height() - page_size().height(); + content_rect->set_y(content_rect->y() + diff / 2); + } +} + } // namespace printing diff --git a/printing/printed_page.h b/printing/printed_page.h index 17181c6..65df888 100644 --- a/printing/printed_page.h +++ b/printing/printed_page.h @@ -34,6 +34,11 @@ class PrintedPage : public base::RefCountedThreadSafe<PrintedPage> { const gfx::Rect& page_content_rect() const { return page_content_rect_; } bool has_visible_overlays() const { return has_visible_overlays_; } + // Get page content rect adjusted based on + // http://dev.w3.org/csswg/css3-page/#positioning-page-box + void GetCenteredPageContentRect(const gfx::Size& paper_size, + gfx::Rect* content_rect) const; + private: friend class base::RefCountedThreadSafe<PrintedPage>; diff --git a/printing/printed_page_unittest.cc b/printing/printed_page_unittest.cc new file mode 100644 index 0000000..91b2111 --- /dev/null +++ b/printing/printed_page_unittest.cc @@ -0,0 +1,67 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "printing/printed_page.h" + +#include <string> +#include <vector> + +#include "testing/gtest/include/gtest/gtest.h" + +namespace printing { + +TEST(PrintedPageTest, GetCenteredPageContentRect) { + scoped_refptr<PrintedPage> page; + gfx::Rect page_content; + + // No centering. + page =new PrintedPage(1, + NULL, + gfx::Size(1200, 1200), + gfx::Rect(0, 0, 400, 1100), + true); + page->GetCenteredPageContentRect(gfx::Size(1000, 1000), &page_content); + EXPECT_EQ(0, page_content.x()); + EXPECT_EQ(0, page_content.y()); + EXPECT_EQ(400, page_content.width()); + EXPECT_EQ(1100, page_content.height()); + + // X centered. + page = new PrintedPage(1, + NULL, + gfx::Size(500, 1200), + gfx::Rect(0, 0, 400, 1100), + true); + page->GetCenteredPageContentRect(gfx::Size(1000, 1000), &page_content); + EXPECT_EQ(250, page_content.x()); + EXPECT_EQ(0, page_content.y()); + EXPECT_EQ(400, page_content.width()); + EXPECT_EQ(1100, page_content.height()); + + // Y centered. + page = new PrintedPage(1, + NULL, + gfx::Size(1200, 500), + gfx::Rect(0, 0, 400, 1100), + true); + page->GetCenteredPageContentRect(gfx::Size(1000, 1000), &page_content); + EXPECT_EQ(0, page_content.x()); + EXPECT_EQ(250, page_content.y()); + EXPECT_EQ(400, page_content.width()); + EXPECT_EQ(1100, page_content.height()); + + // Both X and Y centered. + page = new PrintedPage(1, + NULL, + gfx::Size(500, 500), + gfx::Rect(0, 0, 400, 1100), + true); + page->GetCenteredPageContentRect(gfx::Size(1000, 1000), &page_content); + EXPECT_EQ(250, page_content.x()); + EXPECT_EQ(250, page_content.y()); + EXPECT_EQ(400, page_content.width()); + EXPECT_EQ(1100, page_content.height()); +} + +} // namespace printing diff --git a/printing/printing.gyp b/printing/printing.gyp index 2e255a0..ec1872a 100644 --- a/printing/printing.gyp +++ b/printing/printing.gyp @@ -97,6 +97,7 @@ 'page_setup_unittest.cc', 'pdf_metafile_mac_unittest.cc', 'pdf_ps_metafile_cairo_unittest.cc', + 'printed_page_unittest.cc', 'printing_context_win_unittest.cc', 'run_all_unittests.cc', 'units_unittest.cc', |