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