pdf_metafile_cg_mac.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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#ifndef PRINTING_PDF_METAFILE_CG_MAC_H_
6#define PRINTING_PDF_METAFILE_CG_MAC_H_
7
8#include <ApplicationServices/ApplicationServices.h>
9#include <CoreFoundation/CoreFoundation.h>
10
11#include "base/basictypes.h"
12#include "base/gtest_prod_util.h"
13#include "base/mac/scoped_cftyperef.h"
14#include "base/threading/thread_checker.h"
15#include "printing/metafile.h"
16
17class FilePath;
18
19namespace gfx {
20class Rect;
21class Size;
22}
23
24namespace printing {
25
26// This class creates a graphics context that renders into a PDF data stream.
27class PRINTING_EXPORT PdfMetafileCg : public Metafile {
28 public:
29  PdfMetafileCg();
30  virtual ~PdfMetafileCg();
31
32  // Metafile methods.
33  virtual bool Init() OVERRIDE;
34  virtual bool InitFromData(const void* src_buffer,
35                            uint32 src_buffer_size) OVERRIDE;
36
37  // Not implemented on mac.
38  virtual SkDevice* StartPageForVectorCanvas(
39      const gfx::Size& page_size, const gfx::Rect& content_area,
40      const float& scale_factor) OVERRIDE;
41  virtual bool StartPage(const gfx::Size& page_size,
42                         const gfx::Rect& content_area,
43                         const float& scale_factor) OVERRIDE;
44  virtual bool FinishPage() OVERRIDE;
45  virtual bool FinishDocument() OVERRIDE;
46
47  virtual uint32 GetDataSize() const OVERRIDE;
48  virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const OVERRIDE;
49
50  // For testing purposes only.
51  virtual bool SaveTo(const FilePath& file_path) const OVERRIDE;
52
53  virtual gfx::Rect GetPageBounds(unsigned int page_number) const OVERRIDE;
54  virtual unsigned int GetPageCount() const OVERRIDE;
55
56  // Note: The returned context *must not be retained* past Close(). If it is,
57  // the data returned from GetData will not be valid PDF data.
58  virtual CGContextRef context() const OVERRIDE;
59
60  virtual bool RenderPage(unsigned int page_number,
61                          gfx::NativeDrawingContext context,
62                          const CGRect rect,
63                          const MacRenderPageParams& params) const OVERRIDE;
64
65 private:
66  // Returns a CGPDFDocumentRef version of pdf_data_.
67  CGPDFDocumentRef GetPDFDocument() const;
68
69  base::ThreadChecker thread_checker_;
70
71  // Context for rendering to the pdf.
72  base::mac::ScopedCFTypeRef<CGContextRef> context_;
73
74  // PDF backing store.
75  base::mac::ScopedCFTypeRef<CFMutableDataRef> pdf_data_;
76
77  // Lazily-created CGPDFDocument representation of pdf_data_.
78  mutable base::mac::ScopedCFTypeRef<CGPDFDocumentRef> pdf_doc_;
79
80  // Whether or not a page is currently open.
81  bool page_is_open_;
82
83  // Whether this instantiation of the PdfMetafileCg owns the thread_pdf_docs.
84  bool thread_pdf_docs_owned_;
85
86  DISALLOW_COPY_AND_ASSIGN(PdfMetafileCg);
87};
88
89}  // namespace printing
90
91#endif  // PRINTING_PDF_METAFILE_CG_MAC_H_
92