summaryrefslogtreecommitdiffstats
path: root/printing
diff options
context:
space:
mode:
authordpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-02 16:46:20 +0000
committerdpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-02 16:46:20 +0000
commit1f3b6804df95e260d5e9a3fff0c478818240cdb9 (patch)
tree208ac1115304f544e62e1483ec12a7e17d7ff078 /printing
parent21f1168bab8f4ac626011148ae6e65d8fd24272a (diff)
downloadchromium_src-1f3b6804df95e260d5e9a3fff0c478818240cdb9.zip
chromium_src-1f3b6804df95e260d5e9a3fff0c478818240cdb9.tar.gz
chromium_src-1f3b6804df95e260d5e9a3fff0c478818240cdb9.tar.bz2
Applying factory pattern (through NativeMetafileFactory class). It is used to retrieve different printing contexts (based on the platform and user preferences).
BUG=NONE TEST=NONE Review URL: http://codereview.chromium.org/6544028 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@76553 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
-rw-r--r--printing/emf_win.h54
-rwxr-xr-x[-rw-r--r--]printing/emf_win_unittest.cc3
-rw-r--r--printing/image.cc11
-rw-r--r--printing/native_metafile.h41
-rw-r--r--printing/native_metafile_factory.cc28
-rw-r--r--printing/native_metafile_factory.h30
-rw-r--r--printing/native_metafile_linux.h81
-rw-r--r--printing/native_metafile_mac.h90
-rw-r--r--printing/native_metafile_win.h103
-rw-r--r--printing/pdf_metafile_mac.h45
-rw-r--r--printing/pdf_metafile_mac_unittest.cc6
-rw-r--r--printing/pdf_ps_metafile_cairo.h41
-rw-r--r--printing/pdf_ps_metafile_cairo_unittest.cc8
-rw-r--r--printing/print_settings_initializer_gtk.cc28
-rw-r--r--printing/printed_document.h2
-rw-r--r--printing/printing.gyp7
-rw-r--r--printing/printing_context_cairo.cc16
17 files changed, 473 insertions, 121 deletions
diff --git a/printing/emf_win.h b/printing/emf_win.h
index f8a0558..ba139d8 100644
--- a/printing/emf_win.h
+++ b/printing/emf_win.h
@@ -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.
@@ -9,6 +9,8 @@
#include <vector>
#include "base/basictypes.h"
+#include "base/gtest_prod_util.h"
+#include "printing/native_metafile_win.h"
class FilePath;
@@ -19,30 +21,31 @@ class Rect;
namespace printing {
// Simple wrapper class that manage an EMF data stream and its virtual HDC.
-class Emf {
+class Emf : public NativeMetafile {
public:
class Record;
class Enumerator;
struct EnumerationContext;
- Emf();
- ~Emf();
+ virtual ~Emf();
// Initializes the Emf with the data in |src_buffer|. Returns true on success.
- bool Init(const void* src_buffer, uint32 src_buffer_size);
+ virtual bool Init(const void* src_buffer, uint32 src_buffer_size);
// Generates a virtual HDC that will record every GDI commands and compile it
// in a EMF data stream.
// hdc is used to setup the default DPI and color settings. hdc is optional.
// rect specifies the dimensions (in .01-millimeter units) of the EMF. rect is
// optional.
- bool CreateDc(HDC sibling, const RECT* rect);
+ virtual bool CreateDc(HDC sibling, const RECT* rect);
// Similar to the above method but the metafile is backed by a file.
- bool CreateFileBackedDc(HDC sibling, const RECT* rect, const FilePath& path);
+ virtual bool CreateFileBackedDc(HDC sibling,
+ const RECT* rect,
+ const FilePath& path);
// Load an EMF file.
- bool CreateFromFile(const FilePath& metafile_path);
+ virtual bool CreateFromFile(const FilePath& metafile_path);
// TODO(maruel): CreateFromFile(). If ever used. Maybe users would like to
// have the ability to save web pages to an EMF file? Afterward, it is easy to
@@ -50,10 +53,10 @@ class Emf {
// Closes the HDC created by CreateDc() and generates the compiled EMF
// data.
- bool CloseDc();
+ virtual bool CloseDc();
// Closes the EMF data handle when it is not needed anymore.
- void CloseEmf();
+ virtual void CloseEmf();
// "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
@@ -63,47 +66,56 @@ class Emf {
// 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.
- bool Playback(HDC hdc, const RECT* rect) const;
+ virtual bool Playback(HDC hdc, const RECT* rect) const;
// 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.
- bool SafePlayback(HDC hdc) const;
+ virtual bool SafePlayback(HDC hdc) const;
// Retrieves the bounds of the painted area by this EMF buffer. This value
// should be passed to Playback to keep the exact same size.
- gfx::Rect GetBounds() const;
+ virtual gfx::Rect GetBounds() const;
// Retrieves the EMF stream size.
- uint32 GetDataSize() const;
+ virtual uint32 GetDataSize() const;
// Retrieves the EMF stream.
- bool GetData(void* buffer, uint32 size) const;
+ virtual bool GetData(void* buffer, uint32 size) const;
// Retrieves the EMF stream. It is an helper function.
- bool GetData(std::vector<uint8>* buffer) const;
+ virtual bool GetData(std::vector<uint8>* buffer) const;
- HENHMETAFILE emf() const {
+ virtual HENHMETAFILE emf() const {
return emf_;
}
- HDC hdc() const {
+ virtual HDC hdc() const {
return hdc_;
}
// Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
// (since StartPage and EndPage do not work in a metafile DC). Only valid
// when hdc_ is non-NULL.
- bool StartPage();
- bool EndPage();
+ virtual bool StartPage();
+ virtual bool EndPage();
// Saves the EMF data to a file as-is. It is recommended to use the .emf file
// extension but it is not enforced. This function synchronously writes to the
// file. For testing only.
- bool SaveTo(const std::wstring& filename) const;
+ virtual bool SaveTo(const std::wstring& filename) const;
+
+ protected:
+ Emf();
private:
+ friend class NativeMetafileFactory;
+ FRIEND_TEST_ALL_PREFIXES(EmfTest, DC);
+ FRIEND_TEST_ALL_PREFIXES(EmfTest, FileBackedDC);
+ FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, Enumerate);
+ FRIEND_TEST_ALL_PREFIXES(EmfPrintingTest, PageBreak);
+
// Playbacks safely one EMF record.
static int CALLBACK SafePlaybackProc(HDC hdc,
HANDLETABLE* handle_table,
diff --git a/printing/emf_win_unittest.cc b/printing/emf_win_unittest.cc
index 4312b2c..9d3aa00 100644..100755
--- a/printing/emf_win_unittest.cc
+++ b/printing/emf_win_unittest.cc
@@ -39,6 +39,8 @@ const uint32 EMF_HEADER_SIZE = 128;
} // namespace
+namespace printing {
+
TEST(EmfTest, DC) {
// Simplest use case.
printing::Emf emf;
@@ -195,3 +197,4 @@ TEST(EmfTest, FileBackedDC) {
emf.CloseEmf();
}
+} // namespace printing
diff --git a/printing/image.cc b/printing/image.cc
index 1116f32..a99e307 100644
--- a/printing/image.cc
+++ b/printing/image.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.
@@ -6,7 +6,9 @@
#include "base/file_util.h"
#include "base/md5.h"
+#include "base/scoped_ptr.h"
#include "base/string_number_conversions.h"
+#include "printing/native_metafile_factory.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/codec/png_codec.h"
@@ -144,9 +146,10 @@ bool Image::LoadPng(const std::string& compressed) {
bool Image::LoadMetafile(const std::string& data) {
DCHECK(!data.empty());
- NativeMetafile metafile;
- metafile.Init(data.data(), data.size());
- return LoadMetafile(metafile);
+ scoped_ptr<NativeMetafile> metafile(
+ printing::NativeMetafileFactory::CreateMetafile());
+ metafile->Init(data.data(), data.size());
+ return LoadMetafile(*metafile);
}
} // namespace printing
diff --git a/printing/native_metafile.h b/printing/native_metafile.h
index b0f957a..7e1d25b 100644
--- a/printing/native_metafile.h
+++ b/printing/native_metafile.h
@@ -1,50 +1,19 @@
-// 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.
#ifndef PRINTING_NATIVE_METAFILE_H_
#define PRINTING_NATIVE_METAFILE_H_
+#include "base/basictypes.h"
#include "build/build_config.h"
-// Define a metafile format for the current platform. We use this platform
-// independent define so we can define interfaces in platform agnostic manner.
-// It is still an outstanding design issue whether we create classes on all
-// platforms that have the same interface as Emf or if we change Emf to support
-// multiple platforms (and rename to NativeMetafile).
-
-
#if defined(OS_WIN)
-
-#include "printing/emf_win.h"
-
-namespace printing {
-
-typedef Emf NativeMetafile;
-
-} // namespace printing
-
+#include "printing/native_metafile_win.h"
#elif defined(OS_MACOSX)
-
-#include "printing/pdf_metafile_mac.h"
-
-namespace printing {
-
-typedef PdfMetafile NativeMetafile;
-
-} // namespace printing
-
+#include "printing/native_metafile_mac.h"
#elif defined(OS_POSIX)
-
-#include "printing/pdf_ps_metafile_cairo.h"
-
-namespace printing {
-
-typedef PdfPsMetafile NativeMetafile;
-
-} // namespace printing
-
+#include "printing/native_metafile_linux.h"
#endif
-
#endif // PRINTING_NATIVE_METAFILE_H_
diff --git a/printing/native_metafile_factory.cc b/printing/native_metafile_factory.cc
new file mode 100644
index 0000000..a41428f
--- /dev/null
+++ b/printing/native_metafile_factory.cc
@@ -0,0 +1,28 @@
+// 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"
+
+#if defined(OS_WIN)
+#include "printing/emf_win.h"
+#elif defined(OS_MACOSX)
+#include "printing/pdf_metafile_mac.h"
+#elif defined(OS_POSIX)
+#include "printing/pdf_ps_metafile_cairo.h"
+#endif
+
+namespace printing {
+
+// static
+printing::NativeMetafile* NativeMetafileFactory::CreateMetafile() {
+#if defined(OS_WIN)
+ return new printing::Emf;
+#elif defined(OS_MACOSX)
+ return new printing::PdfMetafile;
+#elif defined(OS_POSIX)
+ return new printing::PdfPsMetafile;
+#endif
+}
+
+} // namespace printing
diff --git a/printing/native_metafile_factory.h b/printing/native_metafile_factory.h
new file mode 100644
index 0000000..d1543c7
--- /dev/null
+++ b/printing/native_metafile_factory.h
@@ -0,0 +1,30 @@
+// 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.
+ static printing::NativeMetafile* CreateMetafile();
+
+ private:
+ NativeMetafileFactory();
+ DISALLOW_COPY_AND_ASSIGN(NativeMetafileFactory);
+};
+
+} // namespace printing
+
+#endif // PRINTING_NATIVE_METAFILE_FACTORY_H_
diff --git a/printing/native_metafile_linux.h b/printing/native_metafile_linux.h
new file mode 100644
index 0000000..9fe622b
--- /dev/null
+++ b/printing/native_metafile_linux.h
@@ -0,0 +1,81 @@
+// 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_LINUX_H_
+#define PRINTING_NATIVE_METAFILE_LINUX_H_
+
+#include "base/basictypes.h"
+
+typedef struct _cairo_surface cairo_surface_t;
+typedef struct _cairo cairo_t;
+
+namespace base {
+struct FileDescriptor;
+}
+
+class FilePath;
+
+namespace printing {
+
+// This class is used to generate a PDF stream and to store rendering results
+// in a string buffer.
+class NativeMetafile {
+ public:
+ virtual ~NativeMetafile() {}
+
+ // Initializes to a fresh new metafile. Returns true on success.
+ // Note: It should only be called from the renderer process to allocate
+ // rendering resources.
+ virtual bool Init() = 0;
+
+ // Initializes a copy of metafile from PDF stream data.
+ // Returns true on success.
+ // |src_buffer| should point to the shared memory which stores PDF
+ // contents generated in the renderer.
+ // Note: It should only be called from the browser process to initialize
+ // |data_|.
+ virtual bool Init(const void* src_buffer, uint32 src_buffer_size) = 0;
+
+ // Sets raw PDF data for the document. This is used when a cairo drawing
+ // surface has already been created. This method will cause all subsequent
+ // drawing on the surface to be discarded (in Close()). If Init() has not yet
+ // been called this method simply calls the second version of Init.
+ virtual bool SetRawData(const void* src_buffer, uint32 src_buffer_size) = 0;
+
+ // Prepares a new cairo surface/context for rendering a new page.
+ // The unit is in point (=1/72 in).
+ // Returns NULL when failed.
+ virtual cairo_t* StartPage(double width_in_points,
+ double height_in_points,
+ double margin_top_in_points,
+ double margin_right_in_points,
+ double margin_bottom_in_points,
+ double margin_left_in_points) = 0;
+
+ // Destroys the surface and the context used in rendering current page.
+ // The results of current page will be appended into buffer |data_|.
+ // Returns true on success.
+ virtual bool FinishPage() = 0;
+
+ // Closes resulting PDF file. No further rendering is allowed.
+ virtual void Close() = 0;
+
+ // Returns size of PDF contents stored in buffer |data_|.
+ // This function should ONLY be called after PDF file is closed.
+ virtual uint32 GetDataSize() const = 0;
+
+ // Copies PDF contents stored in buffer |data_| into |dst_buffer|.
+ // This function should ONLY be called after PDF file is closed.
+ // Returns true only when success.
+ virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0;
+
+ // Saves PDF contents stored in buffer |data_| into the file
+ // associated with |fd|.
+ // This function should ONLY be called after PDF file is closed.
+ virtual bool SaveTo(const base::FileDescriptor& fd) const = 0;
+};
+
+} // namespace printing
+
+#endif // PRINTING_NATIVE_METAFILE_LINUX_H_
diff --git a/printing/native_metafile_mac.h b/printing/native_metafile_mac.h
new file mode 100644
index 0000000..940da50
--- /dev/null
+++ b/printing/native_metafile_mac.h
@@ -0,0 +1,90 @@
+// 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_MAC_H_
+#define PRINTING_NATIVE_METAFILE_MAC_H_
+
+#include <ApplicationServices/ApplicationServices.h>
+#include <CoreFoundation/CoreFoundation.h>
+
+#include "base/basictypes.h"
+#include "base/mac/scoped_cftyperef.h"
+
+namespace gfx {
+class Rect;
+class Size;
+class Point;
+}
+class FilePath;
+
+namespace printing {
+
+// This class creates a graphics context that renders into a PDF data stream.
+class NativeMetafile {
+ public:
+
+ virtual ~NativeMetafile() {}
+
+ // Initializes a new metafile, and returns a drawing context for rendering
+ // into the PDF. Returns NULL on failure.
+ // Note: The returned context *must not be retained* past Close(). If it is,
+ // the data returned from GetData will not be valid PDF data.
+ virtual CGContextRef Init() = 0;
+
+ // Initializes a copy of metafile from PDF data. Returns true on success.
+ virtual bool Init(const void* src_buffer, uint32 src_buffer_size) = 0;
+
+ // Prepares a new pdf page at specified |content_origin| with the given
+ // |page_size| and a |scale_factor| to use for the drawing.
+ virtual CGContextRef StartPage(const gfx::Size& page_size,
+ const gfx::Point& content_origin,
+ const float& scale_factor) = 0;
+
+ // Closes the current page.
+ virtual void FinishPage() = 0;
+
+ // Closes the PDF file; no further rendering is allowed.
+ virtual void Close() = 0;
+
+ // 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, CGContextRef context,
+ const CGRect rect, bool shrink_to_fit, bool stretch_to_fit,
+ bool center_horizontally, bool center_vertically) const = 0;
+ virtual unsigned int GetPageCount() 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;
+
+ // Returns the size of the underlying PDF data. Only valid after Close() has
+ // been called.
+ virtual uint32 GetDataSize() const = 0;
+
+ // Copies the first |dst_buffer_size| bytes of the PDF data into |dst_buffer|.
+ // Only valid after Close() has been called.
+ // Returns true if the copy succeeds.
+ virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const = 0;
+
+ // Saves the raw PDF data to the given file. For testing only.
+ // Returns true if writing succeeded.
+ virtual bool SaveTo(const FilePath& file_path) const = 0;
+};
+
+} // namespace printing
+
+#endif // PRINTING_NATIVE_METAFILE_MAC_H_
diff --git a/printing/native_metafile_win.h b/printing/native_metafile_win.h
new file mode 100644
index 0000000..fa00121
--- /dev/null
+++ b/printing/native_metafile_win.h
@@ -0,0 +1,103 @@
+// 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_WIN_H_
+#define PRINTING_NATIVE_METAFILE_WIN_H_
+
+#include <windows.h>
+#include <vector>
+
+#include "base/basictypes.h"
+
+class FilePath;
+
+namespace gfx {
+class Rect;
+}
+
+namespace printing {
+
+// Simple wrapper class that manages a data stream and its virtual HDC.
+class NativeMetafile {
+ public:
+ virtual ~NativeMetafile() {}
+
+ // Initializes the data stream with the data in |src_buffer|. Returns
+ // true on success.
+ virtual bool Init(const void* src_buffer, uint32 src_buffer_size) = 0;
+
+ // Generates a virtual HDC that will record every GDI commands and compile it
+ // in a EMF data stream.
+ // hdc is used to setup the default DPI and color settings. hdc is optional.
+ // rect specifies the dimensions (in .01-millimeter units) of the EMF. rect is
+ // optional.
+ virtual bool CreateDc(HDC sibling, const RECT* rect) = 0;
+
+ // Similar to the above method but the metafile is backed by a file.
+ virtual bool CreateFileBackedDc(HDC sibling,
+ const RECT* rect,
+ const FilePath& path) = 0;
+
+ // Load a data stream from file.
+ virtual bool CreateFromFile(const FilePath& metafile_path) = 0;
+
+ // TODO(maruel): CreateFromFile(). If ever used. Maybe users would like to
+ // have the ability to save web pages to an EMF file? Afterward, it is easy to
+ // convert to PDF.
+
+ // Closes the HDC created by CreateDc() and generates the compiled EMF
+ // data.
+ virtual bool CloseDc() = 0;
+
+ // Closes the EMF data handle when it is not needed anymore.
+ virtual void CloseEmf() = 0;
+
+ // "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(HDC 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(HDC hdc) const = 0;
+
+ // Retrieves the bounds of the painted area by this EMF buffer. This value
+ // should be passed to Playback to keep the exact same size.
+ virtual gfx::Rect GetBounds() const = 0;
+
+ // Retrieves the data stream size.
+ virtual uint32 GetDataSize() const = 0;
+
+ // Retrieves the data stream.
+ virtual bool GetData(void* buffer, uint32 size) const = 0;
+
+ // Retrieves the data stream. It is an helper function.
+ virtual bool GetData(std::vector<uint8>* buffer) const = 0;
+
+ virtual HENHMETAFILE emf() const = 0;
+
+ virtual HDC hdc() const = 0;
+
+ // Inserts a custom GDICOMMENT records indicating StartPage/EndPage calls
+ // (since StartPage and EndPage do not work in a metafile DC). Only valid
+ // when hdc_ is non-NULL.
+ virtual bool StartPage() = 0;
+ virtual bool EndPage() = 0;
+
+ // Saves the EMF data to a file as-is. It is recommended to use the .emf file
+ // extension but it is not enforced. This function synchronously writes to the
+ // file. For testing only.
+ virtual bool SaveTo(const std::wstring& filename) const = 0;
+};
+
+} // namespace printing
+
+#endif // PRINTING_NATIVE_METAFILE_WIN_H_
diff --git a/printing/pdf_metafile_mac.h b/printing/pdf_metafile_mac.h
index 557b008..c5669a8 100644
--- a/printing/pdf_metafile_mac.h
+++ b/printing/pdf_metafile_mac.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.
@@ -9,7 +9,9 @@
#include <CoreFoundation/CoreFoundation.h>
#include "base/basictypes.h"
+#include "base/gtest_prod_util.h"
#include "base/mac/scoped_cftyperef.h"
+#include "printing/native_metafile_mac.h"
namespace gfx {
class Rect;
@@ -21,36 +23,31 @@ class FilePath;
namespace printing {
// This class creates a graphics context that renders into a PDF data stream.
-class PdfMetafile {
+class PdfMetafile : public NativeMetafile {
public:
- // To create PDF data, callers should also call Init() to set up the
- // rendering context.
- // To create a metafile from existing Data, callers should also call
- // Init(const void*, uint32).
- PdfMetafile();
- ~PdfMetafile();
+ virtual ~PdfMetafile();
// Initializes a new metafile, and returns a drawing context for rendering
// into the PDF. Returns NULL on failure.
// Note: The returned context *must not be retained* past Close(). If it is,
// the data returned from GetData will not be valid PDF data.
- CGContextRef Init();
+ virtual CGContextRef Init();
// Initializes a copy of metafile from PDF data. Returns true on success.
- bool Init(const void* src_buffer, uint32 src_buffer_size);
+ virtual bool Init(const void* src_buffer, uint32 src_buffer_size);
// Prepares a new pdf page at specified |content_origin| with the given
// |page_size| and a |scale_factor| to use for the drawing.
- CGContextRef StartPage(const gfx::Size& page_size,
+ virtual CGContextRef StartPage(const gfx::Size& page_size,
const gfx::Point& content_origin,
const float& scale_factor);
// Closes the current page.
- void FinishPage();
+ virtual void FinishPage();
// Closes the PDF file; no further rendering is allowed.
- void Close();
+ virtual void Close();
// Renders the given page into |rect| in the given context.
// Pages use a 1-based index. The rendering uses the following arguments
@@ -67,29 +64,39 @@ class PdfMetafile {
// |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.
- bool RenderPage(unsigned int page_number, CGContextRef context,
+ virtual bool RenderPage(unsigned int page_number, CGContextRef context,
const CGRect rect, bool shrink_to_fit, bool stretch_to_fit,
bool center_horizontally, bool center_vertically) const;
- unsigned int GetPageCount() const;
+ virtual unsigned int GetPageCount() const;
// Returns the bounds of the given page.
// Pages use a 1-based index.
- gfx::Rect GetPageBounds(unsigned int page_number) const;
+ virtual gfx::Rect GetPageBounds(unsigned int page_number) const;
// Returns the size of the underlying PDF data. Only valid after Close() has
// been called.
- uint32 GetDataSize() const;
+ virtual uint32 GetDataSize() const;
// Copies the first |dst_buffer_size| bytes of the PDF data into |dst_buffer|.
// Only valid after Close() has been called.
// Returns true if the copy succeeds.
- bool GetData(void* dst_buffer, uint32 dst_buffer_size) const;
+ virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const;
// Saves the raw PDF data to the given file. For testing only.
// Returns true if writing succeeded.
- bool SaveTo(const FilePath& file_path) const;
+ virtual bool SaveTo(const FilePath& file_path) const;
+
+ protected:
+ // To create PDF data, callers should call Init() to set up the rendering
+ // context.
+ // To create a metafile from existing data, callers should call
+ // Init(const void*, uint32).
+ PdfMetafile();
private:
+ friend class NativeMetafileFactory;
+ FRIEND_TEST_ALL_PREFIXES(PdfMetafileTest, Pdf);
+
// Returns a CGPDFDocumentRef version of pdf_data_.
CGPDFDocumentRef GetPDFDocument() const;
diff --git a/printing/pdf_metafile_mac_unittest.cc b/printing/pdf_metafile_mac_unittest.cc
index ec8cd3a..8b52b06 100644
--- a/printing/pdf_metafile_mac_unittest.cc
+++ b/printing/pdf_metafile_mac_unittest.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.
@@ -12,6 +12,8 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/rect.h"
+namespace printing {
+
TEST(PdfMetafileTest, Pdf) {
// Test in-renderer constructor.
printing::PdfMetafile pdf;
@@ -60,3 +62,5 @@ TEST(PdfMetafileTest, Pdf) {
EXPECT_EQ(720, page_size.width());
EXPECT_EQ(540, page_size.height());
}
+
+} // namespace printing
diff --git a/printing/pdf_ps_metafile_cairo.h b/printing/pdf_ps_metafile_cairo.h
index 7274dd8..645a462 100644
--- a/printing/pdf_ps_metafile_cairo.h
+++ b/printing/pdf_ps_metafile_cairo.h
@@ -8,6 +8,8 @@
#include <string>
#include "base/basictypes.h"
+#include "base/gtest_prod_util.h"
+#include "printing/native_metafile_linux.h"
typedef struct _cairo_surface cairo_surface_t;
typedef struct _cairo cairo_t;
@@ -22,59 +24,58 @@ namespace printing {
// This class uses Cairo graphics library to generate PostScript/PDF stream
// and stores rendering results in a string buffer.
-class PdfPsMetafile {
+class PdfPsMetafile : public NativeMetafile {
public:
- PdfPsMetafile();
- ~PdfPsMetafile();
+ virtual ~PdfPsMetafile();
// Initializes to a fresh new metafile. Returns true on success.
// Note: Only call in the renderer to allocate rendering resources.
- bool Init();
+ virtual bool Init();
// Initializes a copy of metafile from PDF stream data.
// Returns true on success.
// |src_buffer| should point to the shared memory which stores PDF
// contents generated in the renderer.
// Note: Only call in the browser to initialize |data_|.
- bool Init(const void* src_buffer, uint32 src_buffer_size);
+ virtual bool Init(const void* src_buffer, uint32 src_buffer_size);
// Sets raw PDF data for the document. This is used when a cairo drawing
// surface has already been created. This method will cause all subsequent
// drawing on the surface to be discarded (in Close()). If Init() has not yet
// been called this method simply calls the second version of Init.
- bool SetRawData(const void* src_buffer, uint32 src_buffer_size);
+ virtual bool SetRawData(const void* src_buffer, uint32 src_buffer_size);
// Prepares a new cairo surface/context for rendering a new page.
// The unit is in point (=1/72 in).
// Returns NULL when failed.
- cairo_t* StartPage(double width_in_points,
- double height_in_points,
- double margin_top_in_points,
- double margin_right_in_points,
- double margin_bottom_in_points,
- double margin_left_in_points);
+ virtual cairo_t* StartPage(double width_in_points,
+ double height_in_points,
+ double margin_top_in_points,
+ double margin_right_in_points,
+ double margin_bottom_in_points,
+ double margin_left_in_points);
// Destroys the surface and the context used in rendering current page.
// The results of current page will be appended into buffer |data_|.
// Returns true on success.
- bool FinishPage();
+ virtual bool FinishPage();
// Closes resulting PDF file. No further rendering is allowed.
- void Close();
+ virtual void Close();
// Returns size of PDF contents stored in buffer |data_|.
// This function should ONLY be called after PDF file is closed.
- uint32 GetDataSize() const;
+ virtual uint32 GetDataSize() const;
// Copies PDF contents stored in buffer |data_| into |dst_buffer|.
// This function should ONLY be called after PDF file is closed.
// Returns true only when success.
- bool GetData(void* dst_buffer, uint32 dst_buffer_size) const;
+ virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const;
// Saves PDF contents stored in buffer |data_| into the file
// associated with |fd|.
// This function should ONLY be called after PDF file is closed.
- bool SaveTo(const base::FileDescriptor& fd) const;
+ virtual 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.
@@ -87,7 +88,13 @@ class PdfPsMetafile {
// if the context was not created by a PdfPsMetafile object.
static PdfPsMetafile* FromCairoContext(cairo_t* context);
+ protected:
+ PdfPsMetafile();
+
private:
+ friend class NativeMetafileFactory;
+ FRIEND_TEST_ALL_PREFIXES(PdfPsTest, Pdf);
+
// Cleans up all resources.
void CleanUpAll();
diff --git a/printing/pdf_ps_metafile_cairo_unittest.cc b/printing/pdf_ps_metafile_cairo_unittest.cc
index b7ed449..a3442c8 100644
--- a/printing/pdf_ps_metafile_cairo_unittest.cc
+++ b/printing/pdf_ps_metafile_cairo_unittest.cc
@@ -15,6 +15,8 @@
typedef struct _cairo cairo_t;
+namespace {
+
class PdfPsTest : public testing::Test {
protected:
base::FileDescriptor DevNullFD() {
@@ -22,6 +24,10 @@ class PdfPsTest : public testing::Test {
}
};
+} // namespace
+
+namespace printing {
+
TEST_F(PdfPsTest, Pdf) {
// Tests in-renderer constructor.
printing::PdfPsMetafile pdf;
@@ -81,3 +87,5 @@ TEST_F(PdfPsTest, Pdf) {
pdf3.GetData(WriteInto(&output, size + 1), size);
EXPECT_EQ(test_raw_data, output);
}
+
+} // namespace printing
diff --git a/printing/print_settings_initializer_gtk.cc b/printing/print_settings_initializer_gtk.cc
index 60ab530..8a62929 100644
--- a/printing/print_settings_initializer_gtk.cc
+++ b/printing/print_settings_initializer_gtk.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.
@@ -9,7 +9,7 @@
#include "base/string_piece.h"
#include "base/utf_string_conversions.h"
-#include "printing/native_metafile.h"
+#include "printing/pdf_ps_metafile_cairo.h"
#include "printing/print_settings.h"
#include "printing/units.h"
@@ -58,19 +58,19 @@ void PrintSettingsInitializerGtk::InitPrintSettings(
double page_width_in_pixel = 8.5 * dpi;
double page_height_in_pixel = 11.0 * dpi;
physical_size_device_units.SetSize(
- static_cast<int>(page_width_in_pixel),
- static_cast<int>(page_height_in_pixel));
+ static_cast<int>(page_width_in_pixel),
+ static_cast<int>(page_height_in_pixel));
printable_area_device_units.SetRect(
- static_cast<int>(
- NativeMetafile::kLeftMarginInInch * dpi),
- static_cast<int>(
- NativeMetafile::kTopMarginInInch * dpi),
- page_width_in_pixel -
- (NativeMetafile::kLeftMarginInInch +
- NativeMetafile::kRightMarginInInch) * dpi,
- page_height_in_pixel -
- (NativeMetafile::kTopMarginInInch +
- NativeMetafile::kBottomMarginInInch) * dpi);
+ static_cast<int>(
+ PdfPsMetafile::kLeftMarginInInch * dpi),
+ static_cast<int>(
+ PdfPsMetafile::kTopMarginInInch * dpi),
+ page_width_in_pixel -
+ (PdfPsMetafile::kLeftMarginInInch +
+ PdfPsMetafile::kRightMarginInInch) * dpi,
+ page_height_in_pixel -
+ (PdfPsMetafile::kTopMarginInInch +
+ PdfPsMetafile::kBottomMarginInInch) * dpi);
}
print_settings->set_dpi(dpi);
diff --git a/printing/printed_document.h b/printing/printed_document.h
index f95ddcf..14e5e8c 100644
--- a/printing/printed_document.h
+++ b/printing/printed_document.h
@@ -13,7 +13,6 @@
#include "base/synchronization/lock.h"
#include "googleurl/src/gurl.h"
#include "printing/print_settings.h"
-#include "printing/native_metafile.h"
#include "ui/gfx/native_widget_types.h"
class FilePath;
@@ -25,6 +24,7 @@ class Font;
namespace printing {
+class NativeMetafile;
class PrintedPage;
class PrintedPagesSource;
class PrintingContext;
diff --git a/printing/printing.gyp b/printing/printing.gyp
index 270306f..9201e69 100644
--- a/printing/printing.gyp
+++ b/printing/printing.gyp
@@ -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.
@@ -36,6 +36,11 @@
'image_mac.cc',
'image_win.cc',
'image.h',
+ 'native_metafile_factory.cc',
+ 'native_metafile_factory.h',
+ 'native_metafile_linux.h',
+ 'native_metafile_mac.h',
+ 'native_metafile_win.h',
'native_metafile.h',
'page_number.cc',
'page_number.h',
diff --git a/printing/printing_context_cairo.cc b/printing/printing_context_cairo.cc
index d0110b1..84aaa64 100644
--- a/printing/printing_context_cairo.cc
+++ b/printing/printing_context_cairo.cc
@@ -9,7 +9,9 @@
#if defined(OS_CHROMEOS)
#include <unicode/ulocdata.h>
-#include <printing/native_metafile.h>
+
+#include "printing/native_metafile.h"
+#include "printing/pdf_ps_metafile_cairo.h"
#else
#include <gtk/gtk.h>
#include <gtk/gtkprintunixdialog.h>
@@ -119,12 +121,12 @@ PrintingContext::Result PrintingContextCairo::UseDefaultSettings() {
physical_size_device_units.SetSize(width, height);
printable_area_device_units.SetRect(
- static_cast<int>(NativeMetafile::kLeftMarginInInch * dpi),
- static_cast<int>(NativeMetafile::kTopMarginInInch * dpi),
- width - (NativeMetafile::kLeftMarginInInch +
- NativeMetafile::kRightMarginInInch) * dpi,
- height - (NativeMetafile::kTopMarginInInch +
- NativeMetafile::kBottomMarginInInch) * dpi);
+ static_cast<int>(PdfPsMetafile::kLeftMarginInInch * dpi),
+ static_cast<int>(PdfPsMetafile::kTopMarginInInch * dpi),
+ width - (PdfPsMetafile::kLeftMarginInInch +
+ PdfPsMetafile::kRightMarginInInch) * dpi,
+ height - (PdfPsMetafile::kTopMarginInInch +
+ PdfPsMetafile::kBottomMarginInInch) * dpi);
settings_.set_dpi(dpi);
settings_.SetPrinterPrintableArea(physical_size_device_units,