blob: 733d6ba96ed37c9da495e20d2e5bda82fe579d2c [file] [log] [blame]
Avi Drissmandb497b32022-09-15 19:47:281// Copyright 2012 The Chromium Authors
[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>
avi126e93c2015-12-21 21:48:168#include <stddef.h>
9#include <stdint.h>
[email protected]48091b02010-10-21 02:38:1110
[email protected]48091b02010-10-21 02:38:1111#include "base/mac/scoped_cftyperef.h"
thestig3109df12017-04-26 21:57:2512#include "printing/metafile.h"
tfarina3b0452d2014-12-31 15:20:0913#include "ui/gfx/geometry/rect.h"
[email protected]48091b02010-10-21 02:38:1114
15namespace printing {
16
thestig3109df12017-04-26 21:57:2517bool Image::LoadMetafile(const Metafile& metafile) {
Daniel Hosseinian3553e272021-04-24 00:51:1818 // Load only the first page of `metafile`, just like Windows.
[email protected]48091b02010-10-21 02:38:1119 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();
thestig707a24b22015-09-14 18:16:3325 row_length_ = size_.width() * sizeof(uint32_t);
[email protected]48091b02010-10-21 02:38:1126 size_t bytes = row_length_ * size_.height();
27 DCHECK(bytes);
28
29 data_.resize(bytes);
[email protected]3df79f42013-06-24 18:49:0530 base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
[email protected]48091b02010-10-21 02:38:1131 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
Lei Zhang01a1d3c02019-05-21 04:59:1032 base::ScopedCFTypeRef<CGContextRef> bitmap_context(CGBitmapContextCreate(
33 &*data_.begin(), size_.width(), size_.height(), 8, row_length_,
34 color_space, kCGImageAlphaPremultipliedLast));
[email protected]48091b02010-10-21 02:38:1135 DCHECK(bitmap_context.get());
36
Daniel Hosseinian345ecfa2020-04-08 03:45:1637 return metafile.RenderPage(page_number, bitmap_context, rect.ToCGRect(),
38 /*autorotate=*/false, /*fit_to_page=*/true);
[email protected]48091b02010-10-21 02:38:1139}
40
41} // namespace printing