Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / image_mac.cc
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
3 // found in the LICENSE file.
4
5 #include "printing/image.h"
6
7 #include <ApplicationServices/ApplicationServices.h>
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include "base/mac/scoped_cftyperef.h"
12 #include "printing/metafile.h"
13 #include "ui/gfx/geometry/rect.h"
14
15 namespace printing {
16
17 bool Image::LoadMetafile(const Metafile& metafile) {
18   // Load only the first page of |metafile|, just like Windows.
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();
25   row_length_ = size_.width() * sizeof(uint32_t);
26   size_t bytes = row_length_ * size_.height();
27   DCHECK(bytes);
28
29   data_.resize(bytes);
30   base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
31       CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
32   base::ScopedCFTypeRef<CGContextRef> bitmap_context(
33       CGBitmapContextCreate(&*data_.begin(),
34                             size_.width(),
35                             size_.height(),
36                             8,
37                             row_length_,
38                             color_space,
39                             kCGImageAlphaPremultipliedLast));
40   DCHECK(bitmap_context.get());
41
42   struct Metafile::MacRenderPageParams params;
43   params.shrink_to_fit = true;
44   metafile.RenderPage(page_number, bitmap_context, rect.ToCGRect(), params);
45   return true;
46 }
47
48 }  // namespace printing