Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: printing/pdf_ps_metafile_cairo.h

Issue 6544028: Create a Factory for NativeMetafile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 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 #include "printing/metafile_factory.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 {
28
29 friend class MetafileFactory;
30
26 public: 31 public:
27 enum FileFormat {
28 PDF,
29 PS,
30 };
31 32
32 PdfPsMetafile(); 33 //TODO dpapad: move it to protected (or delete, PDF is supported only anyway)
33
34 // In the renderer process, callers should also call Init(void) to see if the 34 // In the renderer process, callers should also call Init(void) to see if the
35 // metafile can obtain all necessary rendering resources. 35 // metafile can obtain all necessary rendering resources.
36 // In the browser process, callers should also call Init(const void*, uint32) 36 // In the browser process, callers should also call Init(const void*, uint32)
37 // to initialize the buffer |data_| to use SaveTo(). 37 // to initialize the buffer |data_| to use SaveTo().
38 explicit PdfPsMetafile(const FileFormat& format); 38 explicit PdfPsMetafile(const FileFormat& format);
39 39
40 ~PdfPsMetafile(); 40 virtual ~PdfPsMetafile();
41 41
42 // Initializes to a fresh new metafile. Returns true on success. 42 // Initializes to a fresh new metafile. Returns true on success.
43 // Note: Only call in the renderer to allocate rendering resources. 43 // Note: Only call in the renderer to allocate rendering resources.
44 bool Init(); 44 virtual bool Init();
45 45
46 // Initializes a copy of metafile from PDF/PS stream data. 46 // Initializes a copy of metafile from PDF/PS stream data.
47 // Returns true on success. 47 // Returns true on success.
48 // |src_buffer| should point to the shared memory which stores PDF/PS 48 // |src_buffer| should point to the shared memory which stores PDF/PS
49 // contents generated in the renderer. 49 // contents generated in the renderer.
50 // Note: Only call in the browser to initialize |data_|. 50 // Note: Only call in the browser to initialize |data_|.
51 bool Init(const void* src_buffer, uint32 src_buffer_size); 51 virtual bool Init(const void* src_buffer, uint32 src_buffer_size);
52 52
53 // Sets raw PS/PDF data for the document. This is used when a cairo drawing 53 // Sets raw PS/PDF data for the document. This is used when a cairo drawing
54 // surface has already been created. This method will cause all subsequent 54 // surface has already been created. This method will cause all subsequent
55 // drawing on the surface to be discarded (in Close()). If Init() has not yet 55 // drawing on the surface to be discarded (in Close()). If Init() has not yet
56 // been called this method simply calls the second version of Init. 56 // been called this method simply calls the second version of Init.
57 bool SetRawData(const void* src_buffer, uint32 src_buffer_size); 57 virtual bool SetRawData(const void* src_buffer, uint32 src_buffer_size);
58 58
59 FileFormat GetFileFormat() const { return format_; } 59 virtual FileFormat GetFileFormat() const { return format_; }
60 60
61 // Prepares a new cairo surface/context for rendering a new page. 61 // Prepares a new cairo surface/context for rendering a new page.
62 // The unit is in point (=1/72 in). 62 // The unit is in point (=1/72 in).
63 // Returns NULL when failed. 63 // Returns NULL when failed.
64 cairo_t* StartPage(double width_in_points, 64 virtual cairo_t* StartPage(double width_in_points,
65 double height_in_points, 65 double height_in_points,
66 double margin_top_in_points, 66 double margin_top_in_points,
67 double margin_right_in_points, 67 double margin_right_in_points,
68 double margin_bottom_in_points, 68 double margin_bottom_in_points,
69 double margin_left_in_points); 69 double margin_left_in_points);
70 70
71 // Destroys the surface and the context used in rendering current page. 71 // Destroys the surface and the context used in rendering current page.
72 // The results of current page will be appended into buffer |data_|. 72 // The results of current page will be appended into buffer |data_|.
73 // Returns true on success. 73 // Returns true on success.
74 bool FinishPage(); 74 virtual bool FinishPage();
75 75
76 // Closes resulting PDF/PS file. No further rendering is allowed. 76 // Closes resulting PDF/PS file. No further rendering is allowed.
77 void Close(); 77 virtual void Close();
78 78
79 // Returns size of PDF/PS contents stored in buffer |data_|. 79 // Returns size of PDF/PS contents stored in buffer |data_|.
80 // This function should ONLY be called after PDF/PS file is closed. 80 // This function should ONLY be called after PDF/PS file is closed.
81 uint32 GetDataSize() const; 81 virtual uint32 GetDataSize() const;
82 82
83 // Copies PDF/PS contents stored in buffer |data_| into |dst_buffer|. 83 // Copies PDF/PS contents stored in buffer |data_| into |dst_buffer|.
84 // This function should ONLY be called after PDF/PS file is closed. 84 // This function should ONLY be called after PDF/PS file is closed.
85 // Returns true only when success. 85 // Returns true only when success.
86 bool GetData(void* dst_buffer, uint32 dst_buffer_size) const; 86 virtual bool GetData(void* dst_buffer, uint32 dst_buffer_size) const;
87 87
88 // Saves PDF/PS contents stored in buffer |data_| into the file 88 // Saves PDF/PS contents stored in buffer |data_| into the file
89 // associated with |fd|. 89 // associated with |fd|.
90 // This function should ONLY be called after PDF/PS file is closed. 90 // This function should ONLY be called after PDF/PS file is closed.
91 bool SaveTo(const base::FileDescriptor& fd) const; 91 virtual bool SaveTo(const base::FileDescriptor& fd) const;
92 92
93 // The hardcoded margins, in points. These values are based on 72 dpi, 93 // The hardcoded margins, in points. These values are based on 72 dpi,
94 // with 0.25 margins on top, left, and right, and 0.56 on bottom. 94 // with 0.25 margins on top, left, and right, and 0.56 on bottom.
95 static const double kTopMarginInInch; 95 static const double kTopMarginInInch;
96 static const double kRightMarginInInch; 96 static const double kRightMarginInInch;
97 static const double kBottomMarginInInch; 97 static const double kBottomMarginInInch;
98 static const double kLeftMarginInInch; 98 static const double kLeftMarginInInch;
99 99
100 // Returns the PdfPsMetafile object that owns the given context. Returns NULL 100 // Returns the PdfPsMetafile object that owns the given context. Returns NULL
101 // if the context was not created by a PdfPdMetafile object. 101 // if the context was not created by a PdfPdMetafile object.
102 static PdfPsMetafile* FromCairoContext(cairo_t* context); 102 static PdfPsMetafile* FromCairoContext(cairo_t* context);
103 103
104 protected:
105 PdfPsMetafile();
106
104 private: 107 private:
105 // Cleans up all resources. 108 // Cleans up all resources.
106 void CleanUpAll(); 109 virtual void CleanUpAll();
107 110
108 FileFormat format_; 111 FileFormat format_;
109 112
110 // Cairo surface and context for entire PDF/PS file. 113 // Cairo surface and context for entire PDF/PS file.
111 cairo_surface_t* surface_; 114 cairo_surface_t* surface_;
112 cairo_t* context_; 115 cairo_t* context_;
113 116
114 // Buffer stores PDF/PS contents for entire PDF/PS file. 117 // Buffer stores PDF/PS contents for entire PDF/PS file.
115 std::string data_; 118 std::string data_;
116 // Buffer stores raw PDF/PS contents set by SetRawPageData. 119 // Buffer stores raw PDF/PS contents set by SetRawPageData.
117 std::string raw_override_data_; 120 std::string raw_override_data_;
118 121
119 DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile); 122 DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile);
120 }; 123 };
121 124
122 } // namespace printing 125 } // namespace printing
123 126
124 #endif // PRINTING_PDF_PS_METAFILE_CAIRO_H_ 127 #endif // PRINTING_PDF_PS_METAFILE_CAIRO_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698