From 48091b01b6007f5bec158bff3f9a9d127ab70f4f Mon Sep 17 00:00:00 2001 From: "jhawkins@chromium.org" Date: Thu, 21 Oct 2010 02:38:11 +0000 Subject: Printing: Split off Win/Mac implementations of the Image class into their own files. BUG=none TEST=none Review URL: http://codereview.chromium.org/3966002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63316 0039d316-1c4b-4281-b951-d872f2087c98 --- printing/image.cc | 116 +----------------------------------------------- printing/image_cairo.cc | 16 +++++++ printing/image_mac.cc | 42 ++++++++++++++++++ printing/image_win.cc | 88 ++++++++++++++++++++++++++++++++++++ printing/printing.gyp | 3 ++ 5 files changed, 151 insertions(+), 114 deletions(-) create mode 100644 printing/image_cairo.cc create mode 100644 printing/image_mac.cc create mode 100644 printing/image_win.cc (limited to 'printing') diff --git a/printing/image.cc b/printing/image.cc index 27822cf..dd52380 100644 --- a/printing/image.cc +++ b/printing/image.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// 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. @@ -8,55 +8,7 @@ #include "base/md5.h" #include "base/string_number_conversions.h" #include "gfx/codec/png_codec.h" -#include "gfx/rect.h" -#include "skia/ext/platform_device.h" - -#if defined(OS_WIN) -#include "gfx/gdi_util.h" // EMF support -#elif defined(OS_MACOSX) -#include -#include "base/mac/scoped_cftyperef.h" -#endif - -namespace { - -// A simple class which temporarily overrides system settings. -// The bitmap image rendered via the PlayEnhMetaFile() function depends on -// some system settings. -// As a workaround for such dependency, this class saves the system settings -// and changes them. This class also restore the saved settings in its -// destructor. -class DisableFontSmoothing { - public: - explicit DisableFontSmoothing(bool disable) : enable_again_(false) { - if (disable) { -#if defined(OS_WIN) - BOOL enabled; - if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && - enabled) { - if (SystemParametersInfo(SPI_SETFONTSMOOTHING, FALSE, NULL, 0)) - enable_again_ = true; - } -#endif - } - } - - ~DisableFontSmoothing() { - if (enable_again_) { -#if defined(OS_WIN) - BOOL result = SystemParametersInfo(SPI_SETFONTSMOOTHING, TRUE, NULL, 0); - DCHECK(result); -#endif - } - } - - private: - bool enable_again_; - - DISALLOW_COPY_AND_ASSIGN(DisableFontSmoothing); -}; - -} // namespace +#include "third_party/skia/include/core/SkColor.h" namespace printing { @@ -192,73 +144,9 @@ bool Image::LoadPng(const std::string& compressed) { bool Image::LoadMetafile(const std::string& data) { DCHECK(!data.empty()); -#if defined(OS_WIN) || defined(OS_MACOSX) NativeMetafile metafile; metafile.Init(data.data(), data.size()); return LoadMetafile(metafile); -#else - NOTIMPLEMENTED(); - return false; -#endif -} - -bool Image::LoadMetafile(const NativeMetafile& metafile) { -#if defined(OS_WIN) - gfx::Rect rect(metafile.GetBounds()); - DisableFontSmoothing disable_in_this_scope(true); - // Create a temporary HDC and bitmap to retrieve the rendered data. - HDC hdc = CreateCompatibleDC(NULL); - BITMAPV4HEADER hdr; - DCHECK_EQ(rect.x(), 0); - DCHECK_EQ(rect.y(), 0); - DCHECK_GE(rect.width(), 0); // Metafile could be empty. - DCHECK_GE(rect.height(), 0); - if (rect.width() > 0 && rect.height() > 0) { - size_ = rect.size(); - gfx::CreateBitmapV4Header(rect.width(), rect.height(), &hdr); - void* bits; - HBITMAP bitmap = CreateDIBSection(hdc, - reinterpret_cast(&hdr), 0, - &bits, NULL, 0); - DCHECK(bitmap); - DCHECK(SelectObject(hdc, bitmap)); - skia::PlatformDevice::InitializeDC(hdc); - bool success = metafile.Playback(hdc, NULL); - row_length_ = size_.width() * sizeof(uint32); - size_t bytes = row_length_ * size_.height(); - DCHECK(bytes); - data_.resize(bytes); - memcpy(&*data_.begin(), bits, bytes); - DeleteDC(hdc); - DeleteObject(bitmap); - return success; - } -#elif defined(OS_MACOSX) - // The printing system uses single-page metafiles (page indexes are 1-based). - const unsigned int page_number = 1; - gfx::Rect rect(metafile.GetPageBounds(page_number)); - if (rect.width() > 0 && rect.height() > 0) { - size_ = rect.size(); - row_length_ = size_.width() * sizeof(uint32); - size_t bytes = row_length_ * size_.height(); - DCHECK(bytes); - data_.resize(bytes); - base::mac::ScopedCFTypeRef color_space( - CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); - base::mac::ScopedCFTypeRef bitmap_context( - CGBitmapContextCreate(&*data_.begin(), size_.width(), size_.height(), - 8, row_length_, color_space, - kCGImageAlphaPremultipliedLast)); - DCHECK(bitmap_context.get()); - metafile.RenderPage(page_number, bitmap_context, - CGRectMake(0, 0, size_.width(), size_.height()), - true, false, false, false); - } -#else - NOTIMPLEMENTED(); -#endif - - return false; } } // namespace printing diff --git a/printing/image_cairo.cc b/printing/image_cairo.cc new file mode 100644 index 0000000..d19d57e --- /dev/null +++ b/printing/image_cairo.cc @@ -0,0 +1,16 @@ +// 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/image.h" + +#include "base/logging.h" + +namespace printing { + +bool Image::LoadMetafile(const NativeMetafile& metafile) { + NOTIMPLEMENTED(); + return false; +} + +} // namespace printing diff --git a/printing/image_mac.cc b/printing/image_mac.cc new file mode 100644 index 0000000..8b3d638 --- /dev/null +++ b/printing/image_mac.cc @@ -0,0 +1,42 @@ +// 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/image.h" + +#include + +#include "base/mac/scoped_cftyperef.h" +#include "gfx/rect.h" + +namespace printing { + +bool Image::LoadMetafile(const NativeMetafile& metafile) { + // The printing system uses single-page metafiles (page indexes are 1-based). + const unsigned int page_number = 1; + gfx::Rect rect(metafile.GetPageBounds(page_number)); + if (rect.width() < 1 || rect.height() < 1) + return false; + + size_ = rect.size(); + row_length_ = size_.width() * sizeof(uint32); + size_t bytes = row_length_ * size_.height(); + DCHECK(bytes); + + data_.resize(bytes); + base::mac::ScopedCFTypeRef color_space( + CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); + base::mac::ScopedCFTypeRef bitmap_context( + CGBitmapContextCreate(&*data_.begin(), size_.width(), size_.height(), + 8, row_length_, color_space, + kCGImageAlphaPremultipliedLast)); + DCHECK(bitmap_context.get()); + + metafile.RenderPage(page_number, bitmap_context, + CGRectMake(0, 0, size_.width(), size_.height()), + true, false, false, false); + + return true; +} + +} // namespace printing diff --git a/printing/image_win.cc b/printing/image_win.cc new file mode 100644 index 0000000..e8bd368 --- /dev/null +++ b/printing/image_win.cc @@ -0,0 +1,88 @@ +// 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/image.h" + +#include "gfx/gdi_util.h" // EMF support +#include "gfx/rect.h" +#include "skia/ext/platform_device.h" + +namespace { + +// A simple class which temporarily overrides system settings. +// The bitmap image rendered via the PlayEnhMetaFile() function depends on +// some system settings. +// As a workaround for such dependency, this class saves the system settings +// and changes them. This class also restore the saved settings in its +// destructor. +class DisableFontSmoothing { + public: + explicit DisableFontSmoothing() : enable_again_(false) { + BOOL enabled; + if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && + enabled) { + if (SystemParametersInfo(SPI_SETFONTSMOOTHING, FALSE, NULL, 0)) + enable_again_ = true; + } + } + + ~DisableFontSmoothing() { + if (enable_again_) { + BOOL result = SystemParametersInfo(SPI_SETFONTSMOOTHING, TRUE, NULL, 0); + DCHECK(result); + } + } + + private: + bool enable_again_; + + DISALLOW_COPY_AND_ASSIGN(DisableFontSmoothing); +}; + +} // namespace + +namespace printing { + +bool Image::LoadMetafile(const NativeMetafile& metafile) { + gfx::Rect rect(metafile.GetBounds()); + DisableFontSmoothing disable_in_this_scope; + + // Create a temporary HDC and bitmap to retrieve the rendered data. + HDC hdc = CreateCompatibleDC(NULL); + BITMAPV4HEADER hdr; + DCHECK_EQ(rect.x(), 0); + DCHECK_EQ(rect.y(), 0); + DCHECK_GE(rect.width(), 0); // Metafile could be empty. + DCHECK_GE(rect.height(), 0); + + if (rect.width() < 1 || rect.height() < 1) + return false; + + size_ = rect.size(); + gfx::CreateBitmapV4Header(rect.width(), rect.height(), &hdr); + void* bits; + HBITMAP bitmap = CreateDIBSection(hdc, + reinterpret_cast(&hdr), 0, + &bits, NULL, 0); + DCHECK(bitmap); + DCHECK(SelectObject(hdc, bitmap)); + + skia::PlatformDevice::InitializeDC(hdc); + + bool success = metafile.Playback(hdc, NULL); + + row_length_ = size_.width() * sizeof(uint32); + size_t bytes = row_length_ * size_.height(); + DCHECK(bytes); + + data_.resize(bytes); + memcpy(&*data_.begin(), bits, bytes); + + DeleteDC(hdc); + DeleteObject(bitmap); + + return success; +} + +} // namespace printing diff --git a/printing/printing.gyp b/printing/printing.gyp index 3b37771..2970a90 100644 --- a/printing/printing.gyp +++ b/printing/printing.gyp @@ -27,6 +27,9 @@ 'emf_win.cc', 'emf_win.h', 'image.cc', + 'image_cairo.cc', + 'image_mac.cc', + 'image_win.cc', 'image.h', 'native_metafile.h', 'page_number.cc', -- cgit v1.1