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.
5 #ifndef PRINTING_PDF_METAFILE_CG_MAC_H_
6 #define PRINTING_PDF_METAFILE_CG_MAC_H_
8 #include <ApplicationServices/ApplicationServices.h>
9 #include <CoreFoundation/CoreFoundation.h>
12 #include "base/mac/scoped_cftyperef.h"
13 #include "printing/metafile.h"
17 // This class creates a graphics context that renders into a PDF data stream.
18 class PRINTING_EXPORT PdfMetafileCg : public Metafile {
21 PdfMetafileCg(const PdfMetafileCg&) = delete;
22 PdfMetafileCg& operator=(const PdfMetafileCg&) = delete;
23 ~PdfMetafileCg() override;
27 bool InitFromData(base::span<const uint8_t> data) override;
28 void StartPage(const gfx::Size& page_size,
29 const gfx::Rect& content_area,
31 mojom::PageOrientation page_orientation) override;
32 bool FinishPage() override;
33 bool FinishDocument() override;
35 uint32_t GetDataSize() const override;
36 bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const override;
38 gfx::Rect GetPageBounds(unsigned int page_number) const override;
39 unsigned int GetPageCount() const override;
41 // Note: The returned context *must not be retained* past Close(). If it is,
42 // the data returned from GetData will not be valid PDF data.
43 CGContextRef context() const override;
45 bool RenderPage(unsigned int page_number,
46 printing::NativeDrawingContext context,
49 bool fit_to_page) const override;
52 // Returns a CGPDFDocumentRef version of |pdf_data_|.
53 CGPDFDocumentRef GetPDFDocument() const;
55 // Context for rendering to the pdf.
56 base::ScopedCFTypeRef<CGContextRef> context_;
59 base::ScopedCFTypeRef<CFMutableDataRef> pdf_data_;
61 // Lazily-created CGPDFDocument representation of |pdf_data_|.
62 mutable base::ScopedCFTypeRef<CGPDFDocumentRef> pdf_doc_;
64 // Whether or not a page is currently open.
65 bool page_is_open_ = false;
68 } // namespace printing
70 #endif // PRINTING_PDF_METAFILE_CG_MAC_H_