[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "printing/image.h" |
| 6 | |
| 7 | #include <ApplicationServices/ApplicationServices.h> |
halcanary | c843c89f | 2017-04-11 23:18:18 | [diff] [blame] | 8 | #include <CoreFoundation/CoreFoundation.h> |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 9 | #include <stddef.h> |
| 10 | #include <stdint.h> |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 11 | |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 12 | #include "base/mac/scoped_cftyperef.h" |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame^] | 13 | #include "printing/metafile.h" |
halcanary | c843c89f | 2017-04-11 23:18:18 | [diff] [blame] | 14 | #include "printing/pdf_metafile_cg_mac.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 15 | #include "ui/gfx/geometry/rect.h" |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 16 | |
| 17 | namespace printing { |
| 18 | |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame^] | 19 | bool Image::LoadMetafile(const Metafile& metafile) { |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 20 | // The printing system uses single-page metafiles (page indexes are 1-based). |
| 21 | const unsigned int page_number = 1; |
| 22 | gfx::Rect rect(metafile.GetPageBounds(page_number)); |
| 23 | if (rect.width() < 1 || rect.height() < 1) |
| 24 | return false; |
| 25 | |
| 26 | size_ = rect.size(); |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 27 | row_length_ = size_.width() * sizeof(uint32_t); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 28 | size_t bytes = row_length_ * size_.height(); |
| 29 | DCHECK(bytes); |
| 30 | |
| 31 | data_.resize(bytes); |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 32 | base::ScopedCFTypeRef<CGColorSpaceRef> color_space( |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 33 | CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 34 | base::ScopedCFTypeRef<CGContextRef> bitmap_context( |
| 35 | CGBitmapContextCreate(&*data_.begin(), |
| 36 | size_.width(), |
| 37 | size_.height(), |
| 38 | 8, |
| 39 | row_length_, |
| 40 | color_space, |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 41 | kCGImageAlphaPremultipliedLast)); |
| 42 | DCHECK(bitmap_context.get()); |
| 43 | |
halcanary | c843c89f | 2017-04-11 23:18:18 | [diff] [blame] | 44 | PdfMetafileCg::RenderPageParams params; |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 45 | params.shrink_to_fit = true; |
halcanary | c843c89f | 2017-04-11 23:18:18 | [diff] [blame] | 46 | CGRect cg_rect = CGRectMake(0, 0, size_.width(), size_.height()); |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame^] | 47 | std::vector<char> buffer; |
| 48 | return metafile.GetDataAsVector(&buffer) && |
| 49 | PdfMetafileCg::RenderPage(buffer, page_number, bitmap_context, cg_rect, |
| 50 | params); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | } // namespace printing |