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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool Emf::CreateFromFile(const FilePath& metafile_path) { | 80 bool Emf::CreateFromFile(const FilePath& metafile_path) { |
| 81 DCHECK(!emf_ && !hdc_); | 81 DCHECK(!emf_ && !hdc_); |
| 82 emf_ = GetEnhMetaFile(metafile_path.value().c_str()); | 82 emf_ = GetEnhMetaFile(metafile_path.value().c_str()); |
| 83 DCHECK(emf_); | 83 DCHECK(emf_); |
| 84 return emf_ != NULL; | 84 return emf_ != NULL; |
| 85 } | 85 } |
| 86 | 86 |
| 87 | 87 |
| 88 bool Emf::CloseDc() { | 88 bool Emf::Close() { |
| 89 DCHECK(!emf_ && hdc_); | 89 DCHECK(!emf_ && hdc_); |
| 90 emf_ = CloseEnhMetaFile(hdc_); | 90 emf_ = CloseEnhMetaFile(hdc_); |
| 91 DCHECK(emf_); | 91 DCHECK(emf_); |
| 92 hdc_ = NULL; | 92 hdc_ = NULL; |
| 93 return emf_ != NULL; | 93 return emf_ != NULL; |
| 94 } | 94 } |
| 95 | 95 |
| 96 void Emf::CloseEmf() { | 96 void Emf::CloseEmf() { |
| 97 DCHECK(!hdc_); | 97 DCHECK(!hdc_); |
| 98 if (emf_) { | 98 if (emf_) { |
| 99 DeleteEnhMetaFile(emf_); | 99 DeleteEnhMetaFile(emf_); |
| 100 emf_ = NULL; | 100 emf_ = NULL; |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 bool Emf::Playback(HDC hdc, const RECT* rect) const { | 104 bool Emf::Playback(HDC hdc, const RECT* rect) const { |
| 105 DCHECK(emf_ && !hdc_); | 105 DCHECK(emf_ && !hdc_); |
| 106 RECT bounds; | 106 RECT bounds; |
| 107 if (!rect) { | 107 if (!rect) { |
| 108 // Get the natural bounds of the EMF buffer. | 108 // Get the natural bounds of the EMF buffer. |
| 109 bounds = GetBounds().ToRECT(); | 109 bounds = GetPageBounds(1).ToRECT(); |
| 110 rect = &bounds; | 110 rect = &bounds; |
| 111 } | 111 } |
| 112 return PlayEnhMetaFile(hdc, emf_, rect) != 0; | 112 return PlayEnhMetaFile(hdc, emf_, rect) != 0; |
| 113 } | 113 } |
| 114 | 114 |
| 115 bool Emf::SafePlayback(HDC context) const { | 115 bool Emf::SafePlayback(HDC context) const { |
| 116 DCHECK(emf_ && !hdc_); | 116 DCHECK(emf_ && !hdc_); |
| 117 XFORM base_matrix; | 117 XFORM base_matrix; |
| 118 if (!GetWorldTransform(context, &base_matrix)) { | 118 if (!GetWorldTransform(context, &base_matrix)) { |
| 119 NOTREACHED(); | 119 NOTREACHED(); |
| 120 return false; | 120 return false; |
| 121 } | 121 } |
| 122 return EnumEnhMetaFile(context, | 122 return EnumEnhMetaFile(context, |
| 123 emf_, | 123 emf_, |
| 124 &Emf::SafePlaybackProc, | 124 &Emf::SafePlaybackProc, |
| 125 reinterpret_cast<void*>(&base_matrix), | 125 reinterpret_cast<void*>(&base_matrix), |
| 126 &GetBounds().ToRECT()) != 0; | 126 &GetPageBounds(1).ToRECT()) != 0; |
| 127 } | 127 } |
| 128 | 128 |
| 129 gfx::Rect Emf::GetBounds() const { | 129 gfx::Rect Emf::GetPageBounds(unsigned int page_number) const { |
| 130 DCHECK(emf_ && !hdc_); | 130 DCHECK(emf_ && !hdc_); |
| 131 DCHECK_EQ(page_number, static_cast<unsigned int>(1)); | |
|
vandebo (ex-Chrome)
2011/03/14 22:55:05
I think you can use "1U".
dpapad
2011/03/15 16:11:06
Done.
dpapad
2011/03/15 16:11:06
Done.
| |
| 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()), |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 217 DCHECK(record_); | 218 DCHECK(record_); |
| 218 } | 219 } |
| 219 | 220 |
| 220 bool Emf::Record::Play() const { | 221 bool Emf::Record::Play() const { |
| 221 return 0 != PlayEnhMetaFileRecord(context_->hdc, | 222 return 0 != PlayEnhMetaFileRecord(context_->hdc, |
| 222 context_->handle_table, | 223 context_->handle_table, |
| 223 record_, | 224 record_, |
| 224 context_->objects_count); | 225 context_->objects_count); |
| 225 } | 226 } |
| 226 | 227 |
| 228 bool Emf::StartPage() { | |
| 229 DCHECK(hdc_); | |
| 230 if (!hdc_) | |
| 231 return false; | |
| 232 PageBreakRecord record(PageBreakRecord::START_PAGE); | |
| 233 return !!GdiComment(hdc_, sizeof(record), | |
| 234 reinterpret_cast<const BYTE *>(&record)); | |
| 235 } | |
| 236 | |
| 237 bool Emf::FinishPage() { | |
| 238 DCHECK(hdc_); | |
| 239 if (!hdc_) | |
| 240 return false; | |
| 241 PageBreakRecord record(PageBreakRecord::END_PAGE); | |
| 242 return !!GdiComment(hdc_, sizeof(record), | |
| 243 reinterpret_cast<const BYTE *>(&record)); | |
| 244 } | |
| 245 | |
| 227 bool Emf::Record::SafePlayback(const XFORM* base_matrix) const { | 246 bool Emf::Record::SafePlayback(const XFORM* base_matrix) const { |
| 228 // For EMF field description, see [MS-EMF] Enhanced Metafile Format | 247 // For EMF field description, see [MS-EMF] Enhanced Metafile Format |
| 229 // Specification. | 248 // Specification. |
| 230 // | 249 // |
| 231 // This is the second major EMF breakage I get; the first one being | 250 // This is the second major EMF breakage I get; the first one being |
| 232 // SetDCBrushColor/SetDCPenColor/DC_PEN/DC_BRUSH being silently ignored. | 251 // SetDCBrushColor/SetDCPenColor/DC_PEN/DC_BRUSH being silently ignored. |
| 233 // | 252 // |
| 234 // This function is the guts of the fix for bug 1186598. Some printer drivers | 253 // This function is the guts of the fix for bug 1186598. Some printer drivers |
| 235 // somehow choke on certain EMF records, but calling the corresponding | 254 // somehow choke on certain EMF records, but calling the corresponding |
| 236 // function directly on the printer HDC is fine. Still, playing the EMF record | 255 // function directly on the printer HDC is fine. Still, playing the EMF record |
| (...skipping 165 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 |