summaryrefslogtreecommitdiffstats
path: root/printing
diff options
context:
space:
mode:
authorvandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-18 04:58:36 +0000
committervandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-18 04:58:36 +0000
commitacc134844001b556ef162ad267dac826b3000ff0 (patch)
treea50d9986b827e6c4784d63e0930e28b8ca0f7374 /printing
parent55f2c51627eb66c3a742d19d5ed735966d118827 (diff)
downloadchromium_src-acc134844001b556ef162ad267dac826b3000ff0.zip
chromium_src-acc134844001b556ef162ad267dac826b3000ff0.tar.gz
chromium_src-acc134844001b556ef162ad267dac826b3000ff0.tar.bz2
Cleanup NativeMetafile (win) interface and EMF class.
- Rename CreateDc to Init() and remove unused argument (all non-test calls were CreateDc(NULL, NULL). [This matches cross platform interface.] - Remove CreateFileBackedDc from the NativeMetafile interface and make InitToFile() in the EMF class. - Remove CreateFromFile from the NativeMetafile interface and make it InitFromFile() in the EMF class. - Move the CloseEmf method into the destructor, making the Emf class a use once class (matches actual use). BUG=NONE TEST=NONE Review URL: http://codereview.chromium.org/6695013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78666 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
-rw-r--r--printing/emf_win.cc42
-rw-r--r--printing/emf_win.h29
-rw-r--r--printing/emf_win_unittest.cc101
-rw-r--r--printing/native_metafile.h23
4 files changed, 78 insertions, 117 deletions
diff --git a/printing/emf_win.cc b/printing/emf_win.cc
index 6fea291..7e3dbeb 100644
--- a/printing/emf_win.cc
+++ b/printing/emf_win.cc
@@ -50,41 +50,39 @@ Emf::Emf() : emf_(NULL), hdc_(NULL) {
}
Emf::~Emf() {
- CloseEmf();
- DCHECK(!emf_ && !hdc_);
+ DCHECK(!hdc_);
+ if (emf_)
+ DeleteEnhMetaFile(emf_);
}
-bool Emf::InitFromData(const void* src_buffer, uint32 src_buffer_size) {
+bool Emf::InitToFile(const FilePath& metafile_path) {
DCHECK(!emf_ && !hdc_);
- emf_ = SetEnhMetaFileBits(src_buffer_size,
- reinterpret_cast<const BYTE*>(src_buffer));
- return emf_ != NULL;
+ hdc_ = CreateEnhMetaFile(NULL, metafile_path.value().c_str(), NULL, NULL);
+ DCHECK(hdc_);
+ return hdc_ != NULL;
}
-bool Emf::CreateDc(HDC sibling, const RECT* rect) {
+bool Emf::InitFromFile(const FilePath& metafile_path) {
DCHECK(!emf_ && !hdc_);
- hdc_ = CreateEnhMetaFile(sibling, NULL, rect, NULL);
- DCHECK(hdc_);
- return hdc_ != NULL;
+ emf_ = GetEnhMetaFile(metafile_path.value().c_str());
+ DCHECK(emf_);
+ return emf_ != NULL;
}
-bool Emf::CreateFileBackedDc(HDC sibling, const RECT* rect,
- const FilePath& path) {
+bool Emf::Init() {
DCHECK(!emf_ && !hdc_);
- DCHECK(!path.empty());
- hdc_ = CreateEnhMetaFile(sibling, path.value().c_str(), rect, NULL);
+ hdc_ = CreateEnhMetaFile(NULL, NULL, NULL, NULL);
DCHECK(hdc_);
return hdc_ != NULL;
}
-bool Emf::CreateFromFile(const FilePath& metafile_path) {
+bool Emf::InitFromData(const void* src_buffer, uint32 src_buffer_size) {
DCHECK(!emf_ && !hdc_);
- emf_ = GetEnhMetaFile(metafile_path.value().c_str());
- DCHECK(emf_);
+ emf_ = SetEnhMetaFileBits(src_buffer_size,
+ reinterpret_cast<const BYTE*>(src_buffer));
return emf_ != NULL;
}
-
bool Emf::Close() {
DCHECK(!emf_ && hdc_);
emf_ = CloseEnhMetaFile(hdc_);
@@ -93,14 +91,6 @@ bool Emf::Close() {
return emf_ != NULL;
}
-void Emf::CloseEmf() {
- DCHECK(!hdc_);
- if (emf_) {
- DeleteEnhMetaFile(emf_);
- emf_ = NULL;
- }
-}
-
bool Emf::Playback(HDC hdc, const RECT* rect) const {
DCHECK(emf_ && !hdc_);
RECT bounds;
diff --git a/printing/emf_win.h b/printing/emf_win.h
index 5150356..0503e6f 100644
--- a/printing/emf_win.h
+++ b/printing/emf_win.h
@@ -27,10 +27,20 @@ class Emf : public NativeMetafile {
class Enumerator;
struct EnumerationContext;
+ // Generates a virtual HDC that will record every GDI commands and compile
+ // it in a EMF data stream.
+ Emf();
virtual ~Emf();
+ // Generates a new metafile that will record every GDI command, and will
+ // be saved to |metafile_path|.
+ virtual bool InitToFile(const FilePath& metafile_path);
+
+ // Initializes the Emf with the data in |metafile_path|.
+ virtual bool InitFromFile(const FilePath& metafile_path);
+
// NativeMetafile methods.
- virtual bool Init() { return true; }
+ virtual bool Init();
virtual bool InitFromData(const void* src_buffer, uint32 src_buffer_size);
virtual bool StartPage();
@@ -57,14 +67,6 @@ class Emf : public NativeMetafile {
return hdc_;
}
- virtual bool CreateDc(HDC sibling, const RECT* rect);
- virtual bool CreateFileBackedDc(HDC sibling,
- const RECT* rect,
- const FilePath& path);
- virtual bool CreateFromFile(const FilePath& file_path);
-
- virtual void CloseEmf();
-
virtual bool Playback(HDC hdc, const RECT* rect) const;
virtual bool SafePlayback(HDC hdc) const;
@@ -74,16 +76,7 @@ class Emf : public NativeMetafile {
return emf_;
}
- 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 2f4e2ae..16ca82e 100644
--- a/printing/emf_win_unittest.cc
+++ b/printing/emf_win_unittest.cc
@@ -43,26 +43,26 @@ namespace printing {
TEST(EmfTest, DC) {
// Simplest use case.
- printing::Emf emf;
- RECT rect = {100, 100, 200, 200};
- HDC hdc = CreateCompatibleDC(NULL);
- EXPECT_TRUE(hdc != NULL);
- EXPECT_TRUE(emf.CreateDc(hdc, &rect));
- EXPECT_TRUE(emf.context() != NULL);
- // In theory, you'd use the HDC with GDI functions here.
- EXPECT_TRUE(emf.Close());
- uint32 size = emf.GetDataSize();
- EXPECT_EQ(size, EMF_HEADER_SIZE);
+ uint32 size;
std::vector<BYTE> data;
- EXPECT_TRUE(emf.GetData(&data));
- EXPECT_EQ(data.size(), size);
- emf.CloseEmf();
- EXPECT_TRUE(DeleteDC(hdc));
+ {
+ printing::Emf emf;
+ EXPECT_TRUE(emf.Init());
+ EXPECT_TRUE(emf.context() != NULL);
+ // An empty EMF is invalid, so we put at least a rectangle in it.
+ ::Rectangle(emf.context(), 10, 10, 190, 190);
+ EXPECT_TRUE(emf.Close());
+ size = emf.GetDataSize();
+ EXPECT_GT(size, EMF_HEADER_SIZE);
+ EXPECT_TRUE(emf.GetData(&data));
+ EXPECT_EQ(data.size(), size);
+ }
// Playback the data.
- hdc = CreateCompatibleDC(NULL);
- EXPECT_TRUE(hdc);
+ printing::Emf emf;
EXPECT_TRUE(emf.InitFromData(&data.front(), size));
+ HDC hdc = CreateCompatibleDC(NULL);
+ EXPECT_TRUE(hdc);
RECT output_rect = {0, 0, 10, 10};
EXPECT_TRUE(emf.Playback(hdc, &output_rect));
EXPECT_TRUE(DeleteDC(hdc));
@@ -126,28 +126,31 @@ TEST_F(EmfPrintingTest, PageBreak) {
CreateDC(L"WINSPOOL", L"UnitTest Printer", NULL, NULL));
if (!dc.Get())
return;
- printing::Emf emf;
- EXPECT_TRUE(emf.CreateDc(dc.Get(), NULL));
- EXPECT_TRUE(emf.context() != NULL);
- int pages = 3;
- while (pages) {
- EXPECT_TRUE(emf.StartPage());
- ::Rectangle(emf.context(), 10, 10, 190, 190);
- EXPECT_TRUE(emf.FinishPage());
- --pages;
- }
- EXPECT_TRUE(emf.Close());
- uint32 size = emf.GetDataSize();
+ uint32 size;
std::vector<BYTE> data;
- EXPECT_TRUE(emf.GetData(&data));
- EXPECT_EQ(data.size(), size);
- emf.CloseEmf();
+ {
+ printing::Emf emf;
+ EXPECT_TRUE(emf.Init());
+ EXPECT_TRUE(emf.context() != NULL);
+ int pages = 3;
+ while (pages) {
+ EXPECT_TRUE(emf.StartPage());
+ ::Rectangle(emf.context(), 10, 10, 190, 190);
+ EXPECT_TRUE(emf.FinishPage());
+ --pages;
+ }
+ EXPECT_TRUE(emf.Close());
+ size = emf.GetDataSize();
+ EXPECT_TRUE(emf.GetData(&data));
+ EXPECT_EQ(data.size(), size);
+ }
// Playback the data.
DOCINFO di = {0};
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = L"Test Job";
int job_id = ::StartDoc(dc.Get(), &di);
+ printing::Emf emf;
EXPECT_TRUE(emf.InitFromData(&data.front(), size));
EXPECT_TRUE(emf.SafePlayback(dc.Get()));
::EndDoc(dc.Get());
@@ -160,41 +163,39 @@ TEST_F(EmfPrintingTest, PageBreak) {
}
}
-TEST(EmfTest, FileBackedDC) {
+TEST(EmfTest, FileBackedEmf) {
// Simplest use case.
- printing::Emf emf;
- RECT rect = {100, 100, 200, 200};
- HDC hdc = CreateCompatibleDC(NULL);
- EXPECT_TRUE(hdc != NULL);
ScopedTempDir scratch_metafile_dir;
ASSERT_TRUE(scratch_metafile_dir.CreateUniqueTempDir());
FilePath metafile_path;
EXPECT_TRUE(file_util::CreateTemporaryFileInDir(scratch_metafile_dir.path(),
&metafile_path));
- EXPECT_TRUE(emf.CreateFileBackedDc(hdc, &rect, metafile_path));
- EXPECT_TRUE(emf.context() != NULL);
- // In theory, you'd use the HDC with GDI functions here.
- EXPECT_TRUE(emf.Close());
-
- uint32 size = emf.GetDataSize();
- EXPECT_EQ(size, EMF_HEADER_SIZE);
+ uint32 size;
std::vector<BYTE> data;
- EXPECT_TRUE(emf.GetData(&data));
- EXPECT_EQ(data.size(), size);
- emf.CloseEmf();
+ {
+ printing::Emf emf;
+ EXPECT_TRUE(emf.InitToFile(metafile_path));
+ EXPECT_TRUE(emf.context() != NULL);
+ // An empty EMF is invalid, so we put at least a rectangle in it.
+ ::Rectangle(emf.context(), 10, 10, 190, 190);
+ EXPECT_TRUE(emf.Close());
+ size = emf.GetDataSize();
+ EXPECT_GT(size, EMF_HEADER_SIZE);
+ EXPECT_TRUE(emf.GetData(&data));
+ EXPECT_EQ(data.size(), size);
+ }
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);
+ HDC hdc = CreateCompatibleDC(NULL);
EXPECT_TRUE(hdc);
- EXPECT_TRUE(emf.CreateFromFile(metafile_path));
+ printing::Emf emf;
+ EXPECT_TRUE(emf.InitFromFile(metafile_path));
RECT output_rect = {0, 0, 10, 10};
EXPECT_TRUE(emf.Playback(hdc, &output_rect));
EXPECT_TRUE(DeleteDC(hdc));
- emf.CloseEmf();
}
} // namespace printing
diff --git a/printing/native_metafile.h b/printing/native_metafile.h
index a076b32..08b3277 100644
--- a/printing/native_metafile.h
+++ b/printing/native_metafile.h
@@ -101,29 +101,6 @@ class NativeMetafile {
virtual gfx::NativeDrawingContext context() const = 0;
#if defined(OS_WIN)
- // 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(gfx::NativeDrawingContext sibling,
- const RECT* rect) = 0;
-
- // Similar to the above method but the metafile is backed by a file.
- virtual bool CreateFileBackedDc(gfx::NativeDrawingContext sibling,
- const RECT* rect,
- const FilePath& 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 or PS.
- // Load a metafile fromdisk.
- virtual bool CreateFromFile(const FilePath& metafile_path) = 0;
-
- // Closes the HDC created by CreateDc() and generates the compiled EMF
- // data.
- 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