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
3 // found in the LICENSE file.
5 #ifndef PRINTING_PDF_PS_METAFILE_CAIRO_H_
6 #define PRINTING_PDF_PS_METAFILE_CAIRO_H_
10 #include "base/basictypes.h"
12 typedef struct _cairo_surface cairo_surface_t
;
13 typedef struct _cairo cairo_t
;
16 struct FileDescriptor
;
23 // This class uses Cairo graphics library to generate PostScript/PDF stream
24 // and stores rendering results in a string buffer.
34 // In the renderer process, callers should also call Init(void) to see if the
35 // metafile can obtain all necessary rendering resources.
36 // In the browser process, callers should also call Init(const void*, uint32)
37 // to initialize the buffer |data_| to use SaveTo().
38 explicit PdfPsMetafile(const FileFormat
& format
);
42 // Initializes to a fresh new metafile. Returns true on success.
43 // Note: Only call in the renderer to allocate rendering resources.
46 // Initializes a copy of metafile from PDF/PS stream data.
47 // Returns true on success.
48 // |src_buffer| should point to the shared memory which stores PDF/PS
49 // contents generated in the renderer.
50 // Note: Only call in the browser to initialize |data_|.
51 bool Init(const void* src_buffer
, uint32 src_buffer_size
);
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
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.
57 bool SetRawData(const void* src_buffer
, uint32 src_buffer_size
);
59 FileFormat
GetFileFormat() const { return format_
; }
61 // Prepares a new cairo surface/context for rendering a new page.
62 // The unit is in point (=1/72 in).
63 // Returns NULL when failed.
64 cairo_t
* StartPage(double width_in_points
,
65 double height_in_points
,
66 double margin_top_in_points
,
67 double margin_right_in_points
,
68 double margin_bottom_in_points
,
69 double margin_left_in_points
);
71 // Destroys the surface and the context used in rendering current page.
72 // The results of current page will be appended into buffer |data_|.
73 // Returns true on success.
76 // Closes resulting PDF/PS file. No further rendering is allowed.
79 // Returns size of PDF/PS contents stored in buffer |data_|.
80 // This function should ONLY be called after PDF/PS file is closed.
81 uint32
GetDataSize() const;
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.
85 // Returns true only when success.
86 bool GetData(void* dst_buffer
, uint32 dst_buffer_size
) const;
88 // Saves PDF/PS contents stored in buffer |data_| into the file
89 // associated with |fd|.
90 // This function should ONLY be called after PDF/PS file is closed.
91 bool SaveTo(const base::FileDescriptor
& fd
) const;
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.
95 static const double kTopMarginInInch
;
96 static const double kRightMarginInInch
;
97 static const double kBottomMarginInInch
;
98 static const double kLeftMarginInInch
;
100 // Returns the PdfPsMetafile object that owns the given context. Returns NULL
101 // if the context was not created by a PdfPdMetafile object.
102 static PdfPsMetafile
* FromCairoContext(cairo_t
* context
);
105 // Cleans up all resources.
110 // Cairo surface and context for entire PDF/PS file.
111 cairo_surface_t
* surface_
;
114 // Buffer stores PDF/PS contents for entire PDF/PS file.
116 // Buffer stores raw PDF/PS contents set by SetRawPageData.
117 std::string raw_override_data_
;
119 DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile
);
122 } // namespace printing
124 #endif // PRINTING_PDF_PS_METAFILE_CAIRO_H_