OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PRINTING_PDF_METAFILE_MAC_H_ | 5 #ifndef PRINTING_PDF_METAFILE_MAC_H_ |
6 #define PRINTING_PDF_METAFILE_MAC_H_ | 6 #define PRINTING_PDF_METAFILE_MAC_H_ |
7 | 7 |
8 #include <ApplicationServices/ApplicationServices.h> | 8 #include <ApplicationServices/ApplicationServices.h> |
9 #include <CoreFoundation/CoreFoundation.h> | 9 #include <CoreFoundation/CoreFoundation.h> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/gtest_prod_util.h" |
12 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
| 14 #include "printing/native_metafile_mac.h" |
13 | 15 |
14 namespace gfx { | 16 namespace gfx { |
15 class Rect; | 17 class Rect; |
16 class Size; | 18 class Size; |
17 class Point; | 19 class Point; |
18 } | 20 } |
19 class FilePath; | 21 class FilePath; |
20 | 22 |
21 namespace printing { | 23 namespace printing { |
22 | 24 |
23 // This class creates a graphics context that renders into a PDF data stream. | 25 // This class creates a graphics context that renders into a PDF data stream. |
24 class PdfMetafile { | 26 class PdfMetafile : public NativeMetafile { |
25 public: | 27 public: |
26 // To create PDF data, callers should also call Init() to set up the | |
27 // rendering context. | |
28 // To create a metafile from existing Data, callers should also call | |
29 // Init(const void*, uint32). | |
30 PdfMetafile(); | |
31 | 28 |
32 ~PdfMetafile(); | 29 virtual ~PdfMetafile(); |
33 | 30 |
34 // Initializes a new metafile, and returns a drawing context for rendering | 31 // Initializes a new metafile, and returns a drawing context for rendering |
35 // into the PDF. Returns NULL on failure. | 32 // into the PDF. Returns NULL on failure. |
36 // Note: The returned context *must not be retained* past Close(). If it is, | 33 // Note: The returned context *must not be retained* past Close(). If it is, |
37 // the data returned from GetData will not be valid PDF data. | 34 // the data returned from GetData will not be valid PDF data. |
38 CGContextRef Init(); | 35 virtual CGContextRef Init(); |
39 | 36 |
40 // Initializes a copy of metafile from PDF data. Returns true on success. | 37 // Initializes a copy of metafile from PDF data. Returns true on success. |
41 bool Init(const void* src_buffer, uint32 src_buffer_size); | 38 virtual bool Init(const void* src_buffer, uint32 src_buffer_size); |
42 | 39 |
43 // Prepares a new pdf page at specified |content_origin| with the given | 40 // Prepares a new pdf page at specified |content_origin| with the given |
44 // |page_size| and a |scale_factor| to use for the drawing. | 41 // |page_size| and a |scale_factor| to use for the drawing. |
45 CGContextRef StartPage(const gfx::Size& page_size, | 42 virtual CGContextRef StartPage(const gfx::Size& page_size, |
46 const gfx::Point& content_origin, | 43 const gfx::Point& content_origin, |
47 const float& scale_factor); | 44 const float& scale_factor); |
48 | 45 |
49 // Closes the current page. | 46 // Closes the current page. |
50 void FinishPage(); | 47 virtual void FinishPage(); |
51 | 48 |
52 // Closes the PDF file; no further rendering is allowed. | 49 // Closes the PDF file; no further rendering is allowed. |
53 void Close(); | 50 virtual void Close(); |
54 | 51 |
55 // Renders the given page into |rect| in the given context. | 52 // Renders the given page into |rect| in the given context. |
56 // Pages use a 1-based index. The rendering uses the following arguments | 53 // Pages use a 1-based index. The rendering uses the following arguments |
57 // to determine scaling and translation factors. | 54 // to determine scaling and translation factors. |
58 // |shrink_to_fit| specifies whether the output should be shrunk to fit the | 55 // |shrink_to_fit| specifies whether the output should be shrunk to fit the |
59 // supplied |rect| if the page size is larger than |rect| in any dimension. | 56 // supplied |rect| if the page size is larger than |rect| in any dimension. |
60 // If this is false, parts of the PDF page that lie outside the bounds will be | 57 // If this is false, parts of the PDF page that lie outside the bounds will be |
61 // clipped. | 58 // clipped. |
62 // |stretch_to_fit| specifies whether the output should be stretched to fit | 59 // |stretch_to_fit| specifies whether the output should be stretched to fit |
63 // the supplied bounds if the page size is smaller than |rect| in all | 60 // the supplied bounds if the page size is smaller than |rect| in all |
64 // dimensions. | 61 // dimensions. |
65 // |center_horizontally| specifies whether the final image (after any scaling | 62 // |center_horizontally| specifies whether the final image (after any scaling |
66 // is done) should be centered horizontally within the given |rect|. | 63 // is done) should be centered horizontally within the given |rect|. |
67 // |center_vertically| specifies whether the final image (after any scaling | 64 // |center_vertically| specifies whether the final image (after any scaling |
68 // is done) should be centered vertically within the given |rect|. | 65 // is done) should be centered vertically within the given |rect|. |
69 // Note that all scaling preserves the original aspect ratio of the page. | 66 // Note that all scaling preserves the original aspect ratio of the page. |
70 bool RenderPage(unsigned int page_number, CGContextRef context, | 67 virtual bool RenderPage(unsigned int page_number, CGContextRef context, |
71 const CGRect rect, bool shrink_to_fit, bool stretch_to_fit, | 68 const CGRect rect, bool shrink_to_fit, bool stretch_to_fit, |
72 bool center_horizontally, bool center_vertically) const; | 69 bool center_horizontally, bool center_vertically) const; |
73 unsigned int GetPageCount() const; | 70 virtual unsigned int GetPageCount() const; |
74 | 71 |
75 // Returns the bounds of the given page. | 72 // Returns the bounds of the given page. |
76 // Pages use a 1-based index. | 73 // Pages use a 1-based index. |
77 gfx::Rect GetPageBounds(unsigned int page_number) const; | 74 virtual gfx::Rect GetPageBounds(unsigned int page_number) const; |
78 | 75 |
79 // Returns the size of the underlying PDF data. Only valid after Close() has | 76 // Returns the size of the underlying PDF data. Only valid after Close() has |
80 // been called. | 77 // been called. |
81 uint32 GetDataSize() const; | 78 virtual uint32 GetDataSize() const; |
82 | 79 |
83 // Copies the first |dst_buffer_size| bytes of the PDF data into |dst_buffer|. | 80 // Copies the first |dst_buffer_size| bytes of the PDF data into |dst_buffer|. |
84 // Only valid after Close() has been called. | 81 // Only valid after Close() has been called. |
85 // Returns true if the copy succeeds. | 82 // Returns true if the copy succeeds. |
86 bool GetData(void* dst_buffer, uint32 dst_buffer_size) const; | 83 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const; |
87 | 84 |
88 // Saves the raw PDF data to the given file. For testing only. | 85 // Saves the raw PDF data to the given file. For testing only. |
89 // Returns true if writing succeeded. | 86 // Returns true if writing succeeded. |
90 bool SaveTo(const FilePath& file_path) const; | 87 virtual bool SaveTo(const FilePath& file_path) const; |
| 88 |
| 89 protected: |
| 90 // To create PDF data, callers should call Init() to set up the rendering |
| 91 // context. |
| 92 // To create a metafile from existing data, callers should call |
| 93 // Init(const void*, uint32). |
| 94 PdfMetafile(); |
91 | 95 |
92 private: | 96 private: |
| 97 friend class NativeMetafileFactory; |
| 98 FRIEND_TEST_ALL_PREFIXES(PdfMetafileTest, Pdf); |
| 99 |
93 // Returns a CGPDFDocumentRef version of pdf_data_. | 100 // Returns a CGPDFDocumentRef version of pdf_data_. |
94 CGPDFDocumentRef GetPDFDocument() const; | 101 CGPDFDocumentRef GetPDFDocument() const; |
95 | 102 |
96 // Context for rendering to the pdf. | 103 // Context for rendering to the pdf. |
97 base::mac::ScopedCFTypeRef<CGContextRef> context_; | 104 base::mac::ScopedCFTypeRef<CGContextRef> context_; |
98 | 105 |
99 // PDF backing store. | 106 // PDF backing store. |
100 base::mac::ScopedCFTypeRef<CFMutableDataRef> pdf_data_; | 107 base::mac::ScopedCFTypeRef<CFMutableDataRef> pdf_data_; |
101 | 108 |
102 // Lazily-created CGPDFDocument representation of pdf_data_. | 109 // Lazily-created CGPDFDocument representation of pdf_data_. |
103 mutable base::mac::ScopedCFTypeRef<CGPDFDocumentRef> pdf_doc_; | 110 mutable base::mac::ScopedCFTypeRef<CGPDFDocumentRef> pdf_doc_; |
104 | 111 |
105 // Whether or not a page is currently open. | 112 // Whether or not a page is currently open. |
106 bool page_is_open_; | 113 bool page_is_open_; |
107 | 114 |
108 DISALLOW_COPY_AND_ASSIGN(PdfMetafile); | 115 DISALLOW_COPY_AND_ASSIGN(PdfMetafile); |
109 }; | 116 }; |
110 | 117 |
111 } // namespace printing | 118 } // namespace printing |
112 | 119 |
113 #endif // PRINTING_PDF_METAFILE_MAC_H_ | 120 #endif // PRINTING_PDF_METAFILE_MAC_H_ |
OLD | NEW |