summaryrefslogtreecommitdiffstats
path: root/printing
diff options
context:
space:
mode:
authorvandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-22 06:36:58 +0000
committervandebo@chromium.org <vandebo@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-22 06:36:58 +0000
commit481edc5493d2a5bd85ee882b3cf1a21f487a2a94 (patch)
treeb35c073123e217e8306d0d96467a7297bc7e6991 /printing
parent12b251a3514d18fc598766a1277bb3ca36bc9448 (diff)
downloadchromium_src-481edc5493d2a5bd85ee882b3cf1a21f487a2a94.zip
chromium_src-481edc5493d2a5bd85ee882b3cf1a21f487a2a94.tar.gz
chromium_src-481edc5493d2a5bd85ee882b3cf1a21f487a2a94.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 Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=78666 Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=78669 Review URL: http://codereview.chromium.org/6695013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78969 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 8609c34..c411a40 100644
--- a/printing/emf_win.cc
+++ b/printing/emf_win.cc
@@ -53,41 +53,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::FinishDocument() {
DCHECK(!emf_ && hdc_);
emf_ = CloseEnhMetaFile(hdc_);
@@ -96,14 +94,6 @@ bool Emf::FinishDocument() {
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 f2ccffd..f4ee30f 100644
--- a/printing/emf_win.h
+++ b/printing/emf_win.h
@@ -29,10 +29,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 skia::PlatformDevice* StartPageForVectorCanvas(
@@ -68,14 +78,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;
@@ -85,16 +87,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 9f2899b..2492104 100644
--- a/printing/emf_win_unittest.cc
+++ b/printing/emf_win_unittest.cc
@@ -45,26 +45,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.FinishDocument());
- 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.FinishDocument());
+ 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));
@@ -128,28 +128,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(gfx::Size(), gfx::Point(), 1));
- ::Rectangle(emf.context(), 10, 10, 190, 190);
- EXPECT_TRUE(emf.FinishPage());
- --pages;
- }
- EXPECT_TRUE(emf.FinishDocument());
- 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(gfx::Size(), gfx::Point(), 1));
+ ::Rectangle(emf.context(), 10, 10, 190, 190);
+ EXPECT_TRUE(emf.FinishPage());
+ --pages;
+ }
+ EXPECT_TRUE(emf.FinishDocument());
+ 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());
@@ -162,41 +165,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.FinishDocument());
-
- 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.FinishDocument());
+ 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 742ee27..6623343 100644
--- a/printing/native_metafile.h
+++ b/printing/native_metafile.h
@@ -98,29 +98,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