diff options
author | hamaji@chromium.org <hamaji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-28 09:46:00 +0000 |
---|---|---|
committer | hamaji@chromium.org <hamaji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-28 09:46:00 +0000 |
commit | 4ebf5d12a9da2fe381b5bc6e53eba76a2abed8d4 (patch) | |
tree | 8709d2a1c9b6afcff614c4d58b774cb2ba89ff31 | |
parent | fae8f2508f0927d4099fa78be85f089bae55575c (diff) | |
download | chromium_src-4ebf5d12a9da2fe381b5bc6e53eba76a2abed8d4.zip chromium_src-4ebf5d12a9da2fe381b5bc6e53eba76a2abed8d4.tar.gz chromium_src-4ebf5d12a9da2fe381b5bc6e53eba76a2abed8d4.tar.bz2 |
Create constant variables kPointsPerInch and kPixelsPerInch and conversion functions.
BUG=47277
TEST=units_unittest.cc
Review URL: http://codereview.chromium.org/2877001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50968 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/renderer/mock_printer.cc | 4 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper.cc | 2 | ||||
-rw-r--r-- | chrome/renderer/print_web_view_helper_linux.cc | 15 | ||||
-rw-r--r-- | printing/pdf_ps_metafile_cairo.cc | 13 | ||||
-rw-r--r-- | printing/pdf_ps_metafile_cairo.h | 7 | ||||
-rw-r--r-- | printing/units.cc | 8 | ||||
-rw-r--r-- | printing/units.h | 14 | ||||
-rw-r--r-- | printing/units_unittest.cc | 8 |
8 files changed, 53 insertions, 18 deletions
diff --git a/chrome/renderer/mock_printer.cc b/chrome/renderer/mock_printer.cc index 235e056..78b7a78 100644 --- a/chrome/renderer/mock_printer.cc +++ b/chrome/renderer/mock_printer.cc @@ -9,12 +9,13 @@ #include "base/shared_memory.h" #include "chrome/common/render_messages.h" #include "ipc/ipc_message_utils.h" +#include "printing/units.h" #include "testing/gtest/include/gtest/gtest.h" MockPrinter::MockPrinter() : printable_width_(0), printable_height_(0), - dpi_(72), + dpi_(printing::kPointsPerInch), max_shrink_(2.0), min_shrink_(1.25), desired_dpi_(72), @@ -195,4 +196,3 @@ bool MockPrinter::SaveBitmap( int MockPrinter::CreateDocumentCookie() { return ++current_document_cookie_; } - diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc index 726ce2d..9133e38 100644 --- a/chrome/renderer/print_web_view_helper.cc +++ b/chrome/renderer/print_web_view_helper.cc @@ -37,7 +37,7 @@ PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( #if defined(OS_MACOSX) // On the Mac, the printable area is in points, don't do any scaling based // on dpi. - dpi = 72; + dpi = printing::kPointsPerInch; #endif // defined(OS_MACOSX) print_canvas_size_.set_width( printing::ConvertUnit(print_params.printable_size.width(), dpi, diff --git a/chrome/renderer/print_web_view_helper_linux.cc b/chrome/renderer/print_web_view_helper_linux.cc index 2a0e333..609f099 100644 --- a/chrome/renderer/print_web_view_helper_linux.cc +++ b/chrome/renderer/print_web_view_helper_linux.cc @@ -8,9 +8,11 @@ #include "base/logging.h" #include "chrome/common/render_messages.h" #include "printing/native_metafile.h" +#include "printing/units.h" #include "skia/ext/vector_canvas.h" #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" +using printing::NativeMetafile; using WebKit::WebFrame; void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { @@ -23,14 +25,11 @@ void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) { // For testing purpose, we hard-coded printing parameters here. // The paper size is US Letter (8.5 in. by 11 in.). - // Using default margins: - // Left = 0.25 in. - // Right = 0.25 in. - // Top = 0.25 in. - // Bottom = 0.56 in. - const int kDPI = 72; - const int kWidth = static_cast<int>((8.5-0.25-0.25) * kDPI); - const int kHeight = static_cast<int>((11-0.25-0.56) * kDPI); + const int kDPI = printing::kPointsPerInch; + const int kWidth = static_cast<int>( + 8.5 * kDPI - NativeMetafile::kLeftMargin - NativeMetafile::kRightMargin); + const int kHeight = static_cast<int>( + 11 * kDPI - NativeMetafile::kTopMargin - NativeMetafile::kBottomMargin); ViewMsg_Print_Params default_settings; default_settings.printable_size = gfx::Size(kWidth, kHeight); default_settings.dpi = kDPI; diff --git a/printing/pdf_ps_metafile_cairo.cc b/printing/pdf_ps_metafile_cairo.cc index b87992f..96b72c5 100644 --- a/printing/pdf_ps_metafile_cairo.cc +++ b/printing/pdf_ps_metafile_cairo.cc @@ -14,17 +14,11 @@ #include "base/file_descriptor_posix.h" #include "base/file_util.h" #include "base/logging.h" +#include "printing/units.h" #include "skia/ext/vector_platform_device_linux.h" namespace { -// The hardcoded margins, in points. These values are based on 72 dpi, -// with approximately 0.25 margins on top, left, and right, and 0.56 on bottom. -const double kTopMargin = 0.25 * 72.0; -const double kBottomMargin = 0.56 * 72.0; -const double kLeftMargin = 0.25 * 72.0; -const double kRightMargin = 0.25 * 72.0; - // Tests if |surface| is valid. bool IsSurfaceValid(cairo_surface_t* surface) { return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS; @@ -249,4 +243,9 @@ void PdfPsMetafile::CleanUpAll() { skia::VectorPlatformDevice::ClearFontCache(); } +const double PdfPsMetafile::kTopMargin = 0.25 * printing::kPointsPerInch; +const double PdfPsMetafile::kBottomMargin = 0.56 * printing::kPointsPerInch; +const double PdfPsMetafile::kLeftMargin = 0.25 * printing::kPointsPerInch; +const double PdfPsMetafile::kRightMargin = 0.25 * printing::kPointsPerInch; + } // namespace printing diff --git a/printing/pdf_ps_metafile_cairo.h b/printing/pdf_ps_metafile_cairo.h index a5ea5be..8027188 100644 --- a/printing/pdf_ps_metafile_cairo.h +++ b/printing/pdf_ps_metafile_cairo.h @@ -77,6 +77,13 @@ class PdfPsMetafile { // This function should ONLY be called after PDF/PS file is closed. bool SaveTo(const base::FileDescriptor& fd) const; + // The hardcoded margins, in points. These values are based on 72 dpi, + // with 0.25 margins on top, left, and right, and 0.56 on bottom. + static const double kTopMargin; + static const double kRightMargin; + static const double kBottomMargin; + static const double kLeftMargin; + private: // Cleans up all resources. void CleanUpAll(); diff --git a/printing/units.cc b/printing/units.cc index 5f543fe..976fa80 100644 --- a/printing/units.cc +++ b/printing/units.cc @@ -39,4 +39,12 @@ int ConvertHundredThousanthMeterToMilliInch(int cmm) { return ConvertUnit(cmm, 254, 100); } +int ConvertPixelsToPoint(int pixels) { + return ConvertUnit(pixels, kPixelsPerInch, kPointsPerInch); +} + +double ConvertPixelsToPointDouble(double pixels) { + return ConvertUnitDouble(pixels, kPixelsPerInch, kPointsPerInch); +} + } // namespace printing diff --git a/printing/units.h b/printing/units.h index d23e268..cc61241 100644 --- a/printing/units.h +++ b/printing/units.h @@ -10,6 +10,14 @@ namespace printing { // Length of a thousanth of inches in 0.01mm unit. const int kHundrethsMMPerInch = 2540; +// Length of an inch in CSS's 1pt unit. +// http://dev.w3.org/csswg/css3-values/#absolute-length-units-cm-mm.-in-pt-pc +const int kPointsPerInch = 72; + +// Length of an inch in CSS's 1px unit. +// http://dev.w3.org/csswg/css3-values/#the-px-unit +const int kPixelsPerInch = 96; + // Converts from one unit system to another using integer arithmetics. int ConvertUnit(int value, int old_unit, int new_unit); @@ -22,6 +30,12 @@ int ConvertMilliInchToHundredThousanthMeter(int milli_inch); // Converts from 0.00001 meter unit to 0.001 inch. int ConvertHundredThousanthMeterToMilliInch(int cmm); +// Converts from 1 pixel to 1 point using integers. +int ConvertPixelsToPoint(int pixels); + +// Converts from 1 pixel to 1 point using doubles. +double ConvertPixelsToPointDouble(double pixels); + } // namespace printing #endif // PRINTING_UNITS_H_ diff --git a/printing/units_unittest.cc b/printing/units_unittest.cc index b2db567..82f9acb 100644 --- a/printing/units_unittest.cc +++ b/printing/units_unittest.cc @@ -50,4 +50,12 @@ TEST(UnitsTest, Convertions) { EXPECT_EQ(-1000, ConvertHundredThousanthMeterToMilliInch(-kHundrethsMMPerInch)); EXPECT_EQ(0, ConvertHundredThousanthMeterToMilliInch(0)); + + EXPECT_EQ(8.25, ConvertPixelsToPointDouble(11.0)); + // Round down. + EXPECT_EQ(8, ConvertPixelsToPoint(11)); + EXPECT_EQ(7.5, ConvertPixelsToPointDouble(10.0)); + // Round up. + EXPECT_EQ(8, ConvertPixelsToPoint(10)); + EXPECT_EQ(0, ConvertPixelsToPoint(0)); } |