summaryrefslogtreecommitdiffstats
path: root/printing
diff options
context:
space:
mode:
Diffstat (limited to 'printing')
-rw-r--r--printing/native_metafile.h10
-rw-r--r--printing/pdf_ps_metafile_cairo.cc60
-rw-r--r--printing/pdf_ps_metafile_cairo.h19
-rw-r--r--printing/pdf_ps_metafile_cairo_unittest.cc2
4 files changed, 34 insertions, 57 deletions
diff --git a/printing/native_metafile.h b/printing/native_metafile.h
index 6623343..abf3203 100644
--- a/printing/native_metafile.h
+++ b/printing/native_metafile.h
@@ -142,20 +142,12 @@ class NativeMetafile {
bool stretch_to_fit,
bool center_horizontally,
bool center_vertically) const = 0;
-#elif defined(OS_POSIX)
- // 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;
-#if defined(OS_CHROMEOS)
+#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)
-
-#endif
};
} // namespace printing
diff --git a/printing/pdf_ps_metafile_cairo.cc b/printing/pdf_ps_metafile_cairo.cc
index 4a8a1ff..fdbb335 100644
--- a/printing/pdf_ps_metafile_cairo.cc
+++ b/printing/pdf_ps_metafile_cairo.cc
@@ -73,7 +73,8 @@ namespace printing {
PdfPsMetafile::PdfPsMetafile()
: surface_(NULL),
- context_(NULL) {
+ context_(NULL),
+ current_data_(NULL) {
}
PdfPsMetafile::~PdfPsMetafile() {
@@ -82,15 +83,15 @@ PdfPsMetafile::~PdfPsMetafile() {
}
bool PdfPsMetafile::Init() {
- // We need to check at least these two members to ensure Init() has not been
+ // We need to check |current_data_| to ensure Init/InitFromData has not been
// called before.
- DCHECK(!context_);
- DCHECK(data_.empty());
+ DCHECK(!current_data_);
+ current_data_ = &cairo_data_;
// Creates an 1 by 1 Cairo surface for the entire PDF file.
// The size for each page will be overwritten later in StartPage().
surface_ = cairo_pdf_surface_create_for_stream(WriteCairoStream,
- &data_, 1, 1);
+ current_data_, 1, 1);
// Cairo always returns a valid pointer.
// Hence, we have to check if it points to a "nil" object.
@@ -115,33 +116,12 @@ bool PdfPsMetafile::Init() {
bool PdfPsMetafile::InitFromData(const void* src_buffer,
uint32 src_buffer_size) {
- // We need to check at least these two members to ensure Init() has not been
- // called before
- DCHECK(!context_);
- DCHECK(data_.empty());
-
if (src_buffer == NULL || src_buffer_size == 0)
return false;
- data_ = std::string(reinterpret_cast<const char*>(src_buffer),
+ raw_data_ = std::string(reinterpret_cast<const char*>(src_buffer),
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 InitFromData(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);
-
+ current_data_ = &raw_data_;
return true;
}
@@ -194,12 +174,7 @@ bool PdfPsMetafile::FinishDocument() {
cairo_surface_finish(surface_);
- // If we have raw PDF 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.
+ DCHECK(!cairo_data_.empty()); // Make sure we did get something.
CleanUpContext(&context_);
CleanUpSurface(&surface_);
@@ -210,15 +185,15 @@ uint32 PdfPsMetafile::GetDataSize() const {
// We need to check at least these two members to ensure that either Init()
// has been called to initialize |data_|, or metafile has been closed.
DCHECK(!context_);
- DCHECK(!data_.empty());
+ DCHECK(!current_data_->empty());
- return data_.size();
+ return current_data_->size();
}
bool PdfPsMetafile::GetData(void* dst_buffer, uint32 dst_buffer_size) const {
DCHECK(dst_buffer);
DCHECK_GT(dst_buffer_size, 0u);
- memcpy(dst_buffer, data_.data(), dst_buffer_size);
+ memcpy(dst_buffer, current_data_->data(), dst_buffer_size);
return true;
}
@@ -231,10 +206,10 @@ bool PdfPsMetafile::SaveTo(const FilePath& file_path) const {
// We need to check at least these two members to ensure that either Init()
// has been called to initialize |data_|, or metafile has been closed.
DCHECK(!context_);
- DCHECK(!data_.empty());
+ DCHECK(!current_data_->empty());
bool success = true;
- if (file_util::WriteFile(file_path, data_.data(), GetDataSize())
+ if (file_util::WriteFile(file_path, current_data_->data(), GetDataSize())
!= static_cast<int>(GetDataSize())) {
DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
success = false;
@@ -257,7 +232,7 @@ bool PdfPsMetafile::SaveToFD(const base::FileDescriptor& fd) const {
// We need to check at least these two members to ensure that either Init()
// has been called to initialize |data_|, or metafile has been closed.
DCHECK(!context_);
- DCHECK(!data_.empty());
+ DCHECK(!current_data_->empty());
if (fd.fd < 0) {
DLOG(ERROR) << "Invalid file descriptor!";
@@ -265,7 +240,7 @@ bool PdfPsMetafile::SaveToFD(const base::FileDescriptor& fd) const {
}
bool success = true;
- if (file_util::WriteFileDescriptor(fd.fd, data_.data(),
+ if (file_util::WriteFileDescriptor(fd.fd, current_data_->data(),
GetDataSize()) < 0) {
DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
success = false;
@@ -290,7 +265,8 @@ PdfPsMetafile* PdfPsMetafile::FromCairoContext(cairo_t* context) {
void PdfPsMetafile::CleanUpAll() {
CleanUpContext(&context_);
CleanUpSurface(&surface_);
- data_.clear();
+ cairo_data_.clear();
+ raw_data_.clear();
skia::VectorPlatformDevice::ClearFontCache();
}
diff --git a/printing/pdf_ps_metafile_cairo.h b/printing/pdf_ps_metafile_cairo.h
index 3e1e811..2ae1f34 100644
--- a/printing/pdf_ps_metafile_cairo.h
+++ b/printing/pdf_ps_metafile_cairo.h
@@ -29,6 +29,10 @@ class PdfPsMetafile : public NativeMetafile {
// NativeMetafile methods.
virtual bool Init();
+
+ // Calling InitFromData() sets the data for this metafile and masks data
+ // induced by previous calls to Init() or InitFromData(), even if drawing
+ // continues on the surface returned by a previous call to Init().
virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size);
virtual skia::PlatformDevice* StartPageForVectorCanvas(
@@ -50,8 +54,6 @@ class PdfPsMetafile : public NativeMetafile {
virtual cairo_t* context() const;
- virtual bool SetRawData(const void* src_buffer, uint32 src_buffer_size);
-
#if defined(OS_CHROMEOS)
virtual bool SaveToFD(const base::FileDescriptor& fd) const;
#endif // if defined(OS_CHROMEOS)
@@ -75,9 +77,16 @@ class PdfPsMetafile : public NativeMetafile {
cairo_t* context_;
// Buffer stores PDF contents for entire PDF file.
- std::string data_;
- // Buffer stores raw PDF contents set by SetRawPageData.
- std::string raw_override_data_;
+ std::string cairo_data_;
+ // Buffer stores PDF contents. It can only be populated from InitFromData().
+ // Any calls to StartPage(), FinishPage(), FinishDocument() do not affect
+ // this buffer.
+ // Note: Such calls will result in DCHECK errors if Init() has not been called
+ // first.
+ std::string raw_data_;
+ // Points to the appropriate buffer depending on the way the object was
+ // initialized (Init() vs InitFromData()).
+ std::string* current_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 fa1a239..d76d801 100644
--- a/printing/pdf_ps_metafile_cairo_unittest.cc
+++ b/printing/pdf_ps_metafile_cairo_unittest.cc
@@ -73,7 +73,7 @@ TEST_F(PdfPsTest, Pdf) {
EXPECT_TRUE(pdf3.Init());
EXPECT_TRUE(pdf3.StartPage(gfx::Size(72, 73), gfx::Point(4, 5), 1));
std::string test_raw_data = "Dummy PDF";
- EXPECT_TRUE(pdf3.SetRawData(test_raw_data.c_str(), test_raw_data.size()));
+ EXPECT_TRUE(pdf3.InitFromData(test_raw_data.c_str(), test_raw_data.size()));
EXPECT_TRUE(pdf3.FinishPage());
pdf3.FinishDocument();
size = pdf3.GetDataSize();