diff options
-rw-r--r-- | printing/emf_win.cc | 19 | ||||
-rw-r--r-- | printing/emf_win.h | 8 | ||||
-rw-r--r-- | printing/emf_win_unittest.cc | 37 |
3 files changed, 62 insertions, 2 deletions
diff --git a/printing/emf_win.cc b/printing/emf_win.cc index aa1832b..de3b7db 100644 --- a/printing/emf_win.cc +++ b/printing/emf_win.cc @@ -4,6 +4,7 @@ #include "printing/emf_win.h" +#include "base/file_path.h" #include "base/histogram.h" #include "base/logging.h" #include "base/scoped_ptr.h" @@ -60,6 +61,16 @@ bool Emf::CreateDc(HDC sibling, const RECT* rect) { return hdc_ != NULL; } +bool Emf::CreateFileBackedDc(HDC sibling, const RECT* rect, + const FilePath& path) { + DCHECK(!emf_ && !hdc_); + DCHECK(!path.empty()); + hdc_ = CreateEnhMetaFile(sibling, path.value().c_str(), rect, NULL); + DCHECK(hdc_); + return hdc_ != NULL; +} + + bool Emf::CreateFromData(const void* buffer, uint32 size) { DCHECK(!emf_ && !hdc_); emf_ = SetEnhMetaFileBits(size, reinterpret_cast<const BYTE*>(buffer)); @@ -67,6 +78,14 @@ bool Emf::CreateFromData(const void* buffer, uint32 size) { return emf_ != NULL; } +bool Emf::CreateFromFile(const FilePath& metafile_path) { + DCHECK(!emf_ && !hdc_); + emf_ = GetEnhMetaFile(metafile_path.value().c_str()); + DCHECK(emf_); + return emf_ != NULL; +} + + bool Emf::CloseDc() { DCHECK(!emf_ && hdc_); emf_ = CloseEnhMetaFile(hdc_); diff --git a/printing/emf_win.h b/printing/emf_win.h index fc947cb..459886e 100644 --- a/printing/emf_win.h +++ b/printing/emf_win.h @@ -10,6 +10,8 @@ #include "base/basictypes.h" +class FilePath; + namespace gfx { class Rect; } @@ -33,9 +35,15 @@ class Emf { // optional. 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); + // Load a EMF data stream. buffer contains EMF data. bool CreateFromData(const void* buffer, uint32 size); + // Load an EMF file. + 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 // convert to PDF or PS. diff --git a/printing/emf_win_unittest.cc b/printing/emf_win_unittest.cc index 3b4c473..4c1247b 100644 --- a/printing/emf_win_unittest.cc +++ b/printing/emf_win_unittest.cc @@ -33,11 +33,11 @@ class EmfPrintingTest : public testing::Test { } }; +const uint32 EMF_HEADER_SIZE = 128; + } // namespace TEST(EmfTest, DC) { - static const uint32 EMF_HEADER_SIZE = 128; - // Simplest use case. printing::Emf emf; RECT rect = {100, 100, 200, 200}; @@ -154,3 +154,36 @@ TEST_F(EmfPrintingTest, PageBreak) { } } +TEST(EmfTest, FileBackedDC) { + // Simplest use case. + printing::Emf emf; + RECT rect = {100, 100, 200, 200}; + HDC hdc = CreateCompatibleDC(NULL); + EXPECT_TRUE(hdc != NULL); + FilePath metafile_path; + EXPECT_TRUE(file_util::CreateTemporaryFile(&metafile_path)); + EXPECT_TRUE(emf.CreateFileBackedDc(hdc, &rect, metafile_path)); + EXPECT_TRUE(emf.hdc() != NULL); + // In theory, you'd use the HDC with GDI functions here. + EXPECT_TRUE(emf.CloseDc()); + + uint32 size = emf.GetDataSize(); + EXPECT_EQ(size, EMF_HEADER_SIZE); + std::vector<BYTE> data; + EXPECT_TRUE(emf.GetData(&data)); + EXPECT_EQ(data.size(), size); + emf.CloseEmf(); + int64 file_size = 0; + file_util::GetFileSize(metafile_path, &file_size); + EXPECT_EQ(size, file_size); + EXPECT_TRUE(DeleteDC(hdc)); + + // Playback the data. + hdc = CreateCompatibleDC(NULL); + EXPECT_TRUE(hdc); + EXPECT_TRUE(emf.CreateFromFile(metafile_path)); + RECT output_rect = {0, 0, 10, 10}; + EXPECT_TRUE(emf.Playback(hdc, &output_rect)); + EXPECT_TRUE(DeleteDC(hdc)); +} + |