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/image.h" | 5 #include "printing/image.h" |
6 | 6 |
7 #include <ApplicationServices/ApplicationServices.h> | 7 #include <ApplicationServices/ApplicationServices.h> |
8 #include <CoreFoundation/CoreFoundation.h> | 8 #include <CoreFoundation/CoreFoundation.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
11 | 11 |
12 #include "base/mac/scoped_cftyperef.h" | 12 #include "base/mac/scoped_cftyperef.h" |
13 #include "printing/metafile.h" | |
14 #include "printing/pdf_metafile_cg_mac.h" | 13 #include "printing/pdf_metafile_cg_mac.h" |
15 #include "ui/gfx/geometry/rect.h" | 14 #include "ui/gfx/geometry/rect.h" |
16 | 15 |
17 namespace printing { | 16 namespace printing { |
18 | 17 |
19 bool Image::LoadMetafile(const Metafile& metafile) { | 18 bool Image::LoadMetafile(const void* metafile_src_buffer, |
| 19 size_t metafile_src_buffer_size) { |
| 20 PdfMetafileCg metafile; |
| 21 if (!metafile.InitFromData(metafile_src_buffer, metafile_src_buffer_size)) { |
| 22 return false; |
| 23 } |
20 // The printing system uses single-page metafiles (page indexes are 1-based). | 24 // The printing system uses single-page metafiles (page indexes are 1-based). |
21 const unsigned int page_number = 1; | 25 const unsigned int page_number = 1; |
22 gfx::Rect rect(metafile.GetPageBounds(page_number)); | 26 gfx::Rect rect(metafile.GetPageBounds(page_number)); |
23 if (rect.width() < 1 || rect.height() < 1) | 27 if (rect.width() < 1 || rect.height() < 1) |
24 return false; | 28 return false; |
25 | 29 |
26 size_ = rect.size(); | 30 size_ = rect.size(); |
27 row_length_ = size_.width() * sizeof(uint32_t); | 31 row_length_ = size_.width() * sizeof(uint32_t); |
28 size_t bytes = row_length_ * size_.height(); | 32 size_t bytes = row_length_ * size_.height(); |
29 DCHECK(bytes); | 33 DCHECK(bytes); |
30 | 34 |
31 data_.resize(bytes); | 35 data_.resize(bytes); |
32 base::ScopedCFTypeRef<CGColorSpaceRef> color_space( | 36 base::ScopedCFTypeRef<CGColorSpaceRef> color_space( |
33 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); | 37 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
34 base::ScopedCFTypeRef<CGContextRef> bitmap_context( | 38 base::ScopedCFTypeRef<CGContextRef> bitmap_context( |
35 CGBitmapContextCreate(&*data_.begin(), | 39 CGBitmapContextCreate(&*data_.begin(), |
36 size_.width(), | 40 size_.width(), |
37 size_.height(), | 41 size_.height(), |
38 8, | 42 8, |
39 row_length_, | 43 row_length_, |
40 color_space, | 44 color_space, |
41 kCGImageAlphaPremultipliedLast)); | 45 kCGImageAlphaPremultipliedLast)); |
42 DCHECK(bitmap_context.get()); | 46 DCHECK(bitmap_context.get()); |
43 | 47 |
44 PdfMetafileCg::RenderPageParams params; | 48 PdfMetafileCg::RenderPageParams params; |
45 params.shrink_to_fit = true; | 49 params.shrink_to_fit = true; |
46 CGRect cg_rect = CGRectMake(0, 0, size_.width(), size_.height()); | 50 CGRect cg_rect = CGRectMake(0, 0, size_.width(), size_.height()); |
47 std::vector<char> buffer; | 51 return metafile.OnRenderPage(page_number, bitmap_context, cg_rect, params); |
48 return metafile.GetDataAsVector(&buffer) && | |
49 PdfMetafileCg::RenderPage(buffer, page_number, bitmap_context, cg_rect, | |
50 params); | |
51 } | 52 } |
52 | 53 |
53 } // namespace printing | 54 } // namespace printing |
OLD | NEW |