OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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_PS_METAFILE_LINUX_H_ | |
6 #define PRINTING_PDF_PS_METAFILE_LINUX_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | |
11 | |
12 typedef struct _cairo_surface cairo_surface_t; | |
13 typedef struct _cairo cairo_t; | |
14 | |
15 namespace base { | |
16 class FileDescriptor; | |
17 } | |
18 | |
19 class FilePath; | |
20 | |
21 namespace printing { | |
22 | |
23 // This class uses Cairo graphics library to generate PostScript/PDF stream | |
24 // and stores rendering results in a string buffer. | |
25 class PdfPsMetafile { | |
26 public: | |
27 enum FileFormat { | |
28 PDF, | |
29 PS, | |
30 }; | |
31 | |
32 // In the renderer process, callers should also call Init(void) to see if the | |
33 // metafile can obtain all necessary rendering resources. | |
34 // In the browser process, callers should also call Init(const void*, size_t) | |
35 // to initialize the buffer |all_pages_| to use SaveTo(). | |
36 explicit PdfPsMetafile(const FileFormat& format); | |
37 | |
38 ~PdfPsMetafile(); | |
39 | |
40 // Initializes to a fresh new metafile. Returns true on success. | |
41 // Note: Only call in the renderer to allocate rendering resources. | |
42 bool Init(); | |
43 | |
44 // Initializes a copy of metafile from PDF/PS stream data. | |
45 // Returns true on success. | |
46 // |src_buffer| should point to the shared memory which stores PDF/PS | |
47 // contents generated in the renderer. | |
48 // Note: Only call in the browser to initialize |all_pages_|. | |
49 bool Init(const void* src_buffer, size_t src_buffer_size); | |
50 | |
51 FileFormat GetFileFormat() const { return format_; } | |
52 | |
53 // Prepares a new cairo surface/context for rendering a new page. | |
54 // The unit is in point (=1/72 in). | |
55 // Returns NULL when failed. | |
56 cairo_t* StartPage(double width, double height); | |
57 | |
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_|. | |
60 // Returns true on success | |
61 // TODO(myhuang): I plan to also do page setup here (margins, the header | |
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 | |
68 // Closes resulting PDF/PS file. No further rendering is allowed. | |
69 void Close(); | |
70 | |
71 // Returns size of PDF/PS contents stored in buffer |all_pages_|. | |
72 // This function should ONLY be called after PDF/PS file is closed. | |
73 unsigned int GetDataSize() const; | |
74 | |
75 // Copies PDF/PS contents stored in buffer |all_pages_| into |dst_buffer|. | |
76 // This function should ONLY be called after PDF/PS file is closed. | |
77 // Returns true only when success. | |
78 bool GetData(void* dst_buffer, size_t dst_buffer_size) const; | |
79 | |
80 // Saves PDF/PS contents stored in buffer |all_pages_| into the file | |
81 // associated with |fd|. | |
82 // This function should ONLY be called after PDF/PS file is closed. | |
83 bool SaveTo(const base::FileDescriptor& fd) const; | |
84 | |
85 private: | |
86 // Cleans up all resources. | |
87 void CleanUpAll(); | |
88 | |
89 FileFormat format_; | |
90 | |
91 // Cairo surface and context for entire PDF/PS file. | |
92 cairo_surface_t* surface_; | |
93 cairo_t* context_; | |
94 | |
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. | |
100 std::string all_pages_; | |
101 | |
102 // Buffer stores PDF/PS contents for current page only. | |
103 std::string current_page_; | |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile); | |
106 }; | |
107 | |
108 } // namespace printing | |
109 | |
110 #endif // PRINTING_PDF_PS_METAFILE_LINUX_H_ | |
OLD | NEW |