Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Unified Diff: printing/emf_win.cc

Issue 6611032: Unifying NativeMetafile class interface (as much as possible) for Linux, Mac, Win (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes for CHROMEOS Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: printing/emf_win.cc
diff --git a/printing/emf_win.cc b/printing/emf_win.cc
index f2b22fa13f8b865b4c01924254fade83077b0a1b..a16791ceb754288bc5ab5cc0ea2dc26eee5d3abe 100644
--- a/printing/emf_win.cc
+++ b/printing/emf_win.cc
@@ -50,7 +50,7 @@ Emf::Emf() : emf_(NULL), hdc_(NULL) {
}
Emf::~Emf() {
- CloseEmf();
+ Close();
DCHECK(!emf_ && !hdc_);
}
@@ -61,6 +61,89 @@ bool Emf::Init(const void* src_buffer, uint32 src_buffer_size) {
return emf_ != NULL;
}
vandebo (ex-Chrome) 2011/03/12 01:21:06 I don't remember if I specifically said to reorder
dpapad 2011/03/14 18:02:38 Done.
+uint32 Emf::GetDataSize() const {
+ DCHECK(emf_ && !hdc_);
+ return GetEnhMetaFileBits(emf_, 0, NULL);
+}
+
+bool Emf::GetData(void* buffer, uint32 size) const {
+ DCHECK(emf_ && !hdc_);
+ DCHECK(buffer && size);
+ uint32 size2 =
+ GetEnhMetaFileBits(emf_, size, reinterpret_cast<BYTE*>(buffer));
+ DCHECK(size2 == size);
+ return size2 == size && size2 != 0;
+}
+
+bool Emf::FinishPage() {
+ DCHECK(hdc_);
+ if (!hdc_)
+ return false;
+ PageBreakRecord record(PageBreakRecord::END_PAGE);
+ return !!GdiComment(hdc_, sizeof(record),
+ reinterpret_cast<const BYTE *>(&record));
+}
+
+void Emf::Close() {
+ DCHECK(!hdc_);
+ if (emf_) {
+ DeleteEnhMetaFile(emf_);
+ emf_ = NULL;
+ }
+}
+
+bool Emf::SaveTo(const FilePath& file_path) const {
+ HANDLE file = CreateFile(file_path.value().c_str(), GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
+ CREATE_ALWAYS, 0, NULL);
+ if (file == INVALID_HANDLE_VALUE)
+ return false;
+
+ bool success = false;
+ std::vector<uint8> buffer;
+ if (GetData(&buffer)) {
+ DWORD written = 0;
+ if (WriteFile(file, &*buffer.begin(), static_cast<DWORD>(buffer.size()),
+ &written, NULL) &&
+ written == buffer.size()) {
+ success = true;
+ }
+ }
+ CloseHandle(file);
+ return success;
+}
+
+gfx::Rect Emf::GetPageBounds(unsigned int page_number) const {
+ DCHECK(emf_ && !hdc_);
+ ENHMETAHEADER header;
+ if (GetEnhMetaFileHeader(emf_, sizeof(header), &header) != sizeof(header)) {
+ NOTREACHED();
+ return gfx::Rect();
+ }
+ if (header.rclBounds.left == 0 &&
+ header.rclBounds.top == 0 &&
+ header.rclBounds.right == -1 &&
+ header.rclBounds.bottom == -1) {
+ // A freshly created EMF buffer that has no drawing operation has invalid
+ // bounds. Instead of having an (0,0) size, it has a (-1,-1) size. Detect
+ // this special case and returns an empty Rect instead of an invalid one.
+ return gfx::Rect();
+ }
+ return gfx::Rect(header.rclBounds.left,
+ header.rclBounds.top,
+ header.rclBounds.right - header.rclBounds.left,
+ header.rclBounds.bottom - header.rclBounds.top);
+}
+
+bool Emf::StartPage() {
+ DCHECK(hdc_);
+ if (!hdc_)
+ return false;
+ PageBreakRecord record(PageBreakRecord::START_PAGE);
+ return !!GdiComment(hdc_, sizeof(record),
+ reinterpret_cast<const BYTE *>(&record));
+}
+
bool Emf::CreateDc(HDC sibling, const RECT* rect) {
DCHECK(!emf_ && !hdc_);
hdc_ = CreateEnhMetaFile(sibling, NULL, rect, NULL);
@@ -68,7 +151,8 @@ bool Emf::CreateDc(HDC sibling, const RECT* rect) {
return hdc_ != NULL;
}
-bool Emf::CreateFileBackedDc(HDC sibling, const RECT* rect,
+bool Emf::CreateFileBackedDc(HDC sibling,
+ const RECT* rect,
const FilePath& path) {
DCHECK(!emf_ && !hdc_);
DCHECK(!path.empty());
@@ -93,20 +177,12 @@ bool Emf::CloseDc() {
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;
if (!rect) {
// Get the natural bounds of the EMF buffer.
- bounds = GetBounds().ToRECT();
+ bounds = GetPageBounds(1).ToRECT();
rect = &bounds;
}
return PlayEnhMetaFile(hdc, emf_, rect) != 0;
@@ -123,43 +199,7 @@ bool Emf::SafePlayback(HDC context) const {
emf_,
&Emf::SafePlaybackProc,
reinterpret_cast<void*>(&base_matrix),
- &GetBounds().ToRECT()) != 0;
-}
-
-gfx::Rect Emf::GetBounds() const {
- DCHECK(emf_ && !hdc_);
- ENHMETAHEADER header;
- if (GetEnhMetaFileHeader(emf_, sizeof(header), &header) != sizeof(header)) {
- NOTREACHED();
- return gfx::Rect();
- }
- if (header.rclBounds.left == 0 &&
- header.rclBounds.top == 0 &&
- header.rclBounds.right == -1 &&
- header.rclBounds.bottom == -1) {
- // A freshly created EMF buffer that has no drawing operation has invalid
- // bounds. Instead of having an (0,0) size, it has a (-1,-1) size. Detect
- // this special case and returns an empty Rect instead of an invalid one.
- return gfx::Rect();
- }
- return gfx::Rect(header.rclBounds.left,
- header.rclBounds.top,
- header.rclBounds.right - header.rclBounds.left,
- header.rclBounds.bottom - header.rclBounds.top);
-}
-
-uint32 Emf::GetDataSize() const {
- DCHECK(emf_ && !hdc_);
- return GetEnhMetaFileBits(emf_, 0, NULL);
-}
-
-bool Emf::GetData(void* buffer, uint32 size) const {
- DCHECK(emf_ && !hdc_);
- DCHECK(buffer && size);
- uint32 size2 =
- GetEnhMetaFileBits(emf_, size, reinterpret_cast<BYTE*>(buffer));
- DCHECK(size2 == size);
- return size2 == size && size2 != 0;
+ &GetPageBounds(1).ToRECT()) != 0;
}
bool Emf::GetData(std::vector<uint8>* buffer) const {
@@ -173,27 +213,6 @@ bool Emf::GetData(std::vector<uint8>* buffer) const {
return true;
}
-bool Emf::SaveTo(const std::wstring& filename) const {
- HANDLE file = CreateFile(filename.c_str(), GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
- CREATE_ALWAYS, 0, NULL);
- if (file == INVALID_HANDLE_VALUE)
- return false;
-
- bool success = false;
- std::vector<uint8> buffer;
- if (GetData(&buffer)) {
- DWORD written = 0;
- if (WriteFile(file, &*buffer.begin(), static_cast<DWORD>(buffer.size()),
- &written, NULL) &&
- written == buffer.size()) {
- success = true;
- }
- }
- CloseHandle(file);
- return success;
-}
-
int CALLBACK Emf::SafePlaybackProc(HDC hdc,
HANDLETABLE* handle_table,
const ENHMETARECORD* record,
@@ -409,25 +428,6 @@ bool Emf::Record::SafePlayback(const XFORM* base_matrix) const {
return res;
}
-bool Emf::StartPage() {
- DCHECK(hdc_);
- if (!hdc_)
- return false;
- PageBreakRecord record(PageBreakRecord::START_PAGE);
- return !!GdiComment(hdc_, sizeof(record),
- reinterpret_cast<const BYTE *>(&record));
-}
-
-bool Emf::EndPage() {
- DCHECK(hdc_);
- if (!hdc_)
- return false;
- PageBreakRecord record(PageBreakRecord::END_PAGE);
- return !!GdiComment(hdc_, sizeof(record),
- reinterpret_cast<const BYTE *>(&record));
-}
-
-
Emf::Enumerator::Enumerator(const Emf& emf, HDC context, const RECT* rect) {
context_.handle_table = NULL;
context_.objects_count = 0;

Powered by Google App Engine
This is Rietveld 408576698