Skip to content

Commit

Permalink
Added methods to create and read from file-backed metafiles to the Wi…
Browse files Browse the repository at this point in the history
…ndows Emf class.

BUG=None
TEST=Unittests

Review URL: http://codereview.chromium.org/3538003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60994 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
sanjeevr@chromium.org committed Sep 29, 2010
1 parent 1c608a4 commit e6cddc5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
19 changes: 19 additions & 0 deletions printing/emf_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -60,13 +61,31 @@ 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));
DCHECK(emf_);
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_);
Expand Down
8 changes: 8 additions & 0 deletions printing/emf_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "base/basictypes.h"

class FilePath;

namespace gfx {
class Rect;
}
Expand All @@ -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.
Expand Down
37 changes: 35 additions & 2 deletions printing/emf_win_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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));
}

0 comments on commit e6cddc5

Please sign in to comment.