blob: 7edcfd7f0a07d9dbb464a90b2e8c890c6b7fcf03 [file] [log] [blame]
[email protected]b5cf844c2012-06-18 21:49:201// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]48091b02010-10-21 02:38:112// 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>
halcanaryc843c89f2017-04-11 23:18:188#include <CoreFoundation/CoreFoundation.h>
avi126e93c2015-12-21 21:48:169#include <stddef.h>
10#include <stdint.h>
[email protected]48091b02010-10-21 02:38:1111
[email protected]48091b02010-10-21 02:38:1112#include "base/mac/scoped_cftyperef.h"
thestig3109df12017-04-26 21:57:2513#include "printing/metafile.h"
halcanaryc843c89f2017-04-11 23:18:1814#include "printing/pdf_metafile_cg_mac.h"
tfarina3b0452d2014-12-31 15:20:0915#include "ui/gfx/geometry/rect.h"
[email protected]48091b02010-10-21 02:38:1116
17namespace printing {
18
thestig3109df12017-04-26 21:57:2519bool Image::LoadMetafile(const Metafile& metafile) {
[email protected]48091b02010-10-21 02:38:1120 // 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();
thestig707a24b22015-09-14 18:16:3327 row_length_ = size_.width() * sizeof(uint32_t);
[email protected]48091b02010-10-21 02:38:1128 size_t bytes = row_length_ * size_.height();
29 DCHECK(bytes);
30
31 data_.resize(bytes);
[email protected]3df79f42013-06-24 18:49:0532 base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
[email protected]48091b02010-10-21 02:38:1133 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
[email protected]3df79f42013-06-24 18:49:0534 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]48091b02010-10-21 02:38:1141 kCGImageAlphaPremultipliedLast));
42 DCHECK(bitmap_context.get());
43
halcanaryc843c89f2017-04-11 23:18:1844 PdfMetafileCg::RenderPageParams params;
[email protected]b5cf844c2012-06-18 21:49:2045 params.shrink_to_fit = true;
halcanaryc843c89f2017-04-11 23:18:1846 CGRect cg_rect = CGRectMake(0, 0, size_.width(), size_.height());
thestig3109df12017-04-26 21:57:2547 std::vector<char> buffer;
48 return metafile.GetDataAsVector(&buffer) &&
49 PdfMetafileCg::RenderPage(buffer, page_number, bitmap_context, cg_rect,
50 params);
[email protected]48091b02010-10-21 02:38:1151}
52
53} // namespace printing