diff options
author | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-29 22:11:16 +0000 |
---|---|---|
committer | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-29 22:11:16 +0000 |
commit | 37f31054c7415aa893179b32efb0eac1029819af (patch) | |
tree | 69d74fe8d0ead8328c866c5f091c502cc5ecf47d /printing | |
parent | 10fc3db93f082025c005f097ea45c381bdfb3cd0 (diff) | |
download | chromium_src-37f31054c7415aa893179b32efb0eac1029819af.zip chromium_src-37f31054c7415aa893179b32efb0eac1029819af.tar.gz chromium_src-37f31054c7415aa893179b32efb0eac1029819af.tar.bz2 |
Added support for vector printing for Pepper v1 plugins for Linux.
BUG=None
TEST=Test printing from Chrome PDF plugin on Linux.
Review URL: http://codereview.chromium.org/2807027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51181 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
-rw-r--r-- | printing/pdf_ps_metafile_cairo.cc | 36 | ||||
-rw-r--r-- | printing/pdf_ps_metafile_cairo.h | 12 | ||||
-rw-r--r-- | printing/pdf_ps_metafile_cairo_unittest.cc | 18 |
3 files changed, 66 insertions, 0 deletions
diff --git a/printing/pdf_ps_metafile_cairo.cc b/printing/pdf_ps_metafile_cairo.cc index a4b10b2..47f4c14 100644 --- a/printing/pdf_ps_metafile_cairo.cc +++ b/printing/pdf_ps_metafile_cairo.cc @@ -19,6 +19,8 @@ namespace { +const cairo_user_data_key_t kPdfMetafileKey = {0}; + // Tests if |surface| is valid. bool IsSurfaceValid(cairo_surface_t* surface) { return cairo_surface_status(surface) == CAIRO_STATUS_SUCCESS; @@ -60,6 +62,10 @@ cairo_status_t WriteCairoStream(void* dst_buffer, return CAIRO_STATUS_SUCCESS; } +void DestroyContextData(void* data) { + // Nothing to be done here. +} + } // namespace namespace printing { @@ -115,6 +121,8 @@ bool PdfPsMetafile::Init() { return false; } + cairo_set_user_data(context_, &kPdfMetafileKey, this, DestroyContextData); + return true; } @@ -133,6 +141,23 @@ bool PdfPsMetafile::Init(const void* src_buffer, uint32 src_buffer_size) { return true; } +bool PdfPsMetafile::SetRawData(const void* src_buffer, + uint32 src_buffer_size) { + if (!context_) { + // If Init has not already been called, just call Init() + return Init(src_buffer, src_buffer_size); + } + // If a context has already been created, remember this data in + // raw_override_data_ + if (src_buffer == NULL || src_buffer_size == 0) + return false; + + raw_override_data_ = std::string(reinterpret_cast<const char*>(src_buffer), + src_buffer_size); + + return true; +} + cairo_t* PdfPsMetafile::StartPage(double width_in_points, double height_in_points, double margin_top_in_points, @@ -191,6 +216,12 @@ void PdfPsMetafile::Close() { DCHECK(IsContextValid(context_)); cairo_surface_finish(surface_); + + // If we have raw PDF/PS data set use that instead of what was drawn. + if (!raw_override_data_.empty()) { + data_ = raw_override_data_; + raw_override_data_.clear(); + } DCHECK(!data_.empty()); // Make sure we did get something. CleanUpContext(&context_); @@ -242,6 +273,11 @@ bool PdfPsMetafile::SaveTo(const base::FileDescriptor& fd) const { return success; } +PdfPsMetafile* PdfPsMetafile::FromCairoContext(cairo_t* context) { + return reinterpret_cast<PdfPsMetafile*>( + cairo_get_user_data(context, &kPdfMetafileKey)); +} + void PdfPsMetafile::CleanUpAll() { CleanUpContext(&context_); CleanUpSurface(&surface_); diff --git a/printing/pdf_ps_metafile_cairo.h b/printing/pdf_ps_metafile_cairo.h index 24b5d36..7a7b470 100644 --- a/printing/pdf_ps_metafile_cairo.h +++ b/printing/pdf_ps_metafile_cairo.h @@ -48,6 +48,12 @@ class PdfPsMetafile { // Note: Only call in the browser to initialize |data_|. bool Init(const void* src_buffer, uint32 src_buffer_size); + // Sets raw PS/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); + FileFormat GetFileFormat() const { return format_; } // Prepares a new cairo surface/context for rendering a new page. @@ -89,6 +95,10 @@ class PdfPsMetafile { static const double kBottomMarginInInch; static const double kLeftMarginInInch; + // Returns the PdfPsMetafile object that owns the given context. Returns NULL + // if the context was not created by a PdfPdMetafile object. + static PdfPsMetafile* FromCairoContext(cairo_t* context); + private: // Cleans up all resources. void CleanUpAll(); @@ -101,6 +111,8 @@ class PdfPsMetafile { // Buffer stores PDF/PS contents for entire PDF/PS file. std::string data_; + // Buffer stores raw PDF/PS contents set by SetRawPageData. + std::string raw_override_data_; DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile); }; diff --git a/printing/pdf_ps_metafile_cairo_unittest.cc b/printing/pdf_ps_metafile_cairo_unittest.cc index e1b84d0..9273066 100644 --- a/printing/pdf_ps_metafile_cairo_unittest.cc +++ b/printing/pdf_ps_metafile_cairo_unittest.cc @@ -10,6 +10,7 @@ #include "base/file_descriptor_posix.h" #include "base/file_util.h" +#include "base/string_util.h" #include "testing/gtest/include/gtest/gtest.h" typedef struct _cairo cairo_t; @@ -29,6 +30,7 @@ TEST_F(PdfPsTest, Pdf) { // Renders page 1. cairo_t* context = pdf.StartPage(72, 72, 1, 2, 3, 4); EXPECT_TRUE(context != NULL); + EXPECT_EQ(printing::PdfPsMetafile::FromCairoContext(context), &pdf); // In theory, we should use Cairo to draw something on |context|. EXPECT_TRUE(pdf.FinishPage()); @@ -63,6 +65,21 @@ TEST_F(PdfPsTest, Pdf) { // Tests if we can save data. EXPECT_TRUE(pdf.SaveTo(DevNullFD())); + + // Test overriding the metafile with raw data. + printing::PdfPsMetafile pdf3(printing::PdfPsMetafile::PDF); + EXPECT_TRUE(pdf3.Init()); + context = pdf3.StartPage(72, 72, 1, 2, 3, 4); + EXPECT_TRUE(context != NULL); + std::string test_raw_data = "Dummy PDF"; + EXPECT_TRUE(pdf3.SetRawData(test_raw_data.c_str(), test_raw_data.size())); + EXPECT_TRUE(pdf3.FinishPage()); + pdf3.Close(); + size = pdf3.GetDataSize(); + EXPECT_EQ(test_raw_data.size(), size); + std::string output; + pdf3.GetData(WriteInto(&output, size + 1), size); + EXPECT_EQ(test_raw_data, output); } TEST_F(PdfPsTest, Ps) { @@ -73,6 +90,7 @@ TEST_F(PdfPsTest, Ps) { // Renders page 1. cairo_t* context = ps.StartPage(72, 72, 1, 2, 3, 4); EXPECT_TRUE(context != NULL); + EXPECT_EQ(printing::PdfPsMetafile::FromCairoContext(context), &ps); // In theory, we should use Cairo to draw something on |context|. EXPECT_TRUE(ps.FinishPage()); |