| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "printing/emf_win.h" | 5 #include "printing/emf_win.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 ExtEscape(dc, escape, size, reinterpret_cast<LPCSTR>(bits), | 43 ExtEscape(dc, escape, size, reinterpret_cast<LPCSTR>(bits), |
| 44 sizeof(supported), reinterpret_cast<LPSTR>(&supported)); | 44 sizeof(supported), reinterpret_cast<LPSTR>(&supported)); |
| 45 } | 45 } |
| 46 return !!supported; | 46 return !!supported; |
| 47 } | 47 } |
| 48 | 48 |
| 49 Emf::Emf() : emf_(NULL), hdc_(NULL) { | 49 Emf::Emf() : emf_(NULL), hdc_(NULL) { |
| 50 } | 50 } |
| 51 | 51 |
| 52 Emf::~Emf() { | 52 Emf::~Emf() { |
| 53 CloseEmf(); | 53 Close(); |
| 54 DCHECK(!emf_ && !hdc_); | 54 DCHECK(!emf_ && !hdc_); |
| 55 } | 55 } |
| 56 | 56 |
| 57 bool Emf::Init(const void* src_buffer, uint32 src_buffer_size) { | 57 bool Emf::Init(const void* src_buffer, uint32 src_buffer_size) { |
| 58 DCHECK(!emf_ && !hdc_); | 58 DCHECK(!emf_ && !hdc_); |
| 59 emf_ = SetEnhMetaFileBits(src_buffer_size, | 59 emf_ = SetEnhMetaFileBits(src_buffer_size, |
| 60 reinterpret_cast<const BYTE*>(src_buffer)); | 60 reinterpret_cast<const BYTE*>(src_buffer)); |
| 61 return emf_ != NULL; | 61 return emf_ != NULL; |
| 62 } | 62 } |
| 63 | 63 |
| 64 bool Emf::CreateDc(HDC sibling, const RECT* rect) { | 64 bool Emf::CreateDc(HDC sibling, const RECT* rect) { |
| 65 DCHECK(!emf_ && !hdc_); | 65 DCHECK(!emf_ && !hdc_); |
| 66 hdc_ = CreateEnhMetaFile(sibling, NULL, rect, NULL); | 66 hdc_ = CreateEnhMetaFile(sibling, NULL, rect, NULL); |
| 67 DCHECK(hdc_); | 67 DCHECK(hdc_); |
| 68 return hdc_ != NULL; | 68 return hdc_ != NULL; |
| 69 } | 69 } |
| 70 | 70 |
| 71 bool Emf::CreateFileBackedDc(HDC sibling, const RECT* rect, | 71 bool Emf::CreateFileBackedDc(HDC sibling, |
| 72 const RECT* rect, |
| 72 const FilePath& path) { | 73 const FilePath& path) { |
| 73 DCHECK(!emf_ && !hdc_); | 74 DCHECK(!emf_ && !hdc_); |
| 74 DCHECK(!path.empty()); | 75 DCHECK(!path.empty()); |
| 75 hdc_ = CreateEnhMetaFile(sibling, path.value().c_str(), rect, NULL); | 76 hdc_ = CreateEnhMetaFile(sibling, path.value().c_str(), rect, NULL); |
| 76 DCHECK(hdc_); | 77 DCHECK(hdc_); |
| 77 return hdc_ != NULL; | 78 return hdc_ != NULL; |
| 78 } | 79 } |
| 79 | 80 |
| 80 bool Emf::CreateFromFile(const FilePath& metafile_path) { | 81 bool Emf::CreateFromFile(const FilePath& metafile_path) { |
| 81 DCHECK(!emf_ && !hdc_); | 82 DCHECK(!emf_ && !hdc_); |
| 82 emf_ = GetEnhMetaFile(metafile_path.value().c_str()); | 83 emf_ = GetEnhMetaFile(metafile_path.value().c_str()); |
| 83 DCHECK(emf_); | 84 DCHECK(emf_); |
| 84 return emf_ != NULL; | 85 return emf_ != NULL; |
| 85 } | 86 } |
| 86 | 87 |
| 87 | 88 |
| 88 bool Emf::CloseDc() { | 89 bool Emf::CloseDc() { |
| 89 DCHECK(!emf_ && hdc_); | 90 DCHECK(!emf_ && hdc_); |
| 90 emf_ = CloseEnhMetaFile(hdc_); | 91 emf_ = CloseEnhMetaFile(hdc_); |
| 91 DCHECK(emf_); | 92 DCHECK(emf_); |
| 92 hdc_ = NULL; | 93 hdc_ = NULL; |
| 93 return emf_ != NULL; | 94 return emf_ != NULL; |
| 94 } | 95 } |
| 95 | 96 |
| 96 void Emf::CloseEmf() { | 97 void Emf::Close() { |
| 97 DCHECK(!hdc_); | 98 DCHECK(!hdc_); |
| 98 if (emf_) { | 99 if (emf_) { |
| 99 DeleteEnhMetaFile(emf_); | 100 DeleteEnhMetaFile(emf_); |
| 100 emf_ = NULL; | 101 emf_ = NULL; |
| 101 } | 102 } |
| 102 } | 103 } |
| 103 | 104 |
| 104 bool Emf::Playback(HDC hdc, const RECT* rect) const { | 105 bool Emf::Playback(HDC hdc, const RECT* rect) const { |
| 105 DCHECK(emf_ && !hdc_); | 106 DCHECK(emf_ && !hdc_); |
| 106 RECT bounds; | 107 RECT bounds; |
| 107 if (!rect) { | 108 if (!rect) { |
| 108 // Get the natural bounds of the EMF buffer. | 109 // Get the natural bounds of the EMF buffer. |
| 109 bounds = GetBounds().ToRECT(); | 110 bounds = GetPageBounds().ToRECT(); |
| 110 rect = &bounds; | 111 rect = &bounds; |
| 111 } | 112 } |
| 112 return PlayEnhMetaFile(hdc, emf_, rect) != 0; | 113 return PlayEnhMetaFile(hdc, emf_, rect) != 0; |
| 113 } | 114 } |
| 114 | 115 |
| 115 bool Emf::SafePlayback(HDC context) const { | 116 bool Emf::SafePlayback(HDC context) const { |
| 116 DCHECK(emf_ && !hdc_); | 117 DCHECK(emf_ && !hdc_); |
| 117 XFORM base_matrix; | 118 XFORM base_matrix; |
| 118 if (!GetWorldTransform(context, &base_matrix)) { | 119 if (!GetWorldTransform(context, &base_matrix)) { |
| 119 NOTREACHED(); | 120 NOTREACHED(); |
| 120 return false; | 121 return false; |
| 121 } | 122 } |
| 122 return EnumEnhMetaFile(context, | 123 return EnumEnhMetaFile(context, |
| 123 emf_, | 124 emf_, |
| 124 &Emf::SafePlaybackProc, | 125 &Emf::SafePlaybackProc, |
| 125 reinterpret_cast<void*>(&base_matrix), | 126 reinterpret_cast<void*>(&base_matrix), |
| 126 &GetBounds().ToRECT()) != 0; | 127 &GetPageBounds().ToRECT()) != 0; |
| 127 } | 128 } |
| 128 | 129 |
| 129 gfx::Rect Emf::GetBounds() const { | 130 gfx::Rect Emf::GetPageBounds() const { |
| 131 return GetPageBounds(1); |
| 132 } |
| 133 |
| 134 gfx::Rect Emf::GetPageBounds(unsigned int page_number) const { |
| 130 DCHECK(emf_ && !hdc_); | 135 DCHECK(emf_ && !hdc_); |
| 131 ENHMETAHEADER header; | 136 ENHMETAHEADER header; |
| 132 if (GetEnhMetaFileHeader(emf_, sizeof(header), &header) != sizeof(header)) { | 137 if (GetEnhMetaFileHeader(emf_, sizeof(header), &header) != sizeof(header)) { |
| 133 NOTREACHED(); | 138 NOTREACHED(); |
| 134 return gfx::Rect(); | 139 return gfx::Rect(); |
| 135 } | 140 } |
| 136 if (header.rclBounds.left == 0 && | 141 if (header.rclBounds.left == 0 && |
| 137 header.rclBounds.top == 0 && | 142 header.rclBounds.top == 0 && |
| 138 header.rclBounds.right == -1 && | 143 header.rclBounds.right == -1 && |
| 139 header.rclBounds.bottom == -1) { | 144 header.rclBounds.bottom == -1) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 166 uint32 size = GetDataSize(); | 171 uint32 size = GetDataSize(); |
| 167 if (!size) | 172 if (!size) |
| 168 return false; | 173 return false; |
| 169 | 174 |
| 170 buffer->resize(size); | 175 buffer->resize(size); |
| 171 if (!GetData(&buffer->front(), size)) | 176 if (!GetData(&buffer->front(), size)) |
| 172 return false; | 177 return false; |
| 173 return true; | 178 return true; |
| 174 } | 179 } |
| 175 | 180 |
| 176 bool Emf::SaveTo(const std::wstring& filename) const { | 181 bool Emf::SaveTo(const FilePath& file_path) const { |
| 177 HANDLE file = CreateFile(filename.c_str(), GENERIC_WRITE, | 182 HANDLE file = CreateFile(file_path.value().c_str(), GENERIC_WRITE, |
| 178 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | 183 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 179 CREATE_ALWAYS, 0, NULL); | 184 CREATE_ALWAYS, 0, NULL); |
| 180 if (file == INVALID_HANDLE_VALUE) | 185 if (file == INVALID_HANDLE_VALUE) |
| 181 return false; | 186 return false; |
| 182 | 187 |
| 183 bool success = false; | 188 bool success = false; |
| 184 std::vector<uint8> buffer; | 189 std::vector<uint8> buffer; |
| 185 if (GetData(&buffer)) { | 190 if (GetData(&buffer)) { |
| 186 DWORD written = 0; | 191 DWORD written = 0; |
| 187 if (WriteFile(file, &*buffer.begin(), static_cast<DWORD>(buffer.size()), | 192 if (WriteFile(file, &*buffer.begin(), static_cast<DWORD>(buffer.size()), |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 | 416 |
| 412 bool Emf::StartPage() { | 417 bool Emf::StartPage() { |
| 413 DCHECK(hdc_); | 418 DCHECK(hdc_); |
| 414 if (!hdc_) | 419 if (!hdc_) |
| 415 return false; | 420 return false; |
| 416 PageBreakRecord record(PageBreakRecord::START_PAGE); | 421 PageBreakRecord record(PageBreakRecord::START_PAGE); |
| 417 return !!GdiComment(hdc_, sizeof(record), | 422 return !!GdiComment(hdc_, sizeof(record), |
| 418 reinterpret_cast<const BYTE *>(&record)); | 423 reinterpret_cast<const BYTE *>(&record)); |
| 419 } | 424 } |
| 420 | 425 |
| 421 bool Emf::EndPage() { | 426 bool Emf::FinishPage() { |
| 422 DCHECK(hdc_); | 427 DCHECK(hdc_); |
| 423 if (!hdc_) | 428 if (!hdc_) |
| 424 return false; | 429 return false; |
| 425 PageBreakRecord record(PageBreakRecord::END_PAGE); | 430 PageBreakRecord record(PageBreakRecord::END_PAGE); |
| 426 return !!GdiComment(hdc_, sizeof(record), | 431 return !!GdiComment(hdc_, sizeof(record), |
| 427 reinterpret_cast<const BYTE *>(&record)); | 432 reinterpret_cast<const BYTE *>(&record)); |
| 428 } | 433 } |
| 429 | 434 |
| 430 | 435 |
| 431 Emf::Enumerator::Enumerator(const Emf& emf, HDC context, const RECT* rect) { | 436 Emf::Enumerator::Enumerator(const Emf& emf, HDC context, const RECT* rect) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 } else { | 472 } else { |
| 468 DCHECK_EQ(emf.context_.handle_table, handle_table); | 473 DCHECK_EQ(emf.context_.handle_table, handle_table); |
| 469 DCHECK_EQ(emf.context_.objects_count, objects_count); | 474 DCHECK_EQ(emf.context_.objects_count, objects_count); |
| 470 DCHECK_EQ(emf.context_.hdc, hdc); | 475 DCHECK_EQ(emf.context_.hdc, hdc); |
| 471 } | 476 } |
| 472 emf.items_.push_back(Record(&emf.context_, record)); | 477 emf.items_.push_back(Record(&emf.context_, record)); |
| 473 return 1; | 478 return 1; |
| 474 } | 479 } |
| 475 | 480 |
| 476 } // namespace printing | 481 } // namespace printing |
| OLD | NEW |