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 <cairo.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 |
| 14 class FilePath; |
| 15 |
| 16 namespace printing { |
| 17 |
| 18 // This class uses Cairo graphics library to generate PostScript/PDF stream |
| 19 // and stores rendering results in a string buffer. |
| 20 class PdfPsMetafile { |
| 21 public: |
| 22 enum FileFormat { |
| 23 PDF, |
| 24 PS, |
| 25 }; |
| 26 |
| 27 // The constructor we should use in the renderer process. |
| 28 explicit PdfPsMetafile(const FileFormat& format); |
| 29 |
| 30 // The constructor we should use in the browser process. |
| 31 // |src_buffer| should point to the shared memory which stores PDF/PS |
| 32 // contents generated in the renderer. |
| 33 PdfPsMetafile(const FileFormat& format, |
| 34 const void* src_buffer, |
| 35 size_t src_buffer_size); |
| 36 |
| 37 ~PdfPsMetafile(); |
| 38 |
| 39 FileFormat GetFileFormat() { return format_; } |
| 40 |
| 41 // Prepares a new cairo surface/context for rendering a new page. |
| 42 bool StartPage(double width, double height); // The unit is pt (=1/72 in). |
| 43 |
| 44 // Returns the Cairo context for rendering current page. |
| 45 cairo_t* GetPageContext() const { return page_context_; } |
| 46 |
| 47 // Destroys the surface and the context used in rendering current page. |
| 48 // The results of current page will be appended into buffer |all_pages_|. |
| 49 // TODO(myhuang): I plan to also do page setup here (margins, the header |
| 50 // and the footer). At this moment, only pre-defined margins for US letter |
| 51 // paper are hard-coded here. |
| 52 // |shrink| decides the scaling factor to fit raw printing results into |
| 53 // printable area. |
| 54 void FinishPage(float shrink); |
| 55 |
| 56 // Closes resulting PDF/PS file. No further rendering is allowed. |
| 57 void Close(); |
| 58 |
| 59 // Returns size of PDF/PS contents stored in buffer |all_pages_|. |
| 60 // This function should ONLY be called after PDF/PS file is closed. |
| 61 unsigned int GetDataSize() const; |
| 62 |
| 63 // Copies PDF/PS contents stored in buffer |all_pages_| into |dst_buffer|. |
| 64 // This function should ONLY be called after PDF/PS file is closed. |
| 65 void GetData(void* dst_buffer, size_t dst_buffer_size) const; |
| 66 |
| 67 // Saves PDF/PS contents stored in buffer |all_pages_| into |filename| on |
| 68 // the disk. |
| 69 // This function should ONLY be called after PDF/PS file is closed. |
| 70 bool SaveTo(const FilePath& filename) const; |
| 71 |
| 72 private: |
| 73 // Callback function for Cairo to write PDF/PS stream. |
| 74 // |dst_buffer| is actually a pointer of type `std::string*`. |
| 75 static cairo_status_t WriteCairoStream(void* dst_buffer, |
| 76 const unsigned char* src_data, |
| 77 unsigned int src_data_length); |
| 78 |
| 79 // Convenient function to test if |surface| is valid. |
| 80 bool IsSurfaceValid(cairo_surface_t* surface) const; |
| 81 |
| 82 // Convenient function to test if |context| is valid. |
| 83 bool IsContextValid(cairo_t* context) const; |
| 84 |
| 85 FileFormat format_; |
| 86 |
| 87 // Cairo surface and context for entire PDF/PS file. |
| 88 cairo_surface_t* surface_; |
| 89 cairo_t* context_; |
| 90 |
| 91 // Cairo surface and context for current page only. |
| 92 cairo_surface_t* page_surface_; |
| 93 cairo_t* page_context_; |
| 94 |
| 95 // Buffer stores PDF/PS contents for entire PDF/PS file. |
| 96 std::string all_pages_; |
| 97 |
| 98 // Buffer stores PDF/PS contents for current page only. |
| 99 std::string current_page_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(PdfPsMetafile); |
| 102 }; |
| 103 |
| 104 } // namespace printing |
| 105 |
| 106 #endif // PRINTING_PDF_PS_METAFILE_LINUX_H_ |
| 107 |
OLD | NEW |