image_mac.cc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. #include "printing/image.h"
  5. #include <ApplicationServices/ApplicationServices.h>
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "base/mac/scoped_cftyperef.h"
  9. #include "printing/metafile.h"
  10. #include "ui/gfx/geometry/rect.h"
  11. namespace printing {
  12. bool Image::LoadMetafile(const Metafile& metafile) {
  13. // Load only the first page of `metafile`, just like Windows.
  14. const unsigned int page_number = 1;
  15. gfx::Rect rect(metafile.GetPageBounds(page_number));
  16. if (rect.width() < 1 || rect.height() < 1)
  17. return false;
  18. size_ = rect.size();
  19. row_length_ = size_.width() * sizeof(uint32_t);
  20. size_t bytes = row_length_ * size_.height();
  21. DCHECK(bytes);
  22. data_.resize(bytes);
  23. base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
  24. CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB));
  25. base::ScopedCFTypeRef<CGContextRef> bitmap_context(CGBitmapContextCreate(
  26. &*data_.begin(), size_.width(), size_.height(), 8, row_length_,
  27. color_space, kCGImageAlphaPremultipliedLast));
  28. DCHECK(bitmap_context.get());
  29. return metafile.RenderPage(page_number, bitmap_context, rect.ToCGRect(),
  30. /*autorotate=*/false, /*fit_to_page=*/true);
  31. }
  32. } // namespace printing