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