Skip to content

Commit

Permalink
Migrate GetCenteredPageContentRect() out of PrintedDocument
Browse files Browse the repository at this point in the history
The Windows-only PrintedDocument::GetCenteredPageContentRect() function
doesn't require any PrintedDocument internals.  This can be moved to
the printing utilities.

This relocation is part of the PrintedDocument refactoring to remove
system printing calls from its implementations.  Placing it in the
utility location will assist in the forthcoming refactoring of
PrintedDocument::RenderPrintedPage() in https://crrev.com/c/3183193.

There is no change to the functionality of GetCenteredPageContentRect()
with this move.

Bug: 809738
Change-Id: I80aaf066721e1abc504cf566606dc7c4fd9ca6df
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3183046
Commit-Queue: Alan Screen <awscreen@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#947595}
  • Loading branch information
Alan Screen authored and Chromium LUCI CQ committed Dec 2, 2021
1 parent 883aa8c commit 95898fd
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 80 deletions.
1 change: 0 additions & 1 deletion printing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ test("printing_unittests") {
if (is_win) {
sources += [
"emf_win_unittest.cc",
"printed_document_unittest.cc",
"printed_page_win_unittest.cc",
"printing_context_win_unittest.cc",
]
Expand Down
19 changes: 0 additions & 19 deletions printing/printed_document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,6 @@ void PrintedDocument::DebugDumpData(
base::RetainedRef(data)));
}

#if defined(OS_WIN)
// static
gfx::Rect PrintedDocument::GetCenteredPageContentRect(
const gfx::Size& paper_size,
const gfx::Size& page_size,
const gfx::Rect& page_content_rect) {
gfx::Rect 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);
}
return content_rect;
}
#endif

PrintedDocument::Mutable::Mutable() = default;

PrintedDocument::Mutable::~Mutable() = default;
Expand Down
8 changes: 0 additions & 8 deletions printing/printed_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,6 @@ class COMPONENT_EXPORT(PRINTING) PrintedDocument
const std::u16string& document_name,
const base::FilePath::StringType& extension);

#if defined(OS_WIN)
// Get page content rect adjusted based on
// http://dev.w3.org/csswg/css3-page/#positioning-page-box
static gfx::Rect GetCenteredPageContentRect(const gfx::Size& paper_size,
const gfx::Size& page_size,
const gfx::Rect& content_rect);
#endif

// Dump data on blocking task runner.
// Should only be called when debug dumps are enabled.
void DebugDumpData(const base::RefCountedMemory* data,
Expand Down
52 changes: 0 additions & 52 deletions printing/printed_document_unittest.cc

This file was deleted.

1 change: 1 addition & 0 deletions printing/printed_document_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "printing/page_number.h"
#include "printing/printed_page_win.h"
#include "printing/printing_context_win.h"
#include "printing/printing_utils.h"
#include "printing/units.h"
#include "skia/ext/skia_utils_win.h"

Expand Down
18 changes: 18 additions & 0 deletions printing/printing_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "printing/units.h"
#include "third_party/icu/source/common/unicode/uchar.h"
#include "ui/gfx/geometry/size.h"
Expand Down Expand Up @@ -106,4 +107,21 @@ bool SizesEqualWithinEpsilon(const gfx::Size& lhs,
std::abs(lhs.height() - rhs.height()) <= epsilon;
}

#if defined(OS_WIN)
gfx::Rect GetCenteredPageContentRect(const gfx::Size& paper_size,
const gfx::Size& page_size,
const gfx::Rect& page_content_rect) {
gfx::Rect 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);
}
return content_rect;
}
#endif // defined(OS_WIN)

} // namespace printing
14 changes: 14 additions & 0 deletions printing/printing_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

#include "base/component_export.h"
#include "base/strings/string_piece.h"
#include "build/build_config.h"

#if defined(OS_WIN)
#include "ui/gfx/geometry/rect.h"
#endif

namespace gfx {
class Size;
Expand Down Expand Up @@ -48,6 +53,15 @@ bool SizesEqualWithinEpsilon(const gfx::Size& lhs,
const gfx::Size& rhs,
int epsilon);

#if defined(OS_WIN)
// Get page content rect adjusted based on
// http://dev.w3.org/csswg/css3-page/#positioning-page-box
COMPONENT_EXPORT(PRINTING_BASE)
gfx::Rect GetCenteredPageContentRect(const gfx::Size& paper_size,
const gfx::Size& page_size,
const gfx::Rect& page_content_rect);
#endif

} // namespace printing

#endif // PRINTING_PRINTING_UTILS_H_
48 changes: 48 additions & 0 deletions printing/printing_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
#include <string>

#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"

#if defined(OS_WIN)
#include "ui/gfx/geometry/rect.h"
#endif

namespace printing {

namespace {
Expand Down Expand Up @@ -99,4 +104,47 @@ TEST(PrintingUtilsTest, SizesEqualWithinEpsilon) {
SizesEqualWithinEpsilon(kIsoA4Microns, gfx::Size(210500, 296500), 500));
}

#if defined(OS_WIN)
TEST(PrintingUtilsTest, GetCenteredPageContentRect) {
gfx::Rect page_content;

// No centering.
gfx::Size page_size = gfx::Size(1200, 1200);
gfx::Rect page_content_rect = gfx::Rect(0, 0, 400, 1100);
page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
page_content_rect);
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_size = gfx::Size(500, 1200);
page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
page_content_rect);
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_size = gfx::Size(1200, 500);
page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
page_content_rect);
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_size = gfx::Size(500, 500),
page_content = GetCenteredPageContentRect(gfx::Size(1000, 1000), page_size,
page_content_rect);
EXPECT_EQ(250, page_content.x());
EXPECT_EQ(250, page_content.y());
EXPECT_EQ(400, page_content.width());
EXPECT_EQ(1100, page_content.height());
}
#endif // defined(OS_WIN)

} // namespace printing

0 comments on commit 95898fd

Please sign in to comment.