1 // Copyright 2012 The Chromium Authors
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 <CoreFoundation/CoreFoundation.h>
9 #include <CoreGraphics/CoreGraphics.h>
12 #include "base/apple/scoped_cftyperef.h"
13 #include "base/component_export.h"
14 #include "printing/metafile.h"
18 // This class creates a graphics context that renders into a PDF data stream.
19 class COMPONENT_EXPORT(PRINTING_METAFILE) PdfMetafileCg : public Metafile {
22 PdfMetafileCg(const PdfMetafileCg&) = delete;
23 PdfMetafileCg& operator=(const PdfMetafileCg&) = delete;
24 ~PdfMetafileCg() override;
28 bool InitFromData(base::span<const uint8_t> data) override;
29 void StartPage(const gfx::Size& page_size,
30 const gfx::Rect& content_area,
32 mojom::PageOrientation page_orientation) override;
33 bool FinishPage() override;
34 bool FinishDocument() override;
36 uint32_t GetDataSize() const override;
37 bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const override;
38 bool ShouldCopySharedMemoryRegionData() const override;
39 mojom::MetafileDataType GetDataType() const override;
41 gfx::Rect GetPageBounds(unsigned int page_number) const override;
42 unsigned int GetPageCount() const override;
44 // Note: The returned context *must not be retained* past Close(). If it is,
45 // the data returned from GetData will not be valid PDF data.
46 CGContextRef context() const override;
48 bool RenderPage(unsigned int page_number,
49 printing::NativeDrawingContext context,
52 bool fit_to_page) const override;
55 // Returns a CGPDFDocumentRef version of `pdf_data_`.
56 CGPDFDocumentRef GetPDFDocument() const;
58 // Context for rendering to the pdf.
59 base::apple::ScopedCFTypeRef<CGContextRef> context_;
62 base::apple::ScopedCFTypeRef<CFMutableDataRef> pdf_data_;
64 // Lazily-created CGPDFDocument representation of `pdf_data_`.
65 mutable base::apple::ScopedCFTypeRef<CGPDFDocumentRef> pdf_doc_;
67 // Whether or not a page is currently open.
68 bool page_is_open_ = false;
71 } // namespace printing
73 #endif // PRINTING_PDF_METAFILE_CG_MAC_H_