OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/file.h" |
7 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/win/scoped_gdi_object.h" | 11 #include "base/win/scoped_gdi_object.h" |
11 #include "base/win/scoped_hdc.h" | 12 #include "base/win/scoped_hdc.h" |
12 #include "base/win/scoped_select_object.h" | 13 #include "base/win/scoped_select_object.h" |
13 #include "skia/ext/vector_platform_device_emf_win.h" | 14 #include "skia/ext/vector_platform_device_emf_win.h" |
14 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
15 #include "ui/gfx/codec/jpeg_codec.h" | 16 #include "ui/gfx/codec/jpeg_codec.h" |
16 #include "ui/gfx/codec/png_codec.h" | 17 #include "ui/gfx/codec/png_codec.h" |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 | 267 |
267 bool Emf::GetData(void* buffer, uint32 size) const { | 268 bool Emf::GetData(void* buffer, uint32 size) const { |
268 DCHECK(emf_ && !hdc_); | 269 DCHECK(emf_ && !hdc_); |
269 DCHECK(buffer && size); | 270 DCHECK(buffer && size); |
270 uint32 size2 = | 271 uint32 size2 = |
271 GetEnhMetaFileBits(emf_, size, reinterpret_cast<BYTE*>(buffer)); | 272 GetEnhMetaFileBits(emf_, size, reinterpret_cast<BYTE*>(buffer)); |
272 DCHECK(size2 == size); | 273 DCHECK(size2 == size); |
273 return size2 == size && size2 != 0; | 274 return size2 == size && size2 != 0; |
274 } | 275 } |
275 | 276 |
276 bool Emf::GetDataAsVector(std::vector<uint8>* buffer) const { | |
277 uint32 size = GetDataSize(); | |
278 if (!size) | |
279 return false; | |
280 | |
281 buffer->resize(size); | |
282 if (!GetData(&buffer->front(), size)) | |
283 return false; | |
284 return true; | |
285 } | |
286 | |
287 bool Emf::SaveTo(const base::FilePath& file_path) const { | |
288 HANDLE file = CreateFile(file_path.value().c_str(), GENERIC_WRITE, | |
289 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | |
290 CREATE_ALWAYS, 0, NULL); | |
291 if (file == INVALID_HANDLE_VALUE) | |
292 return false; | |
293 | |
294 bool success = false; | |
295 std::vector<uint8> buffer; | |
296 if (GetDataAsVector(&buffer)) { | |
297 DWORD written = 0; | |
298 if (WriteFile(file, &*buffer.begin(), static_cast<DWORD>(buffer.size()), | |
299 &written, NULL) && | |
300 written == buffer.size()) { | |
301 success = true; | |
302 } | |
303 } | |
304 CloseHandle(file); | |
305 return success; | |
306 } | |
307 | |
308 int CALLBACK Emf::SafePlaybackProc(HDC hdc, | 277 int CALLBACK Emf::SafePlaybackProc(HDC hdc, |
309 HANDLETABLE* handle_table, | 278 HANDLETABLE* handle_table, |
310 const ENHMETARECORD* record, | 279 const ENHMETARECORD* record, |
311 int objects_count, | 280 int objects_count, |
312 LPARAM param) { | 281 LPARAM param) { |
313 Emf::EnumerationContext* context = | 282 Emf::EnumerationContext* context = |
314 reinterpret_cast<Emf::EnumerationContext*>(param); | 283 reinterpret_cast<Emf::EnumerationContext*>(param); |
315 context->handle_table = handle_table; | 284 context->handle_table = handle_table; |
316 context->objects_count = objects_count; | 285 context->objects_count = objects_count; |
317 context->hdc = hdc; | 286 context->hdc = hdc; |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 bool Emf::IsAlphaBlendUsed() const { | 568 bool Emf::IsAlphaBlendUsed() const { |
600 bool result = false; | 569 bool result = false; |
601 ::EnumEnhMetaFile(NULL, | 570 ::EnumEnhMetaFile(NULL, |
602 emf(), | 571 emf(), |
603 &IsAlphaBlendUsedEnumProc, | 572 &IsAlphaBlendUsedEnumProc, |
604 &result, | 573 &result, |
605 NULL); | 574 NULL); |
606 return result; | 575 return result; |
607 } | 576 } |
608 | 577 |
609 Emf* Emf::RasterizeMetafile(int raster_area_in_pixels) const { | 578 scoped_ptr<Emf> Emf::RasterizeMetafile(int raster_area_in_pixels) const { |
610 gfx::Rect page_bounds = GetPageBounds(1); | 579 gfx::Rect page_bounds = GetPageBounds(1); |
611 gfx::Size page_size(page_bounds.size()); | 580 gfx::Size page_size(page_bounds.size()); |
612 if (page_size.GetArea() <= 0) { | 581 if (page_size.GetArea() <= 0) { |
613 NOTREACHED() << "Metafile is empty"; | 582 NOTREACHED() << "Metafile is empty"; |
614 page_bounds = gfx::Rect(1, 1); | 583 page_bounds = gfx::Rect(1, 1); |
615 } | 584 } |
616 | 585 |
617 float scale = sqrt(float(raster_area_in_pixels) / page_size.GetArea()); | 586 float scale = sqrt(float(raster_area_in_pixels) / page_size.GetArea()); |
618 page_size.set_width(std::max<int>(1, page_size.width() * scale)); | 587 page_size.set_width(std::max<int>(1, page_size.width() * scale)); |
619 page_size.set_height(std::max<int>(1, page_size.height() * scale)); | 588 page_size.set_height(std::max<int>(1, page_size.height() * scale)); |
(...skipping 21 matching lines...) Expand all Loading... |
641 page_bounds.x(), | 610 page_bounds.x(), |
642 page_bounds.y(), | 611 page_bounds.y(), |
643 }; | 612 }; |
644 ::SetWorldTransform(hdc, &xform); | 613 ::SetWorldTransform(hdc, &xform); |
645 ::BitBlt(hdc, 0, 0, bitmap_rect.width(), bitmap_rect.height(), | 614 ::BitBlt(hdc, 0, 0, bitmap_rect.width(), bitmap_rect.height(), |
646 bitmap.context(), bitmap_rect.x(), bitmap_rect.y(), SRCCOPY); | 615 bitmap.context(), bitmap_rect.x(), bitmap_rect.y(), SRCCOPY); |
647 | 616 |
648 result->FinishPage(); | 617 result->FinishPage(); |
649 result->FinishDocument(); | 618 result->FinishDocument(); |
650 | 619 |
651 return result.release(); | 620 return result.Pass(); |
652 } | 621 } |
653 | 622 |
654 Emf* Emf::RasterizeAlphaBlend() const { | 623 scoped_ptr<Emf> Emf::RasterizeAlphaBlend() const { |
655 gfx::Rect page_bounds = GetPageBounds(1); | 624 gfx::Rect page_bounds = GetPageBounds(1); |
656 if (page_bounds.size().GetArea() <= 0) { | 625 if (page_bounds.size().GetArea() <= 0) { |
657 NOTREACHED() << "Metafile is empty"; | 626 NOTREACHED() << "Metafile is empty"; |
658 page_bounds = gfx::Rect(1, 1); | 627 page_bounds = gfx::Rect(1, 1); |
659 } | 628 } |
660 | 629 |
661 RasterBitmap bitmap(page_bounds.size()); | 630 RasterBitmap bitmap(page_bounds.size()); |
662 | 631 |
663 // Map metafile page_bounds.x(), page_bounds.y() to bitmap 0, 0. | 632 // Map metafile page_bounds.x(), page_bounds.y() to bitmap 0, 0. |
664 XFORM xform = { 1, 0, 0, 1, -page_bounds.x(), -page_bounds.y()}; | 633 XFORM xform = { 1, 0, 0, 1, -page_bounds.x(), -page_bounds.y()}; |
665 ::SetWorldTransform(bitmap.context(), &xform); | 634 ::SetWorldTransform(bitmap.context(), &xform); |
666 | 635 |
667 scoped_ptr<Emf> result(new Emf); | 636 scoped_ptr<Emf> result(new Emf); |
668 result->Init(); | 637 result->Init(); |
669 HDC hdc = result->context(); | 638 HDC hdc = result->context(); |
670 DCHECK(hdc); | 639 DCHECK(hdc); |
671 skia::InitializeDC(hdc); | 640 skia::InitializeDC(hdc); |
672 | 641 |
673 HDC bitmap_dc = bitmap.context(); | 642 HDC bitmap_dc = bitmap.context(); |
674 RECT rect = page_bounds.ToRECT(); | 643 RECT rect = page_bounds.ToRECT(); |
675 ::EnumEnhMetaFile(hdc, emf(), &RasterizeAlphaBlendProc, &bitmap_dc, &rect); | 644 ::EnumEnhMetaFile(hdc, emf(), &RasterizeAlphaBlendProc, &bitmap_dc, &rect); |
676 | 645 |
677 result->FinishDocument(); | 646 result->FinishDocument(); |
678 | 647 |
679 return result.release(); | 648 return result.Pass(); |
680 } | 649 } |
681 | 650 |
682 | 651 |
683 } // namespace printing | 652 } // namespace printing |
OLD | NEW |