pdf_metafile_cg_mac.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef PRINTING_PDF_METAFILE_CG_MAC_H_
  5. #define PRINTING_PDF_METAFILE_CG_MAC_H_
  6. #include <ApplicationServices/ApplicationServices.h>
  7. #include <CoreFoundation/CoreFoundation.h>
  8. #include <stdint.h>
  9. #include "base/component_export.h"
  10. #include "base/mac/scoped_cftyperef.h"
  11. #include "printing/metafile.h"
  12. namespace printing {
  13. // This class creates a graphics context that renders into a PDF data stream.
  14. class COMPONENT_EXPORT(PRINTING_METAFILE) PdfMetafileCg : public Metafile {
  15. public:
  16. PdfMetafileCg();
  17. PdfMetafileCg(const PdfMetafileCg&) = delete;
  18. PdfMetafileCg& operator=(const PdfMetafileCg&) = delete;
  19. ~PdfMetafileCg() override;
  20. // Metafile methods.
  21. bool Init() override;
  22. bool InitFromData(base::span<const uint8_t> data) override;
  23. void StartPage(const gfx::Size& page_size,
  24. const gfx::Rect& content_area,
  25. float scale_factor,
  26. mojom::PageOrientation page_orientation) override;
  27. bool FinishPage() override;
  28. bool FinishDocument() override;
  29. uint32_t GetDataSize() const override;
  30. bool GetData(void* dst_buffer, uint32_t dst_buffer_size) const override;
  31. bool ShouldCopySharedMemoryRegionData() const override;
  32. mojom::MetafileDataType GetDataType() const override;
  33. gfx::Rect GetPageBounds(unsigned int page_number) const override;
  34. unsigned int GetPageCount() const override;
  35. // Note: The returned context *must not be retained* past Close(). If it is,
  36. // the data returned from GetData will not be valid PDF data.
  37. CGContextRef context() const override;
  38. bool RenderPage(unsigned int page_number,
  39. printing::NativeDrawingContext context,
  40. const CGRect& rect,
  41. bool autorotate,
  42. bool fit_to_page) const override;
  43. private:
  44. // Returns a CGPDFDocumentRef version of `pdf_data_`.
  45. CGPDFDocumentRef GetPDFDocument() const;
  46. // Context for rendering to the pdf.
  47. base::ScopedCFTypeRef<CGContextRef> context_;
  48. // PDF backing store.
  49. base::ScopedCFTypeRef<CFMutableDataRef> pdf_data_;
  50. // Lazily-created CGPDFDocument representation of `pdf_data_`.
  51. mutable base::ScopedCFTypeRef<CGPDFDocumentRef> pdf_doc_;
  52. // Whether or not a page is currently open.
  53. bool page_is_open_ = false;
  54. };
  55. } // namespace printing
  56. #endif // PRINTING_PDF_METAFILE_CG_MAC_H_