OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 | 11 |
(...skipping 13 matching lines...) Expand all Loading... |
25 class PdfPsMetafile { | 25 class PdfPsMetafile { |
26 public: | 26 public: |
27 enum FileFormat { | 27 enum FileFormat { |
28 PDF, | 28 PDF, |
29 PS, | 29 PS, |
30 }; | 30 }; |
31 | 31 |
32 // In the renderer process, callers should also call Init(void) to see if the | 32 // In the renderer process, callers should also call Init(void) to see if the |
33 // metafile can obtain all necessary rendering resources. | 33 // metafile can obtain all necessary rendering resources. |
34 // In the browser process, callers should also call Init(const void*, uint32) | 34 // In the browser process, callers should also call Init(const void*, uint32) |
35 // to initialize the buffer |all_pages_| to use SaveTo(). | 35 // to initialize the buffer |data_| to use SaveTo(). |
36 explicit PdfPsMetafile(const FileFormat& format); | 36 explicit PdfPsMetafile(const FileFormat& format); |
37 | 37 |
38 ~PdfPsMetafile(); | 38 ~PdfPsMetafile(); |
39 | 39 |
40 // Initializes to a fresh new metafile. Returns true on success. | 40 // Initializes to a fresh new metafile. Returns true on success. |
41 // Note: Only call in the renderer to allocate rendering resources. | 41 // Note: Only call in the renderer to allocate rendering resources. |
42 bool Init(); | 42 bool Init(); |
43 | 43 |
44 // Initializes a copy of metafile from PDF/PS stream data. | 44 // Initializes a copy of metafile from PDF/PS stream data. |
45 // Returns true on success. | 45 // Returns true on success. |
46 // |src_buffer| should point to the shared memory which stores PDF/PS | 46 // |src_buffer| should point to the shared memory which stores PDF/PS |
47 // contents generated in the renderer. | 47 // contents generated in the renderer. |
48 // Note: Only call in the browser to initialize |all_pages_|. | 48 // Note: Only call in the browser to initialize |data_|. |
49 bool Init(const void* src_buffer, uint32 src_buffer_size); | 49 bool Init(const void* src_buffer, uint32 src_buffer_size); |
50 | 50 |
51 FileFormat GetFileFormat() const { return format_; } | 51 FileFormat GetFileFormat() const { return format_; } |
52 | 52 |
53 // Prepares a new cairo surface/context for rendering a new page. | 53 // Prepares a new cairo surface/context for rendering a new page. |
54 // The unit is in point (=1/72 in). | 54 // The unit is in point (=1/72 in). |
55 // Returns NULL when failed. | 55 // Returns NULL when failed. |
56 cairo_t* StartPage(double width, double height); | 56 cairo_t* StartPage(double width, double height); |
57 | 57 |
58 // Destroys the surface and the context used in rendering current page. | 58 // Destroys the surface and the context used in rendering current page. |
59 // The results of current page will be appended into buffer |all_pages_|. | 59 // The results of current page will be appended into buffer |data_|. |
60 // Returns true on success | 60 // Returns true on success. |
61 // TODO(myhuang): I plan to also do page setup here (margins, the header | 61 bool FinishPage(); |
62 // and the footer). At this moment, only pre-defined margins for US letter | |
63 // paper are hard-coded here. | |
64 // |shrink| decides the scaling factor to fit raw printing results into | |
65 // printable area. | |
66 bool FinishPage(float shrink); | |
67 | 62 |
68 // Closes resulting PDF/PS file. No further rendering is allowed. | 63 // Closes resulting PDF/PS file. No further rendering is allowed. |
69 void Close(); | 64 void Close(); |
70 | 65 |
71 // Returns size of PDF/PS contents stored in buffer |all_pages_|. | 66 // Returns size of PDF/PS contents stored in buffer |data_|. |
72 // This function should ONLY be called after PDF/PS file is closed. | 67 // This function should ONLY be called after PDF/PS file is closed. |
73 uint32 GetDataSize() const; | 68 uint32 GetDataSize() const; |
74 | 69 |
75 // Copies PDF/PS contents stored in buffer |all_pages_| into |dst_buffer|. | 70 // Copies PDF/PS contents stored in buffer |data_| into |dst_buffer|. |
76 // This function should ONLY be called after PDF/PS file is closed. | 71 // This function should ONLY be called after PDF/PS file is closed. |
77 // Returns true only when success. | 72 // Returns true only when success. |
78 bool GetData(void* dst_buffer, uint32 dst_buffer_size) const; | 73 bool GetData(void* dst_buffer, uint32 dst_buffer_size) const; |
79 | 74 |
80 // Saves PDF/PS contents stored in buffer |all_pages_| into the file | 75 // Saves PDF/PS contents stored in buffer |data_| into the file |
81 // associated with |fd|. | 76 // associated with |fd|. |
82 // This function should ONLY be called after PDF/PS file is closed. | 77 // This function should ONLY be called after PDF/PS file is closed. |
83 bool SaveTo(const base::FileDescriptor& fd) const; | 78 bool SaveTo(const base::FileDescriptor& fd) const; |
84 | 79 |
85 private: | 80 private: |
86 // Cleans up all resources. | 81 // Cleans up all resources. |
87 void CleanUpAll(); | 82 void CleanUpAll(); |
88 | 83 |
89 FileFormat format_; | 84 FileFormat format_; |
90 | 85 |
91 // Cairo surface and context for entire PDF/PS file. | 86 // Cairo surface and context for entire PDF/PS file. |
92 cairo_surface_t* surface_; | 87 cairo_surface_t* surface_; |
93 cairo_t* context_; | 88 cairo_t* context_; |
94 | 89 |
95 // Cairo surface and context for current page only. | |
96 cairo_surface_t* page_surface_; | |
97 cairo_t* page_context_; | |
98 | |
99 // Buffer stores PDF/PS contents for entire PDF/PS file. | 90 // Buffer stores PDF/PS contents for entire PDF/PS file. |
100 std::string all_pages_; | 91 std::string data_; |
101 | |
102 // Buffer stores PDF/PS contents for current page only. | |
103 std::string current_page_; | |
104 | 92 |
105 DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile); | 93 DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile); |
106 }; | 94 }; |
107 | 95 |
108 } // namespace printing | 96 } // namespace printing |
109 | 97 |
110 #endif // PRINTING_PDF_PS_METAFILE_CAIRO_H_ | 98 #endif // PRINTING_PDF_PS_METAFILE_CAIRO_H_ |
OLD | NEW |