Chromium Code Reviews| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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, |
|
vandebo (ex-Chrome)
2011/03/14 20:24:25
Extra line break.
dpapad
2011/03/14 22:15:12
Done.
| |
| 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::Close() { |
| 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::CloseEmf() { |
| 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(1).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(1).ToRECT()) != 0; |
| 127 } | 128 } |
| 128 | 129 |
| 129 gfx::Rect Emf::GetBounds() const { | 130 gfx::Rect Emf::GetPageBounds(unsigned int page_number) const { |
| 130 DCHECK(emf_ && !hdc_); | 131 DCHECK(emf_ && !hdc_); |
|
vandebo (ex-Chrome)
2011/03/14 20:24:25
Add DCHECK_EQ(page_number, 1);
dpapad
2011/03/14 22:15:12
Done. static_cast is necessary to avoid compilatio
| |
| 131 ENHMETAHEADER header; | 132 ENHMETAHEADER header; |
| 132 if (GetEnhMetaFileHeader(emf_, sizeof(header), &header) != sizeof(header)) { | 133 if (GetEnhMetaFileHeader(emf_, sizeof(header), &header) != sizeof(header)) { |
| 133 NOTREACHED(); | 134 NOTREACHED(); |
| 134 return gfx::Rect(); | 135 return gfx::Rect(); |
| 135 } | 136 } |
| 136 if (header.rclBounds.left == 0 && | 137 if (header.rclBounds.left == 0 && |
| 137 header.rclBounds.top == 0 && | 138 header.rclBounds.top == 0 && |
| 138 header.rclBounds.right == -1 && | 139 header.rclBounds.right == -1 && |
| 139 header.rclBounds.bottom == -1) { | 140 header.rclBounds.bottom == -1) { |
| 140 // A freshly created EMF buffer that has no drawing operation has invalid | 141 // A freshly created EMF buffer that has no drawing operation has invalid |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 166 uint32 size = GetDataSize(); | 167 uint32 size = GetDataSize(); |
| 167 if (!size) | 168 if (!size) |
| 168 return false; | 169 return false; |
| 169 | 170 |
| 170 buffer->resize(size); | 171 buffer->resize(size); |
| 171 if (!GetData(&buffer->front(), size)) | 172 if (!GetData(&buffer->front(), size)) |
| 172 return false; | 173 return false; |
| 173 return true; | 174 return true; |
| 174 } | 175 } |
| 175 | 176 |
| 176 bool Emf::SaveTo(const std::wstring& filename) const { | 177 bool Emf::SaveTo(const FilePath& file_path) const { |
| 177 HANDLE file = CreateFile(filename.c_str(), GENERIC_WRITE, | 178 HANDLE file = CreateFile(file_path.value().c_str(), GENERIC_WRITE, |
| 178 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | 179 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 179 CREATE_ALWAYS, 0, NULL); | 180 CREATE_ALWAYS, 0, NULL); |
| 180 if (file == INVALID_HANDLE_VALUE) | 181 if (file == INVALID_HANDLE_VALUE) |
| 181 return false; | 182 return false; |
| 182 | 183 |
| 183 bool success = false; | 184 bool success = false; |
| 184 std::vector<uint8> buffer; | 185 std::vector<uint8> buffer; |
| 185 if (GetData(&buffer)) { | 186 if (GetData(&buffer)) { |
| 186 DWORD written = 0; | 187 DWORD written = 0; |
| 187 if (WriteFile(file, &*buffer.begin(), static_cast<DWORD>(buffer.size()), | 188 if (WriteFile(file, &*buffer.begin(), static_cast<DWORD>(buffer.size()), |
| 188 &written, NULL) && | 189 &written, NULL) && |
| 189 written == buffer.size()) { | 190 written == buffer.size()) { |
| 190 success = true; | 191 success = true; |
| 191 } | 192 } |
| 192 } | 193 } |
| 193 CloseHandle(file); | 194 CloseHandle(file); |
| 194 return success; | 195 return success; |
| 195 } | 196 } |
| 196 | 197 |
| 198 bool Emf::StartPage() { | |
|
vandebo (ex-Chrome)
2011/03/14 20:24:25
No reason to move these methods.
dpapad
2011/03/14 22:15:12
Done.
vandebo (ex-Chrome)
2011/03/14 22:55:05
The CR still shows them moved. They used to be be
| |
| 199 DCHECK(hdc_); | |
| 200 if (!hdc_) | |
| 201 return false; | |
| 202 PageBreakRecord record(PageBreakRecord::START_PAGE); | |
| 203 return !!GdiComment(hdc_, sizeof(record), | |
| 204 reinterpret_cast<const BYTE *>(&record)); | |
| 205 } | |
| 206 | |
| 207 bool Emf::FinishPage() { | |
| 208 DCHECK(hdc_); | |
| 209 if (!hdc_) | |
| 210 return false; | |
| 211 PageBreakRecord record(PageBreakRecord::END_PAGE); | |
| 212 return !!GdiComment(hdc_, sizeof(record), | |
| 213 reinterpret_cast<const BYTE *>(&record)); | |
| 214 } | |
| 215 | |
| 197 int CALLBACK Emf::SafePlaybackProc(HDC hdc, | 216 int CALLBACK Emf::SafePlaybackProc(HDC hdc, |
| 198 HANDLETABLE* handle_table, | 217 HANDLETABLE* handle_table, |
| 199 const ENHMETARECORD* record, | 218 const ENHMETARECORD* record, |
| 200 int objects_count, | 219 int objects_count, |
| 201 LPARAM param) { | 220 LPARAM param) { |
| 202 const XFORM* base_matrix = reinterpret_cast<const XFORM*>(param); | 221 const XFORM* base_matrix = reinterpret_cast<const XFORM*>(param); |
| 203 EnumerationContext context; | 222 EnumerationContext context; |
| 204 context.handle_table = handle_table; | 223 context.handle_table = handle_table; |
| 205 context.objects_count = objects_count; | 224 context.objects_count = objects_count; |
| 206 context.hdc = hdc; | 225 context.hdc = hdc; |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 402 break; | 421 break; |
| 403 } | 422 } |
| 404 default: { | 423 default: { |
| 405 res = Play(); | 424 res = Play(); |
| 406 break; | 425 break; |
| 407 } | 426 } |
| 408 } | 427 } |
| 409 return res; | 428 return res; |
| 410 } | 429 } |
| 411 | 430 |
| 412 bool Emf::StartPage() { | |
| 413 DCHECK(hdc_); | |
| 414 if (!hdc_) | |
| 415 return false; | |
| 416 PageBreakRecord record(PageBreakRecord::START_PAGE); | |
| 417 return !!GdiComment(hdc_, sizeof(record), | |
| 418 reinterpret_cast<const BYTE *>(&record)); | |
| 419 } | |
| 420 | |
| 421 bool Emf::EndPage() { | |
| 422 DCHECK(hdc_); | |
| 423 if (!hdc_) | |
| 424 return false; | |
| 425 PageBreakRecord record(PageBreakRecord::END_PAGE); | |
| 426 return !!GdiComment(hdc_, sizeof(record), | |
| 427 reinterpret_cast<const BYTE *>(&record)); | |
| 428 } | |
| 429 | |
| 430 | |
| 431 Emf::Enumerator::Enumerator(const Emf& emf, HDC context, const RECT* rect) { | 431 Emf::Enumerator::Enumerator(const Emf& emf, HDC context, const RECT* rect) { |
| 432 context_.handle_table = NULL; | 432 context_.handle_table = NULL; |
| 433 context_.objects_count = 0; | 433 context_.objects_count = 0; |
| 434 context_.hdc = NULL; | 434 context_.hdc = NULL; |
| 435 items_.clear(); | 435 items_.clear(); |
| 436 if (!EnumEnhMetaFile(context, | 436 if (!EnumEnhMetaFile(context, |
| 437 emf.emf(), | 437 emf.emf(), |
| 438 &Emf::Enumerator::EnhMetaFileProc, | 438 &Emf::Enumerator::EnhMetaFileProc, |
| 439 reinterpret_cast<void*>(this), | 439 reinterpret_cast<void*>(this), |
| 440 rect)) { | 440 rect)) { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 467 } else { | 467 } else { |
| 468 DCHECK_EQ(emf.context_.handle_table, handle_table); | 468 DCHECK_EQ(emf.context_.handle_table, handle_table); |
| 469 DCHECK_EQ(emf.context_.objects_count, objects_count); | 469 DCHECK_EQ(emf.context_.objects_count, objects_count); |
| 470 DCHECK_EQ(emf.context_.hdc, hdc); | 470 DCHECK_EQ(emf.context_.hdc, hdc); |
| 471 } | 471 } |
| 472 emf.items_.push_back(Record(&emf.context_, record)); | 472 emf.items_.push_back(Record(&emf.context_, record)); |
| 473 return 1; | 473 return 1; |
| 474 } | 474 } |
| 475 | 475 |
| 476 } // namespace printing | 476 } // namespace printing |
| OLD | NEW |