From 7d748990b39550c77aa914ba6846885dd7352e10 Mon Sep 17 00:00:00 2001 From: "vandebo@chromium.org" Date: Mon, 11 Apr 2011 21:54:06 +0000 Subject: Connect the right metafiles for print preview on Linux and Windows. + Remove the NativeMetafileFactory since we can't just use preview flag. + Update each Metafile constructor site to use PreviewMetafile or NativeMetafileImpl. + Fix misc. problems blocking pdf generation on Windows. + Rename the metafile interface. BUG=NONE TEST=NONE Review URL: http://codereview.chromium.org/6826027 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81161 0039d316-1c4b-4281-b951-d872f2087c98 --- printing/emf_win.h | 6 +- printing/image.cc | 14 ++- printing/image.h | 9 +- printing/image_cairo.cc | 4 +- printing/image_mac.cc | 5 +- printing/image_win.cc | 5 +- printing/metafile.h | 151 ++++++++++++++++++++++++++++++ printing/metafile_impl.h | 33 +++++++ printing/metafile_skia_wrapper.cc | 53 +++++++++++ printing/metafile_skia_wrapper.h | 34 +++++++ printing/native_metafile.h | 152 ------------------------------- printing/native_metafile_factory.cc | 47 ---------- printing/native_metafile_factory.h | 42 --------- printing/native_metafile_skia_wrapper.cc | 55 ----------- printing/native_metafile_skia_wrapper.h | 34 ------- printing/pdf_metafile_cairo_linux.h | 13 +-- printing/pdf_metafile_cg_mac.h | 14 +-- printing/pdf_metafile_skia.h | 15 +-- printing/print_dialog_gtk_interface.h | 4 +- printing/printed_document.cc | 10 +- printing/printed_document.h | 4 +- printing/printed_document_cairo.cc | 2 +- printing/printed_document_mac.cc | 6 +- printing/printed_document_win.cc | 2 +- printing/printed_page.cc | 10 +- printing/printed_page.h | 8 +- printing/printing.gyp | 9 +- printing/printing_context_cairo.cc | 3 +- printing/printing_context_cairo.h | 7 +- 29 files changed, 338 insertions(+), 413 deletions(-) create mode 100644 printing/metafile.h create mode 100644 printing/metafile_impl.h create mode 100644 printing/metafile_skia_wrapper.cc create mode 100644 printing/metafile_skia_wrapper.h delete mode 100644 printing/native_metafile.h delete mode 100644 printing/native_metafile_factory.cc delete mode 100644 printing/native_metafile_factory.h delete mode 100644 printing/native_metafile_skia_wrapper.cc delete mode 100644 printing/native_metafile_skia_wrapper.h (limited to 'printing') diff --git a/printing/emf_win.h b/printing/emf_win.h index ecc1028..4d8302c 100644 --- a/printing/emf_win.h +++ b/printing/emf_win.h @@ -10,7 +10,7 @@ #include "base/basictypes.h" #include "base/gtest_prod_util.h" -#include "printing/native_metafile.h" +#include "printing/metafile.h" class FilePath; @@ -23,7 +23,7 @@ class Size; namespace printing { // Simple wrapper class that manage an EMF data stream and its virtual HDC. -class Emf : public NativeMetafile { +class Emf : public Metafile { public: class Record; class Enumerator; @@ -41,7 +41,7 @@ class Emf : public NativeMetafile { // Initializes the Emf with the data in |metafile_path|. virtual bool InitFromFile(const FilePath& metafile_path); - // NativeMetafile methods. + // Metafile methods. virtual bool Init(); virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size); diff --git a/printing/image.cc b/printing/image.cc index a7ae67d..062ac89 100644 --- a/printing/image.cc +++ b/printing/image.cc @@ -6,9 +6,9 @@ #include "base/file_util.h" #include "base/md5.h" -#include "base/memory/scoped_ptr.h" #include "base/string_number_conversions.h" -#include "printing/native_metafile_factory.h" +#include "printing/metafile.h" +#include "printing/metafile_impl.h" #include "third_party/skia/include/core/SkColor.h" #include "ui/gfx/codec/png_codec.h" @@ -34,7 +34,7 @@ Image::Image(const FilePath& path) } } -Image::Image(const NativeMetafile& metafile) +Image::Image(const Metafile& metafile) : row_length_(0), ignore_alpha_(true) { LoadMetafile(metafile); @@ -146,12 +146,10 @@ bool Image::LoadPng(const std::string& compressed) { bool Image::LoadMetafile(const std::string& data) { DCHECK(!data.empty()); - scoped_ptr metafile( - printing::NativeMetafileFactory::CreateFromData(data.data(), - data.size())); - if(!metafile.get()) + printing::NativeMetafile metafile; + if (!metafile.InitFromData(data.data(), data.size())) return false; - return LoadMetafile(*metafile); + return LoadMetafile(metafile); } } // namespace printing diff --git a/printing/image.h b/printing/image.h index 5d84c8d..3bed218 100644 --- a/printing/image.h +++ b/printing/image.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -10,13 +10,14 @@ #include "base/basictypes.h" #include "base/logging.h" -#include "printing/native_metafile.h" #include "ui/gfx/size.h" class FilePath; namespace printing { +class Metafile; + // Lightweight raw-bitmap management. The image, once initialized, is immutable. // The main purpose is testing image contents. class Image { @@ -28,7 +29,7 @@ class Image { // Creates the image from the metafile. Deduces bounds based on bounds in // metafile. If loading fails size().IsEmpty() will be true. - explicit Image(const NativeMetafile& metafile); + explicit Image(const Metafile& metafile); // Copy constructor. explicit Image(const Image& image); @@ -73,7 +74,7 @@ class Image { bool LoadMetafile(const std::string& data); - bool LoadMetafile(const NativeMetafile& metafile); + bool LoadMetafile(const Metafile& metafile); // Pixel dimensions of the image. gfx::Size size_; diff --git a/printing/image_cairo.cc b/printing/image_cairo.cc index d19d57e..694fd7d 100644 --- a/printing/image_cairo.cc +++ b/printing/image_cairo.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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,7 +8,7 @@ namespace printing { -bool Image::LoadMetafile(const NativeMetafile& metafile) { +bool Image::LoadMetafile(const Metafile& metafile) { NOTIMPLEMENTED(); return false; } diff --git a/printing/image_mac.cc b/printing/image_mac.cc index b11757d..1ed9e0c 100644 --- a/printing/image_mac.cc +++ b/printing/image_mac.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -7,11 +7,12 @@ #include #include "base/mac/scoped_cftyperef.h" +#include "printing/metafile.h" #include "ui/gfx/rect.h" namespace printing { -bool Image::LoadMetafile(const NativeMetafile& metafile) { +bool Image::LoadMetafile(const Metafile& 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)); diff --git a/printing/image_win.cc b/printing/image_win.cc index be089dc..c9ad1fe 100644 --- a/printing/image_win.cc +++ b/printing/image_win.cc @@ -1,9 +1,10 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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 "printing/metafile.h" #include "skia/ext/platform_device.h" #include "ui/gfx/gdi_util.h" // EMF support #include "ui/gfx/rect.h" @@ -44,7 +45,7 @@ class DisableFontSmoothing { namespace printing { -bool Image::LoadMetafile(const NativeMetafile& metafile) { +bool Image::LoadMetafile(const Metafile& metafile) { gfx::Rect rect(metafile.GetPageBounds(1)); DisableFontSmoothing disable_in_this_scope; diff --git a/printing/metafile.h b/printing/metafile.h new file mode 100644 index 0000000..264c8fe --- /dev/null +++ b/printing/metafile.h @@ -0,0 +1,151 @@ +// Copyright (c) 2011 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. + +#ifndef PRINTING_METAFILE_H_ +#define PRINTING_METAFILE_H_ + +#include "base/basictypes.h" +#include "build/build_config.h" +#include "ui/gfx/native_widget_types.h" + +#if defined(OS_WIN) +#include +#elif defined(OS_MACOSX) +#include +#include +#include "base/mac/scoped_cftyperef.h" +#endif + +class FilePath; + +namespace gfx { +class Point; +class Rect; +class Size; +} + +namespace skia { +class PlatformDevice; +} + +#if defined(OS_CHROMEOS) +namespace base { +class FileDescriptor; +} +#endif + +namespace printing { + +// This class creates a graphics context that renders into a data stream +// (usually PDF or EMF). +class Metafile { + public: + virtual ~Metafile() {} + + // Initializes a fresh new metafile for rendering. Returns false on failure. + // Note: It should only be called from within the renderer process to allocate + // rendering resources. + virtual bool Init() = 0; + + // Initializes the metafile with the data in |src_buffer|. Returns true + // on success. + // Note: It should only be called from within the browser process. + virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0; + + // This method calls StartPage and then returns an appropriate + // VectorPlatformDevice implementation bound to the context created by + // StartPage or NULL on error. + virtual skia::PlatformDevice* StartPageForVectorCanvas( + const gfx::Size& page_size, const gfx::Point& content_origin, + const float& scale_factor) = 0; + + // Prepares a context for rendering a new page at the specified + // |content_origin| with the given |page_size| and a |scale_factor| to use for + // the drawing. The units are in points (=1/72 in). Returns true on success. + virtual bool StartPage(const gfx::Size& page_size, + const gfx::Point& content_origin, + const float& scale_factor) = 0; + + // Closes the current page and destroys the context used in rendering that + // page. The results of current page will be appended into the underlying + // data stream. Returns true on success. + virtual bool FinishPage() = 0; + + // Closes the metafile. No further rendering is allowed (the current page + // is implicitly closed). + virtual bool FinishDocument() = 0; + + // Returns the size of the underlying data stream. Only valid after Close() + // has been called. + virtual uint32 GetDataSize() const = 0; + + // Copies the first |dst_buffer_size| bytes of the underlying data stream into + // |dst_buffer|. This function should ONLY be called after Close() is invoked. + // Returns true if the copy succeeds. + virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0; + + // Saves the underlying data to the given file. This function should ONLY be + // called after the metafile is closed. Returns true if writing succeeded. + virtual bool SaveTo(const FilePath& file_path) const = 0; + + // Returns the bounds of the given page. Pages use a 1-based index. + virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0; + virtual unsigned int GetPageCount() const = 0; + + // Get the context for rendering to the PDF. + virtual gfx::NativeDrawingContext context() const = 0; + +#if defined(OS_WIN) + // "Plays" the EMF buffer in a HDC. It is the same effect as calling the + // original GDI function that were called when recording the EMF. |rect| is in + // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds + // are used. + // Note: Windows has been known to have stack buffer overflow in its GDI + // functions, whether used directly or indirectly through precompiled EMF + // data. We have to accept the risk here. Since it is used only for printing, + // it requires user intervention. + virtual bool Playback(gfx::NativeDrawingContext hdc, + const RECT* rect) const = 0; + + // The slow version of Playback(). It enumerates all the records and play them + // back in the HDC. The trick is that it skip over the records known to have + // issue with some printers. See Emf::Record::SafePlayback implementation for + // details. + virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0; + + virtual HENHMETAFILE emf() const = 0; +#elif defined(OS_MACOSX) + // Renders the given page into |rect| in the given context. + // Pages use a 1-based index. The rendering uses the following arguments + // to determine scaling and translation factors. + // |shrink_to_fit| specifies whether the output should be shrunk to fit the + // supplied |rect| if the page size is larger than |rect| in any dimension. + // If this is false, parts of the PDF page that lie outside the bounds will be + // clipped. + // |stretch_to_fit| specifies whether the output should be stretched to fit + // the supplied bounds if the page size is smaller than |rect| in all + // dimensions. + // |center_horizontally| specifies whether the final image (after any scaling + // is done) should be centered horizontally within the given |rect|. + // |center_vertically| specifies whether the final image (after any scaling + // is done) should be centered vertically within the given |rect|. + // Note that all scaling preserves the original aspect ratio of the page. + virtual bool RenderPage(unsigned int page_number, + gfx::NativeDrawingContext context, + const CGRect rect, + bool shrink_to_fit, + bool stretch_to_fit, + bool center_horizontally, + bool center_vertically) const = 0; +#elif defined(OS_CHROMEOS) + // Saves the underlying data to the file associated with fd. This function + // should ONLY be called after the metafile is closed. + // Returns true if writing succeeded. + virtual bool SaveToFD(const base::FileDescriptor& fd) const = 0; +#endif // if defined(OS_CHROMEOS) +}; + +} // namespace printing + +#endif // PRINTING_METAFILE_H_ diff --git a/printing/metafile_impl.h b/printing/metafile_impl.h new file mode 100644 index 0000000..d25481d --- /dev/null +++ b/printing/metafile_impl.h @@ -0,0 +1,33 @@ +// Copyright (c) 2011 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. + +#ifndef PRINTING_METAFILE_IMPL_H_ +#define PRINTING_METAFILE_IMPL_H_ + +#if defined(OS_WIN) +#include "printing/emf_win.h" +#include "printing/pdf_metafile_skia.h" +#elif defined(OS_MACOSX) +#include "printing/pdf_metafile_cg_mac.h" +#elif defined(OS_POSIX) +#include "printing/pdf_metafile_cairo_linux.h" +#include "printing/pdf_metafile_skia.h" +#endif + +namespace printing { + +#if defined(OS_WIN) +typedef Emf NativeMetafile; +typedef PdfMetafileSkia PreviewMetafile; +#elif defined(OS_MACOSX) +typedef PdfMetafileCg NativeMetafile; +typedef PdfMetafileCg PreviewMetafile; +#elif defined(OS_POSIX) +typedef PdfMetafileCairo NativeMetafile; +typedef PdfMetafileSkia PreviewMetafile; +#endif + +} // namespace printing + +#endif // PRINTING_METAFILE_IMPL_H_ diff --git a/printing/metafile_skia_wrapper.cc b/printing/metafile_skia_wrapper.cc new file mode 100644 index 0000000..22b58c8 --- /dev/null +++ b/printing/metafile_skia_wrapper.cc @@ -0,0 +1,53 @@ +// Copyright (c) 2011 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 "base/logging.h" +#include "printing/metafile_skia_wrapper.h" +#include "third_party/skia/include/core/SkCanvas.h" +#include "third_party/skia/include/core/SkDevice.h" +#include "third_party/skia/include/core/SkMetaData.h" + +namespace printing { + +namespace { + +static const char* kMetafileKey = "CrMetafile"; + +SkMetaData& getMetaData(SkCanvas* canvas) { + DCHECK(canvas != NULL); + + SkDevice* device = canvas->getDevice(); + DCHECK(device != NULL); + return device->getMetaData(); +} + +} // namespace + +// static +void MetafileSkiaWrapper::SetMetafileOnCanvas(SkCanvas* canvas, + Metafile* metafile) { + MetafileSkiaWrapper* wrapper = NULL; + if (metafile) + wrapper = new MetafileSkiaWrapper(metafile); + + SkMetaData& meta = getMetaData(canvas); + meta.setRefCnt(kMetafileKey, wrapper); + SkSafeUnref(wrapper); +} + +// static +Metafile* MetafileSkiaWrapper::GetMetafileFromCanvas(SkCanvas* canvas) { + SkMetaData& meta = getMetaData(canvas); + SkRefCnt* value; + if (!meta.findRefCnt(kMetafileKey, &value) || !value) + return NULL; + + return static_cast(value)->metafile_; +} + +MetafileSkiaWrapper::MetafileSkiaWrapper(Metafile* metafile) + : metafile_(metafile) { +} + +} // namespace printing diff --git a/printing/metafile_skia_wrapper.h b/printing/metafile_skia_wrapper.h new file mode 100644 index 0000000..751179f --- /dev/null +++ b/printing/metafile_skia_wrapper.h @@ -0,0 +1,34 @@ +// Copyright (c) 2011 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. + +#ifndef PRINTING_METAFILE_SKIA_WRAPPER_H_ +#define PRINTING_METAFILE_SKIA_WRAPPER_H_ + +#include "third_party/skia/include/core/SkRefCnt.h" + +class SkCanvas; + +namespace printing { + +class Metafile; + +// A wrapper class with static methods to set and retrieve a Metafile +// on an SkCanvas. The ownership of the metafile is not affected and it +// is the caller's responsibility to ensure that the metafile remains valid +// as long as the canvas. +class MetafileSkiaWrapper : public SkRefCnt { + public: + static void SetMetafileOnCanvas(SkCanvas* canvas, Metafile* metafile); + + static Metafile* GetMetafileFromCanvas(SkCanvas* canvas); + + private: + explicit MetafileSkiaWrapper(Metafile* metafile); + + Metafile* metafile_; +}; + +} // namespace printing + +#endif // PRINTING_METAFILE_SKIA_WRAPPER_H_ diff --git a/printing/native_metafile.h b/printing/native_metafile.h deleted file mode 100644 index 8bb9651..0000000 --- a/printing/native_metafile.h +++ /dev/null @@ -1,152 +0,0 @@ -// Copyright (c) 2011 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. - -#ifndef PRINTING_NATIVE_METAFILE_H_ -#define PRINTING_NATIVE_METAFILE_H_ - -#include "base/basictypes.h" -#include "build/build_config.h" -#include "ui/gfx/native_widget_types.h" - -#if defined(OS_WIN) -#include -#include -#elif defined(OS_MACOSX) -#include -#include -#include "base/mac/scoped_cftyperef.h" -#endif - -class FilePath; - -namespace gfx { -class Point; -class Rect; -class Size; -} - -namespace skia { -class PlatformDevice; -} - -#if defined(OS_CHROMEOS) -namespace base { -class FileDescriptor; -} -#endif - -namespace printing { - -// This class creates a graphics context that renders into a data stream -// (usually PDF or EMF). -class NativeMetafile { - public: - virtual ~NativeMetafile() {} - - // Initializes a fresh new metafile for rendering. Returns false on failure. - // Note: It should only be called from within the renderer process to allocate - // rendering resources. - virtual bool Init() = 0; - - // Initializes the metafile with the data in |src_buffer|. Returns true - // on success. - // Note: It should only be called from within the browser process. - virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size) = 0; - - // This method calls StartPage and then returns an appropriate - // VectorPlatformDevice implementation bound to the context created by - // StartPage or NULL on error. - virtual skia::PlatformDevice* StartPageForVectorCanvas( - const gfx::Size& page_size, const gfx::Point& content_origin, - const float& scale_factor) = 0; - - // Prepares a context for rendering a new page at the specified - // |content_origin| with the given |page_size| and a |scale_factor| to use for - // the drawing. The units are in points (=1/72 in). Returns true on success. - virtual bool StartPage(const gfx::Size& page_size, - const gfx::Point& content_origin, - const float& scale_factor) = 0; - - // Closes the current page and destroys the context used in rendering that - // page. The results of current page will be appended into the underlying - // data stream. Returns true on success. - virtual bool FinishPage() = 0; - - // Closes the metafile. No further rendering is allowed (the current page - // is implicitly closed). - virtual bool FinishDocument() = 0; - - // Returns the size of the underlying data stream. Only valid after Close() - // has been called. - virtual uint32 GetDataSize() const = 0; - - // Copies the first |dst_buffer_size| bytes of the underlying data stream into - // |dst_buffer|. This function should ONLY be called after Close() is invoked. - // Returns true if the copy succeeds. - virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0; - - // Saves the underlying data to the given file. This function should ONLY be - // called after the metafile is closed. Returns true if writing succeeded. - virtual bool SaveTo(const FilePath& file_path) const = 0; - - // Returns the bounds of the given page. Pages use a 1-based index. - virtual gfx::Rect GetPageBounds(unsigned int page_number) const = 0; - virtual unsigned int GetPageCount() const = 0; - - // Get the context for rendering to the PDF. - virtual gfx::NativeDrawingContext context() const = 0; - -#if defined(OS_WIN) - // "Plays" the EMF buffer in a HDC. It is the same effect as calling the - // original GDI function that were called when recording the EMF. |rect| is in - // "logical units" and is optional. If |rect| is NULL, the natural EMF bounds - // are used. - // Note: Windows has been known to have stack buffer overflow in its GDI - // functions, whether used directly or indirectly through precompiled EMF - // data. We have to accept the risk here. Since it is used only for printing, - // it requires user intervention. - virtual bool Playback(gfx::NativeDrawingContext hdc, - const RECT* rect) const = 0; - - // The slow version of Playback(). It enumerates all the records and play them - // back in the HDC. The trick is that it skip over the records known to have - // issue with some printers. See Emf::Record::SafePlayback implementation for - // details. - virtual bool SafePlayback(gfx::NativeDrawingContext hdc) const = 0; - - virtual HENHMETAFILE emf() const = 0; -#elif defined(OS_MACOSX) - // Renders the given page into |rect| in the given context. - // Pages use a 1-based index. The rendering uses the following arguments - // to determine scaling and translation factors. - // |shrink_to_fit| specifies whether the output should be shrunk to fit the - // supplied |rect| if the page size is larger than |rect| in any dimension. - // If this is false, parts of the PDF page that lie outside the bounds will be - // clipped. - // |stretch_to_fit| specifies whether the output should be stretched to fit - // the supplied bounds if the page size is smaller than |rect| in all - // dimensions. - // |center_horizontally| specifies whether the final image (after any scaling - // is done) should be centered horizontally within the given |rect|. - // |center_vertically| specifies whether the final image (after any scaling - // is done) should be centered vertically within the given |rect|. - // Note that all scaling preserves the original aspect ratio of the page. - virtual bool RenderPage(unsigned int page_number, - gfx::NativeDrawingContext context, - const CGRect rect, - bool shrink_to_fit, - bool stretch_to_fit, - bool center_horizontally, - bool center_vertically) const = 0; -#elif defined(OS_CHROMEOS) - // Saves the underlying data to the file associated with fd. This function - // should ONLY be called after the metafile is closed. - // Returns true if writing succeeded. - virtual bool SaveToFD(const base::FileDescriptor& fd) const = 0; -#endif // if defined(OS_CHROMEOS) -}; - -} // namespace printing - -#endif // PRINTING_NATIVE_METAFILE_H_ diff --git a/printing/native_metafile_factory.cc b/printing/native_metafile_factory.cc deleted file mode 100644 index 5db823e..0000000 --- a/printing/native_metafile_factory.cc +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2011 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/native_metafile_factory.h" - -#include "base/memory/scoped_ptr.h" - -#if defined(OS_WIN) -#include "printing/emf_win.h" -#elif defined(OS_MACOSX) -#include "printing/pdf_metafile_cg_mac.h" -#elif defined(OS_POSIX) -#include "printing/pdf_metafile_cairo_linux.h" -#endif - -namespace printing { - -// static -NativeMetafile* NativeMetafileFactory::Create() { - scoped_ptr metafile(CreateNewMetafile()); - if (!metafile->Init()) - return NULL; - return metafile.release(); -} - -// static -NativeMetafile* NativeMetafileFactory::CreateFromData( - const void* src_buffer, uint32 src_buffer_size) { - scoped_ptr metafile(CreateNewMetafile()); - if (!metafile->InitFromData(src_buffer, src_buffer_size)) - return NULL; - return metafile.release(); -} - -// static -NativeMetafile* NativeMetafileFactory::CreateNewMetafile(){ -#if defined(OS_WIN) - return new printing::Emf; -#elif defined(OS_MACOSX) - return new printing::PdfMetafileCg; -#elif defined(OS_POSIX) - return new printing::PdfMetafileCairo; -#endif -} - -} // namespace printing diff --git a/printing/native_metafile_factory.h b/printing/native_metafile_factory.h deleted file mode 100644 index 610970f..0000000 --- a/printing/native_metafile_factory.h +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2011 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. - -#ifndef PRINTING_NATIVE_METAFILE_FACTORY_H_ -#define PRINTING_NATIVE_METAFILE_FACTORY_H_ - -#include "base/basictypes.h" -#include "printing/native_metafile.h" - -namespace printing { - -// Various printing contexts will be supported in the future (cairo, skia, emf). -// So this class returns the appropriate context depending on the platform and -// user preferences. -// (Note: For the moment there is only one option per platform.) -class NativeMetafileFactory { - public: - // This method returns a pointer to the appropriate NativeMetafile object - // according to the platform. The metafile is already initialized by invoking - // Init() on it. NULL is returned if Init() fails. - static printing::NativeMetafile* Create(); - - // This method returns a pointer to the appropriate NativeMetafile object - // according to the platform. The metafile is already initialized by invoking - // InitFromData(src_buffer,_buffer_size) on it. NULL is returned if - // InitiFromData() fails. - static printing::NativeMetafile* CreateFromData(const void* src_buffer, - uint32 src_buffer_size); - - private: - NativeMetafileFactory(); - - // Retrieves a new uninitialized metafile. - static NativeMetafile* CreateNewMetafile(); - - DISALLOW_COPY_AND_ASSIGN(NativeMetafileFactory); -}; - -} // namespace printing - -#endif // PRINTING_NATIVE_METAFILE_FACTORY_H_ diff --git a/printing/native_metafile_skia_wrapper.cc b/printing/native_metafile_skia_wrapper.cc deleted file mode 100644 index 38de9df..0000000 --- a/printing/native_metafile_skia_wrapper.cc +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (c) 2011 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 "base/logging.h" -#include "printing/native_metafile_skia_wrapper.h" -#include "third_party/skia/include/core/SkCanvas.h" -#include "third_party/skia/include/core/SkDevice.h" -#include "third_party/skia/include/core/SkMetaData.h" - -namespace printing { - -namespace { - -static const char* kNativeMetafileKey = "CrNativeMetafile"; - -SkMetaData& getMetaData(SkCanvas* canvas) { - DCHECK(canvas != NULL); - - SkDevice* device = canvas->getDevice(); - DCHECK(device != NULL); - return device->getMetaData(); -} - -} // namespace - - -// static -void NativeMetafileSkiaWrapper::SetMetafileOnCanvas(SkCanvas* canvas, - NativeMetafile* metafile) { - NativeMetafileSkiaWrapper* wrapper = NULL; - if (metafile) - wrapper = new NativeMetafileSkiaWrapper(metafile); - - SkMetaData& meta = getMetaData(canvas); - meta.setRefCnt(kNativeMetafileKey, wrapper); - SkSafeUnref(wrapper); -} - -// static -NativeMetafile* NativeMetafileSkiaWrapper::GetMetafileFromCanvas( - SkCanvas* canvas) { - SkMetaData& meta = getMetaData(canvas); - SkRefCnt* value; - if (!meta.findRefCnt(kNativeMetafileKey, &value) || !value) - return NULL; - - return static_cast(value)->metafile_; -} - -NativeMetafileSkiaWrapper::NativeMetafileSkiaWrapper(NativeMetafile* metafile) - : metafile_(metafile) { -} - -} // namespace printing diff --git a/printing/native_metafile_skia_wrapper.h b/printing/native_metafile_skia_wrapper.h deleted file mode 100644 index 7eb2887..0000000 --- a/printing/native_metafile_skia_wrapper.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) 2011 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. - -#ifndef PRINTING_NATIVE_METAFILE_SKIA_WRAPPER_H_ -#define PRINTING_NATIVE_METAFILE_SKIA_WRAPPER_H_ - -#include "third_party/skia/include/core/SkRefCnt.h" - -class SkCanvas; - -namespace printing { - -class NativeMetafile; - -// A wrapper class with static methods to set and retrieve a NativeMetafile -// on an SkCanvas. The ownership of the metafile is not affected and it -// is the caller's responsibility to ensure that the metafile remains valid -// as long as the canvas. -class NativeMetafileSkiaWrapper : public SkRefCnt { - public: - static void SetMetafileOnCanvas(SkCanvas* canvas, NativeMetafile* metafile); - - static NativeMetafile* GetMetafileFromCanvas(SkCanvas* canvas); - - private: - explicit NativeMetafileSkiaWrapper(NativeMetafile* metafile); - - NativeMetafile* metafile_; -}; - -} // namespace printing - -#endif // PRINTING_NATIVE_METAFILE_SKIA_WRAPPER_H_ diff --git a/printing/pdf_metafile_cairo_linux.h b/printing/pdf_metafile_cairo_linux.h index f3a80aa..fe00ff6 100644 --- a/printing/pdf_metafile_cairo_linux.h +++ b/printing/pdf_metafile_cairo_linux.h @@ -9,7 +9,7 @@ #include "base/basictypes.h" #include "base/gtest_prod_util.h" -#include "printing/native_metafile.h" +#include "printing/metafile.h" namespace gfx { class Point; @@ -23,11 +23,12 @@ namespace printing { // This class uses Cairo graphics library to generate PDF stream and stores // rendering results in a string buffer. -class PdfMetafileCairo : public NativeMetafile { +class PdfMetafileCairo : public Metafile { public: + PdfMetafileCairo(); virtual ~PdfMetafileCairo(); - // NativeMetafile methods. + // Metafile methods. virtual bool Init(); // Calling InitFromData() sets the data for this metafile and masks data @@ -58,13 +59,7 @@ class PdfMetafileCairo : public NativeMetafile { virtual bool SaveToFD(const base::FileDescriptor& fd) const; #endif // if defined(OS_CHROMEOS) - protected: - PdfMetafileCairo(); - private: - friend class NativeMetafileFactory; - FRIEND_TEST_ALL_PREFIXES(PdfMetafileCairoTest, Pdf); - // Cleans up all resources. void CleanUpAll(); diff --git a/printing/pdf_metafile_cg_mac.h b/printing/pdf_metafile_cg_mac.h index 0183246..616d448 100644 --- a/printing/pdf_metafile_cg_mac.h +++ b/printing/pdf_metafile_cg_mac.h @@ -11,7 +11,7 @@ #include "base/basictypes.h" #include "base/gtest_prod_util.h" #include "base/mac/scoped_cftyperef.h" -#include "printing/native_metafile.h" +#include "printing/metafile.h" class FilePath; @@ -24,12 +24,12 @@ class Point; namespace printing { // This class creates a graphics context that renders into a PDF data stream. -class PdfMetafileCg : public NativeMetafile { +class PdfMetafileCg : public Metafile { public: - + PdfMetafileCg(); virtual ~PdfMetafileCg(); - // NativeMetafile methods. + // Metafile methods. virtual bool Init(); virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size); @@ -64,13 +64,7 @@ class PdfMetafileCg : public NativeMetafile { bool center_horizontally, bool center_vertically) const; - protected: - PdfMetafileCg(); - private: - friend class NativeMetafileFactory; - FRIEND_TEST_ALL_PREFIXES(PdfMetafileCgTest, Pdf); - // Returns a CGPDFDocumentRef version of pdf_data_. CGPDFDocumentRef GetPDFDocument() const; diff --git a/printing/pdf_metafile_skia.h b/printing/pdf_metafile_skia.h index 4e8a003..515c3a8 100644 --- a/printing/pdf_metafile_skia.h +++ b/printing/pdf_metafile_skia.h @@ -9,7 +9,7 @@ #include "base/logging.h" #include "base/scoped_ptr.h" #include "build/build_config.h" -#include "printing/native_metafile.h" +#include "printing/metafile.h" #if defined(OS_WIN) #include @@ -20,11 +20,12 @@ namespace printing { struct PdfMetafileSkiaData; // This class uses Skia graphics library to generate a PDF document. -class PdfMetafileSkia : public NativeMetafile { +class PdfMetafileSkia : public Metafile { public: + PdfMetafileSkia(); virtual ~PdfMetafileSkia(); - // NativeMetafile interface + // Metafile methods. virtual bool Init(); virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size); @@ -55,15 +56,9 @@ class PdfMetafileSkia : public NativeMetafile { #endif // if defined(OS_WIN) #if defined(OS_CHROMEOS) - virtual bool SaveToFD(const base::FileDescriptor& fd) const = 0; + virtual bool SaveToFD(const base::FileDescriptor& fd) const; #endif // if defined(OS_CHROMEOS) - - protected: - PdfMetafileSkia(); - private: - friend class NativeMetafileFactory; - scoped_ptr data_; DISALLOW_COPY_AND_ASSIGN(PdfMetafileSkia); diff --git a/printing/print_dialog_gtk_interface.h b/printing/print_dialog_gtk_interface.h index 04198da..dab0c9c 100644 --- a/printing/print_dialog_gtk_interface.h +++ b/printing/print_dialog_gtk_interface.h @@ -10,6 +10,8 @@ namespace printing { +class Metafile; + // An interface for GTK printing dialogs. Classes that live outside of // printing/ can implement this interface and get threading requirements // correct without exposing those requirements to printing/. @@ -22,7 +24,7 @@ class PrintDialogGtkInterface { // Prints the document named |document_name| contained in |metafile|. // Called from the print worker thread. Once called, the // PrintDialogGtkInterface instance should not be reused. - virtual void PrintDocument(const NativeMetafile* metafile, + virtual void PrintDocument(const Metafile* metafile, const string16& document_name) = 0; // Same as AddRef/Release, but with different names since diff --git a/printing/printed_document.cc b/printing/printed_document.cc index 07eeec6d..5a85c1a 100644 --- a/printing/printed_document.cc +++ b/printing/printed_document.cc @@ -64,7 +64,7 @@ PrintedDocument::~PrintedDocument() { } void PrintedDocument::SetPage(int page_number, - NativeMetafile* metafile, + Metafile* metafile, double shrink, const gfx::Size& paper_size, const gfx::Rect& page_rect, @@ -125,7 +125,7 @@ bool PrintedDocument::IsComplete() const { PrintedPages::const_iterator itr = mutable_.pages_.find(page.ToInt()); if (itr == mutable_.pages_.end() || !itr->second.get()) return false; - if (metafile_must_be_valid && !itr->second->native_metafile()) + if (metafile_must_be_valid && !itr->second->metafile()) return false; } return true; @@ -151,7 +151,7 @@ uint32 PrintedDocument::MemoryUsage() const { } uint32 total = 0; for (size_t i = 0; i < pages_copy.size(); ++i) { - total += pages_copy[i]->native_metafile()->GetDataSize(); + total += pages_copy[i]->metafile()->GetDataSize(); } return total; } @@ -263,11 +263,11 @@ void PrintedDocument::DebugDump(const PrintedPage& page) { filename += ASCIIToUTF16(StringPrintf("%02d", page.page_number())); #if defined(OS_WIN) filename += ASCIIToUTF16("_.emf"); - page.native_metafile()->SaveTo( + page.metafile()->SaveTo( g_debug_dump_info.Get().debug_dump_path.Append(filename)); #else // OS_WIN filename += ASCIIToUTF16("_.pdf"); - page.native_metafile()->SaveTo( + page.metafile()->SaveTo( g_debug_dump_info.Get().debug_dump_path.Append(UTF16ToUTF8(filename))); #endif // OS_WIN } diff --git a/printing/printed_document.h b/printing/printed_document.h index b577f47..a53b90f 100644 --- a/printing/printed_document.h +++ b/printing/printed_document.h @@ -24,7 +24,7 @@ class Font; namespace printing { -class NativeMetafile; +class Metafile; class PrintedPage; class PrintedPagesSource; class PrintingContext; @@ -45,7 +45,7 @@ class PrintedDocument : public base::RefCountedThreadSafe { // Sets a page's data. 0-based. Takes metafile ownership. // Note: locks for a short amount of time. - void SetPage(int page_number, NativeMetafile* metafile, double shrink, + void SetPage(int page_number, Metafile* metafile, double shrink, const gfx::Size& paper_size, const gfx::Rect& page_rect, bool has_visible_overlays); diff --git a/printing/printed_document_cairo.cc b/printing/printed_document_cairo.cc index cb9bbd0..7675b96 100644 --- a/printing/printed_document_cairo.cc +++ b/printing/printed_document_cairo.cc @@ -28,7 +28,7 @@ void PrintedDocument::RenderPrintedPage( base::AutoLock lock(lock_); if (page.page_number() - 1 == mutable_.first_page) { reinterpret_cast(context)->PrintDocument( - page.native_metafile()); + page.metafile()); } } #endif // !defined(OS_CHROMEOS) diff --git a/printing/printed_document_mac.cc b/printing/printed_document_mac.cc index 2651516..bb067c88 100644 --- a/printing/printed_document_mac.cc +++ b/printing/printed_document_mac.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -30,8 +30,8 @@ void PrintedDocument::RenderPrintedPage( 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 printing::Metafile* metafile = page.metafile(); + // Each Metafile is a one-page PDF, and pages use 1-based indexing. const int page_number = 1; metafile->RenderPage(page_number, context, content_area.ToCGRect(), false, false, false, false); diff --git a/printing/printed_document_win.cc b/printing/printed_document_win.cc index a0719b4..cb25fdef 100644 --- a/printing/printed_document_win.cc +++ b/printing/printed_document_win.cc @@ -114,7 +114,7 @@ void PrintedDocument::RenderPrintedPage( content_area.y() - page_setup.printable_area().y(), mutable_.shrink_factor); - if (!page.native_metafile()->SafePlayback(context)) { + if (!page.metafile()->SafePlayback(context)) { NOTREACHED(); } diff --git a/printing/printed_page.cc b/printing/printed_page.cc index 1661813..ac1a2d8 100644 --- a/printing/printed_page.cc +++ b/printing/printed_page.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -7,12 +7,12 @@ namespace printing { PrintedPage::PrintedPage(int page_number, - NativeMetafile* native_metafile, + Metafile* metafile, const gfx::Size& page_size, const gfx::Rect& page_content_rect, bool has_visible_overlays) : page_number_(page_number), - native_metafile_(native_metafile), + metafile_(metafile), page_size_(page_size), page_content_rect_(page_content_rect), has_visible_overlays_(has_visible_overlays) { @@ -21,8 +21,8 @@ PrintedPage::PrintedPage(int page_number, PrintedPage::~PrintedPage() { } -const NativeMetafile* PrintedPage::native_metafile() const { - return native_metafile_.get(); +const Metafile* PrintedPage::metafile() const { + return metafile_.get(); } void PrintedPage::GetCenteredPageContentRect( diff --git a/printing/printed_page.h b/printing/printed_page.h index 0fc9069..3771457 100644 --- a/printing/printed_page.h +++ b/printing/printed_page.h @@ -7,7 +7,7 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" -#include "printing/native_metafile.h" +#include "printing/metafile.h" #include "ui/gfx/rect.h" #include "ui/gfx/size.h" @@ -22,14 +22,14 @@ namespace printing { class PrintedPage : public base::RefCountedThreadSafe { public: PrintedPage(int page_number, - NativeMetafile* native_metafile, + Metafile* metafile, const gfx::Size& page_size, const gfx::Rect& page_content_rect, bool has_visible_overlays); // Getters int page_number() const { return page_number_; } - const NativeMetafile* native_metafile() const; + const Metafile* metafile() const; const gfx::Size& page_size() const { return page_size_; } const gfx::Rect& page_content_rect() const { return page_content_rect_; } bool has_visible_overlays() const { return has_visible_overlays_; } @@ -48,7 +48,7 @@ class PrintedPage : public base::RefCountedThreadSafe { const int page_number_; // Actual paint data. - const scoped_ptr native_metafile_; + const scoped_ptr metafile_; // The physical page size. To support multiple page formats inside on print // job. diff --git a/printing/printing.gyp b/printing/printing.gyp index 6e9afcb..1a39bfd 100644 --- a/printing/printing.gyp +++ b/printing/printing.gyp @@ -36,9 +36,8 @@ 'image_mac.cc', 'image_win.cc', 'image.h', - 'native_metafile_factory.cc', - 'native_metafile_factory.h', - 'native_metafile.h', + 'metafile.h', + 'metafile_impl.h', 'page_number.cc', 'page_number.h', 'page_overlays.cc', @@ -100,8 +99,8 @@ }], ['OS=="linux" or OS=="freebsd" or OS=="openbsd"', { 'sources': [ - 'native_metafile_skia_wrapper.cc', - 'native_metafile_skia_wrapper.h', + 'metafile_skia_wrapper.cc', + 'metafile_skia_wrapper.h', ], 'dependencies': [ # For FT_Init_FreeType and friends. diff --git a/printing/printing_context_cairo.cc b/printing/printing_context_cairo.cc index 552cfbb..6b24063 100644 --- a/printing/printing_context_cairo.cc +++ b/printing/printing_context_cairo.cc @@ -6,6 +6,7 @@ #include "base/logging.h" #include "base/values.h" +#include "printing/metafile.h" #include "printing/print_job_constants.h" #include "printing/print_settings_initializer_gtk.h" #include "printing/units.h" @@ -62,7 +63,7 @@ void PrintingContextCairo::SetCreatePrintDialogFunction( create_dialog_func_ = create_dialog_func; } -void PrintingContextCairo::PrintDocument(const NativeMetafile* metafile) { +void PrintingContextCairo::PrintDocument(const Metafile* metafile) { DCHECK(print_dialog_); DCHECK(metafile); print_dialog_->PrintDocument(metafile, document_name_); diff --git a/printing/printing_context_cairo.h b/printing/printing_context_cairo.h index 3002b06..2326dea 100644 --- a/printing/printing_context_cairo.h +++ b/printing/printing_context_cairo.h @@ -9,12 +9,9 @@ #include "printing/printing_context.h" -#if !defined(OS_CHROMEOS) -#include "printing/native_metafile.h" -#endif - namespace printing { +class Metafile; class PrintDialogGtkInterface; class PrintingContextCairo : public PrintingContext { @@ -29,7 +26,7 @@ class PrintingContextCairo : public PrintingContext { PrintingContextCairo* context)); // Prints the document contained in |metafile|. - void PrintDocument(const NativeMetafile* metafile); + void PrintDocument(const Metafile* metafile); #endif // PrintingContext implementation. -- cgit v1.1