Avi Drissman | db497b3 | 2022-09-15 19:47:28 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[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> |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 10 | |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 11 | #include "base/mac/scoped_cftyperef.h" |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame] | 12 | #include "printing/metafile.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 13 | #include "ui/gfx/geometry/rect.h" |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 14 | |
| 15 | namespace printing { |
| 16 | |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame] | 17 | bool Image::LoadMetafile(const Metafile& metafile) { |
Daniel Hosseinian | 3553e27 | 2021-04-24 00:51:18 | [diff] [blame] | 18 | // Load only the first page of `metafile`, just like Windows. |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 19 | const unsigned int page_number = 1; |
| 20 | gfx::Rect rect(metafile.GetPageBounds(page_number)); |
| 21 | if (rect.width() < 1 || rect.height() < 1) |
| 22 | return false; |
| 23 | |
| 24 | size_ = rect.size(); |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 25 | row_length_ = size_.width() * sizeof(uint32_t); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 26 | size_t bytes = row_length_ * size_.height(); |
| 27 | DCHECK(bytes); |
| 28 | |
| 29 | data_.resize(bytes); |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 30 | base::ScopedCFTypeRef<CGColorSpaceRef> color_space( |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 31 | CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 32 | base::ScopedCFTypeRef<CGContextRef> bitmap_context(CGBitmapContextCreate( |
| 33 | &*data_.begin(), size_.width(), size_.height(), 8, row_length_, |
| 34 | color_space, kCGImageAlphaPremultipliedLast)); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 35 | DCHECK(bitmap_context.get()); |
| 36 | |
Daniel Hosseinian | 345ecfa | 2020-04-08 03:45:16 | [diff] [blame] | 37 | return metafile.RenderPage(page_number, bitmap_context, rect.ToCGRect(), |
| 38 | /*autorotate=*/false, /*fit_to_page=*/true); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | } // namespace printing |