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