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(1U, page_number); |
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 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 | 412 |
412 bool Emf::StartPage() { | 413 bool Emf::StartPage() { |
413 DCHECK(hdc_); | 414 DCHECK(hdc_); |
414 if (!hdc_) | 415 if (!hdc_) |
415 return false; | 416 return false; |
416 PageBreakRecord record(PageBreakRecord::START_PAGE); | 417 PageBreakRecord record(PageBreakRecord::START_PAGE); |
417 return !!GdiComment(hdc_, sizeof(record), | 418 return !!GdiComment(hdc_, sizeof(record), |
418 reinterpret_cast<const BYTE *>(&record)); | 419 reinterpret_cast<const BYTE *>(&record)); |
419 } | 420 } |
420 | 421 |
421 bool Emf::EndPage() { | 422 bool Emf::FinishPage() { |
422 DCHECK(hdc_); | 423 DCHECK(hdc_); |
423 if (!hdc_) | 424 if (!hdc_) |
424 return false; | 425 return false; |
425 PageBreakRecord record(PageBreakRecord::END_PAGE); | 426 PageBreakRecord record(PageBreakRecord::END_PAGE); |
426 return !!GdiComment(hdc_, sizeof(record), | 427 return !!GdiComment(hdc_, sizeof(record), |
427 reinterpret_cast<const BYTE *>(&record)); | 428 reinterpret_cast<const BYTE *>(&record)); |
428 } | 429 } |
429 | 430 |
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 |