OLD | NEW |
1 // Copyright (c) 2011 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_PS_METAFILE_CAIRO_H_ | 5 #ifndef PRINTING_PDF_PS_METAFILE_CAIRO_H_ |
6 #define PRINTING_PDF_PS_METAFILE_CAIRO_H_ | 6 #define PRINTING_PDF_PS_METAFILE_CAIRO_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/gtest_prod_util.h" |
| 12 #include "printing/native_metafile_linux.h" |
11 | 13 |
12 typedef struct _cairo_surface cairo_surface_t; | 14 typedef struct _cairo_surface cairo_surface_t; |
13 typedef struct _cairo cairo_t; | 15 typedef struct _cairo cairo_t; |
14 | 16 |
15 namespace base { | 17 namespace base { |
16 struct FileDescriptor; | 18 struct FileDescriptor; |
17 } | 19 } |
18 | 20 |
19 class FilePath; | 21 class FilePath; |
20 | 22 |
21 namespace printing { | 23 namespace printing { |
22 | 24 |
23 // This class uses Cairo graphics library to generate PostScript/PDF stream | 25 // This class uses Cairo graphics library to generate PostScript/PDF stream |
24 // and stores rendering results in a string buffer. | 26 // and stores rendering results in a string buffer. |
25 class PdfPsMetafile { | 27 class PdfPsMetafile : public NativeMetafile { |
26 public: | 28 public: |
27 PdfPsMetafile(); | 29 virtual ~PdfPsMetafile(); |
28 ~PdfPsMetafile(); | |
29 | 30 |
30 // Initializes to a fresh new metafile. Returns true on success. | 31 // Initializes to a fresh new metafile. Returns true on success. |
31 // Note: Only call in the renderer to allocate rendering resources. | 32 // Note: Only call in the renderer to allocate rendering resources. |
32 bool Init(); | 33 virtual bool Init(); |
33 | 34 |
34 // Initializes a copy of metafile from PDF stream data. | 35 // Initializes a copy of metafile from PDF stream data. |
35 // Returns true on success. | 36 // Returns true on success. |
36 // |src_buffer| should point to the shared memory which stores PDF | 37 // |src_buffer| should point to the shared memory which stores PDF |
37 // contents generated in the renderer. | 38 // contents generated in the renderer. |
38 // Note: Only call in the browser to initialize |data_|. | 39 // Note: Only call in the browser to initialize |data_|. |
39 bool Init(const void* src_buffer, uint32 src_buffer_size); | 40 virtual bool Init(const void* src_buffer, uint32 src_buffer_size); |
40 | 41 |
41 // Sets raw PDF data for the document. This is used when a cairo drawing | 42 // Sets raw PDF data for the document. This is used when a cairo drawing |
42 // surface has already been created. This method will cause all subsequent | 43 // surface has already been created. This method will cause all subsequent |
43 // drawing on the surface to be discarded (in Close()). If Init() has not yet | 44 // drawing on the surface to be discarded (in Close()). If Init() has not yet |
44 // been called this method simply calls the second version of Init. | 45 // been called this method simply calls the second version of Init. |
45 bool SetRawData(const void* src_buffer, uint32 src_buffer_size); | 46 virtual bool SetRawData(const void* src_buffer, uint32 src_buffer_size); |
46 | 47 |
47 // Prepares a new cairo surface/context for rendering a new page. | 48 // Prepares a new cairo surface/context for rendering a new page. |
48 // The unit is in point (=1/72 in). | 49 // The unit is in point (=1/72 in). |
49 // Returns NULL when failed. | 50 // Returns NULL when failed. |
50 cairo_t* StartPage(double width_in_points, | 51 virtual cairo_t* StartPage(double width_in_points, |
51 double height_in_points, | 52 double height_in_points, |
52 double margin_top_in_points, | 53 double margin_top_in_points, |
53 double margin_right_in_points, | 54 double margin_right_in_points, |
54 double margin_bottom_in_points, | 55 double margin_bottom_in_points, |
55 double margin_left_in_points); | 56 double margin_left_in_points); |
56 | 57 |
57 // Destroys the surface and the context used in rendering current page. | 58 // Destroys the surface and the context used in rendering current page. |
58 // The results of current page will be appended into buffer |data_|. | 59 // The results of current page will be appended into buffer |data_|. |
59 // Returns true on success. | 60 // Returns true on success. |
60 bool FinishPage(); | 61 virtual bool FinishPage(); |
61 | 62 |
62 // Closes resulting PDF file. No further rendering is allowed. | 63 // Closes resulting PDF file. No further rendering is allowed. |
63 void Close(); | 64 virtual void Close(); |
64 | 65 |
65 // Returns size of PDF contents stored in buffer |data_|. | 66 // Returns size of PDF contents stored in buffer |data_|. |
66 // This function should ONLY be called after PDF file is closed. | 67 // This function should ONLY be called after PDF file is closed. |
67 uint32 GetDataSize() const; | 68 virtual uint32 GetDataSize() const; |
68 | 69 |
69 // Copies PDF contents stored in buffer |data_| into |dst_buffer|. | 70 // Copies PDF contents stored in buffer |data_| into |dst_buffer|. |
70 // This function should ONLY be called after PDF file is closed. | 71 // This function should ONLY be called after PDF file is closed. |
71 // Returns true only when success. | 72 // Returns true only when success. |
72 bool GetData(void* dst_buffer, uint32 dst_buffer_size) const; | 73 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const; |
73 | 74 |
74 // Saves PDF contents stored in buffer |data_| into the file | 75 // Saves PDF contents stored in buffer |data_| into the file |
75 // associated with |fd|. | 76 // associated with |fd|. |
76 // This function should ONLY be called after PDF file is closed. | 77 // This function should ONLY be called after PDF file is closed. |
77 bool SaveTo(const base::FileDescriptor& fd) const; | 78 virtual bool SaveTo(const base::FileDescriptor& fd) const; |
78 | 79 |
79 // The hardcoded margins, in points. These values are based on 72 dpi, | 80 // The hardcoded margins, in points. These values are based on 72 dpi, |
80 // with 0.25 margins on top, left, and right, and 0.56 on bottom. | 81 // with 0.25 margins on top, left, and right, and 0.56 on bottom. |
81 static const double kTopMarginInInch; | 82 static const double kTopMarginInInch; |
82 static const double kRightMarginInInch; | 83 static const double kRightMarginInInch; |
83 static const double kBottomMarginInInch; | 84 static const double kBottomMarginInInch; |
84 static const double kLeftMarginInInch; | 85 static const double kLeftMarginInInch; |
85 | 86 |
86 // Returns the PdfPsMetafile object that owns the given context. Returns NULL | 87 // Returns the PdfPsMetafile object that owns the given context. Returns NULL |
87 // if the context was not created by a PdfPsMetafile object. | 88 // if the context was not created by a PdfPsMetafile object. |
88 static PdfPsMetafile* FromCairoContext(cairo_t* context); | 89 static PdfPsMetafile* FromCairoContext(cairo_t* context); |
89 | 90 |
| 91 protected: |
| 92 PdfPsMetafile(); |
| 93 |
90 private: | 94 private: |
| 95 friend class NativeMetafileFactory; |
| 96 FRIEND_TEST_ALL_PREFIXES(PdfPsTest, Pdf); |
| 97 |
91 // Cleans up all resources. | 98 // Cleans up all resources. |
92 void CleanUpAll(); | 99 void CleanUpAll(); |
93 | 100 |
94 // Cairo surface and context for entire PDF file. | 101 // Cairo surface and context for entire PDF file. |
95 cairo_surface_t* surface_; | 102 cairo_surface_t* surface_; |
96 cairo_t* context_; | 103 cairo_t* context_; |
97 | 104 |
98 // Buffer stores PDF contents for entire PDF file. | 105 // Buffer stores PDF contents for entire PDF file. |
99 std::string data_; | 106 std::string data_; |
100 // Buffer stores raw PDF contents set by SetRawPageData. | 107 // Buffer stores raw PDF contents set by SetRawPageData. |
101 std::string raw_override_data_; | 108 std::string raw_override_data_; |
102 | 109 |
103 DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile); | 110 DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile); |
104 }; | 111 }; |
105 | 112 |
106 } // namespace printing | 113 } // namespace printing |
107 | 114 |
108 #endif // PRINTING_PDF_PS_METAFILE_CAIRO_H_ | 115 #endif // PRINTING_PDF_PS_METAFILE_CAIRO_H_ |
OLD | NEW |